1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
261 -- Refined_State Post_Class
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
322 -- Create_Generic_Contract
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
340 -- Requires_Delayed_Save
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
345 -- Save_Global_References_In_Contract
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
353 -- Instantiate_Subprogram_Contract
355 Circularity_Detected : Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
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 formal into this local package. The result is a
384 -- a 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 formal. 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 -- Add_Pending_Instantiation --
1030 --------------------------------
1032 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1035 -- Add to the instantiation node and the corresponding unit declaration
1036 -- the current values of global flags to be used when analyzing the
1039 Pending_Instantiations.Append
1040 ((Inst_Node => Inst,
1041 Act_Decl => Act_Decl,
1042 Expander_Status => Expander_Active,
1043 Current_Sem_Unit => Current_Sem_Unit,
1044 Scope_Suppress => Scope_Suppress,
1045 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1046 Version => Ada_Version,
1047 Version_Pragma => Ada_Version_Pragma,
1048 Warnings => Save_Warnings,
1049 SPARK_Mode => SPARK_Mode,
1050 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1051 end Add_Pending_Instantiation;
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1066 function Analyze_Associations
1069 F_Copy : List_Id) return List_Id
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1084 Saved_Formal : Node_Id;
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1106 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1107 -- Warn if an actual fixed-point type has user-defined arithmetic
1108 -- operations, but there is no corresponding formal in the generic,
1109 -- in which case the predefined operations will be used. This merits
1110 -- a warning because of the special semantics of fixed point ops.
1112 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1113 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1114 -- cannot have a named association for it. AI05-0025 extends this rule
1115 -- to formals of formal packages by AI05-0025, and it also applies to
1116 -- box-initialized formals.
1118 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1119 -- Determine whether the parameter types and the return type of Subp
1120 -- are fully defined at the point of instantiation.
1122 function Matching_Actual
1124 A_F : Entity_Id) return Node_Id;
1125 -- Find actual that corresponds to a given a formal parameter. If the
1126 -- actuals are positional, return the next one, if any. If the actuals
1127 -- are named, scan the parameter associations to find the right one.
1128 -- A_F is the corresponding entity in the analyzed generic, which is
1129 -- placed on the selector name for ASIS use.
1131 -- In Ada 2005, a named association may be given with a box, in which
1132 -- case Matching_Actual sets Found_Assoc to the generic association,
1133 -- but return Empty for the actual itself. In this case the code below
1134 -- creates a corresponding declaration for the formal.
1136 function Partial_Parameterization return Boolean;
1137 -- Ada 2005: if no match is found for a given formal, check if the
1138 -- association for it includes a box, or whether the associations
1139 -- include an Others clause.
1141 procedure Process_Default (F : Entity_Id);
1142 -- Add a copy of the declaration of generic formal F to the list of
1143 -- associations, and add an explicit box association for F if there
1144 -- is none yet, and the default comes from an Others_Choice.
1146 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1147 -- Determine whether Subp renames one of the subprograms defined in the
1148 -- generated package Standard.
1150 procedure Set_Analyzed_Formal;
1151 -- Find the node in the generic copy that corresponds to a given formal.
1152 -- The semantic information on this node is used to perform legality
1153 -- checks on the actuals. Because semantic analysis can introduce some
1154 -- anonymous entities or modify the declaration node itself, the
1155 -- correspondence between the two lists is not one-one. In addition to
1156 -- anonymous types, the presence a formal equality will introduce an
1157 -- implicit declaration for the corresponding inequality.
1159 ----------------------------------------
1160 -- Check_Overloaded_Formal_Subprogram --
1161 ----------------------------------------
1163 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1164 Temp_Formal : Entity_Id;
1167 Temp_Formal := First (Formals);
1168 while Present (Temp_Formal) loop
1169 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1170 and then Temp_Formal /= Formal
1172 Chars (Defining_Unit_Name (Specification (Formal))) =
1173 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1175 if Present (Found_Assoc) then
1177 ("named association not allowed for overloaded formal",
1182 ("named association not allowed for overloaded formal",
1186 Abandon_Instantiation (Instantiation_Node);
1191 end Check_Overloaded_Formal_Subprogram;
1193 -------------------------------
1194 -- Check_Fixed_Point_Actual --
1195 -------------------------------
1197 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1198 Typ : constant Entity_Id := Entity (Actual);
1199 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 -- Locate primitive operations of the type that are arithmetic
1208 Elem := First_Elmt (Prims);
1209 while Present (Elem) loop
1210 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1212 -- Check whether the generic unit has a formal subprogram of
1213 -- the same name. This does not check types but is good enough
1214 -- to justify a warning.
1216 Formal := First_Non_Pragma (Formals);
1217 Op := Alias (Node (Elem));
1219 while Present (Formal) loop
1220 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1221 and then Chars (Defining_Entity (Formal)) =
1226 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1232 -- Locate corresponding actual, and check whether it
1233 -- includes a fixed-point type.
1235 Assoc := First (Assoc_List);
1236 while Present (Assoc) loop
1238 Nkind (Assoc) = N_Package_Renaming_Declaration
1239 and then Chars (Defining_Unit_Name (Assoc)) =
1240 Chars (Defining_Identifier (Formal));
1245 if Present (Assoc) then
1247 -- If formal package declares a fixed-point type,
1248 -- and the user-defined operator is derived from
1249 -- a generic instance package, the fixed-point type
1250 -- does not use the corresponding predefined op.
1252 Ent := First_Entity (Entity (Name (Assoc)));
1253 while Present (Ent) loop
1254 if Is_Fixed_Point_Type (Ent)
1255 and then Present (Op)
1256 and then Is_Generic_Instance (Scope (Op))
1271 Error_Msg_Sloc := Sloc (Node (Elem));
1273 ("?instance uses predefined operation, not primitive "
1274 & "operation&#", Actual, Node (Elem));
1280 end Check_Fixed_Point_Actual;
1282 -------------------------------
1283 -- Has_Fully_Defined_Profile --
1284 -------------------------------
1286 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1287 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1288 -- Determine whethet type Typ is fully defined
1290 ---------------------------
1291 -- Is_Fully_Defined_Type --
1292 ---------------------------
1294 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1296 -- A private type without a full view is not fully defined
1298 if Is_Private_Type (Typ)
1299 and then No (Full_View (Typ))
1303 -- An incomplete type is never fully defined
1305 elsif Is_Incomplete_Type (Typ) then
1308 -- All other types are fully defined
1313 end Is_Fully_Defined_Type;
1315 -- Local declarations
1319 -- Start of processing for Has_Fully_Defined_Profile
1322 -- Check the parameters
1324 Param := First_Formal (Subp);
1325 while Present (Param) loop
1326 if not Is_Fully_Defined_Type (Etype (Param)) then
1330 Next_Formal (Param);
1333 -- Check the return type
1335 return Is_Fully_Defined_Type (Etype (Subp));
1336 end Has_Fully_Defined_Profile;
1338 ---------------------
1339 -- Matching_Actual --
1340 ---------------------
1342 function Matching_Actual
1344 A_F : Entity_Id) return Node_Id
1350 Is_Named_Assoc := False;
1352 -- End of list of purely positional parameters
1354 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1355 Found_Assoc := Empty;
1358 -- Case of positional parameter corresponding to current formal
1360 elsif No (Selector_Name (Actual)) then
1361 Found_Assoc := Actual;
1362 Act := Explicit_Generic_Actual_Parameter (Actual);
1363 Num_Matched := Num_Matched + 1;
1366 -- Otherwise scan list of named actuals to find the one with the
1367 -- desired name. All remaining actuals have explicit names.
1370 Is_Named_Assoc := True;
1371 Found_Assoc := Empty;
1375 while Present (Actual) loop
1376 if Nkind (Actual) = N_Others_Choice then
1377 Found_Assoc := Empty;
1380 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1381 Set_Entity (Selector_Name (Actual), A_F);
1382 Set_Etype (Selector_Name (Actual), Etype (A_F));
1383 Generate_Reference (A_F, Selector_Name (Actual));
1385 Found_Assoc := Actual;
1386 Act := Explicit_Generic_Actual_Parameter (Actual);
1387 Num_Matched := Num_Matched + 1;
1395 -- Reset for subsequent searches. In most cases the named
1396 -- associations are in order. If they are not, we reorder them
1397 -- to avoid scanning twice the same actual. This is not just a
1398 -- question of efficiency: there may be multiple defaults with
1399 -- boxes that have the same name. In a nested instantiation we
1400 -- insert actuals for those defaults, and cannot rely on their
1401 -- names to disambiguate them.
1403 if Actual = First_Named then
1406 elsif Present (Actual) then
1407 Insert_Before (First_Named, Remove_Next (Prev));
1410 Actual := First_Named;
1413 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1414 Set_Used_As_Generic_Actual (Entity (Act));
1418 end Matching_Actual;
1420 ------------------------------
1421 -- Partial_Parameterization --
1422 ------------------------------
1424 function Partial_Parameterization return Boolean is
1426 return Others_Present
1427 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1428 end Partial_Parameterization;
1430 ---------------------
1431 -- Process_Default --
1432 ---------------------
1434 procedure Process_Default (F : Entity_Id) is
1435 Loc : constant Source_Ptr := Sloc (I_Node);
1436 F_Id : constant Entity_Id := Defining_Entity (F);
1442 -- Append copy of formal declaration to associations, and create new
1443 -- defining identifier for it.
1445 Decl := New_Copy_Tree (F);
1446 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1448 if Nkind (F) in N_Formal_Subprogram_Declaration then
1449 Set_Defining_Unit_Name (Specification (Decl), Id);
1452 Set_Defining_Identifier (Decl, Id);
1455 Append (Decl, Assoc_List);
1457 if No (Found_Assoc) then
1459 Make_Generic_Association (Loc,
1461 New_Occurrence_Of (Id, Loc),
1462 Explicit_Generic_Actual_Parameter => Empty);
1463 Set_Box_Present (Default);
1464 Append (Default, Default_Formals);
1466 end Process_Default;
1468 ---------------------------------
1469 -- Renames_Standard_Subprogram --
1470 ---------------------------------
1472 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1477 while Present (Id) loop
1478 if Scope (Id) = Standard_Standard then
1486 end Renames_Standard_Subprogram;
1488 -------------------------
1489 -- Set_Analyzed_Formal --
1490 -------------------------
1492 procedure Set_Analyzed_Formal is
1496 while Present (Analyzed_Formal) loop
1497 Kind := Nkind (Analyzed_Formal);
1499 case Nkind (Formal) is
1500 when N_Formal_Subprogram_Declaration =>
1501 exit when Kind in N_Formal_Subprogram_Declaration
1504 (Defining_Unit_Name (Specification (Formal))) =
1506 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1508 when N_Formal_Package_Declaration =>
1509 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1510 N_Generic_Package_Declaration,
1511 N_Package_Declaration);
1513 when N_Use_Package_Clause
1520 -- Skip freeze nodes, and nodes inserted to replace
1521 -- unrecognized pragmas.
1524 Kind not in N_Formal_Subprogram_Declaration
1525 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1529 and then Chars (Defining_Identifier (Formal)) =
1530 Chars (Defining_Identifier (Analyzed_Formal));
1533 Next (Analyzed_Formal);
1535 end Set_Analyzed_Formal;
1537 -- Start of processing for Analyze_Associations
1540 Actuals := Generic_Associations (I_Node);
1542 if Present (Actuals) then
1544 -- Check for an Others choice, indicating a partial parameterization
1545 -- for a formal package.
1547 Actual := First (Actuals);
1548 while Present (Actual) loop
1549 if Nkind (Actual) = N_Others_Choice then
1550 Others_Present := True;
1551 Others_Choice := Actual;
1553 if Present (Next (Actual)) then
1554 Error_Msg_N ("others must be last association", Actual);
1557 -- This subprogram is used both for formal packages and for
1558 -- instantiations. For the latter, associations must all be
1561 if Nkind (I_Node) /= N_Formal_Package_Declaration
1562 and then Comes_From_Source (I_Node)
1565 ("others association not allowed in an instance",
1569 -- In any case, nothing to do after the others association
1573 elsif Box_Present (Actual)
1574 and then Comes_From_Source (I_Node)
1575 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1578 ("box association not allowed in an instance", Actual);
1584 -- If named associations are present, save first named association
1585 -- (it may of course be Empty) to facilitate subsequent name search.
1587 First_Named := First (Actuals);
1588 while Present (First_Named)
1589 and then Nkind (First_Named) /= N_Others_Choice
1590 and then No (Selector_Name (First_Named))
1592 Num_Actuals := Num_Actuals + 1;
1597 Named := First_Named;
1598 while Present (Named) loop
1599 if Nkind (Named) /= N_Others_Choice
1600 and then No (Selector_Name (Named))
1602 Error_Msg_N ("invalid positional actual after named one", Named);
1603 Abandon_Instantiation (Named);
1606 -- A named association may lack an actual parameter, if it was
1607 -- introduced for a default subprogram that turns out to be local
1608 -- to the outer instantiation. If it has a box association it must
1609 -- correspond to some formal in the generic.
1611 if Nkind (Named) /= N_Others_Choice
1612 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1613 or else Box_Present (Named))
1615 Num_Actuals := Num_Actuals + 1;
1621 if Present (Formals) then
1622 Formal := First_Non_Pragma (Formals);
1623 Analyzed_Formal := First_Non_Pragma (F_Copy);
1625 if Present (Actuals) then
1626 Actual := First (Actuals);
1628 -- All formals should have default values
1634 while Present (Formal) loop
1635 Set_Analyzed_Formal;
1636 Saved_Formal := Next_Non_Pragma (Formal);
1638 case Nkind (Formal) is
1639 when N_Formal_Object_Declaration =>
1642 (Defining_Identifier (Formal),
1643 Defining_Identifier (Analyzed_Formal));
1645 if No (Match) and then Partial_Parameterization then
1646 Process_Default (Formal);
1650 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1653 -- For a defaulted in_parameter, create an entry in the
1654 -- the list of defaulted actuals, for GNATProve use. Do
1655 -- not included these defaults for an instance nested
1656 -- within a generic, because the defaults are also used
1657 -- in the analysis of the enclosing generic, and only
1658 -- defaulted subprograms are relevant there.
1660 if No (Match) and then not Inside_A_Generic then
1661 Append_To (Default_Actuals,
1662 Make_Generic_Association (Sloc (I_Node),
1665 (Defining_Identifier (Formal), Sloc (I_Node)),
1666 Explicit_Generic_Actual_Parameter =>
1667 New_Copy_Tree (Default_Expression (Formal))));
1671 -- If the object is a call to an expression function, this
1672 -- is a freezing point for it.
1674 if Is_Entity_Name (Match)
1675 and then Present (Entity (Match))
1677 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1678 = N_Expression_Function
1680 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1683 when N_Formal_Type_Declaration =>
1686 (Defining_Identifier (Formal),
1687 Defining_Identifier (Analyzed_Formal));
1690 if Partial_Parameterization then
1691 Process_Default (Formal);
1694 Error_Msg_Sloc := Sloc (Gen_Unit);
1697 Instantiation_Node, Defining_Identifier (Formal));
1699 ("\in instantiation of & declared#",
1700 Instantiation_Node, Gen_Unit);
1701 Abandon_Instantiation (Instantiation_Node);
1708 (Formal, Match, Analyzed_Formal, Assoc_List),
1711 -- Warn when an actual is a fixed-point with user-
1712 -- defined promitives. The warning is superfluous
1713 -- if the fornal is private, because there can be
1714 -- no arithmetic operations in the generic so there
1715 -- no danger of confusion.
1717 if Is_Fixed_Point_Type (Entity (Match))
1718 and then not Is_Private_Type
1719 (Defining_Identifier (Analyzed_Formal))
1721 Check_Fixed_Point_Actual (Match);
1724 -- An instantiation is a freeze point for the actuals,
1725 -- unless this is a rewritten formal package, or the
1726 -- formal is an Ada 2012 formal incomplete type.
1728 if Nkind (I_Node) = N_Formal_Package_Declaration
1730 (Ada_Version >= Ada_2012
1732 Ekind (Defining_Identifier (Analyzed_Formal)) =
1738 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1742 -- A remote access-to-class-wide type is not a legal actual
1743 -- for a generic formal of an access type (E.2.2(17/2)).
1744 -- In GNAT an exception to this rule is introduced when
1745 -- the formal is marked as remote using implementation
1746 -- defined aspect/pragma Remote_Access_Type. In that case
1747 -- the actual must be remote as well.
1749 -- If the current instantiation is the construction of a
1750 -- local copy for a formal package the actuals may be
1751 -- defaulted, and there is no matching actual to check.
1753 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1755 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1756 N_Access_To_Object_Definition
1757 and then Present (Match)
1760 Formal_Ent : constant Entity_Id :=
1761 Defining_Identifier (Analyzed_Formal);
1763 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1764 = Is_Remote_Types (Formal_Ent)
1766 -- Remoteness of formal and actual match
1770 elsif Is_Remote_Types (Formal_Ent) then
1772 -- Remote formal, non-remote actual
1775 ("actual for& must be remote", Match, Formal_Ent);
1778 -- Non-remote formal, remote actual
1781 ("actual for& may not be remote",
1787 when N_Formal_Subprogram_Declaration =>
1790 (Defining_Unit_Name (Specification (Formal)),
1791 Defining_Unit_Name (Specification (Analyzed_Formal)));
1793 -- If the formal subprogram has the same name as another
1794 -- formal subprogram of the generic, then a named
1795 -- association is illegal (12.3(9)). Exclude named
1796 -- associations that are generated for a nested instance.
1799 and then Is_Named_Assoc
1800 and then Comes_From_Source (Found_Assoc)
1802 Check_Overloaded_Formal_Subprogram (Formal);
1805 -- If there is no corresponding actual, this may be case
1806 -- of partial parameterization, or else the formal has a
1807 -- default or a box.
1809 if No (Match) and then Partial_Parameterization then
1810 Process_Default (Formal);
1812 if Nkind (I_Node) = N_Formal_Package_Declaration then
1813 Check_Overloaded_Formal_Subprogram (Formal);
1817 Append_To (Assoc_List,
1818 Instantiate_Formal_Subprogram
1819 (Formal, Match, Analyzed_Formal));
1821 -- An instantiation is a freeze point for the actuals,
1822 -- unless this is a rewritten formal package.
1824 if Nkind (I_Node) /= N_Formal_Package_Declaration
1825 and then Nkind (Match) = N_Identifier
1826 and then Is_Subprogram (Entity (Match))
1828 -- The actual subprogram may rename a routine defined
1829 -- in Standard. Avoid freezing such renamings because
1830 -- subprograms coming from Standard cannot be frozen.
1833 not Renames_Standard_Subprogram (Entity (Match))
1835 -- If the actual subprogram comes from a different
1836 -- unit, it is already frozen, either by a body in
1837 -- that unit or by the end of the declarative part
1838 -- of the unit. This check avoids the freezing of
1839 -- subprograms defined in Standard which are used
1840 -- as generic actuals.
1842 and then In_Same_Code_Unit (Entity (Match), I_Node)
1843 and then Has_Fully_Defined_Profile (Entity (Match))
1845 -- Mark the subprogram as having a delayed freeze
1846 -- since this may be an out-of-order action.
1848 Set_Has_Delayed_Freeze (Entity (Match));
1849 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1853 -- If this is a nested generic, preserve default for later
1854 -- instantiations. We do this as well for GNATProve use,
1855 -- so that the list of generic associations is complete.
1857 if No (Match) and then Box_Present (Formal) then
1859 Subp : constant Entity_Id :=
1861 (Specification (Last (Assoc_List)));
1864 Append_To (Default_Actuals,
1865 Make_Generic_Association (Sloc (I_Node),
1867 New_Occurrence_Of (Subp, Sloc (I_Node)),
1868 Explicit_Generic_Actual_Parameter =>
1869 New_Occurrence_Of (Subp, Sloc (I_Node))));
1873 when N_Formal_Package_Declaration =>
1876 (Defining_Identifier (Formal),
1877 Defining_Identifier (Original_Node (Analyzed_Formal)));
1880 if Partial_Parameterization then
1881 Process_Default (Formal);
1884 Error_Msg_Sloc := Sloc (Gen_Unit);
1887 Instantiation_Node, Defining_Identifier (Formal));
1889 ("\in instantiation of & declared#",
1890 Instantiation_Node, Gen_Unit);
1892 Abandon_Instantiation (Instantiation_Node);
1898 (Instantiate_Formal_Package
1899 (Formal, Match, Analyzed_Formal),
1902 -- Determine whether the actual package needs an explicit
1903 -- freeze node. This is only the case if the actual is
1904 -- declared in the same unit and has a body. Normally
1905 -- packages do not have explicit freeze nodes, and gigi
1906 -- only uses them to elaborate entities in a package
1909 Explicit_Freeze_Check : declare
1910 Actual : constant Entity_Id := Entity (Match);
1911 Gen_Par : Entity_Id;
1913 Needs_Freezing : Boolean;
1916 procedure Check_Generic_Parent;
1917 -- The actual may be an instantiation of a unit
1918 -- declared in a previous instantiation. If that
1919 -- one is also in the current compilation, it must
1920 -- itself be frozen before the actual. The actual
1921 -- may be an instantiation of a generic child unit,
1922 -- in which case the same applies to the instance
1923 -- of the parent which must be frozen before the
1925 -- Should this itself be recursive ???
1927 --------------------------
1928 -- Check_Generic_Parent --
1929 --------------------------
1931 procedure Check_Generic_Parent is
1932 Inst : constant Node_Id :=
1933 Next (Unit_Declaration_Node (Actual));
1939 if Nkind (Parent (Actual)) = N_Package_Specification
1941 Par := Scope (Generic_Parent (Parent (Actual)));
1943 if Is_Generic_Instance (Par) then
1946 -- If the actual is a child generic unit, check
1947 -- whether the instantiation of the parent is
1948 -- also local and must also be frozen now. We
1949 -- must retrieve the instance node to locate the
1950 -- parent instance if any.
1952 elsif Ekind (Par) = E_Generic_Package
1953 and then Is_Child_Unit (Gen_Par)
1954 and then Ekind (Scope (Gen_Par)) =
1957 if Nkind (Inst) = N_Package_Instantiation
1958 and then Nkind (Name (Inst)) =
1961 -- Retrieve entity of parent instance
1963 Par := Entity (Prefix (Name (Inst)));
1972 and then Is_Generic_Instance (Par)
1973 and then Scope (Par) = Current_Scope
1975 (No (Freeze_Node (Par))
1977 not Is_List_Member (Freeze_Node (Par)))
1979 Set_Has_Delayed_Freeze (Par);
1980 Append_Elmt (Par, Actuals_To_Freeze);
1982 end Check_Generic_Parent;
1984 -- Start of processing for Explicit_Freeze_Check
1987 if Present (Renamed_Entity (Actual)) then
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node
1991 (Renamed_Entity (Actual))));
1994 Generic_Parent (Specification
1995 (Unit_Declaration_Node (Actual)));
1998 if not Expander_Active
1999 or else not Has_Completion (Actual)
2000 or else not In_Same_Source_Unit (I_Node, Actual)
2001 or else Is_Frozen (Actual)
2003 (Present (Renamed_Entity (Actual))
2005 not In_Same_Source_Unit
2006 (I_Node, (Renamed_Entity (Actual))))
2011 -- Finally we want to exclude such freeze nodes
2012 -- from statement sequences, which freeze
2013 -- everything before them.
2014 -- Is this strictly necessary ???
2016 Needs_Freezing := True;
2019 while Present (S) loop
2020 if Ekind_In (S, E_Block,
2025 Needs_Freezing := False;
2032 if Needs_Freezing then
2033 Check_Generic_Parent;
2035 -- If the actual is a renaming of a proper
2036 -- instance of the formal package, indicate
2037 -- that it is the instance that must be frozen.
2039 if Nkind (Parent (Actual)) =
2040 N_Package_Renaming_Declaration
2042 Set_Has_Delayed_Freeze
2043 (Renamed_Entity (Actual));
2045 (Renamed_Entity (Actual),
2048 Set_Has_Delayed_Freeze (Actual);
2049 Append_Elmt (Actual, Actuals_To_Freeze);
2053 end Explicit_Freeze_Check;
2056 -- For use type and use package appearing in the generic part,
2057 -- we have already copied them, so we can just move them where
2058 -- they belong (we mustn't recopy them since this would mess up
2059 -- the Sloc values).
2061 when N_Use_Package_Clause
2064 if Nkind (Original_Node (I_Node)) =
2065 N_Formal_Package_Declaration
2067 Append (New_Copy_Tree (Formal), Assoc_List);
2070 Append (Formal, Assoc_List);
2074 raise Program_Error;
2077 Formal := Saved_Formal;
2078 Next_Non_Pragma (Analyzed_Formal);
2081 if Num_Actuals > Num_Matched then
2082 Error_Msg_Sloc := Sloc (Gen_Unit);
2084 if Present (Selector_Name (Actual)) then
2086 ("unmatched actual &", Actual, Selector_Name (Actual));
2088 ("\in instantiation of & declared#", Actual, Gen_Unit);
2091 ("unmatched actual in instantiation of & declared#",
2096 elsif Present (Actuals) then
2098 ("too many actuals in generic instantiation", Instantiation_Node);
2101 -- An instantiation freezes all generic actuals. The only exceptions
2102 -- to this are incomplete types and subprograms which are not fully
2103 -- defined at the point of instantiation.
2106 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2108 while Present (Elmt) loop
2109 Freeze_Before (I_Node, Node (Elmt));
2114 -- If there are default subprograms, normalize the tree by adding
2115 -- explicit associations for them. This is required if the instance
2116 -- appears within a generic.
2118 if not Is_Empty_List (Default_Actuals) then
2123 Default := First (Default_Actuals);
2124 while Present (Default) loop
2125 Mark_Rewrite_Insertion (Default);
2129 if No (Actuals) then
2130 Set_Generic_Associations (I_Node, Default_Actuals);
2132 Append_List_To (Actuals, Default_Actuals);
2137 -- If this is a formal package, normalize the parameter list by adding
2138 -- explicit box associations for the formals that are covered by an
2141 if not Is_Empty_List (Default_Formals) then
2142 Append_List (Default_Formals, Formals);
2146 end Analyze_Associations;
2148 -------------------------------
2149 -- Analyze_Formal_Array_Type --
2150 -------------------------------
2152 procedure Analyze_Formal_Array_Type
2153 (T : in out Entity_Id;
2159 -- Treated like a non-generic array declaration, with additional
2164 if Nkind (Def) = N_Constrained_Array_Definition then
2165 DSS := First (Discrete_Subtype_Definitions (Def));
2166 while Present (DSS) loop
2167 if Nkind_In (DSS, N_Subtype_Indication,
2169 N_Attribute_Reference)
2171 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2178 Array_Type_Declaration (T, Def);
2179 Set_Is_Generic_Type (Base_Type (T));
2181 if Ekind (Component_Type (T)) = E_Incomplete_Type
2182 and then No (Full_View (Component_Type (T)))
2184 Error_Msg_N ("premature usage of incomplete type", Def);
2186 -- Check that range constraint is not allowed on the component type
2187 -- of a generic formal array type (AARM 12.5.3(3))
2189 elsif Is_Internal (Component_Type (T))
2190 and then Present (Subtype_Indication (Component_Definition (Def)))
2191 and then Nkind (Original_Node
2192 (Subtype_Indication (Component_Definition (Def)))) =
2193 N_Subtype_Indication
2196 ("in a formal, a subtype indication can only be "
2197 & "a subtype mark (RM 12.5.3(3))",
2198 Subtype_Indication (Component_Definition (Def)));
2201 end Analyze_Formal_Array_Type;
2203 ---------------------------------------------
2204 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2205 ---------------------------------------------
2207 -- As for other generic types, we create a valid type representation with
2208 -- legal but arbitrary attributes, whose values are never considered
2209 -- static. For all scalar types we introduce an anonymous base type, with
2210 -- the same attributes. We choose the corresponding integer type to be
2211 -- Standard_Integer.
2212 -- Here and in other similar routines, the Sloc of the generated internal
2213 -- type must be the same as the sloc of the defining identifier of the
2214 -- formal type declaration, to provide proper source navigation.
2216 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2220 Loc : constant Source_Ptr := Sloc (Def);
2222 Base : constant Entity_Id :=
2224 (E_Decimal_Fixed_Point_Type,
2226 Sloc (Defining_Identifier (Parent (Def))), 'G');
2228 Int_Base : constant Entity_Id := Standard_Integer;
2229 Delta_Val : constant Ureal := Ureal_1;
2230 Digs_Val : constant Uint := Uint_6;
2232 function Make_Dummy_Bound return Node_Id;
2233 -- Return a properly typed universal real literal to use as a bound
2235 ----------------------
2236 -- Make_Dummy_Bound --
2237 ----------------------
2239 function Make_Dummy_Bound return Node_Id is
2240 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2242 Set_Etype (Bound, Universal_Real);
2244 end Make_Dummy_Bound;
2246 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, Int_Base);
2253 Set_RM_Size (Base, RM_Size (Int_Base));
2254 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2255 Set_Digits_Value (Base, Digs_Val);
2256 Set_Delta_Value (Base, Delta_Val);
2257 Set_Small_Value (Base, Delta_Val);
2258 Set_Scalar_Range (Base,
2260 Low_Bound => Make_Dummy_Bound,
2261 High_Bound => Make_Dummy_Bound));
2263 Set_Is_Generic_Type (Base);
2264 Set_Parent (Base, Parent (Def));
2266 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2267 Set_Etype (T, Base);
2268 Set_Size_Info (T, Int_Base);
2269 Set_RM_Size (T, RM_Size (Int_Base));
2270 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2271 Set_Digits_Value (T, Digs_Val);
2272 Set_Delta_Value (T, Delta_Val);
2273 Set_Small_Value (T, Delta_Val);
2274 Set_Scalar_Range (T, Scalar_Range (Base));
2275 Set_Is_Constrained (T);
2277 Check_Restriction (No_Fixed_Point, Def);
2278 end Analyze_Formal_Decimal_Fixed_Point_Type;
2280 -------------------------------------------
2281 -- Analyze_Formal_Derived_Interface_Type --
2282 -------------------------------------------
2284 procedure Analyze_Formal_Derived_Interface_Type
2289 Loc : constant Source_Ptr := Sloc (Def);
2292 -- Rewrite as a type declaration of a derived type. This ensures that
2293 -- the interface list and primitive operations are properly captured.
2296 Make_Full_Type_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Type_Definition => Def));
2300 Set_Is_Generic_Type (T);
2301 end Analyze_Formal_Derived_Interface_Type;
2303 ---------------------------------
2304 -- Analyze_Formal_Derived_Type --
2305 ---------------------------------
2307 procedure Analyze_Formal_Derived_Type
2312 Loc : constant Source_Ptr := Sloc (Def);
2313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2317 Set_Is_Generic_Type (T);
2319 if Private_Present (Def) then
2321 Make_Private_Extension_Declaration (Loc,
2322 Defining_Identifier => T,
2323 Discriminant_Specifications => Discriminant_Specifications (N),
2324 Unknown_Discriminants_Present => Unk_Disc,
2325 Subtype_Indication => Subtype_Mark (Def),
2326 Interface_List => Interface_List (Def));
2328 Set_Abstract_Present (New_N, Abstract_Present (Def));
2329 Set_Limited_Present (New_N, Limited_Present (Def));
2330 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2334 Make_Full_Type_Declaration (Loc,
2335 Defining_Identifier => T,
2336 Discriminant_Specifications =>
2337 Discriminant_Specifications (Parent (T)),
2339 Make_Derived_Type_Definition (Loc,
2340 Subtype_Indication => Subtype_Mark (Def)));
2342 Set_Abstract_Present
2343 (Type_Definition (New_N), Abstract_Present (Def));
2345 (Type_Definition (New_N), Limited_Present (Def));
2352 if not Is_Composite_Type (T) then
2354 ("unknown discriminants not allowed for elementary types", N);
2356 Set_Has_Unknown_Discriminants (T);
2357 Set_Is_Constrained (T, False);
2361 -- If the parent type has a known size, so does the formal, which makes
2362 -- legal representation clauses that involve the formal.
2364 Set_Size_Known_At_Compile_Time
2365 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2366 end Analyze_Formal_Derived_Type;
2368 ----------------------------------
2369 -- Analyze_Formal_Discrete_Type --
2370 ----------------------------------
2372 -- The operations defined for a discrete types are those of an enumeration
2373 -- type. The size is set to an arbitrary value, for use in analyzing the
2376 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2377 Loc : constant Source_Ptr := Sloc (Def);
2381 Base : constant Entity_Id :=
2383 (E_Floating_Point_Type, Current_Scope,
2384 Sloc (Defining_Identifier (Parent (Def))), 'G');
2388 Set_Ekind (T, E_Enumeration_Subtype);
2389 Set_Etype (T, Base);
2392 Set_Is_Generic_Type (T);
2393 Set_Is_Constrained (T);
2395 -- For semantic analysis, the bounds of the type must be set to some
2396 -- non-static value. The simplest is to create attribute nodes for those
2397 -- bounds, that refer to the type itself. These bounds are never
2398 -- analyzed but serve as place-holders.
2401 Make_Attribute_Reference (Loc,
2402 Attribute_Name => Name_First,
2403 Prefix => New_Occurrence_Of (T, Loc));
2407 Make_Attribute_Reference (Loc,
2408 Attribute_Name => Name_Last,
2409 Prefix => New_Occurrence_Of (T, Loc));
2412 Set_Scalar_Range (T,
2417 Set_Ekind (Base, E_Enumeration_Type);
2418 Set_Etype (Base, Base);
2419 Init_Size (Base, 8);
2420 Init_Alignment (Base);
2421 Set_Is_Generic_Type (Base);
2422 Set_Scalar_Range (Base, Scalar_Range (T));
2423 Set_Parent (Base, Parent (Def));
2424 end Analyze_Formal_Discrete_Type;
2426 ----------------------------------
2427 -- Analyze_Formal_Floating_Type --
2428 ---------------------------------
2430 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2431 Base : constant Entity_Id :=
2433 (E_Floating_Point_Type, Current_Scope,
2434 Sloc (Defining_Identifier (Parent (Def))), 'G');
2437 -- The various semantic attributes are taken from the predefined type
2438 -- Float, just so that all of them are initialized. Their values are
2439 -- never used because no constant folding or expansion takes place in
2440 -- the generic itself.
2443 Set_Ekind (T, E_Floating_Point_Subtype);
2444 Set_Etype (T, Base);
2445 Set_Size_Info (T, (Standard_Float));
2446 Set_RM_Size (T, RM_Size (Standard_Float));
2447 Set_Digits_Value (T, Digits_Value (Standard_Float));
2448 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2449 Set_Is_Constrained (T);
2451 Set_Is_Generic_Type (Base);
2452 Set_Etype (Base, Base);
2453 Set_Size_Info (Base, (Standard_Float));
2454 Set_RM_Size (Base, RM_Size (Standard_Float));
2455 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2456 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2457 Set_Parent (Base, Parent (Def));
2459 Check_Restriction (No_Floating_Point, Def);
2460 end Analyze_Formal_Floating_Type;
2462 -----------------------------------
2463 -- Analyze_Formal_Interface_Type;--
2464 -----------------------------------
2466 procedure Analyze_Formal_Interface_Type
2471 Loc : constant Source_Ptr := Sloc (N);
2476 Make_Full_Type_Declaration (Loc,
2477 Defining_Identifier => T,
2478 Type_Definition => Def);
2482 Set_Is_Generic_Type (T);
2483 end Analyze_Formal_Interface_Type;
2485 ---------------------------------
2486 -- Analyze_Formal_Modular_Type --
2487 ---------------------------------
2489 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2491 -- Apart from their entity kind, generic modular types are treated like
2492 -- signed integer types, and have the same attributes.
2494 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 Set_Ekind (T, E_Modular_Integer_Subtype);
2496 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2498 end Analyze_Formal_Modular_Type;
2500 ---------------------------------------
2501 -- Analyze_Formal_Object_Declaration --
2502 ---------------------------------------
2504 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2505 E : constant Node_Id := Default_Expression (N);
2506 Id : constant Node_Id := Defining_Identifier (N);
2513 -- Determine the mode of the formal object
2515 if Out_Present (N) then
2516 K := E_Generic_In_Out_Parameter;
2518 if not In_Present (N) then
2519 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2523 K := E_Generic_In_Parameter;
2526 if Present (Subtype_Mark (N)) then
2527 Find_Type (Subtype_Mark (N));
2528 T := Entity (Subtype_Mark (N));
2530 -- Verify that there is no redundant null exclusion
2532 if Null_Exclusion_Present (N) then
2533 if not Is_Access_Type (T) then
2535 ("null exclusion can only apply to an access type", N);
2537 elsif Can_Never_Be_Null (T) then
2539 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2543 -- Ada 2005 (AI-423): Formal object with an access definition
2546 Check_Access_Definition (N);
2547 T := Access_Definition
2549 N => Access_Definition (N));
2552 if Ekind (T) = E_Incomplete_Type then
2554 Error_Node : Node_Id;
2557 if Present (Subtype_Mark (N)) then
2558 Error_Node := Subtype_Mark (N);
2560 Check_Access_Definition (N);
2561 Error_Node := Access_Definition (N);
2564 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2568 if K = E_Generic_In_Parameter then
2570 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2572 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2574 ("generic formal of mode IN must not be of limited type", N);
2575 Explain_Limited_Type (T, N);
2578 if Is_Abstract_Type (T) then
2580 ("generic formal of mode IN must not be of abstract type", N);
2584 Preanalyze_Spec_Expression (E, T);
2586 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2588 ("initialization not allowed for limited types", E);
2589 Explain_Limited_Type (T, E);
2596 -- Case of generic IN OUT parameter
2599 -- If the formal has an unconstrained type, construct its actual
2600 -- subtype, as is done for subprogram formals. In this fashion, all
2601 -- its uses can refer to specific bounds.
2606 if (Is_Array_Type (T) and then not Is_Constrained (T))
2607 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2610 Non_Freezing_Ref : constant Node_Id :=
2611 New_Occurrence_Of (Id, Sloc (Id));
2615 -- Make sure the actual subtype doesn't generate bogus freezing
2617 Set_Must_Not_Freeze (Non_Freezing_Ref);
2618 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2619 Insert_Before_And_Analyze (N, Decl);
2620 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2623 Set_Actual_Subtype (Id, T);
2628 ("initialization not allowed for `IN OUT` formals", N);
2632 if Has_Aspects (N) then
2633 Analyze_Aspect_Specifications (N, Id);
2635 end Analyze_Formal_Object_Declaration;
2637 ----------------------------------------------
2638 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2639 ----------------------------------------------
2641 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2645 Loc : constant Source_Ptr := Sloc (Def);
2646 Base : constant Entity_Id :=
2648 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2649 Sloc (Defining_Identifier (Parent (Def))), 'G');
2652 -- The semantic attributes are set for completeness only, their values
2653 -- will never be used, since all properties of the type are non-static.
2656 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2657 Set_Etype (T, Base);
2658 Set_Size_Info (T, Standard_Integer);
2659 Set_RM_Size (T, RM_Size (Standard_Integer));
2660 Set_Small_Value (T, Ureal_1);
2661 Set_Delta_Value (T, Ureal_1);
2662 Set_Scalar_Range (T,
2664 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2665 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2666 Set_Is_Constrained (T);
2668 Set_Is_Generic_Type (Base);
2669 Set_Etype (Base, Base);
2670 Set_Size_Info (Base, Standard_Integer);
2671 Set_RM_Size (Base, RM_Size (Standard_Integer));
2672 Set_Small_Value (Base, Ureal_1);
2673 Set_Delta_Value (Base, Ureal_1);
2674 Set_Scalar_Range (Base, Scalar_Range (T));
2675 Set_Parent (Base, Parent (Def));
2677 Check_Restriction (No_Fixed_Point, Def);
2678 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2680 ----------------------------------------
2681 -- Analyze_Formal_Package_Declaration --
2682 ----------------------------------------
2684 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2685 Gen_Id : constant Node_Id := Name (N);
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2690 Gen_Unit : Entity_Id;
2693 Vis_Prims_List : Elist_Id := No_Elist;
2694 -- List of primitives made temporarily visible in the instantiation
2695 -- to match the visibility of the formal type.
2697 function Build_Local_Package return Node_Id;
2698 -- The formal package is rewritten so that its parameters are replaced
2699 -- with corresponding declarations. For parameters with bona fide
2700 -- associations these declarations are created by Analyze_Associations
2701 -- as for a regular instantiation. For boxed parameters, we preserve
2702 -- the formal declarations and analyze them, in order to introduce
2703 -- entities of the right kind in the environment of the formal.
2705 -------------------------
2706 -- Build_Local_Package --
2707 -------------------------
2709 function Build_Local_Package return Node_Id is
2711 Pack_Decl : Node_Id;
2714 -- Within the formal, the name of the generic package is a renaming
2715 -- of the formal (as for a regular instantiation).
2718 Make_Package_Declaration (Loc,
2721 (Specification (Original_Node (Gen_Decl)),
2722 Empty, Instantiating => True));
2725 Make_Package_Renaming_Declaration (Loc,
2726 Defining_Unit_Name =>
2727 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2728 Name => New_Occurrence_Of (Formal, Loc));
2730 if Nkind (Gen_Id) = N_Identifier
2731 and then Chars (Gen_Id) = Chars (Pack_Id)
2734 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2737 -- If the formal is declared with a box, or with an others choice,
2738 -- create corresponding declarations for all entities in the formal
2739 -- part, so that names with the proper types are available in the
2740 -- specification of the formal package.
2742 -- On the other hand, if there are no associations, then all the
2743 -- formals must have defaults, and this will be checked by the
2744 -- call to Analyze_Associations.
2747 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2750 Formal_Decl : Node_Id;
2753 -- TBA : for a formal package, need to recurse ???
2758 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2759 while Present (Formal_Decl) loop
2763 (Formal_Decl, Empty, Instantiating => True));
2768 -- If generic associations are present, use Analyze_Associations to
2769 -- create the proper renaming declarations.
2773 Act_Tree : constant Node_Id :=
2775 (Original_Node (Gen_Decl), Empty,
2776 Instantiating => True);
2779 Generic_Renamings.Set_Last (0);
2780 Generic_Renamings_HTable.Reset;
2781 Instantiation_Node := N;
2784 Analyze_Associations
2785 (I_Node => Original_Node (N),
2786 Formals => Generic_Formal_Declarations (Act_Tree),
2787 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2789 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2793 Append (Renaming, To => Decls);
2795 -- Add generated declarations ahead of local declarations in
2798 if No (Visible_Declarations (Specification (Pack_Decl))) then
2799 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2802 (First (Visible_Declarations (Specification (Pack_Decl))),
2807 end Build_Local_Package;
2811 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2812 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2814 Associations : Boolean := True;
2816 Parent_Installed : Boolean := False;
2817 Parent_Instance : Entity_Id;
2818 Renaming_In_Par : Entity_Id;
2820 -- Start of processing for Analyze_Formal_Package_Declaration
2823 Check_Text_IO_Special_Unit (Gen_Id);
2826 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2827 Gen_Unit := Entity (Gen_Id);
2829 -- Check for a formal package that is a package renaming
2831 if Present (Renamed_Object (Gen_Unit)) then
2833 -- Indicate that unit is used, before replacing it with renamed
2834 -- entity for use below.
2836 if In_Extended_Main_Source_Unit (N) then
2837 Set_Is_Instantiated (Gen_Unit);
2838 Generate_Reference (Gen_Unit, N);
2841 Gen_Unit := Renamed_Object (Gen_Unit);
2844 if Ekind (Gen_Unit) /= E_Generic_Package then
2845 Error_Msg_N ("expect generic package name", Gen_Id);
2849 elsif Gen_Unit = Current_Scope then
2851 ("generic package cannot be used as a formal package of itself",
2856 elsif In_Open_Scopes (Gen_Unit) then
2857 if Is_Compilation_Unit (Gen_Unit)
2858 and then Is_Child_Unit (Current_Scope)
2860 -- Special-case the error when the formal is a parent, and
2861 -- continue analysis to minimize cascaded errors.
2864 ("generic parent cannot be used as formal package of a child "
2869 ("generic package cannot be used as a formal package within "
2870 & "itself", Gen_Id);
2876 -- Check that name of formal package does not hide name of generic,
2877 -- or its leading prefix. This check must be done separately because
2878 -- the name of the generic has already been analyzed.
2881 Gen_Name : Entity_Id;
2885 while Nkind (Gen_Name) = N_Expanded_Name loop
2886 Gen_Name := Prefix (Gen_Name);
2889 if Chars (Gen_Name) = Chars (Pack_Id) then
2891 ("& is hidden within declaration of formal package",
2897 or else No (Generic_Associations (N))
2898 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2900 Associations := False;
2903 -- If there are no generic associations, the generic parameters appear
2904 -- as local entities and are instantiated like them. We copy the generic
2905 -- package declaration as if it were an instantiation, and analyze it
2906 -- like a regular package, except that we treat the formals as
2907 -- additional visible components.
2909 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2911 if In_Extended_Main_Source_Unit (N) then
2912 Set_Is_Instantiated (Gen_Unit);
2913 Generate_Reference (Gen_Unit, N);
2916 Formal := New_Copy (Pack_Id);
2917 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2919 -- Make local generic without formals. The formals will be replaced with
2920 -- internal declarations.
2923 New_N := Build_Local_Package;
2925 -- If there are errors in the parameter list, Analyze_Associations
2926 -- raises Instantiation_Error. Patch the declaration to prevent further
2927 -- exception propagation.
2930 when Instantiation_Error =>
2931 Enter_Name (Formal);
2932 Set_Ekind (Formal, E_Variable);
2933 Set_Etype (Formal, Any_Type);
2934 Restore_Hidden_Primitives (Vis_Prims_List);
2936 if Parent_Installed then
2944 Set_Defining_Unit_Name (Specification (New_N), Formal);
2945 Set_Generic_Parent (Specification (N), Gen_Unit);
2946 Set_Instance_Env (Gen_Unit, Formal);
2947 Set_Is_Generic_Instance (Formal);
2949 Enter_Name (Formal);
2950 Set_Ekind (Formal, E_Package);
2951 Set_Etype (Formal, Standard_Void_Type);
2952 Set_Inner_Instances (Formal, New_Elmt_List);
2953 Push_Scope (Formal);
2955 -- Manually set the SPARK_Mode from the context because the package
2956 -- declaration is never analyzed.
2958 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2959 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2960 Set_SPARK_Pragma_Inherited (Formal);
2961 Set_SPARK_Aux_Pragma_Inherited (Formal);
2963 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2965 -- Similarly, we have to make the name of the formal visible in the
2966 -- parent instance, to resolve properly fully qualified names that
2967 -- may appear in the generic unit. The parent instance has been
2968 -- placed on the scope stack ahead of the current scope.
2970 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2973 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2974 Set_Ekind (Renaming_In_Par, E_Package);
2975 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2976 Set_Scope (Renaming_In_Par, Parent_Instance);
2977 Set_Parent (Renaming_In_Par, Parent (Formal));
2978 Set_Renamed_Object (Renaming_In_Par, Formal);
2979 Append_Entity (Renaming_In_Par, Parent_Instance);
2982 -- A formal package declaration behaves as a package instantiation with
2983 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2984 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2985 -- all SPARK_Mode pragmas within the generic_package_name.
2987 if SPARK_Mode /= On then
2988 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2990 -- Mark the formal spec in case the body is instantiated at a later
2991 -- pass. This preserves the original context in effect for the body.
2993 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2996 Analyze (Specification (N));
2998 -- The formals for which associations are provided are not visible
2999 -- outside of the formal package. The others are still declared by a
3000 -- formal parameter declaration.
3002 -- If there are no associations, the only local entity to hide is the
3003 -- generated package renaming itself.
3009 E := First_Entity (Formal);
3010 while Present (E) loop
3011 if Associations and then not Is_Generic_Formal (E) then
3015 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3024 End_Package_Scope (Formal);
3025 Restore_Hidden_Primitives (Vis_Prims_List);
3027 if Parent_Installed then
3033 -- Inside the generic unit, the formal package is a regular package, but
3034 -- no body is needed for it. Note that after instantiation, the defining
3035 -- unit name we need is in the new tree and not in the original (see
3036 -- Package_Instantiation). A generic formal package is an instance, and
3037 -- can be used as an actual for an inner instance.
3039 Set_Has_Completion (Formal, True);
3041 -- Add semantic information to the original defining identifier for ASIS
3044 Set_Ekind (Pack_Id, E_Package);
3045 Set_Etype (Pack_Id, Standard_Void_Type);
3046 Set_Scope (Pack_Id, Scope (Formal));
3047 Set_Has_Completion (Pack_Id, True);
3050 if Has_Aspects (N) then
3051 Analyze_Aspect_Specifications (N, Pack_Id);
3054 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3055 end Analyze_Formal_Package_Declaration;
3057 ---------------------------------
3058 -- Analyze_Formal_Private_Type --
3059 ---------------------------------
3061 procedure Analyze_Formal_Private_Type
3067 New_Private_Type (N, T, Def);
3069 -- Set the size to an arbitrary but legal value
3071 Set_Size_Info (T, Standard_Integer);
3072 Set_RM_Size (T, RM_Size (Standard_Integer));
3073 end Analyze_Formal_Private_Type;
3075 ------------------------------------
3076 -- Analyze_Formal_Incomplete_Type --
3077 ------------------------------------
3079 procedure Analyze_Formal_Incomplete_Type
3085 Set_Ekind (T, E_Incomplete_Type);
3087 Set_Private_Dependents (T, New_Elmt_List);
3089 if Tagged_Present (Def) then
3090 Set_Is_Tagged_Type (T);
3091 Make_Class_Wide_Type (T);
3092 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3094 end Analyze_Formal_Incomplete_Type;
3096 ----------------------------------------
3097 -- Analyze_Formal_Signed_Integer_Type --
3098 ----------------------------------------
3100 procedure Analyze_Formal_Signed_Integer_Type
3104 Base : constant Entity_Id :=
3106 (E_Signed_Integer_Type,
3108 Sloc (Defining_Identifier (Parent (Def))), 'G');
3113 Set_Ekind (T, E_Signed_Integer_Subtype);
3114 Set_Etype (T, Base);
3115 Set_Size_Info (T, Standard_Integer);
3116 Set_RM_Size (T, RM_Size (Standard_Integer));
3117 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3118 Set_Is_Constrained (T);
3120 Set_Is_Generic_Type (Base);
3121 Set_Size_Info (Base, Standard_Integer);
3122 Set_RM_Size (Base, RM_Size (Standard_Integer));
3123 Set_Etype (Base, Base);
3124 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3125 Set_Parent (Base, Parent (Def));
3126 end Analyze_Formal_Signed_Integer_Type;
3128 -------------------------------------------
3129 -- Analyze_Formal_Subprogram_Declaration --
3130 -------------------------------------------
3132 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3133 Spec : constant Node_Id := Specification (N);
3134 Def : constant Node_Id := Default_Name (N);
3135 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3143 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3144 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3148 Analyze_Subprogram_Declaration (N);
3149 Set_Is_Formal_Subprogram (Nam);
3150 Set_Has_Completion (Nam);
3152 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3153 Set_Is_Abstract_Subprogram (Nam);
3155 Set_Is_Dispatching_Operation (Nam);
3157 -- A formal abstract procedure cannot have a null default
3158 -- (RM 12.6(4.1/2)).
3160 if Nkind (Spec) = N_Procedure_Specification
3161 and then Null_Present (Spec)
3164 ("a formal abstract subprogram cannot default to null", Spec);
3168 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3170 if No (Ctrl_Type) then
3172 ("abstract formal subprogram must have a controlling type",
3175 elsif Ada_Version >= Ada_2012
3176 and then Is_Incomplete_Type (Ctrl_Type)
3179 ("controlling type of abstract formal subprogram cannot "
3180 & "be incomplete type", N, Ctrl_Type);
3183 Check_Controlling_Formals (Ctrl_Type, Nam);
3188 -- Default name is resolved at the point of instantiation
3190 if Box_Present (N) then
3193 -- Else default is bound at the point of generic declaration
3195 elsif Present (Def) then
3196 if Nkind (Def) = N_Operator_Symbol then
3197 Find_Direct_Name (Def);
3199 elsif Nkind (Def) /= N_Attribute_Reference then
3203 -- For an attribute reference, analyze the prefix and verify
3204 -- that it has the proper profile for the subprogram.
3206 Analyze (Prefix (Def));
3207 Valid_Default_Attribute (Nam, Def);
3211 -- Default name may be overloaded, in which case the interpretation
3212 -- with the correct profile must be selected, as for a renaming.
3213 -- If the definition is an indexed component, it must denote a
3214 -- member of an entry family. If it is a selected component, it
3215 -- can be a protected operation.
3217 if Etype (Def) = Any_Type then
3220 elsif Nkind (Def) = N_Selected_Component then
3221 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3222 Error_Msg_N ("expect valid subprogram name as default", Def);
3225 elsif Nkind (Def) = N_Indexed_Component then
3226 if Is_Entity_Name (Prefix (Def)) then
3227 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3228 Error_Msg_N ("expect valid subprogram name as default", Def);
3231 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3232 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3235 Error_Msg_N ("expect valid subprogram name as default", Def);
3239 Error_Msg_N ("expect valid subprogram name as default", Def);
3243 elsif Nkind (Def) = N_Character_Literal then
3245 -- Needs some type checks: subprogram should be parameterless???
3247 Resolve (Def, (Etype (Nam)));
3249 elsif not Is_Entity_Name (Def)
3250 or else not Is_Overloadable (Entity (Def))
3252 Error_Msg_N ("expect valid subprogram name as default", Def);
3255 elsif not Is_Overloaded (Def) then
3256 Subp := Entity (Def);
3259 Error_Msg_N ("premature usage of formal subprogram", Def);
3261 elsif not Entity_Matches_Spec (Subp, Nam) then
3262 Error_Msg_N ("no visible entity matches specification", Def);
3265 -- More than one interpretation, so disambiguate as for a renaming
3270 I1 : Interp_Index := 0;
3276 Get_First_Interp (Def, I, It);
3277 while Present (It.Nam) loop
3278 if Entity_Matches_Spec (It.Nam, Nam) then
3279 if Subp /= Any_Id then
3280 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3282 if It1 = No_Interp then
3283 Error_Msg_N ("ambiguous default subprogram", Def);
3296 Get_Next_Interp (I, It);
3300 if Subp /= Any_Id then
3302 -- Subprogram found, generate reference to it
3304 Set_Entity (Def, Subp);
3305 Generate_Reference (Subp, Def);
3308 Error_Msg_N ("premature usage of formal subprogram", Def);
3310 elsif Ekind (Subp) /= E_Operator then
3311 Check_Mode_Conformant (Subp, Nam);
3315 Error_Msg_N ("no visible subprogram matches specification", N);
3321 if Has_Aspects (N) then
3322 Analyze_Aspect_Specifications (N, Nam);
3325 end Analyze_Formal_Subprogram_Declaration;
3327 -------------------------------------
3328 -- Analyze_Formal_Type_Declaration --
3329 -------------------------------------
3331 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3332 Def : constant Node_Id := Formal_Type_Definition (N);
3336 T := Defining_Identifier (N);
3338 if Present (Discriminant_Specifications (N))
3339 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3342 ("discriminants not allowed for this formal type", T);
3345 -- Enter the new name, and branch to specific routine
3348 when N_Formal_Private_Type_Definition =>
3349 Analyze_Formal_Private_Type (N, T, Def);
3351 when N_Formal_Derived_Type_Definition =>
3352 Analyze_Formal_Derived_Type (N, T, Def);
3354 when N_Formal_Incomplete_Type_Definition =>
3355 Analyze_Formal_Incomplete_Type (T, Def);
3357 when N_Formal_Discrete_Type_Definition =>
3358 Analyze_Formal_Discrete_Type (T, Def);
3360 when N_Formal_Signed_Integer_Type_Definition =>
3361 Analyze_Formal_Signed_Integer_Type (T, Def);
3363 when N_Formal_Modular_Type_Definition =>
3364 Analyze_Formal_Modular_Type (T, Def);
3366 when N_Formal_Floating_Point_Definition =>
3367 Analyze_Formal_Floating_Type (T, Def);
3369 when N_Formal_Ordinary_Fixed_Point_Definition =>
3370 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3372 when N_Formal_Decimal_Fixed_Point_Definition =>
3373 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3375 when N_Array_Type_Definition =>
3376 Analyze_Formal_Array_Type (T, Def);
3378 when N_Access_Function_Definition
3379 | N_Access_Procedure_Definition
3380 | N_Access_To_Object_Definition
3382 Analyze_Generic_Access_Type (T, Def);
3384 -- Ada 2005: a interface declaration is encoded as an abstract
3385 -- record declaration or a abstract type derivation.
3387 when N_Record_Definition =>
3388 Analyze_Formal_Interface_Type (N, T, Def);
3390 when N_Derived_Type_Definition =>
3391 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3397 raise Program_Error;
3400 Set_Is_Generic_Type (T);
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, T);
3405 end Analyze_Formal_Type_Declaration;
3407 ------------------------------------
3408 -- Analyze_Function_Instantiation --
3409 ------------------------------------
3411 procedure Analyze_Function_Instantiation (N : Node_Id) is
3413 Analyze_Subprogram_Instantiation (N, E_Function);
3414 end Analyze_Function_Instantiation;
3416 ---------------------------------
3417 -- Analyze_Generic_Access_Type --
3418 ---------------------------------
3420 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3424 if Nkind (Def) = N_Access_To_Object_Definition then
3425 Access_Type_Declaration (T, Def);
3427 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3428 and then No (Full_View (Designated_Type (T)))
3429 and then not Is_Generic_Type (Designated_Type (T))
3431 Error_Msg_N ("premature usage of incomplete type", Def);
3433 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3435 ("only a subtype mark is allowed in a formal", Def);
3439 Access_Subprogram_Declaration (T, Def);
3441 end Analyze_Generic_Access_Type;
3443 ---------------------------------
3444 -- Analyze_Generic_Formal_Part --
3445 ---------------------------------
3447 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3448 Gen_Parm_Decl : Node_Id;
3451 -- The generic formals are processed in the scope of the generic unit,
3452 -- where they are immediately visible. The scope is installed by the
3455 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3456 while Present (Gen_Parm_Decl) loop
3457 Analyze (Gen_Parm_Decl);
3458 Next (Gen_Parm_Decl);
3461 Generate_Reference_To_Generic_Formals (Current_Scope);
3462 end Analyze_Generic_Formal_Part;
3464 ------------------------------------------
3465 -- Analyze_Generic_Package_Declaration --
3466 ------------------------------------------
3468 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3469 Decls : constant List_Id := Visible_Declarations (Specification (N));
3470 Loc : constant Source_Ptr := Sloc (N);
3476 Save_Parent : Node_Id;
3479 Check_SPARK_05_Restriction ("generic is not allowed", N);
3481 -- We introduce a renaming of the enclosing package, to have a usable
3482 -- entity as the prefix of an expanded name for a local entity of the
3483 -- form Par.P.Q, where P is the generic package. This is because a local
3484 -- entity named P may hide it, so that the usual visibility rules in
3485 -- the instance will not resolve properly.
3488 Make_Package_Renaming_Declaration (Loc,
3489 Defining_Unit_Name =>
3490 Make_Defining_Identifier (Loc,
3491 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3493 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3495 -- The declaration is inserted before other declarations, but before
3496 -- pragmas that may be library-unit pragmas and must appear before other
3497 -- declarations. The pragma Compile_Time_Error is not in this class, and
3498 -- may contain an expression that includes such a qualified name, so the
3499 -- renaming declaration must appear before it.
3501 -- Are there other pragmas that require this special handling ???
3503 if Present (Decls) then
3504 Decl := First (Decls);
3505 while Present (Decl)
3506 and then Nkind (Decl) = N_Pragma
3507 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3512 if Present (Decl) then
3513 Insert_Before (Decl, Renaming);
3515 Append (Renaming, Visible_Declarations (Specification (N)));
3519 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3522 -- Create copy of generic unit, and save for instantiation. If the unit
3523 -- is a child unit, do not copy the specifications for the parent, which
3524 -- are not part of the generic tree.
3526 Save_Parent := Parent_Spec (N);
3527 Set_Parent_Spec (N, Empty);
3529 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3530 Set_Parent_Spec (New_N, Save_Parent);
3533 -- Once the contents of the generic copy and the template are swapped,
3534 -- do the same for their respective aspect specifications.
3536 Exchange_Aspects (N, New_N);
3538 -- Collect all contract-related source pragmas found within the template
3539 -- and attach them to the contract of the package spec. This contract is
3540 -- used in the capture of global references within annotations.
3542 Create_Generic_Contract (N);
3544 Id := Defining_Entity (N);
3545 Generate_Definition (Id);
3547 -- Expansion is not applied to generic units
3552 Set_Ekind (Id, E_Generic_Package);
3553 Set_Etype (Id, Standard_Void_Type);
3555 -- Set SPARK_Mode from context
3557 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3558 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3559 Set_SPARK_Pragma_Inherited (Id);
3560 Set_SPARK_Aux_Pragma_Inherited (Id);
3562 -- Preserve relevant elaboration-related attributes of the context which
3563 -- are no longer available or very expensive to recompute once analysis,
3564 -- resolution, and expansion are over.
3566 Mark_Elaboration_Attributes
3571 -- Analyze aspects now, so that generated pragmas appear in the
3572 -- declarations before building and analyzing the generic copy.
3574 if Has_Aspects (N) then
3575 Analyze_Aspect_Specifications (N, Id);
3579 Enter_Generic_Scope (Id);
3580 Set_Inner_Instances (Id, New_Elmt_List);
3582 Set_Categorization_From_Pragmas (N);
3583 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3585 -- Link the declaration of the generic homonym in the generic copy to
3586 -- the package it renames, so that it is always resolved properly.
3588 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3589 Set_Entity (Associated_Node (Name (Renaming)), Id);
3591 -- For a library unit, we have reconstructed the entity for the unit,
3592 -- and must reset it in the library tables.
3594 if Nkind (Parent (N)) = N_Compilation_Unit then
3595 Set_Cunit_Entity (Current_Sem_Unit, Id);
3598 Analyze_Generic_Formal_Part (N);
3600 -- After processing the generic formals, analysis proceeds as for a
3601 -- non-generic package.
3603 Analyze (Specification (N));
3605 Validate_Categorization_Dependency (N, Id);
3609 End_Package_Scope (Id);
3610 Exit_Generic_Scope (Id);
3612 -- If the generic appears within a package unit, the body of that unit
3613 -- has to be present for instantiation and inlining.
3615 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3616 Set_Body_Needed_For_Inlining
3617 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3620 if Nkind (Parent (N)) /= N_Compilation_Unit then
3621 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3622 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3623 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3626 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3627 Validate_RT_RAT_Component (N);
3629 -- If this is a spec without a body, check that generic parameters
3632 if not Body_Required (Parent (N)) then
3633 Check_References (Id);
3637 -- If there is a specified storage pool in the context, create an
3638 -- aspect on the package declaration, so that it is used in any
3639 -- instance that does not override it.
3641 if Present (Default_Pool) then
3647 Make_Aspect_Specification (Loc,
3648 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3649 Expression => New_Copy (Default_Pool));
3651 if No (Aspect_Specifications (Specification (N))) then
3652 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3654 Append (ASN, Aspect_Specifications (Specification (N)));
3658 end Analyze_Generic_Package_Declaration;
3660 --------------------------------------------
3661 -- Analyze_Generic_Subprogram_Declaration --
3662 --------------------------------------------
3664 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3668 Result_Type : Entity_Id;
3669 Save_Parent : Node_Id;
3674 Check_SPARK_05_Restriction ("generic is not allowed", N);
3676 -- Create copy of generic unit, and save for instantiation. If the unit
3677 -- is a child unit, do not copy the specifications for the parent, which
3678 -- are not part of the generic tree.
3680 Save_Parent := Parent_Spec (N);
3681 Set_Parent_Spec (N, Empty);
3683 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3684 Set_Parent_Spec (New_N, Save_Parent);
3687 -- Once the contents of the generic copy and the template are swapped,
3688 -- do the same for their respective aspect specifications.
3690 Exchange_Aspects (N, New_N);
3692 -- Collect all contract-related source pragmas found within the template
3693 -- and attach them to the contract of the subprogram spec. This contract
3694 -- is used in the capture of global references within annotations.
3696 Create_Generic_Contract (N);
3698 Spec := Specification (N);
3699 Id := Defining_Entity (Spec);
3700 Generate_Definition (Id);
3702 if Nkind (Id) = N_Defining_Operator_Symbol then
3704 ("operator symbol not allowed for generic subprogram", Id);
3710 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3712 -- Analyze the aspects of the generic copy to ensure that all generated
3713 -- pragmas (if any) perform their semantic effects.
3715 if Has_Aspects (N) then
3716 Analyze_Aspect_Specifications (N, Id);
3720 Enter_Generic_Scope (Id);
3721 Set_Inner_Instances (Id, New_Elmt_List);
3722 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3724 Analyze_Generic_Formal_Part (N);
3726 if Nkind (Spec) = N_Function_Specification then
3727 Set_Ekind (Id, E_Generic_Function);
3729 Set_Ekind (Id, E_Generic_Procedure);
3732 -- Set SPARK_Mode from context
3734 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3735 Set_SPARK_Pragma_Inherited (Id);
3737 -- Preserve relevant elaboration-related attributes of the context which
3738 -- are no longer available or very expensive to recompute once analysis,
3739 -- resolution, and expansion are over.
3741 Mark_Elaboration_Attributes
3746 Formals := Parameter_Specifications (Spec);
3748 if Present (Formals) then
3749 Process_Formals (Formals, Spec);
3752 if Nkind (Spec) = N_Function_Specification then
3753 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3754 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3755 Set_Etype (Id, Result_Type);
3757 -- Check restriction imposed by AI05-073: a generic function
3758 -- cannot return an abstract type or an access to such.
3760 -- This is a binding interpretation should it apply to earlier
3761 -- versions of Ada as well as Ada 2012???
3763 if Is_Abstract_Type (Designated_Type (Result_Type))
3764 and then Ada_Version >= Ada_2012
3767 ("generic function cannot have an access result "
3768 & "that designates an abstract type", Spec);
3772 Find_Type (Result_Definition (Spec));
3773 Typ := Entity (Result_Definition (Spec));
3775 if Is_Abstract_Type (Typ)
3776 and then Ada_Version >= Ada_2012
3779 ("generic function cannot have abstract result type", Spec);
3782 -- If a null exclusion is imposed on the result type, then create
3783 -- a null-excluding itype (an access subtype) and use it as the
3784 -- function's Etype.
3786 if Is_Access_Type (Typ)
3787 and then Null_Exclusion_Present (Spec)
3790 Create_Null_Excluding_Itype
3792 Related_Nod => Spec,
3793 Scope_Id => Defining_Unit_Name (Spec)));
3795 Set_Etype (Id, Typ);
3800 Set_Etype (Id, Standard_Void_Type);
3803 -- For a library unit, we have reconstructed the entity for the unit,
3804 -- and must reset it in the library tables. We also make sure that
3805 -- Body_Required is set properly in the original compilation unit node.
3807 if Nkind (Parent (N)) = N_Compilation_Unit then
3808 Set_Cunit_Entity (Current_Sem_Unit, Id);
3809 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3812 -- If the generic appears within a package unit, the body of that unit
3813 -- has to be present for instantiation and inlining.
3815 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3816 and then Unit_Requires_Body (Id)
3818 Set_Body_Needed_For_Inlining
3819 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3822 Set_Categorization_From_Pragmas (N);
3823 Validate_Categorization_Dependency (N, Id);
3825 -- Capture all global references that occur within the profile of the
3826 -- generic subprogram. Aspects are not part of this processing because
3827 -- they must be delayed. If processed now, Save_Global_References will
3828 -- destroy the Associated_Node links and prevent the capture of global
3829 -- references when the contract of the generic subprogram is analyzed.
3831 Save_Global_References (Original_Node (N));
3835 Exit_Generic_Scope (Id);
3836 Generate_Reference_To_Formals (Id);
3838 List_Inherited_Pre_Post_Aspects (Id);
3839 end Analyze_Generic_Subprogram_Declaration;
3841 -----------------------------------
3842 -- Analyze_Package_Instantiation --
3843 -----------------------------------
3845 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3846 -- must be replaced by gotos which jump to the end of the routine in order
3847 -- to restore the Ghost and SPARK modes.
3849 procedure Analyze_Package_Instantiation (N : Node_Id) is
3850 Has_Inline_Always : Boolean := False;
3852 procedure Delay_Descriptors (E : Entity_Id);
3853 -- Delay generation of subprogram descriptors for given entity
3855 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3856 -- If inlining is active and the generic contains inlined subprograms,
3857 -- we instantiate the body. This may cause superfluous instantiations,
3858 -- but it is simpler than detecting the need for the body at the point
3859 -- of inlining, when the context of the instance is not available.
3861 -----------------------
3862 -- Delay_Descriptors --
3863 -----------------------
3865 procedure Delay_Descriptors (E : Entity_Id) is
3867 if not Delay_Subprogram_Descriptors (E) then
3868 Set_Delay_Subprogram_Descriptors (E);
3869 Pending_Descriptor.Append (E);
3871 end Delay_Descriptors;
3873 -----------------------
3874 -- Might_Inline_Subp --
3875 -----------------------
3877 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3881 if not Inline_Processing_Required then
3885 E := First_Entity (Gen_Unit);
3886 while Present (E) loop
3887 if Is_Subprogram (E) and then Is_Inlined (E) then
3888 -- Remember if there are any subprograms with Inline_Always
3890 if Has_Pragma_Inline_Always (E) then
3891 Has_Inline_Always := True;
3902 end Might_Inline_Subp;
3904 -- Local declarations
3906 Gen_Id : constant Node_Id := Name (N);
3907 Is_Actual_Pack : constant Boolean :=
3908 Is_Internal (Defining_Entity (N));
3909 Loc : constant Source_Ptr := Sloc (N);
3911 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3912 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3913 Saved_ISMP : constant Boolean :=
3914 Ignore_SPARK_Mode_Pragmas_In_Instance;
3915 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3916 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3917 -- Save the Ghost and SPARK mode-related data to restore on exit
3919 Saved_Style_Check : constant Boolean := Style_Check;
3920 -- Save style check mode for restore on exit
3923 Act_Decl_Name : Node_Id;
3924 Act_Decl_Id : Entity_Id;
3927 Env_Installed : Boolean := False;
3930 Gen_Unit : Entity_Id;
3931 Inline_Now : Boolean := False;
3932 Needs_Body : Boolean;
3933 Parent_Installed : Boolean := False;
3934 Renaming_List : List_Id;
3935 Unit_Renaming : Node_Id;
3937 Vis_Prims_List : Elist_Id := No_Elist;
3938 -- List of primitives made temporarily visible in the instantiation
3939 -- to match the visibility of the formal type
3941 -- Start of processing for Analyze_Package_Instantiation
3944 -- Preserve relevant elaboration-related attributes of the context which
3945 -- are no longer available or very expensive to recompute once analysis,
3946 -- resolution, and expansion are over.
3948 Mark_Elaboration_Attributes
3955 Check_SPARK_05_Restriction ("generic is not allowed", N);
3957 -- Very first thing: check for Text_IO special unit in case we are
3958 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3960 Check_Text_IO_Special_Unit (Name (N));
3962 -- Make node global for error reporting
3964 Instantiation_Node := N;
3966 -- Case of instantiation of a generic package
3968 if Nkind (N) = N_Package_Instantiation then
3969 Act_Decl_Id := New_Copy (Defining_Entity (N));
3970 Set_Comes_From_Source (Act_Decl_Id, True);
3972 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3974 Make_Defining_Program_Unit_Name (Loc,
3976 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3977 Defining_Identifier => Act_Decl_Id);
3979 Act_Decl_Name := Act_Decl_Id;
3982 -- Case of instantiation of a formal package
3985 Act_Decl_Id := Defining_Identifier (N);
3986 Act_Decl_Name := Act_Decl_Id;
3989 Generate_Definition (Act_Decl_Id);
3990 Set_Ekind (Act_Decl_Id, E_Package);
3992 -- Initialize list of incomplete actuals before analysis
3994 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3996 Preanalyze_Actuals (N, Act_Decl_Id);
3998 -- Turn off style checking in instances. If the check is enabled on the
3999 -- generic unit, a warning in an instance would just be noise. If not
4000 -- enabled on the generic, then a warning in an instance is just wrong.
4001 -- This must be done after analyzing the actuals, which do come from
4002 -- source and are subject to style checking.
4004 Style_Check := False;
4007 Env_Installed := True;
4009 -- Reset renaming map for formal types. The mapping is established
4010 -- when analyzing the generic associations, but some mappings are
4011 -- inherited from formal packages of parent units, and these are
4012 -- constructed when the parents are installed.
4014 Generic_Renamings.Set_Last (0);
4015 Generic_Renamings_HTable.Reset;
4017 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4018 Gen_Unit := Entity (Gen_Id);
4020 -- A package instantiation is Ghost when it is subject to pragma Ghost
4021 -- or the generic template is Ghost. Set the mode now to ensure that
4022 -- any nodes generated during analysis and expansion are marked as
4025 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4027 -- Verify that it is the name of a generic package
4029 -- A visibility glitch: if the instance is a child unit and the generic
4030 -- is the generic unit of a parent instance (i.e. both the parent and
4031 -- the child units are instances of the same package) the name now
4032 -- denotes the renaming within the parent, not the intended generic
4033 -- unit. See if there is a homonym that is the desired generic. The
4034 -- renaming declaration must be visible inside the instance of the
4035 -- child, but not when analyzing the name in the instantiation itself.
4037 if Ekind (Gen_Unit) = E_Package
4038 and then Present (Renamed_Entity (Gen_Unit))
4039 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4040 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4041 and then Present (Homonym (Gen_Unit))
4043 Gen_Unit := Homonym (Gen_Unit);
4046 if Etype (Gen_Unit) = Any_Type then
4050 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4052 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4054 if From_Limited_With (Gen_Unit) then
4056 ("cannot instantiate a limited withed package", Gen_Id);
4059 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4066 if In_Extended_Main_Source_Unit (N) then
4067 Set_Is_Instantiated (Gen_Unit);
4068 Generate_Reference (Gen_Unit, N);
4070 if Present (Renamed_Object (Gen_Unit)) then
4071 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4072 Generate_Reference (Renamed_Object (Gen_Unit), N);
4076 if Nkind (Gen_Id) = N_Identifier
4077 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4080 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4082 elsif Nkind (Gen_Id) = N_Expanded_Name
4083 and then Is_Child_Unit (Gen_Unit)
4084 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4085 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4088 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4091 Set_Entity (Gen_Id, Gen_Unit);
4093 -- If generic is a renaming, get original generic unit
4095 if Present (Renamed_Object (Gen_Unit))
4096 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4098 Gen_Unit := Renamed_Object (Gen_Unit);
4101 -- Verify that there are no circular instantiations
4103 if In_Open_Scopes (Gen_Unit) then
4104 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4108 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4109 Error_Msg_Node_2 := Current_Scope;
4111 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4112 Circularity_Detected := True;
4117 -- If the context of the instance is subject to SPARK_Mode "off" or
4118 -- the annotation is altogether missing, set the global flag which
4119 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4122 if SPARK_Mode /= On then
4123 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4125 -- Mark the instance spec in case the body is instantiated at a
4126 -- later pass. This preserves the original context in effect for
4129 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4132 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4133 Gen_Spec := Specification (Gen_Decl);
4135 -- Initialize renamings map, for error checking, and the list that
4136 -- holds private entities whose views have changed between generic
4137 -- definition and instantiation. If this is the instance created to
4138 -- validate an actual package, the instantiation environment is that
4139 -- of the enclosing instance.
4141 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4143 -- Copy original generic tree, to produce text for instantiation
4147 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4149 Act_Spec := Specification (Act_Tree);
4151 -- If this is the instance created to validate an actual package,
4152 -- only the formals matter, do not examine the package spec itself.
4154 if Is_Actual_Pack then
4155 Set_Visible_Declarations (Act_Spec, New_List);
4156 Set_Private_Declarations (Act_Spec, New_List);
4160 Analyze_Associations
4162 Formals => Generic_Formal_Declarations (Act_Tree),
4163 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4165 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4167 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4168 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4169 Set_Is_Generic_Instance (Act_Decl_Id);
4170 Set_Generic_Parent (Act_Spec, Gen_Unit);
4172 -- References to the generic in its own declaration or its body are
4173 -- references to the instance. Add a renaming declaration for the
4174 -- generic unit itself. This declaration, as well as the renaming
4175 -- declarations for the generic formals, must remain private to the
4176 -- unit: the formals, because this is the language semantics, and
4177 -- the unit because its use is an artifact of the implementation.
4180 Make_Package_Renaming_Declaration (Loc,
4181 Defining_Unit_Name =>
4182 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4183 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4185 Append (Unit_Renaming, Renaming_List);
4187 -- The renaming declarations are the first local declarations of the
4190 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4192 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4194 Set_Visible_Declarations (Act_Spec, Renaming_List);
4197 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4199 -- Propagate the aspect specifications from the package declaration
4200 -- template to the instantiated version of the package declaration.
4202 if Has_Aspects (Act_Tree) then
4203 Set_Aspect_Specifications (Act_Decl,
4204 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4207 -- The generic may have a generated Default_Storage_Pool aspect,
4208 -- set at the point of generic declaration. If the instance has
4209 -- that aspect, it overrides the one inherited from the generic.
4211 if Has_Aspects (Gen_Spec) then
4212 if No (Aspect_Specifications (N)) then
4213 Set_Aspect_Specifications (N,
4215 (Aspect_Specifications (Gen_Spec))));
4219 Inherited_Aspects : constant List_Id :=
4221 (Aspect_Specifications (Gen_Spec));
4225 Pool_Present : Boolean := False;
4228 ASN1 := First (Aspect_Specifications (N));
4229 while Present (ASN1) loop
4230 if Chars (Identifier (ASN1)) =
4231 Name_Default_Storage_Pool
4233 Pool_Present := True;
4240 if Pool_Present then
4242 -- If generic carries a default storage pool, remove it
4243 -- in favor of the instance one.
4245 ASN2 := First (Inherited_Aspects);
4246 while Present (ASN2) loop
4247 if Chars (Identifier (ASN2)) =
4248 Name_Default_Storage_Pool
4259 (Aspect_Specifications (N), Inherited_Aspects);
4264 -- Save the instantiation node, for subsequent instantiation of the
4265 -- body, if there is one and we are generating code for the current
4266 -- unit. Mark unit as having a body (avoids premature error message).
4268 -- We instantiate the body if we are generating code, if we are
4269 -- generating cross-reference information, or if we are building
4270 -- trees for ASIS use or GNATprove use.
4273 Enclosing_Body_Present : Boolean := False;
4274 -- If the generic unit is not a compilation unit, then a body may
4275 -- be present in its parent even if none is required. We create a
4276 -- tentative pending instantiation for the body, which will be
4277 -- discarded if none is actually present.
4282 if Scope (Gen_Unit) /= Standard_Standard
4283 and then not Is_Child_Unit (Gen_Unit)
4285 Scop := Scope (Gen_Unit);
4286 while Present (Scop) and then Scop /= Standard_Standard loop
4287 if Unit_Requires_Body (Scop) then
4288 Enclosing_Body_Present := True;
4291 elsif In_Open_Scopes (Scop)
4292 and then In_Package_Body (Scop)
4294 Enclosing_Body_Present := True;
4298 exit when Is_Compilation_Unit (Scop);
4299 Scop := Scope (Scop);
4303 -- If front-end inlining is enabled or there are any subprograms
4304 -- marked with Inline_Always, and this is a unit for which code
4305 -- will be generated, we instantiate the body at once.
4307 -- This is done if the instance is not the main unit, and if the
4308 -- generic is not a child unit of another generic, to avoid scope
4309 -- problems and the reinstallation of parent instances.
4312 and then (not Is_Child_Unit (Gen_Unit)
4313 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4314 and then Might_Inline_Subp (Gen_Unit)
4315 and then not Is_Actual_Pack
4317 if not Back_End_Inlining
4318 and then (Front_End_Inlining or else Has_Inline_Always)
4319 and then (Is_In_Main_Unit (N)
4320 or else In_Main_Context (Current_Scope))
4321 and then Nkind (Parent (N)) /= N_Compilation_Unit
4325 -- In configurable_run_time mode we force the inlining of
4326 -- predefined subprograms marked Inline_Always, to minimize
4327 -- the use of the run-time library.
4329 elsif In_Predefined_Unit (Gen_Decl)
4330 and then Configurable_Run_Time_Mode
4331 and then Nkind (Parent (N)) /= N_Compilation_Unit
4336 -- If the current scope is itself an instance within a child
4337 -- unit, there will be duplications in the scope stack, and the
4338 -- unstacking mechanism in Inline_Instance_Body will fail.
4339 -- This loses some rare cases of optimization, and might be
4340 -- improved some day, if we can find a proper abstraction for
4341 -- "the complete compilation context" that can be saved and
4344 if Is_Generic_Instance (Current_Scope) then
4346 Curr_Unit : constant Entity_Id :=
4347 Cunit_Entity (Current_Sem_Unit);
4349 if Curr_Unit /= Current_Scope
4350 and then Is_Child_Unit (Curr_Unit)
4352 Inline_Now := False;
4359 (Unit_Requires_Body (Gen_Unit)
4360 or else Enclosing_Body_Present
4361 or else Present (Corresponding_Body (Gen_Decl)))
4362 and then (Is_In_Main_Unit (N)
4363 or else Might_Inline_Subp (Gen_Unit))
4364 and then not Is_Actual_Pack
4365 and then not Inline_Now
4366 and then (Operating_Mode = Generate_Code
4368 -- Need comment for this check ???
4370 or else (Operating_Mode = Check_Semantics
4371 and then (ASIS_Mode or GNATprove_Mode)));
4373 -- If front-end inlining is enabled or there are any subprograms
4374 -- marked with Inline_Always, do not instantiate body when within
4375 -- a generic context.
4377 if ((Front_End_Inlining or else Has_Inline_Always)
4378 and then not Expander_Active)
4379 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4381 Needs_Body := False;
4384 -- If the current context is generic, and the package being
4385 -- instantiated is declared within a formal package, there is no
4386 -- body to instantiate until the enclosing generic is instantiated
4387 -- and there is an actual for the formal package. If the formal
4388 -- package has parameters, we build a regular package instance for
4389 -- it, that precedes the original formal package declaration.
4391 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4393 Decl : constant Node_Id :=
4395 (Unit_Declaration_Node (Scope (Gen_Unit)));
4397 if Nkind (Decl) = N_Formal_Package_Declaration
4398 or else (Nkind (Decl) = N_Package_Declaration
4399 and then Is_List_Member (Decl)
4400 and then Present (Next (Decl))
4402 Nkind (Next (Decl)) =
4403 N_Formal_Package_Declaration)
4405 Needs_Body := False;
4411 -- For RCI unit calling stubs, we omit the instance body if the
4412 -- instance is the RCI library unit itself.
4414 -- However there is a special case for nested instances: in this case
4415 -- we do generate the instance body, as it might be required, e.g.
4416 -- because it provides stream attributes for some type used in the
4417 -- profile of a remote subprogram. This is consistent with 12.3(12),
4418 -- which indicates that the instance body occurs at the place of the
4419 -- instantiation, and thus is part of the RCI declaration, which is
4420 -- present on all client partitions (this is E.2.3(18)).
4422 -- Note that AI12-0002 may make it illegal at some point to have
4423 -- stream attributes defined in an RCI unit, in which case this
4424 -- special case will become unnecessary. In the meantime, there
4425 -- is known application code in production that depends on this
4426 -- being possible, so we definitely cannot eliminate the body in
4427 -- the case of nested instances for the time being.
4429 -- When we generate a nested instance body, calling stubs for any
4430 -- relevant subprogram will be be inserted immediately after the
4431 -- subprogram declarations, and will take precedence over the
4432 -- subsequent (original) body. (The stub and original body will be
4433 -- complete homographs, but this is permitted in an instance).
4434 -- (Could we do better and remove the original body???)
4436 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4437 and then Comes_From_Source (N)
4438 and then Nkind (Parent (N)) = N_Compilation_Unit
4440 Needs_Body := False;
4445 -- Here is a defence against a ludicrous number of instantiations
4446 -- caused by a circular set of instantiation attempts.
4448 if Pending_Instantiations.Last > Maximum_Instantiations then
4449 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4450 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4451 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4452 raise Unrecoverable_Error;
4455 -- Indicate that the enclosing scopes contain an instantiation,
4456 -- and that cleanup actions should be delayed until after the
4457 -- instance body is expanded.
4459 Check_Forward_Instantiation (Gen_Decl);
4460 if Nkind (N) = N_Package_Instantiation then
4462 Enclosing_Master : Entity_Id;
4465 -- Loop to search enclosing masters
4467 Enclosing_Master := Current_Scope;
4468 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4469 if Ekind (Enclosing_Master) = E_Package then
4470 if Is_Compilation_Unit (Enclosing_Master) then
4471 if In_Package_Body (Enclosing_Master) then
4473 (Body_Entity (Enclosing_Master));
4482 Enclosing_Master := Scope (Enclosing_Master);
4485 elsif Is_Generic_Unit (Enclosing_Master)
4486 or else Ekind (Enclosing_Master) = E_Void
4488 -- Cleanup actions will eventually be performed on the
4489 -- enclosing subprogram or package instance, if any.
4490 -- Enclosing scope is void in the formal part of a
4491 -- generic subprogram.
4496 if Ekind (Enclosing_Master) = E_Entry
4498 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4500 if not Expander_Active then
4504 Protected_Body_Subprogram (Enclosing_Master);
4508 Set_Delay_Cleanups (Enclosing_Master);
4510 while Ekind (Enclosing_Master) = E_Block loop
4511 Enclosing_Master := Scope (Enclosing_Master);
4514 if Is_Subprogram (Enclosing_Master) then
4515 Delay_Descriptors (Enclosing_Master);
4517 elsif Is_Task_Type (Enclosing_Master) then
4519 TBP : constant Node_Id :=
4520 Get_Task_Body_Procedure
4523 if Present (TBP) then
4524 Delay_Descriptors (TBP);
4525 Set_Delay_Cleanups (TBP);
4532 end loop Scope_Loop;
4535 -- Make entry in table
4537 Add_Pending_Instantiation (N, Act_Decl);
4541 Set_Categorization_From_Pragmas (Act_Decl);
4543 if Parent_Installed then
4547 Set_Instance_Spec (N, Act_Decl);
4549 -- If not a compilation unit, insert the package declaration before
4550 -- the original instantiation node.
4552 if Nkind (Parent (N)) /= N_Compilation_Unit then
4553 Mark_Rewrite_Insertion (Act_Decl);
4554 Insert_Before (N, Act_Decl);
4556 if Has_Aspects (N) then
4557 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4559 -- The pragma created for a Default_Storage_Pool aspect must
4560 -- appear ahead of the declarations in the instance spec.
4561 -- Analysis has placed it after the instance node, so remove
4562 -- it and reinsert it properly now.
4565 ASN : constant Node_Id := First (Aspect_Specifications (N));
4566 A_Name : constant Name_Id := Chars (Identifier (ASN));
4570 if A_Name = Name_Default_Storage_Pool then
4571 if No (Visible_Declarations (Act_Spec)) then
4572 Set_Visible_Declarations (Act_Spec, New_List);
4576 while Present (Decl) loop
4577 if Nkind (Decl) = N_Pragma then
4579 Prepend (Decl, Visible_Declarations (Act_Spec));
4591 -- For an instantiation that is a compilation unit, place
4592 -- declaration on current node so context is complete for analysis
4593 -- (including nested instantiations). If this is the main unit,
4594 -- the declaration eventually replaces the instantiation node.
4595 -- If the instance body is created later, it replaces the
4596 -- instance node, and the declaration is attached to it
4597 -- (see Build_Instance_Compilation_Unit_Nodes).
4600 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4602 -- The entity for the current unit is the newly created one,
4603 -- and all semantic information is attached to it.
4605 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4607 -- If this is the main unit, replace the main entity as well
4609 if Current_Sem_Unit = Main_Unit then
4610 Main_Unit_Entity := Act_Decl_Id;
4614 Set_Unit (Parent (N), Act_Decl);
4615 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4616 Set_Package_Instantiation (Act_Decl_Id, N);
4618 -- Process aspect specifications of the instance node, if any, to
4619 -- take into account categorization pragmas before analyzing the
4622 if Has_Aspects (N) then
4623 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4627 Set_Unit (Parent (N), N);
4628 Set_Body_Required (Parent (N), False);
4630 -- We never need elaboration checks on instantiations, since by
4631 -- definition, the body instantiation is elaborated at the same
4632 -- time as the spec instantiation.
4634 if Legacy_Elaboration_Checks then
4635 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4636 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4640 if Legacy_Elaboration_Checks then
4641 Check_Elab_Instantiation (N);
4644 -- Save the scenario for later examination by the ABE Processing
4647 Record_Elaboration_Scenario (N);
4649 -- The instantiation results in a guaranteed ABE
4651 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4653 -- Do not instantiate the corresponding body because gigi cannot
4654 -- handle certain types of premature instantiations.
4656 Pending_Instantiations.Decrement_Last;
4658 -- Create completing bodies for all subprogram declarations since
4659 -- their real bodies will not be instantiated.
4661 Provide_Completing_Bodies (Instance_Spec (N));
4664 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4666 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4667 First_Private_Entity (Act_Decl_Id));
4669 -- If the instantiation will receive a body, the unit will be
4670 -- transformed into a package body, and receive its own elaboration
4671 -- entity. Otherwise, the nature of the unit is now a package
4674 if Nkind (Parent (N)) = N_Compilation_Unit
4675 and then not Needs_Body
4677 Rewrite (N, Act_Decl);
4680 if Present (Corresponding_Body (Gen_Decl))
4681 or else Unit_Requires_Body (Gen_Unit)
4683 Set_Has_Completion (Act_Decl_Id);
4686 Check_Formal_Packages (Act_Decl_Id);
4688 Restore_Hidden_Primitives (Vis_Prims_List);
4689 Restore_Private_Views (Act_Decl_Id);
4691 Inherit_Context (Gen_Decl, N);
4693 if Parent_Installed then
4698 Env_Installed := False;
4701 Validate_Categorization_Dependency (N, Act_Decl_Id);
4703 -- There used to be a check here to prevent instantiations in local
4704 -- contexts if the No_Local_Allocators restriction was active. This
4705 -- check was removed by a binding interpretation in AI-95-00130/07,
4706 -- but we retain the code for documentation purposes.
4708 -- if Ekind (Act_Decl_Id) /= E_Void
4709 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4711 -- Check_Restriction (No_Local_Allocators, N);
4715 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4718 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4719 -- be used as defining identifiers for a formal package and for the
4720 -- corresponding expanded package.
4722 if Nkind (N) = N_Formal_Package_Declaration then
4723 Act_Decl_Id := New_Copy (Defining_Entity (N));
4724 Set_Comes_From_Source (Act_Decl_Id, True);
4725 Set_Is_Generic_Instance (Act_Decl_Id, False);
4726 Set_Defining_Identifier (N, Act_Decl_Id);
4729 -- Check that if N is an instantiation of System.Dim_Float_IO or
4730 -- System.Dim_Integer_IO, the formal type has a dimension system.
4732 if Nkind (N) = N_Package_Instantiation
4733 and then Is_Dim_IO_Package_Instantiation (N)
4736 Assoc : constant Node_Id := First (Generic_Associations (N));
4738 if not Has_Dimension_System
4739 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4741 Error_Msg_N ("type with a dimension system expected", Assoc);
4747 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4748 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4751 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4752 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4753 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4754 Style_Check := Saved_Style_Check;
4757 when Instantiation_Error =>
4758 if Parent_Installed then
4762 if Env_Installed then
4766 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4767 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4768 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4769 Style_Check := Saved_Style_Check;
4770 end Analyze_Package_Instantiation;
4772 --------------------------
4773 -- Inline_Instance_Body --
4774 --------------------------
4776 -- WARNING: This routine manages SPARK regions. Return statements must be
4777 -- replaced by gotos which jump to the end of the routine and restore the
4780 procedure Inline_Instance_Body
4782 Gen_Unit : Entity_Id;
4785 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4786 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4787 Gen_Comp : constant Entity_Id :=
4788 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4790 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4791 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4792 -- Save the SPARK mode-related data to restore on exit. Removing
4793 -- enclosing scopes to provide a clean environment for analysis of
4794 -- the inlined body will eliminate any previously set SPARK_Mode.
4796 Scope_Stack_Depth : constant Pos :=
4797 Scope_Stack.Last - Scope_Stack.First + 1;
4799 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4800 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4801 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4803 Curr_Scope : Entity_Id := Empty;
4804 List : Elist_Id := No_Elist; -- init to avoid warning
4805 N_Instances : Nat := 0;
4806 Num_Inner : Nat := 0;
4807 Num_Scopes : Nat := 0;
4808 Removed : Boolean := False;
4813 -- Case of generic unit defined in another unit. We must remove the
4814 -- complete context of the current unit to install that of the generic.
4816 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4818 -- Add some comments for the following two loops ???
4821 while Present (S) and then S /= Standard_Standard loop
4823 Num_Scopes := Num_Scopes + 1;
4825 Use_Clauses (Num_Scopes) :=
4827 (Scope_Stack.Last - Num_Scopes + 1).
4829 End_Use_Clauses (Use_Clauses (Num_Scopes));
4831 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4832 or else Scope_Stack.Table
4833 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4836 exit when Is_Generic_Instance (S)
4837 and then (In_Package_Body (S)
4838 or else Ekind (S) = E_Procedure
4839 or else Ekind (S) = E_Function);
4843 Vis := Is_Immediately_Visible (Gen_Comp);
4845 -- Find and save all enclosing instances
4850 and then S /= Standard_Standard
4852 if Is_Generic_Instance (S) then
4853 N_Instances := N_Instances + 1;
4854 Instances (N_Instances) := S;
4856 exit when In_Package_Body (S);
4862 -- Remove context of current compilation unit, unless we are within a
4863 -- nested package instantiation, in which case the context has been
4864 -- removed previously.
4866 -- If current scope is the body of a child unit, remove context of
4867 -- spec as well. If an enclosing scope is an instance body, the
4868 -- context has already been removed, but the entities in the body
4869 -- must be made invisible as well.
4872 while Present (S) and then S /= Standard_Standard loop
4873 if Is_Generic_Instance (S)
4874 and then (In_Package_Body (S)
4875 or else Ekind_In (S, E_Procedure, E_Function))
4877 -- We still have to remove the entities of the enclosing
4878 -- instance from direct visibility.
4883 E := First_Entity (S);
4884 while Present (E) loop
4885 Set_Is_Immediately_Visible (E, False);
4894 or else (Ekind (Curr_Unit) = E_Package_Body
4895 and then S = Spec_Entity (Curr_Unit))
4896 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4897 and then S = Corresponding_Spec
4898 (Unit_Declaration_Node (Curr_Unit)))
4902 -- Remove entities in current scopes from visibility, so that
4903 -- instance body is compiled in a clean environment.
4905 List := Save_Scope_Stack (Handle_Use => False);
4907 if Is_Child_Unit (S) then
4909 -- Remove child unit from stack, as well as inner scopes.
4910 -- Removing the context of a child unit removes parent units
4913 while Current_Scope /= S loop
4914 Num_Inner := Num_Inner + 1;
4915 Inner_Scopes (Num_Inner) := Current_Scope;
4920 Remove_Context (Curr_Comp);
4924 Remove_Context (Curr_Comp);
4927 if Ekind (Curr_Unit) = E_Package_Body then
4928 Remove_Context (Library_Unit (Curr_Comp));
4935 pragma Assert (Num_Inner < Num_Scopes);
4937 -- The inlined package body must be analyzed with the SPARK_Mode of
4938 -- the enclosing context, otherwise the body may cause bogus errors
4939 -- if a configuration SPARK_Mode pragma in in effect.
4941 Push_Scope (Standard_Standard);
4942 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4943 Instantiate_Package_Body
4946 Act_Decl => Act_Decl,
4947 Expander_Status => Expander_Active,
4948 Current_Sem_Unit => Current_Sem_Unit,
4949 Scope_Suppress => Scope_Suppress,
4950 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4951 Version => Ada_Version,
4952 Version_Pragma => Ada_Version_Pragma,
4953 Warnings => Save_Warnings,
4954 SPARK_Mode => Saved_SM,
4955 SPARK_Mode_Pragma => Saved_SMP)),
4956 Inlined_Body => True);
4962 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4964 -- Reset Generic_Instance flag so that use clauses can be installed
4965 -- in the proper order. (See Use_One_Package for effect of enclosing
4966 -- instances on processing of use clauses).
4968 for J in 1 .. N_Instances loop
4969 Set_Is_Generic_Instance (Instances (J), False);
4973 Install_Context (Curr_Comp, Chain => False);
4975 if Present (Curr_Scope)
4976 and then Is_Child_Unit (Curr_Scope)
4978 Push_Scope (Curr_Scope);
4979 Set_Is_Immediately_Visible (Curr_Scope);
4981 -- Finally, restore inner scopes as well
4983 for J in reverse 1 .. Num_Inner loop
4984 Push_Scope (Inner_Scopes (J));
4988 Restore_Scope_Stack (List, Handle_Use => False);
4990 if Present (Curr_Scope)
4992 (In_Private_Part (Curr_Scope)
4993 or else In_Package_Body (Curr_Scope))
4995 -- Install private declaration of ancestor units, which are
4996 -- currently available. Restore_Scope_Stack and Install_Context
4997 -- only install the visible part of parents.
5002 Par := Scope (Curr_Scope);
5003 while (Present (Par)) and then Par /= Standard_Standard loop
5004 Install_Private_Declarations (Par);
5011 -- Restore use clauses. For a child unit, use clauses in the parents
5012 -- are restored when installing the context, so only those in inner
5013 -- scopes (and those local to the child unit itself) need to be
5014 -- installed explicitly.
5016 if Is_Child_Unit (Curr_Unit) and then Removed then
5017 for J in reverse 1 .. Num_Inner + 1 loop
5018 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5020 Install_Use_Clauses (Use_Clauses (J));
5024 for J in reverse 1 .. Num_Scopes loop
5025 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5027 Install_Use_Clauses (Use_Clauses (J));
5031 -- Restore status of instances. If one of them is a body, make its
5032 -- local entities visible again.
5039 for J in 1 .. N_Instances loop
5040 Inst := Instances (J);
5041 Set_Is_Generic_Instance (Inst, True);
5043 if In_Package_Body (Inst)
5044 or else Ekind_In (S, E_Procedure, E_Function)
5046 E := First_Entity (Instances (J));
5047 while Present (E) loop
5048 Set_Is_Immediately_Visible (E);
5055 -- If generic unit is in current unit, current context is correct. Note
5056 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5057 -- enclosing scopes were removed.
5060 Instantiate_Package_Body
5063 Act_Decl => Act_Decl,
5064 Expander_Status => Expander_Active,
5065 Current_Sem_Unit => Current_Sem_Unit,
5066 Scope_Suppress => Scope_Suppress,
5067 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5068 Version => Ada_Version,
5069 Version_Pragma => Ada_Version_Pragma,
5070 Warnings => Save_Warnings,
5071 SPARK_Mode => SPARK_Mode,
5072 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
5073 Inlined_Body => True);
5075 end Inline_Instance_Body;
5077 -------------------------------------
5078 -- Analyze_Procedure_Instantiation --
5079 -------------------------------------
5081 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5083 Analyze_Subprogram_Instantiation (N, E_Procedure);
5084 end Analyze_Procedure_Instantiation;
5086 -----------------------------------
5087 -- Need_Subprogram_Instance_Body --
5088 -----------------------------------
5090 function Need_Subprogram_Instance_Body
5092 Subp : Entity_Id) return Boolean
5094 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5095 -- Return True if E is an inlined subprogram, an inlined renaming or a
5096 -- subprogram nested in an inlined subprogram. The inlining machinery
5097 -- totally disregards nested subprograms since it considers that they
5098 -- will always be compiled if the parent is (see Inline.Is_Nested).
5100 ------------------------------------
5101 -- Is_Inlined_Or_Child_Of_Inlined --
5102 ------------------------------------
5104 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5108 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5113 while Scop /= Standard_Standard loop
5114 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5118 Scop := Scope (Scop);
5122 end Is_Inlined_Or_Child_Of_Inlined;
5125 -- Must be in the main unit or inlined (or child of inlined)
5127 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5129 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5131 and then (Operating_Mode = Generate_Code
5132 or else (Operating_Mode = Check_Semantics
5133 and then (ASIS_Mode or GNATprove_Mode)))
5135 -- The body is needed when generating code (full expansion), in ASIS
5136 -- mode for other tools, and in GNATprove mode (special expansion) for
5137 -- formal verification of the body itself.
5139 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5141 -- No point in inlining if ABE is inevitable
5143 and then not Is_Known_Guaranteed_ABE (N)
5145 -- Or if subprogram is eliminated
5147 and then not Is_Eliminated (Subp)
5149 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5152 -- Here if not inlined, or we ignore the inlining
5157 end Need_Subprogram_Instance_Body;
5159 --------------------------------------
5160 -- Analyze_Subprogram_Instantiation --
5161 --------------------------------------
5163 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5164 -- must be replaced by gotos which jump to the end of the routine in order
5165 -- to restore the Ghost and SPARK modes.
5167 procedure Analyze_Subprogram_Instantiation
5171 Loc : constant Source_Ptr := Sloc (N);
5172 Gen_Id : constant Node_Id := Name (N);
5173 Errs : constant Nat := Serious_Errors_Detected;
5175 Anon_Id : constant Entity_Id :=
5176 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5177 Chars => New_External_Name
5178 (Chars (Defining_Entity (N)), 'R'));
5180 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5185 Env_Installed : Boolean := False;
5186 Gen_Unit : Entity_Id;
5188 Pack_Id : Entity_Id;
5189 Parent_Installed : Boolean := False;
5191 Renaming_List : List_Id;
5192 -- The list of declarations that link formals and actuals of the
5193 -- instance. These are subtype declarations for formal types, and
5194 -- renaming declarations for other formals. The subprogram declaration
5195 -- for the instance is then appended to the list, and the last item on
5196 -- the list is the renaming declaration for the instance.
5198 procedure Analyze_Instance_And_Renamings;
5199 -- The instance must be analyzed in a context that includes the mappings
5200 -- of generic parameters into actuals. We create a package declaration
5201 -- for this purpose, and a subprogram with an internal name within the
5202 -- package. The subprogram instance is simply an alias for the internal
5203 -- subprogram, declared in the current scope.
5205 procedure Build_Subprogram_Renaming;
5206 -- If the subprogram is recursive, there are occurrences of the name of
5207 -- the generic within the body, which must resolve to the current
5208 -- instance. We add a renaming declaration after the declaration, which
5209 -- is available in the instance body, as well as in the analysis of
5210 -- aspects that appear in the generic. This renaming declaration is
5211 -- inserted after the instance declaration which it renames.
5213 ------------------------------------
5214 -- Analyze_Instance_And_Renamings --
5215 ------------------------------------
5217 procedure Analyze_Instance_And_Renamings is
5218 Def_Ent : constant Entity_Id := Defining_Entity (N);
5219 Pack_Decl : Node_Id;
5222 if Nkind (Parent (N)) = N_Compilation_Unit then
5224 -- For the case of a compilation unit, the container package has
5225 -- the same name as the instantiation, to insure that the binder
5226 -- calls the elaboration procedure with the right name. Copy the
5227 -- entity of the instance, which may have compilation level flags
5228 -- (e.g. Is_Child_Unit) set.
5230 Pack_Id := New_Copy (Def_Ent);
5233 -- Otherwise we use the name of the instantiation concatenated
5234 -- with its source position to ensure uniqueness if there are
5235 -- several instantiations with the same name.
5238 Make_Defining_Identifier (Loc,
5239 Chars => New_External_Name
5240 (Related_Id => Chars (Def_Ent),
5242 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5246 Make_Package_Declaration (Loc,
5247 Specification => Make_Package_Specification (Loc,
5248 Defining_Unit_Name => Pack_Id,
5249 Visible_Declarations => Renaming_List,
5250 End_Label => Empty));
5252 Set_Instance_Spec (N, Pack_Decl);
5253 Set_Is_Generic_Instance (Pack_Id);
5254 Set_Debug_Info_Needed (Pack_Id);
5256 -- Case of not a compilation unit
5258 if Nkind (Parent (N)) /= N_Compilation_Unit then
5259 Mark_Rewrite_Insertion (Pack_Decl);
5260 Insert_Before (N, Pack_Decl);
5261 Set_Has_Completion (Pack_Id);
5263 -- Case of an instantiation that is a compilation unit
5265 -- Place declaration on current node so context is complete for
5266 -- analysis (including nested instantiations), and for use in a
5267 -- context_clause (see Analyze_With_Clause).
5270 Set_Unit (Parent (N), Pack_Decl);
5271 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5274 Analyze (Pack_Decl);
5275 Check_Formal_Packages (Pack_Id);
5276 Set_Is_Generic_Instance (Pack_Id, False);
5278 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5281 -- Body of the enclosing package is supplied when instantiating the
5282 -- subprogram body, after semantic analysis is completed.
5284 if Nkind (Parent (N)) = N_Compilation_Unit then
5286 -- Remove package itself from visibility, so it does not
5287 -- conflict with subprogram.
5289 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5291 -- Set name and scope of internal subprogram so that the proper
5292 -- external name will be generated. The proper scope is the scope
5293 -- of the wrapper package. We need to generate debugging info for
5294 -- the internal subprogram, so set flag accordingly.
5296 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5297 Set_Scope (Anon_Id, Scope (Pack_Id));
5299 -- Mark wrapper package as referenced, to avoid spurious warnings
5300 -- if the instantiation appears in various with_ clauses of
5301 -- subunits of the main unit.
5303 Set_Referenced (Pack_Id);
5306 Set_Is_Generic_Instance (Anon_Id);
5307 Set_Debug_Info_Needed (Anon_Id);
5308 Act_Decl_Id := New_Copy (Anon_Id);
5310 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5311 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5312 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5314 -- Subprogram instance comes from source only if generic does
5316 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5318 -- If the instance is a child unit, mark the Id accordingly. Mark
5319 -- the anonymous entity as well, which is the real subprogram and
5320 -- which is used when the instance appears in a context clause.
5321 -- Similarly, propagate the Is_Eliminated flag to handle properly
5322 -- nested eliminated subprograms.
5324 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5325 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5326 New_Overloaded_Entity (Act_Decl_Id);
5327 Check_Eliminated (Act_Decl_Id);
5328 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5330 if Nkind (Parent (N)) = N_Compilation_Unit then
5332 -- In compilation unit case, kill elaboration checks on the
5333 -- instantiation, since they are never needed - the body is
5334 -- instantiated at the same point as the spec.
5336 if Legacy_Elaboration_Checks then
5337 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5338 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5341 Set_Is_Compilation_Unit (Anon_Id);
5342 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5345 -- The instance is not a freezing point for the new subprogram.
5346 -- The anonymous subprogram may have a freeze node, created for
5347 -- some delayed aspects. This freeze node must not be inherited
5348 -- by the visible subprogram entity.
5350 Set_Is_Frozen (Act_Decl_Id, False);
5351 Set_Freeze_Node (Act_Decl_Id, Empty);
5353 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5354 Valid_Operator_Definition (Act_Decl_Id);
5357 Set_Alias (Act_Decl_Id, Anon_Id);
5358 Set_Has_Completion (Act_Decl_Id);
5359 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5361 if Nkind (Parent (N)) = N_Compilation_Unit then
5362 Set_Body_Required (Parent (N), False);
5364 end Analyze_Instance_And_Renamings;
5366 -------------------------------
5367 -- Build_Subprogram_Renaming --
5368 -------------------------------
5370 procedure Build_Subprogram_Renaming is
5371 Renaming_Decl : Node_Id;
5372 Unit_Renaming : Node_Id;
5376 Make_Subprogram_Renaming_Declaration (Loc,
5379 (Specification (Original_Node (Gen_Decl)),
5381 Instantiating => True),
5382 Name => New_Occurrence_Of (Anon_Id, Loc));
5384 -- The generic may be a a child unit. The renaming needs an
5385 -- identifier with the proper name.
5387 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5388 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5390 -- If there is a formal subprogram with the same name as the unit
5391 -- itself, do not add this renaming declaration, to prevent
5392 -- ambiguities when there is a call with that name in the body.
5393 -- This is a partial and ugly fix for one ACATS test. ???
5395 Renaming_Decl := First (Renaming_List);
5396 while Present (Renaming_Decl) loop
5397 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5399 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5404 Next (Renaming_Decl);
5407 if No (Renaming_Decl) then
5408 Append (Unit_Renaming, Renaming_List);
5410 end Build_Subprogram_Renaming;
5414 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5415 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5416 Saved_ISMP : constant Boolean :=
5417 Ignore_SPARK_Mode_Pragmas_In_Instance;
5418 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5419 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5420 -- Save the Ghost and SPARK mode-related data to restore on exit
5422 Vis_Prims_List : Elist_Id := No_Elist;
5423 -- List of primitives made temporarily visible in the instantiation
5424 -- to match the visibility of the formal type
5426 -- Start of processing for Analyze_Subprogram_Instantiation
5429 -- Preserve relevant elaboration-related attributes of the context which
5430 -- are no longer available or very expensive to recompute once analysis,
5431 -- resolution, and expansion are over.
5433 Mark_Elaboration_Attributes
5440 Check_SPARK_05_Restriction ("generic is not allowed", N);
5442 -- Very first thing: check for special Text_IO unit in case we are
5443 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5444 -- such an instantiation is bogus (these are packages, not subprograms),
5445 -- but we get a better error message if we do this.
5447 Check_Text_IO_Special_Unit (Gen_Id);
5449 -- Make node global for error reporting
5451 Instantiation_Node := N;
5453 -- For package instantiations we turn off style checks, because they
5454 -- will have been emitted in the generic. For subprogram instantiations
5455 -- we want to apply at least the check on overriding indicators so we
5456 -- do not modify the style check status.
5458 -- The renaming declarations for the actuals do not come from source and
5459 -- will not generate spurious warnings.
5461 Preanalyze_Actuals (N);
5464 Env_Installed := True;
5465 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5466 Gen_Unit := Entity (Gen_Id);
5468 -- A subprogram instantiation is Ghost when it is subject to pragma
5469 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5470 -- that any nodes generated during analysis and expansion are marked as
5473 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5475 Generate_Reference (Gen_Unit, Gen_Id);
5477 if Nkind (Gen_Id) = N_Identifier
5478 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5481 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5484 if Etype (Gen_Unit) = Any_Type then
5489 -- Verify that it is a generic subprogram of the right kind, and that
5490 -- it does not lead to a circular instantiation.
5492 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5494 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5496 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5498 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5500 elsif In_Open_Scopes (Gen_Unit) then
5501 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5504 Set_Entity (Gen_Id, Gen_Unit);
5505 Set_Is_Instantiated (Gen_Unit);
5507 if In_Extended_Main_Source_Unit (N) then
5508 Generate_Reference (Gen_Unit, N);
5511 -- If renaming, get original unit
5513 if Present (Renamed_Object (Gen_Unit))
5514 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5517 Gen_Unit := Renamed_Object (Gen_Unit);
5518 Set_Is_Instantiated (Gen_Unit);
5519 Generate_Reference (Gen_Unit, N);
5522 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5523 Error_Msg_Node_2 := Current_Scope;
5525 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5526 Circularity_Detected := True;
5527 Restore_Hidden_Primitives (Vis_Prims_List);
5531 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5533 -- Initialize renamings map, for error checking
5535 Generic_Renamings.Set_Last (0);
5536 Generic_Renamings_HTable.Reset;
5538 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5540 -- Copy original generic tree, to produce text for instantiation
5544 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5546 -- Inherit overriding indicator from instance node
5548 Act_Spec := Specification (Act_Tree);
5549 Set_Must_Override (Act_Spec, Must_Override (N));
5550 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5553 Analyze_Associations
5555 Formals => Generic_Formal_Declarations (Act_Tree),
5556 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5558 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5560 -- The subprogram itself cannot contain a nested instance, so the
5561 -- current parent is left empty.
5563 Set_Instance_Env (Gen_Unit, Empty);
5565 -- Build the subprogram declaration, which does not appear in the
5566 -- generic template, and give it a sloc consistent with that of the
5569 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5570 Set_Generic_Parent (Act_Spec, Gen_Unit);
5572 Make_Subprogram_Declaration (Sloc (Act_Spec),
5573 Specification => Act_Spec);
5575 -- The aspects have been copied previously, but they have to be
5576 -- linked explicitly to the new subprogram declaration. Explicit
5577 -- pre/postconditions on the instance are analyzed below, in a
5580 Move_Aspects (Act_Tree, To => Act_Decl);
5581 Set_Categorization_From_Pragmas (Act_Decl);
5583 if Parent_Installed then
5587 Append (Act_Decl, Renaming_List);
5589 -- Contract-related source pragmas that follow a generic subprogram
5590 -- must be instantiated explicitly because they are not part of the
5591 -- subprogram template.
5593 Instantiate_Subprogram_Contract
5594 (Original_Node (Gen_Decl), Renaming_List);
5596 Build_Subprogram_Renaming;
5598 -- If the context of the instance is subject to SPARK_Mode "off" or
5599 -- the annotation is altogether missing, set the global flag which
5600 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5601 -- the instance. This should be done prior to analyzing the instance.
5603 if SPARK_Mode /= On then
5604 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5607 -- If the context of an instance is not subject to SPARK_Mode "off",
5608 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5609 -- the latter should be the one applicable to the instance.
5611 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5612 and then Saved_SM /= Off
5613 and then Present (SPARK_Pragma (Gen_Unit))
5615 Set_SPARK_Mode (Gen_Unit);
5618 Analyze_Instance_And_Renamings;
5620 -- Restore SPARK_Mode from the context after analysis of the package
5621 -- declaration, so that the SPARK_Mode on the generic spec does not
5622 -- apply to the pending instance for the instance body.
5624 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5625 and then Saved_SM /= Off
5626 and then Present (SPARK_Pragma (Gen_Unit))
5628 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5631 -- If the generic is marked Import (Intrinsic), then so is the
5632 -- instance. This indicates that there is no body to instantiate. If
5633 -- generic is marked inline, so it the instance, and the anonymous
5634 -- subprogram it renames. If inlined, or else if inlining is enabled
5635 -- for the compilation, we generate the instance body even if it is
5636 -- not within the main unit.
5638 if Is_Intrinsic_Subprogram (Gen_Unit) then
5639 Set_Is_Intrinsic_Subprogram (Anon_Id);
5640 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5642 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5643 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5647 -- Inherit convention from generic unit. Intrinsic convention, as for
5648 -- an instance of unchecked conversion, is not inherited because an
5649 -- explicit Ada instance has been created.
5651 if Has_Convention_Pragma (Gen_Unit)
5652 and then Convention (Gen_Unit) /= Convention_Intrinsic
5654 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5655 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5658 Generate_Definition (Act_Decl_Id);
5660 -- Inherit all inlining-related flags which apply to the generic in
5661 -- the subprogram and its declaration.
5663 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5664 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5666 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5667 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5669 -- Propagate No_Return if pragma applied to generic unit. This must
5670 -- be done explicitly because pragma does not appear in generic
5671 -- declaration (unlike the aspect case).
5673 if No_Return (Gen_Unit) then
5674 Set_No_Return (Act_Decl_Id);
5675 Set_No_Return (Anon_Id);
5678 Set_Has_Pragma_Inline_Always
5679 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5680 Set_Has_Pragma_Inline_Always
5681 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5683 -- Mark both the instance spec and the anonymous package in case the
5684 -- body is instantiated at a later pass. This preserves the original
5685 -- context in effect for the body.
5687 if SPARK_Mode /= On then
5688 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5689 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5692 if Legacy_Elaboration_Checks
5693 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5695 Check_Elab_Instantiation (N);
5698 -- Save the scenario for later examination by the ABE Processing
5701 Record_Elaboration_Scenario (N);
5703 -- The instantiation results in a guaranteed ABE. Create a completing
5704 -- body for the subprogram declaration because the real body will not
5707 if Is_Known_Guaranteed_ABE (N) then
5708 Provide_Completing_Bodies (Instance_Spec (N));
5711 if Is_Dispatching_Operation (Act_Decl_Id)
5712 and then Ada_Version >= Ada_2005
5718 Formal := First_Formal (Act_Decl_Id);
5719 while Present (Formal) loop
5720 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5721 and then Is_Controlling_Formal (Formal)
5722 and then not Can_Never_Be_Null (Formal)
5725 ("access parameter& is controlling,", N, Formal);
5727 ("\corresponding parameter of & must be explicitly "
5728 & "null-excluding", N, Gen_Id);
5731 Next_Formal (Formal);
5736 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5738 Validate_Categorization_Dependency (N, Act_Decl_Id);
5740 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5741 Inherit_Context (Gen_Decl, N);
5743 Restore_Private_Views (Pack_Id, False);
5745 -- If the context requires a full instantiation, mark node for
5746 -- subsequent construction of the body.
5748 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5749 Check_Forward_Instantiation (Gen_Decl);
5751 -- The wrapper package is always delayed, because it does not
5752 -- constitute a freeze point, but to insure that the freeze node
5753 -- is placed properly, it is created directly when instantiating
5754 -- the body (otherwise the freeze node might appear to early for
5755 -- nested instantiations). For ASIS purposes, indicate that the
5756 -- wrapper package has replaced the instantiation node.
5758 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5759 Rewrite (N, Unit (Parent (N)));
5760 Set_Unit (Parent (N), N);
5763 -- Replace instance node for library-level instantiations of
5764 -- intrinsic subprograms, for ASIS use.
5766 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5767 Rewrite (N, Unit (Parent (N)));
5768 Set_Unit (Parent (N), N);
5771 if Parent_Installed then
5775 Restore_Hidden_Primitives (Vis_Prims_List);
5777 Env_Installed := False;
5778 Generic_Renamings.Set_Last (0);
5779 Generic_Renamings_HTable.Reset;
5783 -- Analyze aspects in declaration if no errors appear in the instance.
5785 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5786 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5789 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5790 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5791 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5794 when Instantiation_Error =>
5795 if Parent_Installed then
5799 if Env_Installed then
5803 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5804 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5805 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5806 end Analyze_Subprogram_Instantiation;
5808 -------------------------
5809 -- Get_Associated_Node --
5810 -------------------------
5812 function Get_Associated_Node (N : Node_Id) return Node_Id is
5816 Assoc := Associated_Node (N);
5818 if Nkind (Assoc) /= Nkind (N) then
5821 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5825 -- If the node is part of an inner generic, it may itself have been
5826 -- remapped into a further generic copy. Associated_Node is otherwise
5827 -- used for the entity of the node, and will be of a different node
5828 -- kind, or else N has been rewritten as a literal or function call.
5830 while Present (Associated_Node (Assoc))
5831 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5833 Assoc := Associated_Node (Assoc);
5836 -- Follow an additional link in case the final node was rewritten.
5837 -- This can only happen with nested generic units.
5839 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5840 and then Present (Associated_Node (Assoc))
5841 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5842 N_Explicit_Dereference,
5847 Assoc := Associated_Node (Assoc);
5850 -- An additional special case: an unconstrained type in an object
5851 -- declaration may have been rewritten as a local subtype constrained
5852 -- by the expression in the declaration. We need to recover the
5853 -- original entity, which may be global.
5855 if Present (Original_Node (Assoc))
5856 and then Nkind (Parent (N)) = N_Object_Declaration
5858 Assoc := Original_Node (Assoc);
5863 end Get_Associated_Node;
5865 ----------------------------
5866 -- Build_Function_Wrapper --
5867 ----------------------------
5869 function Build_Function_Wrapper
5870 (Formal_Subp : Entity_Id;
5871 Actual_Subp : Entity_Id) return Node_Id
5873 Loc : constant Source_Ptr := Sloc (Current_Scope);
5874 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5877 Func_Name : Node_Id;
5879 Parm_Type : Node_Id;
5880 Profile : List_Id := New_List;
5887 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5889 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5890 Set_Ekind (Func, E_Function);
5891 Set_Is_Generic_Actual_Subprogram (Func);
5893 Actuals := New_List;
5894 Profile := New_List;
5896 Act_F := First_Formal (Actual_Subp);
5897 Form_F := First_Formal (Formal_Subp);
5898 while Present (Form_F) loop
5900 -- Create new formal for profile of wrapper, and add a reference
5901 -- to it in the list of actuals for the enclosing call. The name
5902 -- must be that of the formal in the formal subprogram, because
5903 -- calls to it in the generic body may use named associations.
5905 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5908 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5911 Make_Parameter_Specification (Loc,
5912 Defining_Identifier => New_F,
5913 Parameter_Type => Parm_Type));
5915 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5916 Next_Formal (Form_F);
5918 if Present (Act_F) then
5919 Next_Formal (Act_F);
5924 Make_Function_Specification (Loc,
5925 Defining_Unit_Name => Func,
5926 Parameter_Specifications => Profile,
5927 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5930 Make_Expression_Function (Loc,
5931 Specification => Spec,
5933 Make_Function_Call (Loc,
5935 Parameter_Associations => Actuals));
5938 end Build_Function_Wrapper;
5940 ----------------------------
5941 -- Build_Operator_Wrapper --
5942 ----------------------------
5944 function Build_Operator_Wrapper
5945 (Formal_Subp : Entity_Id;
5946 Actual_Subp : Entity_Id) return Node_Id
5948 Loc : constant Source_Ptr := Sloc (Current_Scope);
5949 Ret_Type : constant Entity_Id :=
5950 Get_Instance_Of (Etype (Formal_Subp));
5951 Op_Type : constant Entity_Id :=
5952 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5953 Is_Binary : constant Boolean :=
5954 Present (Next_Formal (First_Formal (Formal_Subp)));
5957 Expr : Node_Id := Empty;
5965 Op_Name := Chars (Actual_Subp);
5967 -- Create entities for wrapper function and its formals
5969 F1 := Make_Temporary (Loc, 'A');
5970 F2 := Make_Temporary (Loc, 'B');
5971 L := New_Occurrence_Of (F1, Loc);
5972 R := New_Occurrence_Of (F2, Loc);
5974 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5975 Set_Ekind (Func, E_Function);
5976 Set_Is_Generic_Actual_Subprogram (Func);
5979 Make_Function_Specification (Loc,
5980 Defining_Unit_Name => Func,
5981 Parameter_Specifications => New_List (
5982 Make_Parameter_Specification (Loc,
5983 Defining_Identifier => F1,
5984 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5985 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5988 Append_To (Parameter_Specifications (Spec),
5989 Make_Parameter_Specification (Loc,
5990 Defining_Identifier => F2,
5991 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5994 -- Build expression as a function call, or as an operator node
5995 -- that corresponds to the name of the actual, starting with
5996 -- binary operators.
5998 if Op_Name not in Any_Operator_Name then
6000 Make_Function_Call (Loc,
6002 New_Occurrence_Of (Actual_Subp, Loc),
6003 Parameter_Associations => New_List (L));
6006 Append_To (Parameter_Associations (Expr), R);
6011 elsif Is_Binary then
6012 if Op_Name = Name_Op_And then
6013 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6014 elsif Op_Name = Name_Op_Or then
6015 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6016 elsif Op_Name = Name_Op_Xor then
6017 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6018 elsif Op_Name = Name_Op_Eq then
6019 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6020 elsif Op_Name = Name_Op_Ne then
6021 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6022 elsif Op_Name = Name_Op_Le then
6023 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6024 elsif Op_Name = Name_Op_Gt then
6025 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6026 elsif Op_Name = Name_Op_Ge then
6027 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6028 elsif Op_Name = Name_Op_Lt then
6029 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6030 elsif Op_Name = Name_Op_Add then
6031 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6032 elsif Op_Name = Name_Op_Subtract then
6033 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6034 elsif Op_Name = Name_Op_Concat then
6035 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6036 elsif Op_Name = Name_Op_Multiply then
6037 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6038 elsif Op_Name = Name_Op_Divide then
6039 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6040 elsif Op_Name = Name_Op_Mod then
6041 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6042 elsif Op_Name = Name_Op_Rem then
6043 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6044 elsif Op_Name = Name_Op_Expon then
6045 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6051 if Op_Name = Name_Op_Add then
6052 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6053 elsif Op_Name = Name_Op_Subtract then
6054 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6055 elsif Op_Name = Name_Op_Abs then
6056 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6057 elsif Op_Name = Name_Op_Not then
6058 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6063 Make_Expression_Function (Loc,
6064 Specification => Spec,
6065 Expression => Expr);
6068 end Build_Operator_Wrapper;
6070 -------------------------------------------
6071 -- Build_Instance_Compilation_Unit_Nodes --
6072 -------------------------------------------
6074 procedure Build_Instance_Compilation_Unit_Nodes
6079 Decl_Cunit : Node_Id;
6080 Body_Cunit : Node_Id;
6082 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6083 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6086 -- A new compilation unit node is built for the instance declaration
6089 Make_Compilation_Unit (Sloc (N),
6090 Context_Items => Empty_List,
6092 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6094 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6096 -- The new compilation unit is linked to its body, but both share the
6097 -- same file, so we do not set Body_Required on the new unit so as not
6098 -- to create a spurious dependency on a non-existent body in the ali.
6099 -- This simplifies CodePeer unit traversal.
6101 -- We use the original instantiation compilation unit as the resulting
6102 -- compilation unit of the instance, since this is the main unit.
6104 Rewrite (N, Act_Body);
6106 -- Propagate the aspect specifications from the package body template to
6107 -- the instantiated version of the package body.
6109 if Has_Aspects (Act_Body) then
6110 Set_Aspect_Specifications
6111 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6114 Body_Cunit := Parent (N);
6116 -- The two compilation unit nodes are linked by the Library_Unit field
6118 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6119 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6121 -- Preserve the private nature of the package if needed
6123 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6125 -- If the instance is not the main unit, its context, categorization
6126 -- and elaboration entity are not relevant to the compilation.
6128 if Body_Cunit /= Cunit (Main_Unit) then
6129 Make_Instance_Unit (Body_Cunit, In_Main => False);
6133 -- The context clause items on the instantiation, which are now attached
6134 -- to the body compilation unit (since the body overwrote the original
6135 -- instantiation node), semantically belong on the spec, so copy them
6136 -- there. It's harmless to leave them on the body as well. In fact one
6137 -- could argue that they belong in both places.
6139 Citem := First (Context_Items (Body_Cunit));
6140 while Present (Citem) loop
6141 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6145 -- Propagate categorization flags on packages, so that they appear in
6146 -- the ali file for the spec of the unit.
6148 if Ekind (New_Main) = E_Package then
6149 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6150 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6151 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6152 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6153 Set_Is_Remote_Call_Interface
6154 (Old_Main, Is_Remote_Call_Interface (New_Main));
6157 -- Make entry in Units table, so that binder can generate call to
6158 -- elaboration procedure for body, if any.
6160 Make_Instance_Unit (Body_Cunit, In_Main => True);
6161 Main_Unit_Entity := New_Main;
6162 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6164 -- Build elaboration entity, since the instance may certainly generate
6165 -- elaboration code requiring a flag for protection.
6167 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6168 end Build_Instance_Compilation_Unit_Nodes;
6170 -----------------------------
6171 -- Check_Access_Definition --
6172 -----------------------------
6174 procedure Check_Access_Definition (N : Node_Id) is
6177 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6179 end Check_Access_Definition;
6181 -----------------------------------
6182 -- Check_Formal_Package_Instance --
6183 -----------------------------------
6185 -- If the formal has specific parameters, they must match those of the
6186 -- actual. Both of them are instances, and the renaming declarations for
6187 -- their formal parameters appear in the same order in both. The analyzed
6188 -- formal has been analyzed in the context of the current instance.
6190 procedure Check_Formal_Package_Instance
6191 (Formal_Pack : Entity_Id;
6192 Actual_Pack : Entity_Id)
6194 E1 : Entity_Id := First_Entity (Actual_Pack);
6195 E2 : Entity_Id := First_Entity (Formal_Pack);
6196 Prev_E1 : Entity_Id;
6201 procedure Check_Mismatch (B : Boolean);
6202 -- Common error routine for mismatch between the parameters of the
6203 -- actual instance and those of the formal package.
6205 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6206 -- The formal may come from a nested formal package, and the actual may
6207 -- have been constant-folded. To determine whether the two denote the
6208 -- same entity we may have to traverse several definitions to recover
6209 -- the ultimate entity that they refer to.
6211 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6212 -- The formal and the actual must be identical, but if both are
6213 -- given by attributes they end up renaming different generated bodies,
6214 -- and we must verify that the attributes themselves match.
6216 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6217 -- Similarly, if the formal comes from a nested formal package, the
6218 -- actual may designate the formal through multiple renamings, which
6219 -- have to be followed to determine the original variable in question.
6221 --------------------
6222 -- Check_Mismatch --
6223 --------------------
6225 procedure Check_Mismatch (B : Boolean) is
6226 -- A Formal_Type_Declaration for a derived private type is rewritten
6227 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6228 -- which is why we examine the original node.
6230 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6233 if Kind = N_Formal_Type_Declaration then
6236 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6237 N_Formal_Package_Declaration)
6238 or else Kind in N_Formal_Subprogram_Declaration
6242 -- Ada 2012: If both formal and actual are incomplete types they
6245 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6250 ("actual for & in actual instance does not match formal",
6251 Parent (Actual_Pack), E1);
6255 --------------------------------
6256 -- Same_Instantiated_Constant --
6257 --------------------------------
6259 function Same_Instantiated_Constant
6260 (E1, E2 : Entity_Id) return Boolean
6266 while Present (Ent) loop
6270 elsif Ekind (Ent) /= E_Constant then
6273 elsif Is_Entity_Name (Constant_Value (Ent)) then
6274 if Entity (Constant_Value (Ent)) = E1 then
6277 Ent := Entity (Constant_Value (Ent));
6280 -- The actual may be a constant that has been folded. Recover
6283 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6284 Ent := Entity (Original_Node (Constant_Value (Ent)));
6292 end Same_Instantiated_Constant;
6294 --------------------------------
6295 -- Same_Instantiated_Function --
6296 --------------------------------
6298 function Same_Instantiated_Function
6299 (E1, E2 : Entity_Id) return Boolean
6303 if Alias (E1) = Alias (E2) then
6306 elsif Present (Alias (E2)) then
6307 U1 := Original_Node (Unit_Declaration_Node (E1));
6308 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6310 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6311 and then Nkind (Name (U1)) = N_Attribute_Reference
6313 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6314 and then Nkind (Name (U2)) = N_Attribute_Reference
6317 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6321 end Same_Instantiated_Function;
6323 --------------------------------
6324 -- Same_Instantiated_Variable --
6325 --------------------------------
6327 function Same_Instantiated_Variable
6328 (E1, E2 : Entity_Id) return Boolean
6330 function Original_Entity (E : Entity_Id) return Entity_Id;
6331 -- Follow chain of renamings to the ultimate ancestor
6333 ---------------------
6334 -- Original_Entity --
6335 ---------------------
6337 function Original_Entity (E : Entity_Id) return Entity_Id is
6342 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6343 and then Present (Renamed_Object (Orig))
6344 and then Is_Entity_Name (Renamed_Object (Orig))
6346 Orig := Entity (Renamed_Object (Orig));
6350 end Original_Entity;
6352 -- Start of processing for Same_Instantiated_Variable
6355 return Ekind (E1) = Ekind (E2)
6356 and then Original_Entity (E1) = Original_Entity (E2);
6357 end Same_Instantiated_Variable;
6359 -- Start of processing for Check_Formal_Package_Instance
6363 while Present (E1) and then Present (E2) loop
6364 exit when Ekind (E1) = E_Package
6365 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6367 -- If the formal is the renaming of the formal package, this
6368 -- is the end of its formal part, which may occur before the
6369 -- end of the formal part in the actual in the presence of
6370 -- defaulted parameters in the formal package.
6372 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6373 and then Renamed_Entity (E2) = Scope (E2);
6375 -- The analysis of the actual may generate additional internal
6376 -- entities. If the formal is defaulted, there is no corresponding
6377 -- analysis and the internal entities must be skipped, until we
6378 -- find corresponding entities again.
6380 if Comes_From_Source (E2)
6381 and then not Comes_From_Source (E1)
6382 and then Chars (E1) /= Chars (E2)
6384 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6392 -- Entities may be declared without full declaration, such as
6393 -- itypes and predefined operators (concatenation for arrays, eg).
6394 -- Skip it and keep the formal entity to find a later match for it.
6396 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6400 -- If the formal entity comes from a formal declaration, it was
6401 -- defaulted in the formal package, and no check is needed on it.
6403 elsif Nkind_In (Original_Node (Parent (E2)),
6404 N_Formal_Object_Declaration,
6405 N_Formal_Type_Declaration)
6407 -- If the formal is a tagged type the corresponding class-wide
6408 -- type has been generated as well, and it must be skipped.
6410 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6416 -- Ditto for defaulted formal subprograms.
6418 elsif Is_Overloadable (E1)
6419 and then Nkind (Unit_Declaration_Node (E2)) in
6420 N_Formal_Subprogram_Declaration
6424 elsif Is_Type (E1) then
6426 -- Subtypes must statically match. E1, E2 are the local entities
6427 -- that are subtypes of the actuals. Itypes generated for other
6428 -- parameters need not be checked, the check will be performed
6429 -- on the parameters themselves.
6431 -- If E2 is a formal type declaration, it is a defaulted parameter
6432 -- and needs no checking.
6434 if not Is_Itype (E1) and then not Is_Itype (E2) then
6437 or else Etype (E1) /= Etype (E2)
6438 or else not Subtypes_Statically_Match (E1, E2));
6441 elsif Ekind (E1) = E_Constant then
6443 -- IN parameters must denote the same static value, or the same
6444 -- constant, or the literal null.
6446 Expr1 := Expression (Parent (E1));
6448 if Ekind (E2) /= E_Constant then
6449 Check_Mismatch (True);
6452 Expr2 := Expression (Parent (E2));
6455 if Is_OK_Static_Expression (Expr1) then
6456 if not Is_OK_Static_Expression (Expr2) then
6457 Check_Mismatch (True);
6459 elsif Is_Discrete_Type (Etype (E1)) then
6461 V1 : constant Uint := Expr_Value (Expr1);
6462 V2 : constant Uint := Expr_Value (Expr2);
6464 Check_Mismatch (V1 /= V2);
6467 elsif Is_Real_Type (Etype (E1)) then
6469 V1 : constant Ureal := Expr_Value_R (Expr1);
6470 V2 : constant Ureal := Expr_Value_R (Expr2);
6472 Check_Mismatch (V1 /= V2);
6475 elsif Is_String_Type (Etype (E1))
6476 and then Nkind (Expr1) = N_String_Literal
6478 if Nkind (Expr2) /= N_String_Literal then
6479 Check_Mismatch (True);
6482 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6486 elsif Is_Entity_Name (Expr1) then
6487 if Is_Entity_Name (Expr2) then
6488 if Entity (Expr1) = Entity (Expr2) then
6492 (not Same_Instantiated_Constant
6493 (Entity (Expr1), Entity (Expr2)));
6497 Check_Mismatch (True);
6500 elsif Is_Entity_Name (Original_Node (Expr1))
6501 and then Is_Entity_Name (Expr2)
6502 and then Same_Instantiated_Constant
6503 (Entity (Original_Node (Expr1)), Entity (Expr2))
6507 elsif Nkind (Expr1) = N_Null then
6508 Check_Mismatch (Nkind (Expr1) /= N_Null);
6511 Check_Mismatch (True);
6514 elsif Ekind (E1) = E_Variable then
6515 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6517 elsif Ekind (E1) = E_Package then
6519 (Ekind (E1) /= Ekind (E2)
6520 or else (Present (Renamed_Object (E2))
6521 and then Renamed_Object (E1) /=
6522 Renamed_Object (E2)));
6524 elsif Is_Overloadable (E1) then
6525 -- Verify that the actual subprograms match. Note that actuals
6526 -- that are attributes are rewritten as subprograms. If the
6527 -- subprogram in the formal package is defaulted, no check is
6528 -- needed. Note that this can only happen in Ada 2005 when the
6529 -- formal package can be partially parameterized.
6531 if Nkind (Unit_Declaration_Node (E1)) =
6532 N_Subprogram_Renaming_Declaration
6533 and then From_Default (Unit_Declaration_Node (E1))
6537 -- If the formal package has an "others" box association that
6538 -- covers this formal, there is no need for a check either.
6540 elsif Nkind (Unit_Declaration_Node (E2)) in
6541 N_Formal_Subprogram_Declaration
6542 and then Box_Present (Unit_Declaration_Node (E2))
6546 -- No check needed if subprogram is a defaulted null procedure
6548 elsif No (Alias (E2))
6549 and then Ekind (E2) = E_Procedure
6551 Null_Present (Specification (Unit_Declaration_Node (E2)))
6555 -- Otherwise the actual in the formal and the actual in the
6556 -- instantiation of the formal must match, up to renamings.
6560 (Ekind (E2) /= Ekind (E1)
6561 or else not Same_Instantiated_Function (E1, E2));
6565 raise Program_Error;
6573 end Check_Formal_Package_Instance;
6575 ---------------------------
6576 -- Check_Formal_Packages --
6577 ---------------------------
6579 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6581 Formal_P : Entity_Id;
6582 Formal_Decl : Node_Id;
6584 -- Iterate through the declarations in the instance, looking for package
6585 -- renaming declarations that denote instances of formal packages. Stop
6586 -- when we find the renaming of the current package itself. The
6587 -- declaration for a formal package without a box is followed by an
6588 -- internal entity that repeats the instantiation.
6590 E := First_Entity (P_Id);
6591 while Present (E) loop
6592 if Ekind (E) = E_Package then
6593 if Renamed_Object (E) = P_Id then
6596 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6600 Formal_Decl := Parent (Associated_Formal_Package (E));
6602 -- Nothing to check if the formal has a box or an others_clause
6603 -- (necessarily with a box).
6605 if Box_Present (Formal_Decl) then
6608 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6611 -- The internal validating package was generated but formal
6612 -- and instance are known to be compatible.
6614 Formal_P := Next_Entity (E);
6615 Remove (Unit_Declaration_Node (Formal_P));
6618 Formal_P := Next_Entity (E);
6620 -- If the instance is within an enclosing instance body
6621 -- there is no need to verify the legality of current formal
6622 -- packages because they were legal in the generic body.
6623 -- This optimization may be applicable elsewhere, and it
6624 -- also removes spurious errors that may arise with
6625 -- on-the-fly inlining and confusion between private and
6628 if not In_Instance_Body then
6629 Check_Formal_Package_Instance (Formal_P, E);
6632 -- Restore the visibility of formals of the formal instance
6633 -- that are not defaulted, and are hidden within the current
6634 -- generic. These formals may be visible within an enclosing
6640 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6641 while Present (Elmt) loop
6642 Set_Is_Hidden (Node (Elmt), False);
6647 -- After checking, remove the internal validating package.
6648 -- It is only needed for semantic checks, and as it may
6649 -- contain generic formal declarations it should not reach
6652 Remove (Unit_Declaration_Node (Formal_P));
6659 end Check_Formal_Packages;
6661 ---------------------------------
6662 -- Check_Forward_Instantiation --
6663 ---------------------------------
6665 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6667 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6670 -- The instantiation appears before the generic body if we are in the
6671 -- scope of the unit containing the generic, either in its spec or in
6672 -- the package body, and before the generic body.
6674 if Ekind (Gen_Comp) = E_Package_Body then
6675 Gen_Comp := Spec_Entity (Gen_Comp);
6678 if In_Open_Scopes (Gen_Comp)
6679 and then No (Corresponding_Body (Decl))
6684 and then not Is_Compilation_Unit (S)
6685 and then not Is_Child_Unit (S)
6687 if Ekind (S) = E_Package then
6688 Set_Has_Forward_Instantiation (S);
6694 end Check_Forward_Instantiation;
6696 ---------------------------
6697 -- Check_Generic_Actuals --
6698 ---------------------------
6700 -- The visibility of the actuals may be different between the point of
6701 -- generic instantiation and the instantiation of the body.
6703 procedure Check_Generic_Actuals
6704 (Instance : Entity_Id;
6705 Is_Formal_Box : Boolean)
6710 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6711 -- For a formal that is an array type, the component type is often a
6712 -- previous formal in the same unit. The privacy status of the component
6713 -- type will have been examined earlier in the traversal of the
6714 -- corresponding actuals, and this status should not be modified for
6715 -- the array (sub)type itself. However, if the base type of the array
6716 -- (sub)type is private, its full view must be restored in the body to
6717 -- be consistent with subsequent index subtypes, etc.
6719 -- To detect this case we have to rescan the list of formals, which is
6720 -- usually short enough to ignore the resulting inefficiency.
6722 -----------------------------
6723 -- Denotes_Previous_Actual --
6724 -----------------------------
6726 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6730 Prev := First_Entity (Instance);
6731 while Present (Prev) loop
6733 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6734 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6735 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6748 end Denotes_Previous_Actual;
6750 -- Start of processing for Check_Generic_Actuals
6753 E := First_Entity (Instance);
6754 while Present (E) loop
6756 and then Nkind (Parent (E)) = N_Subtype_Declaration
6757 and then Scope (Etype (E)) /= Instance
6758 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6760 if Is_Array_Type (E)
6761 and then not Is_Private_Type (Etype (E))
6762 and then Denotes_Previous_Actual (Component_Type (E))
6766 Check_Private_View (Subtype_Indication (Parent (E)));
6769 Set_Is_Generic_Actual_Type (E, True);
6770 Set_Is_Hidden (E, False);
6771 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6773 -- We constructed the generic actual type as a subtype of the
6774 -- supplied type. This means that it normally would not inherit
6775 -- subtype specific attributes of the actual, which is wrong for
6776 -- the generic case.
6778 Astype := Ancestor_Subtype (E);
6782 -- This can happen when E is an itype that is the full view of
6783 -- a private type completed, e.g. with a constrained array. In
6784 -- that case, use the first subtype, which will carry size
6785 -- information. The base type itself is unconstrained and will
6788 Astype := First_Subtype (E);
6791 Set_Size_Info (E, (Astype));
6792 Set_RM_Size (E, RM_Size (Astype));
6793 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6795 if Is_Discrete_Or_Fixed_Point_Type (E) then
6796 Set_RM_Size (E, RM_Size (Astype));
6798 -- In nested instances, the base type of an access actual may
6799 -- itself be private, and need to be exchanged.
6801 elsif Is_Access_Type (E)
6802 and then Is_Private_Type (Etype (E))
6805 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6808 elsif Ekind (E) = E_Package then
6810 -- If this is the renaming for the current instance, we're done.
6811 -- Otherwise it is a formal package. If the corresponding formal
6812 -- was declared with a box, the (instantiations of the) generic
6813 -- formal part are also visible. Otherwise, ignore the entity
6814 -- created to validate the actuals.
6816 if Renamed_Object (E) = Instance then
6819 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6822 -- The visibility of a formal of an enclosing generic is already
6825 elsif Denotes_Formal_Package (E) then
6828 elsif Present (Associated_Formal_Package (E))
6829 and then not Is_Generic_Formal (E)
6831 if Box_Present (Parent (Associated_Formal_Package (E))) then
6832 Check_Generic_Actuals (Renamed_Object (E), True);
6835 Check_Generic_Actuals (Renamed_Object (E), False);
6838 Set_Is_Hidden (E, False);
6841 -- If this is a subprogram instance (in a wrapper package) the
6842 -- actual is fully visible.
6844 elsif Is_Wrapper_Package (Instance) then
6845 Set_Is_Hidden (E, False);
6847 -- If the formal package is declared with a box, or if the formal
6848 -- parameter is defaulted, it is visible in the body.
6850 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6851 Set_Is_Hidden (E, False);
6854 if Ekind (E) = E_Constant then
6856 -- If the type of the actual is a private type declared in the
6857 -- enclosing scope of the generic unit, the body of the generic
6858 -- sees the full view of the type (because it has to appear in
6859 -- the corresponding package body). If the type is private now,
6860 -- exchange views to restore the proper visiblity in the instance.
6863 Typ : constant Entity_Id := Base_Type (Etype (E));
6864 -- The type of the actual
6869 Parent_Scope : Entity_Id;
6870 -- The enclosing scope of the generic unit
6873 if Is_Wrapper_Package (Instance) then
6877 (Unit_Declaration_Node
6878 (Related_Instance (Instance))));
6881 Generic_Parent (Package_Specification (Instance));
6884 Parent_Scope := Scope (Gen_Id);
6886 -- The exchange is only needed if the generic is defined
6887 -- within a package which is not a common ancestor of the
6888 -- scope of the instance, and is not already in scope.
6890 if Is_Private_Type (Typ)
6891 and then Scope (Typ) = Parent_Scope
6892 and then Scope (Instance) /= Parent_Scope
6893 and then Ekind (Parent_Scope) = E_Package
6894 and then not Is_Child_Unit (Gen_Id)
6898 -- If the type of the entity is a subtype, it may also have
6899 -- to be made visible, together with the base type of its
6900 -- full view, after exchange.
6902 if Is_Private_Type (Etype (E)) then
6903 Switch_View (Etype (E));
6904 Switch_View (Base_Type (Etype (E)));
6912 end Check_Generic_Actuals;
6914 ------------------------------
6915 -- Check_Generic_Child_Unit --
6916 ------------------------------
6918 procedure Check_Generic_Child_Unit
6920 Parent_Installed : in out Boolean)
6922 Loc : constant Source_Ptr := Sloc (Gen_Id);
6923 Gen_Par : Entity_Id := Empty;
6925 Inst_Par : Entity_Id;
6928 function Find_Generic_Child
6930 Id : Node_Id) return Entity_Id;
6931 -- Search generic parent for possible child unit with the given name
6933 function In_Enclosing_Instance return Boolean;
6934 -- Within an instance of the parent, the child unit may be denoted by
6935 -- a simple name, or an abbreviated expanded name. Examine enclosing
6936 -- scopes to locate a possible parent instantiation.
6938 ------------------------
6939 -- Find_Generic_Child --
6940 ------------------------
6942 function Find_Generic_Child
6944 Id : Node_Id) return Entity_Id
6949 -- If entity of name is already set, instance has already been
6950 -- resolved, e.g. in an enclosing instantiation.
6952 if Present (Entity (Id)) then
6953 if Scope (Entity (Id)) = Scop then
6960 E := First_Entity (Scop);
6961 while Present (E) loop
6962 if Chars (E) = Chars (Id)
6963 and then Is_Child_Unit (E)
6965 if Is_Child_Unit (E)
6966 and then not Is_Visible_Lib_Unit (E)
6969 ("generic child unit& is not visible", Gen_Id, E);
6981 end Find_Generic_Child;
6983 ---------------------------
6984 -- In_Enclosing_Instance --
6985 ---------------------------
6987 function In_Enclosing_Instance return Boolean is
6988 Enclosing_Instance : Node_Id;
6989 Instance_Decl : Node_Id;
6992 -- We do not inline any call that contains instantiations, except
6993 -- for instantiations of Unchecked_Conversion, so if we are within
6994 -- an inlined body the current instance does not require parents.
6996 if In_Inlined_Body then
6997 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7001 -- Loop to check enclosing scopes
7003 Enclosing_Instance := Current_Scope;
7004 while Present (Enclosing_Instance) loop
7005 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7007 if Ekind (Enclosing_Instance) = E_Package
7008 and then Is_Generic_Instance (Enclosing_Instance)
7010 (Generic_Parent (Specification (Instance_Decl)))
7012 -- Check whether the generic we are looking for is a child of
7015 E := Find_Generic_Child
7016 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7017 exit when Present (E);
7023 Enclosing_Instance := Scope (Enclosing_Instance);
7035 Make_Expanded_Name (Loc,
7037 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7038 Selector_Name => New_Occurrence_Of (E, Loc)));
7040 Set_Entity (Gen_Id, E);
7041 Set_Etype (Gen_Id, Etype (E));
7042 Parent_Installed := False; -- Already in scope.
7045 end In_Enclosing_Instance;
7047 -- Start of processing for Check_Generic_Child_Unit
7050 -- If the name of the generic is given by a selected component, it may
7051 -- be the name of a generic child unit, and the prefix is the name of an
7052 -- instance of the parent, in which case the child unit must be visible.
7053 -- If this instance is not in scope, it must be placed there and removed
7054 -- after instantiation, because what is being instantiated is not the
7055 -- original child, but the corresponding child present in the instance
7058 -- If the child is instantiated within the parent, it can be given by
7059 -- a simple name. In this case the instance is already in scope, but
7060 -- the child generic must be recovered from the generic parent as well.
7062 if Nkind (Gen_Id) = N_Selected_Component then
7063 S := Selector_Name (Gen_Id);
7064 Analyze (Prefix (Gen_Id));
7065 Inst_Par := Entity (Prefix (Gen_Id));
7067 if Ekind (Inst_Par) = E_Package
7068 and then Present (Renamed_Object (Inst_Par))
7070 Inst_Par := Renamed_Object (Inst_Par);
7073 if Ekind (Inst_Par) = E_Package then
7074 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7075 Gen_Par := Generic_Parent (Parent (Inst_Par));
7077 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7079 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7081 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7084 elsif Ekind (Inst_Par) = E_Generic_Package
7085 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7087 -- A formal package may be a real child package, and not the
7088 -- implicit instance within a parent. In this case the child is
7089 -- not visible and has to be retrieved explicitly as well.
7091 Gen_Par := Inst_Par;
7094 if Present (Gen_Par) then
7096 -- The prefix denotes an instantiation. The entity itself may be a
7097 -- nested generic, or a child unit.
7099 E := Find_Generic_Child (Gen_Par, S);
7102 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7103 Set_Entity (Gen_Id, E);
7104 Set_Etype (Gen_Id, Etype (E));
7106 Set_Etype (S, Etype (E));
7108 -- Indicate that this is a reference to the parent
7110 if In_Extended_Main_Source_Unit (Gen_Id) then
7111 Set_Is_Instantiated (Inst_Par);
7114 -- A common mistake is to replicate the naming scheme of a
7115 -- hierarchy by instantiating a generic child directly, rather
7116 -- than the implicit child in a parent instance:
7118 -- generic .. package Gpar is ..
7119 -- generic .. package Gpar.Child is ..
7120 -- package Par is new Gpar ();
7123 -- package Par.Child is new Gpar.Child ();
7124 -- rather than Par.Child
7126 -- In this case the instantiation is within Par, which is an
7127 -- instance, but Gpar does not denote Par because we are not IN
7128 -- the instance of Gpar, so this is illegal. The test below
7129 -- recognizes this particular case.
7131 if Is_Child_Unit (E)
7132 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7133 and then (not In_Instance
7134 or else Nkind (Parent (Parent (Gen_Id))) =
7138 ("prefix of generic child unit must be instance of parent",
7142 if not In_Open_Scopes (Inst_Par)
7143 and then Nkind (Parent (Gen_Id)) not in
7144 N_Generic_Renaming_Declaration
7146 Install_Parent (Inst_Par);
7147 Parent_Installed := True;
7149 elsif In_Open_Scopes (Inst_Par) then
7151 -- If the parent is already installed, install the actuals
7152 -- for its formal packages. This is necessary when the child
7153 -- instance is a child of the parent instance: in this case,
7154 -- the parent is placed on the scope stack but the formal
7155 -- packages are not made visible.
7157 Install_Formal_Packages (Inst_Par);
7161 -- If the generic parent does not contain an entity that
7162 -- corresponds to the selector, the instance doesn't either.
7163 -- Analyzing the node will yield the appropriate error message.
7164 -- If the entity is not a child unit, then it is an inner
7165 -- generic in the parent.
7173 if Is_Child_Unit (Entity (Gen_Id))
7175 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7176 and then not In_Open_Scopes (Inst_Par)
7178 Install_Parent (Inst_Par);
7179 Parent_Installed := True;
7181 -- The generic unit may be the renaming of the implicit child
7182 -- present in an instance. In that case the parent instance is
7183 -- obtained from the name of the renamed entity.
7185 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7186 and then Present (Renamed_Entity (Entity (Gen_Id)))
7187 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7190 Renamed_Package : constant Node_Id :=
7191 Name (Parent (Entity (Gen_Id)));
7193 if Nkind (Renamed_Package) = N_Expanded_Name then
7194 Inst_Par := Entity (Prefix (Renamed_Package));
7195 Install_Parent (Inst_Par);
7196 Parent_Installed := True;
7202 elsif Nkind (Gen_Id) = N_Expanded_Name then
7204 -- Entity already present, analyze prefix, whose meaning may be an
7205 -- instance in the current context. If it is an instance of a
7206 -- relative within another, the proper parent may still have to be
7207 -- installed, if they are not of the same generation.
7209 Analyze (Prefix (Gen_Id));
7211 -- Prevent cascaded errors
7213 if Etype (Prefix (Gen_Id)) = Any_Type then
7217 -- In the unlikely case that a local declaration hides the name of
7218 -- the parent package, locate it on the homonym chain. If the context
7219 -- is an instance of the parent, the renaming entity is flagged as
7222 Inst_Par := Entity (Prefix (Gen_Id));
7223 while Present (Inst_Par)
7224 and then not Is_Package_Or_Generic_Package (Inst_Par)
7226 Inst_Par := Homonym (Inst_Par);
7229 pragma Assert (Present (Inst_Par));
7230 Set_Entity (Prefix (Gen_Id), Inst_Par);
7232 if In_Enclosing_Instance then
7235 elsif Present (Entity (Gen_Id))
7236 and then Is_Child_Unit (Entity (Gen_Id))
7237 and then not In_Open_Scopes (Inst_Par)
7239 Install_Parent (Inst_Par);
7240 Parent_Installed := True;
7243 elsif In_Enclosing_Instance then
7245 -- The child unit is found in some enclosing scope
7252 -- If this is the renaming of the implicit child in a parent
7253 -- instance, recover the parent name and install it.
7255 if Is_Entity_Name (Gen_Id) then
7256 E := Entity (Gen_Id);
7258 if Is_Generic_Unit (E)
7259 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7260 and then Is_Child_Unit (Renamed_Object (E))
7261 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7262 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7264 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7265 Inst_Par := Entity (Prefix (Gen_Id));
7267 if not In_Open_Scopes (Inst_Par) then
7268 Install_Parent (Inst_Par);
7269 Parent_Installed := True;
7272 -- If it is a child unit of a non-generic parent, it may be
7273 -- use-visible and given by a direct name. Install parent as
7276 elsif Is_Generic_Unit (E)
7277 and then Is_Child_Unit (E)
7279 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7280 and then not Is_Generic_Unit (Scope (E))
7282 if not In_Open_Scopes (Scope (E)) then
7283 Install_Parent (Scope (E));
7284 Parent_Installed := True;
7289 end Check_Generic_Child_Unit;
7291 -----------------------------
7292 -- Check_Hidden_Child_Unit --
7293 -----------------------------
7295 procedure Check_Hidden_Child_Unit
7297 Gen_Unit : Entity_Id;
7298 Act_Decl_Id : Entity_Id)
7300 Gen_Id : constant Node_Id := Name (N);
7303 if Is_Child_Unit (Gen_Unit)
7304 and then Is_Child_Unit (Act_Decl_Id)
7305 and then Nkind (Gen_Id) = N_Expanded_Name
7306 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7307 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7309 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7311 ("generic unit & is implicitly declared in &",
7312 Defining_Unit_Name (N), Gen_Unit);
7313 Error_Msg_N ("\instance must have different name",
7314 Defining_Unit_Name (N));
7316 end Check_Hidden_Child_Unit;
7318 ------------------------
7319 -- Check_Private_View --
7320 ------------------------
7322 procedure Check_Private_View (N : Node_Id) is
7323 T : constant Entity_Id := Etype (N);
7327 -- Exchange views if the type was not private in the generic but is
7328 -- private at the point of instantiation. Do not exchange views if
7329 -- the scope of the type is in scope. This can happen if both generic
7330 -- and instance are sibling units, or if type is defined in a parent.
7331 -- In this case the visibility of the type will be correct for all
7335 BT := Base_Type (T);
7337 if Is_Private_Type (T)
7338 and then not Has_Private_View (N)
7339 and then Present (Full_View (T))
7340 and then not In_Open_Scopes (Scope (T))
7342 -- In the generic, the full type was visible. Save the private
7343 -- entity, for subsequent exchange.
7347 elsif Has_Private_View (N)
7348 and then not Is_Private_Type (T)
7349 and then not Has_Been_Exchanged (T)
7350 and then Etype (Get_Associated_Node (N)) /= T
7352 -- Only the private declaration was visible in the generic. If
7353 -- the type appears in a subtype declaration, the subtype in the
7354 -- instance must have a view compatible with that of its parent,
7355 -- which must be exchanged (see corresponding code in Restore_
7356 -- Private_Views). Otherwise, if the type is defined in a parent
7357 -- unit, leave full visibility within instance, which is safe.
7359 if In_Open_Scopes (Scope (Base_Type (T)))
7360 and then not Is_Private_Type (Base_Type (T))
7361 and then Comes_From_Source (Base_Type (T))
7365 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7366 or else not In_Private_Part (Scope (Base_Type (T)))
7368 Prepend_Elmt (T, Exchanged_Views);
7369 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7372 -- For composite types with inconsistent representation exchange
7373 -- component types accordingly.
7375 elsif Is_Access_Type (T)
7376 and then Is_Private_Type (Designated_Type (T))
7377 and then not Has_Private_View (N)
7378 and then Present (Full_View (Designated_Type (T)))
7380 Switch_View (Designated_Type (T));
7382 elsif Is_Array_Type (T) then
7383 if Is_Private_Type (Component_Type (T))
7384 and then not Has_Private_View (N)
7385 and then Present (Full_View (Component_Type (T)))
7387 Switch_View (Component_Type (T));
7390 -- The normal exchange mechanism relies on the setting of a
7391 -- flag on the reference in the generic. However, an additional
7392 -- mechanism is needed for types that are not explicitly
7393 -- mentioned in the generic, but may be needed in expanded code
7394 -- in the instance. This includes component types of arrays and
7395 -- designated types of access types. This processing must also
7396 -- include the index types of arrays which we take care of here.
7403 Indx := First_Index (T);
7404 while Present (Indx) loop
7405 Typ := Base_Type (Etype (Indx));
7407 if Is_Private_Type (Typ)
7408 and then Present (Full_View (Typ))
7417 elsif Is_Private_Type (T)
7418 and then Present (Full_View (T))
7419 and then Is_Array_Type (Full_View (T))
7420 and then Is_Private_Type (Component_Type (Full_View (T)))
7424 -- Finally, a non-private subtype may have a private base type, which
7425 -- must be exchanged for consistency. This can happen when a package
7426 -- body is instantiated, when the scope stack is empty but in fact
7427 -- the subtype and the base type are declared in an enclosing scope.
7429 -- Note that in this case we introduce an inconsistency in the view
7430 -- set, because we switch the base type BT, but there could be some
7431 -- private dependent subtypes of BT which remain unswitched. Such
7432 -- subtypes might need to be switched at a later point (see specific
7433 -- provision for that case in Switch_View).
7435 elsif not Is_Private_Type (T)
7436 and then not Has_Private_View (N)
7437 and then Is_Private_Type (BT)
7438 and then Present (Full_View (BT))
7439 and then not Is_Generic_Type (BT)
7440 and then not In_Open_Scopes (BT)
7442 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7443 Exchange_Declarations (BT);
7446 end Check_Private_View;
7448 -----------------------------
7449 -- Check_Hidden_Primitives --
7450 -----------------------------
7452 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7455 Result : Elist_Id := No_Elist;
7458 if No (Assoc_List) then
7462 -- Traverse the list of associations between formals and actuals
7463 -- searching for renamings of tagged types
7465 Actual := First (Assoc_List);
7466 while Present (Actual) loop
7467 if Nkind (Actual) = N_Subtype_Declaration then
7468 Gen_T := Generic_Parent_Type (Actual);
7470 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7472 -- Traverse the list of primitives of the actual types
7473 -- searching for hidden primitives that are visible in the
7474 -- corresponding generic formal; leave them visible and
7475 -- append them to Result to restore their decoration later.
7477 Install_Hidden_Primitives
7478 (Prims_List => Result,
7480 Act_T => Entity (Subtype_Indication (Actual)));
7488 end Check_Hidden_Primitives;
7490 --------------------------
7491 -- Contains_Instance_Of --
7492 --------------------------
7494 function Contains_Instance_Of
7497 N : Node_Id) return Boolean
7505 -- Verify that there are no circular instantiations. We check whether
7506 -- the unit contains an instance of the current scope or some enclosing
7507 -- scope (in case one of the instances appears in a subunit). Longer
7508 -- circularities involving subunits might seem too pathological to
7509 -- consider, but they were not too pathological for the authors of
7510 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7511 -- enclosing generic scopes as containing an instance.
7514 -- Within a generic subprogram body, the scope is not generic, to
7515 -- allow for recursive subprograms. Use the declaration to determine
7516 -- whether this is a generic unit.
7518 if Ekind (Scop) = E_Generic_Package
7519 or else (Is_Subprogram (Scop)
7520 and then Nkind (Unit_Declaration_Node (Scop)) =
7521 N_Generic_Subprogram_Declaration)
7523 Elmt := First_Elmt (Inner_Instances (Inner));
7525 while Present (Elmt) loop
7526 if Node (Elmt) = Scop then
7527 Error_Msg_Node_2 := Inner;
7529 ("circular Instantiation: & instantiated within &!",
7533 elsif Node (Elmt) = Inner then
7536 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7537 Error_Msg_Node_2 := Inner;
7539 ("circular Instantiation: & instantiated within &!",
7547 -- Indicate that Inner is being instantiated within Scop
7549 Append_Elmt (Inner, Inner_Instances (Scop));
7552 if Scop = Standard_Standard then
7555 Scop := Scope (Scop);
7560 end Contains_Instance_Of;
7562 -----------------------
7563 -- Copy_Generic_Node --
7564 -----------------------
7566 function Copy_Generic_Node
7568 Parent_Id : Node_Id;
7569 Instantiating : Boolean) return Node_Id
7574 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7575 -- Check the given value of one of the Fields referenced by the current
7576 -- node to determine whether to copy it recursively. The field may hold
7577 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7578 -- Char) in which case it need not be copied.
7580 procedure Copy_Descendants;
7581 -- Common utility for various nodes
7583 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7584 -- Make copy of element list
7586 function Copy_Generic_List
7588 Parent_Id : Node_Id) return List_Id;
7589 -- Apply Copy_Node recursively to the members of a node list
7591 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7592 -- True if an identifier is part of the defining program unit name of
7593 -- a child unit. The entity of such an identifier must be kept (for
7594 -- ASIS use) even though as the name of an enclosing generic it would
7595 -- otherwise not be preserved in the generic tree.
7597 ----------------------
7598 -- Copy_Descendants --
7599 ----------------------
7601 procedure Copy_Descendants is
7602 use Atree.Unchecked_Access;
7603 -- This code section is part of the implementation of an untyped
7604 -- tree traversal, so it needs direct access to node fields.
7607 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7608 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7609 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7610 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7611 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7612 end Copy_Descendants;
7614 -----------------------------
7615 -- Copy_Generic_Descendant --
7616 -----------------------------
7618 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7620 if D = Union_Id (Empty) then
7623 elsif D in Node_Range then
7625 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7627 elsif D in List_Range then
7628 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7630 elsif D in Elist_Range then
7631 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7633 -- Nothing else is copyable (e.g. Uint values), return as is
7638 end Copy_Generic_Descendant;
7640 ------------------------
7641 -- Copy_Generic_Elist --
7642 ------------------------
7644 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7651 M := First_Elmt (E);
7652 while Present (M) loop
7654 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7663 end Copy_Generic_Elist;
7665 -----------------------
7666 -- Copy_Generic_List --
7667 -----------------------
7669 function Copy_Generic_List
7671 Parent_Id : Node_Id) return List_Id
7679 Set_Parent (New_L, Parent_Id);
7682 while Present (N) loop
7683 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7692 end Copy_Generic_List;
7694 ---------------------------
7695 -- In_Defining_Unit_Name --
7696 ---------------------------
7698 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7701 Present (Parent (Nam))
7702 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7704 (Nkind (Parent (Nam)) = N_Expanded_Name
7705 and then In_Defining_Unit_Name (Parent (Nam))));
7706 end In_Defining_Unit_Name;
7708 -- Start of processing for Copy_Generic_Node
7715 New_N := New_Copy (N);
7717 -- Copy aspects if present
7719 if Has_Aspects (N) then
7720 Set_Has_Aspects (New_N, False);
7721 Set_Aspect_Specifications
7722 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7725 -- If we are instantiating, we want to adjust the sloc based on the
7726 -- current S_Adjustment. However, if this is the root node of a subunit,
7727 -- we need to defer that adjustment to below (see "elsif Instantiating
7728 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7729 -- computed the adjustment.
7732 and then not (Nkind (N) in N_Proper_Body
7733 and then Was_Originally_Stub (N))
7735 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7738 if not Is_List_Member (N) then
7739 Set_Parent (New_N, Parent_Id);
7742 -- Special casing for identifiers and other entity names and operators
7744 if Nkind_In (New_N, N_Character_Literal,
7748 or else Nkind (New_N) in N_Op
7750 if not Instantiating then
7752 -- Link both nodes in order to assign subsequently the entity of
7753 -- the copy to the original node, in case this is a global
7756 Set_Associated_Node (N, New_N);
7758 -- If we are within an instantiation, this is a nested generic
7759 -- that has already been analyzed at the point of definition.
7760 -- We must preserve references that were global to the enclosing
7761 -- parent at that point. Other occurrences, whether global or
7762 -- local to the current generic, must be resolved anew, so we
7763 -- reset the entity in the generic copy. A global reference has a
7764 -- smaller depth than the parent, or else the same depth in case
7765 -- both are distinct compilation units.
7767 -- A child unit is implicitly declared within the enclosing parent
7768 -- but is in fact global to it, and must be preserved.
7770 -- It is also possible for Current_Instantiated_Parent to be
7771 -- defined, and for this not to be a nested generic, namely if
7772 -- the unit is loaded through Rtsfind. In that case, the entity of
7773 -- New_N is only a link to the associated node, and not a defining
7776 -- The entities for parent units in the defining_program_unit of a
7777 -- generic child unit are established when the context of the unit
7778 -- is first analyzed, before the generic copy is made. They are
7779 -- preserved in the copy for use in ASIS queries.
7781 Ent := Entity (New_N);
7783 if No (Current_Instantiated_Parent.Gen_Id) then
7785 or else Nkind (Ent) /= N_Defining_Identifier
7786 or else not In_Defining_Unit_Name (N)
7788 Set_Associated_Node (New_N, Empty);
7793 not Nkind_In (Ent, N_Defining_Identifier,
7794 N_Defining_Character_Literal,
7795 N_Defining_Operator_Symbol)
7796 or else No (Scope (Ent))
7798 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7799 and then not Is_Child_Unit (Ent))
7801 (Scope_Depth (Scope (Ent)) >
7802 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7804 Get_Source_Unit (Ent) =
7805 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7807 Set_Associated_Node (New_N, Empty);
7810 -- Case of instantiating identifier or some other name or operator
7813 -- If the associated node is still defined, the entity in it
7814 -- is global, and must be copied to the instance. If this copy
7815 -- is being made for a body to inline, it is applied to an
7816 -- instantiated tree, and the entity is already present and
7817 -- must be also preserved.
7820 Assoc : constant Node_Id := Get_Associated_Node (N);
7823 if Present (Assoc) then
7824 if Nkind (Assoc) = Nkind (N) then
7825 Set_Entity (New_N, Entity (Assoc));
7826 Check_Private_View (N);
7828 -- The node is a reference to a global type and acts as the
7829 -- subtype mark of a qualified expression created in order
7830 -- to aid resolution of accidental overloading in instances.
7831 -- Since N is a reference to a type, the Associated_Node of
7832 -- N denotes an entity rather than another identifier. See
7833 -- Qualify_Universal_Operands for details.
7835 elsif Nkind (N) = N_Identifier
7836 and then Nkind (Parent (N)) = N_Qualified_Expression
7837 and then Subtype_Mark (Parent (N)) = N
7838 and then Is_Qualified_Universal_Literal (Parent (N))
7840 Set_Entity (New_N, Assoc);
7842 -- The name in the call may be a selected component if the
7843 -- call has not been analyzed yet, as may be the case for
7844 -- pre/post conditions in a generic unit.
7846 elsif Nkind (Assoc) = N_Function_Call
7847 and then Is_Entity_Name (Name (Assoc))
7849 Set_Entity (New_N, Entity (Name (Assoc)));
7851 elsif Nkind_In (Assoc, N_Defining_Identifier,
7852 N_Defining_Character_Literal,
7853 N_Defining_Operator_Symbol)
7854 and then Expander_Active
7856 -- Inlining case: we are copying a tree that contains
7857 -- global entities, which are preserved in the copy to be
7858 -- used for subsequent inlining.
7863 Set_Entity (New_N, Empty);
7869 -- For expanded name, we must copy the Prefix and Selector_Name
7871 if Nkind (N) = N_Expanded_Name then
7873 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7875 Set_Selector_Name (New_N,
7876 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7878 -- For operators, copy the operands
7880 elsif Nkind (N) in N_Op then
7881 if Nkind (N) in N_Binary_Op then
7882 Set_Left_Opnd (New_N,
7883 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7886 Set_Right_Opnd (New_N,
7887 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7890 -- Establish a link between an entity from the generic template and the
7891 -- corresponding entity in the generic copy to be analyzed.
7893 elsif Nkind (N) in N_Entity then
7894 if not Instantiating then
7895 Set_Associated_Entity (N, New_N);
7898 -- Clear any existing link the copy may inherit from the replicated
7899 -- generic template entity.
7901 Set_Associated_Entity (New_N, Empty);
7903 -- Special casing for stubs
7905 elsif Nkind (N) in N_Body_Stub then
7907 -- In any case, we must copy the specification or defining
7908 -- identifier as appropriate.
7910 if Nkind (N) = N_Subprogram_Body_Stub then
7911 Set_Specification (New_N,
7912 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7915 Set_Defining_Identifier (New_N,
7917 (Defining_Identifier (N), New_N, Instantiating));
7920 -- If we are not instantiating, then this is where we load and
7921 -- analyze subunits, i.e. at the point where the stub occurs. A
7922 -- more permissive system might defer this analysis to the point
7923 -- of instantiation, but this seems too complicated for now.
7925 if not Instantiating then
7927 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7929 Unum : Unit_Number_Type;
7933 -- Make sure that, if it is a subunit of the main unit that is
7934 -- preprocessed and if -gnateG is specified, the preprocessed
7935 -- file will be written.
7937 Lib.Analysing_Subunit_Of_Main :=
7938 Lib.In_Extended_Main_Source_Unit (N);
7941 (Load_Name => Subunit_Name,
7945 Lib.Analysing_Subunit_Of_Main := False;
7947 -- If the proper body is not found, a warning message will be
7948 -- emitted when analyzing the stub, or later at the point of
7949 -- instantiation. Here we just leave the stub as is.
7951 if Unum = No_Unit then
7952 Subunits_Missing := True;
7953 goto Subunit_Not_Found;
7956 Subunit := Cunit (Unum);
7958 if Nkind (Unit (Subunit)) /= N_Subunit then
7960 ("found child unit instead of expected SEPARATE subunit",
7962 Error_Msg_Sloc := Sloc (N);
7963 Error_Msg_N ("\to complete stub #", Subunit);
7964 goto Subunit_Not_Found;
7967 -- We must create a generic copy of the subunit, in order to
7968 -- perform semantic analysis on it, and we must replace the
7969 -- stub in the original generic unit with the subunit, in order
7970 -- to preserve non-local references within.
7972 -- Only the proper body needs to be copied. Library_Unit and
7973 -- context clause are simply inherited by the generic copy.
7974 -- Note that the copy (which may be recursive if there are
7975 -- nested subunits) must be done first, before attaching it to
7976 -- the enclosing generic.
7980 (Proper_Body (Unit (Subunit)),
7981 Empty, Instantiating => False);
7983 -- Now place the original proper body in the original generic
7984 -- unit. This is a body, not a compilation unit.
7986 Rewrite (N, Proper_Body (Unit (Subunit)));
7987 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7988 Set_Was_Originally_Stub (N);
7990 -- Finally replace the body of the subunit with its copy, and
7991 -- make this new subunit into the library unit of the generic
7992 -- copy, which does not have stubs any longer.
7994 Set_Proper_Body (Unit (Subunit), New_Body);
7995 Set_Library_Unit (New_N, Subunit);
7996 Inherit_Context (Unit (Subunit), N);
7999 -- If we are instantiating, this must be an error case, since
8000 -- otherwise we would have replaced the stub node by the proper body
8001 -- that corresponds. So just ignore it in the copy (i.e. we have
8002 -- copied it, and that is good enough).
8008 <<Subunit_Not_Found>> null;
8010 -- If the node is a compilation unit, it is the subunit of a stub, which
8011 -- has been loaded already (see code below). In this case, the library
8012 -- unit field of N points to the parent unit (which is a compilation
8013 -- unit) and need not (and cannot) be copied.
8015 -- When the proper body of the stub is analyzed, the library_unit link
8016 -- is used to establish the proper context (see sem_ch10).
8018 -- The other fields of a compilation unit are copied as usual
8020 elsif Nkind (N) = N_Compilation_Unit then
8022 -- This code can only be executed when not instantiating, because in
8023 -- the copy made for an instantiation, the compilation unit node has
8024 -- disappeared at the point that a stub is replaced by its proper
8027 pragma Assert (not Instantiating);
8029 Set_Context_Items (New_N,
8030 Copy_Generic_List (Context_Items (N), New_N));
8033 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8035 Set_First_Inlined_Subprogram (New_N,
8037 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8042 (Aux_Decls_Node (N), New_N, Instantiating => False));
8044 -- For an assignment node, the assignment is known to be semantically
8045 -- legal if we are instantiating the template. This avoids incorrect
8046 -- diagnostics in generated code.
8048 elsif Nkind (N) = N_Assignment_Statement then
8050 -- Copy name and expression fields in usual manner
8053 Copy_Generic_Node (Name (N), New_N, Instantiating));
8055 Set_Expression (New_N,
8056 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8058 if Instantiating then
8059 Set_Assignment_OK (Name (New_N), True);
8062 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8063 if not Instantiating then
8064 Set_Associated_Node (N, New_N);
8067 if Present (Get_Associated_Node (N))
8068 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8070 -- In the generic the aggregate has some composite type. If at
8071 -- the point of instantiation the type has a private view,
8072 -- install the full view (and that of its ancestors, if any).
8075 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8079 if Present (T) and then Is_Private_Type (T) then
8084 and then Is_Tagged_Type (T)
8085 and then Is_Derived_Type (T)
8087 Rt := Root_Type (T);
8092 if Is_Private_Type (T) then
8103 -- Do not copy the associated node, which points to the generic copy
8104 -- of the aggregate.
8107 use Atree.Unchecked_Access;
8108 -- This code section is part of the implementation of an untyped
8109 -- tree traversal, so it needs direct access to node fields.
8112 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8113 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8114 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8115 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8118 -- Allocators do not have an identifier denoting the access type, so we
8119 -- must locate it through the expression to check whether the views are
8122 elsif Nkind (N) = N_Allocator
8123 and then Nkind (Expression (N)) = N_Qualified_Expression
8124 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8125 and then Instantiating
8128 T : constant Node_Id :=
8129 Get_Associated_Node (Subtype_Mark (Expression (N)));
8135 -- Retrieve the allocator node in the generic copy
8137 Acc_T := Etype (Parent (Parent (T)));
8139 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8140 Switch_View (Acc_T);
8147 -- For a proper body, we must catch the case of a proper body that
8148 -- replaces a stub. This represents the point at which a separate
8149 -- compilation unit, and hence template file, may be referenced, so we
8150 -- must make a new source instantiation entry for the template of the
8151 -- subunit, and ensure that all nodes in the subunit are adjusted using
8152 -- this new source instantiation entry.
8154 elsif Nkind (N) in N_Proper_Body then
8156 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8158 if Instantiating and then Was_Originally_Stub (N) then
8159 Create_Instantiation_Source
8160 (Instantiation_Node,
8161 Defining_Entity (N),
8164 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8167 -- Now copy the fields of the proper body, using the new
8168 -- adjustment factor if one was needed as per test above.
8172 -- Restore the original adjustment factor
8174 S_Adjustment := Save_Adjustment;
8177 elsif Nkind (N) = N_Pragma and then Instantiating then
8179 -- Do not copy Comment or Ident pragmas their content is relevant to
8180 -- the generic unit, not to the instantiating unit.
8182 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8183 New_N := Make_Null_Statement (Sloc (N));
8185 -- Do not copy pragmas generated from aspects because the pragmas do
8186 -- not carry any semantic information, plus they will be regenerated
8189 -- However, generating C we need to copy them since postconditions
8190 -- are inlined by the front end, and the front-end inlining machinery
8191 -- relies on this routine to perform inlining.
8193 elsif From_Aspect_Specification (N)
8194 and then not Modify_Tree_For_C
8196 New_N := Make_Null_Statement (Sloc (N));
8202 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8204 -- No descendant fields need traversing
8208 elsif Nkind (N) = N_String_Literal
8209 and then Present (Etype (N))
8210 and then Instantiating
8212 -- If the string is declared in an outer scope, the string_literal
8213 -- subtype created for it may have the wrong scope. Force reanalysis
8214 -- of the constant to generate a new itype in the proper context.
8216 Set_Etype (New_N, Empty);
8217 Set_Analyzed (New_N, False);
8219 -- For the remaining nodes, copy their descendants recursively
8224 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8225 Set_Generic_Parent (Specification (New_N), N);
8227 -- Should preserve Corresponding_Spec??? (12.3(14))
8231 -- Propagate dimensions if present, so that they are reflected in the
8234 if Nkind (N) in N_Has_Etype
8235 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8236 and then Present (Etype (N))
8237 and then Is_Floating_Point_Type (Etype (N))
8238 and then Has_Dimension_System (Etype (N))
8240 Copy_Dimensions (N, New_N);
8244 end Copy_Generic_Node;
8246 ----------------------------
8247 -- Denotes_Formal_Package --
8248 ----------------------------
8250 function Denotes_Formal_Package
8252 On_Exit : Boolean := False;
8253 Instance : Entity_Id := Empty) return Boolean
8256 Scop : constant Entity_Id := Scope (Pack);
8259 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8260 -- The package in question may be an actual for a previous formal
8261 -- package P of the current instance, so examine its actuals as well.
8262 -- This must be recursive over other formal packages.
8264 ----------------------------------
8265 -- Is_Actual_Of_Previous_Formal --
8266 ----------------------------------
8268 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8272 E1 := First_Entity (P);
8273 while Present (E1) and then E1 /= Instance loop
8274 if Ekind (E1) = E_Package
8275 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8277 if Renamed_Object (E1) = Pack then
8280 elsif E1 = P or else Renamed_Object (E1) = P then
8283 elsif Is_Actual_Of_Previous_Formal (E1) then
8292 end Is_Actual_Of_Previous_Formal;
8294 -- Start of processing for Denotes_Formal_Package
8300 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8302 Par := Current_Instantiated_Parent.Act_Id;
8305 if Ekind (Scop) = E_Generic_Package
8306 or else Nkind (Unit_Declaration_Node (Scop)) =
8307 N_Generic_Subprogram_Declaration
8311 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8312 N_Formal_Package_Declaration
8320 -- Check whether this package is associated with a formal package of
8321 -- the enclosing instantiation. Iterate over the list of renamings.
8323 E := First_Entity (Par);
8324 while Present (E) loop
8325 if Ekind (E) /= E_Package
8326 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8330 elsif Renamed_Object (E) = Par then
8333 elsif Renamed_Object (E) = Pack then
8336 elsif Is_Actual_Of_Previous_Formal (E) then
8346 end Denotes_Formal_Package;
8352 procedure End_Generic is
8354 -- ??? More things could be factored out in this routine. Should
8355 -- probably be done at a later stage.
8357 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8358 Generic_Flags.Decrement_Last;
8360 Expander_Mode_Restore;
8367 function Earlier (N1, N2 : Node_Id) return Boolean is
8368 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8369 -- Find distance from given node to enclosing compilation unit
8375 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8378 and then Nkind (P) /= N_Compilation_Unit
8380 P := True_Parent (P);
8385 -- Local declarations
8394 -- Start of processing for Earlier
8397 Find_Depth (P1, D1);
8398 Find_Depth (P2, D2);
8408 P1 := True_Parent (P1);
8413 P2 := True_Parent (P2);
8417 -- At this point P1 and P2 are at the same distance from the root.
8418 -- We examine their parents until we find a common declarative list.
8419 -- If we reach the root, N1 and N2 do not descend from the same
8420 -- declarative list (e.g. one is nested in the declarative part and
8421 -- the other is in a block in the statement part) and the earlier
8422 -- one is already frozen.
8424 while not Is_List_Member (P1)
8425 or else not Is_List_Member (P2)
8426 or else List_Containing (P1) /= List_Containing (P2)
8428 P1 := True_Parent (P1);
8429 P2 := True_Parent (P2);
8431 if Nkind (Parent (P1)) = N_Subunit then
8432 P1 := Corresponding_Stub (Parent (P1));
8435 if Nkind (Parent (P2)) = N_Subunit then
8436 P2 := Corresponding_Stub (Parent (P2));
8444 -- Expanded code usually shares the source location of the original
8445 -- construct it was generated for. This however may not necessarily
8446 -- reflect the true location of the code within the tree.
8448 -- Before comparing the slocs of the two nodes, make sure that we are
8449 -- working with correct source locations. Assume that P1 is to the left
8450 -- of P2. If either one does not come from source, traverse the common
8451 -- list heading towards the other node and locate the first source
8455 -- ----+===+===+--------------+===+===+----
8456 -- expanded code expanded code
8458 if not Comes_From_Source (P1) then
8459 while Present (P1) loop
8461 -- Neither P2 nor a source statement were located during the
8462 -- search. If we reach the end of the list, then P1 does not
8463 -- occur earlier than P2.
8466 -- start --- P2 ----- P1 --- end
8468 if No (Next (P1)) then
8471 -- We encounter P2 while going to the right of the list. This
8472 -- means that P1 does indeed appear earlier.
8475 -- start --- P1 ===== P2 --- end
8476 -- expanded code in between
8481 -- No need to look any further since we have located a source
8484 elsif Comes_From_Source (P1) then
8494 if not Comes_From_Source (P2) then
8495 while Present (P2) loop
8497 -- Neither P1 nor a source statement were located during the
8498 -- search. If we reach the start of the list, then P1 does not
8499 -- occur earlier than P2.
8502 -- start --- P2 --- P1 --- end
8504 if No (Prev (P2)) then
8507 -- We encounter P1 while going to the left of the list. This
8508 -- means that P1 does indeed appear earlier.
8511 -- start --- P1 ===== P2 --- end
8512 -- expanded code in between
8517 -- No need to look any further since we have located a source
8520 elsif Comes_From_Source (P2) then
8530 -- At this point either both nodes came from source or we approximated
8531 -- their source locations through neighboring source statements.
8533 T1 := Top_Level_Location (Sloc (P1));
8534 T2 := Top_Level_Location (Sloc (P2));
8536 -- When two nodes come from the same instance, they have identical top
8537 -- level locations. To determine proper relation within the tree, check
8538 -- their locations within the template.
8541 return Sloc (P1) < Sloc (P2);
8543 -- The two nodes either come from unrelated instances or do not come
8544 -- from instantiated code at all.
8551 ----------------------
8552 -- Find_Actual_Type --
8553 ----------------------
8555 function Find_Actual_Type
8557 Gen_Type : Entity_Id) return Entity_Id
8559 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8563 -- Special processing only applies to child units
8565 if not Is_Child_Unit (Gen_Scope) then
8566 return Get_Instance_Of (Typ);
8568 -- If designated or component type is itself a formal of the child unit,
8569 -- its instance is available.
8571 elsif Scope (Typ) = Gen_Scope then
8572 return Get_Instance_Of (Typ);
8574 -- If the array or access type is not declared in the parent unit,
8575 -- no special processing needed.
8577 elsif not Is_Generic_Type (Typ)
8578 and then Scope (Gen_Scope) /= Scope (Typ)
8580 return Get_Instance_Of (Typ);
8582 -- Otherwise, retrieve designated or component type by visibility
8585 T := Current_Entity (Typ);
8586 while Present (T) loop
8587 if In_Open_Scopes (Scope (T)) then
8589 elsif Is_Generic_Actual_Type (T) then
8598 end Find_Actual_Type;
8600 ----------------------------
8601 -- Freeze_Subprogram_Body --
8602 ----------------------------
8604 procedure Freeze_Subprogram_Body
8605 (Inst_Node : Node_Id;
8607 Pack_Id : Entity_Id)
8609 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8610 Par : constant Entity_Id := Scope (Gen_Unit);
8616 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8617 -- Find innermost package body that encloses the given node, and which
8618 -- is not a compilation unit. Freeze nodes for the instance, or for its
8619 -- enclosing body, may be inserted after the enclosing_body of the
8620 -- generic unit. Used to determine proper placement of freeze node for
8621 -- both package and subprogram instances.
8623 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8624 -- Find entity for given package body, and locate or create a freeze
8627 ----------------------------
8628 -- Enclosing_Package_Body --
8629 ----------------------------
8631 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8637 and then Nkind (Parent (P)) /= N_Compilation_Unit
8639 if Nkind (P) = N_Package_Body then
8640 if Nkind (Parent (P)) = N_Subunit then
8641 return Corresponding_Stub (Parent (P));
8647 P := True_Parent (P);
8651 end Enclosing_Package_Body;
8653 -------------------------
8654 -- Package_Freeze_Node --
8655 -------------------------
8657 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8661 if Nkind (B) = N_Package_Body then
8662 Id := Corresponding_Spec (B);
8663 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8664 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8667 Ensure_Freeze_Node (Id);
8668 return Freeze_Node (Id);
8669 end Package_Freeze_Node;
8671 -- Start of processing for Freeze_Subprogram_Body
8674 -- If the instance and the generic body appear within the same unit, and
8675 -- the instance precedes the generic, the freeze node for the instance
8676 -- must appear after that of the generic. If the generic is nested
8677 -- within another instance I2, then current instance must be frozen
8678 -- after I2. In both cases, the freeze nodes are those of enclosing
8679 -- packages. Otherwise, the freeze node is placed at the end of the
8680 -- current declarative part.
8682 Enc_G := Enclosing_Package_Body (Gen_Body);
8683 Enc_I := Enclosing_Package_Body (Inst_Node);
8684 Ensure_Freeze_Node (Pack_Id);
8685 F_Node := Freeze_Node (Pack_Id);
8687 if Is_Generic_Instance (Par)
8688 and then Present (Freeze_Node (Par))
8689 and then In_Same_Declarative_Part
8690 (Parent (Freeze_Node (Par)), Inst_Node)
8692 -- The parent was a premature instantiation. Insert freeze node at
8693 -- the end the current declarative part.
8695 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8696 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8698 -- Handle the following case:
8700 -- package Parent_Inst is new ...
8703 -- procedure P ... -- this body freezes Parent_Inst
8705 -- package Inst is new ...
8707 -- In this particular scenario, the freeze node for Inst must be
8708 -- inserted in the same manner as that of Parent_Inst - before the
8709 -- next source body or at the end of the declarative list (body not
8710 -- available). If body P did not exist and Parent_Inst was frozen
8711 -- after Inst, either by a body following Inst or at the end of the
8712 -- declarative region, the freeze node for Inst must be inserted
8713 -- after that of Parent_Inst. This relation is established by
8714 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8716 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8717 List_Containing (Inst_Node)
8718 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8720 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8723 Insert_After (Freeze_Node (Par), F_Node);
8726 -- The body enclosing the instance should be frozen after the body that
8727 -- includes the generic, because the body of the instance may make
8728 -- references to entities therein. If the two are not in the same
8729 -- declarative part, or if the one enclosing the instance is frozen
8730 -- already, freeze the instance at the end of the current declarative
8733 elsif Is_Generic_Instance (Par)
8734 and then Present (Freeze_Node (Par))
8735 and then Present (Enc_I)
8737 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8739 (Nkind (Enc_I) = N_Package_Body
8740 and then In_Same_Declarative_Part
8741 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8743 -- The enclosing package may contain several instances. Rather
8744 -- than computing the earliest point at which to insert its freeze
8745 -- node, we place it at the end of the declarative part of the
8746 -- parent of the generic.
8748 Insert_Freeze_Node_For_Instance
8749 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8752 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8754 elsif Present (Enc_G)
8755 and then Present (Enc_I)
8756 and then Enc_G /= Enc_I
8757 and then Earlier (Inst_Node, Gen_Body)
8759 if Nkind (Enc_G) = N_Package_Body then
8761 Corresponding_Spec (Enc_G);
8762 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8764 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8767 -- Freeze package that encloses instance, and place node after the
8768 -- package that encloses generic. If enclosing package is already
8769 -- frozen we have to assume it is at the proper place. This may be a
8770 -- potential ABE that requires dynamic checking. Do not add a freeze
8771 -- node if the package that encloses the generic is inside the body
8772 -- that encloses the instance, because the freeze node would be in
8773 -- the wrong scope. Additional contortions needed if the bodies are
8774 -- within a subunit.
8777 Enclosing_Body : Node_Id;
8780 if Nkind (Enc_I) = N_Package_Body_Stub then
8781 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8783 Enclosing_Body := Enc_I;
8786 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8787 Insert_Freeze_Node_For_Instance
8788 (Enc_G, Package_Freeze_Node (Enc_I));
8792 -- Freeze enclosing subunit before instance
8794 Ensure_Freeze_Node (E_G_Id);
8796 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8797 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8800 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8803 -- If none of the above, insert freeze node at the end of the current
8804 -- declarative part.
8806 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8808 end Freeze_Subprogram_Body;
8814 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8816 return Generic_Renamings.Table (E).Gen_Id;
8819 ---------------------
8820 -- Get_Instance_Of --
8821 ---------------------
8823 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8824 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8827 if Res /= Assoc_Null then
8828 return Generic_Renamings.Table (Res).Act_Id;
8831 -- On exit, entity is not instantiated: not a generic parameter, or
8832 -- else parameter of an inner generic unit.
8836 end Get_Instance_Of;
8838 ---------------------------------
8839 -- Get_Unit_Instantiation_Node --
8840 ---------------------------------
8842 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8843 Decl : Node_Id := Unit_Declaration_Node (A);
8847 -- If the Package_Instantiation attribute has been set on the package
8848 -- entity, then use it directly when it (or its Original_Node) refers
8849 -- to an N_Package_Instantiation node. In principle it should be
8850 -- possible to have this field set in all cases, which should be
8851 -- investigated, and would allow this function to be significantly
8854 Inst := Package_Instantiation (A);
8856 if Present (Inst) then
8857 if Nkind (Inst) = N_Package_Instantiation then
8860 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8861 return Original_Node (Inst);
8865 -- If the instantiation is a compilation unit that does not need body
8866 -- then the instantiation node has been rewritten as a package
8867 -- declaration for the instance, and we return the original node.
8869 -- If it is a compilation unit and the instance node has not been
8870 -- rewritten, then it is still the unit of the compilation. Finally, if
8871 -- a body is present, this is a parent of the main unit whose body has
8872 -- been compiled for inlining purposes, and the instantiation node has
8873 -- been rewritten with the instance body.
8875 -- Otherwise the instantiation node appears after the declaration. If
8876 -- the entity is a formal package, the declaration may have been
8877 -- rewritten as a generic declaration (in the case of a formal with box)
8878 -- or left as a formal package declaration if it has actuals, and is
8879 -- found with a forward search.
8881 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8882 if Nkind (Decl) = N_Package_Declaration
8883 and then Present (Corresponding_Body (Decl))
8885 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8888 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8889 N_Package_Instantiation,
8890 N_Procedure_Instantiation)
8892 return Original_Node (Decl);
8894 return Unit (Parent (Decl));
8897 elsif Nkind (Decl) = N_Package_Declaration
8898 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8900 return Original_Node (Decl);
8903 Inst := Next (Decl);
8904 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8905 N_Function_Instantiation,
8906 N_Package_Instantiation,
8907 N_Procedure_Instantiation)
8914 end Get_Unit_Instantiation_Node;
8916 ------------------------
8917 -- Has_Been_Exchanged --
8918 ------------------------
8920 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8924 Next := First_Elmt (Exchanged_Views);
8925 while Present (Next) loop
8926 if Full_View (Node (Next)) = E then
8934 end Has_Been_Exchanged;
8940 function Hash (F : Entity_Id) return HTable_Range is
8942 return HTable_Range (F mod HTable_Size);
8945 ------------------------
8946 -- Hide_Current_Scope --
8947 ------------------------
8949 procedure Hide_Current_Scope is
8950 C : constant Entity_Id := Current_Scope;
8954 Set_Is_Hidden_Open_Scope (C);
8956 E := First_Entity (C);
8957 while Present (E) loop
8958 if Is_Immediately_Visible (E) then
8959 Set_Is_Immediately_Visible (E, False);
8960 Append_Elmt (E, Hidden_Entities);
8966 -- Make the scope name invisible as well. This is necessary, but might
8967 -- conflict with calls to Rtsfind later on, in case the scope is a
8968 -- predefined one. There is no clean solution to this problem, so for
8969 -- now we depend on the user not redefining Standard itself in one of
8970 -- the parent units.
8972 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8973 Set_Is_Immediately_Visible (C, False);
8974 Append_Elmt (C, Hidden_Entities);
8977 end Hide_Current_Scope;
8983 procedure Init_Env is
8984 Saved : Instance_Env;
8987 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8988 Saved.Exchanged_Views := Exchanged_Views;
8989 Saved.Hidden_Entities := Hidden_Entities;
8990 Saved.Current_Sem_Unit := Current_Sem_Unit;
8991 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8992 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8994 -- Save configuration switches. These may be reset if the unit is a
8995 -- predefined unit, and the current mode is not Ada 2005.
8997 Save_Opt_Config_Switches (Saved.Switches);
8999 Instance_Envs.Append (Saved);
9001 Exchanged_Views := New_Elmt_List;
9002 Hidden_Entities := New_Elmt_List;
9004 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9005 -- this is set properly in Set_Instance_Env.
9007 Current_Instantiated_Parent :=
9008 (Current_Scope, Current_Scope, Assoc_Null);
9011 ---------------------
9012 -- In_Main_Context --
9013 ---------------------
9015 function In_Main_Context (E : Entity_Id) return Boolean is
9021 if not Is_Compilation_Unit (E)
9022 or else Ekind (E) /= E_Package
9023 or else In_Private_Part (E)
9028 Context := Context_Items (Cunit (Main_Unit));
9030 Clause := First (Context);
9031 while Present (Clause) loop
9032 if Nkind (Clause) = N_With_Clause then
9033 Nam := Name (Clause);
9035 -- If the current scope is part of the context of the main unit,
9036 -- analysis of the corresponding with_clause is not complete, and
9037 -- the entity is not set. We use the Chars field directly, which
9038 -- might produce false positives in rare cases, but guarantees
9039 -- that we produce all the instance bodies we will need.
9041 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9042 or else (Nkind (Nam) = N_Selected_Component
9043 and then Chars (Selector_Name (Nam)) = Chars (E))
9053 end In_Main_Context;
9055 ---------------------
9056 -- Inherit_Context --
9057 ---------------------
9059 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9060 Current_Context : List_Id;
9061 Current_Unit : Node_Id;
9070 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9072 -- The inherited context is attached to the enclosing compilation
9073 -- unit. This is either the main unit, or the declaration for the
9074 -- main unit (in case the instantiation appears within the package
9075 -- declaration and the main unit is its body).
9077 Current_Unit := Parent (Inst);
9078 while Present (Current_Unit)
9079 and then Nkind (Current_Unit) /= N_Compilation_Unit
9081 Current_Unit := Parent (Current_Unit);
9084 Current_Context := Context_Items (Current_Unit);
9086 Item := First (Context_Items (Parent (Gen_Decl)));
9087 while Present (Item) loop
9088 if Nkind (Item) = N_With_Clause then
9089 Lib_Unit := Library_Unit (Item);
9091 -- Take care to prevent direct cyclic with's
9093 if Lib_Unit /= Current_Unit then
9095 -- Do not add a unit if it is already in the context
9097 Clause := First (Current_Context);
9099 while Present (Clause) loop
9100 if Nkind (Clause) = N_With_Clause
9101 and then Library_Unit (Clause) = Lib_Unit
9111 New_I := New_Copy (Item);
9112 Set_Implicit_With (New_I);
9114 Append (New_I, Current_Context);
9122 end Inherit_Context;
9128 procedure Initialize is
9130 Generic_Renamings.Init;
9133 Generic_Renamings_HTable.Reset;
9134 Circularity_Detected := False;
9135 Exchanged_Views := No_Elist;
9136 Hidden_Entities := No_Elist;
9139 -------------------------------------
9140 -- Insert_Freeze_Node_For_Instance --
9141 -------------------------------------
9143 procedure Insert_Freeze_Node_For_Instance
9152 function Enclosing_Body (N : Node_Id) return Node_Id;
9153 -- Find enclosing package or subprogram body, if any. Freeze node may
9154 -- be placed at end of current declarative list if previous instance
9155 -- and current one have different enclosing bodies.
9157 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9158 -- Find the local instance, if any, that declares the generic that is
9159 -- being instantiated. If present, the freeze node for this instance
9160 -- must follow the freeze node for the previous instance.
9162 --------------------
9163 -- Enclosing_Body --
9164 --------------------
9166 function Enclosing_Body (N : Node_Id) return Node_Id is
9172 and then Nkind (Parent (P)) /= N_Compilation_Unit
9174 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9175 if Nkind (Parent (P)) = N_Subunit then
9176 return Corresponding_Stub (Parent (P));
9182 P := True_Parent (P);
9188 -----------------------
9189 -- Previous_Instance --
9190 -----------------------
9192 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9197 while Present (S) and then S /= Standard_Standard loop
9198 if Is_Generic_Instance (S)
9199 and then In_Same_Source_Unit (S, N)
9208 end Previous_Instance;
9210 -- Start of processing for Insert_Freeze_Node_For_Instance
9213 if not Is_List_Member (F_Node) then
9215 Decls := List_Containing (N);
9216 Inst := Entity (F_Node);
9217 Par_N := Parent (Decls);
9219 -- When processing a subprogram instantiation, utilize the actual
9220 -- subprogram instantiation rather than its package wrapper as it
9221 -- carries all the context information.
9223 if Is_Wrapper_Package (Inst) then
9224 Inst := Related_Instance (Inst);
9227 -- If this is a package instance, check whether the generic is
9228 -- declared in a previous instance and the current instance is
9229 -- not within the previous one.
9231 if Present (Generic_Parent (Parent (Inst)))
9232 and then Is_In_Main_Unit (N)
9235 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9236 Par_I : constant Entity_Id :=
9238 (Generic_Parent (Parent (Inst)));
9243 and then Earlier (N, Freeze_Node (Par_I))
9245 Scop := Scope (Inst);
9247 -- If the current instance is within the one that contains
9248 -- the generic, the freeze node for the current one must
9249 -- appear in the current declarative part. Ditto, if the
9250 -- current instance is within another package instance or
9251 -- within a body that does not enclose the current instance.
9252 -- In these three cases the freeze node of the previous
9253 -- instance is not relevant.
9255 while Present (Scop) and then Scop /= Standard_Standard loop
9256 exit when Scop = Par_I
9258 (Is_Generic_Instance (Scop)
9259 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9260 Scop := Scope (Scop);
9263 -- Previous instance encloses current instance
9265 if Scop = Par_I then
9268 -- If the next node is a source body we must freeze in
9269 -- the current scope as well.
9271 elsif Present (Next (N))
9272 and then Nkind_In (Next (N), N_Subprogram_Body,
9274 and then Comes_From_Source (Next (N))
9278 -- Current instance is within an unrelated instance
9280 elsif Is_Generic_Instance (Scop) then
9283 -- Current instance is within an unrelated body
9285 elsif Present (Enclosing_N)
9286 and then Enclosing_N /= Enclosing_Body (Par_I)
9291 Insert_After (Freeze_Node (Par_I), F_Node);
9298 -- When the instantiation occurs in a package declaration, append the
9299 -- freeze node to the private declarations (if any).
9301 if Nkind (Par_N) = N_Package_Specification
9302 and then Decls = Visible_Declarations (Par_N)
9303 and then Present (Private_Declarations (Par_N))
9304 and then not Is_Empty_List (Private_Declarations (Par_N))
9306 Decls := Private_Declarations (Par_N);
9307 Decl := First (Decls);
9310 -- Determine the proper freeze point of a package instantiation. We
9311 -- adhere to the general rule of a package or subprogram body causing
9312 -- freezing of anything before it in the same declarative region. In
9313 -- this case, the proper freeze point of a package instantiation is
9314 -- before the first source body which follows, or before a stub. This
9315 -- ensures that entities coming from the instance are already frozen
9316 -- and usable in source bodies.
9318 if Nkind (Par_N) /= N_Package_Declaration
9319 and then Ekind (Inst) = E_Package
9320 and then Is_Generic_Instance (Inst)
9322 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9324 while Present (Decl) loop
9325 if (Nkind (Decl) in N_Unit_Body
9327 Nkind (Decl) in N_Body_Stub)
9328 and then Comes_From_Source (Decl)
9330 Insert_Before (Decl, F_Node);
9338 -- In a package declaration, or if no previous body, insert at end
9341 Set_Sloc (F_Node, Sloc (Last (Decls)));
9342 Insert_After (Last (Decls), F_Node);
9344 end Insert_Freeze_Node_For_Instance;
9350 procedure Install_Body
9351 (Act_Body : Node_Id;
9356 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9357 -- Check if the generic definition and the instantiation come from
9358 -- a common scope, in which case the instance must be frozen after
9359 -- the generic body.
9361 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9362 -- If the instance is nested inside a generic unit, the Sloc of the
9363 -- instance indicates the place of the original definition, not the
9364 -- point of the current enclosing instance. Pending a better usage of
9365 -- Slocs to indicate instantiation places, we determine the place of
9366 -- origin of a node by finding the maximum sloc of any ancestor node.
9367 -- Why is this not equivalent to Top_Level_Location ???
9373 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9374 Act_Scop : Entity_Id := Scope (Act_Id);
9375 Gen_Scop : Entity_Id := Scope (Gen_Id);
9378 while Act_Scop /= Standard_Standard
9379 and then Gen_Scop /= Standard_Standard
9381 if Act_Scop = Gen_Scop then
9385 Act_Scop := Scope (Act_Scop);
9386 Gen_Scop := Scope (Gen_Scop);
9396 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9403 while Present (N1) and then N1 /= Act_Unit loop
9404 if Sloc (N1) > Res then
9414 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9415 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9416 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9417 Par : constant Entity_Id := Scope (Gen_Id);
9418 Gen_Unit : constant Node_Id :=
9419 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9421 Body_Unit : Node_Id;
9423 Must_Delay : Boolean;
9424 Orig_Body : Node_Id := Gen_Body;
9426 -- Start of processing for Install_Body
9429 -- Handle first the case of an instance with incomplete actual types.
9430 -- The instance body cannot be placed after the declaration because
9431 -- full views have not been seen yet. Any use of the non-limited views
9432 -- in the instance body requires the presence of a regular with_clause
9433 -- in the enclosing unit, and will fail if this with_clause is missing.
9434 -- We place the instance body at the beginning of the enclosing body,
9435 -- which is the unit being compiled. The freeze node for the instance
9436 -- is then placed after the instance body.
9438 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9439 and then Expander_Active
9440 and then Ekind (Scope (Act_Id)) = E_Package
9443 Scop : constant Entity_Id := Scope (Act_Id);
9444 Body_Id : constant Node_Id :=
9445 Corresponding_Body (Unit_Declaration_Node (Scop));
9448 Ensure_Freeze_Node (Act_Id);
9449 F_Node := Freeze_Node (Act_Id);
9450 if Present (Body_Id) then
9451 Set_Is_Frozen (Act_Id, False);
9452 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9453 if Is_List_Member (F_Node) then
9457 Insert_After (Act_Body, F_Node);
9463 -- If the body is a subunit, the freeze point is the corresponding stub
9464 -- in the current compilation, not the subunit itself.
9466 if Nkind (Parent (Gen_Body)) = N_Subunit then
9467 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9469 Orig_Body := Gen_Body;
9472 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9474 -- If the instantiation and the generic definition appear in the same
9475 -- package declaration, this is an early instantiation. If they appear
9476 -- in the same declarative part, it is an early instantiation only if
9477 -- the generic body appears textually later, and the generic body is
9478 -- also in the main unit.
9480 -- If instance is nested within a subprogram, and the generic body
9481 -- is not, the instance is delayed because the enclosing body is. If
9482 -- instance and body are within the same scope, or the same subprogram
9483 -- body, indicate explicitly that the instance is delayed.
9486 (Gen_Unit = Act_Unit
9487 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9488 N_Package_Declaration)
9489 or else (Gen_Unit = Body_Unit
9490 and then True_Sloc (N, Act_Unit) <
9492 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9493 and then In_Same_Scope (Gen_Id, Act_Id));
9495 -- If this is an early instantiation, the freeze node is placed after
9496 -- the generic body. Otherwise, if the generic appears in an instance,
9497 -- we cannot freeze the current instance until the outer one is frozen.
9498 -- This is only relevant if the current instance is nested within some
9499 -- inner scope not itself within the outer instance. If this scope is
9500 -- a package body in the same declarative part as the outer instance,
9501 -- then that body needs to be frozen after the outer instance. Finally,
9502 -- if no delay is needed, we place the freeze node at the end of the
9503 -- current declarative part.
9506 and then (No (Freeze_Node (Act_Id))
9507 or else not Is_List_Member (Freeze_Node (Act_Id)))
9509 Ensure_Freeze_Node (Act_Id);
9510 F_Node := Freeze_Node (Act_Id);
9513 Insert_After (Orig_Body, F_Node);
9515 elsif Is_Generic_Instance (Par)
9516 and then Present (Freeze_Node (Par))
9517 and then Scope (Act_Id) /= Par
9519 -- Freeze instance of inner generic after instance of enclosing
9522 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9524 -- Handle the following case:
9526 -- package Parent_Inst is new ...
9529 -- procedure P ... -- this body freezes Parent_Inst
9531 -- package Inst is new ...
9533 -- In this particular scenario, the freeze node for Inst must
9534 -- be inserted in the same manner as that of Parent_Inst,
9535 -- before the next source body or at the end of the declarative
9536 -- list (body not available). If body P did not exist and
9537 -- Parent_Inst was frozen after Inst, either by a body
9538 -- following Inst or at the end of the declarative region,
9539 -- the freeze node for Inst must be inserted after that of
9540 -- Parent_Inst. This relation is established by comparing
9541 -- the Slocs of Parent_Inst freeze node and Inst.
9542 -- We examine the parents of the enclosing lists to handle
9543 -- the case where the parent instance is in the visible part
9544 -- of a package declaration, and the inner instance is in
9545 -- the corresponding private part.
9547 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9548 = Parent (List_Containing (N))
9549 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9551 Insert_Freeze_Node_For_Instance (N, F_Node);
9553 Insert_After (Freeze_Node (Par), F_Node);
9556 -- Freeze package enclosing instance of inner generic after
9557 -- instance of enclosing generic.
9559 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9560 and then In_Same_Declarative_Part
9561 (Parent (Freeze_Node (Par)), Parent (N))
9564 Enclosing : Entity_Id;
9567 Enclosing := Corresponding_Spec (Parent (N));
9569 if No (Enclosing) then
9570 Enclosing := Defining_Entity (Parent (N));
9573 Insert_Freeze_Node_For_Instance (N, F_Node);
9574 Ensure_Freeze_Node (Enclosing);
9576 if not Is_List_Member (Freeze_Node (Enclosing)) then
9578 -- The enclosing context is a subunit, insert the freeze
9579 -- node after the stub.
9581 if Nkind (Parent (Parent (N))) = N_Subunit then
9582 Insert_Freeze_Node_For_Instance
9583 (Corresponding_Stub (Parent (Parent (N))),
9584 Freeze_Node (Enclosing));
9586 -- The enclosing context is a package with a stub body
9587 -- which has already been replaced by the real body.
9588 -- Insert the freeze node after the actual body.
9590 elsif Ekind (Enclosing) = E_Package
9591 and then Present (Body_Entity (Enclosing))
9592 and then Was_Originally_Stub
9593 (Parent (Body_Entity (Enclosing)))
9595 Insert_Freeze_Node_For_Instance
9596 (Parent (Body_Entity (Enclosing)),
9597 Freeze_Node (Enclosing));
9599 -- The parent instance has been frozen before the body of
9600 -- the enclosing package, insert the freeze node after
9603 elsif List_Containing (Freeze_Node (Par)) =
9604 List_Containing (Parent (N))
9605 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9607 Insert_Freeze_Node_For_Instance
9608 (Parent (N), Freeze_Node (Enclosing));
9612 (Freeze_Node (Par), Freeze_Node (Enclosing));
9618 Insert_Freeze_Node_For_Instance (N, F_Node);
9622 Insert_Freeze_Node_For_Instance (N, F_Node);
9626 Set_Is_Frozen (Act_Id);
9627 Insert_Before (N, Act_Body);
9628 Mark_Rewrite_Insertion (Act_Body);
9631 -----------------------------
9632 -- Install_Formal_Packages --
9633 -----------------------------
9635 procedure Install_Formal_Packages (Par : Entity_Id) is
9638 Gen_E : Entity_Id := Empty;
9641 E := First_Entity (Par);
9643 -- If we are installing an instance parent, locate the formal packages
9644 -- of its generic parent.
9646 if Is_Generic_Instance (Par) then
9647 Gen := Generic_Parent (Package_Specification (Par));
9648 Gen_E := First_Entity (Gen);
9651 while Present (E) loop
9652 if Ekind (E) = E_Package
9653 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9655 -- If this is the renaming for the parent instance, done
9657 if Renamed_Object (E) = Par then
9660 -- The visibility of a formal of an enclosing generic is already
9663 elsif Denotes_Formal_Package (E) then
9666 elsif Present (Associated_Formal_Package (E)) then
9667 Check_Generic_Actuals (Renamed_Object (E), True);
9668 Set_Is_Hidden (E, False);
9670 -- Find formal package in generic unit that corresponds to
9671 -- (instance of) formal package in instance.
9673 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9674 Next_Entity (Gen_E);
9677 if Present (Gen_E) then
9678 Map_Formal_Package_Entities (Gen_E, E);
9685 if Present (Gen_E) then
9686 Next_Entity (Gen_E);
9689 end Install_Formal_Packages;
9691 --------------------
9692 -- Install_Parent --
9693 --------------------
9695 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9696 Ancestors : constant Elist_Id := New_Elmt_List;
9697 S : constant Entity_Id := Current_Scope;
9698 Inst_Par : Entity_Id;
9699 First_Par : Entity_Id;
9700 Inst_Node : Node_Id;
9701 Gen_Par : Entity_Id;
9702 First_Gen : Entity_Id;
9705 procedure Install_Noninstance_Specs (Par : Entity_Id);
9706 -- Install the scopes of noninstance parent units ending with Par
9708 procedure Install_Spec (Par : Entity_Id);
9709 -- The child unit is within the declarative part of the parent, so the
9710 -- declarations within the parent are immediately visible.
9712 -------------------------------
9713 -- Install_Noninstance_Specs --
9714 -------------------------------
9716 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9719 and then Par /= Standard_Standard
9720 and then not In_Open_Scopes (Par)
9722 Install_Noninstance_Specs (Scope (Par));
9725 end Install_Noninstance_Specs;
9731 procedure Install_Spec (Par : Entity_Id) is
9732 Spec : constant Node_Id := Package_Specification (Par);
9735 -- If this parent of the child instance is a top-level unit,
9736 -- then record the unit and its visibility for later resetting in
9737 -- Remove_Parent. We exclude units that are generic instances, as we
9738 -- only want to record this information for the ultimate top-level
9739 -- noninstance parent (is that always correct???).
9741 if Scope (Par) = Standard_Standard
9742 and then not Is_Generic_Instance (Par)
9744 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9745 Instance_Parent_Unit := Par;
9748 -- Open the parent scope and make it and its declarations visible.
9749 -- If this point is not within a body, then only the visible
9750 -- declarations should be made visible, and installation of the
9751 -- private declarations is deferred until the appropriate point
9752 -- within analysis of the spec being instantiated (see the handling
9753 -- of parent visibility in Analyze_Package_Specification). This is
9754 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9755 -- private view problems that occur when compiling instantiations of
9756 -- a generic child of that package (Generic_Dispatching_Constructor).
9757 -- If the instance freezes a tagged type, inlinings of operations
9758 -- from Ada.Tags may need the full view of type Tag. If inlining took
9759 -- proper account of establishing visibility of inlined subprograms'
9760 -- parents then it should be possible to remove this
9761 -- special check. ???
9764 Set_Is_Immediately_Visible (Par);
9765 Install_Visible_Declarations (Par);
9766 Set_Use (Visible_Declarations (Spec));
9768 if In_Body or else Is_RTU (Par, Ada_Tags) then
9769 Install_Private_Declarations (Par);
9770 Set_Use (Private_Declarations (Spec));
9774 -- Start of processing for Install_Parent
9777 -- We need to install the parent instance to compile the instantiation
9778 -- of the child, but the child instance must appear in the current
9779 -- scope. Given that we cannot place the parent above the current scope
9780 -- in the scope stack, we duplicate the current scope and unstack both
9781 -- after the instantiation is complete.
9783 -- If the parent is itself the instantiation of a child unit, we must
9784 -- also stack the instantiation of its parent, and so on. Each such
9785 -- ancestor is the prefix of the name in a prior instantiation.
9787 -- If this is a nested instance, the parent unit itself resolves to
9788 -- a renaming of the parent instance, whose declaration we need.
9790 -- Finally, the parent may be a generic (not an instance) when the
9791 -- child unit appears as a formal package.
9795 if Present (Renamed_Entity (Inst_Par)) then
9796 Inst_Par := Renamed_Entity (Inst_Par);
9799 First_Par := Inst_Par;
9801 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9803 First_Gen := Gen_Par;
9805 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9807 -- Load grandparent instance as well
9809 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9811 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9812 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9814 if Present (Renamed_Entity (Inst_Par)) then
9815 Inst_Par := Renamed_Entity (Inst_Par);
9818 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9820 if Present (Gen_Par) then
9821 Prepend_Elmt (Inst_Par, Ancestors);
9824 -- Parent is not the name of an instantiation
9826 Install_Noninstance_Specs (Inst_Par);
9837 if Present (First_Gen) then
9838 Append_Elmt (First_Par, Ancestors);
9840 Install_Noninstance_Specs (First_Par);
9843 if not Is_Empty_Elmt_List (Ancestors) then
9844 Elmt := First_Elmt (Ancestors);
9845 while Present (Elmt) loop
9846 Install_Spec (Node (Elmt));
9847 Install_Formal_Packages (Node (Elmt));
9857 -------------------------------
9858 -- Install_Hidden_Primitives --
9859 -------------------------------
9861 procedure Install_Hidden_Primitives
9862 (Prims_List : in out Elist_Id;
9867 List : Elist_Id := No_Elist;
9868 Prim_G_Elmt : Elmt_Id;
9869 Prim_A_Elmt : Elmt_Id;
9874 -- No action needed in case of serious errors because we cannot trust
9875 -- in the order of primitives
9877 if Serious_Errors_Detected > 0 then
9880 -- No action possible if we don't have available the list of primitive
9884 or else not Is_Record_Type (Gen_T)
9885 or else not Is_Tagged_Type (Gen_T)
9886 or else not Is_Record_Type (Act_T)
9887 or else not Is_Tagged_Type (Act_T)
9891 -- There is no need to handle interface types since their primitives
9894 elsif Is_Interface (Gen_T) then
9898 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9900 if not Is_Class_Wide_Type (Act_T) then
9901 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9903 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9907 -- Skip predefined primitives in the generic formal
9909 while Present (Prim_G_Elmt)
9910 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9912 Next_Elmt (Prim_G_Elmt);
9915 -- Skip predefined primitives in the generic actual
9917 while Present (Prim_A_Elmt)
9918 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9920 Next_Elmt (Prim_A_Elmt);
9923 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9925 Prim_G := Node (Prim_G_Elmt);
9926 Prim_A := Node (Prim_A_Elmt);
9928 -- There is no need to handle interface primitives because their
9929 -- primitives are not hidden
9931 exit when Present (Interface_Alias (Prim_G));
9933 -- Here we install one hidden primitive
9935 if Chars (Prim_G) /= Chars (Prim_A)
9936 and then Has_Suffix (Prim_A, 'P')
9937 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9939 Set_Chars (Prim_A, Chars (Prim_G));
9940 Append_New_Elmt (Prim_A, To => List);
9943 Next_Elmt (Prim_A_Elmt);
9944 Next_Elmt (Prim_G_Elmt);
9947 -- Append the elements to the list of temporarily visible primitives
9948 -- avoiding duplicates.
9950 if Present (List) then
9951 if No (Prims_List) then
9952 Prims_List := New_Elmt_List;
9955 Elmt := First_Elmt (List);
9956 while Present (Elmt) loop
9957 Append_Unique_Elmt (Node (Elmt), Prims_List);
9961 end Install_Hidden_Primitives;
9963 -------------------------------
9964 -- Restore_Hidden_Primitives --
9965 -------------------------------
9967 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9968 Prim_Elmt : Elmt_Id;
9972 if Prims_List /= No_Elist then
9973 Prim_Elmt := First_Elmt (Prims_List);
9974 while Present (Prim_Elmt) loop
9975 Prim := Node (Prim_Elmt);
9976 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9977 Next_Elmt (Prim_Elmt);
9980 Prims_List := No_Elist;
9982 end Restore_Hidden_Primitives;
9984 --------------------------------
9985 -- Instantiate_Formal_Package --
9986 --------------------------------
9988 function Instantiate_Formal_Package
9991 Analyzed_Formal : Node_Id) return List_Id
9993 Loc : constant Source_Ptr := Sloc (Actual);
9994 Hidden_Formals : constant Elist_Id := New_Elmt_List;
9995 Actual_Pack : Entity_Id;
9996 Formal_Pack : Entity_Id;
9997 Gen_Parent : Entity_Id;
10000 Parent_Spec : Node_Id;
10002 procedure Find_Matching_Actual
10004 Act : in out Entity_Id);
10005 -- We need to associate each formal entity in the formal package with
10006 -- the corresponding entity in the actual package. The actual package
10007 -- has been analyzed and possibly expanded, and as a result there is
10008 -- no one-to-one correspondence between the two lists (for example,
10009 -- the actual may include subtypes, itypes, and inherited primitive
10010 -- operations, interspersed among the renaming declarations for the
10011 -- actuals). We retrieve the corresponding actual by name because each
10012 -- actual has the same name as the formal, and they do appear in the
10015 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10016 -- Retrieve entity of defining entity of generic formal parameter.
10017 -- Only the declarations of formals need to be considered when
10018 -- linking them to actuals, but the declarative list may include
10019 -- internal entities generated during analysis, and those are ignored.
10021 procedure Match_Formal_Entity
10022 (Formal_Node : Node_Id;
10023 Formal_Ent : Entity_Id;
10024 Actual_Ent : Entity_Id);
10025 -- Associates the formal entity with the actual. In the case where
10026 -- Formal_Ent is a formal package, this procedure iterates through all
10027 -- of its formals and enters associations between the actuals occurring
10028 -- in the formal package's corresponding actual package (given by
10029 -- Actual_Ent) and the formal package's formal parameters. This
10030 -- procedure recurses if any of the parameters is itself a package.
10032 function Is_Instance_Of
10033 (Act_Spec : Entity_Id;
10034 Gen_Anc : Entity_Id) return Boolean;
10035 -- The actual can be an instantiation of a generic within another
10036 -- instance, in which case there is no direct link from it to the
10037 -- original generic ancestor. In that case, we recognize that the
10038 -- ultimate ancestor is the same by examining names and scopes.
10040 procedure Process_Nested_Formal (Formal : Entity_Id);
10041 -- If the current formal is declared with a box, its own formals are
10042 -- visible in the instance, as they were in the generic, and their
10043 -- Hidden flag must be reset. If some of these formals are themselves
10044 -- packages declared with a box, the processing must be recursive.
10046 --------------------------
10047 -- Find_Matching_Actual --
10048 --------------------------
10050 procedure Find_Matching_Actual
10052 Act : in out Entity_Id)
10054 Formal_Ent : Entity_Id;
10057 case Nkind (Original_Node (F)) is
10058 when N_Formal_Object_Declaration
10059 | N_Formal_Type_Declaration
10061 Formal_Ent := Defining_Identifier (F);
10063 while Chars (Act) /= Chars (Formal_Ent) loop
10067 when N_Formal_Package_Declaration
10068 | N_Formal_Subprogram_Declaration
10069 | N_Generic_Package_Declaration
10070 | N_Package_Declaration
10072 Formal_Ent := Defining_Entity (F);
10074 while Chars (Act) /= Chars (Formal_Ent) loop
10079 raise Program_Error;
10081 end Find_Matching_Actual;
10083 -------------------------
10084 -- Match_Formal_Entity --
10085 -------------------------
10087 procedure Match_Formal_Entity
10088 (Formal_Node : Node_Id;
10089 Formal_Ent : Entity_Id;
10090 Actual_Ent : Entity_Id)
10092 Act_Pkg : Entity_Id;
10095 Set_Instance_Of (Formal_Ent, Actual_Ent);
10097 if Ekind (Actual_Ent) = E_Package then
10099 -- Record associations for each parameter
10101 Act_Pkg := Actual_Ent;
10104 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10108 Gen_Decl : Node_Id;
10110 Actual : Entity_Id;
10113 -- Retrieve the actual given in the formal package declaration
10115 Actual := Entity (Name (Original_Node (Formal_Node)));
10117 -- The actual in the formal package declaration may be a
10118 -- renamed generic package, in which case we want to retrieve
10119 -- the original generic in order to traverse its formal part.
10121 if Present (Renamed_Entity (Actual)) then
10122 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10124 Gen_Decl := Unit_Declaration_Node (Actual);
10127 Formals := Generic_Formal_Declarations (Gen_Decl);
10129 if Present (Formals) then
10130 F_Node := First_Non_Pragma (Formals);
10135 while Present (A_Ent)
10136 and then Present (F_Node)
10137 and then A_Ent /= First_Private_Entity (Act_Pkg)
10139 F_Ent := Get_Formal_Entity (F_Node);
10141 if Present (F_Ent) then
10143 -- This is a formal of the original package. Record
10144 -- association and recurse.
10146 Find_Matching_Actual (F_Node, A_Ent);
10147 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10148 Next_Entity (A_Ent);
10151 Next_Non_Pragma (F_Node);
10155 end Match_Formal_Entity;
10157 -----------------------
10158 -- Get_Formal_Entity --
10159 -----------------------
10161 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10162 Kind : constant Node_Kind := Nkind (Original_Node (N));
10165 when N_Formal_Object_Declaration =>
10166 return Defining_Identifier (N);
10168 when N_Formal_Type_Declaration =>
10169 return Defining_Identifier (N);
10171 when N_Formal_Subprogram_Declaration =>
10172 return Defining_Unit_Name (Specification (N));
10174 when N_Formal_Package_Declaration =>
10175 return Defining_Identifier (Original_Node (N));
10177 when N_Generic_Package_Declaration =>
10178 return Defining_Identifier (Original_Node (N));
10180 -- All other declarations are introduced by semantic analysis and
10181 -- have no match in the actual.
10186 end Get_Formal_Entity;
10188 --------------------
10189 -- Is_Instance_Of --
10190 --------------------
10192 function Is_Instance_Of
10193 (Act_Spec : Entity_Id;
10194 Gen_Anc : Entity_Id) return Boolean
10196 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10199 if No (Gen_Par) then
10202 -- Simplest case: the generic parent of the actual is the formal
10204 elsif Gen_Par = Gen_Anc then
10207 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10210 -- The actual may be obtained through several instantiations. Its
10211 -- scope must itself be an instance of a generic declared in the
10212 -- same scope as the formal. Any other case is detected above.
10214 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10218 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10220 end Is_Instance_Of;
10222 ---------------------------
10223 -- Process_Nested_Formal --
10224 ---------------------------
10226 procedure Process_Nested_Formal (Formal : Entity_Id) is
10230 if Present (Associated_Formal_Package (Formal))
10231 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10233 Ent := First_Entity (Formal);
10234 while Present (Ent) loop
10235 Set_Is_Hidden (Ent, False);
10236 Set_Is_Visible_Formal (Ent);
10237 Set_Is_Potentially_Use_Visible
10238 (Ent, Is_Potentially_Use_Visible (Formal));
10240 if Ekind (Ent) = E_Package then
10241 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10242 Process_Nested_Formal (Ent);
10248 end Process_Nested_Formal;
10250 -- Start of processing for Instantiate_Formal_Package
10255 if not Is_Entity_Name (Actual)
10256 or else Ekind (Entity (Actual)) /= E_Package
10259 ("expect package instance to instantiate formal", Actual);
10260 Abandon_Instantiation (Actual);
10261 raise Program_Error;
10264 Actual_Pack := Entity (Actual);
10265 Set_Is_Instantiated (Actual_Pack);
10267 -- The actual may be a renamed package, or an outer generic formal
10268 -- package whose instantiation is converted into a renaming.
10270 if Present (Renamed_Object (Actual_Pack)) then
10271 Actual_Pack := Renamed_Object (Actual_Pack);
10274 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10275 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10276 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10279 Generic_Parent (Specification (Analyzed_Formal));
10281 Defining_Unit_Name (Specification (Analyzed_Formal));
10284 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10285 Parent_Spec := Package_Specification (Actual_Pack);
10287 Parent_Spec := Parent (Actual_Pack);
10290 if Gen_Parent = Any_Id then
10292 ("previous error in declaration of formal package", Actual);
10293 Abandon_Instantiation (Actual);
10296 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10302 ("actual parameter must be instance of&", Actual, Gen_Parent);
10303 Abandon_Instantiation (Actual);
10306 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10307 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10310 Make_Package_Renaming_Declaration (Loc,
10311 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10312 Name => New_Occurrence_Of (Actual_Pack, Loc));
10314 Set_Associated_Formal_Package
10315 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10316 Decls := New_List (Nod);
10318 -- If the formal F has a box, then the generic declarations are
10319 -- visible in the generic G. In an instance of G, the corresponding
10320 -- entities in the actual for F (which are the actuals for the
10321 -- instantiation of the generic that F denotes) must also be made
10322 -- visible for analysis of the current instance. On exit from the
10323 -- current instance, those entities are made private again. If the
10324 -- actual is currently in use, these entities are also use-visible.
10326 -- The loop through the actual entities also steps through the formal
10327 -- entities and enters associations from formals to actuals into the
10328 -- renaming map. This is necessary to properly handle checking of
10329 -- actual parameter associations for later formals that depend on
10330 -- actuals declared in the formal package.
10332 -- In Ada 2005, partial parameterization requires that we make
10333 -- visible the actuals corresponding to formals that were defaulted
10334 -- in the formal package. There formals are identified because they
10335 -- remain formal generics within the formal package, rather than
10336 -- being renamings of the actuals supplied.
10339 Gen_Decl : constant Node_Id :=
10340 Unit_Declaration_Node (Gen_Parent);
10341 Formals : constant List_Id :=
10342 Generic_Formal_Declarations (Gen_Decl);
10344 Actual_Ent : Entity_Id;
10345 Actual_Of_Formal : Node_Id;
10346 Formal_Node : Node_Id;
10347 Formal_Ent : Entity_Id;
10350 if Present (Formals) then
10351 Formal_Node := First_Non_Pragma (Formals);
10353 Formal_Node := Empty;
10356 Actual_Ent := First_Entity (Actual_Pack);
10357 Actual_Of_Formal :=
10358 First (Visible_Declarations (Specification (Analyzed_Formal)));
10359 while Present (Actual_Ent)
10360 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10362 if Present (Formal_Node) then
10363 Formal_Ent := Get_Formal_Entity (Formal_Node);
10365 if Present (Formal_Ent) then
10366 Find_Matching_Actual (Formal_Node, Actual_Ent);
10367 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10369 -- We iterate at the same time over the actuals of the
10370 -- local package created for the formal, to determine
10371 -- which one of the formals of the original generic were
10372 -- defaulted in the formal. The corresponding actual
10373 -- entities are visible in the enclosing instance.
10375 if Box_Present (Formal)
10377 (Present (Actual_Of_Formal)
10380 (Get_Formal_Entity (Actual_Of_Formal)))
10382 Set_Is_Hidden (Actual_Ent, False);
10383 Set_Is_Visible_Formal (Actual_Ent);
10384 Set_Is_Potentially_Use_Visible
10385 (Actual_Ent, In_Use (Actual_Pack));
10387 if Ekind (Actual_Ent) = E_Package then
10388 Process_Nested_Formal (Actual_Ent);
10392 if not Is_Hidden (Actual_Ent) then
10393 Append_Elmt (Actual_Ent, Hidden_Formals);
10396 Set_Is_Hidden (Actual_Ent);
10397 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10401 Next_Non_Pragma (Formal_Node);
10402 Next (Actual_Of_Formal);
10405 -- No further formals to match, but the generic part may
10406 -- contain inherited operation that are not hidden in the
10407 -- enclosing instance.
10409 Next_Entity (Actual_Ent);
10413 -- Inherited subprograms generated by formal derived types are
10414 -- also visible if the types are.
10416 Actual_Ent := First_Entity (Actual_Pack);
10417 while Present (Actual_Ent)
10418 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10420 if Is_Overloadable (Actual_Ent)
10422 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10424 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10426 Set_Is_Hidden (Actual_Ent, False);
10427 Set_Is_Potentially_Use_Visible
10428 (Actual_Ent, In_Use (Actual_Pack));
10431 Next_Entity (Actual_Ent);
10435 -- If the formal is not declared with a box, reanalyze it as an
10436 -- abbreviated instantiation, to verify the matching rules of 12.7.
10437 -- The actual checks are performed after the generic associations
10438 -- have been analyzed, to guarantee the same visibility for this
10439 -- instantiation and for the actuals.
10441 -- In Ada 2005, the generic associations for the formal can include
10442 -- defaulted parameters. These are ignored during check. This
10443 -- internal instantiation is removed from the tree after conformance
10444 -- checking, because it contains formal declarations for those
10445 -- defaulted parameters, and those should not reach the back-end.
10447 if not Box_Present (Formal) then
10449 I_Pack : constant Entity_Id :=
10450 Make_Temporary (Sloc (Actual), 'P');
10453 Set_Is_Internal (I_Pack);
10454 Set_Ekind (I_Pack, E_Package);
10455 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10458 Make_Package_Instantiation (Sloc (Actual),
10459 Defining_Unit_Name => I_Pack,
10462 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10463 Generic_Associations => Generic_Associations (Formal)));
10469 end Instantiate_Formal_Package;
10471 -----------------------------------
10472 -- Instantiate_Formal_Subprogram --
10473 -----------------------------------
10475 function Instantiate_Formal_Subprogram
10478 Analyzed_Formal : Node_Id) return Node_Id
10480 Analyzed_S : constant Entity_Id :=
10481 Defining_Unit_Name (Specification (Analyzed_Formal));
10482 Formal_Sub : constant Entity_Id :=
10483 Defining_Unit_Name (Specification (Formal));
10485 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10486 -- If the generic is a child unit, the parent has been installed on the
10487 -- scope stack, but a default subprogram cannot resolve to something
10488 -- on the parent because that parent is not really part of the visible
10489 -- context (it is there to resolve explicit local entities). If the
10490 -- default has resolved in this way, we remove the entity from immediate
10491 -- visibility and analyze the node again to emit an error message or
10492 -- find another visible candidate.
10494 procedure Valid_Actual_Subprogram (Act : Node_Id);
10495 -- Perform legality check and raise exception on failure
10497 -----------------------
10498 -- From_Parent_Scope --
10499 -----------------------
10501 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10502 Gen_Scope : Node_Id;
10505 Gen_Scope := Scope (Analyzed_S);
10506 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10507 if Scope (Subp) = Scope (Gen_Scope) then
10511 Gen_Scope := Scope (Gen_Scope);
10515 end From_Parent_Scope;
10517 -----------------------------
10518 -- Valid_Actual_Subprogram --
10519 -----------------------------
10521 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10525 if Is_Entity_Name (Act) then
10526 Act_E := Entity (Act);
10528 elsif Nkind (Act) = N_Selected_Component
10529 and then Is_Entity_Name (Selector_Name (Act))
10531 Act_E := Entity (Selector_Name (Act));
10537 if (Present (Act_E) and then Is_Overloadable (Act_E))
10538 or else Nkind_In (Act, N_Attribute_Reference,
10539 N_Indexed_Component,
10540 N_Character_Literal,
10541 N_Explicit_Dereference)
10547 ("expect subprogram or entry name in instantiation of &",
10548 Instantiation_Node, Formal_Sub);
10549 Abandon_Instantiation (Instantiation_Node);
10550 end Valid_Actual_Subprogram;
10554 Decl_Node : Node_Id;
10557 New_Spec : Node_Id;
10558 New_Subp : Entity_Id;
10560 -- Start of processing for Instantiate_Formal_Subprogram
10563 New_Spec := New_Copy_Tree (Specification (Formal));
10565 -- The tree copy has created the proper instantiation sloc for the
10566 -- new specification. Use this location for all other constructed
10569 Loc := Sloc (Defining_Unit_Name (New_Spec));
10571 -- Create new entity for the actual (New_Copy_Tree does not), and
10572 -- indicate that it is an actual.
10574 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10575 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10576 Set_Is_Generic_Actual_Subprogram (New_Subp);
10577 Set_Defining_Unit_Name (New_Spec, New_Subp);
10579 -- Create new entities for the each of the formals in the specification
10580 -- of the renaming declaration built for the actual.
10582 if Present (Parameter_Specifications (New_Spec)) then
10588 F := First (Parameter_Specifications (New_Spec));
10589 while Present (F) loop
10590 F_Id := Defining_Identifier (F);
10592 Set_Defining_Identifier (F,
10593 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10599 -- Find entity of actual. If the actual is an attribute reference, it
10600 -- cannot be resolved here (its formal is missing) but is handled
10601 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10602 -- fully resolved subsequently, when the renaming declaration for the
10603 -- formal is analyzed. If it is an explicit dereference, resolve the
10604 -- prefix but not the actual itself, to prevent interpretation as call.
10606 if Present (Actual) then
10607 Loc := Sloc (Actual);
10608 Set_Sloc (New_Spec, Loc);
10610 if Nkind (Actual) = N_Operator_Symbol then
10611 Find_Direct_Name (Actual);
10613 elsif Nkind (Actual) = N_Explicit_Dereference then
10614 Analyze (Prefix (Actual));
10616 elsif Nkind (Actual) /= N_Attribute_Reference then
10620 Valid_Actual_Subprogram (Actual);
10623 elsif Present (Default_Name (Formal)) then
10624 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10625 N_Selected_Component,
10626 N_Indexed_Component,
10627 N_Character_Literal)
10628 and then Present (Entity (Default_Name (Formal)))
10630 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10632 Nam := New_Copy (Default_Name (Formal));
10633 Set_Sloc (Nam, Loc);
10636 elsif Box_Present (Formal) then
10638 -- Actual is resolved at the point of instantiation. Create an
10639 -- identifier or operator with the same name as the formal.
10641 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10643 Make_Operator_Symbol (Loc,
10644 Chars => Chars (Formal_Sub),
10645 Strval => No_String);
10647 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10650 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10651 and then Null_Present (Specification (Formal))
10653 -- Generate null body for procedure, for use in the instance
10656 Make_Subprogram_Body (Loc,
10657 Specification => New_Spec,
10658 Declarations => New_List,
10659 Handled_Statement_Sequence =>
10660 Make_Handled_Sequence_Of_Statements (Loc,
10661 Statements => New_List (Make_Null_Statement (Loc))));
10663 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10667 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10669 ("missing actual&", Instantiation_Node, Formal_Sub);
10671 ("\in instantiation of & declared#",
10672 Instantiation_Node, Scope (Analyzed_S));
10673 Abandon_Instantiation (Instantiation_Node);
10677 Make_Subprogram_Renaming_Declaration (Loc,
10678 Specification => New_Spec,
10681 -- If we do not have an actual and the formal specified <> then set to
10682 -- get proper default.
10684 if No (Actual) and then Box_Present (Formal) then
10685 Set_From_Default (Decl_Node);
10688 -- Gather possible interpretations for the actual before analyzing the
10689 -- instance. If overloaded, it will be resolved when analyzing the
10690 -- renaming declaration.
10692 if Box_Present (Formal) and then No (Actual) then
10695 if Is_Child_Unit (Scope (Analyzed_S))
10696 and then Present (Entity (Nam))
10698 if not Is_Overloaded (Nam) then
10699 if From_Parent_Scope (Entity (Nam)) then
10700 Set_Is_Immediately_Visible (Entity (Nam), False);
10701 Set_Entity (Nam, Empty);
10702 Set_Etype (Nam, Empty);
10705 Set_Is_Immediately_Visible (Entity (Nam));
10714 Get_First_Interp (Nam, I, It);
10715 while Present (It.Nam) loop
10716 if From_Parent_Scope (It.Nam) then
10720 Get_Next_Interp (I, It);
10727 -- The generic instantiation freezes the actual. This can only be done
10728 -- once the actual is resolved, in the analysis of the renaming
10729 -- declaration. To make the formal subprogram entity available, we set
10730 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10731 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10732 -- of formal abstract subprograms.
10734 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10736 -- We cannot analyze the renaming declaration, and thus find the actual,
10737 -- until all the actuals are assembled in the instance. For subsequent
10738 -- checks of other actuals, indicate the node that will hold the
10739 -- instance of this formal.
10741 Set_Instance_Of (Analyzed_S, Nam);
10743 if Nkind (Actual) = N_Selected_Component
10744 and then Is_Task_Type (Etype (Prefix (Actual)))
10745 and then not Is_Frozen (Etype (Prefix (Actual)))
10747 -- The renaming declaration will create a body, which must appear
10748 -- outside of the instantiation, We move the renaming declaration
10749 -- out of the instance, and create an additional renaming inside,
10750 -- to prevent freezing anomalies.
10753 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10756 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10757 Insert_Before (Instantiation_Node, Decl_Node);
10758 Analyze (Decl_Node);
10760 -- Now create renaming within the instance
10763 Make_Subprogram_Renaming_Declaration (Loc,
10764 Specification => New_Copy_Tree (New_Spec),
10765 Name => New_Occurrence_Of (Anon_Id, Loc));
10767 Set_Defining_Unit_Name (Specification (Decl_Node),
10768 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10773 end Instantiate_Formal_Subprogram;
10775 ------------------------
10776 -- Instantiate_Object --
10777 ------------------------
10779 function Instantiate_Object
10782 Analyzed_Formal : Node_Id) return List_Id
10784 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10785 A_Gen_Obj : constant Entity_Id :=
10786 Defining_Identifier (Analyzed_Formal);
10787 Acc_Def : Node_Id := Empty;
10788 Act_Assoc : constant Node_Id := Parent (Actual);
10789 Actual_Decl : Node_Id := Empty;
10790 Decl_Node : Node_Id;
10793 List : constant List_Id := New_List;
10794 Loc : constant Source_Ptr := Sloc (Actual);
10795 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10796 Subt_Decl : Node_Id := Empty;
10797 Subt_Mark : Node_Id := Empty;
10799 function Copy_Access_Def return Node_Id;
10800 -- If formal is an anonymous access, copy access definition of formal
10801 -- for generated object declaration.
10803 ---------------------
10804 -- Copy_Access_Def --
10805 ---------------------
10807 function Copy_Access_Def return Node_Id is
10809 Def := New_Copy_Tree (Acc_Def);
10811 -- In addition, if formal is an access to subprogram we need to
10812 -- generate new formals for the signature of the default, so that
10813 -- the tree is properly formatted for ASIS use.
10815 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10817 Par_Spec : Node_Id;
10820 First (Parameter_Specifications
10821 (Access_To_Subprogram_Definition (Def)));
10822 while Present (Par_Spec) loop
10823 Set_Defining_Identifier (Par_Spec,
10824 Make_Defining_Identifier (Sloc (Acc_Def),
10825 Chars => Chars (Defining_Identifier (Par_Spec))));
10832 end Copy_Access_Def;
10834 -- Start of processing for Instantiate_Object
10837 -- Formal may be an anonymous access
10839 if Present (Subtype_Mark (Formal)) then
10840 Subt_Mark := Subtype_Mark (Formal);
10842 Check_Access_Definition (Formal);
10843 Acc_Def := Access_Definition (Formal);
10846 -- Sloc for error message on missing actual
10848 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10850 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10851 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10854 Set_Parent (List, Parent (Actual));
10858 if Out_Present (Formal) then
10860 -- An IN OUT generic actual must be a name. The instantiation is a
10861 -- renaming declaration. The actual is the name being renamed. We
10862 -- use the actual directly, rather than a copy, because it is not
10863 -- used further in the list of actuals, and because a copy or a use
10864 -- of relocate_node is incorrect if the instance is nested within a
10865 -- generic. In order to simplify ASIS searches, the Generic_Parent
10866 -- field links the declaration to the generic association.
10868 if No (Actual) then
10870 ("missing actual &",
10871 Instantiation_Node, Gen_Obj);
10873 ("\in instantiation of & declared#",
10874 Instantiation_Node, Scope (A_Gen_Obj));
10875 Abandon_Instantiation (Instantiation_Node);
10878 if Present (Subt_Mark) then
10880 Make_Object_Renaming_Declaration (Loc,
10881 Defining_Identifier => New_Copy (Gen_Obj),
10882 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10885 else pragma Assert (Present (Acc_Def));
10887 Make_Object_Renaming_Declaration (Loc,
10888 Defining_Identifier => New_Copy (Gen_Obj),
10889 Access_Definition => New_Copy_Tree (Acc_Def),
10893 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10895 -- The analysis of the actual may produce Insert_Action nodes, so
10896 -- the declaration must have a context in which to attach them.
10898 Append (Decl_Node, List);
10901 -- Return if the analysis of the actual reported some error
10903 if Etype (Actual) = Any_Type then
10907 -- This check is performed here because Analyze_Object_Renaming will
10908 -- not check it when Comes_From_Source is False. Note though that the
10909 -- check for the actual being the name of an object will be performed
10910 -- in Analyze_Object_Renaming.
10912 if Is_Object_Reference (Actual)
10913 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10916 ("illegal discriminant-dependent component for in out parameter",
10920 -- The actual has to be resolved in order to check that it is a
10921 -- variable (due to cases such as F (1), where F returns access to
10922 -- an array, and for overloaded prefixes).
10924 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10926 -- If the type of the formal is not itself a formal, and the current
10927 -- unit is a child unit, the formal type must be declared in a
10928 -- parent, and must be retrieved by visibility.
10930 if Ftyp = Orig_Ftyp
10931 and then Is_Generic_Unit (Scope (Ftyp))
10932 and then Is_Child_Unit (Scope (A_Gen_Obj))
10935 Temp : constant Node_Id :=
10936 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10938 Set_Entity (Temp, Empty);
10940 Ftyp := Entity (Temp);
10944 if Is_Private_Type (Ftyp)
10945 and then not Is_Private_Type (Etype (Actual))
10946 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10947 or else Base_Type (Etype (Actual)) = Ftyp)
10949 -- If the actual has the type of the full view of the formal, or
10950 -- else a non-private subtype of the formal, then the visibility
10951 -- of the formal type has changed. Add to the actuals a subtype
10952 -- declaration that will force the exchange of views in the body
10953 -- of the instance as well.
10956 Make_Subtype_Declaration (Loc,
10957 Defining_Identifier => Make_Temporary (Loc, 'P'),
10958 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10960 Prepend (Subt_Decl, List);
10962 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10963 Exchange_Declarations (Ftyp);
10966 Resolve (Actual, Ftyp);
10968 if not Denotes_Variable (Actual) then
10969 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10971 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10973 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10974 -- the type of the actual shall resolve to a specific anonymous
10977 if Ada_Version < Ada_2005
10978 or else Ekind (Base_Type (Ftyp)) /=
10979 E_Anonymous_Access_Type
10980 or else Ekind (Base_Type (Etype (Actual))) /=
10981 E_Anonymous_Access_Type
10984 ("type of actual does not match type of&", Actual, Gen_Obj);
10988 Note_Possible_Modification (Actual, Sure => True);
10990 -- Check for instantiation of atomic/volatile actual for
10991 -- non-atomic/volatile formal (RM C.6 (12)).
10993 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10995 ("cannot instantiate non-atomic formal object "
10996 & "with atomic actual", Actual);
10998 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11001 ("cannot instantiate non-volatile formal object "
11002 & "with volatile actual", Actual);
11005 -- Formal in-parameter
11008 -- The instantiation of a generic formal in-parameter is constant
11009 -- declaration. The actual is the expression for that declaration.
11010 -- Its type is a full copy of the type of the formal. This may be
11011 -- an access to subprogram, for which we need to generate entities
11012 -- for the formals in the new signature.
11014 if Present (Actual) then
11015 if Present (Subt_Mark) then
11016 Def := New_Copy_Tree (Subt_Mark);
11017 else pragma Assert (Present (Acc_Def));
11018 Def := Copy_Access_Def;
11022 Make_Object_Declaration (Loc,
11023 Defining_Identifier => New_Copy (Gen_Obj),
11024 Constant_Present => True,
11025 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11026 Object_Definition => Def,
11027 Expression => Actual);
11029 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11031 -- A generic formal object of a tagged type is defined to be
11032 -- aliased so the new constant must also be treated as aliased.
11034 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11035 Set_Aliased_Present (Decl_Node);
11038 Append (Decl_Node, List);
11040 -- No need to repeat (pre-)analysis of some expression nodes
11041 -- already handled in Preanalyze_Actuals.
11043 if Nkind (Actual) /= N_Allocator then
11046 -- Return if the analysis of the actual reported some error
11048 if Etype (Actual) = Any_Type then
11054 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11058 Typ := Get_Instance_Of (Formal_Type);
11060 -- If the actual appears in the current or an enclosing scope,
11061 -- use its type directly. This is relevant if it has an actual
11062 -- subtype that is distinct from its nominal one. This cannot
11063 -- be done in general because the type of the actual may
11064 -- depend on other actuals, and only be fully determined when
11065 -- the enclosing instance is analyzed.
11067 if Present (Etype (Actual))
11068 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11070 Freeze_Before (Instantiation_Node, Etype (Actual));
11072 Freeze_Before (Instantiation_Node, Typ);
11075 -- If the actual is an aggregate, perform name resolution on
11076 -- its components (the analysis of an aggregate does not do it)
11077 -- to capture local names that may be hidden if the generic is
11080 if Nkind (Actual) = N_Aggregate then
11081 Preanalyze_And_Resolve (Actual, Typ);
11084 if Is_Limited_Type (Typ)
11085 and then not OK_For_Limited_Init (Typ, Actual)
11088 ("initialization not allowed for limited types", Actual);
11089 Explain_Limited_Type (Typ, Actual);
11093 elsif Present (Default_Expression (Formal)) then
11095 -- Use default to construct declaration
11097 if Present (Subt_Mark) then
11098 Def := New_Copy (Subt_Mark);
11099 else pragma Assert (Present (Acc_Def));
11100 Def := Copy_Access_Def;
11104 Make_Object_Declaration (Sloc (Formal),
11105 Defining_Identifier => New_Copy (Gen_Obj),
11106 Constant_Present => True,
11107 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11108 Object_Definition => Def,
11109 Expression => New_Copy_Tree
11110 (Default_Expression (Formal)));
11112 Append (Decl_Node, List);
11113 Set_Analyzed (Expression (Decl_Node), False);
11116 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11117 Error_Msg_NE ("\in instantiation of & declared#",
11118 Instantiation_Node, Scope (A_Gen_Obj));
11120 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11122 -- Create dummy constant declaration so that instance can be
11123 -- analyzed, to minimize cascaded visibility errors.
11125 if Present (Subt_Mark) then
11127 else pragma Assert (Present (Acc_Def));
11132 Make_Object_Declaration (Loc,
11133 Defining_Identifier => New_Copy (Gen_Obj),
11134 Constant_Present => True,
11135 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11136 Object_Definition => New_Copy (Def),
11138 Make_Attribute_Reference (Sloc (Gen_Obj),
11139 Attribute_Name => Name_First,
11140 Prefix => New_Copy (Def)));
11142 Append (Decl_Node, List);
11145 Abandon_Instantiation (Instantiation_Node);
11150 if Nkind (Actual) in N_Has_Entity then
11151 Actual_Decl := Parent (Entity (Actual));
11154 -- Ada 2005 (AI-423): For a formal object declaration with a null
11155 -- exclusion or an access definition that has a null exclusion: If the
11156 -- actual matching the formal object declaration denotes a generic
11157 -- formal object of another generic unit G, and the instantiation
11158 -- containing the actual occurs within the body of G or within the body
11159 -- of a generic unit declared within the declarative region of G, then
11160 -- the declaration of the formal object of G must have a null exclusion.
11161 -- Otherwise, the subtype of the actual matching the formal object
11162 -- declaration shall exclude null.
11164 if Ada_Version >= Ada_2005
11165 and then Present (Actual_Decl)
11166 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11167 N_Object_Declaration)
11168 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11169 and then not Has_Null_Exclusion (Actual_Decl)
11170 and then Has_Null_Exclusion (Analyzed_Formal)
11172 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11174 ("actual must exclude null to match generic formal#", Actual);
11177 -- An effectively volatile object cannot be used as an actual in a
11178 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11179 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11180 -- legality rule, and also verifies that the actual is an object.
11183 and then Present (Actual)
11184 and then Is_Object_Reference (Actual)
11185 and then Is_Effectively_Volatile_Object (Actual)
11188 ("volatile object cannot act as actual in generic instantiation",
11193 end Instantiate_Object;
11195 ------------------------------
11196 -- Instantiate_Package_Body --
11197 ------------------------------
11199 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11200 -- must be replaced by gotos which jump to the end of the routine in order
11201 -- to restore the Ghost and SPARK modes.
11203 procedure Instantiate_Package_Body
11204 (Body_Info : Pending_Body_Info;
11205 Inlined_Body : Boolean := False;
11206 Body_Optional : Boolean := False)
11208 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11209 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11210 Act_Spec : constant Node_Id := Specification (Act_Decl);
11211 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11212 Gen_Id : constant Node_Id := Name (Inst_Node);
11213 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11214 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11215 Loc : constant Source_Ptr := Sloc (Inst_Node);
11217 Saved_ISMP : constant Boolean :=
11218 Ignore_SPARK_Mode_Pragmas_In_Instance;
11219 Saved_Style_Check : constant Boolean := Style_Check;
11221 procedure Check_Initialized_Types;
11222 -- In a generic package body, an entity of a generic private type may
11223 -- appear uninitialized. This is suspicious, unless the actual is a
11224 -- fully initialized type.
11226 -----------------------------
11227 -- Check_Initialized_Types --
11228 -----------------------------
11230 procedure Check_Initialized_Types is
11232 Formal : Entity_Id;
11233 Actual : Entity_Id;
11234 Uninit_Var : Entity_Id;
11237 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11238 while Present (Decl) loop
11239 Uninit_Var := Empty;
11241 if Nkind (Decl) = N_Private_Extension_Declaration then
11242 Uninit_Var := Uninitialized_Variable (Decl);
11244 elsif Nkind (Decl) = N_Formal_Type_Declaration
11245 and then Nkind (Formal_Type_Definition (Decl)) =
11246 N_Formal_Private_Type_Definition
11249 Uninitialized_Variable (Formal_Type_Definition (Decl));
11252 if Present (Uninit_Var) then
11253 Formal := Defining_Identifier (Decl);
11254 Actual := First_Entity (Act_Decl_Id);
11256 -- For each formal there is a subtype declaration that renames
11257 -- the actual and has the same name as the formal. Locate the
11258 -- formal for warning message about uninitialized variables
11259 -- in the generic, for which the actual type should be a fully
11260 -- initialized type.
11262 while Present (Actual) loop
11263 exit when Ekind (Actual) = E_Package
11264 and then Present (Renamed_Object (Actual));
11266 if Chars (Actual) = Chars (Formal)
11267 and then not Is_Scalar_Type (Actual)
11268 and then not Is_Fully_Initialized_Type (Actual)
11269 and then Warn_On_No_Value_Assigned
11271 Error_Msg_Node_2 := Formal;
11273 ("generic unit has uninitialized variable& of "
11274 & "formal private type &?v?", Actual, Uninit_Var);
11276 ("actual type for& should be fully initialized type?v?",
11281 Next_Entity (Actual);
11287 end Check_Initialized_Types;
11291 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11292 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11293 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11294 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11295 -- Save the Ghost and SPARK mode-related data to restore on exit
11297 Act_Body : Node_Id;
11298 Act_Body_Id : Entity_Id;
11299 Act_Body_Name : Node_Id;
11300 Gen_Body : Node_Id;
11301 Gen_Body_Id : Node_Id;
11302 Par_Ent : Entity_Id := Empty;
11303 Par_Vis : Boolean := False;
11304 Parent_Installed : Boolean := False;
11306 Vis_Prims_List : Elist_Id := No_Elist;
11307 -- List of primitives made temporarily visible in the instantiation
11308 -- to match the visibility of the formal type.
11310 -- Start of processing for Instantiate_Package_Body
11313 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11315 -- The instance body may already have been processed, as the parent of
11316 -- another instance that is inlined (Load_Parent_Of_Generic).
11318 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11322 -- The package being instantiated may be subject to pragma Ghost. Set
11323 -- the mode now to ensure that any nodes generated during instantiation
11324 -- are properly marked as Ghost.
11326 Set_Ghost_Mode (Act_Decl_Id);
11328 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11330 -- Re-establish the state of information on which checks are suppressed.
11331 -- This information was set in Body_Info at the point of instantiation,
11332 -- and now we restore it so that the instance is compiled using the
11333 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11335 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11336 Scope_Suppress := Body_Info.Scope_Suppress;
11337 Opt.Ada_Version := Body_Info.Version;
11338 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11339 Restore_Warnings (Body_Info.Warnings);
11341 -- Install the SPARK mode which applies to the package body
11343 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11345 if No (Gen_Body_Id) then
11347 -- Do not look for parent of generic body if none is required.
11348 -- This may happen when the routine is called as part of the
11349 -- Pending_Instantiations processing, when nested instances
11350 -- may precede the one generated from the main unit.
11352 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11353 and then Body_Optional
11357 Load_Parent_Of_Generic
11358 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11359 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11363 -- Establish global variable for sloc adjustment and for error recovery
11364 -- In the case of an instance body for an instantiation with actuals
11365 -- from a limited view, the instance body is placed at the beginning
11366 -- of the enclosing package body: use the body entity as the source
11367 -- location for nodes of the instance body.
11369 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11371 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11372 Body_Id : constant Node_Id :=
11373 Corresponding_Body (Unit_Declaration_Node (Scop));
11376 Instantiation_Node := Body_Id;
11379 Instantiation_Node := Inst_Node;
11382 if Present (Gen_Body_Id) then
11383 Save_Env (Gen_Unit, Act_Decl_Id);
11384 Style_Check := False;
11386 -- If the context of the instance is subject to SPARK_Mode "off", the
11387 -- annotation is missing, or the body is instantiated at a later pass
11388 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11389 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11392 if SPARK_Mode /= On
11393 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11395 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11398 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11399 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11401 Create_Instantiation_Source
11402 (Inst_Node, Gen_Body_Id, S_Adjustment);
11406 (Original_Node (Gen_Body), Empty, Instantiating => True);
11408 -- Create proper (possibly qualified) defining name for the body, to
11409 -- correspond to the one in the spec.
11412 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11413 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11415 -- Some attributes of spec entity are not inherited by body entity
11417 Set_Handler_Records (Act_Body_Id, No_List);
11419 if Nkind (Defining_Unit_Name (Act_Spec)) =
11420 N_Defining_Program_Unit_Name
11423 Make_Defining_Program_Unit_Name (Loc,
11425 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11426 Defining_Identifier => Act_Body_Id);
11428 Act_Body_Name := Act_Body_Id;
11431 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11433 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11434 Check_Generic_Actuals (Act_Decl_Id, False);
11435 Check_Initialized_Types;
11437 -- Install primitives hidden at the point of the instantiation but
11438 -- visible when processing the generic formals
11444 E := First_Entity (Act_Decl_Id);
11445 while Present (E) loop
11447 and then not Is_Itype (E)
11448 and then Is_Generic_Actual_Type (E)
11449 and then Is_Tagged_Type (E)
11451 Install_Hidden_Primitives
11452 (Prims_List => Vis_Prims_List,
11453 Gen_T => Generic_Parent_Type (Parent (E)),
11461 -- If it is a child unit, make the parent instance (which is an
11462 -- instance of the parent of the generic) visible. The parent
11463 -- instance is the prefix of the name of the generic unit.
11465 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11466 and then Nkind (Gen_Id) = N_Expanded_Name
11468 Par_Ent := Entity (Prefix (Gen_Id));
11469 Par_Vis := Is_Immediately_Visible (Par_Ent);
11470 Install_Parent (Par_Ent, In_Body => True);
11471 Parent_Installed := True;
11473 elsif Is_Child_Unit (Gen_Unit) then
11474 Par_Ent := Scope (Gen_Unit);
11475 Par_Vis := Is_Immediately_Visible (Par_Ent);
11476 Install_Parent (Par_Ent, In_Body => True);
11477 Parent_Installed := True;
11480 -- If the instantiation is a library unit, and this is the main unit,
11481 -- then build the resulting compilation unit nodes for the instance.
11482 -- If this is a compilation unit but it is not the main unit, then it
11483 -- is the body of a unit in the context, that is being compiled
11484 -- because it is encloses some inlined unit or another generic unit
11485 -- being instantiated. In that case, this body is not part of the
11486 -- current compilation, and is not attached to the tree, but its
11487 -- parent must be set for analysis.
11489 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11491 -- Replace instance node with body of instance, and create new
11492 -- node for corresponding instance declaration.
11494 Build_Instance_Compilation_Unit_Nodes
11495 (Inst_Node, Act_Body, Act_Decl);
11496 Analyze (Inst_Node);
11498 if Parent (Inst_Node) = Cunit (Main_Unit) then
11500 -- If the instance is a child unit itself, then set the scope
11501 -- of the expanded body to be the parent of the instantiation
11502 -- (ensuring that the fully qualified name will be generated
11503 -- for the elaboration subprogram).
11505 if Nkind (Defining_Unit_Name (Act_Spec)) =
11506 N_Defining_Program_Unit_Name
11508 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11512 -- Case where instantiation is not a library unit
11515 -- If this is an early instantiation, i.e. appears textually
11516 -- before the corresponding body and must be elaborated first,
11517 -- indicate that the body instance is to be delayed.
11519 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11521 -- Now analyze the body. We turn off all checks if this is an
11522 -- internal unit, since there is no reason to have checks on for
11523 -- any predefined run-time library code. All such code is designed
11524 -- to be compiled with checks off.
11526 -- Note that we do NOT apply this criterion to children of GNAT
11527 -- The latter units must suppress checks explicitly if needed.
11529 -- We also do not suppress checks in CodePeer mode where we are
11530 -- interested in finding possible runtime errors.
11532 if not CodePeer_Mode
11533 and then In_Predefined_Unit (Gen_Decl)
11535 Analyze (Act_Body, Suppress => All_Checks);
11537 Analyze (Act_Body);
11541 Inherit_Context (Gen_Body, Inst_Node);
11543 -- Remove the parent instances if they have been placed on the scope
11544 -- stack to compile the body.
11546 if Parent_Installed then
11547 Remove_Parent (In_Body => True);
11549 -- Restore the previous visibility of the parent
11551 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11554 Restore_Hidden_Primitives (Vis_Prims_List);
11555 Restore_Private_Views (Act_Decl_Id);
11557 -- Remove the current unit from visibility if this is an instance
11558 -- that is not elaborated on the fly for inlining purposes.
11560 if not Inlined_Body then
11561 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11566 -- If we have no body, and the unit requires a body, then complain. This
11567 -- complaint is suppressed if we have detected other errors (since a
11568 -- common reason for missing the body is that it had errors).
11569 -- In CodePeer mode, a warning has been emitted already, no need for
11570 -- further messages.
11572 elsif Unit_Requires_Body (Gen_Unit)
11573 and then not Body_Optional
11575 if CodePeer_Mode then
11578 elsif Serious_Errors_Detected = 0 then
11580 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11582 -- Don't attempt to perform any cleanup actions if some other error
11583 -- was already detected, since this can cause blowups.
11589 -- Case of package that does not need a body
11592 -- If the instantiation of the declaration is a library unit, rewrite
11593 -- the original package instantiation as a package declaration in the
11594 -- compilation unit node.
11596 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11597 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11598 Rewrite (Inst_Node, Act_Decl);
11600 -- Generate elaboration entity, in case spec has elaboration code.
11601 -- This cannot be done when the instance is analyzed, because it
11602 -- is not known yet whether the body exists.
11604 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11605 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11607 -- If the instantiation is not a library unit, then append the
11608 -- declaration to the list of implicitly generated entities, unless
11609 -- it is already a list member which means that it was already
11612 elsif not Is_List_Member (Act_Decl) then
11613 Mark_Rewrite_Insertion (Act_Decl);
11614 Insert_Before (Inst_Node, Act_Decl);
11618 Expander_Mode_Restore;
11621 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11622 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11623 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11624 Style_Check := Saved_Style_Check;
11625 end Instantiate_Package_Body;
11627 ---------------------------------
11628 -- Instantiate_Subprogram_Body --
11629 ---------------------------------
11631 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11632 -- must be replaced by gotos which jump to the end of the routine in order
11633 -- to restore the Ghost and SPARK modes.
11635 procedure Instantiate_Subprogram_Body
11636 (Body_Info : Pending_Body_Info;
11637 Body_Optional : Boolean := False)
11639 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11640 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11641 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11642 Gen_Id : constant Node_Id := Name (Inst_Node);
11643 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11644 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11645 Loc : constant Source_Ptr := Sloc (Inst_Node);
11646 Pack_Id : constant Entity_Id :=
11647 Defining_Unit_Name (Parent (Act_Decl));
11649 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11650 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11651 Saved_ISMP : constant Boolean :=
11652 Ignore_SPARK_Mode_Pragmas_In_Instance;
11653 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11654 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11655 -- Save the Ghost and SPARK mode-related data to restore on exit
11657 Saved_Style_Check : constant Boolean := Style_Check;
11658 Saved_Warnings : constant Warning_Record := Save_Warnings;
11660 Act_Body : Node_Id;
11661 Act_Body_Id : Entity_Id;
11662 Gen_Body : Node_Id;
11663 Gen_Body_Id : Node_Id;
11664 Pack_Body : Node_Id;
11665 Par_Ent : Entity_Id := Empty;
11666 Par_Vis : Boolean := False;
11667 Ret_Expr : Node_Id;
11669 Parent_Installed : Boolean := False;
11672 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11674 -- Subprogram body may have been created already because of an inline
11675 -- pragma, or because of multiple elaborations of the enclosing package
11676 -- when several instances of the subprogram appear in the main unit.
11678 if Present (Corresponding_Body (Act_Decl)) then
11682 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11683 -- the mode now to ensure that any nodes generated during instantiation
11684 -- are properly marked as Ghost.
11686 Set_Ghost_Mode (Act_Decl_Id);
11688 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11690 -- Re-establish the state of information on which checks are suppressed.
11691 -- This information was set in Body_Info at the point of instantiation,
11692 -- and now we restore it so that the instance is compiled using the
11693 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11695 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11696 Scope_Suppress := Body_Info.Scope_Suppress;
11697 Opt.Ada_Version := Body_Info.Version;
11698 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11699 Restore_Warnings (Body_Info.Warnings);
11701 -- Install the SPARK mode which applies to the subprogram body from the
11702 -- instantiation context. This may be refined further if an explicit
11703 -- SPARK_Mode pragma applies to the generic body.
11705 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11707 if No (Gen_Body_Id) then
11709 -- For imported generic subprogram, no body to compile, complete
11710 -- the spec entity appropriately.
11712 if Is_Imported (Gen_Unit) then
11713 Set_Is_Imported (Act_Decl_Id);
11714 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11715 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11716 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11717 Set_Has_Completion (Act_Decl_Id);
11720 -- For other cases, compile the body
11723 Load_Parent_Of_Generic
11724 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11725 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11729 Instantiation_Node := Inst_Node;
11731 if Present (Gen_Body_Id) then
11732 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11734 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11736 -- Either body is not present, or context is non-expanding, as
11737 -- when compiling a subunit. Mark the instance as completed, and
11738 -- diagnose a missing body when needed.
11741 and then Operating_Mode = Generate_Code
11743 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11746 Set_Has_Completion (Act_Decl_Id);
11750 Save_Env (Gen_Unit, Act_Decl_Id);
11751 Style_Check := False;
11753 -- If the context of the instance is subject to SPARK_Mode "off", the
11754 -- annotation is missing, or the body is instantiated at a later pass
11755 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11756 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11759 if SPARK_Mode /= On
11760 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11762 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11765 -- If the context of an instance is not subject to SPARK_Mode "off",
11766 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11767 -- the latter should be the one applicable to the instance.
11769 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11770 and then SPARK_Mode /= Off
11771 and then Present (SPARK_Pragma (Gen_Body_Id))
11773 Set_SPARK_Mode (Gen_Body_Id);
11776 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11777 Create_Instantiation_Source
11784 (Original_Node (Gen_Body), Empty, Instantiating => True);
11786 -- Create proper defining name for the body, to correspond to the one
11790 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11792 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11793 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11795 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11796 Set_Has_Completion (Act_Decl_Id);
11797 Check_Generic_Actuals (Pack_Id, False);
11799 -- Generate a reference to link the visible subprogram instance to
11800 -- the generic body, which for navigation purposes is the only
11801 -- available source for the instance.
11804 (Related_Instance (Pack_Id),
11805 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11807 -- If it is a child unit, make the parent instance (which is an
11808 -- instance of the parent of the generic) visible. The parent
11809 -- instance is the prefix of the name of the generic unit.
11811 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11812 and then Nkind (Gen_Id) = N_Expanded_Name
11814 Par_Ent := Entity (Prefix (Gen_Id));
11815 Par_Vis := Is_Immediately_Visible (Par_Ent);
11816 Install_Parent (Par_Ent, In_Body => True);
11817 Parent_Installed := True;
11819 elsif Is_Child_Unit (Gen_Unit) then
11820 Par_Ent := Scope (Gen_Unit);
11821 Par_Vis := Is_Immediately_Visible (Par_Ent);
11822 Install_Parent (Par_Ent, In_Body => True);
11823 Parent_Installed := True;
11826 -- Subprogram body is placed in the body of wrapper package,
11827 -- whose spec contains the subprogram declaration as well as
11828 -- the renaming declarations for the generic parameters.
11831 Make_Package_Body (Loc,
11832 Defining_Unit_Name => New_Copy (Pack_Id),
11833 Declarations => New_List (Act_Body));
11835 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11837 -- If the instantiation is a library unit, then build resulting
11838 -- compilation unit nodes for the instance. The declaration of
11839 -- the enclosing package is the grandparent of the subprogram
11840 -- declaration. First replace the instantiation node as the unit
11841 -- of the corresponding compilation.
11843 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11844 if Parent (Inst_Node) = Cunit (Main_Unit) then
11845 Set_Unit (Parent (Inst_Node), Inst_Node);
11846 Build_Instance_Compilation_Unit_Nodes
11847 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11848 Analyze (Inst_Node);
11850 Set_Parent (Pack_Body, Parent (Inst_Node));
11851 Analyze (Pack_Body);
11855 Insert_Before (Inst_Node, Pack_Body);
11856 Mark_Rewrite_Insertion (Pack_Body);
11857 Analyze (Pack_Body);
11859 if Expander_Active then
11860 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11864 Inherit_Context (Gen_Body, Inst_Node);
11866 Restore_Private_Views (Pack_Id, False);
11868 if Parent_Installed then
11869 Remove_Parent (In_Body => True);
11871 -- Restore the previous visibility of the parent
11873 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11877 Restore_Warnings (Saved_Warnings);
11879 -- Body not found. Error was emitted already. If there were no previous
11880 -- errors, this may be an instance whose scope is a premature instance.
11881 -- In that case we must insure that the (legal) program does raise
11882 -- program error if executed. We generate a subprogram body for this
11883 -- purpose. See DEC ac30vso.
11885 -- Should not reference proprietary DEC tests in comments ???
11887 elsif Serious_Errors_Detected = 0
11888 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11890 if Body_Optional then
11893 elsif Ekind (Act_Decl_Id) = E_Procedure then
11895 Make_Subprogram_Body (Loc,
11897 Make_Procedure_Specification (Loc,
11898 Defining_Unit_Name =>
11899 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11900 Parameter_Specifications =>
11902 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11904 Declarations => Empty_List,
11905 Handled_Statement_Sequence =>
11906 Make_Handled_Sequence_Of_Statements (Loc,
11907 Statements => New_List (
11908 Make_Raise_Program_Error (Loc,
11909 Reason => PE_Access_Before_Elaboration))));
11913 Make_Raise_Program_Error (Loc,
11914 Reason => PE_Access_Before_Elaboration);
11916 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11917 Set_Analyzed (Ret_Expr);
11920 Make_Subprogram_Body (Loc,
11922 Make_Function_Specification (Loc,
11923 Defining_Unit_Name =>
11924 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11925 Parameter_Specifications =>
11927 (Parameter_Specifications (Parent (Act_Decl_Id))),
11928 Result_Definition =>
11929 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11931 Declarations => Empty_List,
11932 Handled_Statement_Sequence =>
11933 Make_Handled_Sequence_Of_Statements (Loc,
11934 Statements => New_List (
11935 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11939 Make_Package_Body (Loc,
11940 Defining_Unit_Name => New_Copy (Pack_Id),
11941 Declarations => New_List (Act_Body));
11943 Insert_After (Inst_Node, Pack_Body);
11944 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11945 Analyze (Pack_Body);
11948 Expander_Mode_Restore;
11951 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11953 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11954 Style_Check := Saved_Style_Check;
11955 end Instantiate_Subprogram_Body;
11957 ----------------------
11958 -- Instantiate_Type --
11959 ----------------------
11961 function Instantiate_Type
11964 Analyzed_Formal : Node_Id;
11965 Actual_Decls : List_Id) return List_Id
11967 A_Gen_T : constant Entity_Id :=
11968 Defining_Identifier (Analyzed_Formal);
11969 Def : constant Node_Id := Formal_Type_Definition (Formal);
11970 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11972 Ancestor : Entity_Id := Empty;
11973 Decl_Node : Node_Id;
11974 Decl_Nodes : List_Id;
11978 procedure Diagnose_Predicated_Actual;
11979 -- There are a number of constructs in which a discrete type with
11980 -- predicates is illegal, e.g. as an index in an array type declaration.
11981 -- If a generic type is used is such a construct in a generic package
11982 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11983 -- of the generic contract that the actual cannot have predicates.
11985 procedure Validate_Array_Type_Instance;
11986 procedure Validate_Access_Subprogram_Instance;
11987 procedure Validate_Access_Type_Instance;
11988 procedure Validate_Derived_Type_Instance;
11989 procedure Validate_Derived_Interface_Type_Instance;
11990 procedure Validate_Discriminated_Formal_Type;
11991 procedure Validate_Interface_Type_Instance;
11992 procedure Validate_Private_Type_Instance;
11993 procedure Validate_Incomplete_Type_Instance;
11994 -- These procedures perform validation tests for the named case.
11995 -- Validate_Discriminated_Formal_Type is shared by formal private
11996 -- types and Ada 2012 formal incomplete types.
11998 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11999 -- Check that base types are the same and that the subtypes match
12000 -- statically. Used in several of the above.
12002 ---------------------------------
12003 -- Diagnose_Predicated_Actual --
12004 ---------------------------------
12006 procedure Diagnose_Predicated_Actual is
12008 if No_Predicate_On_Actual (A_Gen_T)
12009 and then Has_Predicates (Act_T)
12012 ("actual for& cannot be a type with predicate",
12013 Instantiation_Node, A_Gen_T);
12015 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12016 and then Has_Predicates (Act_T)
12017 and then not Has_Static_Predicate_Aspect (Act_T)
12020 ("actual for& cannot be a type with a dynamic predicate",
12021 Instantiation_Node, A_Gen_T);
12023 end Diagnose_Predicated_Actual;
12025 --------------------
12026 -- Subtypes_Match --
12027 --------------------
12029 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12030 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12033 -- Some detailed comments would be useful here ???
12035 return ((Base_Type (T) = Act_T
12036 or else Base_Type (T) = Base_Type (Act_T))
12037 and then Subtypes_Statically_Match (T, Act_T))
12039 or else (Is_Class_Wide_Type (Gen_T)
12040 and then Is_Class_Wide_Type (Act_T)
12041 and then Subtypes_Match
12042 (Get_Instance_Of (Root_Type (Gen_T)),
12043 Root_Type (Act_T)))
12046 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12047 E_Anonymous_Access_Type)
12048 and then Ekind (Act_T) = Ekind (Gen_T)
12049 and then Subtypes_Statically_Match
12050 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12051 end Subtypes_Match;
12053 -----------------------------------------
12054 -- Validate_Access_Subprogram_Instance --
12055 -----------------------------------------
12057 procedure Validate_Access_Subprogram_Instance is
12059 if not Is_Access_Type (Act_T)
12060 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12063 ("expect access type in instantiation of &", Actual, Gen_T);
12064 Abandon_Instantiation (Actual);
12067 -- According to AI05-288, actuals for access_to_subprograms must be
12068 -- subtype conformant with the generic formal. Previous to AI05-288
12069 -- only mode conformance was required.
12071 -- This is a binding interpretation that applies to previous versions
12072 -- of the language, no need to maintain previous weaker checks.
12074 Check_Subtype_Conformant
12075 (Designated_Type (Act_T),
12076 Designated_Type (A_Gen_T),
12080 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12081 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12083 ("protected access type not allowed for formal &",
12087 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12089 ("expect protected access type for formal &",
12093 -- If the formal has a specified convention (which in most cases
12094 -- will be StdCall) verify that the actual has the same convention.
12096 if Has_Convention_Pragma (A_Gen_T)
12097 and then Convention (A_Gen_T) /= Convention (Act_T)
12099 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12101 ("actual for formal & must have convention %", Actual, Gen_T);
12103 end Validate_Access_Subprogram_Instance;
12105 -----------------------------------
12106 -- Validate_Access_Type_Instance --
12107 -----------------------------------
12109 procedure Validate_Access_Type_Instance is
12110 Desig_Type : constant Entity_Id :=
12111 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12112 Desig_Act : Entity_Id;
12115 if not Is_Access_Type (Act_T) then
12117 ("expect access type in instantiation of &", Actual, Gen_T);
12118 Abandon_Instantiation (Actual);
12121 if Is_Access_Constant (A_Gen_T) then
12122 if not Is_Access_Constant (Act_T) then
12124 ("actual type must be access-to-constant type", Actual);
12125 Abandon_Instantiation (Actual);
12128 if Is_Access_Constant (Act_T) then
12130 ("actual type must be access-to-variable type", Actual);
12131 Abandon_Instantiation (Actual);
12133 elsif Ekind (A_Gen_T) = E_General_Access_Type
12134 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12136 Error_Msg_N -- CODEFIX
12137 ("actual must be general access type!", Actual);
12138 Error_Msg_NE -- CODEFIX
12139 ("add ALL to }!", Actual, Act_T);
12140 Abandon_Instantiation (Actual);
12144 -- The designated subtypes, that is to say the subtypes introduced
12145 -- by an access type declaration (and not by a subtype declaration)
12148 Desig_Act := Designated_Type (Base_Type (Act_T));
12150 -- The designated type may have been introduced through a limited_
12151 -- with clause, in which case retrieve the non-limited view. This
12152 -- applies to incomplete types as well as to class-wide types.
12154 if From_Limited_With (Desig_Act) then
12155 Desig_Act := Available_View (Desig_Act);
12158 if not Subtypes_Match (Desig_Type, Desig_Act) then
12160 ("designated type of actual does not match that of formal &",
12163 if not Predicates_Match (Desig_Type, Desig_Act) then
12164 Error_Msg_N ("\predicates do not match", Actual);
12167 Abandon_Instantiation (Actual);
12169 elsif Is_Access_Type (Designated_Type (Act_T))
12170 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12172 Is_Constrained (Designated_Type (Desig_Type))
12175 ("designated type of actual does not match that of formal &",
12178 if not Predicates_Match (Desig_Type, Desig_Act) then
12179 Error_Msg_N ("\predicates do not match", Actual);
12182 Abandon_Instantiation (Actual);
12185 -- Ada 2005: null-exclusion indicators of the two types must agree
12187 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12189 ("non null exclusion of actual and formal & do not match",
12192 end Validate_Access_Type_Instance;
12194 ----------------------------------
12195 -- Validate_Array_Type_Instance --
12196 ----------------------------------
12198 procedure Validate_Array_Type_Instance is
12203 function Formal_Dimensions return Nat;
12204 -- Count number of dimensions in array type formal
12206 -----------------------
12207 -- Formal_Dimensions --
12208 -----------------------
12210 function Formal_Dimensions return Nat is
12215 if Nkind (Def) = N_Constrained_Array_Definition then
12216 Index := First (Discrete_Subtype_Definitions (Def));
12218 Index := First (Subtype_Marks (Def));
12221 while Present (Index) loop
12223 Next_Index (Index);
12227 end Formal_Dimensions;
12229 -- Start of processing for Validate_Array_Type_Instance
12232 if not Is_Array_Type (Act_T) then
12234 ("expect array type in instantiation of &", Actual, Gen_T);
12235 Abandon_Instantiation (Actual);
12237 elsif Nkind (Def) = N_Constrained_Array_Definition then
12238 if not (Is_Constrained (Act_T)) then
12240 ("expect constrained array in instantiation of &",
12242 Abandon_Instantiation (Actual);
12246 if Is_Constrained (Act_T) then
12248 ("expect unconstrained array in instantiation of &",
12250 Abandon_Instantiation (Actual);
12254 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12256 ("dimensions of actual do not match formal &", Actual, Gen_T);
12257 Abandon_Instantiation (Actual);
12260 I1 := First_Index (A_Gen_T);
12261 I2 := First_Index (Act_T);
12262 for J in 1 .. Formal_Dimensions loop
12264 -- If the indexes of the actual were given by a subtype_mark,
12265 -- the index was transformed into a range attribute. Retrieve
12266 -- the original type mark for checking.
12268 if Is_Entity_Name (Original_Node (I2)) then
12269 T2 := Entity (Original_Node (I2));
12274 if not Subtypes_Match
12275 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12278 ("index types of actual do not match those of formal &",
12280 Abandon_Instantiation (Actual);
12287 -- Check matching subtypes. Note that there are complex visibility
12288 -- issues when the generic is a child unit and some aspect of the
12289 -- generic type is declared in a parent unit of the generic. We do
12290 -- the test to handle this special case only after a direct check
12291 -- for static matching has failed. The case where both the component
12292 -- type and the array type are separate formals, and the component
12293 -- type is a private view may also require special checking in
12294 -- Subtypes_Match. Finally, we assume that a child instance where
12295 -- the component type comes from a formal of a parent instance is
12296 -- correct because the generic was correct. A more precise check
12297 -- seems too complex to install???
12300 (Component_Type (A_Gen_T), Component_Type (Act_T))
12303 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12304 Component_Type (Act_T))
12306 (not Inside_A_Generic
12307 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12312 ("component subtype of actual does not match that of formal &",
12314 Abandon_Instantiation (Actual);
12317 if Has_Aliased_Components (A_Gen_T)
12318 and then not Has_Aliased_Components (Act_T)
12321 ("actual must have aliased components to match formal type &",
12324 end Validate_Array_Type_Instance;
12326 -----------------------------------------------
12327 -- Validate_Derived_Interface_Type_Instance --
12328 -----------------------------------------------
12330 procedure Validate_Derived_Interface_Type_Instance is
12331 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12335 -- First apply interface instance checks
12337 Validate_Interface_Type_Instance;
12339 -- Verify that immediate parent interface is an ancestor of
12343 and then not Interface_Present_In_Ancestor (Act_T, Par)
12346 ("interface actual must include progenitor&", Actual, Par);
12349 -- Now verify that the actual includes all other ancestors of
12352 Elmt := First_Elmt (Interfaces (A_Gen_T));
12353 while Present (Elmt) loop
12354 if not Interface_Present_In_Ancestor
12355 (Act_T, Get_Instance_Of (Node (Elmt)))
12358 ("interface actual must include progenitor&",
12359 Actual, Node (Elmt));
12364 end Validate_Derived_Interface_Type_Instance;
12366 ------------------------------------
12367 -- Validate_Derived_Type_Instance --
12368 ------------------------------------
12370 procedure Validate_Derived_Type_Instance is
12371 Actual_Discr : Entity_Id;
12372 Ancestor_Discr : Entity_Id;
12375 -- Verify that the actual includes the progenitors of the formal,
12376 -- if any. The formal may depend on previous formals and their
12377 -- instance, so we must examine instance of interfaces if present.
12378 -- The actual may be an extension of an interface, in which case
12379 -- it does not appear in the interface list, so this must be
12380 -- checked separately.
12382 if Present (Interface_List (Def)) then
12383 if not Has_Interfaces (Act_T) then
12385 ("actual must implement all interfaces of formal&",
12390 Act_Iface_List : Elist_Id;
12392 Iface_Ent : Entity_Id;
12394 function Instance_Exists (I : Entity_Id) return Boolean;
12395 -- If the interface entity is declared in a generic unit,
12396 -- this can only be legal if we are within an instantiation
12397 -- of a child of that generic. There is currently no
12398 -- mechanism to relate an interface declared within a
12399 -- generic to the corresponding interface in an instance,
12400 -- so we traverse the list of interfaces of the actual,
12401 -- looking for a name match.
12403 ---------------------
12404 -- Instance_Exists --
12405 ---------------------
12407 function Instance_Exists (I : Entity_Id) return Boolean is
12408 Iface_Elmt : Elmt_Id;
12411 Iface_Elmt := First_Elmt (Act_Iface_List);
12412 while Present (Iface_Elmt) loop
12413 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12414 and then Chars (Node (Iface_Elmt)) = Chars (I)
12419 Next_Elmt (Iface_Elmt);
12423 end Instance_Exists;
12426 Iface := First (Abstract_Interface_List (A_Gen_T));
12427 Collect_Interfaces (Act_T, Act_Iface_List);
12429 while Present (Iface) loop
12430 Iface_Ent := Get_Instance_Of (Entity (Iface));
12432 if Is_Ancestor (Iface_Ent, Act_T)
12433 or else Is_Progenitor (Iface_Ent, Act_T)
12437 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12438 and then Instance_Exists (Iface_Ent)
12443 Error_Msg_Name_1 := Chars (Act_T);
12445 ("Actual% must implement interface&",
12446 Actual, Etype (Iface));
12455 -- If the parent type in the generic declaration is itself a previous
12456 -- formal type, then it is local to the generic and absent from the
12457 -- analyzed generic definition. In that case the ancestor is the
12458 -- instance of the formal (which must have been instantiated
12459 -- previously), unless the ancestor is itself a formal derived type.
12460 -- In this latter case (which is the subject of Corrigendum 8652/0038
12461 -- (AI-202) the ancestor of the formals is the ancestor of its
12462 -- parent. Otherwise, the analyzed generic carries the parent type.
12463 -- If the parent type is defined in a previous formal package, then
12464 -- the scope of that formal package is that of the generic type
12465 -- itself, and it has already been mapped into the corresponding type
12466 -- in the actual package.
12468 -- Common case: parent type defined outside of the generic
12470 if Is_Entity_Name (Subtype_Mark (Def))
12471 and then Present (Entity (Subtype_Mark (Def)))
12473 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12475 -- Check whether parent is defined in a previous formal package
12478 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12481 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12483 -- The type may be a local derivation, or a type extension of a
12484 -- previous formal, or of a formal of a parent package.
12486 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12488 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12490 -- Check whether the parent is another derived formal type in the
12491 -- same generic unit.
12493 if Etype (A_Gen_T) /= A_Gen_T
12494 and then Is_Generic_Type (Etype (A_Gen_T))
12495 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12496 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12498 -- Locate ancestor of parent from the subtype declaration
12499 -- created for the actual.
12505 Decl := First (Actual_Decls);
12506 while Present (Decl) loop
12507 if Nkind (Decl) = N_Subtype_Declaration
12508 and then Chars (Defining_Identifier (Decl)) =
12509 Chars (Etype (A_Gen_T))
12511 Ancestor := Generic_Parent_Type (Decl);
12519 pragma Assert (Present (Ancestor));
12521 -- The ancestor itself may be a previous formal that has been
12524 Ancestor := Get_Instance_Of (Ancestor);
12528 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12531 -- Check whether parent is a previous formal of the current generic
12533 elsif Is_Derived_Type (A_Gen_T)
12534 and then Is_Generic_Type (Etype (A_Gen_T))
12535 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12537 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12539 -- An unusual case: the actual is a type declared in a parent unit,
12540 -- but is not a formal type so there is no instance_of for it.
12541 -- Retrieve it by analyzing the record extension.
12543 elsif Is_Child_Unit (Scope (A_Gen_T))
12544 and then In_Open_Scopes (Scope (Act_T))
12545 and then Is_Generic_Instance (Scope (Act_T))
12547 Analyze (Subtype_Mark (Def));
12548 Ancestor := Entity (Subtype_Mark (Def));
12551 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12554 -- If the formal derived type has pragma Preelaborable_Initialization
12555 -- then the actual type must have preelaborable initialization.
12557 if Known_To_Have_Preelab_Init (A_Gen_T)
12558 and then not Has_Preelaborable_Initialization (Act_T)
12561 ("actual for & must have preelaborable initialization",
12565 -- Ada 2005 (AI-251)
12567 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12568 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12570 ("(Ada 2005) expected type implementing & in instantiation",
12574 -- Finally verify that the (instance of) the ancestor is an ancestor
12577 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12579 ("expect type derived from & in instantiation",
12580 Actual, First_Subtype (Ancestor));
12581 Abandon_Instantiation (Actual);
12584 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12585 -- that the formal type declaration has been rewritten as a private
12588 if Ada_Version >= Ada_2005
12589 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12590 and then Synchronized_Present (Parent (A_Gen_T))
12592 -- The actual must be a synchronized tagged type
12594 if not Is_Tagged_Type (Act_T) then
12596 ("actual of synchronized type must be tagged", Actual);
12597 Abandon_Instantiation (Actual);
12599 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12600 and then Nkind (Type_Definition (Parent (Act_T))) =
12601 N_Derived_Type_Definition
12602 and then not Synchronized_Present
12603 (Type_Definition (Parent (Act_T)))
12606 ("actual of synchronized type must be synchronized", Actual);
12607 Abandon_Instantiation (Actual);
12611 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12612 -- removes the second instance of the phrase "or allow pass by copy".
12614 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12616 ("cannot have atomic actual type for non-atomic formal type",
12619 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12621 ("cannot have volatile actual type for non-volatile formal type",
12625 -- It should not be necessary to check for unknown discriminants on
12626 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12627 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12628 -- needs fixing. ???
12630 if Is_Definite_Subtype (A_Gen_T)
12631 and then not Unknown_Discriminants_Present (Formal)
12632 and then not Is_Definite_Subtype (Act_T)
12634 Error_Msg_N ("actual subtype must be constrained", Actual);
12635 Abandon_Instantiation (Actual);
12638 if not Unknown_Discriminants_Present (Formal) then
12639 if Is_Constrained (Ancestor) then
12640 if not Is_Constrained (Act_T) then
12641 Error_Msg_N ("actual subtype must be constrained", Actual);
12642 Abandon_Instantiation (Actual);
12645 -- Ancestor is unconstrained, Check if generic formal and actual
12646 -- agree on constrainedness. The check only applies to array types
12647 -- and discriminated types.
12649 elsif Is_Constrained (Act_T) then
12650 if Ekind (Ancestor) = E_Access_Type
12651 or else (not Is_Constrained (A_Gen_T)
12652 and then Is_Composite_Type (A_Gen_T))
12654 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12655 Abandon_Instantiation (Actual);
12658 -- A class-wide type is only allowed if the formal has unknown
12661 elsif Is_Class_Wide_Type (Act_T)
12662 and then not Has_Unknown_Discriminants (Ancestor)
12665 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12666 Abandon_Instantiation (Actual);
12668 -- Otherwise, the formal and actual must have the same number
12669 -- of discriminants and each discriminant of the actual must
12670 -- correspond to a discriminant of the formal.
12672 elsif Has_Discriminants (Act_T)
12673 and then not Has_Unknown_Discriminants (Act_T)
12674 and then Has_Discriminants (Ancestor)
12676 Actual_Discr := First_Discriminant (Act_T);
12677 Ancestor_Discr := First_Discriminant (Ancestor);
12678 while Present (Actual_Discr)
12679 and then Present (Ancestor_Discr)
12681 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12682 No (Corresponding_Discriminant (Actual_Discr))
12685 ("discriminant & does not correspond "
12686 & "to ancestor discriminant", Actual, Actual_Discr);
12687 Abandon_Instantiation (Actual);
12690 Next_Discriminant (Actual_Discr);
12691 Next_Discriminant (Ancestor_Discr);
12694 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12696 ("actual for & must have same number of discriminants",
12698 Abandon_Instantiation (Actual);
12701 -- This case should be caught by the earlier check for
12702 -- constrainedness, but the check here is added for completeness.
12704 elsif Has_Discriminants (Act_T)
12705 and then not Has_Unknown_Discriminants (Act_T)
12708 ("actual for & must not have discriminants", Actual, Gen_T);
12709 Abandon_Instantiation (Actual);
12711 elsif Has_Discriminants (Ancestor) then
12713 ("actual for & must have known discriminants", Actual, Gen_T);
12714 Abandon_Instantiation (Actual);
12717 if not Subtypes_Statically_Compatible
12718 (Act_T, Ancestor, Formal_Derived_Matching => True)
12721 ("constraint on actual is incompatible with formal", Actual);
12722 Abandon_Instantiation (Actual);
12726 -- If the formal and actual types are abstract, check that there
12727 -- are no abstract primitives of the actual type that correspond to
12728 -- nonabstract primitives of the formal type (second sentence of
12731 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12732 Check_Abstract_Primitives : declare
12733 Gen_Prims : constant Elist_Id :=
12734 Primitive_Operations (A_Gen_T);
12735 Gen_Elmt : Elmt_Id;
12736 Gen_Subp : Entity_Id;
12737 Anc_Subp : Entity_Id;
12738 Anc_Formal : Entity_Id;
12739 Anc_F_Type : Entity_Id;
12741 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12742 Act_Elmt : Elmt_Id;
12743 Act_Subp : Entity_Id;
12744 Act_Formal : Entity_Id;
12745 Act_F_Type : Entity_Id;
12747 Subprograms_Correspond : Boolean;
12749 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12750 -- Returns true if T2 is derived directly or indirectly from
12751 -- T1, including derivations from interfaces. T1 and T2 are
12752 -- required to be specific tagged base types.
12754 ------------------------
12755 -- Is_Tagged_Ancestor --
12756 ------------------------
12758 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12760 Intfc_Elmt : Elmt_Id;
12763 -- The predicate is satisfied if the types are the same
12768 -- If we've reached the top of the derivation chain then
12769 -- we know that T1 is not an ancestor of T2.
12771 elsif Etype (T2) = T2 then
12774 -- Proceed to check T2's immediate parent
12776 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12779 -- Finally, check to see if T1 is an ancestor of any of T2's
12783 Intfc_Elmt := First_Elmt (Interfaces (T2));
12784 while Present (Intfc_Elmt) loop
12785 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12789 Next_Elmt (Intfc_Elmt);
12794 end Is_Tagged_Ancestor;
12796 -- Start of processing for Check_Abstract_Primitives
12799 -- Loop over all of the formal derived type's primitives
12801 Gen_Elmt := First_Elmt (Gen_Prims);
12802 while Present (Gen_Elmt) loop
12803 Gen_Subp := Node (Gen_Elmt);
12805 -- If the primitive of the formal is not abstract, then
12806 -- determine whether there is a corresponding primitive of
12807 -- the actual type that's abstract.
12809 if not Is_Abstract_Subprogram (Gen_Subp) then
12810 Act_Elmt := First_Elmt (Act_Prims);
12811 while Present (Act_Elmt) loop
12812 Act_Subp := Node (Act_Elmt);
12814 -- If we find an abstract primitive of the actual,
12815 -- then we need to test whether it corresponds to the
12816 -- subprogram from which the generic formal primitive
12819 if Is_Abstract_Subprogram (Act_Subp) then
12820 Anc_Subp := Alias (Gen_Subp);
12822 -- Test whether we have a corresponding primitive
12823 -- by comparing names, kinds, formal types, and
12826 if Chars (Anc_Subp) = Chars (Act_Subp)
12827 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12829 Anc_Formal := First_Formal (Anc_Subp);
12830 Act_Formal := First_Formal (Act_Subp);
12831 while Present (Anc_Formal)
12832 and then Present (Act_Formal)
12834 Anc_F_Type := Etype (Anc_Formal);
12835 Act_F_Type := Etype (Act_Formal);
12837 if Ekind (Anc_F_Type) =
12838 E_Anonymous_Access_Type
12840 Anc_F_Type := Designated_Type (Anc_F_Type);
12842 if Ekind (Act_F_Type) =
12843 E_Anonymous_Access_Type
12846 Designated_Type (Act_F_Type);
12852 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12857 Anc_F_Type := Base_Type (Anc_F_Type);
12858 Act_F_Type := Base_Type (Act_F_Type);
12860 -- If the formal is controlling, then the
12861 -- the type of the actual primitive's formal
12862 -- must be derived directly or indirectly
12863 -- from the type of the ancestor primitive's
12866 if Is_Controlling_Formal (Anc_Formal) then
12867 if not Is_Tagged_Ancestor
12868 (Anc_F_Type, Act_F_Type)
12873 -- Otherwise the types of the formals must
12876 elsif Anc_F_Type /= Act_F_Type then
12880 Next_Entity (Anc_Formal);
12881 Next_Entity (Act_Formal);
12884 -- If we traversed through all of the formals
12885 -- then so far the subprograms correspond, so
12886 -- now check that any result types correspond.
12888 if No (Anc_Formal) and then No (Act_Formal) then
12889 Subprograms_Correspond := True;
12891 if Ekind (Act_Subp) = E_Function then
12892 Anc_F_Type := Etype (Anc_Subp);
12893 Act_F_Type := Etype (Act_Subp);
12895 if Ekind (Anc_F_Type) =
12896 E_Anonymous_Access_Type
12899 Designated_Type (Anc_F_Type);
12901 if Ekind (Act_F_Type) =
12902 E_Anonymous_Access_Type
12905 Designated_Type (Act_F_Type);
12907 Subprograms_Correspond := False;
12912 = E_Anonymous_Access_Type
12914 Subprograms_Correspond := False;
12917 Anc_F_Type := Base_Type (Anc_F_Type);
12918 Act_F_Type := Base_Type (Act_F_Type);
12920 -- Now either the result types must be
12921 -- the same or, if the result type is
12922 -- controlling, the result type of the
12923 -- actual primitive must descend from the
12924 -- result type of the ancestor primitive.
12926 if Subprograms_Correspond
12927 and then Anc_F_Type /= Act_F_Type
12929 Has_Controlling_Result (Anc_Subp)
12930 and then not Is_Tagged_Ancestor
12931 (Anc_F_Type, Act_F_Type)
12933 Subprograms_Correspond := False;
12937 -- Found a matching subprogram belonging to
12938 -- formal ancestor type, so actual subprogram
12939 -- corresponds and this violates 3.9.3(9).
12941 if Subprograms_Correspond then
12943 ("abstract subprogram & overrides "
12944 & "nonabstract subprogram of ancestor",
12951 Next_Elmt (Act_Elmt);
12955 Next_Elmt (Gen_Elmt);
12957 end Check_Abstract_Primitives;
12960 -- Verify that limitedness matches. If parent is a limited
12961 -- interface then the generic formal is not unless declared
12962 -- explicitly so. If not declared limited, the actual cannot be
12963 -- limited (see AI05-0087).
12965 -- Even though this AI is a binding interpretation, we enable the
12966 -- check only in Ada 2012 mode, because this improper construct
12967 -- shows up in user code and in existing B-tests.
12969 if Is_Limited_Type (Act_T)
12970 and then not Is_Limited_Type (A_Gen_T)
12971 and then Ada_Version >= Ada_2012
12973 if In_Instance then
12977 ("actual for non-limited & cannot be a limited type",
12979 Explain_Limited_Type (Act_T, Actual);
12980 Abandon_Instantiation (Actual);
12983 end Validate_Derived_Type_Instance;
12985 ----------------------------------------
12986 -- Validate_Discriminated_Formal_Type --
12987 ----------------------------------------
12989 procedure Validate_Discriminated_Formal_Type is
12990 Formal_Discr : Entity_Id;
12991 Actual_Discr : Entity_Id;
12992 Formal_Subt : Entity_Id;
12995 if Has_Discriminants (A_Gen_T) then
12996 if not Has_Discriminants (Act_T) then
12998 ("actual for & must have discriminants", Actual, Gen_T);
12999 Abandon_Instantiation (Actual);
13001 elsif Is_Constrained (Act_T) then
13003 ("actual for & must be unconstrained", Actual, Gen_T);
13004 Abandon_Instantiation (Actual);
13007 Formal_Discr := First_Discriminant (A_Gen_T);
13008 Actual_Discr := First_Discriminant (Act_T);
13009 while Formal_Discr /= Empty loop
13010 if Actual_Discr = Empty then
13012 ("discriminants on actual do not match formal",
13014 Abandon_Instantiation (Actual);
13017 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13019 -- Access discriminants match if designated types do
13021 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13022 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13023 E_Anonymous_Access_Type
13026 (Designated_Type (Base_Type (Formal_Subt))) =
13027 Designated_Type (Base_Type (Etype (Actual_Discr)))
13031 elsif Base_Type (Formal_Subt) /=
13032 Base_Type (Etype (Actual_Discr))
13035 ("types of actual discriminants must match formal",
13037 Abandon_Instantiation (Actual);
13039 elsif not Subtypes_Statically_Match
13040 (Formal_Subt, Etype (Actual_Discr))
13041 and then Ada_Version >= Ada_95
13044 ("subtypes of actual discriminants must match formal",
13046 Abandon_Instantiation (Actual);
13049 Next_Discriminant (Formal_Discr);
13050 Next_Discriminant (Actual_Discr);
13053 if Actual_Discr /= Empty then
13055 ("discriminants on actual do not match formal",
13057 Abandon_Instantiation (Actual);
13061 end Validate_Discriminated_Formal_Type;
13063 ---------------------------------------
13064 -- Validate_Incomplete_Type_Instance --
13065 ---------------------------------------
13067 procedure Validate_Incomplete_Type_Instance is
13069 if not Is_Tagged_Type (Act_T)
13070 and then Is_Tagged_Type (A_Gen_T)
13073 ("actual for & must be a tagged type", Actual, Gen_T);
13076 Validate_Discriminated_Formal_Type;
13077 end Validate_Incomplete_Type_Instance;
13079 --------------------------------------
13080 -- Validate_Interface_Type_Instance --
13081 --------------------------------------
13083 procedure Validate_Interface_Type_Instance is
13085 if not Is_Interface (Act_T) then
13087 ("actual for formal interface type must be an interface",
13090 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13091 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13092 or else Is_Protected_Interface (A_Gen_T) /=
13093 Is_Protected_Interface (Act_T)
13094 or else Is_Synchronized_Interface (A_Gen_T) /=
13095 Is_Synchronized_Interface (Act_T)
13098 ("actual for interface& does not match (RM 12.5.5(4))",
13101 end Validate_Interface_Type_Instance;
13103 ------------------------------------
13104 -- Validate_Private_Type_Instance --
13105 ------------------------------------
13107 procedure Validate_Private_Type_Instance is
13109 if Is_Limited_Type (Act_T)
13110 and then not Is_Limited_Type (A_Gen_T)
13112 if In_Instance then
13116 ("actual for non-limited & cannot be a limited type", Actual,
13118 Explain_Limited_Type (Act_T, Actual);
13119 Abandon_Instantiation (Actual);
13122 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13123 and then not Has_Preelaborable_Initialization (Act_T)
13126 ("actual for & must have preelaborable initialization", Actual,
13129 elsif not Is_Definite_Subtype (Act_T)
13130 and then Is_Definite_Subtype (A_Gen_T)
13131 and then Ada_Version >= Ada_95
13134 ("actual for & must be a definite subtype", Actual, Gen_T);
13136 elsif not Is_Tagged_Type (Act_T)
13137 and then Is_Tagged_Type (A_Gen_T)
13140 ("actual for & must be a tagged type", Actual, Gen_T);
13143 Validate_Discriminated_Formal_Type;
13145 end Validate_Private_Type_Instance;
13147 -- Start of processing for Instantiate_Type
13150 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13151 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13152 return New_List (Error);
13154 elsif not Is_Entity_Name (Actual)
13155 or else not Is_Type (Entity (Actual))
13158 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13159 Abandon_Instantiation (Actual);
13162 Act_T := Entity (Actual);
13164 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13165 -- as a generic actual parameter if the corresponding formal type
13166 -- does not have a known_discriminant_part, or is a formal derived
13167 -- type that is an Unchecked_Union type.
13169 if Is_Unchecked_Union (Base_Type (Act_T)) then
13170 if not Has_Discriminants (A_Gen_T)
13171 or else (Is_Derived_Type (A_Gen_T)
13172 and then Is_Unchecked_Union (A_Gen_T))
13176 Error_Msg_N ("unchecked union cannot be the actual for a "
13177 & "discriminated formal type", Act_T);
13182 -- Deal with fixed/floating restrictions
13184 if Is_Floating_Point_Type (Act_T) then
13185 Check_Restriction (No_Floating_Point, Actual);
13186 elsif Is_Fixed_Point_Type (Act_T) then
13187 Check_Restriction (No_Fixed_Point, Actual);
13190 -- Deal with error of using incomplete type as generic actual.
13191 -- This includes limited views of a type, even if the non-limited
13192 -- view may be available.
13194 if Ekind (Act_T) = E_Incomplete_Type
13195 or else (Is_Class_Wide_Type (Act_T)
13196 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13198 -- If the formal is an incomplete type, the actual can be
13199 -- incomplete as well.
13201 if Ekind (A_Gen_T) = E_Incomplete_Type then
13204 elsif Is_Class_Wide_Type (Act_T)
13205 or else No (Full_View (Act_T))
13207 Error_Msg_N ("premature use of incomplete type", Actual);
13208 Abandon_Instantiation (Actual);
13210 Act_T := Full_View (Act_T);
13211 Set_Entity (Actual, Act_T);
13213 if Has_Private_Component (Act_T) then
13215 ("premature use of type with private component", Actual);
13219 -- Deal with error of premature use of private type as generic actual
13221 elsif Is_Private_Type (Act_T)
13222 and then Is_Private_Type (Base_Type (Act_T))
13223 and then not Is_Generic_Type (Act_T)
13224 and then not Is_Derived_Type (Act_T)
13225 and then No (Full_View (Root_Type (Act_T)))
13227 -- If the formal is an incomplete type, the actual can be
13228 -- private or incomplete as well.
13230 if Ekind (A_Gen_T) = E_Incomplete_Type then
13233 Error_Msg_N ("premature use of private type", Actual);
13236 elsif Has_Private_Component (Act_T) then
13238 ("premature use of type with private component", Actual);
13241 Set_Instance_Of (A_Gen_T, Act_T);
13243 -- If the type is generic, the class-wide type may also be used
13245 if Is_Tagged_Type (A_Gen_T)
13246 and then Is_Tagged_Type (Act_T)
13247 and then not Is_Class_Wide_Type (A_Gen_T)
13249 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13250 Class_Wide_Type (Act_T));
13253 if not Is_Abstract_Type (A_Gen_T)
13254 and then Is_Abstract_Type (Act_T)
13257 ("actual of non-abstract formal cannot be abstract", Actual);
13260 -- A generic scalar type is a first subtype for which we generate
13261 -- an anonymous base type. Indicate that the instance of this base
13262 -- is the base type of the actual.
13264 if Is_Scalar_Type (A_Gen_T) then
13265 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13269 if Error_Posted (Act_T) then
13272 case Nkind (Def) is
13273 when N_Formal_Private_Type_Definition =>
13274 Validate_Private_Type_Instance;
13276 when N_Formal_Incomplete_Type_Definition =>
13277 Validate_Incomplete_Type_Instance;
13279 when N_Formal_Derived_Type_Definition =>
13280 Validate_Derived_Type_Instance;
13282 when N_Formal_Discrete_Type_Definition =>
13283 if not Is_Discrete_Type (Act_T) then
13285 ("expect discrete type in instantiation of&",
13287 Abandon_Instantiation (Actual);
13290 Diagnose_Predicated_Actual;
13292 when N_Formal_Signed_Integer_Type_Definition =>
13293 if not Is_Signed_Integer_Type (Act_T) then
13295 ("expect signed integer type in instantiation of&",
13297 Abandon_Instantiation (Actual);
13300 Diagnose_Predicated_Actual;
13302 when N_Formal_Modular_Type_Definition =>
13303 if not Is_Modular_Integer_Type (Act_T) then
13305 ("expect modular type in instantiation of &",
13307 Abandon_Instantiation (Actual);
13310 Diagnose_Predicated_Actual;
13312 when N_Formal_Floating_Point_Definition =>
13313 if not Is_Floating_Point_Type (Act_T) then
13315 ("expect float type in instantiation of &", Actual, Gen_T);
13316 Abandon_Instantiation (Actual);
13319 when N_Formal_Ordinary_Fixed_Point_Definition =>
13320 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13322 ("expect ordinary fixed point type in instantiation of &",
13324 Abandon_Instantiation (Actual);
13327 when N_Formal_Decimal_Fixed_Point_Definition =>
13328 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13330 ("expect decimal type in instantiation of &",
13332 Abandon_Instantiation (Actual);
13335 when N_Array_Type_Definition =>
13336 Validate_Array_Type_Instance;
13338 when N_Access_To_Object_Definition =>
13339 Validate_Access_Type_Instance;
13341 when N_Access_Function_Definition
13342 | N_Access_Procedure_Definition
13344 Validate_Access_Subprogram_Instance;
13346 when N_Record_Definition =>
13347 Validate_Interface_Type_Instance;
13349 when N_Derived_Type_Definition =>
13350 Validate_Derived_Interface_Type_Instance;
13353 raise Program_Error;
13357 Subt := New_Copy (Gen_T);
13359 -- Use adjusted sloc of subtype name as the location for other nodes in
13360 -- the subtype declaration.
13362 Loc := Sloc (Subt);
13365 Make_Subtype_Declaration (Loc,
13366 Defining_Identifier => Subt,
13367 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13369 if Is_Private_Type (Act_T) then
13370 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13372 elsif Is_Access_Type (Act_T)
13373 and then Is_Private_Type (Designated_Type (Act_T))
13375 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13378 -- In Ada 2012 the actual may be a limited view. Indicate that
13379 -- the local subtype must be treated as such.
13381 if From_Limited_With (Act_T) then
13382 Set_Ekind (Subt, E_Incomplete_Subtype);
13383 Set_From_Limited_With (Subt);
13386 Decl_Nodes := New_List (Decl_Node);
13388 -- Flag actual derived types so their elaboration produces the
13389 -- appropriate renamings for the primitive operations of the ancestor.
13390 -- Flag actual for formal private types as well, to determine whether
13391 -- operations in the private part may override inherited operations.
13392 -- If the formal has an interface list, the ancestor is not the
13393 -- parent, but the analyzed formal that includes the interface
13394 -- operations of all its progenitors.
13396 -- Same treatment for formal private types, so we can check whether the
13397 -- type is tagged limited when validating derivations in the private
13398 -- part. (See AI05-096).
13400 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13401 if Present (Interface_List (Def)) then
13402 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13404 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13407 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13408 N_Formal_Incomplete_Type_Definition)
13410 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13413 -- If the actual is a synchronized type that implements an interface,
13414 -- the primitive operations are attached to the corresponding record,
13415 -- and we have to treat it as an additional generic actual, so that its
13416 -- primitive operations become visible in the instance. The task or
13417 -- protected type itself does not carry primitive operations.
13419 if Is_Concurrent_Type (Act_T)
13420 and then Is_Tagged_Type (Act_T)
13421 and then Present (Corresponding_Record_Type (Act_T))
13422 and then Present (Ancestor)
13423 and then Is_Interface (Ancestor)
13426 Corr_Rec : constant Entity_Id :=
13427 Corresponding_Record_Type (Act_T);
13428 New_Corr : Entity_Id;
13429 Corr_Decl : Node_Id;
13432 New_Corr := Make_Temporary (Loc, 'S');
13434 Make_Subtype_Declaration (Loc,
13435 Defining_Identifier => New_Corr,
13436 Subtype_Indication =>
13437 New_Occurrence_Of (Corr_Rec, Loc));
13438 Append_To (Decl_Nodes, Corr_Decl);
13440 if Ekind (Act_T) = E_Task_Type then
13441 Set_Ekind (Subt, E_Task_Subtype);
13443 Set_Ekind (Subt, E_Protected_Subtype);
13446 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13447 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13448 Set_Generic_Parent_Type (Decl_Node, Empty);
13452 -- For a floating-point type, capture dimension info if any, because
13453 -- the generated subtype declaration does not come from source and
13454 -- will not process dimensions.
13456 if Is_Floating_Point_Type (Act_T) then
13457 Copy_Dimensions (Act_T, Subt);
13461 end Instantiate_Type;
13463 ---------------------
13464 -- Is_In_Main_Unit --
13465 ---------------------
13467 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13468 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13469 Current_Unit : Node_Id;
13472 if Unum = Main_Unit then
13475 -- If the current unit is a subunit then it is either the main unit or
13476 -- is being compiled as part of the main unit.
13478 elsif Nkind (N) = N_Compilation_Unit then
13479 return Nkind (Unit (N)) = N_Subunit;
13482 Current_Unit := Parent (N);
13483 while Present (Current_Unit)
13484 and then Nkind (Current_Unit) /= N_Compilation_Unit
13486 Current_Unit := Parent (Current_Unit);
13489 -- The instantiation node is in the main unit, or else the current node
13490 -- (perhaps as the result of nested instantiations) is in the main unit,
13491 -- or in the declaration of the main unit, which in this last case must
13495 Current_Unit = Cunit (Main_Unit)
13496 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13497 or else (Present (Current_Unit)
13498 and then Present (Library_Unit (Current_Unit))
13499 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13500 end Is_In_Main_Unit;
13502 ----------------------------
13503 -- Load_Parent_Of_Generic --
13504 ----------------------------
13506 procedure Load_Parent_Of_Generic
13509 Body_Optional : Boolean := False)
13511 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13512 Saved_Style_Check : constant Boolean := Style_Check;
13513 Saved_Warnings : constant Warning_Record := Save_Warnings;
13514 True_Parent : Node_Id;
13515 Inst_Node : Node_Id;
13517 Previous_Instances : constant Elist_Id := New_Elmt_List;
13519 procedure Collect_Previous_Instances (Decls : List_Id);
13520 -- Collect all instantiations in the given list of declarations, that
13521 -- precede the generic that we need to load. If the bodies of these
13522 -- instantiations are available, we must analyze them, to ensure that
13523 -- the public symbols generated are the same when the unit is compiled
13524 -- to generate code, and when it is compiled in the context of a unit
13525 -- that needs a particular nested instance. This process is applied to
13526 -- both package and subprogram instances.
13528 --------------------------------
13529 -- Collect_Previous_Instances --
13530 --------------------------------
13532 procedure Collect_Previous_Instances (Decls : List_Id) is
13536 Decl := First (Decls);
13537 while Present (Decl) loop
13538 if Sloc (Decl) >= Sloc (Inst_Node) then
13541 -- If Decl is an instantiation, then record it as requiring
13542 -- instantiation of the corresponding body, except if it is an
13543 -- abbreviated instantiation generated internally for conformance
13544 -- checking purposes only for the case of a formal package
13545 -- declared without a box (see Instantiate_Formal_Package). Such
13546 -- an instantiation does not generate any code (the actual code
13547 -- comes from actual) and thus does not need to be analyzed here.
13548 -- If the instantiation appears with a generic package body it is
13549 -- not analyzed here either.
13551 elsif Nkind (Decl) = N_Package_Instantiation
13552 and then not Is_Internal (Defining_Entity (Decl))
13554 Append_Elmt (Decl, Previous_Instances);
13556 -- For a subprogram instantiation, omit instantiations intrinsic
13557 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13559 elsif Nkind_In (Decl, N_Function_Instantiation,
13560 N_Procedure_Instantiation)
13561 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13563 Append_Elmt (Decl, Previous_Instances);
13565 elsif Nkind (Decl) = N_Package_Declaration then
13566 Collect_Previous_Instances
13567 (Visible_Declarations (Specification (Decl)));
13568 Collect_Previous_Instances
13569 (Private_Declarations (Specification (Decl)));
13571 -- Previous non-generic bodies may contain instances as well
13573 elsif Nkind (Decl) = N_Package_Body
13574 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13576 Collect_Previous_Instances (Declarations (Decl));
13578 elsif Nkind (Decl) = N_Subprogram_Body
13579 and then not Acts_As_Spec (Decl)
13580 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13582 Collect_Previous_Instances (Declarations (Decl));
13587 end Collect_Previous_Instances;
13589 -- Start of processing for Load_Parent_Of_Generic
13592 if not In_Same_Source_Unit (N, Spec)
13593 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13594 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13595 and then not Is_In_Main_Unit (Spec))
13597 -- Find body of parent of spec, and analyze it. A special case arises
13598 -- when the parent is an instantiation, that is to say when we are
13599 -- currently instantiating a nested generic. In that case, there is
13600 -- no separate file for the body of the enclosing instance. Instead,
13601 -- the enclosing body must be instantiated as if it were a pending
13602 -- instantiation, in order to produce the body for the nested generic
13603 -- we require now. Note that in that case the generic may be defined
13604 -- in a package body, the instance defined in the same package body,
13605 -- and the original enclosing body may not be in the main unit.
13607 Inst_Node := Empty;
13609 True_Parent := Parent (Spec);
13610 while Present (True_Parent)
13611 and then Nkind (True_Parent) /= N_Compilation_Unit
13613 if Nkind (True_Parent) = N_Package_Declaration
13615 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13617 -- Parent is a compilation unit that is an instantiation.
13618 -- Instantiation node has been replaced with package decl.
13620 Inst_Node := Original_Node (True_Parent);
13623 elsif Nkind (True_Parent) = N_Package_Declaration
13624 and then Present (Generic_Parent (Specification (True_Parent)))
13625 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13627 -- Parent is an instantiation within another specification.
13628 -- Declaration for instance has been inserted before original
13629 -- instantiation node. A direct link would be preferable?
13631 Inst_Node := Next (True_Parent);
13632 while Present (Inst_Node)
13633 and then Nkind (Inst_Node) /= N_Package_Instantiation
13638 -- If the instance appears within a generic, and the generic
13639 -- unit is defined within a formal package of the enclosing
13640 -- generic, there is no generic body available, and none
13641 -- needed. A more precise test should be used ???
13643 if No (Inst_Node) then
13650 True_Parent := Parent (True_Parent);
13654 -- Case where we are currently instantiating a nested generic
13656 if Present (Inst_Node) then
13657 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13659 -- Instantiation node and declaration of instantiated package
13660 -- were exchanged when only the declaration was needed.
13661 -- Restore instantiation node before proceeding with body.
13663 Set_Unit (Parent (True_Parent), Inst_Node);
13666 -- Now complete instantiation of enclosing body, if it appears in
13667 -- some other unit. If it appears in the current unit, the body
13668 -- will have been instantiated already.
13670 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13672 -- We need to determine the expander mode to instantiate the
13673 -- enclosing body. Because the generic body we need may use
13674 -- global entities declared in the enclosing package (including
13675 -- aggregates) it is in general necessary to compile this body
13676 -- with expansion enabled, except if we are within a generic
13677 -- package, in which case the usual generic rule applies.
13680 Exp_Status : Boolean := True;
13684 -- Loop through scopes looking for generic package
13686 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13687 while Present (Scop)
13688 and then Scop /= Standard_Standard
13690 if Ekind (Scop) = E_Generic_Package then
13691 Exp_Status := False;
13695 Scop := Scope (Scop);
13698 -- Collect previous instantiations in the unit that contains
13699 -- the desired generic.
13701 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13702 and then not Body_Optional
13706 Info : Pending_Body_Info;
13710 Par := Parent (Inst_Node);
13711 while Present (Par) loop
13712 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13713 Par := Parent (Par);
13716 pragma Assert (Present (Par));
13718 if Nkind (Par) = N_Package_Body then
13719 Collect_Previous_Instances (Declarations (Par));
13721 elsif Nkind (Par) = N_Package_Declaration then
13722 Collect_Previous_Instances
13723 (Visible_Declarations (Specification (Par)));
13724 Collect_Previous_Instances
13725 (Private_Declarations (Specification (Par)));
13728 -- Enclosing unit is a subprogram body. In this
13729 -- case all instance bodies are processed in order
13730 -- and there is no need to collect them separately.
13735 Decl := First_Elmt (Previous_Instances);
13736 while Present (Decl) loop
13738 (Inst_Node => Node (Decl),
13740 Instance_Spec (Node (Decl)),
13741 Expander_Status => Exp_Status,
13742 Current_Sem_Unit =>
13743 Get_Code_Unit (Sloc (Node (Decl))),
13744 Scope_Suppress => Scope_Suppress,
13745 Local_Suppress_Stack_Top =>
13746 Local_Suppress_Stack_Top,
13747 Version => Ada_Version,
13748 Version_Pragma => Ada_Version_Pragma,
13749 Warnings => Save_Warnings,
13750 SPARK_Mode => SPARK_Mode,
13751 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13753 -- Package instance
13755 if Nkind (Node (Decl)) = N_Package_Instantiation
13757 Instantiate_Package_Body
13758 (Info, Body_Optional => True);
13760 -- Subprogram instance
13763 -- The instance_spec is in the wrapper package,
13764 -- usually followed by its local renaming
13765 -- declaration. See Build_Subprogram_Renaming
13766 -- for details. If the instance carries aspects,
13767 -- these result in the corresponding pragmas,
13768 -- inserted after the subprogram declaration.
13769 -- They must be skipped as well when retrieving
13770 -- the desired spec. Some of them may have been
13771 -- rewritten as null statements.
13772 -- A direct link would be more robust ???
13776 (Last (Visible_Declarations
13777 (Specification (Info.Act_Decl))));
13779 while Nkind_In (Decl,
13782 N_Subprogram_Renaming_Declaration)
13784 Decl := Prev (Decl);
13787 Info.Act_Decl := Decl;
13790 Instantiate_Subprogram_Body
13791 (Info, Body_Optional => True);
13799 Instantiate_Package_Body
13801 ((Inst_Node => Inst_Node,
13802 Act_Decl => True_Parent,
13803 Expander_Status => Exp_Status,
13804 Current_Sem_Unit => Get_Code_Unit
13805 (Sloc (Inst_Node)),
13806 Scope_Suppress => Scope_Suppress,
13807 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13808 Version => Ada_Version,
13809 Version_Pragma => Ada_Version_Pragma,
13810 Warnings => Save_Warnings,
13811 SPARK_Mode => SPARK_Mode,
13812 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13813 Body_Optional => Body_Optional);
13817 -- Case where we are not instantiating a nested generic
13820 Opt.Style_Check := False;
13821 Expander_Mode_Save_And_Set (True);
13822 Load_Needed_Body (Comp_Unit, OK);
13823 Opt.Style_Check := Saved_Style_Check;
13824 Restore_Warnings (Saved_Warnings);
13825 Expander_Mode_Restore;
13828 and then Unit_Requires_Body (Defining_Entity (Spec))
13829 and then not Body_Optional
13832 Bname : constant Unit_Name_Type :=
13833 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13836 -- In CodePeer mode, the missing body may make the analysis
13837 -- incomplete, but we do not treat it as fatal.
13839 if CodePeer_Mode then
13843 Error_Msg_Unit_1 := Bname;
13844 Error_Msg_N ("this instantiation requires$!", N);
13845 Error_Msg_File_1 :=
13846 Get_File_Name (Bname, Subunit => False);
13847 Error_Msg_N ("\but file{ was not found!", N);
13848 raise Unrecoverable_Error;
13855 -- If loading parent of the generic caused an instantiation circularity,
13856 -- we abandon compilation at this point, because otherwise in some cases
13857 -- we get into trouble with infinite recursions after this point.
13859 if Circularity_Detected then
13860 raise Unrecoverable_Error;
13862 end Load_Parent_Of_Generic;
13864 ---------------------------------
13865 -- Map_Formal_Package_Entities --
13866 ---------------------------------
13868 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13873 Set_Instance_Of (Form, Act);
13875 -- Traverse formal and actual package to map the corresponding entities.
13876 -- We skip over internal entities that may be generated during semantic
13877 -- analysis, and find the matching entities by name, given that they
13878 -- must appear in the same order.
13880 E1 := First_Entity (Form);
13881 E2 := First_Entity (Act);
13882 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13883 -- Could this test be a single condition??? Seems like it could, and
13884 -- isn't FPE (Form) a constant anyway???
13886 if not Is_Internal (E1)
13887 and then Present (Parent (E1))
13888 and then not Is_Class_Wide_Type (E1)
13889 and then not Is_Internal_Name (Chars (E1))
13891 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13898 Set_Instance_Of (E1, E2);
13900 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13901 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13904 if Is_Constrained (E1) then
13905 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13908 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13909 Map_Formal_Package_Entities (E1, E2);
13916 end Map_Formal_Package_Entities;
13918 -----------------------
13919 -- Move_Freeze_Nodes --
13920 -----------------------
13922 procedure Move_Freeze_Nodes
13923 (Out_Of : Entity_Id;
13928 Next_Decl : Node_Id;
13929 Next_Node : Node_Id := After;
13932 function Is_Outer_Type (T : Entity_Id) return Boolean;
13933 -- Check whether entity is declared in a scope external to that of the
13936 -------------------
13937 -- Is_Outer_Type --
13938 -------------------
13940 function Is_Outer_Type (T : Entity_Id) return Boolean is
13941 Scop : Entity_Id := Scope (T);
13944 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13948 while Scop /= Standard_Standard loop
13949 if Scop = Out_Of then
13952 Scop := Scope (Scop);
13960 -- Start of processing for Move_Freeze_Nodes
13967 -- First remove the freeze nodes that may appear before all other
13971 while Present (Decl)
13972 and then Nkind (Decl) = N_Freeze_Entity
13973 and then Is_Outer_Type (Entity (Decl))
13975 Decl := Remove_Head (L);
13976 Insert_After (Next_Node, Decl);
13977 Set_Analyzed (Decl, False);
13982 -- Next scan the list of declarations and remove each freeze node that
13983 -- appears ahead of the current node.
13985 while Present (Decl) loop
13986 while Present (Next (Decl))
13987 and then Nkind (Next (Decl)) = N_Freeze_Entity
13988 and then Is_Outer_Type (Entity (Next (Decl)))
13990 Next_Decl := Remove_Next (Decl);
13991 Insert_After (Next_Node, Next_Decl);
13992 Set_Analyzed (Next_Decl, False);
13993 Next_Node := Next_Decl;
13996 -- If the declaration is a nested package or concurrent type, then
13997 -- recurse. Nested generic packages will have been processed from the
14000 case Nkind (Decl) is
14001 when N_Package_Declaration =>
14002 Spec := Specification (Decl);
14004 when N_Task_Type_Declaration =>
14005 Spec := Task_Definition (Decl);
14007 when N_Protected_Type_Declaration =>
14008 Spec := Protected_Definition (Decl);
14014 if Present (Spec) then
14015 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14016 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14021 end Move_Freeze_Nodes;
14027 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14029 return Generic_Renamings.Table (E).Next_In_HTable;
14032 ------------------------
14033 -- Preanalyze_Actuals --
14034 ------------------------
14036 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14039 Errs : constant Nat := Serious_Errors_Detected;
14041 Cur : Entity_Id := Empty;
14042 -- Current homograph of the instance name
14044 Vis : Boolean := False;
14045 -- Saved visibility status of the current homograph
14048 Assoc := First (Generic_Associations (N));
14050 -- If the instance is a child unit, its name may hide an outer homonym,
14051 -- so make it invisible to perform name resolution on the actuals.
14053 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14055 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14057 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14059 if Is_Compilation_Unit (Cur) then
14060 Vis := Is_Immediately_Visible (Cur);
14061 Set_Is_Immediately_Visible (Cur, False);
14067 while Present (Assoc) loop
14068 if Nkind (Assoc) /= N_Others_Choice then
14069 Act := Explicit_Generic_Actual_Parameter (Assoc);
14071 -- Within a nested instantiation, a defaulted actual is an empty
14072 -- association, so nothing to analyze. If the subprogram actual
14073 -- is an attribute, analyze prefix only, because actual is not a
14074 -- complete attribute reference.
14076 -- If actual is an allocator, analyze expression only. The full
14077 -- analysis can generate code, and if instance is a compilation
14078 -- unit we have to wait until the package instance is installed
14079 -- to have a proper place to insert this code.
14081 -- String literals may be operators, but at this point we do not
14082 -- know whether the actual is a formal subprogram or a string.
14087 elsif Nkind (Act) = N_Attribute_Reference then
14088 Analyze (Prefix (Act));
14090 elsif Nkind (Act) = N_Explicit_Dereference then
14091 Analyze (Prefix (Act));
14093 elsif Nkind (Act) = N_Allocator then
14095 Expr : constant Node_Id := Expression (Act);
14098 if Nkind (Expr) = N_Subtype_Indication then
14099 Analyze (Subtype_Mark (Expr));
14101 -- Analyze separately each discriminant constraint, when
14102 -- given with a named association.
14108 Constr := First (Constraints (Constraint (Expr)));
14109 while Present (Constr) loop
14110 if Nkind (Constr) = N_Discriminant_Association then
14111 Analyze (Expression (Constr));
14125 elsif Nkind (Act) /= N_Operator_Symbol then
14128 -- Within a package instance, mark actuals that are limited
14129 -- views, so their use can be moved to the body of the
14132 if Is_Entity_Name (Act)
14133 and then Is_Type (Entity (Act))
14134 and then From_Limited_With (Entity (Act))
14135 and then Present (Inst)
14137 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14141 if Errs /= Serious_Errors_Detected then
14143 -- Do a minimal analysis of the generic, to prevent spurious
14144 -- warnings complaining about the generic being unreferenced,
14145 -- before abandoning the instantiation.
14147 Analyze (Name (N));
14149 if Is_Entity_Name (Name (N))
14150 and then Etype (Name (N)) /= Any_Type
14152 Generate_Reference (Entity (Name (N)), Name (N));
14153 Set_Is_Instantiated (Entity (Name (N)));
14156 if Present (Cur) then
14158 -- For the case of a child instance hiding an outer homonym,
14159 -- provide additional warning which might explain the error.
14161 Set_Is_Immediately_Visible (Cur, Vis);
14163 ("& hides outer unit with the same name??",
14164 N, Defining_Unit_Name (N));
14167 Abandon_Instantiation (Act);
14174 if Present (Cur) then
14175 Set_Is_Immediately_Visible (Cur, Vis);
14177 end Preanalyze_Actuals;
14179 -------------------------------
14180 -- Provide_Completing_Bodies --
14181 -------------------------------
14183 procedure Provide_Completing_Bodies (N : Node_Id) is
14184 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14185 -- Generate the completing body for subprogram declaration Subp_Decl
14187 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14188 -- Generating completing bodies for all subprograms found in declarative
14191 ---------------------------
14192 -- Build_Completing_Body --
14193 ---------------------------
14195 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14196 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14197 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14201 -- Nothing to do if the subprogram already has a completing body
14203 if Present (Corresponding_Body (Subp_Decl)) then
14206 -- Mark the function as having a valid return statement even though
14207 -- the body contains a single raise statement.
14209 elsif Ekind (Subp_Id) = E_Function then
14210 Set_Return_Present (Subp_Id);
14213 -- Clone the specification to obtain new entities and reset the only
14216 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14217 Set_Generic_Parent (Spec, Empty);
14220 -- function Func ... return ... is
14222 -- procedure Proc ... is
14224 -- raise Program_Error with "access before elaboration";
14227 Insert_After_And_Analyze (Subp_Decl,
14228 Make_Subprogram_Body (Loc,
14229 Specification => Spec,
14230 Declarations => New_List,
14231 Handled_Statement_Sequence =>
14232 Make_Handled_Sequence_Of_Statements (Loc,
14233 Statements => New_List (
14234 Make_Raise_Program_Error (Loc,
14235 Reason => PE_Access_Before_Elaboration)))));
14236 end Build_Completing_Body;
14238 ----------------------------------
14239 -- Provide_Completing_Bodies_In --
14240 ----------------------------------
14242 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14246 if Present (Decls) then
14247 Decl := First (Decls);
14248 while Present (Decl) loop
14249 Provide_Completing_Bodies (Decl);
14253 end Provide_Completing_Bodies_In;
14259 -- Start of processing for Provide_Completing_Bodies
14262 if Nkind (N) = N_Package_Declaration then
14263 Spec := Specification (N);
14265 Push_Scope (Defining_Entity (N));
14266 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14267 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14270 elsif Nkind (N) = N_Subprogram_Declaration then
14271 Build_Completing_Body (N);
14273 end Provide_Completing_Bodies;
14275 -------------------
14276 -- Remove_Parent --
14277 -------------------
14279 procedure Remove_Parent (In_Body : Boolean := False) is
14280 S : Entity_Id := Current_Scope;
14281 -- S is the scope containing the instantiation just completed. The scope
14282 -- stack contains the parent instances of the instantiation, followed by
14291 -- After child instantiation is complete, remove from scope stack the
14292 -- extra copy of the current scope, and then remove parent instances.
14294 if not In_Body then
14297 while Current_Scope /= S loop
14298 P := Current_Scope;
14299 End_Package_Scope (Current_Scope);
14301 if In_Open_Scopes (P) then
14302 E := First_Entity (P);
14303 while Present (E) loop
14304 Set_Is_Immediately_Visible (E, True);
14308 -- If instantiation is declared in a block, it is the enclosing
14309 -- scope that might be a parent instance. Note that only one
14310 -- block can be involved, because the parent instances have
14311 -- been installed within it.
14313 if Ekind (P) = E_Block then
14314 Cur_P := Scope (P);
14319 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14320 -- We are within an instance of some sibling. Retain
14321 -- visibility of parent, for proper subsequent cleanup, and
14322 -- reinstall private declarations as well.
14324 Set_In_Private_Part (P);
14325 Install_Private_Declarations (P);
14328 -- If the ultimate parent is a top-level unit recorded in
14329 -- Instance_Parent_Unit, then reset its visibility to what it was
14330 -- before instantiation. (It's not clear what the purpose is of
14331 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14332 -- present before the ultimate parent test was added.???)
14334 elsif not In_Open_Scopes (Scope (P))
14335 or else (P = Instance_Parent_Unit
14336 and then not Parent_Unit_Visible)
14338 Set_Is_Immediately_Visible (P, False);
14340 -- If the current scope is itself an instantiation of a generic
14341 -- nested within P, and we are in the private part of body of this
14342 -- instantiation, restore the full views of P, that were removed
14343 -- in End_Package_Scope above. This obscure case can occur when a
14344 -- subunit of a generic contains an instance of a child unit of
14345 -- its generic parent unit.
14347 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14349 Par : constant Entity_Id :=
14350 Generic_Parent (Package_Specification (S));
14353 and then P = Scope (Par)
14354 and then (In_Package_Body (S) or else In_Private_Part (S))
14356 Set_In_Private_Part (P);
14357 Install_Private_Declarations (P);
14363 -- Reset visibility of entities in the enclosing scope
14365 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14367 Hidden := First_Elmt (Hidden_Entities);
14368 while Present (Hidden) loop
14369 Set_Is_Immediately_Visible (Node (Hidden), True);
14370 Next_Elmt (Hidden);
14374 -- Each body is analyzed separately, and there is no context that
14375 -- needs preserving from one body instance to the next, so remove all
14376 -- parent scopes that have been installed.
14378 while Present (S) loop
14379 End_Package_Scope (S);
14380 Set_Is_Immediately_Visible (S, False);
14381 S := Current_Scope;
14382 exit when S = Standard_Standard;
14391 procedure Restore_Env is
14392 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14395 if No (Current_Instantiated_Parent.Act_Id) then
14396 -- Restore environment after subprogram inlining
14398 Restore_Private_Views (Empty);
14401 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14402 Exchanged_Views := Saved.Exchanged_Views;
14403 Hidden_Entities := Saved.Hidden_Entities;
14404 Current_Sem_Unit := Saved.Current_Sem_Unit;
14405 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14406 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14408 Restore_Opt_Config_Switches (Saved.Switches);
14410 Instance_Envs.Decrement_Last;
14413 ---------------------------
14414 -- Restore_Private_Views --
14415 ---------------------------
14417 procedure Restore_Private_Views
14418 (Pack_Id : Entity_Id;
14419 Is_Package : Boolean := True)
14424 Dep_Elmt : Elmt_Id;
14427 procedure Restore_Nested_Formal (Formal : Entity_Id);
14428 -- Hide the generic formals of formal packages declared with box which
14429 -- were reachable in the current instantiation.
14431 ---------------------------
14432 -- Restore_Nested_Formal --
14433 ---------------------------
14435 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14439 if Present (Renamed_Object (Formal))
14440 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14444 elsif Present (Associated_Formal_Package (Formal)) then
14445 Ent := First_Entity (Formal);
14446 while Present (Ent) loop
14447 exit when Ekind (Ent) = E_Package
14448 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14450 Set_Is_Hidden (Ent);
14451 Set_Is_Potentially_Use_Visible (Ent, False);
14453 -- If package, then recurse
14455 if Ekind (Ent) = E_Package then
14456 Restore_Nested_Formal (Ent);
14462 end Restore_Nested_Formal;
14464 -- Start of processing for Restore_Private_Views
14467 M := First_Elmt (Exchanged_Views);
14468 while Present (M) loop
14471 -- Subtypes of types whose views have been exchanged, and that are
14472 -- defined within the instance, were not on the Private_Dependents
14473 -- list on entry to the instance, so they have to be exchanged
14474 -- explicitly now, in order to remain consistent with the view of the
14477 if Ekind_In (Typ, E_Private_Type,
14478 E_Limited_Private_Type,
14479 E_Record_Type_With_Private)
14481 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14482 while Present (Dep_Elmt) loop
14483 Dep_Typ := Node (Dep_Elmt);
14485 if Scope (Dep_Typ) = Pack_Id
14486 and then Present (Full_View (Dep_Typ))
14488 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14489 Exchange_Declarations (Dep_Typ);
14492 Next_Elmt (Dep_Elmt);
14496 Exchange_Declarations (Node (M));
14500 if No (Pack_Id) then
14504 -- Make the generic formal parameters private, and make the formal types
14505 -- into subtypes of the actuals again.
14507 E := First_Entity (Pack_Id);
14508 while Present (E) loop
14509 Set_Is_Hidden (E, True);
14512 and then Nkind (Parent (E)) = N_Subtype_Declaration
14514 -- If the actual for E is itself a generic actual type from
14515 -- an enclosing instance, E is still a generic actual type
14516 -- outside of the current instance. This matter when resolving
14517 -- an overloaded call that may be ambiguous in the enclosing
14518 -- instance, when two of its actuals coincide.
14520 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14521 and then Is_Generic_Actual_Type
14522 (Entity (Subtype_Indication (Parent (E))))
14526 Set_Is_Generic_Actual_Type (E, False);
14529 -- An unusual case of aliasing: the actual may also be directly
14530 -- visible in the generic, and be private there, while it is fully
14531 -- visible in the context of the instance. The internal subtype
14532 -- is private in the instance but has full visibility like its
14533 -- parent in the enclosing scope. This enforces the invariant that
14534 -- the privacy status of all private dependents of a type coincide
14535 -- with that of the parent type. This can only happen when a
14536 -- generic child unit is instantiated within a sibling.
14538 if Is_Private_Type (E)
14539 and then not Is_Private_Type (Etype (E))
14541 Exchange_Declarations (E);
14544 elsif Ekind (E) = E_Package then
14546 -- The end of the renaming list is the renaming of the generic
14547 -- package itself. If the instance is a subprogram, all entities
14548 -- in the corresponding package are renamings. If this entity is
14549 -- a formal package, make its own formals private as well. The
14550 -- actual in this case is itself the renaming of an instantiation.
14551 -- If the entity is not a package renaming, it is the entity
14552 -- created to validate formal package actuals: ignore it.
14554 -- If the actual is itself a formal package for the enclosing
14555 -- generic, or the actual for such a formal package, it remains
14556 -- visible on exit from the instance, and therefore nothing needs
14557 -- to be done either, except to keep it accessible.
14559 if Is_Package and then Renamed_Object (E) = Pack_Id then
14562 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14566 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14568 Set_Is_Hidden (E, False);
14572 Act_P : constant Entity_Id := Renamed_Object (E);
14576 Id := First_Entity (Act_P);
14578 and then Id /= First_Private_Entity (Act_P)
14580 exit when Ekind (Id) = E_Package
14581 and then Renamed_Object (Id) = Act_P;
14583 Set_Is_Hidden (Id, True);
14584 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14586 if Ekind (Id) = E_Package then
14587 Restore_Nested_Formal (Id);
14598 end Restore_Private_Views;
14605 (Gen_Unit : Entity_Id;
14606 Act_Unit : Entity_Id)
14610 Set_Instance_Env (Gen_Unit, Act_Unit);
14613 ----------------------------
14614 -- Save_Global_References --
14615 ----------------------------
14617 procedure Save_Global_References (Templ : Node_Id) is
14619 -- ??? it is horrible to use global variables in highly recursive code
14622 -- The entity of the current associated node
14624 Gen_Scope : Entity_Id;
14625 -- The scope of the generic for which references are being saved
14628 -- The current associated node
14630 function Is_Global (E : Entity_Id) return Boolean;
14631 -- Check whether entity is defined outside of generic unit. Examine the
14632 -- scope of an entity, and the scope of the scope, etc, until we find
14633 -- either Standard, in which case the entity is global, or the generic
14634 -- unit itself, which indicates that the entity is local. If the entity
14635 -- is the generic unit itself, as in the case of a recursive call, or
14636 -- the enclosing generic unit, if different from the current scope, then
14637 -- it is local as well, because it will be replaced at the point of
14638 -- instantiation. On the other hand, if it is a reference to a child
14639 -- unit of a common ancestor, which appears in an instantiation, it is
14640 -- global because it is used to denote a specific compilation unit at
14641 -- the time the instantiations will be analyzed.
14643 procedure Qualify_Universal_Operands
14645 Func_Call : Node_Id);
14646 -- Op denotes a binary or unary operator in generic template Templ. Node
14647 -- Func_Call is the function call alternative of the operator within the
14648 -- the analyzed copy of the template. Change each operand which yields a
14649 -- universal type by wrapping it into a qualified expression
14651 -- Actual_Typ'(Operand)
14653 -- where Actual_Typ is the type of corresponding actual parameter of
14654 -- Operand in Func_Call.
14656 procedure Reset_Entity (N : Node_Id);
14657 -- Save semantic information on global entity so that it is not resolved
14658 -- again at instantiation time.
14660 procedure Save_Entity_Descendants (N : Node_Id);
14661 -- Apply Save_Global_References to the two syntactic descendants of
14662 -- non-terminal nodes that carry an Associated_Node and are processed
14663 -- through Reset_Entity. Once the global entity (if any) has been
14664 -- captured together with its type, only two syntactic descendants need
14665 -- to be traversed to complete the processing of the tree rooted at N.
14666 -- This applies to Selected_Components, Expanded_Names, and to Operator
14667 -- nodes. N can also be a character literal, identifier, or operator
14668 -- symbol node, but the call has no effect in these cases.
14670 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14671 -- Default actuals in nested instances must be handled specially
14672 -- because there is no link to them from the original tree. When an
14673 -- actual subprogram is given by a default, we add an explicit generic
14674 -- association for it in the instantiation node. When we save the
14675 -- global references on the name of the instance, we recover the list
14676 -- of generic associations, and add an explicit one to the original
14677 -- generic tree, through which a global actual can be preserved.
14678 -- Similarly, if a child unit is instantiated within a sibling, in the
14679 -- context of the parent, we must preserve the identifier of the parent
14680 -- so that it can be properly resolved in a subsequent instantiation.
14682 procedure Save_Global_Descendant (D : Union_Id);
14683 -- Apply Save_References recursively to the descendants of node D
14685 procedure Save_References (N : Node_Id);
14686 -- This is the recursive procedure that does the work, once the
14687 -- enclosing generic scope has been established.
14693 function Is_Global (E : Entity_Id) return Boolean is
14696 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14697 -- Determine whether the parent node of a reference to a child unit
14698 -- denotes an instantiation or a formal package, in which case the
14699 -- reference to the child unit is global, even if it appears within
14700 -- the current scope (e.g. when the instance appears within the body
14701 -- of an ancestor).
14703 ----------------------
14704 -- Is_Instance_Node --
14705 ----------------------
14707 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14709 return Nkind (Decl) in N_Generic_Instantiation
14711 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14712 end Is_Instance_Node;
14714 -- Start of processing for Is_Global
14717 if E = Gen_Scope then
14720 elsif E = Standard_Standard then
14723 elsif Is_Child_Unit (E)
14724 and then (Is_Instance_Node (Parent (N2))
14725 or else (Nkind (Parent (N2)) = N_Expanded_Name
14726 and then N2 = Selector_Name (Parent (N2))
14728 Is_Instance_Node (Parent (Parent (N2)))))
14734 while Se /= Gen_Scope loop
14735 if Se = Standard_Standard then
14746 --------------------------------
14747 -- Qualify_Universal_Operands --
14748 --------------------------------
14750 procedure Qualify_Universal_Operands
14752 Func_Call : Node_Id)
14754 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14755 -- Rewrite operand Opnd as a qualified expression of the form
14757 -- Actual_Typ'(Opnd)
14759 -- where Actual is the corresponding actual parameter of Opnd in
14760 -- function call Func_Call.
14762 function Qualify_Type
14764 Typ : Entity_Id) return Node_Id;
14765 -- Qualify type Typ by creating a selected component of the form
14767 -- Scope_Of_Typ.Typ
14769 ---------------------
14770 -- Qualify_Operand --
14771 ---------------------
14773 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14774 Loc : constant Source_Ptr := Sloc (Opnd);
14775 Typ : constant Entity_Id := Etype (Actual);
14780 -- Qualify the operand when it is of a universal type. Note that
14781 -- the template is unanalyzed and it is not possible to directly
14782 -- query the type. This transformation is not done when the type
14783 -- of the actual is internally generated because the type will be
14784 -- regenerated in the instance.
14786 if Yields_Universal_Type (Opnd)
14787 and then Comes_From_Source (Typ)
14788 and then not Is_Hidden (Typ)
14790 -- The type of the actual may be a global reference. Save this
14791 -- information by creating a reference to it.
14793 if Is_Global (Typ) then
14794 Mark := New_Occurrence_Of (Typ, Loc);
14796 -- Otherwise rely on resolution to find the proper type within
14800 Mark := Qualify_Type (Loc, Typ);
14804 Make_Qualified_Expression (Loc,
14805 Subtype_Mark => Mark,
14806 Expression => Relocate_Node (Opnd));
14808 -- Mark the qualification to distinguish it from other source
14809 -- constructs and signal the instantiation mechanism that this
14810 -- node requires special processing. See Copy_Generic_Node for
14813 Set_Is_Qualified_Universal_Literal (Qual);
14815 Rewrite (Opnd, Qual);
14817 end Qualify_Operand;
14823 function Qualify_Type
14825 Typ : Entity_Id) return Node_Id
14827 Scop : constant Entity_Id := Scope (Typ);
14831 Result := Make_Identifier (Loc, Chars (Typ));
14833 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14835 Make_Selected_Component (Loc,
14836 Prefix => Make_Identifier (Loc, Chars (Scop)),
14837 Selector_Name => Result);
14845 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14847 -- Start of processing for Qualify_Universal_Operands
14850 if Nkind (Op) in N_Binary_Op then
14851 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14852 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14854 elsif Nkind (Op) in N_Unary_Op then
14855 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14857 end Qualify_Universal_Operands;
14863 procedure Reset_Entity (N : Node_Id) is
14864 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14865 -- If the type of N2 is global to the generic unit, save the type in
14866 -- the generic node. Just as we perform name capture for explicit
14867 -- references within the generic, we must capture the global types
14868 -- of local entities because they may participate in resolution in
14871 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14872 -- Find the ultimate ancestor of the current unit. If it is not a
14873 -- generic unit, then the name of the current unit in the prefix of
14874 -- an expanded name must be replaced with its generic homonym to
14875 -- ensure that it will be properly resolved in an instance.
14877 ---------------------
14878 -- Set_Global_Type --
14879 ---------------------
14881 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14882 Typ : constant Entity_Id := Etype (N2);
14885 Set_Etype (N, Typ);
14887 -- If the entity of N is not the associated node, this is a
14888 -- nested generic and it has an associated node as well, whose
14889 -- type is already the full view (see below). Indicate that the
14890 -- original node has a private view.
14892 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14893 Set_Has_Private_View (N);
14896 -- If not a private type, nothing else to do
14898 if not Is_Private_Type (Typ) then
14899 if Is_Array_Type (Typ)
14900 and then Is_Private_Type (Component_Type (Typ))
14902 Set_Has_Private_View (N);
14905 -- If it is a derivation of a private type in a context where no
14906 -- full view is needed, nothing to do either.
14908 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14911 -- Otherwise mark the type for flipping and use the full view when
14915 Set_Has_Private_View (N);
14917 if Present (Full_View (Typ)) then
14918 Set_Etype (N2, Full_View (Typ));
14922 if Is_Floating_Point_Type (Typ)
14923 and then Has_Dimension_System (Typ)
14925 Copy_Dimensions (N2, N);
14927 end Set_Global_Type;
14933 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14938 while Is_Child_Unit (Par) loop
14939 Par := Scope (Par);
14945 -- Start of processing for Reset_Entity
14948 N2 := Get_Associated_Node (N);
14951 if Present (E) then
14953 -- If the node is an entry call to an entry in an enclosing task,
14954 -- it is rewritten as a selected component. No global entity to
14955 -- preserve in this case, since the expansion will be redone in
14958 if not Nkind_In (E, N_Defining_Character_Literal,
14959 N_Defining_Identifier,
14960 N_Defining_Operator_Symbol)
14962 Set_Associated_Node (N, Empty);
14963 Set_Etype (N, Empty);
14967 -- If the entity is an itype created as a subtype of an access
14968 -- type with a null exclusion restore source entity for proper
14969 -- visibility. The itype will be created anew in the instance.
14972 and then Ekind (E) = E_Access_Subtype
14973 and then Is_Entity_Name (N)
14974 and then Chars (Etype (E)) = Chars (N)
14977 Set_Entity (N2, E);
14981 if Is_Global (E) then
14983 -- If the entity is a package renaming that is the prefix of
14984 -- an expanded name, it has been rewritten as the renamed
14985 -- package, which is necessary semantically but complicates
14986 -- ASIS tree traversal, so we recover the original entity to
14987 -- expose the renaming. Take into account that the context may
14988 -- be a nested generic, that the original node may itself have
14989 -- an associated node that had better be an entity, and that
14990 -- the current node is still a selected component.
14992 if Ekind (E) = E_Package
14993 and then Nkind (N) = N_Selected_Component
14994 and then Nkind (Parent (N)) = N_Expanded_Name
14995 and then Present (Original_Node (N2))
14996 and then Is_Entity_Name (Original_Node (N2))
14997 and then Present (Entity (Original_Node (N2)))
14999 if Is_Global (Entity (Original_Node (N2))) then
15000 N2 := Original_Node (N2);
15001 Set_Associated_Node (N, N2);
15002 Set_Global_Type (N, N2);
15004 -- Renaming is local, and will be resolved in instance
15007 Set_Associated_Node (N, Empty);
15008 Set_Etype (N, Empty);
15012 Set_Global_Type (N, N2);
15015 elsif Nkind (N) = N_Op_Concat
15016 and then Is_Generic_Type (Etype (N2))
15017 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15019 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15020 and then Is_Intrinsic_Subprogram (E)
15024 -- Entity is local. Mark generic node as unresolved. Note that now
15025 -- it does not have an entity.
15028 Set_Associated_Node (N, Empty);
15029 Set_Etype (N, Empty);
15032 if Nkind (Parent (N)) in N_Generic_Instantiation
15033 and then N = Name (Parent (N))
15035 Save_Global_Defaults (Parent (N), Parent (N2));
15038 elsif Nkind (Parent (N)) = N_Selected_Component
15039 and then Nkind (Parent (N2)) = N_Expanded_Name
15041 if Is_Global (Entity (Parent (N2))) then
15042 Change_Selected_Component_To_Expanded_Name (Parent (N));
15043 Set_Associated_Node (Parent (N), Parent (N2));
15044 Set_Global_Type (Parent (N), Parent (N2));
15045 Save_Entity_Descendants (N);
15047 -- If this is a reference to the current generic entity, replace
15048 -- by the name of the generic homonym of the current package. This
15049 -- is because in an instantiation Par.P.Q will not resolve to the
15050 -- name of the instance, whose enclosing scope is not necessarily
15051 -- Par. We use the generic homonym rather that the name of the
15052 -- generic itself because it may be hidden by a local declaration.
15054 elsif In_Open_Scopes (Entity (Parent (N2)))
15056 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15058 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15059 Rewrite (Parent (N),
15060 Make_Identifier (Sloc (N),
15062 Chars (Generic_Homonym (Entity (Parent (N2))))));
15064 Rewrite (Parent (N),
15065 Make_Identifier (Sloc (N),
15066 Chars => Chars (Selector_Name (Parent (N2)))));
15070 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15071 and then Parent (N) = Name (Parent (Parent (N)))
15073 Save_Global_Defaults
15074 (Parent (Parent (N)), Parent (Parent (N2)));
15077 -- A selected component may denote a static constant that has been
15078 -- folded. If the static constant is global to the generic, capture
15079 -- its value. Otherwise the folding will happen in any instantiation.
15081 elsif Nkind (Parent (N)) = N_Selected_Component
15082 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15084 if Present (Entity (Original_Node (Parent (N2))))
15085 and then Is_Global (Entity (Original_Node (Parent (N2))))
15087 Rewrite (Parent (N), New_Copy (Parent (N2)));
15088 Set_Analyzed (Parent (N), False);
15091 -- A selected component may be transformed into a parameterless
15092 -- function call. If the called entity is global, rewrite the node
15093 -- appropriately, i.e. as an extended name for the global entity.
15095 elsif Nkind (Parent (N)) = N_Selected_Component
15096 and then Nkind (Parent (N2)) = N_Function_Call
15097 and then N = Selector_Name (Parent (N))
15099 if No (Parameter_Associations (Parent (N2))) then
15100 if Is_Global (Entity (Name (Parent (N2)))) then
15101 Change_Selected_Component_To_Expanded_Name (Parent (N));
15102 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15103 Set_Global_Type (Parent (N), Name (Parent (N2)));
15104 Save_Entity_Descendants (N);
15107 Set_Is_Prefixed_Call (Parent (N));
15108 Set_Associated_Node (N, Empty);
15109 Set_Etype (N, Empty);
15112 -- In Ada 2005, X.F may be a call to a primitive operation,
15113 -- rewritten as F (X). This rewriting will be done again in an
15114 -- instance, so keep the original node. Global entities will be
15115 -- captured as for other constructs. Indicate that this must
15116 -- resolve as a call, to prevent accidental overloading in the
15117 -- instance, if both a component and a primitive operation appear
15121 Set_Is_Prefixed_Call (Parent (N));
15124 -- Entity is local. Reset in generic unit, so that node is resolved
15125 -- anew at the point of instantiation.
15128 Set_Associated_Node (N, Empty);
15129 Set_Etype (N, Empty);
15133 -----------------------------
15134 -- Save_Entity_Descendants --
15135 -----------------------------
15137 procedure Save_Entity_Descendants (N : Node_Id) is
15140 when N_Binary_Op =>
15141 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15142 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15145 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15147 when N_Expanded_Name
15148 | N_Selected_Component
15150 Save_Global_Descendant (Union_Id (Prefix (N)));
15151 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15153 when N_Character_Literal
15155 | N_Operator_Symbol
15160 raise Program_Error;
15162 end Save_Entity_Descendants;
15164 --------------------------
15165 -- Save_Global_Defaults --
15166 --------------------------
15168 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15169 Loc : constant Source_Ptr := Sloc (N1);
15170 Assoc2 : constant List_Id := Generic_Associations (N2);
15171 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15178 Actual : Entity_Id;
15181 Assoc1 := Generic_Associations (N1);
15183 if Present (Assoc1) then
15184 Act1 := First (Assoc1);
15187 Set_Generic_Associations (N1, New_List);
15188 Assoc1 := Generic_Associations (N1);
15191 if Present (Assoc2) then
15192 Act2 := First (Assoc2);
15197 while Present (Act1) and then Present (Act2) loop
15202 -- Find the associations added for default subprograms
15204 if Present (Act2) then
15205 while Nkind (Act2) /= N_Generic_Association
15206 or else No (Entity (Selector_Name (Act2)))
15207 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15212 -- Add a similar association if the default is global. The
15213 -- renaming declaration for the actual has been analyzed, and
15214 -- its alias is the program it renames. Link the actual in the
15215 -- original generic tree with the node in the analyzed tree.
15217 while Present (Act2) loop
15218 Subp := Entity (Selector_Name (Act2));
15219 Def := Explicit_Generic_Actual_Parameter (Act2);
15221 -- Following test is defence against rubbish errors
15223 if No (Alias (Subp)) then
15227 -- Retrieve the resolved actual from the renaming declaration
15228 -- created for the instantiated formal.
15230 Actual := Entity (Name (Parent (Parent (Subp))));
15231 Set_Entity (Def, Actual);
15232 Set_Etype (Def, Etype (Actual));
15234 if Is_Global (Actual) then
15236 Make_Generic_Association (Loc,
15238 New_Occurrence_Of (Subp, Loc),
15239 Explicit_Generic_Actual_Parameter =>
15240 New_Occurrence_Of (Actual, Loc));
15242 Set_Associated_Node
15243 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15245 Append (Ndec, Assoc1);
15247 -- If there are other defaults, add a dummy association in case
15248 -- there are other defaulted formals with the same name.
15250 elsif Present (Next (Act2)) then
15252 Make_Generic_Association (Loc,
15254 New_Occurrence_Of (Subp, Loc),
15255 Explicit_Generic_Actual_Parameter => Empty);
15257 Append (Ndec, Assoc1);
15264 if Nkind (Name (N1)) = N_Identifier
15265 and then Is_Child_Unit (Gen_Id)
15266 and then Is_Global (Gen_Id)
15267 and then Is_Generic_Unit (Scope (Gen_Id))
15268 and then In_Open_Scopes (Scope (Gen_Id))
15270 -- This is an instantiation of a child unit within a sibling, so
15271 -- that the generic parent is in scope. An eventual instance must
15272 -- occur within the scope of an instance of the parent. Make name
15273 -- in instance into an expanded name, to preserve the identifier
15274 -- of the parent, so it can be resolved subsequently.
15276 Rewrite (Name (N2),
15277 Make_Expanded_Name (Loc,
15278 Chars => Chars (Gen_Id),
15279 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15280 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15281 Set_Entity (Name (N2), Gen_Id);
15283 Rewrite (Name (N1),
15284 Make_Expanded_Name (Loc,
15285 Chars => Chars (Gen_Id),
15286 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15287 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15289 Set_Associated_Node (Name (N1), Name (N2));
15290 Set_Associated_Node (Prefix (Name (N1)), Empty);
15291 Set_Associated_Node
15292 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15293 Set_Etype (Name (N1), Etype (Gen_Id));
15295 end Save_Global_Defaults;
15297 ----------------------------
15298 -- Save_Global_Descendant --
15299 ----------------------------
15301 procedure Save_Global_Descendant (D : Union_Id) is
15305 if D in Node_Range then
15306 if D = Union_Id (Empty) then
15309 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15310 Save_References (Node_Id (D));
15313 elsif D in List_Range then
15314 pragma Assert (D /= Union_Id (No_List));
15315 -- Because No_List = Empty, which is in Node_Range above
15317 if Is_Empty_List (List_Id (D)) then
15321 N1 := First (List_Id (D));
15322 while Present (N1) loop
15323 Save_References (N1);
15328 -- Element list or other non-node field, nothing to do
15333 end Save_Global_Descendant;
15335 ---------------------
15336 -- Save_References --
15337 ---------------------
15339 -- This is the recursive procedure that does the work once the enclosing
15340 -- generic scope has been established. We have to treat specially a
15341 -- number of node rewritings that are required by semantic processing
15342 -- and which change the kind of nodes in the generic copy: typically
15343 -- constant-folding, replacing an operator node by a string literal, or
15344 -- a selected component by an expanded name. In each of those cases, the
15345 -- transformation is propagated to the generic unit.
15347 procedure Save_References (N : Node_Id) is
15348 Loc : constant Source_Ptr := Sloc (N);
15350 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15351 -- Determine whether arbitrary node Nod requires delayed capture of
15352 -- global references within its aspect specifications.
15354 procedure Save_References_In_Aggregate (N : Node_Id);
15355 -- Save all global references in [extension] aggregate node N
15357 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15358 -- Save all global references in a character literal or operator
15359 -- symbol denoted by N.
15361 procedure Save_References_In_Descendants (N : Node_Id);
15362 -- Save all global references in all descendants of node N
15364 procedure Save_References_In_Identifier (N : Node_Id);
15365 -- Save all global references in identifier node N
15367 procedure Save_References_In_Operator (N : Node_Id);
15368 -- Save all global references in operator node N
15370 procedure Save_References_In_Pragma (Prag : Node_Id);
15371 -- Save all global references found within the expression of pragma
15374 ---------------------------
15375 -- Requires_Delayed_Save --
15376 ---------------------------
15378 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15380 -- Generic packages and subprograms require delayed capture of
15381 -- global references within their aspects due to the timing of
15382 -- annotation analysis.
15384 if Nkind_In (Nod, N_Generic_Package_Declaration,
15385 N_Generic_Subprogram_Declaration,
15387 N_Package_Body_Stub,
15389 N_Subprogram_Body_Stub)
15391 -- Since the capture of global references is done on the
15392 -- unanalyzed generic template, there is no information around
15393 -- to infer the context. Use the Associated_Entity linkages to
15394 -- peek into the analyzed generic copy and determine what the
15395 -- template corresponds to.
15397 if Nod = Templ then
15399 Is_Generic_Declaration_Or_Body
15400 (Unit_Declaration_Node
15401 (Associated_Entity (Defining_Entity (Nod))));
15403 -- Otherwise the generic unit being processed is not the top
15404 -- level template. It is safe to capture of global references
15405 -- within the generic unit because at this point the top level
15406 -- copy is fully analyzed.
15412 -- Otherwise capture the global references without interference
15417 end Requires_Delayed_Save;
15419 ----------------------------------
15420 -- Save_References_In_Aggregate --
15421 ----------------------------------
15423 procedure Save_References_In_Aggregate (N : Node_Id) is
15425 Qual : Node_Id := Empty;
15426 Typ : Entity_Id := Empty;
15428 use Atree.Unchecked_Access;
15429 -- This code section is part of implementing an untyped tree
15430 -- traversal, so it needs direct access to node fields.
15433 N2 := Get_Associated_Node (N);
15435 if Present (N2) then
15438 -- In an instance within a generic, use the name of the actual
15439 -- and not the original generic parameter. If the actual is
15440 -- global in the current generic it must be preserved for its
15443 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15444 and then Present (Generic_Parent_Type (Parent (Typ)))
15446 Typ := Base_Type (Typ);
15447 Set_Etype (N2, Typ);
15451 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15452 Set_Associated_Node (N, Empty);
15454 -- If the aggregate is an actual in a call, it has been
15455 -- resolved in the current context, to some local type. The
15456 -- enclosing call may have been disambiguated by the aggregate,
15457 -- and this disambiguation might fail at instantiation time
15458 -- because the type to which the aggregate did resolve is not
15459 -- preserved. In order to preserve some of this information,
15460 -- wrap the aggregate in a qualified expression, using the id
15461 -- of its type. For further disambiguation we qualify the type
15462 -- name with its scope (if visible and not hidden by a local
15463 -- homograph) because both id's will have corresponding
15464 -- entities in an instance. This resolves most of the problems
15465 -- with missing type information on aggregates in instances.
15468 and then Nkind (N2) = Nkind (N)
15469 and then Nkind (Parent (N2)) in N_Subprogram_Call
15470 and then Present (Typ)
15471 and then Comes_From_Source (Typ)
15473 Nam := Make_Identifier (Loc, Chars (Typ));
15475 if Is_Immediately_Visible (Scope (Typ))
15477 (not In_Open_Scopes (Scope (Typ))
15478 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15481 Make_Selected_Component (Loc,
15483 Make_Identifier (Loc, Chars (Scope (Typ))),
15484 Selector_Name => Nam);
15488 Make_Qualified_Expression (Loc,
15489 Subtype_Mark => Nam,
15490 Expression => Relocate_Node (N));
15494 Save_Global_Descendant (Field1 (N));
15495 Save_Global_Descendant (Field2 (N));
15496 Save_Global_Descendant (Field3 (N));
15497 Save_Global_Descendant (Field5 (N));
15499 if Present (Qual) then
15502 end Save_References_In_Aggregate;
15504 ----------------------------------------------
15505 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15506 ----------------------------------------------
15508 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15510 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15513 elsif Nkind (N) = N_Operator_Symbol
15514 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15516 Change_Operator_Symbol_To_String_Literal (N);
15518 end Save_References_In_Char_Lit_Or_Op_Symbol;
15520 ------------------------------------
15521 -- Save_References_In_Descendants --
15522 ------------------------------------
15524 procedure Save_References_In_Descendants (N : Node_Id) is
15525 use Atree.Unchecked_Access;
15526 -- This code section is part of implementing an untyped tree
15527 -- traversal, so it needs direct access to node fields.
15530 Save_Global_Descendant (Field1 (N));
15531 Save_Global_Descendant (Field2 (N));
15532 Save_Global_Descendant (Field3 (N));
15533 Save_Global_Descendant (Field4 (N));
15534 Save_Global_Descendant (Field5 (N));
15535 end Save_References_In_Descendants;
15537 -----------------------------------
15538 -- Save_References_In_Identifier --
15539 -----------------------------------
15541 procedure Save_References_In_Identifier (N : Node_Id) is
15543 -- The node did not undergo a transformation
15545 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15547 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15548 Orig_N2_Parent : constant Node_Id :=
15549 Original_Node (Parent (Aux_N2));
15551 -- The parent of this identifier is a selected component
15552 -- which denotes a named number that was constant folded.
15553 -- Preserve the original name for ASIS and link the parent
15554 -- with its expanded name. The constant folding will be
15555 -- repeated in the instance.
15557 if Nkind (Parent (N)) = N_Selected_Component
15558 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15560 and then Is_Entity_Name (Orig_N2_Parent)
15561 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15562 and then Is_Global (Entity (Orig_N2_Parent))
15565 Set_Associated_Node
15566 (Parent (N), Original_Node (Parent (N2)));
15571 -- If this is a discriminant reference, always save it.
15572 -- It is used in the instance to find the corresponding
15573 -- discriminant positionally rather than by name.
15575 Set_Original_Discriminant
15576 (N, Original_Discriminant (Get_Associated_Node (N)));
15582 -- The analysis of the generic copy transformed the identifier
15583 -- into another construct. Propagate the changes to the template.
15586 N2 := Get_Associated_Node (N);
15588 -- The identifier denotes a call to a parameterless function.
15589 -- Mark the node as resolved when the function is external.
15591 if Nkind (N2) = N_Function_Call then
15592 E := Entity (Name (N2));
15594 if Present (E) and then Is_Global (E) then
15595 Set_Etype (N, Etype (N2));
15597 Set_Associated_Node (N, Empty);
15598 Set_Etype (N, Empty);
15601 -- The identifier denotes a named number that was constant
15602 -- folded. Preserve the original name for ASIS and undo the
15603 -- constant folding which will be repeated in the instance.
15605 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15606 and then Is_Entity_Name (Original_Node (N2))
15608 Set_Associated_Node (N, Original_Node (N2));
15611 -- The identifier resolved to a string literal. Propagate this
15612 -- information to the generic template.
15614 elsif Nkind (N2) = N_String_Literal then
15615 Rewrite (N, New_Copy (N2));
15617 -- The identifier is rewritten as a dereference if it is the
15618 -- prefix of an implicit dereference. Preserve the original
15619 -- tree as the analysis of the instance will expand the node
15620 -- again, but preserve the resolved entity if it is global.
15622 elsif Nkind (N2) = N_Explicit_Dereference then
15623 if Is_Entity_Name (Prefix (N2))
15624 and then Present (Entity (Prefix (N2)))
15625 and then Is_Global (Entity (Prefix (N2)))
15627 Set_Associated_Node (N, Prefix (N2));
15629 elsif Nkind (Prefix (N2)) = N_Function_Call
15630 and then Present (Entity (Name (Prefix (N2))))
15631 and then Is_Global (Entity (Name (Prefix (N2))))
15634 Make_Explicit_Dereference (Loc,
15636 Make_Function_Call (Loc,
15639 (Entity (Name (Prefix (N2))), Loc))));
15642 Set_Associated_Node (N, Empty);
15643 Set_Etype (N, Empty);
15646 -- The subtype mark of a nominally unconstrained object is
15647 -- rewritten as a subtype indication using the bounds of the
15648 -- expression. Recover the original subtype mark.
15650 elsif Nkind (N2) = N_Subtype_Indication
15651 and then Is_Entity_Name (Original_Node (N2))
15653 Set_Associated_Node (N, Original_Node (N2));
15657 end Save_References_In_Identifier;
15659 ---------------------------------
15660 -- Save_References_In_Operator --
15661 ---------------------------------
15663 procedure Save_References_In_Operator (N : Node_Id) is
15665 -- The node did not undergo a transformation
15667 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15668 if Nkind (N) = N_Op_Concat then
15669 Set_Is_Component_Left_Opnd (N,
15670 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15672 Set_Is_Component_Right_Opnd (N,
15673 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15678 -- The analysis of the generic copy transformed the operator into
15679 -- some other construct. Propagate the changes to the template if
15683 N2 := Get_Associated_Node (N);
15685 -- The operator resoved to a function call
15687 if Nkind (N2) = N_Function_Call then
15689 -- Add explicit qualifications in the generic template for
15690 -- all operands of universal type. This aids resolution by
15691 -- preserving the actual type of a literal or an attribute
15692 -- that yields a universal result.
15694 Qualify_Universal_Operands (N, N2);
15696 E := Entity (Name (N2));
15698 if Present (E) and then Is_Global (E) then
15699 Set_Etype (N, Etype (N2));
15701 Set_Associated_Node (N, Empty);
15702 Set_Etype (N, Empty);
15705 -- The operator was folded into a literal
15707 elsif Nkind_In (N2, N_Integer_Literal,
15711 if Present (Original_Node (N2))
15712 and then Nkind (Original_Node (N2)) = Nkind (N)
15714 -- Operation was constant-folded. Whenever possible,
15715 -- recover semantic information from unfolded node,
15718 Set_Associated_Node (N, Original_Node (N2));
15720 if Nkind (N) = N_Op_Concat then
15721 Set_Is_Component_Left_Opnd (N,
15722 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15723 Set_Is_Component_Right_Opnd (N,
15724 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15729 -- Propagate the constant folding back to the template
15732 Rewrite (N, New_Copy (N2));
15733 Set_Analyzed (N, False);
15736 -- The operator was folded into an enumeration literal. Retain
15737 -- the entity to avoid spurious ambiguities if it is overloaded
15738 -- at the point of instantiation or inlining.
15740 elsif Nkind (N2) = N_Identifier
15741 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15743 Rewrite (N, New_Copy (N2));
15744 Set_Analyzed (N, False);
15748 -- Complete the operands check if node has not been constant
15751 if Nkind (N) in N_Op then
15752 Save_Entity_Descendants (N);
15754 end Save_References_In_Operator;
15756 -------------------------------
15757 -- Save_References_In_Pragma --
15758 -------------------------------
15760 procedure Save_References_In_Pragma (Prag : Node_Id) is
15762 Do_Save : Boolean := True;
15764 use Atree.Unchecked_Access;
15765 -- This code section is part of implementing an untyped tree
15766 -- traversal, so it needs direct access to node fields.
15769 -- Do not save global references in pragmas generated from aspects
15770 -- because the pragmas will be regenerated at instantiation time.
15772 if From_Aspect_Specification (Prag) then
15775 -- The capture of global references within contract-related source
15776 -- pragmas associated with generic packages, subprograms or their
15777 -- respective bodies must be delayed due to timing of annotation
15778 -- analysis. Global references are still captured in routine
15779 -- Save_Global_References_In_Contract.
15781 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15782 if Is_Package_Contract_Annotation (Prag) then
15783 Context := Find_Related_Package_Or_Body (Prag);
15785 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15786 Context := Find_Related_Declaration_Or_Body (Prag);
15789 -- The use of Original_Node accounts for the case when the
15790 -- related context is generic template.
15792 if Requires_Delayed_Save (Original_Node (Context)) then
15797 -- For all other cases, save all global references within the
15798 -- descendants, but skip the following semantic fields:
15800 -- Field1 - Next_Pragma
15801 -- Field3 - Corresponding_Aspect
15802 -- Field5 - Next_Rep_Item
15805 Save_Global_Descendant (Field2 (Prag));
15806 Save_Global_Descendant (Field4 (Prag));
15808 end Save_References_In_Pragma;
15810 -- Start of processing for Save_References
15818 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15819 Save_References_In_Aggregate (N);
15821 -- Character literals, operator symbols
15823 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15824 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15826 -- Defining identifiers
15828 elsif Nkind (N) in N_Entity then
15833 elsif Nkind (N) = N_Identifier then
15834 Save_References_In_Identifier (N);
15838 elsif Nkind (N) in N_Op then
15839 Save_References_In_Operator (N);
15843 elsif Nkind (N) = N_Pragma then
15844 Save_References_In_Pragma (N);
15847 Save_References_In_Descendants (N);
15850 -- Save all global references found within the aspect specifications
15851 -- of the related node.
15853 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15855 -- The capture of global references within aspects associated with
15856 -- generic packages, subprograms or their bodies must be delayed
15857 -- due to timing of annotation analysis. Global references are
15858 -- still captured in routine Save_Global_References_In_Contract.
15860 if Requires_Delayed_Save (N) then
15863 -- Otherwise save all global references within the aspects
15866 Save_Global_References_In_Aspects (N);
15869 end Save_References;
15871 -- Start of processing for Save_Global_References
15874 Gen_Scope := Current_Scope;
15876 -- If the generic unit is a child unit, references to entities in the
15877 -- parent are treated as local, because they will be resolved anew in
15878 -- the context of the instance of the parent.
15880 while Is_Child_Unit (Gen_Scope)
15881 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15883 Gen_Scope := Scope (Gen_Scope);
15886 Save_References (Templ);
15887 end Save_Global_References;
15889 ---------------------------------------
15890 -- Save_Global_References_In_Aspects --
15891 ---------------------------------------
15893 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15898 Asp := First (Aspect_Specifications (N));
15899 while Present (Asp) loop
15900 Expr := Expression (Asp);
15902 if Present (Expr) then
15903 Save_Global_References (Expr);
15908 end Save_Global_References_In_Aspects;
15910 ------------------------------------------
15911 -- Set_Copied_Sloc_For_Inherited_Pragma --
15912 ------------------------------------------
15914 procedure Set_Copied_Sloc_For_Inherited_Pragma
15919 Create_Instantiation_Source (N, E,
15920 Inlined_Body => False,
15921 Inherited_Pragma => True,
15922 Factor => S_Adjustment);
15923 end Set_Copied_Sloc_For_Inherited_Pragma;
15925 --------------------------------------
15926 -- Set_Copied_Sloc_For_Inlined_Body --
15927 --------------------------------------
15929 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15931 Create_Instantiation_Source (N, E,
15932 Inlined_Body => True,
15933 Inherited_Pragma => False,
15934 Factor => S_Adjustment);
15935 end Set_Copied_Sloc_For_Inlined_Body;
15937 ---------------------
15938 -- Set_Instance_Of --
15939 ---------------------
15941 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15943 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15944 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15945 Generic_Renamings.Increment_Last;
15946 end Set_Instance_Of;
15948 --------------------
15949 -- Set_Next_Assoc --
15950 --------------------
15952 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15954 Generic_Renamings.Table (E).Next_In_HTable := Next;
15955 end Set_Next_Assoc;
15957 -------------------
15958 -- Start_Generic --
15959 -------------------
15961 procedure Start_Generic is
15963 -- ??? More things could be factored out in this routine.
15964 -- Should probably be done at a later stage.
15966 Generic_Flags.Append (Inside_A_Generic);
15967 Inside_A_Generic := True;
15969 Expander_Mode_Save_And_Set (False);
15972 ----------------------
15973 -- Set_Instance_Env --
15974 ----------------------
15976 -- WARNING: This routine manages SPARK regions
15978 procedure Set_Instance_Env
15979 (Gen_Unit : Entity_Id;
15980 Act_Unit : Entity_Id)
15982 Saved_AE : constant Boolean := Assertions_Enabled;
15983 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15984 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15985 -- Save the SPARK mode-related data because utilizing the configuration
15986 -- values of pragmas and switches will eliminate any previously set
15990 -- Regardless of the current mode, predefined units are analyzed in the
15991 -- most current Ada mode, and earlier version Ada checks do not apply
15992 -- to predefined units. Nothing needs to be done for non-internal units.
15993 -- These are always analyzed in the current mode.
15995 if In_Internal_Unit (Gen_Unit) then
15996 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15998 -- In Ada2012 we may want to enable assertions in an instance of a
15999 -- predefined unit, in which case we need to preserve the current
16000 -- setting for the Assertions_Enabled flag. This will become more
16001 -- critical when pre/postconditions are added to predefined units,
16002 -- as is already the case for some numeric libraries.
16004 if Ada_Version >= Ada_2012 then
16005 Assertions_Enabled := Saved_AE;
16008 -- Reinstall the SPARK_Mode which was in effect at the point of
16011 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16014 Current_Instantiated_Parent :=
16015 (Gen_Id => Gen_Unit,
16016 Act_Id => Act_Unit,
16017 Next_In_HTable => Assoc_Null);
16018 end Set_Instance_Env;
16024 procedure Switch_View (T : Entity_Id) is
16025 BT : constant Entity_Id := Base_Type (T);
16026 Priv_Elmt : Elmt_Id := No_Elmt;
16027 Priv_Sub : Entity_Id;
16030 -- T may be private but its base type may have been exchanged through
16031 -- some other occurrence, in which case there is nothing to switch
16032 -- besides T itself. Note that a private dependent subtype of a private
16033 -- type might not have been switched even if the base type has been,
16034 -- because of the last branch of Check_Private_View (see comment there).
16036 if not Is_Private_Type (BT) then
16037 Prepend_Elmt (Full_View (T), Exchanged_Views);
16038 Exchange_Declarations (T);
16042 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16044 if Present (Full_View (BT)) then
16045 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16046 Exchange_Declarations (BT);
16049 while Present (Priv_Elmt) loop
16050 Priv_Sub := (Node (Priv_Elmt));
16052 -- We avoid flipping the subtype if the Etype of its full view is
16053 -- private because this would result in a malformed subtype. This
16054 -- occurs when the Etype of the subtype full view is the full view of
16055 -- the base type (and since the base types were just switched, the
16056 -- subtype is pointing to the wrong view). This is currently the case
16057 -- for tagged record types, access types (maybe more?) and needs to
16058 -- be resolved. ???
16060 if Present (Full_View (Priv_Sub))
16061 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16063 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16064 Exchange_Declarations (Priv_Sub);
16067 Next_Elmt (Priv_Elmt);
16075 function True_Parent (N : Node_Id) return Node_Id is
16077 if Nkind (Parent (N)) = N_Subunit then
16078 return Parent (Corresponding_Stub (Parent (N)));
16084 -----------------------------
16085 -- Valid_Default_Attribute --
16086 -----------------------------
16088 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16089 Attr_Id : constant Attribute_Id :=
16090 Get_Attribute_Id (Attribute_Name (Def));
16091 T : constant Entity_Id := Entity (Prefix (Def));
16092 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16098 if No (T) or else T = Any_Id then
16103 F := First_Formal (Nam);
16104 while Present (F) loop
16105 Num_F := Num_F + 1;
16110 when Attribute_Adjacent
16111 | Attribute_Ceiling
16112 | Attribute_Copy_Sign
16114 | Attribute_Fraction
16115 | Attribute_Machine
16117 | Attribute_Remainder
16118 | Attribute_Rounding
16119 | Attribute_Unbiased_Rounding
16123 and then Is_Floating_Point_Type (T);
16125 when Attribute_Image
16129 | Attribute_Wide_Image
16130 | Attribute_Wide_Value
16132 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16137 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16139 when Attribute_Input =>
16140 OK := (Is_Fun and then Num_F = 1);
16142 when Attribute_Output
16146 OK := not Is_Fun and then Num_F = 2;
16154 ("attribute reference has wrong profile for subprogram", Def);
16156 end Valid_Default_Attribute;