1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
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
505 procedure Check_Formal_Package_Instance
506 (Formal_Pack : Entity_Id;
507 Actual_Pack : Entity_Id);
508 -- Verify that the actuals of the actual instance match the actuals of
509 -- the template for a formal package that is not declared with a box.
511 procedure Check_Forward_Instantiation (Decl : Node_Id);
512 -- If the generic is a local entity and the corresponding body has not
513 -- been seen yet, flag enclosing packages to indicate that it will be
514 -- elaborated after the generic body. Subprograms declared in the same
515 -- package cannot be inlined by the front-end because front-end inlining
516 -- requires a strict linear order of elaboration.
518 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
519 -- Check if some association between formals and actuals requires to make
520 -- visible primitives of a tagged type, and make those primitives visible.
521 -- Return the list of primitives whose visibility is modified (to restore
522 -- their visibility later through Restore_Hidden_Primitives). If no
523 -- candidate is found then return No_Elist.
525 procedure Check_Hidden_Child_Unit
527 Gen_Unit : Entity_Id;
528 Act_Decl_Id : Entity_Id);
529 -- If the generic unit is an implicit child instance within a parent
530 -- instance, we need to make an explicit test that it is not hidden by
531 -- a child instance of the same name and parent.
533 procedure Check_Generic_Actuals
534 (Instance : Entity_Id;
535 Is_Formal_Box : Boolean);
536 -- Similar to previous one. Check the actuals in the instantiation,
537 -- whose views can change between the point of instantiation and the point
538 -- of instantiation of the body. In addition, mark the generic renamings
539 -- as generic actuals, so that they are not compatible with other actuals.
540 -- Recurse on an actual that is a formal package whose declaration has
543 function Contains_Instance_Of
546 N : Node_Id) return Boolean;
547 -- Inner is instantiated within the generic Outer. Check whether Inner
548 -- directly or indirectly contains an instance of Outer or of one of its
549 -- parents, in the case of a subunit. Each generic unit holds a list of
550 -- the entities instantiated within (at any depth). This procedure
551 -- determines whether the set of such lists contains a cycle, i.e. an
552 -- illegal circular instantiation.
554 function Denotes_Formal_Package
556 On_Exit : Boolean := False;
557 Instance : Entity_Id := Empty) return Boolean;
558 -- Returns True if E is a formal package of an enclosing generic, or
559 -- the actual for such a formal in an enclosing instantiation. If such
560 -- a package is used as a formal in an nested generic, or as an actual
561 -- in a nested instantiation, the visibility of ITS formals should not
562 -- be modified. When called from within Restore_Private_Views, the flag
563 -- On_Exit is true, to indicate that the search for a possible enclosing
564 -- instance should ignore the current one. In that case Instance denotes
565 -- the declaration for which this is an actual. This declaration may be
566 -- an instantiation in the source, or the internal instantiation that
567 -- corresponds to the actual for a formal package.
569 function Earlier (N1, N2 : Node_Id) return Boolean;
570 -- Yields True if N1 and N2 appear in the same compilation unit,
571 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
572 -- traversal of the tree for the unit. Used to determine the placement
573 -- of freeze nodes for instance bodies that may depend on other instances.
575 function Find_Actual_Type
577 Gen_Type : Entity_Id) return Entity_Id;
578 -- When validating the actual types of a child instance, check whether
579 -- the formal is a formal type of the parent unit, and retrieve the current
580 -- actual for it. Typ is the entity in the analyzed formal type declaration
581 -- (component or index type of an array type, or designated type of an
582 -- access formal) and Gen_Type is the enclosing analyzed formal array
583 -- or access type. The desired actual may be a formal of a parent, or may
584 -- be declared in a formal package of a parent. In both cases it is a
585 -- generic actual type because it appears within a visible instance.
586 -- Finally, it may be declared in a parent unit without being a formal
587 -- of that unit, in which case it must be retrieved by visibility.
588 -- Ambiguities may still arise if two homonyms are declared in two formal
589 -- packages, and the prefix of the formal type may be needed to resolve
590 -- the ambiguity in the instance ???
592 procedure Freeze_Subprogram_Body
593 (Inst_Node : Node_Id;
595 Pack_Id : Entity_Id);
596 -- The generic body may appear textually after the instance, including
597 -- in the proper body of a stub, or within a different package instance.
598 -- Given that the instance can only be elaborated after the generic, we
599 -- place freeze_nodes for the instance and/or for packages that may enclose
600 -- the instance and the generic, so that the back-end can establish the
601 -- proper order of elaboration.
603 function Get_Associated_Node (N : Node_Id) return Node_Id;
604 -- In order to propagate semantic information back from the analyzed copy
605 -- to the original generic, we maintain links between selected nodes in the
606 -- generic and their corresponding copies. At the end of generic analysis,
607 -- the routine Save_Global_References traverses the generic tree, examines
608 -- the semantic information, and preserves the links to those nodes that
609 -- contain global information. At instantiation, the information from the
610 -- associated node is placed on the new copy, so that name resolution is
613 -- Three kinds of source nodes have associated nodes:
615 -- a) those that can reference (denote) entities, that is identifiers,
616 -- character literals, expanded_names, operator symbols, operators,
617 -- and attribute reference nodes. These nodes have an Entity field
618 -- and are the set of nodes that are in N_Has_Entity.
620 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
622 -- c) selected components (N_Selected_Component)
624 -- For the first class, the associated node preserves the entity if it is
625 -- global. If the generic contains nested instantiations, the associated
626 -- node itself has been recopied, and a chain of them must be followed.
628 -- For aggregates, the associated node allows retrieval of the type, which
629 -- may otherwise not appear in the generic. The view of this type may be
630 -- different between generic and instantiation, and the full view can be
631 -- installed before the instantiation is analyzed. For aggregates of type
632 -- extensions, the same view exchange may have to be performed for some of
633 -- the ancestor types, if their view is private at the point of
636 -- Nodes that are selected components in the parse tree may be rewritten
637 -- as expanded names after resolution, and must be treated as potential
638 -- entity holders, which is why they also have an Associated_Node.
640 -- Nodes that do not come from source, such as freeze nodes, do not appear
641 -- in the generic tree, and need not have an associated node.
643 -- The associated node is stored in the Associated_Node field. Note that
644 -- this field overlaps Entity, which is fine, because the whole point is
645 -- that we don't need or want the normal Entity field in this situation.
647 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
648 -- Traverse the Exchanged_Views list to see if a type was private
649 -- and has already been flipped during this phase of instantiation.
651 procedure Hide_Current_Scope;
652 -- When instantiating a generic child unit, the parent context must be
653 -- present, but the instance and all entities that may be generated
654 -- must be inserted in the current scope. We leave the current scope
655 -- on the stack, but make its entities invisible to avoid visibility
656 -- problems. This is reversed at the end of the instantiation. This is
657 -- not done for the instantiation of the bodies, which only require the
658 -- instances of the generic parents to be in scope.
660 function In_Same_Declarative_Part
662 Inst : Node_Id) return Boolean;
663 -- True if the instantiation Inst and the given freeze_node F_Node appear
664 -- within the same declarative part, ignoring subunits, but with no inter-
665 -- vening subprograms or concurrent units. Used to find the proper plave
666 -- for the freeze node of an instance, when the generic is declared in a
667 -- previous instance. If predicate is true, the freeze node of the instance
668 -- can be placed after the freeze node of the previous instance, Otherwise
669 -- it has to be placed at the end of the current declarative part.
671 function In_Main_Context (E : Entity_Id) return Boolean;
672 -- Check whether an instantiation is in the context of the main unit.
673 -- Used to determine whether its body should be elaborated to allow
674 -- front-end inlining.
676 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
677 -- Add the context clause of the unit containing a generic unit to a
678 -- compilation unit that is, or contains, an instantiation.
681 -- Establish environment for subsequent instantiation. Separated from
682 -- Save_Env because data-structures for visibility handling must be
683 -- initialized before call to Check_Generic_Child_Unit.
685 procedure Inline_Instance_Body
687 Gen_Unit : Entity_Id;
689 -- If front-end inlining is requested, instantiate the package body,
690 -- and preserve the visibility of its compilation unit, to insure
691 -- that successive instantiations succeed.
693 procedure Insert_Freeze_Node_For_Instance
696 -- N denotes a package or a subprogram instantiation and F_Node is the
697 -- associated freeze node. Insert the freeze node before the first source
698 -- body which follows immediately after N. If no such body is found, the
699 -- freeze node is inserted at the end of the declarative region which
702 procedure Install_Body
707 -- If the instantiation happens textually before the body of the generic,
708 -- the instantiation of the body must be analyzed after the generic body,
709 -- and not at the point of instantiation. Such early instantiations can
710 -- happen if the generic and the instance appear in a package declaration
711 -- because the generic body can only appear in the corresponding package
712 -- body. Early instantiations can also appear if generic, instance and
713 -- body are all in the declarative part of a subprogram or entry. Entities
714 -- of packages that are early instantiations are delayed, and their freeze
715 -- node appears after the generic body.
717 procedure Install_Formal_Packages (Par : Entity_Id);
718 -- Install the visible part of any formal of the parent that is a formal
719 -- package. Note that for the case of a formal package with a box, this
720 -- includes the formal part of the formal package (12.7(10/2)).
722 procedure Install_Hidden_Primitives
723 (Prims_List : in out Elist_Id;
726 -- Remove suffix 'P' from hidden primitives of Act_T to match the
727 -- visibility of primitives of Gen_T. The list of primitives to which
728 -- the suffix is removed is added to Prims_List to restore them later.
730 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
731 -- When compiling an instance of a child unit the parent (which is
732 -- itself an instance) is an enclosing scope that must be made
733 -- immediately visible. This procedure is also used to install the non-
734 -- generic parent of a generic child unit when compiling its body, so
735 -- that full views of types in the parent are made visible.
737 -- The functions Instantiate_XXX perform various legality checks and build
738 -- the declarations for instantiated generic parameters. In all of these
739 -- Formal is the entity in the generic unit, Actual is the entity of
740 -- expression in the generic associations, and Analyzed_Formal is the
741 -- formal in the generic copy, which contains the semantic information to
742 -- be used to validate the actual.
744 function Instantiate_Object
747 Analyzed_Formal : Node_Id) return List_Id;
749 function Instantiate_Type
752 Analyzed_Formal : Node_Id;
753 Actual_Decls : List_Id) return List_Id;
755 function Instantiate_Formal_Subprogram
758 Analyzed_Formal : Node_Id) return Node_Id;
760 function Instantiate_Formal_Package
763 Analyzed_Formal : Node_Id) return List_Id;
764 -- If the formal package is declared with a box, special visibility rules
765 -- apply to its formals: they are in the visible part of the package. This
766 -- is true in the declarative region of the formal package, that is to say
767 -- in the enclosing generic or instantiation. For an instantiation, the
768 -- parameters of the formal package are made visible in an explicit step.
769 -- Furthermore, if the actual has a visible USE clause, these formals must
770 -- be made potentially use-visible as well. On exit from the enclosing
771 -- instantiation, the reverse must be done.
773 -- For a formal package declared without a box, there are conformance rules
774 -- that apply to the actuals in the generic declaration and the actuals of
775 -- the actual package in the enclosing instantiation. The simplest way to
776 -- apply these rules is to repeat the instantiation of the formal package
777 -- in the context of the enclosing instance, and compare the generic
778 -- associations of this instantiation with those of the actual package.
779 -- This internal instantiation only needs to contain the renamings of the
780 -- formals: the visible and private declarations themselves need not be
783 -- In Ada 2005, the formal package may be only partially parameterized.
784 -- In that case the visibility step must make visible those actuals whose
785 -- corresponding formals were given with a box. A final complication
786 -- involves inherited operations from formal derived types, which must
787 -- be visible if the type is.
789 function Is_In_Main_Unit (N : Node_Id) return Boolean;
790 -- Test if given node is in the main unit
792 procedure Load_Parent_Of_Generic
795 Body_Optional : Boolean := False);
796 -- If the generic appears in a separate non-generic library unit, load the
797 -- corresponding body to retrieve the body of the generic. N is the node
798 -- for the generic instantiation, Spec is the generic package declaration.
800 -- Body_Optional is a flag that indicates that the body is being loaded to
801 -- ensure that temporaries are generated consistently when there are other
802 -- instances in the current declarative part that precede the one being
803 -- loaded. In that case a missing body is acceptable.
805 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
806 -- Within the generic part, entities in the formal package are
807 -- visible. To validate subsequent type declarations, indicate
808 -- the correspondence between the entities in the analyzed formal,
809 -- and the entities in the actual package. There are three packages
810 -- involved in the instantiation of a formal package: the parent
811 -- generic P1 which appears in the generic declaration, the fake
812 -- instantiation P2 which appears in the analyzed generic, and whose
813 -- visible entities may be used in subsequent formals, and the actual
814 -- P3 in the instance. To validate subsequent formals, me indicate
815 -- that the entities in P2 are mapped into those of P3. The mapping of
816 -- entities has to be done recursively for nested packages.
818 procedure Move_Freeze_Nodes
822 -- Freeze nodes can be generated in the analysis of a generic unit, but
823 -- will not be seen by the back-end. It is necessary to move those nodes
824 -- to the enclosing scope if they freeze an outer entity. We place them
825 -- at the end of the enclosing generic package, which is semantically
828 procedure Preanalyze_Actuals (N : Node_Id);
829 -- Analyze actuals to perform name resolution. Full resolution is done
830 -- later, when the expected types are known, but names have to be captured
831 -- before installing parents of generics, that are not visible for the
832 -- actuals themselves.
834 procedure Remove_Parent (In_Body : Boolean := False);
835 -- Reverse effect after instantiation of child is complete
837 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
838 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
841 procedure Save_Global_References_In_Aspects (N : Node_Id);
842 -- Save all global references found within the expressions of all aspects
843 -- that appear on node N.
845 procedure Set_Instance_Env
846 (Gen_Unit : Entity_Id;
847 Act_Unit : Entity_Id);
848 -- Save current instance on saved environment, to be used to determine
849 -- the global status of entities in nested instances. Part of Save_Env.
850 -- called after verifying that the generic unit is legal for the instance,
851 -- The procedure also examines whether the generic unit is a predefined
852 -- unit, in order to set configuration switches accordingly. As a result
853 -- the procedure must be called after analyzing and freezing the actuals.
855 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
856 -- Associate analyzed generic parameter with corresponding instance. Used
857 -- for semantic checks at instantiation time.
859 function True_Parent (N : Node_Id) return Node_Id;
860 -- For a subunit, return parent of corresponding stub, else return
863 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
864 -- Verify that an attribute that appears as the default for a formal
865 -- subprogram is a function or procedure with the correct profile.
867 -------------------------------------------
868 -- Data Structures for Generic Renamings --
869 -------------------------------------------
871 -- The map Generic_Renamings associates generic entities with their
872 -- corresponding actuals. Currently used to validate type instances. It
873 -- will eventually be used for all generic parameters to eliminate the
874 -- need for overload resolution in the instance.
876 type Assoc_Ptr is new Int;
878 Assoc_Null : constant Assoc_Ptr := -1;
883 Next_In_HTable : Assoc_Ptr;
886 package Generic_Renamings is new Table.Table
887 (Table_Component_Type => Assoc,
888 Table_Index_Type => Assoc_Ptr,
889 Table_Low_Bound => 0,
891 Table_Increment => 100,
892 Table_Name => "Generic_Renamings");
894 -- Variable to hold enclosing instantiation. When the environment is
895 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
897 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
899 -- Hash table for associations
901 HTable_Size : constant := 37;
902 type HTable_Range is range 0 .. HTable_Size - 1;
904 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
905 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
906 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
907 function Hash (F : Entity_Id) return HTable_Range;
909 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
910 Header_Num => HTable_Range,
912 Elmt_Ptr => Assoc_Ptr,
913 Null_Ptr => Assoc_Null,
914 Set_Next => Set_Next_Assoc,
917 Get_Key => Get_Gen_Id,
921 Exchanged_Views : Elist_Id;
922 -- This list holds the private views that have been exchanged during
923 -- instantiation to restore the visibility of the generic declaration.
924 -- (see comments above). After instantiation, the current visibility is
925 -- reestablished by means of a traversal of this list.
927 Hidden_Entities : Elist_Id;
928 -- This list holds the entities of the current scope that are removed
929 -- from immediate visibility when instantiating a child unit. Their
930 -- visibility is restored in Remove_Parent.
932 -- Because instantiations can be recursive, the following must be saved
933 -- on entry and restored on exit from an instantiation (spec or body).
934 -- This is done by the two procedures Save_Env and Restore_Env. For
935 -- package and subprogram instantiations (but not for the body instances)
936 -- the action of Save_Env is done in two steps: Init_Env is called before
937 -- Check_Generic_Child_Unit, because setting the parent instances requires
938 -- that the visibility data structures be properly initialized. Once the
939 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
941 Parent_Unit_Visible : Boolean := False;
942 -- Parent_Unit_Visible is used when the generic is a child unit, and
943 -- indicates whether the ultimate parent of the generic is visible in the
944 -- instantiation environment. It is used to reset the visibility of the
945 -- parent at the end of the instantiation (see Remove_Parent).
947 Instance_Parent_Unit : Entity_Id := Empty;
948 -- This records the ultimate parent unit of an instance of a generic
949 -- child unit and is used in conjunction with Parent_Unit_Visible to
950 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
952 type Instance_Env is record
953 Instantiated_Parent : Assoc;
954 Exchanged_Views : Elist_Id;
955 Hidden_Entities : Elist_Id;
956 Current_Sem_Unit : Unit_Number_Type;
957 Parent_Unit_Visible : Boolean := False;
958 Instance_Parent_Unit : Entity_Id := Empty;
959 Switches : Config_Switches_Type;
962 package Instance_Envs is new Table.Table (
963 Table_Component_Type => Instance_Env,
964 Table_Index_Type => Int,
965 Table_Low_Bound => 0,
967 Table_Increment => 100,
968 Table_Name => "Instance_Envs");
970 procedure Restore_Private_Views
971 (Pack_Id : Entity_Id;
972 Is_Package : Boolean := True);
973 -- Restore the private views of external types, and unmark the generic
974 -- renamings of actuals, so that they become compatible subtypes again.
975 -- For subprograms, Pack_Id is the package constructed to hold the
978 procedure Switch_View (T : Entity_Id);
979 -- Switch the partial and full views of a type and its private
980 -- dependents (i.e. its subtypes and derived types).
982 ------------------------------------
983 -- Structures for Error Reporting --
984 ------------------------------------
986 Instantiation_Node : Node_Id;
987 -- Used by subprograms that validate instantiation of formal parameters
988 -- where there might be no actual on which to place the error message.
989 -- Also used to locate the instantiation node for generic subunits.
991 Instantiation_Error : exception;
992 -- When there is a semantic error in the generic parameter matching,
993 -- there is no point in continuing the instantiation, because the
994 -- number of cascaded errors is unpredictable. This exception aborts
995 -- the instantiation process altogether.
997 S_Adjustment : Sloc_Adjustment;
998 -- Offset created for each node in an instantiation, in order to keep
999 -- track of the source position of the instantiation in each of its nodes.
1000 -- A subsequent semantic error or warning on a construct of the instance
1001 -- points to both places: the original generic node, and the point of
1002 -- instantiation. See Sinput and Sinput.L for additional details.
1004 ------------------------------------------------------------
1005 -- Data structure for keeping track when inside a Generic --
1006 ------------------------------------------------------------
1008 -- The following table is used to save values of the Inside_A_Generic
1009 -- flag (see spec of Sem) when they are saved by Start_Generic.
1011 package Generic_Flags is new Table.Table (
1012 Table_Component_Type => Boolean,
1013 Table_Index_Type => Int,
1014 Table_Low_Bound => 0,
1015 Table_Initial => 32,
1016 Table_Increment => 200,
1017 Table_Name => "Generic_Flags");
1019 ---------------------------
1020 -- Abandon_Instantiation --
1021 ---------------------------
1023 procedure Abandon_Instantiation (N : Node_Id) is
1025 Error_Msg_N ("\instantiation abandoned!", N);
1026 raise Instantiation_Error;
1027 end Abandon_Instantiation;
1029 --------------------------
1030 -- Analyze_Associations --
1031 --------------------------
1033 function Analyze_Associations
1036 F_Copy : List_Id) return List_Id
1038 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1039 Assoc : constant List_Id := New_List;
1040 Default_Actuals : constant List_Id := New_List;
1041 Gen_Unit : constant Entity_Id :=
1042 Defining_Entity (Parent (F_Copy));
1046 Analyzed_Formal : Node_Id;
1047 First_Named : Node_Id := Empty;
1051 Saved_Formal : Node_Id;
1053 Default_Formals : constant List_Id := New_List;
1054 -- If an Others_Choice is present, some of the formals may be defaulted.
1055 -- To simplify the treatment of visibility in an instance, we introduce
1056 -- individual defaults for each such formal. These defaults are
1057 -- appended to the list of associations and replace the Others_Choice.
1059 Found_Assoc : Node_Id;
1060 -- Association for the current formal being match. Empty if there are
1061 -- no remaining actuals, or if there is no named association with the
1062 -- name of the formal.
1064 Is_Named_Assoc : Boolean;
1065 Num_Matched : Int := 0;
1066 Num_Actuals : Int := 0;
1068 Others_Present : Boolean := False;
1069 Others_Choice : Node_Id := Empty;
1070 -- In Ada 2005, indicates partial parameterization of a formal
1071 -- package. As usual an other association must be last in the list.
1073 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1074 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
1075 -- cannot have a named association for it. AI05-0025 extends this rule
1076 -- to formals of formal packages by AI05-0025, and it also applies to
1077 -- box-initialized formals.
1079 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1080 -- Determine whether the parameter types and the return type of Subp
1081 -- are fully defined at the point of instantiation.
1083 function Matching_Actual
1085 A_F : Entity_Id) return Node_Id;
1086 -- Find actual that corresponds to a given a formal parameter. If the
1087 -- actuals are positional, return the next one, if any. If the actuals
1088 -- are named, scan the parameter associations to find the right one.
1089 -- A_F is the corresponding entity in the analyzed generic,which is
1090 -- placed on the selector name for ASIS use.
1092 -- In Ada 2005, a named association may be given with a box, in which
1093 -- case Matching_Actual sets Found_Assoc to the generic association,
1094 -- but return Empty for the actual itself. In this case the code below
1095 -- creates a corresponding declaration for the formal.
1097 function Partial_Parameterization return Boolean;
1098 -- Ada 2005: if no match is found for a given formal, check if the
1099 -- association for it includes a box, or whether the associations
1100 -- include an Others clause.
1102 procedure Process_Default (F : Entity_Id);
1103 -- Add a copy of the declaration of generic formal F to the list of
1104 -- associations, and add an explicit box association for F if there
1105 -- is none yet, and the default comes from an Others_Choice.
1107 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1108 -- Determine whether Subp renames one of the subprograms defined in the
1109 -- generated package Standard.
1111 procedure Set_Analyzed_Formal;
1112 -- Find the node in the generic copy that corresponds to a given formal.
1113 -- The semantic information on this node is used to perform legality
1114 -- checks on the actuals. Because semantic analysis can introduce some
1115 -- anonymous entities or modify the declaration node itself, the
1116 -- correspondence between the two lists is not one-one. In addition to
1117 -- anonymous types, the presence a formal equality will introduce an
1118 -- implicit declaration for the corresponding inequality.
1120 ----------------------------------------
1121 -- Check_Overloaded_Formal_Subprogram --
1122 ----------------------------------------
1124 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1125 Temp_Formal : Entity_Id;
1128 Temp_Formal := First (Formals);
1129 while Present (Temp_Formal) loop
1130 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1131 and then Temp_Formal /= Formal
1133 Chars (Defining_Unit_Name (Specification (Formal))) =
1134 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1136 if Present (Found_Assoc) then
1138 ("named association not allowed for overloaded formal",
1143 ("named association not allowed for overloaded formal",
1147 Abandon_Instantiation (Instantiation_Node);
1152 end Check_Overloaded_Formal_Subprogram;
1154 -------------------------------
1155 -- Has_Fully_Defined_Profile --
1156 -------------------------------
1158 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1159 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1160 -- Determine whethet type Typ is fully defined
1162 ---------------------------
1163 -- Is_Fully_Defined_Type --
1164 ---------------------------
1166 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1168 -- A private type without a full view is not fully defined
1170 if Is_Private_Type (Typ)
1171 and then No (Full_View (Typ))
1175 -- An incomplete type is never fully defined
1177 elsif Is_Incomplete_Type (Typ) then
1180 -- All other types are fully defined
1185 end Is_Fully_Defined_Type;
1187 -- Local declarations
1191 -- Start of processing for Has_Fully_Defined_Profile
1194 -- Check the parameters
1196 Param := First_Formal (Subp);
1197 while Present (Param) loop
1198 if not Is_Fully_Defined_Type (Etype (Param)) then
1202 Next_Formal (Param);
1205 -- Check the return type
1207 return Is_Fully_Defined_Type (Etype (Subp));
1208 end Has_Fully_Defined_Profile;
1210 ---------------------
1211 -- Matching_Actual --
1212 ---------------------
1214 function Matching_Actual
1216 A_F : Entity_Id) return Node_Id
1222 Is_Named_Assoc := False;
1224 -- End of list of purely positional parameters
1226 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1227 Found_Assoc := Empty;
1230 -- Case of positional parameter corresponding to current formal
1232 elsif No (Selector_Name (Actual)) then
1233 Found_Assoc := Actual;
1234 Act := Explicit_Generic_Actual_Parameter (Actual);
1235 Num_Matched := Num_Matched + 1;
1238 -- Otherwise scan list of named actuals to find the one with the
1239 -- desired name. All remaining actuals have explicit names.
1242 Is_Named_Assoc := True;
1243 Found_Assoc := Empty;
1247 while Present (Actual) loop
1248 if Chars (Selector_Name (Actual)) = Chars (F) then
1249 Set_Entity (Selector_Name (Actual), A_F);
1250 Set_Etype (Selector_Name (Actual), Etype (A_F));
1251 Generate_Reference (A_F, Selector_Name (Actual));
1252 Found_Assoc := Actual;
1253 Act := Explicit_Generic_Actual_Parameter (Actual);
1254 Num_Matched := Num_Matched + 1;
1262 -- Reset for subsequent searches. In most cases the named
1263 -- associations are in order. If they are not, we reorder them
1264 -- to avoid scanning twice the same actual. This is not just a
1265 -- question of efficiency: there may be multiple defaults with
1266 -- boxes that have the same name. In a nested instantiation we
1267 -- insert actuals for those defaults, and cannot rely on their
1268 -- names to disambiguate them.
1270 if Actual = First_Named then
1273 elsif Present (Actual) then
1274 Insert_Before (First_Named, Remove_Next (Prev));
1277 Actual := First_Named;
1280 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1281 Set_Used_As_Generic_Actual (Entity (Act));
1285 end Matching_Actual;
1287 ------------------------------
1288 -- Partial_Parameterization --
1289 ------------------------------
1291 function Partial_Parameterization return Boolean is
1293 return Others_Present
1294 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1295 end Partial_Parameterization;
1297 ---------------------
1298 -- Process_Default --
1299 ---------------------
1301 procedure Process_Default (F : Entity_Id) is
1302 Loc : constant Source_Ptr := Sloc (I_Node);
1303 F_Id : constant Entity_Id := Defining_Entity (F);
1309 -- Append copy of formal declaration to associations, and create new
1310 -- defining identifier for it.
1312 Decl := New_Copy_Tree (F);
1313 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1315 if Nkind (F) in N_Formal_Subprogram_Declaration then
1316 Set_Defining_Unit_Name (Specification (Decl), Id);
1319 Set_Defining_Identifier (Decl, Id);
1322 Append (Decl, Assoc);
1324 if No (Found_Assoc) then
1326 Make_Generic_Association (Loc,
1328 New_Occurrence_Of (Id, Loc),
1329 Explicit_Generic_Actual_Parameter => Empty);
1330 Set_Box_Present (Default);
1331 Append (Default, Default_Formals);
1333 end Process_Default;
1335 ---------------------------------
1336 -- Renames_Standard_Subprogram --
1337 ---------------------------------
1339 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1344 while Present (Id) loop
1345 if Scope (Id) = Standard_Standard then
1353 end Renames_Standard_Subprogram;
1355 -------------------------
1356 -- Set_Analyzed_Formal --
1357 -------------------------
1359 procedure Set_Analyzed_Formal is
1363 while Present (Analyzed_Formal) loop
1364 Kind := Nkind (Analyzed_Formal);
1366 case Nkind (Formal) is
1368 when N_Formal_Subprogram_Declaration =>
1369 exit when Kind in N_Formal_Subprogram_Declaration
1372 (Defining_Unit_Name (Specification (Formal))) =
1374 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1376 when N_Formal_Package_Declaration =>
1377 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1378 N_Generic_Package_Declaration,
1379 N_Package_Declaration);
1381 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1385 -- Skip freeze nodes, and nodes inserted to replace
1386 -- unrecognized pragmas.
1389 Kind not in N_Formal_Subprogram_Declaration
1390 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1394 and then Chars (Defining_Identifier (Formal)) =
1395 Chars (Defining_Identifier (Analyzed_Formal));
1398 Next (Analyzed_Formal);
1400 end Set_Analyzed_Formal;
1402 -- Start of processing for Analyze_Associations
1405 Actuals := Generic_Associations (I_Node);
1407 if Present (Actuals) then
1409 -- Check for an Others choice, indicating a partial parameterization
1410 -- for a formal package.
1412 Actual := First (Actuals);
1413 while Present (Actual) loop
1414 if Nkind (Actual) = N_Others_Choice then
1415 Others_Present := True;
1416 Others_Choice := Actual;
1418 if Present (Next (Actual)) then
1419 Error_Msg_N ("others must be last association", Actual);
1422 -- This subprogram is used both for formal packages and for
1423 -- instantiations. For the latter, associations must all be
1426 if Nkind (I_Node) /= N_Formal_Package_Declaration
1427 and then Comes_From_Source (I_Node)
1430 ("others association not allowed in an instance",
1434 -- In any case, nothing to do after the others association
1438 elsif Box_Present (Actual)
1439 and then Comes_From_Source (I_Node)
1440 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1443 ("box association not allowed in an instance", Actual);
1449 -- If named associations are present, save first named association
1450 -- (it may of course be Empty) to facilitate subsequent name search.
1452 First_Named := First (Actuals);
1453 while Present (First_Named)
1454 and then Nkind (First_Named) /= N_Others_Choice
1455 and then No (Selector_Name (First_Named))
1457 Num_Actuals := Num_Actuals + 1;
1462 Named := First_Named;
1463 while Present (Named) loop
1464 if Nkind (Named) /= N_Others_Choice
1465 and then No (Selector_Name (Named))
1467 Error_Msg_N ("invalid positional actual after named one", Named);
1468 Abandon_Instantiation (Named);
1471 -- A named association may lack an actual parameter, if it was
1472 -- introduced for a default subprogram that turns out to be local
1473 -- to the outer instantiation.
1475 if Nkind (Named) /= N_Others_Choice
1476 and then Present (Explicit_Generic_Actual_Parameter (Named))
1478 Num_Actuals := Num_Actuals + 1;
1484 if Present (Formals) then
1485 Formal := First_Non_Pragma (Formals);
1486 Analyzed_Formal := First_Non_Pragma (F_Copy);
1488 if Present (Actuals) then
1489 Actual := First (Actuals);
1491 -- All formals should have default values
1497 while Present (Formal) loop
1498 Set_Analyzed_Formal;
1499 Saved_Formal := Next_Non_Pragma (Formal);
1501 case Nkind (Formal) is
1502 when N_Formal_Object_Declaration =>
1505 (Defining_Identifier (Formal),
1506 Defining_Identifier (Analyzed_Formal));
1508 if No (Match) and then Partial_Parameterization then
1509 Process_Default (Formal);
1513 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1516 -- For a defaulted in_parameter, create an entry in the
1517 -- the list of defaulted actuals, for GNATProve use. Do
1518 -- not included these defaults for an instance nested
1519 -- within a generic, because the defaults are also used
1520 -- in the analysis of the enclosing generic, and only
1521 -- defaulted subprograms are relevant there.
1523 if No (Match) and then not Inside_A_Generic then
1524 Append_To (Default_Actuals,
1525 Make_Generic_Association (Sloc (I_Node),
1528 (Defining_Identifier (Formal), Sloc (I_Node)),
1529 Explicit_Generic_Actual_Parameter =>
1530 New_Copy_Tree (Default_Expression (Formal))));
1534 -- If the object is a call to an expression function, this
1535 -- is a freezing point for it.
1537 if Is_Entity_Name (Match)
1538 and then Present (Entity (Match))
1540 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1541 = N_Expression_Function
1543 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1546 when N_Formal_Type_Declaration =>
1549 (Defining_Identifier (Formal),
1550 Defining_Identifier (Analyzed_Formal));
1553 if Partial_Parameterization then
1554 Process_Default (Formal);
1557 Error_Msg_Sloc := Sloc (Gen_Unit);
1560 Instantiation_Node, Defining_Identifier (Formal));
1562 ("\in instantiation of & declared#",
1563 Instantiation_Node, Gen_Unit);
1564 Abandon_Instantiation (Instantiation_Node);
1571 (Formal, Match, Analyzed_Formal, Assoc),
1574 -- An instantiation is a freeze point for the actuals,
1575 -- unless this is a rewritten formal package, or the
1576 -- formal is an Ada 2012 formal incomplete type.
1578 if Nkind (I_Node) = N_Formal_Package_Declaration
1580 (Ada_Version >= Ada_2012
1582 Ekind (Defining_Identifier (Analyzed_Formal)) =
1588 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1592 -- A remote access-to-class-wide type is not a legal actual
1593 -- for a generic formal of an access type (E.2.2(17/2)).
1594 -- In GNAT an exception to this rule is introduced when
1595 -- the formal is marked as remote using implementation
1596 -- defined aspect/pragma Remote_Access_Type. In that case
1597 -- the actual must be remote as well.
1599 -- If the current instantiation is the construction of a
1600 -- local copy for a formal package the actuals may be
1601 -- defaulted, and there is no matching actual to check.
1603 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1605 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1606 N_Access_To_Object_Definition
1607 and then Present (Match)
1610 Formal_Ent : constant Entity_Id :=
1611 Defining_Identifier (Analyzed_Formal);
1613 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1614 = Is_Remote_Types (Formal_Ent)
1616 -- Remoteness of formal and actual match
1620 elsif Is_Remote_Types (Formal_Ent) then
1622 -- Remote formal, non-remote actual
1625 ("actual for& must be remote", Match, Formal_Ent);
1628 -- Non-remote formal, remote actual
1631 ("actual for& may not be remote",
1637 when N_Formal_Subprogram_Declaration =>
1640 (Defining_Unit_Name (Specification (Formal)),
1641 Defining_Unit_Name (Specification (Analyzed_Formal)));
1643 -- If the formal subprogram has the same name as another
1644 -- formal subprogram of the generic, then a named
1645 -- association is illegal (12.3(9)). Exclude named
1646 -- associations that are generated for a nested instance.
1649 and then Is_Named_Assoc
1650 and then Comes_From_Source (Found_Assoc)
1652 Check_Overloaded_Formal_Subprogram (Formal);
1655 -- If there is no corresponding actual, this may be case
1656 -- of partial parameterization, or else the formal has a
1657 -- default or a box.
1659 if No (Match) and then Partial_Parameterization then
1660 Process_Default (Formal);
1662 if Nkind (I_Node) = N_Formal_Package_Declaration then
1663 Check_Overloaded_Formal_Subprogram (Formal);
1668 Instantiate_Formal_Subprogram
1669 (Formal, Match, Analyzed_Formal));
1671 -- An instantiation is a freeze point for the actuals,
1672 -- unless this is a rewritten formal package.
1674 if Nkind (I_Node) /= N_Formal_Package_Declaration
1675 and then Nkind (Match) = N_Identifier
1676 and then Is_Subprogram (Entity (Match))
1678 -- The actual subprogram may rename a routine defined
1679 -- in Standard. Avoid freezing such renamings because
1680 -- subprograms coming from Standard cannot be frozen.
1683 not Renames_Standard_Subprogram (Entity (Match))
1685 -- If the actual subprogram comes from a different
1686 -- unit, it is already frozen, either by a body in
1687 -- that unit or by the end of the declarative part
1688 -- of the unit. This check avoids the freezing of
1689 -- subprograms defined in Standard which are used
1690 -- as generic actuals.
1692 and then In_Same_Code_Unit (Entity (Match), I_Node)
1693 and then Has_Fully_Defined_Profile (Entity (Match))
1695 -- Mark the subprogram as having a delayed freeze
1696 -- since this may be an out-of-order action.
1698 Set_Has_Delayed_Freeze (Entity (Match));
1699 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1703 -- If this is a nested generic, preserve default for later
1704 -- instantiations. We do this as well for GNATProve use,
1705 -- so that the list of generic associations is complete.
1707 if No (Match) and then Box_Present (Formal) then
1709 Subp : constant Entity_Id :=
1710 Defining_Unit_Name (Specification (Last (Assoc)));
1713 Append_To (Default_Actuals,
1714 Make_Generic_Association (Sloc (I_Node),
1716 New_Occurrence_Of (Subp, Sloc (I_Node)),
1717 Explicit_Generic_Actual_Parameter =>
1718 New_Occurrence_Of (Subp, Sloc (I_Node))));
1722 when N_Formal_Package_Declaration =>
1725 (Defining_Identifier (Formal),
1726 Defining_Identifier (Original_Node (Analyzed_Formal)));
1729 if Partial_Parameterization then
1730 Process_Default (Formal);
1733 Error_Msg_Sloc := Sloc (Gen_Unit);
1736 Instantiation_Node, Defining_Identifier (Formal));
1738 ("\in instantiation of & declared#",
1739 Instantiation_Node, Gen_Unit);
1741 Abandon_Instantiation (Instantiation_Node);
1747 (Instantiate_Formal_Package
1748 (Formal, Match, Analyzed_Formal),
1752 -- For use type and use package appearing in the generic part,
1753 -- we have already copied them, so we can just move them where
1754 -- they belong (we mustn't recopy them since this would mess up
1755 -- the Sloc values).
1757 when N_Use_Package_Clause |
1758 N_Use_Type_Clause =>
1759 if Nkind (Original_Node (I_Node)) =
1760 N_Formal_Package_Declaration
1762 Append (New_Copy_Tree (Formal), Assoc);
1765 Append (Formal, Assoc);
1769 raise Program_Error;
1773 Formal := Saved_Formal;
1774 Next_Non_Pragma (Analyzed_Formal);
1777 if Num_Actuals > Num_Matched then
1778 Error_Msg_Sloc := Sloc (Gen_Unit);
1780 if Present (Selector_Name (Actual)) then
1782 ("unmatched actual &", Actual, Selector_Name (Actual));
1784 ("\in instantiation of & declared#", Actual, Gen_Unit);
1787 ("unmatched actual in instantiation of & declared#",
1792 elsif Present (Actuals) then
1794 ("too many actuals in generic instantiation", Instantiation_Node);
1797 -- An instantiation freezes all generic actuals. The only exceptions
1798 -- to this are incomplete types and subprograms which are not fully
1799 -- defined at the point of instantiation.
1802 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1804 while Present (Elmt) loop
1805 Freeze_Before (I_Node, Node (Elmt));
1810 -- If there are default subprograms, normalize the tree by adding
1811 -- explicit associations for them. This is required if the instance
1812 -- appears within a generic.
1814 if not Is_Empty_List (Default_Actuals) then
1819 Default := First (Default_Actuals);
1820 while Present (Default) loop
1821 Mark_Rewrite_Insertion (Default);
1825 if No (Actuals) then
1826 Set_Generic_Associations (I_Node, Default_Actuals);
1828 Append_List_To (Actuals, Default_Actuals);
1833 -- If this is a formal package, normalize the parameter list by adding
1834 -- explicit box associations for the formals that are covered by an
1837 if not Is_Empty_List (Default_Formals) then
1838 Append_List (Default_Formals, Formals);
1842 end Analyze_Associations;
1844 -------------------------------
1845 -- Analyze_Formal_Array_Type --
1846 -------------------------------
1848 procedure Analyze_Formal_Array_Type
1849 (T : in out Entity_Id;
1855 -- Treated like a non-generic array declaration, with additional
1860 if Nkind (Def) = N_Constrained_Array_Definition then
1861 DSS := First (Discrete_Subtype_Definitions (Def));
1862 while Present (DSS) loop
1863 if Nkind_In (DSS, N_Subtype_Indication,
1865 N_Attribute_Reference)
1867 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1874 Array_Type_Declaration (T, Def);
1875 Set_Is_Generic_Type (Base_Type (T));
1877 if Ekind (Component_Type (T)) = E_Incomplete_Type
1878 and then No (Full_View (Component_Type (T)))
1880 Error_Msg_N ("premature usage of incomplete type", Def);
1882 -- Check that range constraint is not allowed on the component type
1883 -- of a generic formal array type (AARM 12.5.3(3))
1885 elsif Is_Internal (Component_Type (T))
1886 and then Present (Subtype_Indication (Component_Definition (Def)))
1887 and then Nkind (Original_Node
1888 (Subtype_Indication (Component_Definition (Def)))) =
1889 N_Subtype_Indication
1892 ("in a formal, a subtype indication can only be "
1893 & "a subtype mark (RM 12.5.3(3))",
1894 Subtype_Indication (Component_Definition (Def)));
1897 end Analyze_Formal_Array_Type;
1899 ---------------------------------------------
1900 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1901 ---------------------------------------------
1903 -- As for other generic types, we create a valid type representation with
1904 -- legal but arbitrary attributes, whose values are never considered
1905 -- static. For all scalar types we introduce an anonymous base type, with
1906 -- the same attributes. We choose the corresponding integer type to be
1907 -- Standard_Integer.
1908 -- Here and in other similar routines, the Sloc of the generated internal
1909 -- type must be the same as the sloc of the defining identifier of the
1910 -- formal type declaration, to provide proper source navigation.
1912 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1916 Loc : constant Source_Ptr := Sloc (Def);
1918 Base : constant Entity_Id :=
1920 (E_Decimal_Fixed_Point_Type,
1922 Sloc (Defining_Identifier (Parent (Def))), 'G');
1924 Int_Base : constant Entity_Id := Standard_Integer;
1925 Delta_Val : constant Ureal := Ureal_1;
1926 Digs_Val : constant Uint := Uint_6;
1928 function Make_Dummy_Bound return Node_Id;
1929 -- Return a properly typed universal real literal to use as a bound
1931 ----------------------
1932 -- Make_Dummy_Bound --
1933 ----------------------
1935 function Make_Dummy_Bound return Node_Id is
1936 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1938 Set_Etype (Bound, Universal_Real);
1940 end Make_Dummy_Bound;
1942 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1947 Set_Etype (Base, Base);
1948 Set_Size_Info (Base, Int_Base);
1949 Set_RM_Size (Base, RM_Size (Int_Base));
1950 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1951 Set_Digits_Value (Base, Digs_Val);
1952 Set_Delta_Value (Base, Delta_Val);
1953 Set_Small_Value (Base, Delta_Val);
1954 Set_Scalar_Range (Base,
1956 Low_Bound => Make_Dummy_Bound,
1957 High_Bound => Make_Dummy_Bound));
1959 Set_Is_Generic_Type (Base);
1960 Set_Parent (Base, Parent (Def));
1962 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1963 Set_Etype (T, Base);
1964 Set_Size_Info (T, Int_Base);
1965 Set_RM_Size (T, RM_Size (Int_Base));
1966 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1967 Set_Digits_Value (T, Digs_Val);
1968 Set_Delta_Value (T, Delta_Val);
1969 Set_Small_Value (T, Delta_Val);
1970 Set_Scalar_Range (T, Scalar_Range (Base));
1971 Set_Is_Constrained (T);
1973 Check_Restriction (No_Fixed_Point, Def);
1974 end Analyze_Formal_Decimal_Fixed_Point_Type;
1976 -------------------------------------------
1977 -- Analyze_Formal_Derived_Interface_Type --
1978 -------------------------------------------
1980 procedure Analyze_Formal_Derived_Interface_Type
1985 Loc : constant Source_Ptr := Sloc (Def);
1988 -- Rewrite as a type declaration of a derived type. This ensures that
1989 -- the interface list and primitive operations are properly captured.
1992 Make_Full_Type_Declaration (Loc,
1993 Defining_Identifier => T,
1994 Type_Definition => Def));
1996 Set_Is_Generic_Type (T);
1997 end Analyze_Formal_Derived_Interface_Type;
1999 ---------------------------------
2000 -- Analyze_Formal_Derived_Type --
2001 ---------------------------------
2003 procedure Analyze_Formal_Derived_Type
2008 Loc : constant Source_Ptr := Sloc (Def);
2009 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2013 Set_Is_Generic_Type (T);
2015 if Private_Present (Def) then
2017 Make_Private_Extension_Declaration (Loc,
2018 Defining_Identifier => T,
2019 Discriminant_Specifications => Discriminant_Specifications (N),
2020 Unknown_Discriminants_Present => Unk_Disc,
2021 Subtype_Indication => Subtype_Mark (Def),
2022 Interface_List => Interface_List (Def));
2024 Set_Abstract_Present (New_N, Abstract_Present (Def));
2025 Set_Limited_Present (New_N, Limited_Present (Def));
2026 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2030 Make_Full_Type_Declaration (Loc,
2031 Defining_Identifier => T,
2032 Discriminant_Specifications =>
2033 Discriminant_Specifications (Parent (T)),
2035 Make_Derived_Type_Definition (Loc,
2036 Subtype_Indication => Subtype_Mark (Def)));
2038 Set_Abstract_Present
2039 (Type_Definition (New_N), Abstract_Present (Def));
2041 (Type_Definition (New_N), Limited_Present (Def));
2048 if not Is_Composite_Type (T) then
2050 ("unknown discriminants not allowed for elementary types", N);
2052 Set_Has_Unknown_Discriminants (T);
2053 Set_Is_Constrained (T, False);
2057 -- If the parent type has a known size, so does the formal, which makes
2058 -- legal representation clauses that involve the formal.
2060 Set_Size_Known_At_Compile_Time
2061 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2062 end Analyze_Formal_Derived_Type;
2064 ----------------------------------
2065 -- Analyze_Formal_Discrete_Type --
2066 ----------------------------------
2068 -- The operations defined for a discrete types are those of an enumeration
2069 -- type. The size is set to an arbitrary value, for use in analyzing the
2072 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2073 Loc : constant Source_Ptr := Sloc (Def);
2077 Base : constant Entity_Id :=
2079 (E_Floating_Point_Type, Current_Scope,
2080 Sloc (Defining_Identifier (Parent (Def))), 'G');
2084 Set_Ekind (T, E_Enumeration_Subtype);
2085 Set_Etype (T, Base);
2088 Set_Is_Generic_Type (T);
2089 Set_Is_Constrained (T);
2091 -- For semantic analysis, the bounds of the type must be set to some
2092 -- non-static value. The simplest is to create attribute nodes for those
2093 -- bounds, that refer to the type itself. These bounds are never
2094 -- analyzed but serve as place-holders.
2097 Make_Attribute_Reference (Loc,
2098 Attribute_Name => Name_First,
2099 Prefix => New_Occurrence_Of (T, Loc));
2103 Make_Attribute_Reference (Loc,
2104 Attribute_Name => Name_Last,
2105 Prefix => New_Occurrence_Of (T, Loc));
2108 Set_Scalar_Range (T,
2113 Set_Ekind (Base, E_Enumeration_Type);
2114 Set_Etype (Base, Base);
2115 Init_Size (Base, 8);
2116 Init_Alignment (Base);
2117 Set_Is_Generic_Type (Base);
2118 Set_Scalar_Range (Base, Scalar_Range (T));
2119 Set_Parent (Base, Parent (Def));
2120 end Analyze_Formal_Discrete_Type;
2122 ----------------------------------
2123 -- Analyze_Formal_Floating_Type --
2124 ---------------------------------
2126 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2127 Base : constant Entity_Id :=
2129 (E_Floating_Point_Type, Current_Scope,
2130 Sloc (Defining_Identifier (Parent (Def))), 'G');
2133 -- The various semantic attributes are taken from the predefined type
2134 -- Float, just so that all of them are initialized. Their values are
2135 -- never used because no constant folding or expansion takes place in
2136 -- the generic itself.
2139 Set_Ekind (T, E_Floating_Point_Subtype);
2140 Set_Etype (T, Base);
2141 Set_Size_Info (T, (Standard_Float));
2142 Set_RM_Size (T, RM_Size (Standard_Float));
2143 Set_Digits_Value (T, Digits_Value (Standard_Float));
2144 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2145 Set_Is_Constrained (T);
2147 Set_Is_Generic_Type (Base);
2148 Set_Etype (Base, Base);
2149 Set_Size_Info (Base, (Standard_Float));
2150 Set_RM_Size (Base, RM_Size (Standard_Float));
2151 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2152 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2153 Set_Parent (Base, Parent (Def));
2155 Check_Restriction (No_Floating_Point, Def);
2156 end Analyze_Formal_Floating_Type;
2158 -----------------------------------
2159 -- Analyze_Formal_Interface_Type;--
2160 -----------------------------------
2162 procedure Analyze_Formal_Interface_Type
2167 Loc : constant Source_Ptr := Sloc (N);
2172 Make_Full_Type_Declaration (Loc,
2173 Defining_Identifier => T,
2174 Type_Definition => Def);
2178 Set_Is_Generic_Type (T);
2179 end Analyze_Formal_Interface_Type;
2181 ---------------------------------
2182 -- Analyze_Formal_Modular_Type --
2183 ---------------------------------
2185 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2187 -- Apart from their entity kind, generic modular types are treated like
2188 -- signed integer types, and have the same attributes.
2190 Analyze_Formal_Signed_Integer_Type (T, Def);
2191 Set_Ekind (T, E_Modular_Integer_Subtype);
2192 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2194 end Analyze_Formal_Modular_Type;
2196 ---------------------------------------
2197 -- Analyze_Formal_Object_Declaration --
2198 ---------------------------------------
2200 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2201 E : constant Node_Id := Default_Expression (N);
2202 Id : constant Node_Id := Defining_Identifier (N);
2209 -- Determine the mode of the formal object
2211 if Out_Present (N) then
2212 K := E_Generic_In_Out_Parameter;
2214 if not In_Present (N) then
2215 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2219 K := E_Generic_In_Parameter;
2222 if Present (Subtype_Mark (N)) then
2223 Find_Type (Subtype_Mark (N));
2224 T := Entity (Subtype_Mark (N));
2226 -- Verify that there is no redundant null exclusion
2228 if Null_Exclusion_Present (N) then
2229 if not Is_Access_Type (T) then
2231 ("null exclusion can only apply to an access type", N);
2233 elsif Can_Never_Be_Null (T) then
2235 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2239 -- Ada 2005 (AI-423): Formal object with an access definition
2242 Check_Access_Definition (N);
2243 T := Access_Definition
2245 N => Access_Definition (N));
2248 if Ekind (T) = E_Incomplete_Type then
2250 Error_Node : Node_Id;
2253 if Present (Subtype_Mark (N)) then
2254 Error_Node := Subtype_Mark (N);
2256 Check_Access_Definition (N);
2257 Error_Node := Access_Definition (N);
2260 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2264 if K = E_Generic_In_Parameter then
2266 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2268 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2270 ("generic formal of mode IN must not be of limited type", N);
2271 Explain_Limited_Type (T, N);
2274 if Is_Abstract_Type (T) then
2276 ("generic formal of mode IN must not be of abstract type", N);
2280 Preanalyze_Spec_Expression (E, T);
2282 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2284 ("initialization not allowed for limited types", E);
2285 Explain_Limited_Type (T, E);
2292 -- Case of generic IN OUT parameter
2295 -- If the formal has an unconstrained type, construct its actual
2296 -- subtype, as is done for subprogram formals. In this fashion, all
2297 -- its uses can refer to specific bounds.
2302 if (Is_Array_Type (T) and then not Is_Constrained (T))
2303 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2306 Non_Freezing_Ref : constant Node_Id :=
2307 New_Occurrence_Of (Id, Sloc (Id));
2311 -- Make sure the actual subtype doesn't generate bogus freezing
2313 Set_Must_Not_Freeze (Non_Freezing_Ref);
2314 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2315 Insert_Before_And_Analyze (N, Decl);
2316 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2319 Set_Actual_Subtype (Id, T);
2324 ("initialization not allowed for `IN OUT` formals", N);
2328 if Has_Aspects (N) then
2329 Analyze_Aspect_Specifications (N, Id);
2331 end Analyze_Formal_Object_Declaration;
2333 ----------------------------------------------
2334 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2335 ----------------------------------------------
2337 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2341 Loc : constant Source_Ptr := Sloc (Def);
2342 Base : constant Entity_Id :=
2344 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2345 Sloc (Defining_Identifier (Parent (Def))), 'G');
2348 -- The semantic attributes are set for completeness only, their values
2349 -- will never be used, since all properties of the type are non-static.
2352 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2353 Set_Etype (T, Base);
2354 Set_Size_Info (T, Standard_Integer);
2355 Set_RM_Size (T, RM_Size (Standard_Integer));
2356 Set_Small_Value (T, Ureal_1);
2357 Set_Delta_Value (T, Ureal_1);
2358 Set_Scalar_Range (T,
2360 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2361 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2362 Set_Is_Constrained (T);
2364 Set_Is_Generic_Type (Base);
2365 Set_Etype (Base, Base);
2366 Set_Size_Info (Base, Standard_Integer);
2367 Set_RM_Size (Base, RM_Size (Standard_Integer));
2368 Set_Small_Value (Base, Ureal_1);
2369 Set_Delta_Value (Base, Ureal_1);
2370 Set_Scalar_Range (Base, Scalar_Range (T));
2371 Set_Parent (Base, Parent (Def));
2373 Check_Restriction (No_Fixed_Point, Def);
2374 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2376 ----------------------------------------
2377 -- Analyze_Formal_Package_Declaration --
2378 ----------------------------------------
2380 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2381 Loc : constant Source_Ptr := Sloc (N);
2382 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2384 Gen_Id : constant Node_Id := Name (N);
2386 Gen_Unit : Entity_Id;
2388 Parent_Installed : Boolean := False;
2390 Parent_Instance : Entity_Id;
2391 Renaming_In_Par : Entity_Id;
2392 Associations : Boolean := True;
2394 Vis_Prims_List : Elist_Id := No_Elist;
2395 -- List of primitives made temporarily visible in the instantiation
2396 -- to match the visibility of the formal type
2398 function Build_Local_Package return Node_Id;
2399 -- The formal package is rewritten so that its parameters are replaced
2400 -- with corresponding declarations. For parameters with bona fide
2401 -- associations these declarations are created by Analyze_Associations
2402 -- as for a regular instantiation. For boxed parameters, we preserve
2403 -- the formal declarations and analyze them, in order to introduce
2404 -- entities of the right kind in the environment of the formal.
2406 -------------------------
2407 -- Build_Local_Package --
2408 -------------------------
2410 function Build_Local_Package return Node_Id is
2412 Pack_Decl : Node_Id;
2415 -- Within the formal, the name of the generic package is a renaming
2416 -- of the formal (as for a regular instantiation).
2419 Make_Package_Declaration (Loc,
2422 (Specification (Original_Node (Gen_Decl)),
2423 Empty, Instantiating => True));
2426 Make_Package_Renaming_Declaration (Loc,
2427 Defining_Unit_Name =>
2428 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2429 Name => New_Occurrence_Of (Formal, Loc));
2431 if Nkind (Gen_Id) = N_Identifier
2432 and then Chars (Gen_Id) = Chars (Pack_Id)
2435 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2438 -- If the formal is declared with a box, or with an others choice,
2439 -- create corresponding declarations for all entities in the formal
2440 -- part, so that names with the proper types are available in the
2441 -- specification of the formal package.
2443 -- On the other hand, if there are no associations, then all the
2444 -- formals must have defaults, and this will be checked by the
2445 -- call to Analyze_Associations.
2448 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2451 Formal_Decl : Node_Id;
2454 -- TBA : for a formal package, need to recurse ???
2459 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2460 while Present (Formal_Decl) loop
2462 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2467 -- If generic associations are present, use Analyze_Associations to
2468 -- create the proper renaming declarations.
2472 Act_Tree : constant Node_Id :=
2474 (Original_Node (Gen_Decl), Empty,
2475 Instantiating => True);
2478 Generic_Renamings.Set_Last (0);
2479 Generic_Renamings_HTable.Reset;
2480 Instantiation_Node := N;
2483 Analyze_Associations
2484 (I_Node => Original_Node (N),
2485 Formals => Generic_Formal_Declarations (Act_Tree),
2486 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2488 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2492 Append (Renaming, To => Decls);
2494 -- Add generated declarations ahead of local declarations in
2497 if No (Visible_Declarations (Specification (Pack_Decl))) then
2498 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2501 (First (Visible_Declarations (Specification (Pack_Decl))),
2506 end Build_Local_Package;
2508 -- Start of processing for Analyze_Formal_Package_Declaration
2511 Check_Text_IO_Special_Unit (Gen_Id);
2514 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2515 Gen_Unit := Entity (Gen_Id);
2517 -- Check for a formal package that is a package renaming
2519 if Present (Renamed_Object (Gen_Unit)) then
2521 -- Indicate that unit is used, before replacing it with renamed
2522 -- entity for use below.
2524 if In_Extended_Main_Source_Unit (N) then
2525 Set_Is_Instantiated (Gen_Unit);
2526 Generate_Reference (Gen_Unit, N);
2529 Gen_Unit := Renamed_Object (Gen_Unit);
2532 if Ekind (Gen_Unit) /= E_Generic_Package then
2533 Error_Msg_N ("expect generic package name", Gen_Id);
2537 elsif Gen_Unit = Current_Scope then
2539 ("generic package cannot be used as a formal package of itself",
2544 elsif In_Open_Scopes (Gen_Unit) then
2545 if Is_Compilation_Unit (Gen_Unit)
2546 and then Is_Child_Unit (Current_Scope)
2548 -- Special-case the error when the formal is a parent, and
2549 -- continue analysis to minimize cascaded errors.
2552 ("generic parent cannot be used as formal package "
2553 & "of a child unit", Gen_Id);
2557 ("generic package cannot be used as a formal package "
2558 & "within itself", Gen_Id);
2564 -- Check that name of formal package does not hide name of generic,
2565 -- or its leading prefix. This check must be done separately because
2566 -- the name of the generic has already been analyzed.
2569 Gen_Name : Entity_Id;
2573 while Nkind (Gen_Name) = N_Expanded_Name loop
2574 Gen_Name := Prefix (Gen_Name);
2577 if Chars (Gen_Name) = Chars (Pack_Id) then
2579 ("& is hidden within declaration of formal package",
2585 or else No (Generic_Associations (N))
2586 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2588 Associations := False;
2591 -- If there are no generic associations, the generic parameters appear
2592 -- as local entities and are instantiated like them. We copy the generic
2593 -- package declaration as if it were an instantiation, and analyze it
2594 -- like a regular package, except that we treat the formals as
2595 -- additional visible components.
2597 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2599 if In_Extended_Main_Source_Unit (N) then
2600 Set_Is_Instantiated (Gen_Unit);
2601 Generate_Reference (Gen_Unit, N);
2604 Formal := New_Copy (Pack_Id);
2605 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2608 -- Make local generic without formals. The formals will be replaced
2609 -- with internal declarations.
2611 New_N := Build_Local_Package;
2613 -- If there are errors in the parameter list, Analyze_Associations
2614 -- raises Instantiation_Error. Patch the declaration to prevent
2615 -- further exception propagation.
2618 when Instantiation_Error =>
2620 Enter_Name (Formal);
2621 Set_Ekind (Formal, E_Variable);
2622 Set_Etype (Formal, Any_Type);
2623 Restore_Hidden_Primitives (Vis_Prims_List);
2625 if Parent_Installed then
2633 Set_Defining_Unit_Name (Specification (New_N), Formal);
2634 Set_Generic_Parent (Specification (N), Gen_Unit);
2635 Set_Instance_Env (Gen_Unit, Formal);
2636 Set_Is_Generic_Instance (Formal);
2638 Enter_Name (Formal);
2639 Set_Ekind (Formal, E_Package);
2640 Set_Etype (Formal, Standard_Void_Type);
2641 Set_Inner_Instances (Formal, New_Elmt_List);
2642 Push_Scope (Formal);
2644 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2646 -- Similarly, we have to make the name of the formal visible in the
2647 -- parent instance, to resolve properly fully qualified names that
2648 -- may appear in the generic unit. The parent instance has been
2649 -- placed on the scope stack ahead of the current scope.
2651 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2654 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2655 Set_Ekind (Renaming_In_Par, E_Package);
2656 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2657 Set_Scope (Renaming_In_Par, Parent_Instance);
2658 Set_Parent (Renaming_In_Par, Parent (Formal));
2659 Set_Renamed_Object (Renaming_In_Par, Formal);
2660 Append_Entity (Renaming_In_Par, Parent_Instance);
2663 Analyze (Specification (N));
2665 -- The formals for which associations are provided are not visible
2666 -- outside of the formal package. The others are still declared by a
2667 -- formal parameter declaration.
2669 -- If there are no associations, the only local entity to hide is the
2670 -- generated package renaming itself.
2676 E := First_Entity (Formal);
2677 while Present (E) loop
2678 if Associations and then not Is_Generic_Formal (E) then
2682 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2691 End_Package_Scope (Formal);
2692 Restore_Hidden_Primitives (Vis_Prims_List);
2694 if Parent_Installed then
2700 -- Inside the generic unit, the formal package is a regular package, but
2701 -- no body is needed for it. Note that after instantiation, the defining
2702 -- unit name we need is in the new tree and not in the original (see
2703 -- Package_Instantiation). A generic formal package is an instance, and
2704 -- can be used as an actual for an inner instance.
2706 Set_Has_Completion (Formal, True);
2708 -- Add semantic information to the original defining identifier.
2711 Set_Ekind (Pack_Id, E_Package);
2712 Set_Etype (Pack_Id, Standard_Void_Type);
2713 Set_Scope (Pack_Id, Scope (Formal));
2714 Set_Has_Completion (Pack_Id, True);
2717 if Has_Aspects (N) then
2718 Analyze_Aspect_Specifications (N, Pack_Id);
2720 end Analyze_Formal_Package_Declaration;
2722 ---------------------------------
2723 -- Analyze_Formal_Private_Type --
2724 ---------------------------------
2726 procedure Analyze_Formal_Private_Type
2732 New_Private_Type (N, T, Def);
2734 -- Set the size to an arbitrary but legal value
2736 Set_Size_Info (T, Standard_Integer);
2737 Set_RM_Size (T, RM_Size (Standard_Integer));
2738 end Analyze_Formal_Private_Type;
2740 ------------------------------------
2741 -- Analyze_Formal_Incomplete_Type --
2742 ------------------------------------
2744 procedure Analyze_Formal_Incomplete_Type
2750 Set_Ekind (T, E_Incomplete_Type);
2752 Set_Private_Dependents (T, New_Elmt_List);
2754 if Tagged_Present (Def) then
2755 Set_Is_Tagged_Type (T);
2756 Make_Class_Wide_Type (T);
2757 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2759 end Analyze_Formal_Incomplete_Type;
2761 ----------------------------------------
2762 -- Analyze_Formal_Signed_Integer_Type --
2763 ----------------------------------------
2765 procedure Analyze_Formal_Signed_Integer_Type
2769 Base : constant Entity_Id :=
2771 (E_Signed_Integer_Type,
2773 Sloc (Defining_Identifier (Parent (Def))), 'G');
2778 Set_Ekind (T, E_Signed_Integer_Subtype);
2779 Set_Etype (T, Base);
2780 Set_Size_Info (T, Standard_Integer);
2781 Set_RM_Size (T, RM_Size (Standard_Integer));
2782 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2783 Set_Is_Constrained (T);
2785 Set_Is_Generic_Type (Base);
2786 Set_Size_Info (Base, Standard_Integer);
2787 Set_RM_Size (Base, RM_Size (Standard_Integer));
2788 Set_Etype (Base, Base);
2789 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2790 Set_Parent (Base, Parent (Def));
2791 end Analyze_Formal_Signed_Integer_Type;
2793 -------------------------------------------
2794 -- Analyze_Formal_Subprogram_Declaration --
2795 -------------------------------------------
2797 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2798 Spec : constant Node_Id := Specification (N);
2799 Def : constant Node_Id := Default_Name (N);
2800 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2808 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2809 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2813 Analyze_Subprogram_Declaration (N);
2814 Set_Is_Formal_Subprogram (Nam);
2815 Set_Has_Completion (Nam);
2817 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2818 Set_Is_Abstract_Subprogram (Nam);
2819 Set_Is_Dispatching_Operation (Nam);
2822 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2824 if No (Ctrl_Type) then
2826 ("abstract formal subprogram must have a controlling type",
2829 elsif Ada_Version >= Ada_2012
2830 and then Is_Incomplete_Type (Ctrl_Type)
2833 ("controlling type of abstract formal subprogram cannot "
2834 & "be incomplete type", N, Ctrl_Type);
2837 Check_Controlling_Formals (Ctrl_Type, Nam);
2842 -- Default name is resolved at the point of instantiation
2844 if Box_Present (N) then
2847 -- Else default is bound at the point of generic declaration
2849 elsif Present (Def) then
2850 if Nkind (Def) = N_Operator_Symbol then
2851 Find_Direct_Name (Def);
2853 elsif Nkind (Def) /= N_Attribute_Reference then
2857 -- For an attribute reference, analyze the prefix and verify
2858 -- that it has the proper profile for the subprogram.
2860 Analyze (Prefix (Def));
2861 Valid_Default_Attribute (Nam, Def);
2865 -- Default name may be overloaded, in which case the interpretation
2866 -- with the correct profile must be selected, as for a renaming.
2867 -- If the definition is an indexed component, it must denote a
2868 -- member of an entry family. If it is a selected component, it
2869 -- can be a protected operation.
2871 if Etype (Def) = Any_Type then
2874 elsif Nkind (Def) = N_Selected_Component then
2875 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2876 Error_Msg_N ("expect valid subprogram name as default", Def);
2879 elsif Nkind (Def) = N_Indexed_Component then
2880 if Is_Entity_Name (Prefix (Def)) then
2881 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2882 Error_Msg_N ("expect valid subprogram name as default", Def);
2885 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2886 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2889 Error_Msg_N ("expect valid subprogram name as default", Def);
2893 Error_Msg_N ("expect valid subprogram name as default", Def);
2897 elsif Nkind (Def) = N_Character_Literal then
2899 -- Needs some type checks: subprogram should be parameterless???
2901 Resolve (Def, (Etype (Nam)));
2903 elsif not Is_Entity_Name (Def)
2904 or else not Is_Overloadable (Entity (Def))
2906 Error_Msg_N ("expect valid subprogram name as default", Def);
2909 elsif not Is_Overloaded (Def) then
2910 Subp := Entity (Def);
2913 Error_Msg_N ("premature usage of formal subprogram", Def);
2915 elsif not Entity_Matches_Spec (Subp, Nam) then
2916 Error_Msg_N ("no visible entity matches specification", Def);
2919 -- More than one interpretation, so disambiguate as for a renaming
2924 I1 : Interp_Index := 0;
2930 Get_First_Interp (Def, I, It);
2931 while Present (It.Nam) loop
2932 if Entity_Matches_Spec (It.Nam, Nam) then
2933 if Subp /= Any_Id then
2934 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2936 if It1 = No_Interp then
2937 Error_Msg_N ("ambiguous default subprogram", Def);
2950 Get_Next_Interp (I, It);
2954 if Subp /= Any_Id then
2956 -- Subprogram found, generate reference to it
2958 Set_Entity (Def, Subp);
2959 Generate_Reference (Subp, Def);
2962 Error_Msg_N ("premature usage of formal subprogram", Def);
2964 elsif Ekind (Subp) /= E_Operator then
2965 Check_Mode_Conformant (Subp, Nam);
2969 Error_Msg_N ("no visible subprogram matches specification", N);
2975 if Has_Aspects (N) then
2976 Analyze_Aspect_Specifications (N, Nam);
2979 end Analyze_Formal_Subprogram_Declaration;
2981 -------------------------------------
2982 -- Analyze_Formal_Type_Declaration --
2983 -------------------------------------
2985 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2986 Def : constant Node_Id := Formal_Type_Definition (N);
2990 T := Defining_Identifier (N);
2992 if Present (Discriminant_Specifications (N))
2993 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2996 ("discriminants not allowed for this formal type", T);
2999 -- Enter the new name, and branch to specific routine
3002 when N_Formal_Private_Type_Definition =>
3003 Analyze_Formal_Private_Type (N, T, Def);
3005 when N_Formal_Derived_Type_Definition =>
3006 Analyze_Formal_Derived_Type (N, T, Def);
3008 when N_Formal_Incomplete_Type_Definition =>
3009 Analyze_Formal_Incomplete_Type (T, Def);
3011 when N_Formal_Discrete_Type_Definition =>
3012 Analyze_Formal_Discrete_Type (T, Def);
3014 when N_Formal_Signed_Integer_Type_Definition =>
3015 Analyze_Formal_Signed_Integer_Type (T, Def);
3017 when N_Formal_Modular_Type_Definition =>
3018 Analyze_Formal_Modular_Type (T, Def);
3020 when N_Formal_Floating_Point_Definition =>
3021 Analyze_Formal_Floating_Type (T, Def);
3023 when N_Formal_Ordinary_Fixed_Point_Definition =>
3024 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3026 when N_Formal_Decimal_Fixed_Point_Definition =>
3027 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3029 when N_Array_Type_Definition =>
3030 Analyze_Formal_Array_Type (T, Def);
3032 when N_Access_To_Object_Definition |
3033 N_Access_Function_Definition |
3034 N_Access_Procedure_Definition =>
3035 Analyze_Generic_Access_Type (T, Def);
3037 -- Ada 2005: a interface declaration is encoded as an abstract
3038 -- record declaration or a abstract type derivation.
3040 when N_Record_Definition =>
3041 Analyze_Formal_Interface_Type (N, T, Def);
3043 when N_Derived_Type_Definition =>
3044 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3050 raise Program_Error;
3054 Set_Is_Generic_Type (T);
3056 if Has_Aspects (N) then
3057 Analyze_Aspect_Specifications (N, T);
3059 end Analyze_Formal_Type_Declaration;
3061 ------------------------------------
3062 -- Analyze_Function_Instantiation --
3063 ------------------------------------
3065 procedure Analyze_Function_Instantiation (N : Node_Id) is
3067 Analyze_Subprogram_Instantiation (N, E_Function);
3068 end Analyze_Function_Instantiation;
3070 ---------------------------------
3071 -- Analyze_Generic_Access_Type --
3072 ---------------------------------
3074 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3078 if Nkind (Def) = N_Access_To_Object_Definition then
3079 Access_Type_Declaration (T, Def);
3081 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3082 and then No (Full_View (Designated_Type (T)))
3083 and then not Is_Generic_Type (Designated_Type (T))
3085 Error_Msg_N ("premature usage of incomplete type", Def);
3087 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3089 ("only a subtype mark is allowed in a formal", Def);
3093 Access_Subprogram_Declaration (T, Def);
3095 end Analyze_Generic_Access_Type;
3097 ---------------------------------
3098 -- Analyze_Generic_Formal_Part --
3099 ---------------------------------
3101 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3102 Gen_Parm_Decl : Node_Id;
3105 -- The generic formals are processed in the scope of the generic unit,
3106 -- where they are immediately visible. The scope is installed by the
3109 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3110 while Present (Gen_Parm_Decl) loop
3111 Analyze (Gen_Parm_Decl);
3112 Next (Gen_Parm_Decl);
3115 Generate_Reference_To_Generic_Formals (Current_Scope);
3116 end Analyze_Generic_Formal_Part;
3118 ------------------------------------------
3119 -- Analyze_Generic_Package_Declaration --
3120 ------------------------------------------
3122 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3123 Loc : constant Source_Ptr := Sloc (N);
3126 Save_Parent : Node_Id;
3128 Decls : constant List_Id :=
3129 Visible_Declarations (Specification (N));
3133 -- The generic package declaration may be subject to pragma Ghost with
3134 -- policy Ignore. Set the mode now to ensure that any nodes generated
3135 -- during analysis and expansion are properly flagged as ignored Ghost.
3138 Check_SPARK_05_Restriction ("generic is not allowed", N);
3140 -- We introduce a renaming of the enclosing package, to have a usable
3141 -- entity as the prefix of an expanded name for a local entity of the
3142 -- form Par.P.Q, where P is the generic package. This is because a local
3143 -- entity named P may hide it, so that the usual visibility rules in
3144 -- the instance will not resolve properly.
3147 Make_Package_Renaming_Declaration (Loc,
3148 Defining_Unit_Name =>
3149 Make_Defining_Identifier (Loc,
3150 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3152 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3154 if Present (Decls) then
3155 Decl := First (Decls);
3156 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3160 if Present (Decl) then
3161 Insert_Before (Decl, Renaming);
3163 Append (Renaming, Visible_Declarations (Specification (N)));
3167 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3170 -- Create copy of generic unit, and save for instantiation. If the unit
3171 -- is a child unit, do not copy the specifications for the parent, which
3172 -- are not part of the generic tree.
3174 Save_Parent := Parent_Spec (N);
3175 Set_Parent_Spec (N, Empty);
3177 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3178 Set_Parent_Spec (New_N, Save_Parent);
3181 -- Once the contents of the generic copy and the template are swapped,
3182 -- do the same for their respective aspect specifications.
3184 Exchange_Aspects (N, New_N);
3186 -- Collect all contract-related source pragmas found within the template
3187 -- and attach them to the contract of the package spec. This contract is
3188 -- used in the capture of global references within annotations.
3190 Create_Generic_Contract (N);
3192 Id := Defining_Entity (N);
3193 Generate_Definition (Id);
3195 -- Expansion is not applied to generic units
3200 Set_Ekind (Id, E_Generic_Package);
3201 Set_Etype (Id, Standard_Void_Type);
3203 -- A generic package declared within a Ghost region is rendered Ghost
3204 -- (SPARK RM 6.9(2)).
3206 if Ghost_Mode > None then
3207 Set_Is_Ghost_Entity (Id);
3210 -- Analyze aspects now, so that generated pragmas appear in the
3211 -- declarations before building and analyzing the generic copy.
3213 if Has_Aspects (N) then
3214 Analyze_Aspect_Specifications (N, Id);
3218 Enter_Generic_Scope (Id);
3219 Set_Inner_Instances (Id, New_Elmt_List);
3221 Set_Categorization_From_Pragmas (N);
3222 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3224 -- Link the declaration of the generic homonym in the generic copy to
3225 -- the package it renames, so that it is always resolved properly.
3227 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3228 Set_Entity (Associated_Node (Name (Renaming)), Id);
3230 -- For a library unit, we have reconstructed the entity for the unit,
3231 -- and must reset it in the library tables.
3233 if Nkind (Parent (N)) = N_Compilation_Unit then
3234 Set_Cunit_Entity (Current_Sem_Unit, Id);
3237 Analyze_Generic_Formal_Part (N);
3239 -- After processing the generic formals, analysis proceeds as for a
3240 -- non-generic package.
3242 Analyze (Specification (N));
3244 Validate_Categorization_Dependency (N, Id);
3248 End_Package_Scope (Id);
3249 Exit_Generic_Scope (Id);
3251 if Nkind (Parent (N)) /= N_Compilation_Unit then
3252 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3253 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3254 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3257 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3258 Validate_RT_RAT_Component (N);
3260 -- If this is a spec without a body, check that generic parameters
3263 if not Body_Required (Parent (N)) then
3264 Check_References (Id);
3268 -- If there is a specified storage pool in the context, create an
3269 -- aspect on the package declaration, so that it is used in any
3270 -- instance that does not override it.
3272 if Present (Default_Pool) then
3278 Make_Aspect_Specification (Loc,
3279 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3280 Expression => New_Copy (Default_Pool));
3282 if No (Aspect_Specifications (Specification (N))) then
3283 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3285 Append (ASN, Aspect_Specifications (Specification (N)));
3289 end Analyze_Generic_Package_Declaration;
3291 --------------------------------------------
3292 -- Analyze_Generic_Subprogram_Declaration --
3293 --------------------------------------------
3295 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3299 Result_Type : Entity_Id;
3300 Save_Parent : Node_Id;
3305 -- The generic subprogram declaration may be subject to pragma Ghost
3306 -- with policy Ignore. Set the mode now to ensure that any nodes
3307 -- generated during analysis and expansion are properly flagged as
3311 Check_SPARK_05_Restriction ("generic is not allowed", N);
3313 -- Create copy of generic unit, and save for instantiation. If the unit
3314 -- is a child unit, do not copy the specifications for the parent, which
3315 -- are not part of the generic tree.
3317 Save_Parent := Parent_Spec (N);
3318 Set_Parent_Spec (N, Empty);
3320 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3321 Set_Parent_Spec (New_N, Save_Parent);
3324 -- Once the contents of the generic copy and the template are swapped,
3325 -- do the same for their respective aspect specifications.
3327 Exchange_Aspects (N, New_N);
3329 -- Collect all contract-related source pragmas found within the template
3330 -- and attach them to the contract of the subprogram spec. This contract
3331 -- is used in the capture of global references within annotations.
3333 Create_Generic_Contract (N);
3335 Spec := Specification (N);
3336 Id := Defining_Entity (Spec);
3337 Generate_Definition (Id);
3339 if Nkind (Id) = N_Defining_Operator_Symbol then
3341 ("operator symbol not allowed for generic subprogram", Id);
3347 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3349 -- Analyze the aspects of the generic copy to ensure that all generated
3350 -- pragmas (if any) perform their semantic effects.
3352 if Has_Aspects (N) then
3353 Analyze_Aspect_Specifications (N, Id);
3357 Enter_Generic_Scope (Id);
3358 Set_Inner_Instances (Id, New_Elmt_List);
3359 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3361 Analyze_Generic_Formal_Part (N);
3363 Formals := Parameter_Specifications (Spec);
3365 if Present (Formals) then
3366 Process_Formals (Formals, Spec);
3369 if Nkind (Spec) = N_Function_Specification then
3370 Set_Ekind (Id, E_Generic_Function);
3372 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3373 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3374 Set_Etype (Id, Result_Type);
3376 -- Check restriction imposed by AI05-073: a generic function
3377 -- cannot return an abstract type or an access to such.
3379 -- This is a binding interpretation should it apply to earlier
3380 -- versions of Ada as well as Ada 2012???
3382 if Is_Abstract_Type (Designated_Type (Result_Type))
3383 and then Ada_Version >= Ada_2012
3386 ("generic function cannot have an access result "
3387 & "that designates an abstract type", Spec);
3391 Find_Type (Result_Definition (Spec));
3392 Typ := Entity (Result_Definition (Spec));
3394 if Is_Abstract_Type (Typ)
3395 and then Ada_Version >= Ada_2012
3398 ("generic function cannot have abstract result type", Spec);
3401 -- If a null exclusion is imposed on the result type, then create
3402 -- a null-excluding itype (an access subtype) and use it as the
3403 -- function's Etype.
3405 if Is_Access_Type (Typ)
3406 and then Null_Exclusion_Present (Spec)
3409 Create_Null_Excluding_Itype
3411 Related_Nod => Spec,
3412 Scope_Id => Defining_Unit_Name (Spec)));
3414 Set_Etype (Id, Typ);
3419 Set_Ekind (Id, E_Generic_Procedure);
3420 Set_Etype (Id, Standard_Void_Type);
3423 -- A generic subprogram declared within a Ghost region is rendered Ghost
3424 -- (SPARK RM 6.9(2)).
3426 if Ghost_Mode > None then
3427 Set_Is_Ghost_Entity (Id);
3430 -- For a library unit, we have reconstructed the entity for the unit,
3431 -- and must reset it in the library tables. We also make sure that
3432 -- Body_Required is set properly in the original compilation unit node.
3434 if Nkind (Parent (N)) = N_Compilation_Unit then
3435 Set_Cunit_Entity (Current_Sem_Unit, Id);
3436 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3439 Set_Categorization_From_Pragmas (N);
3440 Validate_Categorization_Dependency (N, Id);
3442 -- Capture all global references that occur within the profile of the
3443 -- generic subprogram. Aspects are not part of this processing because
3444 -- they must be delayed. If processed now, Save_Global_References will
3445 -- destroy the Associated_Node links and prevent the capture of global
3446 -- references when the contract of the generic subprogram is analyzed.
3448 Save_Global_References (Original_Node (N));
3452 Exit_Generic_Scope (Id);
3453 Generate_Reference_To_Formals (Id);
3455 List_Inherited_Pre_Post_Aspects (Id);
3456 end Analyze_Generic_Subprogram_Declaration;
3458 -----------------------------------
3459 -- Analyze_Package_Instantiation --
3460 -----------------------------------
3462 procedure Analyze_Package_Instantiation (N : Node_Id) is
3463 Loc : constant Source_Ptr := Sloc (N);
3464 Gen_Id : constant Node_Id := Name (N);
3467 Act_Decl_Name : Node_Id;
3468 Act_Decl_Id : Entity_Id;
3474 Gen_Unit : Entity_Id;
3476 Is_Actual_Pack : constant Boolean :=
3477 Is_Internal (Defining_Entity (N));
3479 Env_Installed : Boolean := False;
3480 Parent_Installed : Boolean := False;
3481 Renaming_List : List_Id;
3482 Unit_Renaming : Node_Id;
3483 Needs_Body : Boolean;
3484 Inline_Now : Boolean := False;
3485 Has_Inline_Always : Boolean := False;
3487 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3488 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3490 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3491 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3492 -- Save the SPARK_Mode-related data for restore on exit
3494 Save_Style_Check : constant Boolean := Style_Check;
3495 -- Save style check mode for restore on exit
3497 procedure Delay_Descriptors (E : Entity_Id);
3498 -- Delay generation of subprogram descriptors for given entity
3500 function Might_Inline_Subp return Boolean;
3501 -- If inlining is active and the generic contains inlined subprograms,
3502 -- we instantiate the body. This may cause superfluous instantiations,
3503 -- but it is simpler than detecting the need for the body at the point
3504 -- of inlining, when the context of the instance is not available.
3506 -----------------------
3507 -- Delay_Descriptors --
3508 -----------------------
3510 procedure Delay_Descriptors (E : Entity_Id) is
3512 if not Delay_Subprogram_Descriptors (E) then
3513 Set_Delay_Subprogram_Descriptors (E);
3514 Pending_Descriptor.Append (E);
3516 end Delay_Descriptors;
3518 -----------------------
3519 -- Might_Inline_Subp --
3520 -----------------------
3522 function Might_Inline_Subp return Boolean is
3526 if not Inline_Processing_Required then
3530 E := First_Entity (Gen_Unit);
3531 while Present (E) loop
3532 if Is_Subprogram (E) and then Is_Inlined (E) then
3533 -- Remember if there are any subprograms with Inline_Always
3535 if Has_Pragma_Inline_Always (E) then
3536 Has_Inline_Always := True;
3547 end Might_Inline_Subp;
3549 -- Local declarations
3551 Vis_Prims_List : Elist_Id := No_Elist;
3552 -- List of primitives made temporarily visible in the instantiation
3553 -- to match the visibility of the formal type
3555 -- Start of processing for Analyze_Package_Instantiation
3558 Check_SPARK_05_Restriction ("generic is not allowed", N);
3560 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3561 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3563 Check_Text_IO_Special_Unit (Name (N));
3565 -- Make node global for error reporting
3567 Instantiation_Node := N;
3569 -- Turn off style checking in instances. If the check is enabled on the
3570 -- generic unit, a warning in an instance would just be noise. If not
3571 -- enabled on the generic, then a warning in an instance is just wrong.
3573 Style_Check := False;
3575 -- Case of instantiation of a generic package
3577 if Nkind (N) = N_Package_Instantiation then
3578 Act_Decl_Id := New_Copy (Defining_Entity (N));
3579 Set_Comes_From_Source (Act_Decl_Id, True);
3581 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3583 Make_Defining_Program_Unit_Name (Loc,
3585 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3586 Defining_Identifier => Act_Decl_Id);
3588 Act_Decl_Name := Act_Decl_Id;
3591 -- Case of instantiation of a formal package
3594 Act_Decl_Id := Defining_Identifier (N);
3595 Act_Decl_Name := Act_Decl_Id;
3598 Generate_Definition (Act_Decl_Id);
3599 Preanalyze_Actuals (N);
3602 Env_Installed := True;
3604 -- Reset renaming map for formal types. The mapping is established
3605 -- when analyzing the generic associations, but some mappings are
3606 -- inherited from formal packages of parent units, and these are
3607 -- constructed when the parents are installed.
3609 Generic_Renamings.Set_Last (0);
3610 Generic_Renamings_HTable.Reset;
3612 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3613 Gen_Unit := Entity (Gen_Id);
3615 -- Verify that it is the name of a generic package
3617 -- A visibility glitch: if the instance is a child unit and the generic
3618 -- is the generic unit of a parent instance (i.e. both the parent and
3619 -- the child units are instances of the same package) the name now
3620 -- denotes the renaming within the parent, not the intended generic
3621 -- unit. See if there is a homonym that is the desired generic. The
3622 -- renaming declaration must be visible inside the instance of the
3623 -- child, but not when analyzing the name in the instantiation itself.
3625 if Ekind (Gen_Unit) = E_Package
3626 and then Present (Renamed_Entity (Gen_Unit))
3627 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3628 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3629 and then Present (Homonym (Gen_Unit))
3631 Gen_Unit := Homonym (Gen_Unit);
3634 if Etype (Gen_Unit) = Any_Type then
3638 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3640 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3642 if From_Limited_With (Gen_Unit) then
3644 ("cannot instantiate a limited withed package", Gen_Id);
3647 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3654 if In_Extended_Main_Source_Unit (N) then
3655 Set_Is_Instantiated (Gen_Unit);
3656 Generate_Reference (Gen_Unit, N);
3658 if Present (Renamed_Object (Gen_Unit)) then
3659 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3660 Generate_Reference (Renamed_Object (Gen_Unit), N);
3664 if Nkind (Gen_Id) = N_Identifier
3665 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3668 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3670 elsif Nkind (Gen_Id) = N_Expanded_Name
3671 and then Is_Child_Unit (Gen_Unit)
3672 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3673 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3676 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3679 Set_Entity (Gen_Id, Gen_Unit);
3681 -- If generic is a renaming, get original generic unit
3683 if Present (Renamed_Object (Gen_Unit))
3684 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3686 Gen_Unit := Renamed_Object (Gen_Unit);
3689 -- Verify that there are no circular instantiations
3691 if In_Open_Scopes (Gen_Unit) then
3692 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3696 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3697 Error_Msg_Node_2 := Current_Scope;
3699 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3700 Circularity_Detected := True;
3705 -- If the context of the instance is subject to SPARK_Mode "off",
3706 -- set the global flag which signals Analyze_Pragma to ignore all
3707 -- SPARK_Mode pragmas within the instance.
3709 if SPARK_Mode = Off then
3710 Ignore_Pragma_SPARK_Mode := True;
3713 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3714 Gen_Spec := Specification (Gen_Decl);
3716 -- Initialize renamings map, for error checking, and the list that
3717 -- holds private entities whose views have changed between generic
3718 -- definition and instantiation. If this is the instance created to
3719 -- validate an actual package, the instantiation environment is that
3720 -- of the enclosing instance.
3722 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3724 -- Copy original generic tree, to produce text for instantiation
3728 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3730 Act_Spec := Specification (Act_Tree);
3732 -- If this is the instance created to validate an actual package,
3733 -- only the formals matter, do not examine the package spec itself.
3735 if Is_Actual_Pack then
3736 Set_Visible_Declarations (Act_Spec, New_List);
3737 Set_Private_Declarations (Act_Spec, New_List);
3741 Analyze_Associations
3743 Formals => Generic_Formal_Declarations (Act_Tree),
3744 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3746 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3748 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3749 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3750 Set_Is_Generic_Instance (Act_Decl_Id);
3751 Set_Generic_Parent (Act_Spec, Gen_Unit);
3753 -- References to the generic in its own declaration or its body are
3754 -- references to the instance. Add a renaming declaration for the
3755 -- generic unit itself. This declaration, as well as the renaming
3756 -- declarations for the generic formals, must remain private to the
3757 -- unit: the formals, because this is the language semantics, and
3758 -- the unit because its use is an artifact of the implementation.
3761 Make_Package_Renaming_Declaration (Loc,
3762 Defining_Unit_Name =>
3763 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3764 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3766 Append (Unit_Renaming, Renaming_List);
3768 -- The renaming declarations are the first local declarations of the
3771 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3773 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3775 Set_Visible_Declarations (Act_Spec, Renaming_List);
3778 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3780 -- Propagate the aspect specifications from the package declaration
3781 -- template to the instantiated version of the package declaration.
3783 if Has_Aspects (Act_Tree) then
3784 Set_Aspect_Specifications (Act_Decl,
3785 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3788 -- The generic may have a generated Default_Storage_Pool aspect,
3789 -- set at the point of generic declaration. If the instance has
3790 -- that aspect, it overrides the one inherited from the generic.
3792 if Has_Aspects (Gen_Spec) then
3793 if No (Aspect_Specifications (N)) then
3794 Set_Aspect_Specifications (N,
3796 (Aspect_Specifications (Gen_Spec))));
3800 ASN1, ASN2 : Node_Id;
3803 ASN1 := First (Aspect_Specifications (N));
3804 while Present (ASN1) loop
3805 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3807 -- If generic carries a default storage pool, remove
3808 -- it in favor of the instance one.
3810 ASN2 := First (Aspect_Specifications (Gen_Spec));
3811 while Present (ASN2) loop
3812 if Chars (Identifier (ASN2)) =
3813 Name_Default_Storage_Pool
3826 Prepend_List_To (Aspect_Specifications (N),
3828 (Aspect_Specifications (Gen_Spec))));
3833 -- Save the instantiation node, for subsequent instantiation of the
3834 -- body, if there is one and we are generating code for the current
3835 -- unit. Mark unit as having a body (avoids premature error message).
3837 -- We instantiate the body if we are generating code, if we are
3838 -- generating cross-reference information, or if we are building
3839 -- trees for ASIS use or GNATprove use.
3842 Enclosing_Body_Present : Boolean := False;
3843 -- If the generic unit is not a compilation unit, then a body may
3844 -- be present in its parent even if none is required. We create a
3845 -- tentative pending instantiation for the body, which will be
3846 -- discarded if none is actually present.
3851 if Scope (Gen_Unit) /= Standard_Standard
3852 and then not Is_Child_Unit (Gen_Unit)
3854 Scop := Scope (Gen_Unit);
3855 while Present (Scop) and then Scop /= Standard_Standard loop
3856 if Unit_Requires_Body (Scop) then
3857 Enclosing_Body_Present := True;
3860 elsif In_Open_Scopes (Scop)
3861 and then In_Package_Body (Scop)
3863 Enclosing_Body_Present := True;
3867 exit when Is_Compilation_Unit (Scop);
3868 Scop := Scope (Scop);
3872 -- If front-end inlining is enabled or there are any subprograms
3873 -- marked with Inline_Always, and this is a unit for which code
3874 -- will be generated, we instantiate the body at once.
3876 -- This is done if the instance is not the main unit, and if the
3877 -- generic is not a child unit of another generic, to avoid scope
3878 -- problems and the reinstallation of parent instances.
3881 and then (not Is_Child_Unit (Gen_Unit)
3882 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3883 and then Might_Inline_Subp
3884 and then not Is_Actual_Pack
3886 if not Back_End_Inlining
3887 and then (Front_End_Inlining or else Has_Inline_Always)
3888 and then (Is_In_Main_Unit (N)
3889 or else In_Main_Context (Current_Scope))
3890 and then Nkind (Parent (N)) /= N_Compilation_Unit
3894 -- In configurable_run_time mode we force the inlining of
3895 -- predefined subprograms marked Inline_Always, to minimize
3896 -- the use of the run-time library.
3898 elsif Is_Predefined_File_Name
3899 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3900 and then Configurable_Run_Time_Mode
3901 and then Nkind (Parent (N)) /= N_Compilation_Unit
3906 -- If the current scope is itself an instance within a child
3907 -- unit, there will be duplications in the scope stack, and the
3908 -- unstacking mechanism in Inline_Instance_Body will fail.
3909 -- This loses some rare cases of optimization, and might be
3910 -- improved some day, if we can find a proper abstraction for
3911 -- "the complete compilation context" that can be saved and
3914 if Is_Generic_Instance (Current_Scope) then
3916 Curr_Unit : constant Entity_Id :=
3917 Cunit_Entity (Current_Sem_Unit);
3919 if Curr_Unit /= Current_Scope
3920 and then Is_Child_Unit (Curr_Unit)
3922 Inline_Now := False;
3929 (Unit_Requires_Body (Gen_Unit)
3930 or else Enclosing_Body_Present
3931 or else Present (Corresponding_Body (Gen_Decl)))
3932 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3933 and then not Is_Actual_Pack
3934 and then not Inline_Now
3935 and then (Operating_Mode = Generate_Code
3937 -- Need comment for this check ???
3939 or else (Operating_Mode = Check_Semantics
3940 and then (ASIS_Mode or GNATprove_Mode)));
3942 -- If front-end inlining is enabled or there are any subprograms
3943 -- marked with Inline_Always, do not instantiate body when within
3944 -- a generic context.
3946 if ((Front_End_Inlining or else Has_Inline_Always)
3947 and then not Expander_Active)
3948 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3950 Needs_Body := False;
3953 -- If the current context is generic, and the package being
3954 -- instantiated is declared within a formal package, there is no
3955 -- body to instantiate until the enclosing generic is instantiated
3956 -- and there is an actual for the formal package. If the formal
3957 -- package has parameters, we build a regular package instance for
3958 -- it, that precedes the original formal package declaration.
3960 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3962 Decl : constant Node_Id :=
3964 (Unit_Declaration_Node (Scope (Gen_Unit)));
3966 if Nkind (Decl) = N_Formal_Package_Declaration
3967 or else (Nkind (Decl) = N_Package_Declaration
3968 and then Is_List_Member (Decl)
3969 and then Present (Next (Decl))
3971 Nkind (Next (Decl)) =
3972 N_Formal_Package_Declaration)
3974 Needs_Body := False;
3980 -- For RCI unit calling stubs, we omit the instance body if the
3981 -- instance is the RCI library unit itself.
3983 -- However there is a special case for nested instances: in this case
3984 -- we do generate the instance body, as it might be required, e.g.
3985 -- because it provides stream attributes for some type used in the
3986 -- profile of a remote subprogram. This is consistent with 12.3(12),
3987 -- which indicates that the instance body occurs at the place of the
3988 -- instantiation, and thus is part of the RCI declaration, which is
3989 -- present on all client partitions (this is E.2.3(18)).
3991 -- Note that AI12-0002 may make it illegal at some point to have
3992 -- stream attributes defined in an RCI unit, in which case this
3993 -- special case will become unnecessary. In the meantime, there
3994 -- is known application code in production that depends on this
3995 -- being possible, so we definitely cannot eliminate the body in
3996 -- the case of nested instances for the time being.
3998 -- When we generate a nested instance body, calling stubs for any
3999 -- relevant subprogram will be be inserted immediately after the
4000 -- subprogram declarations, and will take precedence over the
4001 -- subsequent (original) body. (The stub and original body will be
4002 -- complete homographs, but this is permitted in an instance).
4003 -- (Could we do better and remove the original body???)
4005 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4006 and then Comes_From_Source (N)
4007 and then Nkind (Parent (N)) = N_Compilation_Unit
4009 Needs_Body := False;
4014 -- Here is a defence against a ludicrous number of instantiations
4015 -- caused by a circular set of instantiation attempts.
4017 if Pending_Instantiations.Last > Maximum_Instantiations then
4018 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4019 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4020 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4021 raise Unrecoverable_Error;
4024 -- Indicate that the enclosing scopes contain an instantiation,
4025 -- and that cleanup actions should be delayed until after the
4026 -- instance body is expanded.
4028 Check_Forward_Instantiation (Gen_Decl);
4029 if Nkind (N) = N_Package_Instantiation then
4031 Enclosing_Master : Entity_Id;
4034 -- Loop to search enclosing masters
4036 Enclosing_Master := Current_Scope;
4037 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4038 if Ekind (Enclosing_Master) = E_Package then
4039 if Is_Compilation_Unit (Enclosing_Master) then
4040 if In_Package_Body (Enclosing_Master) then
4042 (Body_Entity (Enclosing_Master));
4051 Enclosing_Master := Scope (Enclosing_Master);
4054 elsif Is_Generic_Unit (Enclosing_Master)
4055 or else Ekind (Enclosing_Master) = E_Void
4057 -- Cleanup actions will eventually be performed on the
4058 -- enclosing subprogram or package instance, if any.
4059 -- Enclosing scope is void in the formal part of a
4060 -- generic subprogram.
4065 if Ekind (Enclosing_Master) = E_Entry
4067 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4069 if not Expander_Active then
4073 Protected_Body_Subprogram (Enclosing_Master);
4077 Set_Delay_Cleanups (Enclosing_Master);
4079 while Ekind (Enclosing_Master) = E_Block loop
4080 Enclosing_Master := Scope (Enclosing_Master);
4083 if Is_Subprogram (Enclosing_Master) then
4084 Delay_Descriptors (Enclosing_Master);
4086 elsif Is_Task_Type (Enclosing_Master) then
4088 TBP : constant Node_Id :=
4089 Get_Task_Body_Procedure
4092 if Present (TBP) then
4093 Delay_Descriptors (TBP);
4094 Set_Delay_Cleanups (TBP);
4101 end loop Scope_Loop;
4104 -- Make entry in table
4106 Pending_Instantiations.Append
4108 Act_Decl => Act_Decl,
4109 Expander_Status => Expander_Active,
4110 Current_Sem_Unit => Current_Sem_Unit,
4111 Scope_Suppress => Scope_Suppress,
4112 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4113 Version => Ada_Version,
4114 Version_Pragma => Ada_Version_Pragma,
4115 Warnings => Save_Warnings,
4116 SPARK_Mode => SPARK_Mode,
4117 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4121 Set_Categorization_From_Pragmas (Act_Decl);
4123 if Parent_Installed then
4127 Set_Instance_Spec (N, Act_Decl);
4129 -- If not a compilation unit, insert the package declaration before
4130 -- the original instantiation node.
4132 if Nkind (Parent (N)) /= N_Compilation_Unit then
4133 Mark_Rewrite_Insertion (Act_Decl);
4134 Insert_Before (N, Act_Decl);
4136 if Has_Aspects (N) then
4137 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4139 -- The pragma created for a Default_Storage_Pool aspect must
4140 -- appear ahead of the declarations in the instance spec.
4141 -- Analysis has placed it after the instance node, so remove
4142 -- it and reinsert it properly now.
4145 ASN : constant Node_Id := First (Aspect_Specifications (N));
4146 A_Name : constant Name_Id := Chars (Identifier (ASN));
4150 if A_Name = Name_Default_Storage_Pool then
4151 if No (Visible_Declarations (Act_Spec)) then
4152 Set_Visible_Declarations (Act_Spec, New_List);
4156 while Present (Decl) loop
4157 if Nkind (Decl) = N_Pragma then
4159 Prepend (Decl, Visible_Declarations (Act_Spec));
4171 -- For an instantiation that is a compilation unit, place
4172 -- declaration on current node so context is complete for analysis
4173 -- (including nested instantiations). If this is the main unit,
4174 -- the declaration eventually replaces the instantiation node.
4175 -- If the instance body is created later, it replaces the
4176 -- instance node, and the declaration is attached to it
4177 -- (see Build_Instance_Compilation_Unit_Nodes).
4180 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4182 -- The entity for the current unit is the newly created one,
4183 -- and all semantic information is attached to it.
4185 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4187 -- If this is the main unit, replace the main entity as well
4189 if Current_Sem_Unit = Main_Unit then
4190 Main_Unit_Entity := Act_Decl_Id;
4194 Set_Unit (Parent (N), Act_Decl);
4195 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4196 Set_Package_Instantiation (Act_Decl_Id, N);
4198 -- Process aspect specifications of the instance node, if any, to
4199 -- take into account categorization pragmas before analyzing the
4202 if Has_Aspects (N) then
4203 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4207 Set_Unit (Parent (N), N);
4208 Set_Body_Required (Parent (N), False);
4210 -- We never need elaboration checks on instantiations, since by
4211 -- definition, the body instantiation is elaborated at the same
4212 -- time as the spec instantiation.
4214 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4215 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4218 Check_Elab_Instantiation (N);
4220 if ABE_Is_Certain (N) and then Needs_Body then
4221 Pending_Instantiations.Decrement_Last;
4224 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4226 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4227 First_Private_Entity (Act_Decl_Id));
4229 -- If the instantiation will receive a body, the unit will be
4230 -- transformed into a package body, and receive its own elaboration
4231 -- entity. Otherwise, the nature of the unit is now a package
4234 if Nkind (Parent (N)) = N_Compilation_Unit
4235 and then not Needs_Body
4237 Rewrite (N, Act_Decl);
4240 if Present (Corresponding_Body (Gen_Decl))
4241 or else Unit_Requires_Body (Gen_Unit)
4243 Set_Has_Completion (Act_Decl_Id);
4246 Check_Formal_Packages (Act_Decl_Id);
4248 Restore_Hidden_Primitives (Vis_Prims_List);
4249 Restore_Private_Views (Act_Decl_Id);
4251 Inherit_Context (Gen_Decl, N);
4253 if Parent_Installed then
4258 Env_Installed := False;
4261 Validate_Categorization_Dependency (N, Act_Decl_Id);
4263 -- There used to be a check here to prevent instantiations in local
4264 -- contexts if the No_Local_Allocators restriction was active. This
4265 -- check was removed by a binding interpretation in AI-95-00130/07,
4266 -- but we retain the code for documentation purposes.
4268 -- if Ekind (Act_Decl_Id) /= E_Void
4269 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4271 -- Check_Restriction (No_Local_Allocators, N);
4275 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4278 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4279 -- be used as defining identifiers for a formal package and for the
4280 -- corresponding expanded package.
4282 if Nkind (N) = N_Formal_Package_Declaration then
4283 Act_Decl_Id := New_Copy (Defining_Entity (N));
4284 Set_Comes_From_Source (Act_Decl_Id, True);
4285 Set_Is_Generic_Instance (Act_Decl_Id, False);
4286 Set_Defining_Identifier (N, Act_Decl_Id);
4289 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4290 SPARK_Mode := Save_SM;
4291 SPARK_Mode_Pragma := Save_SMP;
4292 Style_Check := Save_Style_Check;
4294 if SPARK_Mode = On then
4295 Dynamic_Elaboration_Checks := False;
4298 -- Check that if N is an instantiation of System.Dim_Float_IO or
4299 -- System.Dim_Integer_IO, the formal type has a dimension system.
4301 if Nkind (N) = N_Package_Instantiation
4302 and then Is_Dim_IO_Package_Instantiation (N)
4305 Assoc : constant Node_Id := First (Generic_Associations (N));
4307 if not Has_Dimension_System
4308 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4310 Error_Msg_N ("type with a dimension system expected", Assoc);
4316 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4317 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4321 when Instantiation_Error =>
4322 if Parent_Installed then
4326 if Env_Installed then
4330 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4331 SPARK_Mode := Save_SM;
4332 SPARK_Mode_Pragma := Save_SMP;
4333 Style_Check := Save_Style_Check;
4335 if SPARK_Mode = On then
4336 Dynamic_Elaboration_Checks := False;
4338 end Analyze_Package_Instantiation;
4340 --------------------------
4341 -- Inline_Instance_Body --
4342 --------------------------
4344 procedure Inline_Instance_Body
4346 Gen_Unit : Entity_Id;
4349 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4350 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4351 Gen_Comp : constant Entity_Id :=
4352 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4354 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4355 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4356 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4357 -- to provide a clean environment for analysis of the inlined body will
4358 -- eliminate any previously set SPARK_Mode.
4360 Scope_Stack_Depth : constant Int :=
4361 Scope_Stack.Last - Scope_Stack.First + 1;
4363 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4364 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4365 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4366 Curr_Scope : Entity_Id := Empty;
4368 Num_Inner : Int := 0;
4369 Num_Scopes : Int := 0;
4370 N_Instances : Int := 0;
4371 Removed : Boolean := False;
4376 -- Case of generic unit defined in another unit. We must remove the
4377 -- complete context of the current unit to install that of the generic.
4379 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4381 -- Add some comments for the following two loops ???
4384 while Present (S) and then S /= Standard_Standard loop
4386 Num_Scopes := Num_Scopes + 1;
4388 Use_Clauses (Num_Scopes) :=
4390 (Scope_Stack.Last - Num_Scopes + 1).
4392 End_Use_Clauses (Use_Clauses (Num_Scopes));
4394 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4395 or else Scope_Stack.Table
4396 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4399 exit when Is_Generic_Instance (S)
4400 and then (In_Package_Body (S)
4401 or else Ekind (S) = E_Procedure
4402 or else Ekind (S) = E_Function);
4406 Vis := Is_Immediately_Visible (Gen_Comp);
4408 -- Find and save all enclosing instances
4413 and then S /= Standard_Standard
4415 if Is_Generic_Instance (S) then
4416 N_Instances := N_Instances + 1;
4417 Instances (N_Instances) := S;
4419 exit when In_Package_Body (S);
4425 -- Remove context of current compilation unit, unless we are within a
4426 -- nested package instantiation, in which case the context has been
4427 -- removed previously.
4429 -- If current scope is the body of a child unit, remove context of
4430 -- spec as well. If an enclosing scope is an instance body, the
4431 -- context has already been removed, but the entities in the body
4432 -- must be made invisible as well.
4435 while Present (S) and then S /= Standard_Standard loop
4436 if Is_Generic_Instance (S)
4437 and then (In_Package_Body (S)
4438 or else Ekind_In (S, E_Procedure, E_Function))
4440 -- We still have to remove the entities of the enclosing
4441 -- instance from direct visibility.
4446 E := First_Entity (S);
4447 while Present (E) loop
4448 Set_Is_Immediately_Visible (E, False);
4457 or else (Ekind (Curr_Unit) = E_Package_Body
4458 and then S = Spec_Entity (Curr_Unit))
4459 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4460 and then S = Corresponding_Spec
4461 (Unit_Declaration_Node (Curr_Unit)))
4465 -- Remove entities in current scopes from visibility, so that
4466 -- instance body is compiled in a clean environment.
4468 List := Save_Scope_Stack (Handle_Use => False);
4470 if Is_Child_Unit (S) then
4472 -- Remove child unit from stack, as well as inner scopes.
4473 -- Removing the context of a child unit removes parent units
4476 while Current_Scope /= S loop
4477 Num_Inner := Num_Inner + 1;
4478 Inner_Scopes (Num_Inner) := Current_Scope;
4483 Remove_Context (Curr_Comp);
4487 Remove_Context (Curr_Comp);
4490 if Ekind (Curr_Unit) = E_Package_Body then
4491 Remove_Context (Library_Unit (Curr_Comp));
4498 pragma Assert (Num_Inner < Num_Scopes);
4500 -- The inlined package body must be analyzed with the SPARK_Mode of
4501 -- the enclosing context, otherwise the body may cause bogus errors
4502 -- if a configuration SPARK_Mode pragma in in effect.
4504 Push_Scope (Standard_Standard);
4505 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4506 Instantiate_Package_Body
4509 Act_Decl => Act_Decl,
4510 Expander_Status => Expander_Active,
4511 Current_Sem_Unit => Current_Sem_Unit,
4512 Scope_Suppress => Scope_Suppress,
4513 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4514 Version => Ada_Version,
4515 Version_Pragma => Ada_Version_Pragma,
4516 Warnings => Save_Warnings,
4517 SPARK_Mode => Save_SM,
4518 SPARK_Mode_Pragma => Save_SMP)),
4519 Inlined_Body => True);
4525 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4527 -- Reset Generic_Instance flag so that use clauses can be installed
4528 -- in the proper order. (See Use_One_Package for effect of enclosing
4529 -- instances on processing of use clauses).
4531 for J in 1 .. N_Instances loop
4532 Set_Is_Generic_Instance (Instances (J), False);
4536 Install_Context (Curr_Comp);
4538 if Present (Curr_Scope)
4539 and then Is_Child_Unit (Curr_Scope)
4541 Push_Scope (Curr_Scope);
4542 Set_Is_Immediately_Visible (Curr_Scope);
4544 -- Finally, restore inner scopes as well
4546 for J in reverse 1 .. Num_Inner loop
4547 Push_Scope (Inner_Scopes (J));
4551 Restore_Scope_Stack (List, Handle_Use => False);
4553 if Present (Curr_Scope)
4555 (In_Private_Part (Curr_Scope)
4556 or else In_Package_Body (Curr_Scope))
4558 -- Install private declaration of ancestor units, which are
4559 -- currently available. Restore_Scope_Stack and Install_Context
4560 -- only install the visible part of parents.
4565 Par := Scope (Curr_Scope);
4566 while (Present (Par)) and then Par /= Standard_Standard loop
4567 Install_Private_Declarations (Par);
4574 -- Restore use clauses. For a child unit, use clauses in the parents
4575 -- are restored when installing the context, so only those in inner
4576 -- scopes (and those local to the child unit itself) need to be
4577 -- installed explicitly.
4579 if Is_Child_Unit (Curr_Unit) and then Removed then
4580 for J in reverse 1 .. Num_Inner + 1 loop
4581 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4583 Install_Use_Clauses (Use_Clauses (J));
4587 for J in reverse 1 .. Num_Scopes loop
4588 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4590 Install_Use_Clauses (Use_Clauses (J));
4594 -- Restore status of instances. If one of them is a body, make its
4595 -- local entities visible again.
4602 for J in 1 .. N_Instances loop
4603 Inst := Instances (J);
4604 Set_Is_Generic_Instance (Inst, True);
4606 if In_Package_Body (Inst)
4607 or else Ekind_In (S, E_Procedure, E_Function)
4609 E := First_Entity (Instances (J));
4610 while Present (E) loop
4611 Set_Is_Immediately_Visible (E);
4618 -- If generic unit is in current unit, current context is correct. Note
4619 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4620 -- enclosing scopes were removed.
4623 Instantiate_Package_Body
4626 Act_Decl => Act_Decl,
4627 Expander_Status => Expander_Active,
4628 Current_Sem_Unit => Current_Sem_Unit,
4629 Scope_Suppress => Scope_Suppress,
4630 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4631 Version => Ada_Version,
4632 Version_Pragma => Ada_Version_Pragma,
4633 Warnings => Save_Warnings,
4634 SPARK_Mode => SPARK_Mode,
4635 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4636 Inlined_Body => True);
4638 end Inline_Instance_Body;
4640 -------------------------------------
4641 -- Analyze_Procedure_Instantiation --
4642 -------------------------------------
4644 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4646 Analyze_Subprogram_Instantiation (N, E_Procedure);
4647 end Analyze_Procedure_Instantiation;
4649 -----------------------------------
4650 -- Need_Subprogram_Instance_Body --
4651 -----------------------------------
4653 function Need_Subprogram_Instance_Body
4655 Subp : Entity_Id) return Boolean
4658 -- Must be inlined (or inlined renaming)
4660 if (Is_In_Main_Unit (N)
4661 or else Is_Inlined (Subp)
4662 or else Is_Inlined (Alias (Subp)))
4664 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4666 and then (Operating_Mode = Generate_Code
4667 or else (Operating_Mode = Check_Semantics
4668 and then (ASIS_Mode or GNATprove_Mode)))
4670 -- The body is needed when generating code (full expansion), in ASIS
4671 -- mode for other tools, and in GNATprove mode (special expansion) for
4672 -- formal verification of the body itself.
4674 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4676 -- No point in inlining if ABE is inevitable
4678 and then not ABE_Is_Certain (N)
4680 -- Or if subprogram is eliminated
4682 and then not Is_Eliminated (Subp)
4684 Pending_Instantiations.Append
4686 Act_Decl => Unit_Declaration_Node (Subp),
4687 Expander_Status => Expander_Active,
4688 Current_Sem_Unit => Current_Sem_Unit,
4689 Scope_Suppress => Scope_Suppress,
4690 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4691 Version => Ada_Version,
4692 Version_Pragma => Ada_Version_Pragma,
4693 Warnings => Save_Warnings,
4694 SPARK_Mode => SPARK_Mode,
4695 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4698 -- Here if not inlined, or we ignore the inlining
4703 end Need_Subprogram_Instance_Body;
4705 --------------------------------------
4706 -- Analyze_Subprogram_Instantiation --
4707 --------------------------------------
4709 procedure Analyze_Subprogram_Instantiation
4713 Loc : constant Source_Ptr := Sloc (N);
4714 Gen_Id : constant Node_Id := Name (N);
4716 Anon_Id : constant Entity_Id :=
4717 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4718 Chars => New_External_Name
4719 (Chars (Defining_Entity (N)), 'R'));
4721 Act_Decl_Id : Entity_Id;
4726 Env_Installed : Boolean := False;
4727 Gen_Unit : Entity_Id;
4729 Pack_Id : Entity_Id;
4730 Parent_Installed : Boolean := False;
4732 Renaming_List : List_Id;
4733 -- The list of declarations that link formals and actuals of the
4734 -- instance. These are subtype declarations for formal types, and
4735 -- renaming declarations for other formals. The subprogram declaration
4736 -- for the instance is then appended to the list, and the last item on
4737 -- the list is the renaming declaration for the instance.
4739 procedure Analyze_Instance_And_Renamings;
4740 -- The instance must be analyzed in a context that includes the mappings
4741 -- of generic parameters into actuals. We create a package declaration
4742 -- for this purpose, and a subprogram with an internal name within the
4743 -- package. The subprogram instance is simply an alias for the internal
4744 -- subprogram, declared in the current scope.
4746 procedure Build_Subprogram_Renaming;
4747 -- If the subprogram is recursive, there are occurrences of the name of
4748 -- the generic within the body, which must resolve to the current
4749 -- instance. We add a renaming declaration after the declaration, which
4750 -- is available in the instance body, as well as in the analysis of
4751 -- aspects that appear in the generic. This renaming declaration is
4752 -- inserted after the instance declaration which it renames.
4754 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4755 -- Instantiate all source pragmas found in the contract of the generic
4756 -- subprogram declaration template denoted by Templ. The instantiated
4757 -- pragmas are added to list Renaming_List.
4759 ------------------------------------
4760 -- Analyze_Instance_And_Renamings --
4761 ------------------------------------
4763 procedure Analyze_Instance_And_Renamings is
4764 Def_Ent : constant Entity_Id := Defining_Entity (N);
4765 Pack_Decl : Node_Id;
4768 if Nkind (Parent (N)) = N_Compilation_Unit then
4770 -- For the case of a compilation unit, the container package has
4771 -- the same name as the instantiation, to insure that the binder
4772 -- calls the elaboration procedure with the right name. Copy the
4773 -- entity of the instance, which may have compilation level flags
4774 -- (e.g. Is_Child_Unit) set.
4776 Pack_Id := New_Copy (Def_Ent);
4779 -- Otherwise we use the name of the instantiation concatenated
4780 -- with its source position to ensure uniqueness if there are
4781 -- several instantiations with the same name.
4784 Make_Defining_Identifier (Loc,
4785 Chars => New_External_Name
4786 (Related_Id => Chars (Def_Ent),
4788 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4792 Make_Package_Declaration (Loc,
4793 Specification => Make_Package_Specification (Loc,
4794 Defining_Unit_Name => Pack_Id,
4795 Visible_Declarations => Renaming_List,
4796 End_Label => Empty));
4798 Set_Instance_Spec (N, Pack_Decl);
4799 Set_Is_Generic_Instance (Pack_Id);
4800 Set_Debug_Info_Needed (Pack_Id);
4802 -- Case of not a compilation unit
4804 if Nkind (Parent (N)) /= N_Compilation_Unit then
4805 Mark_Rewrite_Insertion (Pack_Decl);
4806 Insert_Before (N, Pack_Decl);
4807 Set_Has_Completion (Pack_Id);
4809 -- Case of an instantiation that is a compilation unit
4811 -- Place declaration on current node so context is complete for
4812 -- analysis (including nested instantiations), and for use in a
4813 -- context_clause (see Analyze_With_Clause).
4816 Set_Unit (Parent (N), Pack_Decl);
4817 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4820 Analyze (Pack_Decl);
4821 Check_Formal_Packages (Pack_Id);
4822 Set_Is_Generic_Instance (Pack_Id, False);
4824 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4827 -- Body of the enclosing package is supplied when instantiating the
4828 -- subprogram body, after semantic analysis is completed.
4830 if Nkind (Parent (N)) = N_Compilation_Unit then
4832 -- Remove package itself from visibility, so it does not
4833 -- conflict with subprogram.
4835 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4837 -- Set name and scope of internal subprogram so that the proper
4838 -- external name will be generated. The proper scope is the scope
4839 -- of the wrapper package. We need to generate debugging info for
4840 -- the internal subprogram, so set flag accordingly.
4842 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4843 Set_Scope (Anon_Id, Scope (Pack_Id));
4845 -- Mark wrapper package as referenced, to avoid spurious warnings
4846 -- if the instantiation appears in various with_ clauses of
4847 -- subunits of the main unit.
4849 Set_Referenced (Pack_Id);
4852 Set_Is_Generic_Instance (Anon_Id);
4853 Set_Debug_Info_Needed (Anon_Id);
4854 Act_Decl_Id := New_Copy (Anon_Id);
4856 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4857 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4858 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4860 -- Subprogram instance comes from source only if generic does
4862 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4864 -- The signature may involve types that are not frozen yet, but the
4865 -- subprogram will be frozen at the point the wrapper package is
4866 -- frozen, so it does not need its own freeze node. In fact, if one
4867 -- is created, it might conflict with the freezing actions from the
4870 Set_Has_Delayed_Freeze (Anon_Id, False);
4872 -- If the instance is a child unit, mark the Id accordingly. Mark
4873 -- the anonymous entity as well, which is the real subprogram and
4874 -- which is used when the instance appears in a context clause.
4875 -- Similarly, propagate the Is_Eliminated flag to handle properly
4876 -- nested eliminated subprograms.
4878 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4879 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4880 New_Overloaded_Entity (Act_Decl_Id);
4881 Check_Eliminated (Act_Decl_Id);
4882 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4884 -- In compilation unit case, kill elaboration checks on the
4885 -- instantiation, since they are never needed -- the body is
4886 -- instantiated at the same point as the spec.
4888 if Nkind (Parent (N)) = N_Compilation_Unit then
4889 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4890 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4891 Set_Is_Compilation_Unit (Anon_Id);
4893 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4896 -- The instance is not a freezing point for the new subprogram
4898 Set_Is_Frozen (Act_Decl_Id, False);
4900 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4901 Valid_Operator_Definition (Act_Decl_Id);
4904 Set_Alias (Act_Decl_Id, Anon_Id);
4905 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4906 Set_Has_Completion (Act_Decl_Id);
4907 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4909 if Nkind (Parent (N)) = N_Compilation_Unit then
4910 Set_Body_Required (Parent (N), False);
4912 end Analyze_Instance_And_Renamings;
4914 -------------------------------
4915 -- Build_Subprogram_Renaming --
4916 -------------------------------
4918 procedure Build_Subprogram_Renaming is
4919 Renaming_Decl : Node_Id;
4920 Unit_Renaming : Node_Id;
4924 Make_Subprogram_Renaming_Declaration (Loc,
4927 (Specification (Original_Node (Gen_Decl)),
4929 Instantiating => True),
4930 Name => New_Occurrence_Of (Anon_Id, Loc));
4932 -- The generic may be a a child unit. The renaming needs an
4933 -- identifier with the proper name.
4935 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4936 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4938 -- If there is a formal subprogram with the same name as the unit
4939 -- itself, do not add this renaming declaration, to prevent
4940 -- ambiguities when there is a call with that name in the body.
4941 -- This is a partial and ugly fix for one ACATS test. ???
4943 Renaming_Decl := First (Renaming_List);
4944 while Present (Renaming_Decl) loop
4945 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4947 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4952 Next (Renaming_Decl);
4955 if No (Renaming_Decl) then
4956 Append (Unit_Renaming, Renaming_List);
4958 end Build_Subprogram_Renaming;
4960 -------------------------------------
4961 -- Instantiate_Subprogram_Contract --
4962 -------------------------------------
4964 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
4965 procedure Instantiate_Pragmas (First_Prag : Node_Id);
4966 -- Instantiate all contract-related source pragmas found in the list
4967 -- starting with pragma First_Prag. Each instantiated pragma is added
4968 -- to list Renaming_List.
4970 -------------------------
4971 -- Instantiate_Pragmas --
4972 -------------------------
4974 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
4975 Inst_Prag : Node_Id;
4980 while Present (Prag) loop
4981 if Is_Generic_Contract_Pragma (Prag) then
4983 Copy_Generic_Node (Prag, Empty, Instantiating => True);
4985 Set_Analyzed (Inst_Prag, False);
4986 Append_To (Renaming_List, Inst_Prag);
4989 Prag := Next_Pragma (Prag);
4991 end Instantiate_Pragmas;
4995 Items : constant Node_Id := Contract (Defining_Entity (Templ));
4997 -- Start of processing for Instantiate_Subprogram_Contract
5000 if Present (Items) then
5001 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5002 Instantiate_Pragmas (Contract_Test_Cases (Items));
5003 Instantiate_Pragmas (Classifications (Items));
5005 end Instantiate_Subprogram_Contract;
5009 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5010 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5012 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5013 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5014 -- Save the SPARK_Mode-related data for restore on exit
5016 Vis_Prims_List : Elist_Id := No_Elist;
5017 -- List of primitives made temporarily visible in the instantiation
5018 -- to match the visibility of the formal type
5020 -- Start of processing for Analyze_Subprogram_Instantiation
5023 Check_SPARK_05_Restriction ("generic is not allowed", N);
5025 -- Very first thing: check for special Text_IO unit in case we are
5026 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5027 -- such an instantiation is bogus (these are packages, not subprograms),
5028 -- but we get a better error message if we do this.
5030 Check_Text_IO_Special_Unit (Gen_Id);
5032 -- Make node global for error reporting
5034 Instantiation_Node := N;
5036 -- For package instantiations we turn off style checks, because they
5037 -- will have been emitted in the generic. For subprogram instantiations
5038 -- we want to apply at least the check on overriding indicators so we
5039 -- do not modify the style check status.
5041 -- The renaming declarations for the actuals do not come from source and
5042 -- will not generate spurious warnings.
5044 Preanalyze_Actuals (N);
5047 Env_Installed := True;
5048 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5049 Gen_Unit := Entity (Gen_Id);
5051 Generate_Reference (Gen_Unit, Gen_Id);
5053 if Nkind (Gen_Id) = N_Identifier
5054 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5057 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5060 if Etype (Gen_Unit) = Any_Type then
5065 -- Verify that it is a generic subprogram of the right kind, and that
5066 -- it does not lead to a circular instantiation.
5068 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5070 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5072 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5074 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5076 elsif In_Open_Scopes (Gen_Unit) then
5077 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5080 -- If the context of the instance is subject to SPARK_Mode "off",
5081 -- set the global flag which signals Analyze_Pragma to ignore all
5082 -- SPARK_Mode pragmas within the instance.
5084 if SPARK_Mode = Off then
5085 Ignore_Pragma_SPARK_Mode := True;
5088 Set_Entity (Gen_Id, Gen_Unit);
5089 Set_Is_Instantiated (Gen_Unit);
5091 if In_Extended_Main_Source_Unit (N) then
5092 Generate_Reference (Gen_Unit, N);
5095 -- If renaming, get original unit
5097 if Present (Renamed_Object (Gen_Unit))
5098 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5101 Gen_Unit := Renamed_Object (Gen_Unit);
5102 Set_Is_Instantiated (Gen_Unit);
5103 Generate_Reference (Gen_Unit, N);
5106 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5107 Error_Msg_Node_2 := Current_Scope;
5109 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5110 Circularity_Detected := True;
5111 Restore_Hidden_Primitives (Vis_Prims_List);
5115 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5117 -- Initialize renamings map, for error checking
5119 Generic_Renamings.Set_Last (0);
5120 Generic_Renamings_HTable.Reset;
5122 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5124 -- Copy original generic tree, to produce text for instantiation
5128 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5130 -- Inherit overriding indicator from instance node
5132 Act_Spec := Specification (Act_Tree);
5133 Set_Must_Override (Act_Spec, Must_Override (N));
5134 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5137 Analyze_Associations
5139 Formals => Generic_Formal_Declarations (Act_Tree),
5140 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5142 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5144 -- The subprogram itself cannot contain a nested instance, so the
5145 -- current parent is left empty.
5147 Set_Instance_Env (Gen_Unit, Empty);
5149 -- Build the subprogram declaration, which does not appear in the
5150 -- generic template, and give it a sloc consistent with that of the
5153 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5154 Set_Generic_Parent (Act_Spec, Gen_Unit);
5156 Make_Subprogram_Declaration (Sloc (Act_Spec),
5157 Specification => Act_Spec);
5159 -- The aspects have been copied previously, but they have to be
5160 -- linked explicitly to the new subprogram declaration. Explicit
5161 -- pre/postconditions on the instance are analyzed below, in a
5164 Move_Aspects (Act_Tree, To => Act_Decl);
5165 Set_Categorization_From_Pragmas (Act_Decl);
5167 if Parent_Installed then
5171 Append (Act_Decl, Renaming_List);
5173 -- Contract-related source pragmas that follow a generic subprogram
5174 -- must be instantiated explicitly because they are not part of the
5175 -- subprogram template.
5177 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5178 Build_Subprogram_Renaming;
5180 Analyze_Instance_And_Renamings;
5182 -- If the generic is marked Import (Intrinsic), then so is the
5183 -- instance. This indicates that there is no body to instantiate. If
5184 -- generic is marked inline, so it the instance, and the anonymous
5185 -- subprogram it renames. If inlined, or else if inlining is enabled
5186 -- for the compilation, we generate the instance body even if it is
5187 -- not within the main unit.
5189 if Is_Intrinsic_Subprogram (Gen_Unit) then
5190 Set_Is_Intrinsic_Subprogram (Anon_Id);
5191 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5193 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5194 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5198 -- Inherit convention from generic unit. Intrinsic convention, as for
5199 -- an instance of unchecked conversion, is not inherited because an
5200 -- explicit Ada instance has been created.
5202 if Has_Convention_Pragma (Gen_Unit)
5203 and then Convention (Gen_Unit) /= Convention_Intrinsic
5205 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5206 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5209 Generate_Definition (Act_Decl_Id);
5211 -- Inherit all inlining-related flags which apply to the generic in
5212 -- the subprogram and its declaration.
5214 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5215 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5217 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5218 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5220 Set_Has_Pragma_Inline_Always
5221 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5222 Set_Has_Pragma_Inline_Always
5223 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5225 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5226 Check_Elab_Instantiation (N);
5229 if Is_Dispatching_Operation (Act_Decl_Id)
5230 and then Ada_Version >= Ada_2005
5236 Formal := First_Formal (Act_Decl_Id);
5237 while Present (Formal) loop
5238 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5239 and then Is_Controlling_Formal (Formal)
5240 and then not Can_Never_Be_Null (Formal)
5243 ("access parameter& is controlling,", N, Formal);
5245 ("\corresponding parameter of & must be "
5246 & "explicitly null-excluding", N, Gen_Id);
5249 Next_Formal (Formal);
5254 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5256 Validate_Categorization_Dependency (N, Act_Decl_Id);
5258 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5259 Inherit_Context (Gen_Decl, N);
5261 Restore_Private_Views (Pack_Id, False);
5263 -- If the context requires a full instantiation, mark node for
5264 -- subsequent construction of the body.
5266 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5267 Check_Forward_Instantiation (Gen_Decl);
5269 -- The wrapper package is always delayed, because it does not
5270 -- constitute a freeze point, but to insure that the freeze
5271 -- node is placed properly, it is created directly when
5272 -- instantiating the body (otherwise the freeze node might
5273 -- appear to early for nested instantiations).
5275 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5277 -- For ASIS purposes, indicate that the wrapper package has
5278 -- replaced the instantiation node.
5280 Rewrite (N, Unit (Parent (N)));
5281 Set_Unit (Parent (N), N);
5284 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5286 -- Replace instance node for library-level instantiations of
5287 -- intrinsic subprograms, for ASIS use.
5289 Rewrite (N, Unit (Parent (N)));
5290 Set_Unit (Parent (N), N);
5293 if Parent_Installed then
5297 Restore_Hidden_Primitives (Vis_Prims_List);
5299 Env_Installed := False;
5300 Generic_Renamings.Set_Last (0);
5301 Generic_Renamings_HTable.Reset;
5303 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5304 SPARK_Mode := Save_SM;
5305 SPARK_Mode_Pragma := Save_SMP;
5307 if SPARK_Mode = On then
5308 Dynamic_Elaboration_Checks := False;
5314 if Has_Aspects (N) then
5315 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5319 when Instantiation_Error =>
5320 if Parent_Installed then
5324 if Env_Installed then
5328 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5329 SPARK_Mode := Save_SM;
5330 SPARK_Mode_Pragma := Save_SMP;
5332 if SPARK_Mode = On then
5333 Dynamic_Elaboration_Checks := False;
5335 end Analyze_Subprogram_Instantiation;
5337 -------------------------
5338 -- Get_Associated_Node --
5339 -------------------------
5341 function Get_Associated_Node (N : Node_Id) return Node_Id is
5345 Assoc := Associated_Node (N);
5347 if Nkind (Assoc) /= Nkind (N) then
5350 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5354 -- If the node is part of an inner generic, it may itself have been
5355 -- remapped into a further generic copy. Associated_Node is otherwise
5356 -- used for the entity of the node, and will be of a different node
5357 -- kind, or else N has been rewritten as a literal or function call.
5359 while Present (Associated_Node (Assoc))
5360 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5362 Assoc := Associated_Node (Assoc);
5365 -- Follow and additional link in case the final node was rewritten.
5366 -- This can only happen with nested generic units.
5368 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5369 and then Present (Associated_Node (Assoc))
5370 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5371 N_Explicit_Dereference,
5376 Assoc := Associated_Node (Assoc);
5379 -- An additional special case: an unconstrained type in an object
5380 -- declaration may have been rewritten as a local subtype constrained
5381 -- by the expression in the declaration. We need to recover the
5382 -- original entity which may be global.
5384 if Present (Original_Node (Assoc))
5385 and then Nkind (Parent (N)) = N_Object_Declaration
5387 Assoc := Original_Node (Assoc);
5392 end Get_Associated_Node;
5394 ----------------------------
5395 -- Build_Function_Wrapper --
5396 ----------------------------
5398 function Build_Function_Wrapper
5399 (Formal_Subp : Entity_Id;
5400 Actual_Subp : Entity_Id) return Node_Id
5402 Loc : constant Source_Ptr := Sloc (Current_Scope);
5403 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5406 Func_Name : Node_Id;
5408 Parm_Type : Node_Id;
5409 Profile : List_Id := New_List;
5416 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5418 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5419 Set_Ekind (Func, E_Function);
5420 Set_Is_Generic_Actual_Subprogram (Func);
5422 Actuals := New_List;
5423 Profile := New_List;
5425 Act_F := First_Formal (Actual_Subp);
5426 Form_F := First_Formal (Formal_Subp);
5427 while Present (Form_F) loop
5429 -- Create new formal for profile of wrapper, and add a reference
5430 -- to it in the list of actuals for the enclosing call. The name
5431 -- must be that of the formal in the formal subprogram, because
5432 -- calls to it in the generic body may use named associations.
5434 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5437 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5440 Make_Parameter_Specification (Loc,
5441 Defining_Identifier => New_F,
5442 Parameter_Type => Parm_Type));
5444 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5445 Next_Formal (Form_F);
5447 if Present (Act_F) then
5448 Next_Formal (Act_F);
5453 Make_Function_Specification (Loc,
5454 Defining_Unit_Name => Func,
5455 Parameter_Specifications => Profile,
5456 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5459 Make_Expression_Function (Loc,
5460 Specification => Spec,
5462 Make_Function_Call (Loc,
5464 Parameter_Associations => Actuals));
5467 end Build_Function_Wrapper;
5469 ----------------------------
5470 -- Build_Operator_Wrapper --
5471 ----------------------------
5473 function Build_Operator_Wrapper
5474 (Formal_Subp : Entity_Id;
5475 Actual_Subp : Entity_Id) return Node_Id
5477 Loc : constant Source_Ptr := Sloc (Current_Scope);
5478 Ret_Type : constant Entity_Id :=
5479 Get_Instance_Of (Etype (Formal_Subp));
5480 Op_Type : constant Entity_Id :=
5481 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5482 Is_Binary : constant Boolean :=
5483 Present (Next_Formal (First_Formal (Formal_Subp)));
5494 Op_Name := Chars (Actual_Subp);
5496 -- Create entities for wrapper function and its formals
5498 F1 := Make_Temporary (Loc, 'A');
5499 F2 := Make_Temporary (Loc, 'B');
5500 L := New_Occurrence_Of (F1, Loc);
5501 R := New_Occurrence_Of (F2, Loc);
5503 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5504 Set_Ekind (Func, E_Function);
5505 Set_Is_Generic_Actual_Subprogram (Func);
5508 Make_Function_Specification (Loc,
5509 Defining_Unit_Name => Func,
5510 Parameter_Specifications => New_List (
5511 Make_Parameter_Specification (Loc,
5512 Defining_Identifier => F1,
5513 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5514 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5517 Append_To (Parameter_Specifications (Spec),
5518 Make_Parameter_Specification (Loc,
5519 Defining_Identifier => F2,
5520 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5523 -- Build expression as a function call, or as an operator node
5524 -- that corresponds to the name of the actual, starting with
5525 -- binary operators.
5527 if Op_Name not in Any_Operator_Name then
5529 Make_Function_Call (Loc,
5531 New_Occurrence_Of (Actual_Subp, Loc),
5532 Parameter_Associations => New_List (L));
5535 Append_To (Parameter_Associations (Expr), R);
5540 elsif Is_Binary then
5541 if Op_Name = Name_Op_And then
5542 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5543 elsif Op_Name = Name_Op_Or then
5544 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5545 elsif Op_Name = Name_Op_Xor then
5546 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5547 elsif Op_Name = Name_Op_Eq then
5548 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5549 elsif Op_Name = Name_Op_Ne then
5550 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5551 elsif Op_Name = Name_Op_Le then
5552 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5553 elsif Op_Name = Name_Op_Gt then
5554 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5555 elsif Op_Name = Name_Op_Ge then
5556 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5557 elsif Op_Name = Name_Op_Lt then
5558 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5559 elsif Op_Name = Name_Op_Add then
5560 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5561 elsif Op_Name = Name_Op_Subtract then
5562 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5563 elsif Op_Name = Name_Op_Concat then
5564 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5565 elsif Op_Name = Name_Op_Multiply then
5566 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5567 elsif Op_Name = Name_Op_Divide then
5568 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5569 elsif Op_Name = Name_Op_Mod then
5570 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5571 elsif Op_Name = Name_Op_Rem then
5572 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5573 elsif Op_Name = Name_Op_Expon then
5574 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5580 if Op_Name = Name_Op_Add then
5581 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5582 elsif Op_Name = Name_Op_Subtract then
5583 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5584 elsif Op_Name = Name_Op_Abs then
5585 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5586 elsif Op_Name = Name_Op_Not then
5587 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5592 Make_Expression_Function (Loc,
5593 Specification => Spec,
5594 Expression => Expr);
5597 end Build_Operator_Wrapper;
5599 -------------------------------------------
5600 -- Build_Instance_Compilation_Unit_Nodes --
5601 -------------------------------------------
5603 procedure Build_Instance_Compilation_Unit_Nodes
5608 Decl_Cunit : Node_Id;
5609 Body_Cunit : Node_Id;
5611 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5612 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5615 -- A new compilation unit node is built for the instance declaration
5618 Make_Compilation_Unit (Sloc (N),
5619 Context_Items => Empty_List,
5621 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5623 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5625 -- The new compilation unit is linked to its body, but both share the
5626 -- same file, so we do not set Body_Required on the new unit so as not
5627 -- to create a spurious dependency on a non-existent body in the ali.
5628 -- This simplifies CodePeer unit traversal.
5630 -- We use the original instantiation compilation unit as the resulting
5631 -- compilation unit of the instance, since this is the main unit.
5633 Rewrite (N, Act_Body);
5635 -- Propagate the aspect specifications from the package body template to
5636 -- the instantiated version of the package body.
5638 if Has_Aspects (Act_Body) then
5639 Set_Aspect_Specifications
5640 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5643 Body_Cunit := Parent (N);
5645 -- The two compilation unit nodes are linked by the Library_Unit field
5647 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5648 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5650 -- Preserve the private nature of the package if needed
5652 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5654 -- If the instance is not the main unit, its context, categorization
5655 -- and elaboration entity are not relevant to the compilation.
5657 if Body_Cunit /= Cunit (Main_Unit) then
5658 Make_Instance_Unit (Body_Cunit, In_Main => False);
5662 -- The context clause items on the instantiation, which are now attached
5663 -- to the body compilation unit (since the body overwrote the original
5664 -- instantiation node), semantically belong on the spec, so copy them
5665 -- there. It's harmless to leave them on the body as well. In fact one
5666 -- could argue that they belong in both places.
5668 Citem := First (Context_Items (Body_Cunit));
5669 while Present (Citem) loop
5670 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5674 -- Propagate categorization flags on packages, so that they appear in
5675 -- the ali file for the spec of the unit.
5677 if Ekind (New_Main) = E_Package then
5678 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5679 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5680 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5681 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5682 Set_Is_Remote_Call_Interface
5683 (Old_Main, Is_Remote_Call_Interface (New_Main));
5686 -- Make entry in Units table, so that binder can generate call to
5687 -- elaboration procedure for body, if any.
5689 Make_Instance_Unit (Body_Cunit, In_Main => True);
5690 Main_Unit_Entity := New_Main;
5691 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5693 -- Build elaboration entity, since the instance may certainly generate
5694 -- elaboration code requiring a flag for protection.
5696 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5697 end Build_Instance_Compilation_Unit_Nodes;
5699 -----------------------------
5700 -- Check_Access_Definition --
5701 -----------------------------
5703 procedure Check_Access_Definition (N : Node_Id) is
5706 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5708 end Check_Access_Definition;
5710 -----------------------------------
5711 -- Check_Formal_Package_Instance --
5712 -----------------------------------
5714 -- If the formal has specific parameters, they must match those of the
5715 -- actual. Both of them are instances, and the renaming declarations for
5716 -- their formal parameters appear in the same order in both. The analyzed
5717 -- formal has been analyzed in the context of the current instance.
5719 procedure Check_Formal_Package_Instance
5720 (Formal_Pack : Entity_Id;
5721 Actual_Pack : Entity_Id)
5723 E1 : Entity_Id := First_Entity (Actual_Pack);
5724 E2 : Entity_Id := First_Entity (Formal_Pack);
5729 procedure Check_Mismatch (B : Boolean);
5730 -- Common error routine for mismatch between the parameters of the
5731 -- actual instance and those of the formal package.
5733 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5734 -- The formal may come from a nested formal package, and the actual may
5735 -- have been constant-folded. To determine whether the two denote the
5736 -- same entity we may have to traverse several definitions to recover
5737 -- the ultimate entity that they refer to.
5739 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5740 -- Similarly, if the formal comes from a nested formal package, the
5741 -- actual may designate the formal through multiple renamings, which
5742 -- have to be followed to determine the original variable in question.
5744 --------------------
5745 -- Check_Mismatch --
5746 --------------------
5748 procedure Check_Mismatch (B : Boolean) is
5749 Kind : constant Node_Kind := Nkind (Parent (E2));
5752 if Kind = N_Formal_Type_Declaration then
5755 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5756 N_Formal_Package_Declaration)
5757 or else Kind in N_Formal_Subprogram_Declaration
5761 -- Ada 2012: If both formal and actual are incomplete types they
5764 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5769 ("actual for & in actual instance does not match formal",
5770 Parent (Actual_Pack), E1);
5774 --------------------------------
5775 -- Same_Instantiated_Constant --
5776 --------------------------------
5778 function Same_Instantiated_Constant
5779 (E1, E2 : Entity_Id) return Boolean
5785 while Present (Ent) loop
5789 elsif Ekind (Ent) /= E_Constant then
5792 elsif Is_Entity_Name (Constant_Value (Ent)) then
5793 if Entity (Constant_Value (Ent)) = E1 then
5796 Ent := Entity (Constant_Value (Ent));
5799 -- The actual may be a constant that has been folded. Recover
5802 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5803 Ent := Entity (Original_Node (Constant_Value (Ent)));
5811 end Same_Instantiated_Constant;
5813 --------------------------------
5814 -- Same_Instantiated_Variable --
5815 --------------------------------
5817 function Same_Instantiated_Variable
5818 (E1, E2 : Entity_Id) return Boolean
5820 function Original_Entity (E : Entity_Id) return Entity_Id;
5821 -- Follow chain of renamings to the ultimate ancestor
5823 ---------------------
5824 -- Original_Entity --
5825 ---------------------
5827 function Original_Entity (E : Entity_Id) return Entity_Id is
5832 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5833 and then Present (Renamed_Object (Orig))
5834 and then Is_Entity_Name (Renamed_Object (Orig))
5836 Orig := Entity (Renamed_Object (Orig));
5840 end Original_Entity;
5842 -- Start of processing for Same_Instantiated_Variable
5845 return Ekind (E1) = Ekind (E2)
5846 and then Original_Entity (E1) = Original_Entity (E2);
5847 end Same_Instantiated_Variable;
5849 -- Start of processing for Check_Formal_Package_Instance
5852 while Present (E1) and then Present (E2) loop
5853 exit when Ekind (E1) = E_Package
5854 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5856 -- If the formal is the renaming of the formal package, this
5857 -- is the end of its formal part, which may occur before the
5858 -- end of the formal part in the actual in the presence of
5859 -- defaulted parameters in the formal package.
5861 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5862 and then Renamed_Entity (E2) = Scope (E2);
5864 -- The analysis of the actual may generate additional internal
5865 -- entities. If the formal is defaulted, there is no corresponding
5866 -- analysis and the internal entities must be skipped, until we
5867 -- find corresponding entities again.
5869 if Comes_From_Source (E2)
5870 and then not Comes_From_Source (E1)
5871 and then Chars (E1) /= Chars (E2)
5873 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5881 -- If the formal entity comes from a formal declaration, it was
5882 -- defaulted in the formal package, and no check is needed on it.
5884 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5887 -- Ditto for defaulted formal subprograms.
5889 elsif Is_Overloadable (E1)
5890 and then Nkind (Unit_Declaration_Node (E2)) in
5891 N_Formal_Subprogram_Declaration
5895 elsif Is_Type (E1) then
5897 -- Subtypes must statically match. E1, E2 are the local entities
5898 -- that are subtypes of the actuals. Itypes generated for other
5899 -- parameters need not be checked, the check will be performed
5900 -- on the parameters themselves.
5902 -- If E2 is a formal type declaration, it is a defaulted parameter
5903 -- and needs no checking.
5905 if not Is_Itype (E1) and then not Is_Itype (E2) then
5908 or else Etype (E1) /= Etype (E2)
5909 or else not Subtypes_Statically_Match (E1, E2));
5912 elsif Ekind (E1) = E_Constant then
5914 -- IN parameters must denote the same static value, or the same
5915 -- constant, or the literal null.
5917 Expr1 := Expression (Parent (E1));
5919 if Ekind (E2) /= E_Constant then
5920 Check_Mismatch (True);
5923 Expr2 := Expression (Parent (E2));
5926 if Is_OK_Static_Expression (Expr1) then
5927 if not Is_OK_Static_Expression (Expr2) then
5928 Check_Mismatch (True);
5930 elsif Is_Discrete_Type (Etype (E1)) then
5932 V1 : constant Uint := Expr_Value (Expr1);
5933 V2 : constant Uint := Expr_Value (Expr2);
5935 Check_Mismatch (V1 /= V2);
5938 elsif Is_Real_Type (Etype (E1)) then
5940 V1 : constant Ureal := Expr_Value_R (Expr1);
5941 V2 : constant Ureal := Expr_Value_R (Expr2);
5943 Check_Mismatch (V1 /= V2);
5946 elsif Is_String_Type (Etype (E1))
5947 and then Nkind (Expr1) = N_String_Literal
5949 if Nkind (Expr2) /= N_String_Literal then
5950 Check_Mismatch (True);
5953 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5957 elsif Is_Entity_Name (Expr1) then
5958 if Is_Entity_Name (Expr2) then
5959 if Entity (Expr1) = Entity (Expr2) then
5963 (not Same_Instantiated_Constant
5964 (Entity (Expr1), Entity (Expr2)));
5968 Check_Mismatch (True);
5971 elsif Is_Entity_Name (Original_Node (Expr1))
5972 and then Is_Entity_Name (Expr2)
5973 and then Same_Instantiated_Constant
5974 (Entity (Original_Node (Expr1)), Entity (Expr2))
5978 elsif Nkind (Expr1) = N_Null then
5979 Check_Mismatch (Nkind (Expr1) /= N_Null);
5982 Check_Mismatch (True);
5985 elsif Ekind (E1) = E_Variable then
5986 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5988 elsif Ekind (E1) = E_Package then
5990 (Ekind (E1) /= Ekind (E2)
5991 or else Renamed_Object (E1) /= Renamed_Object (E2));
5993 elsif Is_Overloadable (E1) then
5995 -- Verify that the actual subprograms match. Note that actuals
5996 -- that are attributes are rewritten as subprograms. If the
5997 -- subprogram in the formal package is defaulted, no check is
5998 -- needed. Note that this can only happen in Ada 2005 when the
5999 -- formal package can be partially parameterized.
6001 if Nkind (Unit_Declaration_Node (E1)) =
6002 N_Subprogram_Renaming_Declaration
6003 and then From_Default (Unit_Declaration_Node (E1))
6007 -- If the formal package has an "others" box association that
6008 -- covers this formal, there is no need for a check either.
6010 elsif Nkind (Unit_Declaration_Node (E2)) in
6011 N_Formal_Subprogram_Declaration
6012 and then Box_Present (Unit_Declaration_Node (E2))
6016 -- No check needed if subprogram is a defaulted null procedure
6018 elsif No (Alias (E2))
6019 and then Ekind (E2) = E_Procedure
6021 Null_Present (Specification (Unit_Declaration_Node (E2)))
6025 -- Otherwise the actual in the formal and the actual in the
6026 -- instantiation of the formal must match, up to renamings.
6030 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6034 raise Program_Error;
6041 end Check_Formal_Package_Instance;
6043 ---------------------------
6044 -- Check_Formal_Packages --
6045 ---------------------------
6047 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6049 Formal_P : Entity_Id;
6052 -- Iterate through the declarations in the instance, looking for package
6053 -- renaming declarations that denote instances of formal packages. Stop
6054 -- when we find the renaming of the current package itself. The
6055 -- declaration for a formal package without a box is followed by an
6056 -- internal entity that repeats the instantiation.
6058 E := First_Entity (P_Id);
6059 while Present (E) loop
6060 if Ekind (E) = E_Package then
6061 if Renamed_Object (E) = P_Id then
6064 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6067 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6068 Formal_P := Next_Entity (E);
6069 Check_Formal_Package_Instance (Formal_P, E);
6071 -- After checking, remove the internal validating package. It
6072 -- is only needed for semantic checks, and as it may contain
6073 -- generic formal declarations it should not reach gigi.
6075 Remove (Unit_Declaration_Node (Formal_P));
6081 end Check_Formal_Packages;
6083 ---------------------------------
6084 -- Check_Forward_Instantiation --
6085 ---------------------------------
6087 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6089 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6092 -- The instantiation appears before the generic body if we are in the
6093 -- scope of the unit containing the generic, either in its spec or in
6094 -- the package body, and before the generic body.
6096 if Ekind (Gen_Comp) = E_Package_Body then
6097 Gen_Comp := Spec_Entity (Gen_Comp);
6100 if In_Open_Scopes (Gen_Comp)
6101 and then No (Corresponding_Body (Decl))
6106 and then not Is_Compilation_Unit (S)
6107 and then not Is_Child_Unit (S)
6109 if Ekind (S) = E_Package then
6110 Set_Has_Forward_Instantiation (S);
6116 end Check_Forward_Instantiation;
6118 ---------------------------
6119 -- Check_Generic_Actuals --
6120 ---------------------------
6122 -- The visibility of the actuals may be different between the point of
6123 -- generic instantiation and the instantiation of the body.
6125 procedure Check_Generic_Actuals
6126 (Instance : Entity_Id;
6127 Is_Formal_Box : Boolean)
6132 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6133 -- For a formal that is an array type, the component type is often a
6134 -- previous formal in the same unit. The privacy status of the component
6135 -- type will have been examined earlier in the traversal of the
6136 -- corresponding actuals, and this status should not be modified for
6137 -- the array (sub)type itself. However, if the base type of the array
6138 -- (sub)type is private, its full view must be restored in the body to
6139 -- be consistent with subsequent index subtypes, etc.
6141 -- To detect this case we have to rescan the list of formals, which is
6142 -- usually short enough to ignore the resulting inefficiency.
6144 -----------------------------
6145 -- Denotes_Previous_Actual --
6146 -----------------------------
6148 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6152 Prev := First_Entity (Instance);
6153 while Present (Prev) loop
6155 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6156 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6157 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6170 end Denotes_Previous_Actual;
6172 -- Start of processing for Check_Generic_Actuals
6175 E := First_Entity (Instance);
6176 while Present (E) loop
6178 and then Nkind (Parent (E)) = N_Subtype_Declaration
6179 and then Scope (Etype (E)) /= Instance
6180 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6182 if Is_Array_Type (E)
6183 and then not Is_Private_Type (Etype (E))
6184 and then Denotes_Previous_Actual (Component_Type (E))
6188 Check_Private_View (Subtype_Indication (Parent (E)));
6191 Set_Is_Generic_Actual_Type (E, True);
6192 Set_Is_Hidden (E, False);
6193 Set_Is_Potentially_Use_Visible (E,
6196 -- We constructed the generic actual type as a subtype of the
6197 -- supplied type. This means that it normally would not inherit
6198 -- subtype specific attributes of the actual, which is wrong for
6199 -- the generic case.
6201 Astype := Ancestor_Subtype (E);
6205 -- This can happen when E is an itype that is the full view of
6206 -- a private type completed, e.g. with a constrained array. In
6207 -- that case, use the first subtype, which will carry size
6208 -- information. The base type itself is unconstrained and will
6211 Astype := First_Subtype (E);
6214 Set_Size_Info (E, (Astype));
6215 Set_RM_Size (E, RM_Size (Astype));
6216 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6218 if Is_Discrete_Or_Fixed_Point_Type (E) then
6219 Set_RM_Size (E, RM_Size (Astype));
6221 -- In nested instances, the base type of an access actual may
6222 -- itself be private, and need to be exchanged.
6224 elsif Is_Access_Type (E)
6225 and then Is_Private_Type (Etype (E))
6228 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6231 elsif Ekind (E) = E_Package then
6233 -- If this is the renaming for the current instance, we're done.
6234 -- Otherwise it is a formal package. If the corresponding formal
6235 -- was declared with a box, the (instantiations of the) generic
6236 -- formal part are also visible. Otherwise, ignore the entity
6237 -- created to validate the actuals.
6239 if Renamed_Object (E) = Instance then
6242 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6245 -- The visibility of a formal of an enclosing generic is already
6248 elsif Denotes_Formal_Package (E) then
6251 elsif Present (Associated_Formal_Package (E))
6252 and then not Is_Generic_Formal (E)
6254 if Box_Present (Parent (Associated_Formal_Package (E))) then
6255 Check_Generic_Actuals (Renamed_Object (E), True);
6258 Check_Generic_Actuals (Renamed_Object (E), False);
6261 Set_Is_Hidden (E, False);
6264 -- If this is a subprogram instance (in a wrapper package) the
6265 -- actual is fully visible.
6267 elsif Is_Wrapper_Package (Instance) then
6268 Set_Is_Hidden (E, False);
6270 -- If the formal package is declared with a box, or if the formal
6271 -- parameter is defaulted, it is visible in the body.
6273 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6274 Set_Is_Hidden (E, False);
6277 if Ekind (E) = E_Constant then
6279 -- If the type of the actual is a private type declared in the
6280 -- enclosing scope of the generic unit, the body of the generic
6281 -- sees the full view of the type (because it has to appear in
6282 -- the corresponding package body). If the type is private now,
6283 -- exchange views to restore the proper visiblity in the instance.
6286 Typ : constant Entity_Id := Base_Type (Etype (E));
6287 -- The type of the actual
6292 Parent_Scope : Entity_Id;
6293 -- The enclosing scope of the generic unit
6296 if Is_Wrapper_Package (Instance) then
6300 (Unit_Declaration_Node
6301 (Related_Instance (Instance))));
6304 Generic_Parent (Package_Specification (Instance));
6307 Parent_Scope := Scope (Gen_Id);
6309 -- The exchange is only needed if the generic is defined
6310 -- within a package which is not a common ancestor of the
6311 -- scope of the instance, and is not already in scope.
6313 if Is_Private_Type (Typ)
6314 and then Scope (Typ) = Parent_Scope
6315 and then Scope (Instance) /= Parent_Scope
6316 and then Ekind (Parent_Scope) = E_Package
6317 and then not Is_Child_Unit (Gen_Id)
6321 -- If the type of the entity is a subtype, it may also have
6322 -- to be made visible, together with the base type of its
6323 -- full view, after exchange.
6325 if Is_Private_Type (Etype (E)) then
6326 Switch_View (Etype (E));
6327 Switch_View (Base_Type (Etype (E)));
6335 end Check_Generic_Actuals;
6337 ------------------------------
6338 -- Check_Generic_Child_Unit --
6339 ------------------------------
6341 procedure Check_Generic_Child_Unit
6343 Parent_Installed : in out Boolean)
6345 Loc : constant Source_Ptr := Sloc (Gen_Id);
6346 Gen_Par : Entity_Id := Empty;
6348 Inst_Par : Entity_Id;
6351 function Find_Generic_Child
6353 Id : Node_Id) return Entity_Id;
6354 -- Search generic parent for possible child unit with the given name
6356 function In_Enclosing_Instance return Boolean;
6357 -- Within an instance of the parent, the child unit may be denoted by
6358 -- a simple name, or an abbreviated expanded name. Examine enclosing
6359 -- scopes to locate a possible parent instantiation.
6361 ------------------------
6362 -- Find_Generic_Child --
6363 ------------------------
6365 function Find_Generic_Child
6367 Id : Node_Id) return Entity_Id
6372 -- If entity of name is already set, instance has already been
6373 -- resolved, e.g. in an enclosing instantiation.
6375 if Present (Entity (Id)) then
6376 if Scope (Entity (Id)) = Scop then
6383 E := First_Entity (Scop);
6384 while Present (E) loop
6385 if Chars (E) = Chars (Id)
6386 and then Is_Child_Unit (E)
6388 if Is_Child_Unit (E)
6389 and then not Is_Visible_Lib_Unit (E)
6392 ("generic child unit& is not visible", Gen_Id, E);
6404 end Find_Generic_Child;
6406 ---------------------------
6407 -- In_Enclosing_Instance --
6408 ---------------------------
6410 function In_Enclosing_Instance return Boolean is
6411 Enclosing_Instance : Node_Id;
6412 Instance_Decl : Node_Id;
6415 -- We do not inline any call that contains instantiations, except
6416 -- for instantiations of Unchecked_Conversion, so if we are within
6417 -- an inlined body the current instance does not require parents.
6419 if In_Inlined_Body then
6420 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6424 -- Loop to check enclosing scopes
6426 Enclosing_Instance := Current_Scope;
6427 while Present (Enclosing_Instance) loop
6428 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6430 if Ekind (Enclosing_Instance) = E_Package
6431 and then Is_Generic_Instance (Enclosing_Instance)
6433 (Generic_Parent (Specification (Instance_Decl)))
6435 -- Check whether the generic we are looking for is a child of
6438 E := Find_Generic_Child
6439 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6440 exit when Present (E);
6446 Enclosing_Instance := Scope (Enclosing_Instance);
6458 Make_Expanded_Name (Loc,
6460 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6461 Selector_Name => New_Occurrence_Of (E, Loc)));
6463 Set_Entity (Gen_Id, E);
6464 Set_Etype (Gen_Id, Etype (E));
6465 Parent_Installed := False; -- Already in scope.
6468 end In_Enclosing_Instance;
6470 -- Start of processing for Check_Generic_Child_Unit
6473 -- If the name of the generic is given by a selected component, it may
6474 -- be the name of a generic child unit, and the prefix is the name of an
6475 -- instance of the parent, in which case the child unit must be visible.
6476 -- If this instance is not in scope, it must be placed there and removed
6477 -- after instantiation, because what is being instantiated is not the
6478 -- original child, but the corresponding child present in the instance
6481 -- If the child is instantiated within the parent, it can be given by
6482 -- a simple name. In this case the instance is already in scope, but
6483 -- the child generic must be recovered from the generic parent as well.
6485 if Nkind (Gen_Id) = N_Selected_Component then
6486 S := Selector_Name (Gen_Id);
6487 Analyze (Prefix (Gen_Id));
6488 Inst_Par := Entity (Prefix (Gen_Id));
6490 if Ekind (Inst_Par) = E_Package
6491 and then Present (Renamed_Object (Inst_Par))
6493 Inst_Par := Renamed_Object (Inst_Par);
6496 if Ekind (Inst_Par) = E_Package then
6497 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6498 Gen_Par := Generic_Parent (Parent (Inst_Par));
6500 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6502 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6504 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6507 elsif Ekind (Inst_Par) = E_Generic_Package
6508 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6510 -- A formal package may be a real child package, and not the
6511 -- implicit instance within a parent. In this case the child is
6512 -- not visible and has to be retrieved explicitly as well.
6514 Gen_Par := Inst_Par;
6517 if Present (Gen_Par) then
6519 -- The prefix denotes an instantiation. The entity itself may be a
6520 -- nested generic, or a child unit.
6522 E := Find_Generic_Child (Gen_Par, S);
6525 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6526 Set_Entity (Gen_Id, E);
6527 Set_Etype (Gen_Id, Etype (E));
6529 Set_Etype (S, Etype (E));
6531 -- Indicate that this is a reference to the parent
6533 if In_Extended_Main_Source_Unit (Gen_Id) then
6534 Set_Is_Instantiated (Inst_Par);
6537 -- A common mistake is to replicate the naming scheme of a
6538 -- hierarchy by instantiating a generic child directly, rather
6539 -- than the implicit child in a parent instance:
6541 -- generic .. package Gpar is ..
6542 -- generic .. package Gpar.Child is ..
6543 -- package Par is new Gpar ();
6546 -- package Par.Child is new Gpar.Child ();
6547 -- rather than Par.Child
6549 -- In this case the instantiation is within Par, which is an
6550 -- instance, but Gpar does not denote Par because we are not IN
6551 -- the instance of Gpar, so this is illegal. The test below
6552 -- recognizes this particular case.
6554 if Is_Child_Unit (E)
6555 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6556 and then (not In_Instance
6557 or else Nkind (Parent (Parent (Gen_Id))) =
6561 ("prefix of generic child unit must be instance of parent",
6565 if not In_Open_Scopes (Inst_Par)
6566 and then Nkind (Parent (Gen_Id)) not in
6567 N_Generic_Renaming_Declaration
6569 Install_Parent (Inst_Par);
6570 Parent_Installed := True;
6572 elsif In_Open_Scopes (Inst_Par) then
6574 -- If the parent is already installed, install the actuals
6575 -- for its formal packages. This is necessary when the child
6576 -- instance is a child of the parent instance: in this case,
6577 -- the parent is placed on the scope stack but the formal
6578 -- packages are not made visible.
6580 Install_Formal_Packages (Inst_Par);
6584 -- If the generic parent does not contain an entity that
6585 -- corresponds to the selector, the instance doesn't either.
6586 -- Analyzing the node will yield the appropriate error message.
6587 -- If the entity is not a child unit, then it is an inner
6588 -- generic in the parent.
6596 if Is_Child_Unit (Entity (Gen_Id))
6598 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6599 and then not In_Open_Scopes (Inst_Par)
6601 Install_Parent (Inst_Par);
6602 Parent_Installed := True;
6604 -- The generic unit may be the renaming of the implicit child
6605 -- present in an instance. In that case the parent instance is
6606 -- obtained from the name of the renamed entity.
6608 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6609 and then Present (Renamed_Entity (Entity (Gen_Id)))
6610 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6613 Renamed_Package : constant Node_Id :=
6614 Name (Parent (Entity (Gen_Id)));
6616 if Nkind (Renamed_Package) = N_Expanded_Name then
6617 Inst_Par := Entity (Prefix (Renamed_Package));
6618 Install_Parent (Inst_Par);
6619 Parent_Installed := True;
6625 elsif Nkind (Gen_Id) = N_Expanded_Name then
6627 -- Entity already present, analyze prefix, whose meaning may be
6628 -- an instance in the current context. If it is an instance of
6629 -- a relative within another, the proper parent may still have
6630 -- to be installed, if they are not of the same generation.
6632 Analyze (Prefix (Gen_Id));
6634 -- In the unlikely case that a local declaration hides the name
6635 -- of the parent package, locate it on the homonym chain. If the
6636 -- context is an instance of the parent, the renaming entity is
6639 Inst_Par := Entity (Prefix (Gen_Id));
6640 while Present (Inst_Par)
6641 and then not Is_Package_Or_Generic_Package (Inst_Par)
6643 Inst_Par := Homonym (Inst_Par);
6646 pragma Assert (Present (Inst_Par));
6647 Set_Entity (Prefix (Gen_Id), Inst_Par);
6649 if In_Enclosing_Instance then
6652 elsif Present (Entity (Gen_Id))
6653 and then Is_Child_Unit (Entity (Gen_Id))
6654 and then not In_Open_Scopes (Inst_Par)
6656 Install_Parent (Inst_Par);
6657 Parent_Installed := True;
6660 elsif In_Enclosing_Instance then
6662 -- The child unit is found in some enclosing scope
6669 -- If this is the renaming of the implicit child in a parent
6670 -- instance, recover the parent name and install it.
6672 if Is_Entity_Name (Gen_Id) then
6673 E := Entity (Gen_Id);
6675 if Is_Generic_Unit (E)
6676 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6677 and then Is_Child_Unit (Renamed_Object (E))
6678 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6679 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6681 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6682 Inst_Par := Entity (Prefix (Gen_Id));
6684 if not In_Open_Scopes (Inst_Par) then
6685 Install_Parent (Inst_Par);
6686 Parent_Installed := True;
6689 -- If it is a child unit of a non-generic parent, it may be
6690 -- use-visible and given by a direct name. Install parent as
6693 elsif Is_Generic_Unit (E)
6694 and then Is_Child_Unit (E)
6696 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6697 and then not Is_Generic_Unit (Scope (E))
6699 if not In_Open_Scopes (Scope (E)) then
6700 Install_Parent (Scope (E));
6701 Parent_Installed := True;
6706 end Check_Generic_Child_Unit;
6708 -----------------------------
6709 -- Check_Hidden_Child_Unit --
6710 -----------------------------
6712 procedure Check_Hidden_Child_Unit
6714 Gen_Unit : Entity_Id;
6715 Act_Decl_Id : Entity_Id)
6717 Gen_Id : constant Node_Id := Name (N);
6720 if Is_Child_Unit (Gen_Unit)
6721 and then Is_Child_Unit (Act_Decl_Id)
6722 and then Nkind (Gen_Id) = N_Expanded_Name
6723 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6724 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6726 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6728 ("generic unit & is implicitly declared in &",
6729 Defining_Unit_Name (N), Gen_Unit);
6730 Error_Msg_N ("\instance must have different name",
6731 Defining_Unit_Name (N));
6733 end Check_Hidden_Child_Unit;
6735 ------------------------
6736 -- Check_Private_View --
6737 ------------------------
6739 procedure Check_Private_View (N : Node_Id) is
6740 T : constant Entity_Id := Etype (N);
6744 -- Exchange views if the type was not private in the generic but is
6745 -- private at the point of instantiation. Do not exchange views if
6746 -- the scope of the type is in scope. This can happen if both generic
6747 -- and instance are sibling units, or if type is defined in a parent.
6748 -- In this case the visibility of the type will be correct for all
6752 BT := Base_Type (T);
6754 if Is_Private_Type (T)
6755 and then not Has_Private_View (N)
6756 and then Present (Full_View (T))
6757 and then not In_Open_Scopes (Scope (T))
6759 -- In the generic, the full type was visible. Save the private
6760 -- entity, for subsequent exchange.
6764 elsif Has_Private_View (N)
6765 and then not Is_Private_Type (T)
6766 and then not Has_Been_Exchanged (T)
6767 and then Etype (Get_Associated_Node (N)) /= T
6769 -- Only the private declaration was visible in the generic. If
6770 -- the type appears in a subtype declaration, the subtype in the
6771 -- instance must have a view compatible with that of its parent,
6772 -- which must be exchanged (see corresponding code in Restore_
6773 -- Private_Views). Otherwise, if the type is defined in a parent
6774 -- unit, leave full visibility within instance, which is safe.
6776 if In_Open_Scopes (Scope (Base_Type (T)))
6777 and then not Is_Private_Type (Base_Type (T))
6778 and then Comes_From_Source (Base_Type (T))
6782 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6783 or else not In_Private_Part (Scope (Base_Type (T)))
6785 Prepend_Elmt (T, Exchanged_Views);
6786 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6789 -- For composite types with inconsistent representation exchange
6790 -- component types accordingly.
6792 elsif Is_Access_Type (T)
6793 and then Is_Private_Type (Designated_Type (T))
6794 and then not Has_Private_View (N)
6795 and then Present (Full_View (Designated_Type (T)))
6797 Switch_View (Designated_Type (T));
6799 elsif Is_Array_Type (T) then
6800 if Is_Private_Type (Component_Type (T))
6801 and then not Has_Private_View (N)
6802 and then Present (Full_View (Component_Type (T)))
6804 Switch_View (Component_Type (T));
6807 -- The normal exchange mechanism relies on the setting of a
6808 -- flag on the reference in the generic. However, an additional
6809 -- mechanism is needed for types that are not explicitly
6810 -- mentioned in the generic, but may be needed in expanded code
6811 -- in the instance. This includes component types of arrays and
6812 -- designated types of access types. This processing must also
6813 -- include the index types of arrays which we take care of here.
6820 Indx := First_Index (T);
6821 while Present (Indx) loop
6822 Typ := Base_Type (Etype (Indx));
6824 if Is_Private_Type (Typ)
6825 and then Present (Full_View (Typ))
6834 elsif Is_Private_Type (T)
6835 and then Present (Full_View (T))
6836 and then Is_Array_Type (Full_View (T))
6837 and then Is_Private_Type (Component_Type (Full_View (T)))
6841 -- Finally, a non-private subtype may have a private base type, which
6842 -- must be exchanged for consistency. This can happen when a package
6843 -- body is instantiated, when the scope stack is empty but in fact
6844 -- the subtype and the base type are declared in an enclosing scope.
6846 -- Note that in this case we introduce an inconsistency in the view
6847 -- set, because we switch the base type BT, but there could be some
6848 -- private dependent subtypes of BT which remain unswitched. Such
6849 -- subtypes might need to be switched at a later point (see specific
6850 -- provision for that case in Switch_View).
6852 elsif not Is_Private_Type (T)
6853 and then not Has_Private_View (N)
6854 and then Is_Private_Type (BT)
6855 and then Present (Full_View (BT))
6856 and then not Is_Generic_Type (BT)
6857 and then not In_Open_Scopes (BT)
6859 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6860 Exchange_Declarations (BT);
6863 end Check_Private_View;
6865 -----------------------------
6866 -- Check_Hidden_Primitives --
6867 -----------------------------
6869 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6872 Result : Elist_Id := No_Elist;
6875 if No (Assoc_List) then
6879 -- Traverse the list of associations between formals and actuals
6880 -- searching for renamings of tagged types
6882 Actual := First (Assoc_List);
6883 while Present (Actual) loop
6884 if Nkind (Actual) = N_Subtype_Declaration then
6885 Gen_T := Generic_Parent_Type (Actual);
6887 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6889 -- Traverse the list of primitives of the actual types
6890 -- searching for hidden primitives that are visible in the
6891 -- corresponding generic formal; leave them visible and
6892 -- append them to Result to restore their decoration later.
6894 Install_Hidden_Primitives
6895 (Prims_List => Result,
6897 Act_T => Entity (Subtype_Indication (Actual)));
6905 end Check_Hidden_Primitives;
6907 --------------------------
6908 -- Contains_Instance_Of --
6909 --------------------------
6911 function Contains_Instance_Of
6914 N : Node_Id) return Boolean
6922 -- Verify that there are no circular instantiations. We check whether
6923 -- the unit contains an instance of the current scope or some enclosing
6924 -- scope (in case one of the instances appears in a subunit). Longer
6925 -- circularities involving subunits might seem too pathological to
6926 -- consider, but they were not too pathological for the authors of
6927 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6928 -- enclosing generic scopes as containing an instance.
6931 -- Within a generic subprogram body, the scope is not generic, to
6932 -- allow for recursive subprograms. Use the declaration to determine
6933 -- whether this is a generic unit.
6935 if Ekind (Scop) = E_Generic_Package
6936 or else (Is_Subprogram (Scop)
6937 and then Nkind (Unit_Declaration_Node (Scop)) =
6938 N_Generic_Subprogram_Declaration)
6940 Elmt := First_Elmt (Inner_Instances (Inner));
6942 while Present (Elmt) loop
6943 if Node (Elmt) = Scop then
6944 Error_Msg_Node_2 := Inner;
6946 ("circular Instantiation: & instantiated within &!",
6950 elsif Node (Elmt) = Inner then
6953 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6954 Error_Msg_Node_2 := Inner;
6956 ("circular Instantiation: & instantiated within &!",
6964 -- Indicate that Inner is being instantiated within Scop
6966 Append_Elmt (Inner, Inner_Instances (Scop));
6969 if Scop = Standard_Standard then
6972 Scop := Scope (Scop);
6977 end Contains_Instance_Of;
6979 -----------------------
6980 -- Copy_Generic_Node --
6981 -----------------------
6983 function Copy_Generic_Node
6985 Parent_Id : Node_Id;
6986 Instantiating : Boolean) return Node_Id
6991 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6992 -- Check the given value of one of the Fields referenced by the current
6993 -- node to determine whether to copy it recursively. The field may hold
6994 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6995 -- Char) in which case it need not be copied.
6997 procedure Copy_Descendants;
6998 -- Common utility for various nodes
7000 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7001 -- Make copy of element list
7003 function Copy_Generic_List
7005 Parent_Id : Node_Id) return List_Id;
7006 -- Apply Copy_Node recursively to the members of a node list
7008 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7009 -- True if an identifier is part of the defining program unit name of
7010 -- a child unit. The entity of such an identifier must be kept (for
7011 -- ASIS use) even though as the name of an enclosing generic it would
7012 -- otherwise not be preserved in the generic tree.
7014 ----------------------
7015 -- Copy_Descendants --
7016 ----------------------
7018 procedure Copy_Descendants is
7019 use Atree.Unchecked_Access;
7020 -- This code section is part of the implementation of an untyped
7021 -- tree traversal, so it needs direct access to node fields.
7024 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7025 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7026 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7027 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7028 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7029 end Copy_Descendants;
7031 -----------------------------
7032 -- Copy_Generic_Descendant --
7033 -----------------------------
7035 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7037 if D = Union_Id (Empty) then
7040 elsif D in Node_Range then
7042 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7044 elsif D in List_Range then
7045 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7047 elsif D in Elist_Range then
7048 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7050 -- Nothing else is copyable (e.g. Uint values), return as is
7055 end Copy_Generic_Descendant;
7057 ------------------------
7058 -- Copy_Generic_Elist --
7059 ------------------------
7061 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7068 M := First_Elmt (E);
7069 while Present (M) loop
7071 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7080 end Copy_Generic_Elist;
7082 -----------------------
7083 -- Copy_Generic_List --
7084 -----------------------
7086 function Copy_Generic_List
7088 Parent_Id : Node_Id) return List_Id
7096 Set_Parent (New_L, Parent_Id);
7099 while Present (N) loop
7100 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7109 end Copy_Generic_List;
7111 ---------------------------
7112 -- In_Defining_Unit_Name --
7113 ---------------------------
7115 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7118 Present (Parent (Nam))
7119 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7121 (Nkind (Parent (Nam)) = N_Expanded_Name
7122 and then In_Defining_Unit_Name (Parent (Nam))));
7123 end In_Defining_Unit_Name;
7125 -- Start of processing for Copy_Generic_Node
7132 New_N := New_Copy (N);
7134 -- Copy aspects if present
7136 if Has_Aspects (N) then
7137 Set_Has_Aspects (New_N, False);
7138 Set_Aspect_Specifications
7139 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7142 if Instantiating then
7143 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7146 if not Is_List_Member (N) then
7147 Set_Parent (New_N, Parent_Id);
7150 -- Special casing for identifiers and other entity names and operators
7152 if Nkind_In (New_N, N_Character_Literal,
7156 or else Nkind (New_N) in N_Op
7158 if not Instantiating then
7160 -- Link both nodes in order to assign subsequently the entity of
7161 -- the copy to the original node, in case this is a global
7164 Set_Associated_Node (N, New_N);
7166 -- If we are within an instantiation, this is a nested generic
7167 -- that has already been analyzed at the point of definition.
7168 -- We must preserve references that were global to the enclosing
7169 -- parent at that point. Other occurrences, whether global or
7170 -- local to the current generic, must be resolved anew, so we
7171 -- reset the entity in the generic copy. A global reference has a
7172 -- smaller depth than the parent, or else the same depth in case
7173 -- both are distinct compilation units.
7175 -- A child unit is implicitly declared within the enclosing parent
7176 -- but is in fact global to it, and must be preserved.
7178 -- It is also possible for Current_Instantiated_Parent to be
7179 -- defined, and for this not to be a nested generic, namely if
7180 -- the unit is loaded through Rtsfind. In that case, the entity of
7181 -- New_N is only a link to the associated node, and not a defining
7184 -- The entities for parent units in the defining_program_unit of a
7185 -- generic child unit are established when the context of the unit
7186 -- is first analyzed, before the generic copy is made. They are
7187 -- preserved in the copy for use in ASIS queries.
7189 Ent := Entity (New_N);
7191 if No (Current_Instantiated_Parent.Gen_Id) then
7193 or else Nkind (Ent) /= N_Defining_Identifier
7194 or else not In_Defining_Unit_Name (N)
7196 Set_Associated_Node (New_N, Empty);
7201 not Nkind_In (Ent, N_Defining_Identifier,
7202 N_Defining_Character_Literal,
7203 N_Defining_Operator_Symbol)
7204 or else No (Scope (Ent))
7206 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7207 and then not Is_Child_Unit (Ent))
7209 (Scope_Depth (Scope (Ent)) >
7210 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7212 Get_Source_Unit (Ent) =
7213 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7215 Set_Associated_Node (New_N, Empty);
7218 -- Case of instantiating identifier or some other name or operator
7221 -- If the associated node is still defined, the entity in it
7222 -- is global, and must be copied to the instance. If this copy
7223 -- is being made for a body to inline, it is applied to an
7224 -- instantiated tree, and the entity is already present and
7225 -- must be also preserved.
7228 Assoc : constant Node_Id := Get_Associated_Node (N);
7231 if Present (Assoc) then
7232 if Nkind (Assoc) = Nkind (N) then
7233 Set_Entity (New_N, Entity (Assoc));
7234 Check_Private_View (N);
7236 -- The name in the call may be a selected component if the
7237 -- call has not been analyzed yet, as may be the case for
7238 -- pre/post conditions in a generic unit.
7240 elsif Nkind (Assoc) = N_Function_Call
7241 and then Is_Entity_Name (Name (Assoc))
7243 Set_Entity (New_N, Entity (Name (Assoc)));
7245 elsif Nkind_In (Assoc, N_Defining_Identifier,
7246 N_Defining_Character_Literal,
7247 N_Defining_Operator_Symbol)
7248 and then Expander_Active
7250 -- Inlining case: we are copying a tree that contains
7251 -- global entities, which are preserved in the copy to be
7252 -- used for subsequent inlining.
7257 Set_Entity (New_N, Empty);
7263 -- For expanded name, we must copy the Prefix and Selector_Name
7265 if Nkind (N) = N_Expanded_Name then
7267 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7269 Set_Selector_Name (New_N,
7270 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7272 -- For operators, we must copy the right operand
7274 elsif Nkind (N) in N_Op then
7275 Set_Right_Opnd (New_N,
7276 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7278 -- And for binary operators, the left operand as well
7280 if Nkind (N) in N_Binary_Op then
7281 Set_Left_Opnd (New_N,
7282 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7286 -- Establish a link between an entity from the generic template and the
7287 -- corresponding entity in the generic copy to be analyzed.
7289 elsif Nkind (N) in N_Entity then
7290 if not Instantiating then
7291 Set_Associated_Entity (N, New_N);
7294 -- Clear any existing link the copy may inherit from the replicated
7295 -- generic template entity.
7297 Set_Associated_Entity (New_N, Empty);
7299 -- Special casing for stubs
7301 elsif Nkind (N) in N_Body_Stub then
7303 -- In any case, we must copy the specification or defining
7304 -- identifier as appropriate.
7306 if Nkind (N) = N_Subprogram_Body_Stub then
7307 Set_Specification (New_N,
7308 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7311 Set_Defining_Identifier (New_N,
7313 (Defining_Identifier (N), New_N, Instantiating));
7316 -- If we are not instantiating, then this is where we load and
7317 -- analyze subunits, i.e. at the point where the stub occurs. A
7318 -- more permissive system might defer this analysis to the point
7319 -- of instantiation, but this seems too complicated for now.
7321 if not Instantiating then
7323 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7325 Unum : Unit_Number_Type;
7329 -- Make sure that, if it is a subunit of the main unit that is
7330 -- preprocessed and if -gnateG is specified, the preprocessed
7331 -- file will be written.
7333 Lib.Analysing_Subunit_Of_Main :=
7334 Lib.In_Extended_Main_Source_Unit (N);
7337 (Load_Name => Subunit_Name,
7341 Lib.Analysing_Subunit_Of_Main := False;
7343 -- If the proper body is not found, a warning message will be
7344 -- emitted when analyzing the stub, or later at the point of
7345 -- instantiation. Here we just leave the stub as is.
7347 if Unum = No_Unit then
7348 Subunits_Missing := True;
7349 goto Subunit_Not_Found;
7352 Subunit := Cunit (Unum);
7354 if Nkind (Unit (Subunit)) /= N_Subunit then
7356 ("found child unit instead of expected SEPARATE subunit",
7358 Error_Msg_Sloc := Sloc (N);
7359 Error_Msg_N ("\to complete stub #", Subunit);
7360 goto Subunit_Not_Found;
7363 -- We must create a generic copy of the subunit, in order to
7364 -- perform semantic analysis on it, and we must replace the
7365 -- stub in the original generic unit with the subunit, in order
7366 -- to preserve non-local references within.
7368 -- Only the proper body needs to be copied. Library_Unit and
7369 -- context clause are simply inherited by the generic copy.
7370 -- Note that the copy (which may be recursive if there are
7371 -- nested subunits) must be done first, before attaching it to
7372 -- the enclosing generic.
7376 (Proper_Body (Unit (Subunit)),
7377 Empty, Instantiating => False);
7379 -- Now place the original proper body in the original generic
7380 -- unit. This is a body, not a compilation unit.
7382 Rewrite (N, Proper_Body (Unit (Subunit)));
7383 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7384 Set_Was_Originally_Stub (N);
7386 -- Finally replace the body of the subunit with its copy, and
7387 -- make this new subunit into the library unit of the generic
7388 -- copy, which does not have stubs any longer.
7390 Set_Proper_Body (Unit (Subunit), New_Body);
7391 Set_Library_Unit (New_N, Subunit);
7392 Inherit_Context (Unit (Subunit), N);
7395 -- If we are instantiating, this must be an error case, since
7396 -- otherwise we would have replaced the stub node by the proper body
7397 -- that corresponds. So just ignore it in the copy (i.e. we have
7398 -- copied it, and that is good enough).
7404 <<Subunit_Not_Found>> null;
7406 -- If the node is a compilation unit, it is the subunit of a stub, which
7407 -- has been loaded already (see code below). In this case, the library
7408 -- unit field of N points to the parent unit (which is a compilation
7409 -- unit) and need not (and cannot) be copied.
7411 -- When the proper body of the stub is analyzed, the library_unit link
7412 -- is used to establish the proper context (see sem_ch10).
7414 -- The other fields of a compilation unit are copied as usual
7416 elsif Nkind (N) = N_Compilation_Unit then
7418 -- This code can only be executed when not instantiating, because in
7419 -- the copy made for an instantiation, the compilation unit node has
7420 -- disappeared at the point that a stub is replaced by its proper
7423 pragma Assert (not Instantiating);
7425 Set_Context_Items (New_N,
7426 Copy_Generic_List (Context_Items (N), New_N));
7429 Copy_Generic_Node (Unit (N), New_N, False));
7431 Set_First_Inlined_Subprogram (New_N,
7433 (First_Inlined_Subprogram (N), New_N, False));
7435 Set_Aux_Decls_Node (New_N,
7436 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7438 -- For an assignment node, the assignment is known to be semantically
7439 -- legal if we are instantiating the template. This avoids incorrect
7440 -- diagnostics in generated code.
7442 elsif Nkind (N) = N_Assignment_Statement then
7444 -- Copy name and expression fields in usual manner
7447 Copy_Generic_Node (Name (N), New_N, Instantiating));
7449 Set_Expression (New_N,
7450 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7452 if Instantiating then
7453 Set_Assignment_OK (Name (New_N), True);
7456 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7457 if not Instantiating then
7458 Set_Associated_Node (N, New_N);
7461 if Present (Get_Associated_Node (N))
7462 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7464 -- In the generic the aggregate has some composite type. If at
7465 -- the point of instantiation the type has a private view,
7466 -- install the full view (and that of its ancestors, if any).
7469 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7473 if Present (T) and then Is_Private_Type (T) then
7478 and then Is_Tagged_Type (T)
7479 and then Is_Derived_Type (T)
7481 Rt := Root_Type (T);
7486 if Is_Private_Type (T) then
7497 -- Do not copy the associated node, which points to the generic copy
7498 -- of the aggregate.
7501 use Atree.Unchecked_Access;
7502 -- This code section is part of the implementation of an untyped
7503 -- tree traversal, so it needs direct access to node fields.
7506 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7507 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7508 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7509 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7512 -- Allocators do not have an identifier denoting the access type, so we
7513 -- must locate it through the expression to check whether the views are
7516 elsif Nkind (N) = N_Allocator
7517 and then Nkind (Expression (N)) = N_Qualified_Expression
7518 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7519 and then Instantiating
7522 T : constant Node_Id :=
7523 Get_Associated_Node (Subtype_Mark (Expression (N)));
7529 -- Retrieve the allocator node in the generic copy
7531 Acc_T := Etype (Parent (Parent (T)));
7533 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7534 Switch_View (Acc_T);
7541 -- For a proper body, we must catch the case of a proper body that
7542 -- replaces a stub. This represents the point at which a separate
7543 -- compilation unit, and hence template file, may be referenced, so we
7544 -- must make a new source instantiation entry for the template of the
7545 -- subunit, and ensure that all nodes in the subunit are adjusted using
7546 -- this new source instantiation entry.
7548 elsif Nkind (N) in N_Proper_Body then
7550 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7553 if Instantiating and then Was_Originally_Stub (N) then
7554 Create_Instantiation_Source
7555 (Instantiation_Node,
7556 Defining_Entity (N),
7561 -- Now copy the fields of the proper body, using the new
7562 -- adjustment factor if one was needed as per test above.
7566 -- Restore the original adjustment factor in case changed
7568 S_Adjustment := Save_Adjustment;
7571 elsif Nkind (N) = N_Pragma and then Instantiating then
7573 -- Do not copy Comment or Ident pragmas their content is relevant to
7574 -- the generic unit, not to the instantiating unit.
7576 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7577 New_N := Make_Null_Statement (Sloc (N));
7579 -- Do not copy pragmas generated from aspects because the pragmas do
7580 -- not carry any semantic information, plus they will be regenerated
7583 elsif From_Aspect_Specification (N) then
7584 New_N := Make_Null_Statement (Sloc (N));
7590 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7592 -- No descendant fields need traversing
7596 elsif Nkind (N) = N_String_Literal
7597 and then Present (Etype (N))
7598 and then Instantiating
7600 -- If the string is declared in an outer scope, the string_literal
7601 -- subtype created for it may have the wrong scope. Force reanalysis
7602 -- of the constant to generate a new itype in the proper context.
7604 Set_Etype (New_N, Empty);
7605 Set_Analyzed (New_N, False);
7607 -- For the remaining nodes, copy their descendants recursively
7612 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7613 Set_Generic_Parent (Specification (New_N), N);
7615 -- Should preserve Corresponding_Spec??? (12.3(14))
7620 end Copy_Generic_Node;
7622 ----------------------------
7623 -- Denotes_Formal_Package --
7624 ----------------------------
7626 function Denotes_Formal_Package
7628 On_Exit : Boolean := False;
7629 Instance : Entity_Id := Empty) return Boolean
7632 Scop : constant Entity_Id := Scope (Pack);
7635 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7636 -- The package in question may be an actual for a previous formal
7637 -- package P of the current instance, so examine its actuals as well.
7638 -- This must be recursive over other formal packages.
7640 ----------------------------------
7641 -- Is_Actual_Of_Previous_Formal --
7642 ----------------------------------
7644 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7648 E1 := First_Entity (P);
7649 while Present (E1) and then E1 /= Instance loop
7650 if Ekind (E1) = E_Package
7651 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7653 if Renamed_Object (E1) = Pack then
7656 elsif E1 = P or else Renamed_Object (E1) = P then
7659 elsif Is_Actual_Of_Previous_Formal (E1) then
7668 end Is_Actual_Of_Previous_Formal;
7670 -- Start of processing for Denotes_Formal_Package
7676 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7678 Par := Current_Instantiated_Parent.Act_Id;
7681 if Ekind (Scop) = E_Generic_Package
7682 or else Nkind (Unit_Declaration_Node (Scop)) =
7683 N_Generic_Subprogram_Declaration
7687 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7688 N_Formal_Package_Declaration
7696 -- Check whether this package is associated with a formal package of
7697 -- the enclosing instantiation. Iterate over the list of renamings.
7699 E := First_Entity (Par);
7700 while Present (E) loop
7701 if Ekind (E) /= E_Package
7702 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7706 elsif Renamed_Object (E) = Par then
7709 elsif Renamed_Object (E) = Pack then
7712 elsif Is_Actual_Of_Previous_Formal (E) then
7722 end Denotes_Formal_Package;
7728 procedure End_Generic is
7730 -- ??? More things could be factored out in this routine. Should
7731 -- probably be done at a later stage.
7733 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7734 Generic_Flags.Decrement_Last;
7736 Expander_Mode_Restore;
7743 function Earlier (N1, N2 : Node_Id) return Boolean is
7744 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7745 -- Find distance from given node to enclosing compilation unit
7751 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7754 and then Nkind (P) /= N_Compilation_Unit
7756 P := True_Parent (P);
7761 -- Local declarations
7770 -- Start of processing for Earlier
7773 Find_Depth (P1, D1);
7774 Find_Depth (P2, D2);
7784 P1 := True_Parent (P1);
7789 P2 := True_Parent (P2);
7793 -- At this point P1 and P2 are at the same distance from the root.
7794 -- We examine their parents until we find a common declarative list.
7795 -- If we reach the root, N1 and N2 do not descend from the same
7796 -- declarative list (e.g. one is nested in the declarative part and
7797 -- the other is in a block in the statement part) and the earlier
7798 -- one is already frozen.
7800 while not Is_List_Member (P1)
7801 or else not Is_List_Member (P2)
7802 or else List_Containing (P1) /= List_Containing (P2)
7804 P1 := True_Parent (P1);
7805 P2 := True_Parent (P2);
7807 if Nkind (Parent (P1)) = N_Subunit then
7808 P1 := Corresponding_Stub (Parent (P1));
7811 if Nkind (Parent (P2)) = N_Subunit then
7812 P2 := Corresponding_Stub (Parent (P2));
7820 -- Expanded code usually shares the source location of the original
7821 -- construct it was generated for. This however may not necessarely
7822 -- reflect the true location of the code within the tree.
7824 -- Before comparing the slocs of the two nodes, make sure that we are
7825 -- working with correct source locations. Assume that P1 is to the left
7826 -- of P2. If either one does not come from source, traverse the common
7827 -- list heading towards the other node and locate the first source
7831 -- ----+===+===+--------------+===+===+----
7832 -- expanded code expanded code
7834 if not Comes_From_Source (P1) then
7835 while Present (P1) loop
7837 -- Neither P2 nor a source statement were located during the
7838 -- search. If we reach the end of the list, then P1 does not
7839 -- occur earlier than P2.
7842 -- start --- P2 ----- P1 --- end
7844 if No (Next (P1)) then
7847 -- We encounter P2 while going to the right of the list. This
7848 -- means that P1 does indeed appear earlier.
7851 -- start --- P1 ===== P2 --- end
7852 -- expanded code in between
7857 -- No need to look any further since we have located a source
7860 elsif Comes_From_Source (P1) then
7870 if not Comes_From_Source (P2) then
7871 while Present (P2) loop
7873 -- Neither P1 nor a source statement were located during the
7874 -- search. If we reach the start of the list, then P1 does not
7875 -- occur earlier than P2.
7878 -- start --- P2 --- P1 --- end
7880 if No (Prev (P2)) then
7883 -- We encounter P1 while going to the left of the list. This
7884 -- means that P1 does indeed appear earlier.
7887 -- start --- P1 ===== P2 --- end
7888 -- expanded code in between
7893 -- No need to look any further since we have located a source
7896 elsif Comes_From_Source (P2) then
7906 -- At this point either both nodes came from source or we approximated
7907 -- their source locations through neighbouring source statements.
7909 T1 := Top_Level_Location (Sloc (P1));
7910 T2 := Top_Level_Location (Sloc (P2));
7912 -- When two nodes come from the same instance, they have identical top
7913 -- level locations. To determine proper relation within the tree, check
7914 -- their locations within the template.
7917 return Sloc (P1) < Sloc (P2);
7919 -- The two nodes either come from unrelated instances or do not come
7920 -- from instantiated code at all.
7927 ----------------------
7928 -- Find_Actual_Type --
7929 ----------------------
7931 function Find_Actual_Type
7933 Gen_Type : Entity_Id) return Entity_Id
7935 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7939 -- Special processing only applies to child units
7941 if not Is_Child_Unit (Gen_Scope) then
7942 return Get_Instance_Of (Typ);
7944 -- If designated or component type is itself a formal of the child unit,
7945 -- its instance is available.
7947 elsif Scope (Typ) = Gen_Scope then
7948 return Get_Instance_Of (Typ);
7950 -- If the array or access type is not declared in the parent unit,
7951 -- no special processing needed.
7953 elsif not Is_Generic_Type (Typ)
7954 and then Scope (Gen_Scope) /= Scope (Typ)
7956 return Get_Instance_Of (Typ);
7958 -- Otherwise, retrieve designated or component type by visibility
7961 T := Current_Entity (Typ);
7962 while Present (T) loop
7963 if In_Open_Scopes (Scope (T)) then
7965 elsif Is_Generic_Actual_Type (T) then
7974 end Find_Actual_Type;
7976 ----------------------------
7977 -- Freeze_Subprogram_Body --
7978 ----------------------------
7980 procedure Freeze_Subprogram_Body
7981 (Inst_Node : Node_Id;
7983 Pack_Id : Entity_Id)
7985 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7986 Par : constant Entity_Id := Scope (Gen_Unit);
7992 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7993 -- Find innermost package body that encloses the given node, and which
7994 -- is not a compilation unit. Freeze nodes for the instance, or for its
7995 -- enclosing body, may be inserted after the enclosing_body of the
7996 -- generic unit. Used to determine proper placement of freeze node for
7997 -- both package and subprogram instances.
7999 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8000 -- Find entity for given package body, and locate or create a freeze
8003 ----------------------------
8004 -- Enclosing_Package_Body --
8005 ----------------------------
8007 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8013 and then Nkind (Parent (P)) /= N_Compilation_Unit
8015 if Nkind (P) = N_Package_Body then
8016 if Nkind (Parent (P)) = N_Subunit then
8017 return Corresponding_Stub (Parent (P));
8023 P := True_Parent (P);
8027 end Enclosing_Package_Body;
8029 -------------------------
8030 -- Package_Freeze_Node --
8031 -------------------------
8033 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8037 if Nkind (B) = N_Package_Body then
8038 Id := Corresponding_Spec (B);
8039 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8040 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8043 Ensure_Freeze_Node (Id);
8044 return Freeze_Node (Id);
8045 end Package_Freeze_Node;
8047 -- Start of processing of Freeze_Subprogram_Body
8050 -- If the instance and the generic body appear within the same unit, and
8051 -- the instance precedes the generic, the freeze node for the instance
8052 -- must appear after that of the generic. If the generic is nested
8053 -- within another instance I2, then current instance must be frozen
8054 -- after I2. In both cases, the freeze nodes are those of enclosing
8055 -- packages. Otherwise, the freeze node is placed at the end of the
8056 -- current declarative part.
8058 Enc_G := Enclosing_Package_Body (Gen_Body);
8059 Enc_I := Enclosing_Package_Body (Inst_Node);
8060 Ensure_Freeze_Node (Pack_Id);
8061 F_Node := Freeze_Node (Pack_Id);
8063 if Is_Generic_Instance (Par)
8064 and then Present (Freeze_Node (Par))
8065 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8067 -- The parent was a premature instantiation. Insert freeze node at
8068 -- the end the current declarative part.
8070 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8071 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8073 -- Handle the following case:
8075 -- package Parent_Inst is new ...
8078 -- procedure P ... -- this body freezes Parent_Inst
8080 -- package Inst is new ...
8082 -- In this particular scenario, the freeze node for Inst must be
8083 -- inserted in the same manner as that of Parent_Inst - before the
8084 -- next source body or at the end of the declarative list (body not
8085 -- available). If body P did not exist and Parent_Inst was frozen
8086 -- after Inst, either by a body following Inst or at the end of the
8087 -- declarative region, the freeze node for Inst must be inserted
8088 -- after that of Parent_Inst. This relation is established by
8089 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8091 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8092 List_Containing (Inst_Node)
8093 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8095 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8098 Insert_After (Freeze_Node (Par), F_Node);
8101 -- The body enclosing the instance should be frozen after the body that
8102 -- includes the generic, because the body of the instance may make
8103 -- references to entities therein. If the two are not in the same
8104 -- declarative part, or if the one enclosing the instance is frozen
8105 -- already, freeze the instance at the end of the current declarative
8108 elsif Is_Generic_Instance (Par)
8109 and then Present (Freeze_Node (Par))
8110 and then Present (Enc_I)
8112 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8114 (Nkind (Enc_I) = N_Package_Body
8116 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8118 -- The enclosing package may contain several instances. Rather
8119 -- than computing the earliest point at which to insert its freeze
8120 -- node, we place it at the end of the declarative part of the
8121 -- parent of the generic.
8123 Insert_Freeze_Node_For_Instance
8124 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8127 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8129 elsif Present (Enc_G)
8130 and then Present (Enc_I)
8131 and then Enc_G /= Enc_I
8132 and then Earlier (Inst_Node, Gen_Body)
8134 if Nkind (Enc_G) = N_Package_Body then
8136 Corresponding_Spec (Enc_G);
8137 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8139 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8142 -- Freeze package that encloses instance, and place node after the
8143 -- package that encloses generic. If enclosing package is already
8144 -- frozen we have to assume it is at the proper place. This may be a
8145 -- potential ABE that requires dynamic checking. Do not add a freeze
8146 -- node if the package that encloses the generic is inside the body
8147 -- that encloses the instance, because the freeze node would be in
8148 -- the wrong scope. Additional contortions needed if the bodies are
8149 -- within a subunit.
8152 Enclosing_Body : Node_Id;
8155 if Nkind (Enc_I) = N_Package_Body_Stub then
8156 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8158 Enclosing_Body := Enc_I;
8161 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8162 Insert_Freeze_Node_For_Instance
8163 (Enc_G, Package_Freeze_Node (Enc_I));
8167 -- Freeze enclosing subunit before instance
8169 Ensure_Freeze_Node (E_G_Id);
8171 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8172 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8175 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8178 -- If none of the above, insert freeze node at the end of the current
8179 -- declarative part.
8181 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8183 end Freeze_Subprogram_Body;
8189 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8191 return Generic_Renamings.Table (E).Gen_Id;
8194 ---------------------
8195 -- Get_Instance_Of --
8196 ---------------------
8198 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8199 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8202 if Res /= Assoc_Null then
8203 return Generic_Renamings.Table (Res).Act_Id;
8206 -- On exit, entity is not instantiated: not a generic parameter, or
8207 -- else parameter of an inner generic unit.
8211 end Get_Instance_Of;
8213 ------------------------------------
8214 -- Get_Package_Instantiation_Node --
8215 ------------------------------------
8217 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8218 Decl : Node_Id := Unit_Declaration_Node (A);
8222 -- If the Package_Instantiation attribute has been set on the package
8223 -- entity, then use it directly when it (or its Original_Node) refers
8224 -- to an N_Package_Instantiation node. In principle it should be
8225 -- possible to have this field set in all cases, which should be
8226 -- investigated, and would allow this function to be significantly
8229 Inst := Package_Instantiation (A);
8231 if Present (Inst) then
8232 if Nkind (Inst) = N_Package_Instantiation then
8235 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8236 return Original_Node (Inst);
8240 -- If the instantiation is a compilation unit that does not need body
8241 -- then the instantiation node has been rewritten as a package
8242 -- declaration for the instance, and we return the original node.
8244 -- If it is a compilation unit and the instance node has not been
8245 -- rewritten, then it is still the unit of the compilation. Finally, if
8246 -- a body is present, this is a parent of the main unit whose body has
8247 -- been compiled for inlining purposes, and the instantiation node has
8248 -- been rewritten with the instance body.
8250 -- Otherwise the instantiation node appears after the declaration. If
8251 -- the entity is a formal package, the declaration may have been
8252 -- rewritten as a generic declaration (in the case of a formal with box)
8253 -- or left as a formal package declaration if it has actuals, and is
8254 -- found with a forward search.
8256 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8257 if Nkind (Decl) = N_Package_Declaration
8258 and then Present (Corresponding_Body (Decl))
8260 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8263 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8264 return Original_Node (Decl);
8266 return Unit (Parent (Decl));
8269 elsif Nkind (Decl) = N_Package_Declaration
8270 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8272 return Original_Node (Decl);
8275 Inst := Next (Decl);
8276 while not Nkind_In (Inst, N_Package_Instantiation,
8277 N_Formal_Package_Declaration)
8284 end Get_Package_Instantiation_Node;
8286 ------------------------
8287 -- Has_Been_Exchanged --
8288 ------------------------
8290 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8294 Next := First_Elmt (Exchanged_Views);
8295 while Present (Next) loop
8296 if Full_View (Node (Next)) = E then
8304 end Has_Been_Exchanged;
8310 function Hash (F : Entity_Id) return HTable_Range is
8312 return HTable_Range (F mod HTable_Size);
8315 ------------------------
8316 -- Hide_Current_Scope --
8317 ------------------------
8319 procedure Hide_Current_Scope is
8320 C : constant Entity_Id := Current_Scope;
8324 Set_Is_Hidden_Open_Scope (C);
8326 E := First_Entity (C);
8327 while Present (E) loop
8328 if Is_Immediately_Visible (E) then
8329 Set_Is_Immediately_Visible (E, False);
8330 Append_Elmt (E, Hidden_Entities);
8336 -- Make the scope name invisible as well. This is necessary, but might
8337 -- conflict with calls to Rtsfind later on, in case the scope is a
8338 -- predefined one. There is no clean solution to this problem, so for
8339 -- now we depend on the user not redefining Standard itself in one of
8340 -- the parent units.
8342 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8343 Set_Is_Immediately_Visible (C, False);
8344 Append_Elmt (C, Hidden_Entities);
8347 end Hide_Current_Scope;
8353 procedure Init_Env is
8354 Saved : Instance_Env;
8357 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8358 Saved.Exchanged_Views := Exchanged_Views;
8359 Saved.Hidden_Entities := Hidden_Entities;
8360 Saved.Current_Sem_Unit := Current_Sem_Unit;
8361 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8362 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8364 -- Save configuration switches. These may be reset if the unit is a
8365 -- predefined unit, and the current mode is not Ada 2005.
8367 Save_Opt_Config_Switches (Saved.Switches);
8369 Instance_Envs.Append (Saved);
8371 Exchanged_Views := New_Elmt_List;
8372 Hidden_Entities := New_Elmt_List;
8374 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8375 -- this is set properly in Set_Instance_Env.
8377 Current_Instantiated_Parent :=
8378 (Current_Scope, Current_Scope, Assoc_Null);
8381 ------------------------------
8382 -- In_Same_Declarative_Part --
8383 ------------------------------
8385 function In_Same_Declarative_Part
8387 Inst : Node_Id) return Boolean
8389 Decls : constant Node_Id := Parent (F_Node);
8393 Nod := Parent (Inst);
8394 while Present (Nod) loop
8398 elsif Nkind_In (Nod, N_Subprogram_Body,
8400 N_Package_Declaration,
8407 elsif Nkind (Nod) = N_Subunit then
8408 Nod := Corresponding_Stub (Nod);
8410 elsif Nkind (Nod) = N_Compilation_Unit then
8414 Nod := Parent (Nod);
8419 end In_Same_Declarative_Part;
8421 ---------------------
8422 -- In_Main_Context --
8423 ---------------------
8425 function In_Main_Context (E : Entity_Id) return Boolean is
8431 if not Is_Compilation_Unit (E)
8432 or else Ekind (E) /= E_Package
8433 or else In_Private_Part (E)
8438 Context := Context_Items (Cunit (Main_Unit));
8440 Clause := First (Context);
8441 while Present (Clause) loop
8442 if Nkind (Clause) = N_With_Clause then
8443 Nam := Name (Clause);
8445 -- If the current scope is part of the context of the main unit,
8446 -- analysis of the corresponding with_clause is not complete, and
8447 -- the entity is not set. We use the Chars field directly, which
8448 -- might produce false positives in rare cases, but guarantees
8449 -- that we produce all the instance bodies we will need.
8451 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8452 or else (Nkind (Nam) = N_Selected_Component
8453 and then Chars (Selector_Name (Nam)) = Chars (E))
8463 end In_Main_Context;
8465 ---------------------
8466 -- Inherit_Context --
8467 ---------------------
8469 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8470 Current_Context : List_Id;
8471 Current_Unit : Node_Id;
8480 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8482 -- The inherited context is attached to the enclosing compilation
8483 -- unit. This is either the main unit, or the declaration for the
8484 -- main unit (in case the instantiation appears within the package
8485 -- declaration and the main unit is its body).
8487 Current_Unit := Parent (Inst);
8488 while Present (Current_Unit)
8489 and then Nkind (Current_Unit) /= N_Compilation_Unit
8491 Current_Unit := Parent (Current_Unit);
8494 Current_Context := Context_Items (Current_Unit);
8496 Item := First (Context_Items (Parent (Gen_Decl)));
8497 while Present (Item) loop
8498 if Nkind (Item) = N_With_Clause then
8499 Lib_Unit := Library_Unit (Item);
8501 -- Take care to prevent direct cyclic with's
8503 if Lib_Unit /= Current_Unit then
8505 -- Do not add a unit if it is already in the context
8507 Clause := First (Current_Context);
8509 while Present (Clause) loop
8510 if Nkind (Clause) = N_With_Clause and then
8511 Library_Unit (Clause) = Lib_Unit
8521 New_I := New_Copy (Item);
8522 Set_Implicit_With (New_I, True);
8523 Set_Implicit_With_From_Instantiation (New_I, True);
8524 Append (New_I, Current_Context);
8532 end Inherit_Context;
8538 procedure Initialize is
8540 Generic_Renamings.Init;
8543 Generic_Renamings_HTable.Reset;
8544 Circularity_Detected := False;
8545 Exchanged_Views := No_Elist;
8546 Hidden_Entities := No_Elist;
8549 -------------------------------------
8550 -- Insert_Freeze_Node_For_Instance --
8551 -------------------------------------
8553 procedure Insert_Freeze_Node_For_Instance
8562 function Enclosing_Body (N : Node_Id) return Node_Id;
8563 -- Find enclosing package or subprogram body, if any. Freeze node may
8564 -- be placed at end of current declarative list if previous instance
8565 -- and current one have different enclosing bodies.
8567 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8568 -- Find the local instance, if any, that declares the generic that is
8569 -- being instantiated. If present, the freeze node for this instance
8570 -- must follow the freeze node for the previous instance.
8572 --------------------
8573 -- Enclosing_Body --
8574 --------------------
8576 function Enclosing_Body (N : Node_Id) return Node_Id is
8582 and then Nkind (Parent (P)) /= N_Compilation_Unit
8584 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8585 if Nkind (Parent (P)) = N_Subunit then
8586 return Corresponding_Stub (Parent (P));
8592 P := True_Parent (P);
8598 -----------------------
8599 -- Previous_Instance --
8600 -----------------------
8602 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8607 while Present (S) and then S /= Standard_Standard loop
8608 if Is_Generic_Instance (S)
8609 and then In_Same_Source_Unit (S, N)
8618 end Previous_Instance;
8620 -- Start of processing for Insert_Freeze_Node_For_Instance
8623 if not Is_List_Member (F_Node) then
8625 Decls := List_Containing (N);
8626 Inst := Entity (F_Node);
8627 Par_N := Parent (Decls);
8629 -- When processing a subprogram instantiation, utilize the actual
8630 -- subprogram instantiation rather than its package wrapper as it
8631 -- carries all the context information.
8633 if Is_Wrapper_Package (Inst) then
8634 Inst := Related_Instance (Inst);
8637 -- If this is a package instance, check whether the generic is
8638 -- declared in a previous instance and the current instance is
8639 -- not within the previous one.
8641 if Present (Generic_Parent (Parent (Inst)))
8642 and then Is_In_Main_Unit (N)
8645 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8646 Par_I : constant Entity_Id :=
8648 (Generic_Parent (Parent (Inst)));
8653 and then Earlier (N, Freeze_Node (Par_I))
8655 Scop := Scope (Inst);
8657 -- If the current instance is within the one that contains
8658 -- the generic, the freeze node for the current one must
8659 -- appear in the current declarative part. Ditto, if the
8660 -- current instance is within another package instance or
8661 -- within a body that does not enclose the current instance.
8662 -- In these three cases the freeze node of the previous
8663 -- instance is not relevant.
8665 while Present (Scop) and then Scop /= Standard_Standard loop
8666 exit when Scop = Par_I
8668 (Is_Generic_Instance (Scop)
8669 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8670 Scop := Scope (Scop);
8673 -- Previous instance encloses current instance
8675 if Scop = Par_I then
8678 -- If the next node is a source body we must freeze in
8679 -- the current scope as well.
8681 elsif Present (Next (N))
8682 and then Nkind_In (Next (N), N_Subprogram_Body,
8684 and then Comes_From_Source (Next (N))
8688 -- Current instance is within an unrelated instance
8690 elsif Is_Generic_Instance (Scop) then
8693 -- Current instance is within an unrelated body
8695 elsif Present (Enclosing_N)
8696 and then Enclosing_N /= Enclosing_Body (Par_I)
8701 Insert_After (Freeze_Node (Par_I), F_Node);
8708 -- When the instantiation occurs in a package declaration, append the
8709 -- freeze node to the private declarations (if any).
8711 if Nkind (Par_N) = N_Package_Specification
8712 and then Decls = Visible_Declarations (Par_N)
8713 and then Present (Private_Declarations (Par_N))
8714 and then not Is_Empty_List (Private_Declarations (Par_N))
8716 Decls := Private_Declarations (Par_N);
8717 Decl := First (Decls);
8720 -- Determine the proper freeze point of a package instantiation. We
8721 -- adhere to the general rule of a package or subprogram body causing
8722 -- freezing of anything before it in the same declarative region. In
8723 -- this case, the proper freeze point of a package instantiation is
8724 -- before the first source body which follows, or before a stub. This
8725 -- ensures that entities coming from the instance are already frozen
8726 -- and usable in source bodies.
8728 if Nkind (Par_N) /= N_Package_Declaration
8729 and then Ekind (Inst) = E_Package
8730 and then Is_Generic_Instance (Inst)
8732 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8734 while Present (Decl) loop
8735 if (Nkind (Decl) in N_Unit_Body
8737 Nkind (Decl) in N_Body_Stub)
8738 and then Comes_From_Source (Decl)
8740 Insert_Before (Decl, F_Node);
8748 -- In a package declaration, or if no previous body, insert at end
8751 Set_Sloc (F_Node, Sloc (Last (Decls)));
8752 Insert_After (Last (Decls), F_Node);
8754 end Insert_Freeze_Node_For_Instance;
8760 procedure Install_Body
8761 (Act_Body : Node_Id;
8766 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8767 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8768 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8769 Par : constant Entity_Id := Scope (Gen_Id);
8770 Gen_Unit : constant Node_Id :=
8771 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8772 Orig_Body : Node_Id := Gen_Body;
8774 Body_Unit : Node_Id;
8776 Must_Delay : Boolean;
8778 function In_Same_Enclosing_Subp return Boolean;
8779 -- Check whether instance and generic body are within same subprogram.
8781 function True_Sloc (N : Node_Id) return Source_Ptr;
8782 -- If the instance is nested inside a generic unit, the Sloc of the
8783 -- instance indicates the place of the original definition, not the
8784 -- point of the current enclosing instance. Pending a better usage of
8785 -- Slocs to indicate instantiation places, we determine the place of
8786 -- origin of a node by finding the maximum sloc of any ancestor node.
8787 -- Why is this not equivalent to Top_Level_Location ???
8789 ----------------------------
8790 -- In_Same_Enclosing_Subp --
8791 ----------------------------
8793 function In_Same_Enclosing_Subp return Boolean is
8798 Scop := Scope (Act_Id);
8799 while Scop /= Standard_Standard
8800 and then not Is_Overloadable (Scop)
8802 Scop := Scope (Scop);
8805 if Scop = Standard_Standard then
8811 Scop := Scope (Gen_Id);
8812 while Scop /= Standard_Standard loop
8816 Scop := Scope (Scop);
8821 end In_Same_Enclosing_Subp;
8827 function True_Sloc (N : Node_Id) return Source_Ptr is
8834 while Present (N1) and then N1 /= Act_Unit loop
8835 if Sloc (N1) > Res then
8845 -- Start of processing for Install_Body
8848 -- If the body is a subunit, the freeze point is the corresponding stub
8849 -- in the current compilation, not the subunit itself.
8851 if Nkind (Parent (Gen_Body)) = N_Subunit then
8852 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8854 Orig_Body := Gen_Body;
8857 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8859 -- If the instantiation and the generic definition appear in the same
8860 -- package declaration, this is an early instantiation. If they appear
8861 -- in the same declarative part, it is an early instantiation only if
8862 -- the generic body appears textually later, and the generic body is
8863 -- also in the main unit.
8865 -- If instance is nested within a subprogram, and the generic body
8866 -- is not, the instance is delayed because the enclosing body is. If
8867 -- instance and body are within the same scope, or the same subprogram
8868 -- body, indicate explicitly that the instance is delayed.
8871 (Gen_Unit = Act_Unit
8872 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8873 N_Generic_Package_Declaration)
8874 or else (Gen_Unit = Body_Unit
8875 and then True_Sloc (N) < Sloc (Orig_Body)))
8876 and then Is_In_Main_Unit (Gen_Unit)
8877 and then (Scope (Act_Id) = Scope (Gen_Id)
8878 or else In_Same_Enclosing_Subp));
8880 -- If this is an early instantiation, the freeze node is placed after
8881 -- the generic body. Otherwise, if the generic appears in an instance,
8882 -- we cannot freeze the current instance until the outer one is frozen.
8883 -- This is only relevant if the current instance is nested within some
8884 -- inner scope not itself within the outer instance. If this scope is
8885 -- a package body in the same declarative part as the outer instance,
8886 -- then that body needs to be frozen after the outer instance. Finally,
8887 -- if no delay is needed, we place the freeze node at the end of the
8888 -- current declarative part.
8890 if Expander_Active then
8891 Ensure_Freeze_Node (Act_Id);
8892 F_Node := Freeze_Node (Act_Id);
8895 Insert_After (Orig_Body, F_Node);
8897 elsif Is_Generic_Instance (Par)
8898 and then Present (Freeze_Node (Par))
8899 and then Scope (Act_Id) /= Par
8901 -- Freeze instance of inner generic after instance of enclosing
8904 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8906 -- Handle the following case:
8908 -- package Parent_Inst is new ...
8911 -- procedure P ... -- this body freezes Parent_Inst
8913 -- package Inst is new ...
8915 -- In this particular scenario, the freeze node for Inst must
8916 -- be inserted in the same manner as that of Parent_Inst,
8917 -- before the next source body or at the end of the declarative
8918 -- list (body not available). If body P did not exist and
8919 -- Parent_Inst was frozen after Inst, either by a body
8920 -- following Inst or at the end of the declarative region,
8921 -- the freeze node for Inst must be inserted after that of
8922 -- Parent_Inst. This relation is established by comparing
8923 -- the Slocs of Parent_Inst freeze node and Inst.
8925 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8927 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8929 Insert_Freeze_Node_For_Instance (N, F_Node);
8931 Insert_After (Freeze_Node (Par), F_Node);
8934 -- Freeze package enclosing instance of inner generic after
8935 -- instance of enclosing generic.
8937 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8938 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8941 Enclosing : Entity_Id;
8944 Enclosing := Corresponding_Spec (Parent (N));
8946 if No (Enclosing) then
8947 Enclosing := Defining_Entity (Parent (N));
8950 Insert_Freeze_Node_For_Instance (N, F_Node);
8951 Ensure_Freeze_Node (Enclosing);
8953 if not Is_List_Member (Freeze_Node (Enclosing)) then
8955 -- The enclosing context is a subunit, insert the freeze
8956 -- node after the stub.
8958 if Nkind (Parent (Parent (N))) = N_Subunit then
8959 Insert_Freeze_Node_For_Instance
8960 (Corresponding_Stub (Parent (Parent (N))),
8961 Freeze_Node (Enclosing));
8963 -- The enclosing context is a package with a stub body
8964 -- which has already been replaced by the real body.
8965 -- Insert the freeze node after the actual body.
8967 elsif Ekind (Enclosing) = E_Package
8968 and then Present (Body_Entity (Enclosing))
8969 and then Was_Originally_Stub
8970 (Parent (Body_Entity (Enclosing)))
8972 Insert_Freeze_Node_For_Instance
8973 (Parent (Body_Entity (Enclosing)),
8974 Freeze_Node (Enclosing));
8976 -- The parent instance has been frozen before the body of
8977 -- the enclosing package, insert the freeze node after
8980 elsif List_Containing (Freeze_Node (Par)) =
8981 List_Containing (Parent (N))
8982 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8984 Insert_Freeze_Node_For_Instance
8985 (Parent (N), Freeze_Node (Enclosing));
8989 (Freeze_Node (Par), Freeze_Node (Enclosing));
8995 Insert_Freeze_Node_For_Instance (N, F_Node);
8999 Insert_Freeze_Node_For_Instance (N, F_Node);
9003 Set_Is_Frozen (Act_Id);
9004 Insert_Before (N, Act_Body);
9005 Mark_Rewrite_Insertion (Act_Body);
9008 -----------------------------
9009 -- Install_Formal_Packages --
9010 -----------------------------
9012 procedure Install_Formal_Packages (Par : Entity_Id) is
9015 Gen_E : Entity_Id := Empty;
9018 E := First_Entity (Par);
9020 -- If we are installing an instance parent, locate the formal packages
9021 -- of its generic parent.
9023 if Is_Generic_Instance (Par) then
9024 Gen := Generic_Parent (Package_Specification (Par));
9025 Gen_E := First_Entity (Gen);
9028 while Present (E) loop
9029 if Ekind (E) = E_Package
9030 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9032 -- If this is the renaming for the parent instance, done
9034 if Renamed_Object (E) = Par then
9037 -- The visibility of a formal of an enclosing generic is already
9040 elsif Denotes_Formal_Package (E) then
9043 elsif Present (Associated_Formal_Package (E)) then
9044 Check_Generic_Actuals (Renamed_Object (E), True);
9045 Set_Is_Hidden (E, False);
9047 -- Find formal package in generic unit that corresponds to
9048 -- (instance of) formal package in instance.
9050 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9051 Next_Entity (Gen_E);
9054 if Present (Gen_E) then
9055 Map_Formal_Package_Entities (Gen_E, E);
9062 if Present (Gen_E) then
9063 Next_Entity (Gen_E);
9066 end Install_Formal_Packages;
9068 --------------------
9069 -- Install_Parent --
9070 --------------------
9072 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9073 Ancestors : constant Elist_Id := New_Elmt_List;
9074 S : constant Entity_Id := Current_Scope;
9075 Inst_Par : Entity_Id;
9076 First_Par : Entity_Id;
9077 Inst_Node : Node_Id;
9078 Gen_Par : Entity_Id;
9079 First_Gen : Entity_Id;
9082 procedure Install_Noninstance_Specs (Par : Entity_Id);
9083 -- Install the scopes of noninstance parent units ending with Par
9085 procedure Install_Spec (Par : Entity_Id);
9086 -- The child unit is within the declarative part of the parent, so the
9087 -- declarations within the parent are immediately visible.
9089 -------------------------------
9090 -- Install_Noninstance_Specs --
9091 -------------------------------
9093 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9096 and then Par /= Standard_Standard
9097 and then not In_Open_Scopes (Par)
9099 Install_Noninstance_Specs (Scope (Par));
9102 end Install_Noninstance_Specs;
9108 procedure Install_Spec (Par : Entity_Id) is
9109 Spec : constant Node_Id := Package_Specification (Par);
9112 -- If this parent of the child instance is a top-level unit,
9113 -- then record the unit and its visibility for later resetting in
9114 -- Remove_Parent. We exclude units that are generic instances, as we
9115 -- only want to record this information for the ultimate top-level
9116 -- noninstance parent (is that always correct???).
9118 if Scope (Par) = Standard_Standard
9119 and then not Is_Generic_Instance (Par)
9121 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9122 Instance_Parent_Unit := Par;
9125 -- Open the parent scope and make it and its declarations visible.
9126 -- If this point is not within a body, then only the visible
9127 -- declarations should be made visible, and installation of the
9128 -- private declarations is deferred until the appropriate point
9129 -- within analysis of the spec being instantiated (see the handling
9130 -- of parent visibility in Analyze_Package_Specification). This is
9131 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9132 -- private view problems that occur when compiling instantiations of
9133 -- a generic child of that package (Generic_Dispatching_Constructor).
9134 -- If the instance freezes a tagged type, inlinings of operations
9135 -- from Ada.Tags may need the full view of type Tag. If inlining took
9136 -- proper account of establishing visibility of inlined subprograms'
9137 -- parents then it should be possible to remove this
9138 -- special check. ???
9141 Set_Is_Immediately_Visible (Par);
9142 Install_Visible_Declarations (Par);
9143 Set_Use (Visible_Declarations (Spec));
9145 if In_Body or else Is_RTU (Par, Ada_Tags) then
9146 Install_Private_Declarations (Par);
9147 Set_Use (Private_Declarations (Spec));
9151 -- Start of processing for Install_Parent
9154 -- We need to install the parent instance to compile the instantiation
9155 -- of the child, but the child instance must appear in the current
9156 -- scope. Given that we cannot place the parent above the current scope
9157 -- in the scope stack, we duplicate the current scope and unstack both
9158 -- after the instantiation is complete.
9160 -- If the parent is itself the instantiation of a child unit, we must
9161 -- also stack the instantiation of its parent, and so on. Each such
9162 -- ancestor is the prefix of the name in a prior instantiation.
9164 -- If this is a nested instance, the parent unit itself resolves to
9165 -- a renaming of the parent instance, whose declaration we need.
9167 -- Finally, the parent may be a generic (not an instance) when the
9168 -- child unit appears as a formal package.
9172 if Present (Renamed_Entity (Inst_Par)) then
9173 Inst_Par := Renamed_Entity (Inst_Par);
9176 First_Par := Inst_Par;
9178 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9180 First_Gen := Gen_Par;
9182 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9184 -- Load grandparent instance as well
9186 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9188 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9189 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9191 if Present (Renamed_Entity (Inst_Par)) then
9192 Inst_Par := Renamed_Entity (Inst_Par);
9195 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9197 if Present (Gen_Par) then
9198 Prepend_Elmt (Inst_Par, Ancestors);
9201 -- Parent is not the name of an instantiation
9203 Install_Noninstance_Specs (Inst_Par);
9214 if Present (First_Gen) then
9215 Append_Elmt (First_Par, Ancestors);
9217 Install_Noninstance_Specs (First_Par);
9220 if not Is_Empty_Elmt_List (Ancestors) then
9221 Elmt := First_Elmt (Ancestors);
9222 while Present (Elmt) loop
9223 Install_Spec (Node (Elmt));
9224 Install_Formal_Packages (Node (Elmt));
9234 -------------------------------
9235 -- Install_Hidden_Primitives --
9236 -------------------------------
9238 procedure Install_Hidden_Primitives
9239 (Prims_List : in out Elist_Id;
9244 List : Elist_Id := No_Elist;
9245 Prim_G_Elmt : Elmt_Id;
9246 Prim_A_Elmt : Elmt_Id;
9251 -- No action needed in case of serious errors because we cannot trust
9252 -- in the order of primitives
9254 if Serious_Errors_Detected > 0 then
9257 -- No action possible if we don't have available the list of primitive
9261 or else not Is_Record_Type (Gen_T)
9262 or else not Is_Tagged_Type (Gen_T)
9263 or else not Is_Record_Type (Act_T)
9264 or else not Is_Tagged_Type (Act_T)
9268 -- There is no need to handle interface types since their primitives
9271 elsif Is_Interface (Gen_T) then
9275 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9277 if not Is_Class_Wide_Type (Act_T) then
9278 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9280 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9284 -- Skip predefined primitives in the generic formal
9286 while Present (Prim_G_Elmt)
9287 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9289 Next_Elmt (Prim_G_Elmt);
9292 -- Skip predefined primitives in the generic actual
9294 while Present (Prim_A_Elmt)
9295 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9297 Next_Elmt (Prim_A_Elmt);
9300 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9302 Prim_G := Node (Prim_G_Elmt);
9303 Prim_A := Node (Prim_A_Elmt);
9305 -- There is no need to handle interface primitives because their
9306 -- primitives are not hidden
9308 exit when Present (Interface_Alias (Prim_G));
9310 -- Here we install one hidden primitive
9312 if Chars (Prim_G) /= Chars (Prim_A)
9313 and then Has_Suffix (Prim_A, 'P')
9314 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9316 Set_Chars (Prim_A, Chars (Prim_G));
9317 Append_New_Elmt (Prim_A, To => List);
9320 Next_Elmt (Prim_A_Elmt);
9321 Next_Elmt (Prim_G_Elmt);
9324 -- Append the elements to the list of temporarily visible primitives
9325 -- avoiding duplicates.
9327 if Present (List) then
9328 if No (Prims_List) then
9329 Prims_List := New_Elmt_List;
9332 Elmt := First_Elmt (List);
9333 while Present (Elmt) loop
9334 Append_Unique_Elmt (Node (Elmt), Prims_List);
9338 end Install_Hidden_Primitives;
9340 -------------------------------
9341 -- Restore_Hidden_Primitives --
9342 -------------------------------
9344 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9345 Prim_Elmt : Elmt_Id;
9349 if Prims_List /= No_Elist then
9350 Prim_Elmt := First_Elmt (Prims_List);
9351 while Present (Prim_Elmt) loop
9352 Prim := Node (Prim_Elmt);
9353 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9354 Next_Elmt (Prim_Elmt);
9357 Prims_List := No_Elist;
9359 end Restore_Hidden_Primitives;
9361 --------------------------------
9362 -- Instantiate_Formal_Package --
9363 --------------------------------
9365 function Instantiate_Formal_Package
9368 Analyzed_Formal : Node_Id) return List_Id
9370 Loc : constant Source_Ptr := Sloc (Actual);
9371 Actual_Pack : Entity_Id;
9372 Formal_Pack : Entity_Id;
9373 Gen_Parent : Entity_Id;
9376 Parent_Spec : Node_Id;
9378 procedure Find_Matching_Actual
9380 Act : in out Entity_Id);
9381 -- We need to associate each formal entity in the formal package with
9382 -- the corresponding entity in the actual package. The actual package
9383 -- has been analyzed and possibly expanded, and as a result there is
9384 -- no one-to-one correspondence between the two lists (for example,
9385 -- the actual may include subtypes, itypes, and inherited primitive
9386 -- operations, interspersed among the renaming declarations for the
9387 -- actuals) . We retrieve the corresponding actual by name because each
9388 -- actual has the same name as the formal, and they do appear in the
9391 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9392 -- Retrieve entity of defining entity of generic formal parameter.
9393 -- Only the declarations of formals need to be considered when
9394 -- linking them to actuals, but the declarative list may include
9395 -- internal entities generated during analysis, and those are ignored.
9397 procedure Match_Formal_Entity
9398 (Formal_Node : Node_Id;
9399 Formal_Ent : Entity_Id;
9400 Actual_Ent : Entity_Id);
9401 -- Associates the formal entity with the actual. In the case where
9402 -- Formal_Ent is a formal package, this procedure iterates through all
9403 -- of its formals and enters associations between the actuals occurring
9404 -- in the formal package's corresponding actual package (given by
9405 -- Actual_Ent) and the formal package's formal parameters. This
9406 -- procedure recurses if any of the parameters is itself a package.
9408 function Is_Instance_Of
9409 (Act_Spec : Entity_Id;
9410 Gen_Anc : Entity_Id) return Boolean;
9411 -- The actual can be an instantiation of a generic within another
9412 -- instance, in which case there is no direct link from it to the
9413 -- original generic ancestor. In that case, we recognize that the
9414 -- ultimate ancestor is the same by examining names and scopes.
9416 procedure Process_Nested_Formal (Formal : Entity_Id);
9417 -- If the current formal is declared with a box, its own formals are
9418 -- visible in the instance, as they were in the generic, and their
9419 -- Hidden flag must be reset. If some of these formals are themselves
9420 -- packages declared with a box, the processing must be recursive.
9422 --------------------------
9423 -- Find_Matching_Actual --
9424 --------------------------
9426 procedure Find_Matching_Actual
9428 Act : in out Entity_Id)
9430 Formal_Ent : Entity_Id;
9433 case Nkind (Original_Node (F)) is
9434 when N_Formal_Object_Declaration |
9435 N_Formal_Type_Declaration =>
9436 Formal_Ent := Defining_Identifier (F);
9438 while Chars (Act) /= Chars (Formal_Ent) loop
9442 when N_Formal_Subprogram_Declaration |
9443 N_Formal_Package_Declaration |
9444 N_Package_Declaration |
9445 N_Generic_Package_Declaration =>
9446 Formal_Ent := Defining_Entity (F);
9448 while Chars (Act) /= Chars (Formal_Ent) loop
9453 raise Program_Error;
9455 end Find_Matching_Actual;
9457 -------------------------
9458 -- Match_Formal_Entity --
9459 -------------------------
9461 procedure Match_Formal_Entity
9462 (Formal_Node : Node_Id;
9463 Formal_Ent : Entity_Id;
9464 Actual_Ent : Entity_Id)
9466 Act_Pkg : Entity_Id;
9469 Set_Instance_Of (Formal_Ent, Actual_Ent);
9471 if Ekind (Actual_Ent) = E_Package then
9473 -- Record associations for each parameter
9475 Act_Pkg := Actual_Ent;
9478 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9487 -- Retrieve the actual given in the formal package declaration
9489 Actual := Entity (Name (Original_Node (Formal_Node)));
9491 -- The actual in the formal package declaration may be a
9492 -- renamed generic package, in which case we want to retrieve
9493 -- the original generic in order to traverse its formal part.
9495 if Present (Renamed_Entity (Actual)) then
9496 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9498 Gen_Decl := Unit_Declaration_Node (Actual);
9501 Formals := Generic_Formal_Declarations (Gen_Decl);
9503 if Present (Formals) then
9504 F_Node := First_Non_Pragma (Formals);
9509 while Present (A_Ent)
9510 and then Present (F_Node)
9511 and then A_Ent /= First_Private_Entity (Act_Pkg)
9513 F_Ent := Get_Formal_Entity (F_Node);
9515 if Present (F_Ent) then
9517 -- This is a formal of the original package. Record
9518 -- association and recurse.
9520 Find_Matching_Actual (F_Node, A_Ent);
9521 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9522 Next_Entity (A_Ent);
9525 Next_Non_Pragma (F_Node);
9529 end Match_Formal_Entity;
9531 -----------------------
9532 -- Get_Formal_Entity --
9533 -----------------------
9535 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9536 Kind : constant Node_Kind := Nkind (Original_Node (N));
9539 when N_Formal_Object_Declaration =>
9540 return Defining_Identifier (N);
9542 when N_Formal_Type_Declaration =>
9543 return Defining_Identifier (N);
9545 when N_Formal_Subprogram_Declaration =>
9546 return Defining_Unit_Name (Specification (N));
9548 when N_Formal_Package_Declaration =>
9549 return Defining_Identifier (Original_Node (N));
9551 when N_Generic_Package_Declaration =>
9552 return Defining_Identifier (Original_Node (N));
9554 -- All other declarations are introduced by semantic analysis and
9555 -- have no match in the actual.
9560 end Get_Formal_Entity;
9562 --------------------
9563 -- Is_Instance_Of --
9564 --------------------
9566 function Is_Instance_Of
9567 (Act_Spec : Entity_Id;
9568 Gen_Anc : Entity_Id) return Boolean
9570 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9573 if No (Gen_Par) then
9576 -- Simplest case: the generic parent of the actual is the formal
9578 elsif Gen_Par = Gen_Anc then
9581 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9584 -- The actual may be obtained through several instantiations. Its
9585 -- scope must itself be an instance of a generic declared in the
9586 -- same scope as the formal. Any other case is detected above.
9588 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9592 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9596 ---------------------------
9597 -- Process_Nested_Formal --
9598 ---------------------------
9600 procedure Process_Nested_Formal (Formal : Entity_Id) is
9604 if Present (Associated_Formal_Package (Formal))
9605 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9607 Ent := First_Entity (Formal);
9608 while Present (Ent) loop
9609 Set_Is_Hidden (Ent, False);
9610 Set_Is_Visible_Formal (Ent);
9611 Set_Is_Potentially_Use_Visible
9612 (Ent, Is_Potentially_Use_Visible (Formal));
9614 if Ekind (Ent) = E_Package then
9615 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9616 Process_Nested_Formal (Ent);
9622 end Process_Nested_Formal;
9624 -- Start of processing for Instantiate_Formal_Package
9629 if not Is_Entity_Name (Actual)
9630 or else Ekind (Entity (Actual)) /= E_Package
9633 ("expect package instance to instantiate formal", Actual);
9634 Abandon_Instantiation (Actual);
9635 raise Program_Error;
9638 Actual_Pack := Entity (Actual);
9639 Set_Is_Instantiated (Actual_Pack);
9641 -- The actual may be a renamed package, or an outer generic formal
9642 -- package whose instantiation is converted into a renaming.
9644 if Present (Renamed_Object (Actual_Pack)) then
9645 Actual_Pack := Renamed_Object (Actual_Pack);
9648 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9649 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9650 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9653 Generic_Parent (Specification (Analyzed_Formal));
9655 Defining_Unit_Name (Specification (Analyzed_Formal));
9658 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9659 Parent_Spec := Package_Specification (Actual_Pack);
9661 Parent_Spec := Parent (Actual_Pack);
9664 if Gen_Parent = Any_Id then
9666 ("previous error in declaration of formal package", Actual);
9667 Abandon_Instantiation (Actual);
9670 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9676 ("actual parameter must be instance of&", Actual, Gen_Parent);
9677 Abandon_Instantiation (Actual);
9680 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9681 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9684 Make_Package_Renaming_Declaration (Loc,
9685 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9686 Name => New_Occurrence_Of (Actual_Pack, Loc));
9688 Set_Associated_Formal_Package
9689 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9690 Decls := New_List (Nod);
9692 -- If the formal F has a box, then the generic declarations are
9693 -- visible in the generic G. In an instance of G, the corresponding
9694 -- entities in the actual for F (which are the actuals for the
9695 -- instantiation of the generic that F denotes) must also be made
9696 -- visible for analysis of the current instance. On exit from the
9697 -- current instance, those entities are made private again. If the
9698 -- actual is currently in use, these entities are also use-visible.
9700 -- The loop through the actual entities also steps through the formal
9701 -- entities and enters associations from formals to actuals into the
9702 -- renaming map. This is necessary to properly handle checking of
9703 -- actual parameter associations for later formals that depend on
9704 -- actuals declared in the formal package.
9706 -- In Ada 2005, partial parameterization requires that we make
9707 -- visible the actuals corresponding to formals that were defaulted
9708 -- in the formal package. There formals are identified because they
9709 -- remain formal generics within the formal package, rather than
9710 -- being renamings of the actuals supplied.
9713 Gen_Decl : constant Node_Id :=
9714 Unit_Declaration_Node (Gen_Parent);
9715 Formals : constant List_Id :=
9716 Generic_Formal_Declarations (Gen_Decl);
9718 Actual_Ent : Entity_Id;
9719 Actual_Of_Formal : Node_Id;
9720 Formal_Node : Node_Id;
9721 Formal_Ent : Entity_Id;
9724 if Present (Formals) then
9725 Formal_Node := First_Non_Pragma (Formals);
9727 Formal_Node := Empty;
9730 Actual_Ent := First_Entity (Actual_Pack);
9732 First (Visible_Declarations (Specification (Analyzed_Formal)));
9733 while Present (Actual_Ent)
9734 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9736 if Present (Formal_Node) then
9737 Formal_Ent := Get_Formal_Entity (Formal_Node);
9739 if Present (Formal_Ent) then
9740 Find_Matching_Actual (Formal_Node, Actual_Ent);
9741 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9743 -- We iterate at the same time over the actuals of the
9744 -- local package created for the formal, to determine
9745 -- which one of the formals of the original generic were
9746 -- defaulted in the formal. The corresponding actual
9747 -- entities are visible in the enclosing instance.
9749 if Box_Present (Formal)
9751 (Present (Actual_Of_Formal)
9754 (Get_Formal_Entity (Actual_Of_Formal)))
9756 Set_Is_Hidden (Actual_Ent, False);
9757 Set_Is_Visible_Formal (Actual_Ent);
9758 Set_Is_Potentially_Use_Visible
9759 (Actual_Ent, In_Use (Actual_Pack));
9761 if Ekind (Actual_Ent) = E_Package then
9762 Process_Nested_Formal (Actual_Ent);
9766 Set_Is_Hidden (Actual_Ent);
9767 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9771 Next_Non_Pragma (Formal_Node);
9772 Next (Actual_Of_Formal);
9775 -- No further formals to match, but the generic part may
9776 -- contain inherited operation that are not hidden in the
9777 -- enclosing instance.
9779 Next_Entity (Actual_Ent);
9783 -- Inherited subprograms generated by formal derived types are
9784 -- also visible if the types are.
9786 Actual_Ent := First_Entity (Actual_Pack);
9787 while Present (Actual_Ent)
9788 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9790 if Is_Overloadable (Actual_Ent)
9792 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9794 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9796 Set_Is_Hidden (Actual_Ent, False);
9797 Set_Is_Potentially_Use_Visible
9798 (Actual_Ent, In_Use (Actual_Pack));
9801 Next_Entity (Actual_Ent);
9805 -- If the formal is not declared with a box, reanalyze it as an
9806 -- abbreviated instantiation, to verify the matching rules of 12.7.
9807 -- The actual checks are performed after the generic associations
9808 -- have been analyzed, to guarantee the same visibility for this
9809 -- instantiation and for the actuals.
9811 -- In Ada 2005, the generic associations for the formal can include
9812 -- defaulted parameters. These are ignored during check. This
9813 -- internal instantiation is removed from the tree after conformance
9814 -- checking, because it contains formal declarations for those
9815 -- defaulted parameters, and those should not reach the back-end.
9817 if not Box_Present (Formal) then
9819 I_Pack : constant Entity_Id :=
9820 Make_Temporary (Sloc (Actual), 'P');
9823 Set_Is_Internal (I_Pack);
9826 Make_Package_Instantiation (Sloc (Actual),
9827 Defining_Unit_Name => I_Pack,
9830 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9831 Generic_Associations => Generic_Associations (Formal)));
9837 end Instantiate_Formal_Package;
9839 -----------------------------------
9840 -- Instantiate_Formal_Subprogram --
9841 -----------------------------------
9843 function Instantiate_Formal_Subprogram
9846 Analyzed_Formal : Node_Id) return Node_Id
9848 Analyzed_S : constant Entity_Id :=
9849 Defining_Unit_Name (Specification (Analyzed_Formal));
9850 Formal_Sub : constant Entity_Id :=
9851 Defining_Unit_Name (Specification (Formal));
9853 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9854 -- If the generic is a child unit, the parent has been installed on the
9855 -- scope stack, but a default subprogram cannot resolve to something
9856 -- on the parent because that parent is not really part of the visible
9857 -- context (it is there to resolve explicit local entities). If the
9858 -- default has resolved in this way, we remove the entity from immediate
9859 -- visibility and analyze the node again to emit an error message or
9860 -- find another visible candidate.
9862 procedure Valid_Actual_Subprogram (Act : Node_Id);
9863 -- Perform legality check and raise exception on failure
9865 -----------------------
9866 -- From_Parent_Scope --
9867 -----------------------
9869 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9870 Gen_Scope : Node_Id;
9873 Gen_Scope := Scope (Analyzed_S);
9874 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9875 if Scope (Subp) = Scope (Gen_Scope) then
9879 Gen_Scope := Scope (Gen_Scope);
9883 end From_Parent_Scope;
9885 -----------------------------
9886 -- Valid_Actual_Subprogram --
9887 -----------------------------
9889 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9893 if Is_Entity_Name (Act) then
9894 Act_E := Entity (Act);
9896 elsif Nkind (Act) = N_Selected_Component
9897 and then Is_Entity_Name (Selector_Name (Act))
9899 Act_E := Entity (Selector_Name (Act));
9905 if (Present (Act_E) and then Is_Overloadable (Act_E))
9906 or else Nkind_In (Act, N_Attribute_Reference,
9907 N_Indexed_Component,
9908 N_Character_Literal,
9909 N_Explicit_Dereference)
9915 ("expect subprogram or entry name in instantiation of &",
9916 Instantiation_Node, Formal_Sub);
9917 Abandon_Instantiation (Instantiation_Node);
9918 end Valid_Actual_Subprogram;
9922 Decl_Node : Node_Id;
9926 New_Subp : Entity_Id;
9928 -- Start of processing for Instantiate_Formal_Subprogram
9931 New_Spec := New_Copy_Tree (Specification (Formal));
9933 -- The tree copy has created the proper instantiation sloc for the
9934 -- new specification. Use this location for all other constructed
9937 Loc := Sloc (Defining_Unit_Name (New_Spec));
9939 -- Create new entity for the actual (New_Copy_Tree does not), and
9940 -- indicate that it is an actual.
9942 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9943 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9944 Set_Is_Generic_Actual_Subprogram (New_Subp);
9945 Set_Defining_Unit_Name (New_Spec, New_Subp);
9947 -- Create new entities for the each of the formals in the specification
9948 -- of the renaming declaration built for the actual.
9950 if Present (Parameter_Specifications (New_Spec)) then
9956 F := First (Parameter_Specifications (New_Spec));
9957 while Present (F) loop
9958 F_Id := Defining_Identifier (F);
9960 Set_Defining_Identifier (F,
9961 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9967 -- Find entity of actual. If the actual is an attribute reference, it
9968 -- cannot be resolved here (its formal is missing) but is handled
9969 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9970 -- fully resolved subsequently, when the renaming declaration for the
9971 -- formal is analyzed. If it is an explicit dereference, resolve the
9972 -- prefix but not the actual itself, to prevent interpretation as call.
9974 if Present (Actual) then
9975 Loc := Sloc (Actual);
9976 Set_Sloc (New_Spec, Loc);
9978 if Nkind (Actual) = N_Operator_Symbol then
9979 Find_Direct_Name (Actual);
9981 elsif Nkind (Actual) = N_Explicit_Dereference then
9982 Analyze (Prefix (Actual));
9984 elsif Nkind (Actual) /= N_Attribute_Reference then
9988 Valid_Actual_Subprogram (Actual);
9991 elsif Present (Default_Name (Formal)) then
9992 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9993 N_Selected_Component,
9994 N_Indexed_Component,
9995 N_Character_Literal)
9996 and then Present (Entity (Default_Name (Formal)))
9998 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10000 Nam := New_Copy (Default_Name (Formal));
10001 Set_Sloc (Nam, Loc);
10004 elsif Box_Present (Formal) then
10006 -- Actual is resolved at the point of instantiation. Create an
10007 -- identifier or operator with the same name as the formal.
10009 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10011 Make_Operator_Symbol (Loc,
10012 Chars => Chars (Formal_Sub),
10013 Strval => No_String);
10015 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10018 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10019 and then Null_Present (Specification (Formal))
10021 -- Generate null body for procedure, for use in the instance
10024 Make_Subprogram_Body (Loc,
10025 Specification => New_Spec,
10026 Declarations => New_List,
10027 Handled_Statement_Sequence =>
10028 Make_Handled_Sequence_Of_Statements (Loc,
10029 Statements => New_List (Make_Null_Statement (Loc))));
10031 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10035 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10037 ("missing actual&", Instantiation_Node, Formal_Sub);
10039 ("\in instantiation of & declared#",
10040 Instantiation_Node, Scope (Analyzed_S));
10041 Abandon_Instantiation (Instantiation_Node);
10045 Make_Subprogram_Renaming_Declaration (Loc,
10046 Specification => New_Spec,
10049 -- If we do not have an actual and the formal specified <> then set to
10050 -- get proper default.
10052 if No (Actual) and then Box_Present (Formal) then
10053 Set_From_Default (Decl_Node);
10056 -- Gather possible interpretations for the actual before analyzing the
10057 -- instance. If overloaded, it will be resolved when analyzing the
10058 -- renaming declaration.
10060 if Box_Present (Formal) and then No (Actual) then
10063 if Is_Child_Unit (Scope (Analyzed_S))
10064 and then Present (Entity (Nam))
10066 if not Is_Overloaded (Nam) then
10067 if From_Parent_Scope (Entity (Nam)) then
10068 Set_Is_Immediately_Visible (Entity (Nam), False);
10069 Set_Entity (Nam, Empty);
10070 Set_Etype (Nam, Empty);
10073 Set_Is_Immediately_Visible (Entity (Nam));
10082 Get_First_Interp (Nam, I, It);
10083 while Present (It.Nam) loop
10084 if From_Parent_Scope (It.Nam) then
10088 Get_Next_Interp (I, It);
10095 -- The generic instantiation freezes the actual. This can only be done
10096 -- once the actual is resolved, in the analysis of the renaming
10097 -- declaration. To make the formal subprogram entity available, we set
10098 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10099 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10100 -- of formal abstract subprograms.
10102 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10104 -- We cannot analyze the renaming declaration, and thus find the actual,
10105 -- until all the actuals are assembled in the instance. For subsequent
10106 -- checks of other actuals, indicate the node that will hold the
10107 -- instance of this formal.
10109 Set_Instance_Of (Analyzed_S, Nam);
10111 if Nkind (Actual) = N_Selected_Component
10112 and then Is_Task_Type (Etype (Prefix (Actual)))
10113 and then not Is_Frozen (Etype (Prefix (Actual)))
10115 -- The renaming declaration will create a body, which must appear
10116 -- outside of the instantiation, We move the renaming declaration
10117 -- out of the instance, and create an additional renaming inside,
10118 -- to prevent freezing anomalies.
10121 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10124 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10125 Insert_Before (Instantiation_Node, Decl_Node);
10126 Analyze (Decl_Node);
10128 -- Now create renaming within the instance
10131 Make_Subprogram_Renaming_Declaration (Loc,
10132 Specification => New_Copy_Tree (New_Spec),
10133 Name => New_Occurrence_Of (Anon_Id, Loc));
10135 Set_Defining_Unit_Name (Specification (Decl_Node),
10136 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10141 end Instantiate_Formal_Subprogram;
10143 ------------------------
10144 -- Instantiate_Object --
10145 ------------------------
10147 function Instantiate_Object
10150 Analyzed_Formal : Node_Id) return List_Id
10152 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10153 A_Gen_Obj : constant Entity_Id :=
10154 Defining_Identifier (Analyzed_Formal);
10155 Acc_Def : Node_Id := Empty;
10156 Act_Assoc : constant Node_Id := Parent (Actual);
10157 Actual_Decl : Node_Id := Empty;
10158 Decl_Node : Node_Id;
10161 List : constant List_Id := New_List;
10162 Loc : constant Source_Ptr := Sloc (Actual);
10163 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10164 Subt_Decl : Node_Id := Empty;
10165 Subt_Mark : Node_Id := Empty;
10167 function Copy_Access_Def return Node_Id;
10168 -- If formal is an anonymous access, copy access definition of formal
10169 -- for generated object declaration.
10171 ---------------------
10172 -- Copy_Access_Def --
10173 ---------------------
10175 function Copy_Access_Def return Node_Id is
10177 Def := New_Copy_Tree (Acc_Def);
10179 -- In addition, if formal is an access to subprogram we need to
10180 -- generate new formals for the signature of the default, so that
10181 -- the tree is properly formatted for ASIS use.
10183 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10185 Par_Spec : Node_Id;
10188 First (Parameter_Specifications
10189 (Access_To_Subprogram_Definition (Def)));
10190 while Present (Par_Spec) loop
10191 Set_Defining_Identifier (Par_Spec,
10192 Make_Defining_Identifier (Sloc (Acc_Def),
10193 Chars => Chars (Defining_Identifier (Par_Spec))));
10200 end Copy_Access_Def;
10202 -- Start of processing for Instantiate_Object
10205 -- Formal may be an anonymous access
10207 if Present (Subtype_Mark (Formal)) then
10208 Subt_Mark := Subtype_Mark (Formal);
10210 Check_Access_Definition (Formal);
10211 Acc_Def := Access_Definition (Formal);
10214 -- Sloc for error message on missing actual
10216 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10218 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10219 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10222 Set_Parent (List, Parent (Actual));
10226 if Out_Present (Formal) then
10228 -- An IN OUT generic actual must be a name. The instantiation is a
10229 -- renaming declaration. The actual is the name being renamed. We
10230 -- use the actual directly, rather than a copy, because it is not
10231 -- used further in the list of actuals, and because a copy or a use
10232 -- of relocate_node is incorrect if the instance is nested within a
10233 -- generic. In order to simplify ASIS searches, the Generic_Parent
10234 -- field links the declaration to the generic association.
10236 if No (Actual) then
10238 ("missing actual &",
10239 Instantiation_Node, Gen_Obj);
10241 ("\in instantiation of & declared#",
10242 Instantiation_Node, Scope (A_Gen_Obj));
10243 Abandon_Instantiation (Instantiation_Node);
10246 if Present (Subt_Mark) then
10248 Make_Object_Renaming_Declaration (Loc,
10249 Defining_Identifier => New_Copy (Gen_Obj),
10250 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10253 else pragma Assert (Present (Acc_Def));
10255 Make_Object_Renaming_Declaration (Loc,
10256 Defining_Identifier => New_Copy (Gen_Obj),
10257 Access_Definition => New_Copy_Tree (Acc_Def),
10261 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10263 -- The analysis of the actual may produce Insert_Action nodes, so
10264 -- the declaration must have a context in which to attach them.
10266 Append (Decl_Node, List);
10269 -- Return if the analysis of the actual reported some error
10271 if Etype (Actual) = Any_Type then
10275 -- This check is performed here because Analyze_Object_Renaming will
10276 -- not check it when Comes_From_Source is False. Note though that the
10277 -- check for the actual being the name of an object will be performed
10278 -- in Analyze_Object_Renaming.
10280 if Is_Object_Reference (Actual)
10281 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10284 ("illegal discriminant-dependent component for in out parameter",
10288 -- The actual has to be resolved in order to check that it is a
10289 -- variable (due to cases such as F (1), where F returns access to
10290 -- an array, and for overloaded prefixes).
10292 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10294 -- If the type of the formal is not itself a formal, and the current
10295 -- unit is a child unit, the formal type must be declared in a
10296 -- parent, and must be retrieved by visibility.
10298 if Ftyp = Orig_Ftyp
10299 and then Is_Generic_Unit (Scope (Ftyp))
10300 and then Is_Child_Unit (Scope (A_Gen_Obj))
10303 Temp : constant Node_Id :=
10304 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10306 Set_Entity (Temp, Empty);
10308 Ftyp := Entity (Temp);
10312 if Is_Private_Type (Ftyp)
10313 and then not Is_Private_Type (Etype (Actual))
10314 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10315 or else Base_Type (Etype (Actual)) = Ftyp)
10317 -- If the actual has the type of the full view of the formal, or
10318 -- else a non-private subtype of the formal, then the visibility
10319 -- of the formal type has changed. Add to the actuals a subtype
10320 -- declaration that will force the exchange of views in the body
10321 -- of the instance as well.
10324 Make_Subtype_Declaration (Loc,
10325 Defining_Identifier => Make_Temporary (Loc, 'P'),
10326 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10328 Prepend (Subt_Decl, List);
10330 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10331 Exchange_Declarations (Ftyp);
10334 Resolve (Actual, Ftyp);
10336 if not Denotes_Variable (Actual) then
10337 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10339 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10341 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10342 -- the type of the actual shall resolve to a specific anonymous
10345 if Ada_Version < Ada_2005
10346 or else Ekind (Base_Type (Ftyp)) /=
10347 E_Anonymous_Access_Type
10348 or else Ekind (Base_Type (Etype (Actual))) /=
10349 E_Anonymous_Access_Type
10352 ("type of actual does not match type of&", Actual, Gen_Obj);
10356 Note_Possible_Modification (Actual, Sure => True);
10358 -- Check for instantiation of atomic/volatile actual for
10359 -- non-atomic/volatile formal (RM C.6 (12)).
10361 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10363 ("cannot instantiate non-atomic formal object "
10364 & "with atomic actual", Actual);
10366 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10369 ("cannot instantiate non-volatile formal object "
10370 & "with volatile actual", Actual);
10373 -- Formal in-parameter
10376 -- The instantiation of a generic formal in-parameter is constant
10377 -- declaration. The actual is the expression for that declaration.
10378 -- Its type is a full copy of the type of the formal. This may be
10379 -- an access to subprogram, for which we need to generate entities
10380 -- for the formals in the new signature.
10382 if Present (Actual) then
10383 if Present (Subt_Mark) then
10384 Def := New_Copy_Tree (Subt_Mark);
10385 else pragma Assert (Present (Acc_Def));
10386 Def := Copy_Access_Def;
10390 Make_Object_Declaration (Loc,
10391 Defining_Identifier => New_Copy (Gen_Obj),
10392 Constant_Present => True,
10393 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10394 Object_Definition => Def,
10395 Expression => Actual);
10397 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10399 -- A generic formal object of a tagged type is defined to be
10400 -- aliased so the new constant must also be treated as aliased.
10402 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10403 Set_Aliased_Present (Decl_Node);
10406 Append (Decl_Node, List);
10408 -- No need to repeat (pre-)analysis of some expression nodes
10409 -- already handled in Preanalyze_Actuals.
10411 if Nkind (Actual) /= N_Allocator then
10414 -- Return if the analysis of the actual reported some error
10416 if Etype (Actual) = Any_Type then
10422 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10426 Typ := Get_Instance_Of (Formal_Type);
10428 -- If the actual appears in the current or an enclosing scope,
10429 -- use its type directly. This is relevant if it has an actual
10430 -- subtype that is distinct from its nominal one. This cannot
10431 -- be done in general because the type of the actual may
10432 -- depend on other actuals, and only be fully determined when
10433 -- the enclosing instance is analyzed.
10435 if Present (Etype (Actual))
10436 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10438 Freeze_Before (Instantiation_Node, Etype (Actual));
10440 Freeze_Before (Instantiation_Node, Typ);
10443 -- If the actual is an aggregate, perform name resolution on
10444 -- its components (the analysis of an aggregate does not do it)
10445 -- to capture local names that may be hidden if the generic is
10448 if Nkind (Actual) = N_Aggregate then
10449 Preanalyze_And_Resolve (Actual, Typ);
10452 if Is_Limited_Type (Typ)
10453 and then not OK_For_Limited_Init (Typ, Actual)
10456 ("initialization not allowed for limited types", Actual);
10457 Explain_Limited_Type (Typ, Actual);
10461 elsif Present (Default_Expression (Formal)) then
10463 -- Use default to construct declaration
10465 if Present (Subt_Mark) then
10466 Def := New_Copy (Subt_Mark);
10467 else pragma Assert (Present (Acc_Def));
10468 Def := Copy_Access_Def;
10472 Make_Object_Declaration (Sloc (Formal),
10473 Defining_Identifier => New_Copy (Gen_Obj),
10474 Constant_Present => True,
10475 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10476 Object_Definition => Def,
10477 Expression => New_Copy_Tree
10478 (Default_Expression (Formal)));
10480 Append (Decl_Node, List);
10481 Set_Analyzed (Expression (Decl_Node), False);
10484 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10485 Error_Msg_NE ("\in instantiation of & declared#",
10486 Instantiation_Node, Scope (A_Gen_Obj));
10488 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10490 -- Create dummy constant declaration so that instance can be
10491 -- analyzed, to minimize cascaded visibility errors.
10493 if Present (Subt_Mark) then
10495 else pragma Assert (Present (Acc_Def));
10500 Make_Object_Declaration (Loc,
10501 Defining_Identifier => New_Copy (Gen_Obj),
10502 Constant_Present => True,
10503 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10504 Object_Definition => New_Copy (Def),
10506 Make_Attribute_Reference (Sloc (Gen_Obj),
10507 Attribute_Name => Name_First,
10508 Prefix => New_Copy (Def)));
10510 Append (Decl_Node, List);
10513 Abandon_Instantiation (Instantiation_Node);
10518 if Nkind (Actual) in N_Has_Entity then
10519 Actual_Decl := Parent (Entity (Actual));
10522 -- Ada 2005 (AI-423): For a formal object declaration with a null
10523 -- exclusion or an access definition that has a null exclusion: If the
10524 -- actual matching the formal object declaration denotes a generic
10525 -- formal object of another generic unit G, and the instantiation
10526 -- containing the actual occurs within the body of G or within the body
10527 -- of a generic unit declared within the declarative region of G, then
10528 -- the declaration of the formal object of G must have a null exclusion.
10529 -- Otherwise, the subtype of the actual matching the formal object
10530 -- declaration shall exclude null.
10532 if Ada_Version >= Ada_2005
10533 and then Present (Actual_Decl)
10534 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10535 N_Object_Declaration)
10536 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10537 and then not Has_Null_Exclusion (Actual_Decl)
10538 and then Has_Null_Exclusion (Analyzed_Formal)
10540 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10542 ("actual must exclude null to match generic formal#", Actual);
10545 -- An effectively volatile object cannot be used as an actual in
10546 -- a generic instance. The following check is only relevant when
10547 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10550 and then Present (Actual)
10551 and then Is_Effectively_Volatile_Object (Actual)
10554 ("volatile object cannot act as actual in generic instantiation "
10555 & "(SPARK RM 7.1.3(8))", Actual);
10559 end Instantiate_Object;
10561 ------------------------------
10562 -- Instantiate_Package_Body --
10563 ------------------------------
10565 procedure Instantiate_Package_Body
10566 (Body_Info : Pending_Body_Info;
10567 Inlined_Body : Boolean := False;
10568 Body_Optional : Boolean := False)
10570 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10571 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10572 Loc : constant Source_Ptr := Sloc (Inst_Node);
10574 Gen_Id : constant Node_Id := Name (Inst_Node);
10575 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10576 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10577 Act_Spec : constant Node_Id := Specification (Act_Decl);
10578 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10580 Act_Body_Name : Node_Id;
10581 Gen_Body : Node_Id;
10582 Gen_Body_Id : Node_Id;
10583 Act_Body : Node_Id;
10584 Act_Body_Id : Entity_Id;
10586 Parent_Installed : Boolean := False;
10587 Save_Style_Check : constant Boolean := Style_Check;
10589 Par_Ent : Entity_Id := Empty;
10590 Par_Vis : Boolean := False;
10592 Vis_Prims_List : Elist_Id := No_Elist;
10593 -- List of primitives made temporarily visible in the instantiation
10594 -- to match the visibility of the formal type
10596 procedure Check_Initialized_Types;
10597 -- In a generic package body, an entity of a generic private type may
10598 -- appear uninitialized. This is suspicious, unless the actual is a
10599 -- fully initialized type.
10601 -----------------------------
10602 -- Check_Initialized_Types --
10603 -----------------------------
10605 procedure Check_Initialized_Types is
10607 Formal : Entity_Id;
10608 Actual : Entity_Id;
10609 Uninit_Var : Entity_Id;
10612 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10613 while Present (Decl) loop
10614 Uninit_Var := Empty;
10616 if Nkind (Decl) = N_Private_Extension_Declaration then
10617 Uninit_Var := Uninitialized_Variable (Decl);
10619 elsif Nkind (Decl) = N_Formal_Type_Declaration
10620 and then Nkind (Formal_Type_Definition (Decl)) =
10621 N_Formal_Private_Type_Definition
10624 Uninitialized_Variable (Formal_Type_Definition (Decl));
10627 if Present (Uninit_Var) then
10628 Formal := Defining_Identifier (Decl);
10629 Actual := First_Entity (Act_Decl_Id);
10631 -- For each formal there is a subtype declaration that renames
10632 -- the actual and has the same name as the formal. Locate the
10633 -- formal for warning message about uninitialized variables
10634 -- in the generic, for which the actual type should be a fully
10635 -- initialized type.
10637 while Present (Actual) loop
10638 exit when Ekind (Actual) = E_Package
10639 and then Present (Renamed_Object (Actual));
10641 if Chars (Actual) = Chars (Formal)
10642 and then not Is_Scalar_Type (Actual)
10643 and then not Is_Fully_Initialized_Type (Actual)
10644 and then Warn_On_No_Value_Assigned
10646 Error_Msg_Node_2 := Formal;
10648 ("generic unit has uninitialized variable& of "
10649 & "formal private type &?v?", Actual, Uninit_Var);
10651 ("actual type for& should be fully initialized type?v?",
10656 Next_Entity (Actual);
10662 end Check_Initialized_Types;
10664 -- Start of processing for Instantiate_Package_Body
10667 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10669 -- The instance body may already have been processed, as the parent of
10670 -- another instance that is inlined (Load_Parent_Of_Generic).
10672 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10676 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10678 -- Re-establish the state of information on which checks are suppressed.
10679 -- This information was set in Body_Info at the point of instantiation,
10680 -- and now we restore it so that the instance is compiled using the
10681 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10683 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10684 Scope_Suppress := Body_Info.Scope_Suppress;
10685 Opt.Ada_Version := Body_Info.Version;
10686 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10687 Restore_Warnings (Body_Info.Warnings);
10688 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10689 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10691 if No (Gen_Body_Id) then
10693 -- Do not look for parent of generic body if none is required.
10694 -- This may happen when the routine is called as part of the
10695 -- Pending_Instantiations processing, when nested instances
10696 -- may precede the one generated from the main unit.
10698 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10699 and then Body_Optional
10703 Load_Parent_Of_Generic
10704 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10705 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10709 -- Establish global variable for sloc adjustment and for error recovery
10711 Instantiation_Node := Inst_Node;
10713 if Present (Gen_Body_Id) then
10714 Save_Env (Gen_Unit, Act_Decl_Id);
10715 Style_Check := False;
10716 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10718 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10720 Create_Instantiation_Source
10721 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10725 (Original_Node (Gen_Body), Empty, Instantiating => True);
10727 -- Build new name (possibly qualified) for body declaration
10729 Act_Body_Id := New_Copy (Act_Decl_Id);
10731 -- Some attributes of spec entity are not inherited by body entity
10733 Set_Handler_Records (Act_Body_Id, No_List);
10735 if Nkind (Defining_Unit_Name (Act_Spec)) =
10736 N_Defining_Program_Unit_Name
10739 Make_Defining_Program_Unit_Name (Loc,
10740 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10741 Defining_Identifier => Act_Body_Id);
10743 Act_Body_Name := Act_Body_Id;
10746 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10748 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10749 Check_Generic_Actuals (Act_Decl_Id, False);
10750 Check_Initialized_Types;
10752 -- Install primitives hidden at the point of the instantiation but
10753 -- visible when processing the generic formals
10759 E := First_Entity (Act_Decl_Id);
10760 while Present (E) loop
10762 and then Is_Generic_Actual_Type (E)
10763 and then Is_Tagged_Type (E)
10765 Install_Hidden_Primitives
10766 (Prims_List => Vis_Prims_List,
10767 Gen_T => Generic_Parent_Type (Parent (E)),
10775 -- If it is a child unit, make the parent instance (which is an
10776 -- instance of the parent of the generic) visible. The parent
10777 -- instance is the prefix of the name of the generic unit.
10779 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10780 and then Nkind (Gen_Id) = N_Expanded_Name
10782 Par_Ent := Entity (Prefix (Gen_Id));
10783 Par_Vis := Is_Immediately_Visible (Par_Ent);
10784 Install_Parent (Par_Ent, In_Body => True);
10785 Parent_Installed := True;
10787 elsif Is_Child_Unit (Gen_Unit) then
10788 Par_Ent := Scope (Gen_Unit);
10789 Par_Vis := Is_Immediately_Visible (Par_Ent);
10790 Install_Parent (Par_Ent, In_Body => True);
10791 Parent_Installed := True;
10794 -- If the instantiation is a library unit, and this is the main unit,
10795 -- then build the resulting compilation unit nodes for the instance.
10796 -- If this is a compilation unit but it is not the main unit, then it
10797 -- is the body of a unit in the context, that is being compiled
10798 -- because it is encloses some inlined unit or another generic unit
10799 -- being instantiated. In that case, this body is not part of the
10800 -- current compilation, and is not attached to the tree, but its
10801 -- parent must be set for analysis.
10803 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10805 -- Replace instance node with body of instance, and create new
10806 -- node for corresponding instance declaration.
10808 Build_Instance_Compilation_Unit_Nodes
10809 (Inst_Node, Act_Body, Act_Decl);
10810 Analyze (Inst_Node);
10812 if Parent (Inst_Node) = Cunit (Main_Unit) then
10814 -- If the instance is a child unit itself, then set the scope
10815 -- of the expanded body to be the parent of the instantiation
10816 -- (ensuring that the fully qualified name will be generated
10817 -- for the elaboration subprogram).
10819 if Nkind (Defining_Unit_Name (Act_Spec)) =
10820 N_Defining_Program_Unit_Name
10822 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10826 -- Case where instantiation is not a library unit
10829 -- If this is an early instantiation, i.e. appears textually
10830 -- before the corresponding body and must be elaborated first,
10831 -- indicate that the body instance is to be delayed.
10833 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10835 -- Now analyze the body. We turn off all checks if this is an
10836 -- internal unit, since there is no reason to have checks on for
10837 -- any predefined run-time library code. All such code is designed
10838 -- to be compiled with checks off.
10840 -- Note that we do NOT apply this criterion to children of GNAT
10841 -- The latter units must suppress checks explicitly if needed.
10843 if Is_Predefined_File_Name
10844 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10846 Analyze (Act_Body, Suppress => All_Checks);
10848 Analyze (Act_Body);
10852 Inherit_Context (Gen_Body, Inst_Node);
10854 -- Remove the parent instances if they have been placed on the scope
10855 -- stack to compile the body.
10857 if Parent_Installed then
10858 Remove_Parent (In_Body => True);
10860 -- Restore the previous visibility of the parent
10862 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10865 Restore_Hidden_Primitives (Vis_Prims_List);
10866 Restore_Private_Views (Act_Decl_Id);
10868 -- Remove the current unit from visibility if this is an instance
10869 -- that is not elaborated on the fly for inlining purposes.
10871 if not Inlined_Body then
10872 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10876 Style_Check := Save_Style_Check;
10878 -- If we have no body, and the unit requires a body, then complain. This
10879 -- complaint is suppressed if we have detected other errors (since a
10880 -- common reason for missing the body is that it had errors).
10881 -- In CodePeer mode, a warning has been emitted already, no need for
10882 -- further messages.
10884 elsif Unit_Requires_Body (Gen_Unit)
10885 and then not Body_Optional
10887 if CodePeer_Mode then
10890 elsif Serious_Errors_Detected = 0 then
10892 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10894 -- Don't attempt to perform any cleanup actions if some other error
10895 -- was already detected, since this can cause blowups.
10901 -- Case of package that does not need a body
10904 -- If the instantiation of the declaration is a library unit, rewrite
10905 -- the original package instantiation as a package declaration in the
10906 -- compilation unit node.
10908 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10909 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10910 Rewrite (Inst_Node, Act_Decl);
10912 -- Generate elaboration entity, in case spec has elaboration code.
10913 -- This cannot be done when the instance is analyzed, because it
10914 -- is not known yet whether the body exists.
10916 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10917 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10919 -- If the instantiation is not a library unit, then append the
10920 -- declaration to the list of implicitly generated entities, unless
10921 -- it is already a list member which means that it was already
10924 elsif not Is_List_Member (Act_Decl) then
10925 Mark_Rewrite_Insertion (Act_Decl);
10926 Insert_Before (Inst_Node, Act_Decl);
10930 Expander_Mode_Restore;
10931 end Instantiate_Package_Body;
10933 ---------------------------------
10934 -- Instantiate_Subprogram_Body --
10935 ---------------------------------
10937 procedure Instantiate_Subprogram_Body
10938 (Body_Info : Pending_Body_Info;
10939 Body_Optional : Boolean := False)
10941 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10942 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10943 Loc : constant Source_Ptr := Sloc (Inst_Node);
10944 Gen_Id : constant Node_Id := Name (Inst_Node);
10945 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10946 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10947 Anon_Id : constant Entity_Id :=
10948 Defining_Unit_Name (Specification (Act_Decl));
10949 Pack_Id : constant Entity_Id :=
10950 Defining_Unit_Name (Parent (Act_Decl));
10952 Saved_Style_Check : constant Boolean := Style_Check;
10953 Saved_Warnings : constant Warning_Record := Save_Warnings;
10955 Act_Body : Node_Id;
10956 Gen_Body : Node_Id;
10957 Gen_Body_Id : Node_Id;
10958 Pack_Body : Node_Id;
10959 Par_Ent : Entity_Id := Empty;
10960 Par_Vis : Boolean := False;
10961 Ret_Expr : Node_Id;
10963 Parent_Installed : Boolean := False;
10966 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10968 -- Subprogram body may have been created already because of an inline
10969 -- pragma, or because of multiple elaborations of the enclosing package
10970 -- when several instances of the subprogram appear in the main unit.
10972 if Present (Corresponding_Body (Act_Decl)) then
10976 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10978 -- Re-establish the state of information on which checks are suppressed.
10979 -- This information was set in Body_Info at the point of instantiation,
10980 -- and now we restore it so that the instance is compiled using the
10981 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10983 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10984 Scope_Suppress := Body_Info.Scope_Suppress;
10985 Opt.Ada_Version := Body_Info.Version;
10986 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10987 Restore_Warnings (Body_Info.Warnings);
10988 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10989 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10991 if No (Gen_Body_Id) then
10993 -- For imported generic subprogram, no body to compile, complete
10994 -- the spec entity appropriately.
10996 if Is_Imported (Gen_Unit) then
10997 Set_Is_Imported (Anon_Id);
10998 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10999 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11000 Set_Convention (Anon_Id, Convention (Gen_Unit));
11001 Set_Has_Completion (Anon_Id);
11004 -- For other cases, compile the body
11007 Load_Parent_Of_Generic
11008 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11009 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11013 Instantiation_Node := Inst_Node;
11015 if Present (Gen_Body_Id) then
11016 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11018 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11020 -- Either body is not present, or context is non-expanding, as
11021 -- when compiling a subunit. Mark the instance as completed, and
11022 -- diagnose a missing body when needed.
11025 and then Operating_Mode = Generate_Code
11028 ("missing proper body for instantiation", Gen_Body);
11031 Set_Has_Completion (Anon_Id);
11035 Save_Env (Gen_Unit, Anon_Id);
11036 Style_Check := False;
11037 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11038 Create_Instantiation_Source
11046 (Original_Node (Gen_Body), Empty, Instantiating => True);
11048 -- Create proper defining name for the body, to correspond to
11049 -- the one in the spec.
11051 Set_Defining_Unit_Name (Specification (Act_Body),
11052 Make_Defining_Identifier
11053 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11054 Set_Corresponding_Spec (Act_Body, Anon_Id);
11055 Set_Has_Completion (Anon_Id);
11056 Check_Generic_Actuals (Pack_Id, False);
11058 -- Generate a reference to link the visible subprogram instance to
11059 -- the generic body, which for navigation purposes is the only
11060 -- available source for the instance.
11063 (Related_Instance (Pack_Id),
11064 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11066 -- If it is a child unit, make the parent instance (which is an
11067 -- instance of the parent of the generic) visible. The parent
11068 -- instance is the prefix of the name of the generic unit.
11070 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11071 and then Nkind (Gen_Id) = N_Expanded_Name
11073 Par_Ent := Entity (Prefix (Gen_Id));
11074 Par_Vis := Is_Immediately_Visible (Par_Ent);
11075 Install_Parent (Par_Ent, In_Body => True);
11076 Parent_Installed := True;
11078 elsif Is_Child_Unit (Gen_Unit) then
11079 Par_Ent := Scope (Gen_Unit);
11080 Par_Vis := Is_Immediately_Visible (Par_Ent);
11081 Install_Parent (Par_Ent, In_Body => True);
11082 Parent_Installed := True;
11085 -- Subprogram body is placed in the body of wrapper package,
11086 -- whose spec contains the subprogram declaration as well as
11087 -- the renaming declarations for the generic parameters.
11090 Make_Package_Body (Loc,
11091 Defining_Unit_Name => New_Copy (Pack_Id),
11092 Declarations => New_List (Act_Body));
11094 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11096 -- If the instantiation is a library unit, then build resulting
11097 -- compilation unit nodes for the instance. The declaration of
11098 -- the enclosing package is the grandparent of the subprogram
11099 -- declaration. First replace the instantiation node as the unit
11100 -- of the corresponding compilation.
11102 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11103 if Parent (Inst_Node) = Cunit (Main_Unit) then
11104 Set_Unit (Parent (Inst_Node), Inst_Node);
11105 Build_Instance_Compilation_Unit_Nodes
11106 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11107 Analyze (Inst_Node);
11109 Set_Parent (Pack_Body, Parent (Inst_Node));
11110 Analyze (Pack_Body);
11114 Insert_Before (Inst_Node, Pack_Body);
11115 Mark_Rewrite_Insertion (Pack_Body);
11116 Analyze (Pack_Body);
11118 if Expander_Active then
11119 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11123 Inherit_Context (Gen_Body, Inst_Node);
11125 Restore_Private_Views (Pack_Id, False);
11127 if Parent_Installed then
11128 Remove_Parent (In_Body => True);
11130 -- Restore the previous visibility of the parent
11132 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11136 Style_Check := Saved_Style_Check;
11137 Restore_Warnings (Saved_Warnings);
11139 -- Body not found. Error was emitted already. If there were no previous
11140 -- errors, this may be an instance whose scope is a premature instance.
11141 -- In that case we must insure that the (legal) program does raise
11142 -- program error if executed. We generate a subprogram body for this
11143 -- purpose. See DEC ac30vso.
11145 -- Should not reference proprietary DEC tests in comments ???
11147 elsif Serious_Errors_Detected = 0
11148 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11150 if Body_Optional then
11153 elsif Ekind (Anon_Id) = E_Procedure then
11155 Make_Subprogram_Body (Loc,
11157 Make_Procedure_Specification (Loc,
11158 Defining_Unit_Name =>
11159 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11160 Parameter_Specifications =>
11162 (Parameter_Specifications (Parent (Anon_Id)))),
11164 Declarations => Empty_List,
11165 Handled_Statement_Sequence =>
11166 Make_Handled_Sequence_Of_Statements (Loc,
11169 Make_Raise_Program_Error (Loc,
11171 PE_Access_Before_Elaboration))));
11175 Make_Raise_Program_Error (Loc,
11176 Reason => PE_Access_Before_Elaboration);
11178 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11179 Set_Analyzed (Ret_Expr);
11182 Make_Subprogram_Body (Loc,
11184 Make_Function_Specification (Loc,
11185 Defining_Unit_Name =>
11186 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11187 Parameter_Specifications =>
11189 (Parameter_Specifications (Parent (Anon_Id))),
11190 Result_Definition =>
11191 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11193 Declarations => Empty_List,
11194 Handled_Statement_Sequence =>
11195 Make_Handled_Sequence_Of_Statements (Loc,
11198 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11201 Pack_Body := Make_Package_Body (Loc,
11202 Defining_Unit_Name => New_Copy (Pack_Id),
11203 Declarations => New_List (Act_Body));
11205 Insert_After (Inst_Node, Pack_Body);
11206 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11207 Analyze (Pack_Body);
11210 Expander_Mode_Restore;
11211 end Instantiate_Subprogram_Body;
11213 ----------------------
11214 -- Instantiate_Type --
11215 ----------------------
11217 function Instantiate_Type
11220 Analyzed_Formal : Node_Id;
11221 Actual_Decls : List_Id) return List_Id
11223 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11224 A_Gen_T : constant Entity_Id :=
11225 Defining_Identifier (Analyzed_Formal);
11226 Ancestor : Entity_Id := Empty;
11227 Def : constant Node_Id := Formal_Type_Definition (Formal);
11229 Decl_Node : Node_Id;
11230 Decl_Nodes : List_Id;
11234 procedure Diagnose_Predicated_Actual;
11235 -- There are a number of constructs in which a discrete type with
11236 -- predicates is illegal, e.g. as an index in an array type declaration.
11237 -- If a generic type is used is such a construct in a generic package
11238 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11239 -- of the generic contract that the actual cannot have predicates.
11241 procedure Validate_Array_Type_Instance;
11242 procedure Validate_Access_Subprogram_Instance;
11243 procedure Validate_Access_Type_Instance;
11244 procedure Validate_Derived_Type_Instance;
11245 procedure Validate_Derived_Interface_Type_Instance;
11246 procedure Validate_Discriminated_Formal_Type;
11247 procedure Validate_Interface_Type_Instance;
11248 procedure Validate_Private_Type_Instance;
11249 procedure Validate_Incomplete_Type_Instance;
11250 -- These procedures perform validation tests for the named case.
11251 -- Validate_Discriminated_Formal_Type is shared by formal private
11252 -- types and Ada 2012 formal incomplete types.
11254 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11255 -- Check that base types are the same and that the subtypes match
11256 -- statically. Used in several of the above.
11258 ---------------------------------
11259 -- Diagnose_Predicated_Actual --
11260 ---------------------------------
11262 procedure Diagnose_Predicated_Actual is
11264 if No_Predicate_On_Actual (A_Gen_T)
11265 and then Has_Predicates (Act_T)
11268 ("actual for& cannot be a type with predicate",
11269 Instantiation_Node, A_Gen_T);
11271 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11272 and then Has_Predicates (Act_T)
11273 and then not Has_Static_Predicate_Aspect (Act_T)
11276 ("actual for& cannot be a type with a dynamic predicate",
11277 Instantiation_Node, A_Gen_T);
11279 end Diagnose_Predicated_Actual;
11281 --------------------
11282 -- Subtypes_Match --
11283 --------------------
11285 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11286 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11289 -- Some detailed comments would be useful here ???
11291 return ((Base_Type (T) = Act_T
11292 or else Base_Type (T) = Base_Type (Act_T))
11293 and then Subtypes_Statically_Match (T, Act_T))
11295 or else (Is_Class_Wide_Type (Gen_T)
11296 and then Is_Class_Wide_Type (Act_T)
11297 and then Subtypes_Match
11298 (Get_Instance_Of (Root_Type (Gen_T)),
11299 Root_Type (Act_T)))
11302 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11303 E_Anonymous_Access_Type)
11304 and then Ekind (Act_T) = Ekind (Gen_T)
11305 and then Subtypes_Statically_Match
11306 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11307 end Subtypes_Match;
11309 -----------------------------------------
11310 -- Validate_Access_Subprogram_Instance --
11311 -----------------------------------------
11313 procedure Validate_Access_Subprogram_Instance is
11315 if not Is_Access_Type (Act_T)
11316 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11319 ("expect access type in instantiation of &", Actual, Gen_T);
11320 Abandon_Instantiation (Actual);
11323 -- According to AI05-288, actuals for access_to_subprograms must be
11324 -- subtype conformant with the generic formal. Previous to AI05-288
11325 -- only mode conformance was required.
11327 -- This is a binding interpretation that applies to previous versions
11328 -- of the language, no need to maintain previous weaker checks.
11330 Check_Subtype_Conformant
11331 (Designated_Type (Act_T),
11332 Designated_Type (A_Gen_T),
11336 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11337 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11339 ("protected access type not allowed for formal &",
11343 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11345 ("expect protected access type for formal &",
11349 -- If the formal has a specified convention (which in most cases
11350 -- will be StdCall) verify that the actual has the same convention.
11352 if Has_Convention_Pragma (A_Gen_T)
11353 and then Convention (A_Gen_T) /= Convention (Act_T)
11355 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11357 ("actual for formal & must have convention %", Actual, Gen_T);
11359 end Validate_Access_Subprogram_Instance;
11361 -----------------------------------
11362 -- Validate_Access_Type_Instance --
11363 -----------------------------------
11365 procedure Validate_Access_Type_Instance is
11366 Desig_Type : constant Entity_Id :=
11367 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11368 Desig_Act : Entity_Id;
11371 if not Is_Access_Type (Act_T) then
11373 ("expect access type in instantiation of &", Actual, Gen_T);
11374 Abandon_Instantiation (Actual);
11377 if Is_Access_Constant (A_Gen_T) then
11378 if not Is_Access_Constant (Act_T) then
11380 ("actual type must be access-to-constant type", Actual);
11381 Abandon_Instantiation (Actual);
11384 if Is_Access_Constant (Act_T) then
11386 ("actual type must be access-to-variable type", Actual);
11387 Abandon_Instantiation (Actual);
11389 elsif Ekind (A_Gen_T) = E_General_Access_Type
11390 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11392 Error_Msg_N -- CODEFIX
11393 ("actual must be general access type!", Actual);
11394 Error_Msg_NE -- CODEFIX
11395 ("add ALL to }!", Actual, Act_T);
11396 Abandon_Instantiation (Actual);
11400 -- The designated subtypes, that is to say the subtypes introduced
11401 -- by an access type declaration (and not by a subtype declaration)
11404 Desig_Act := Designated_Type (Base_Type (Act_T));
11406 -- The designated type may have been introduced through a limited_
11407 -- with clause, in which case retrieve the non-limited view. This
11408 -- applies to incomplete types as well as to class-wide types.
11410 if From_Limited_With (Desig_Act) then
11411 Desig_Act := Available_View (Desig_Act);
11414 if not Subtypes_Match (Desig_Type, Desig_Act) then
11416 ("designated type of actual does not match that of formal &",
11419 if not Predicates_Match (Desig_Type, Desig_Act) then
11420 Error_Msg_N ("\predicates do not match", Actual);
11423 Abandon_Instantiation (Actual);
11425 elsif Is_Access_Type (Designated_Type (Act_T))
11426 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11428 Is_Constrained (Designated_Type (Desig_Type))
11431 ("designated type of actual does not match that of formal &",
11434 if not Predicates_Match (Desig_Type, Desig_Act) then
11435 Error_Msg_N ("\predicates do not match", Actual);
11438 Abandon_Instantiation (Actual);
11441 -- Ada 2005: null-exclusion indicators of the two types must agree
11443 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11445 ("non null exclusion of actual and formal & do not match",
11448 end Validate_Access_Type_Instance;
11450 ----------------------------------
11451 -- Validate_Array_Type_Instance --
11452 ----------------------------------
11454 procedure Validate_Array_Type_Instance is
11459 function Formal_Dimensions return Int;
11460 -- Count number of dimensions in array type formal
11462 -----------------------
11463 -- Formal_Dimensions --
11464 -----------------------
11466 function Formal_Dimensions return Int is
11471 if Nkind (Def) = N_Constrained_Array_Definition then
11472 Index := First (Discrete_Subtype_Definitions (Def));
11474 Index := First (Subtype_Marks (Def));
11477 while Present (Index) loop
11479 Next_Index (Index);
11483 end Formal_Dimensions;
11485 -- Start of processing for Validate_Array_Type_Instance
11488 if not Is_Array_Type (Act_T) then
11490 ("expect array type in instantiation of &", Actual, Gen_T);
11491 Abandon_Instantiation (Actual);
11493 elsif Nkind (Def) = N_Constrained_Array_Definition then
11494 if not (Is_Constrained (Act_T)) then
11496 ("expect constrained array in instantiation of &",
11498 Abandon_Instantiation (Actual);
11502 if Is_Constrained (Act_T) then
11504 ("expect unconstrained array in instantiation of &",
11506 Abandon_Instantiation (Actual);
11510 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11512 ("dimensions of actual do not match formal &", Actual, Gen_T);
11513 Abandon_Instantiation (Actual);
11516 I1 := First_Index (A_Gen_T);
11517 I2 := First_Index (Act_T);
11518 for J in 1 .. Formal_Dimensions loop
11520 -- If the indexes of the actual were given by a subtype_mark,
11521 -- the index was transformed into a range attribute. Retrieve
11522 -- the original type mark for checking.
11524 if Is_Entity_Name (Original_Node (I2)) then
11525 T2 := Entity (Original_Node (I2));
11530 if not Subtypes_Match
11531 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11534 ("index types of actual do not match those of formal &",
11536 Abandon_Instantiation (Actual);
11543 -- Check matching subtypes. Note that there are complex visibility
11544 -- issues when the generic is a child unit and some aspect of the
11545 -- generic type is declared in a parent unit of the generic. We do
11546 -- the test to handle this special case only after a direct check
11547 -- for static matching has failed. The case where both the component
11548 -- type and the array type are separate formals, and the component
11549 -- type is a private view may also require special checking in
11553 (Component_Type (A_Gen_T), Component_Type (Act_T))
11556 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11557 Component_Type (Act_T))
11562 ("component subtype of actual does not match that of formal &",
11564 Abandon_Instantiation (Actual);
11567 if Has_Aliased_Components (A_Gen_T)
11568 and then not Has_Aliased_Components (Act_T)
11571 ("actual must have aliased components to match formal type &",
11574 end Validate_Array_Type_Instance;
11576 -----------------------------------------------
11577 -- Validate_Derived_Interface_Type_Instance --
11578 -----------------------------------------------
11580 procedure Validate_Derived_Interface_Type_Instance is
11581 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11585 -- First apply interface instance checks
11587 Validate_Interface_Type_Instance;
11589 -- Verify that immediate parent interface is an ancestor of
11593 and then not Interface_Present_In_Ancestor (Act_T, Par)
11596 ("interface actual must include progenitor&", Actual, Par);
11599 -- Now verify that the actual includes all other ancestors of
11602 Elmt := First_Elmt (Interfaces (A_Gen_T));
11603 while Present (Elmt) loop
11604 if not Interface_Present_In_Ancestor
11605 (Act_T, Get_Instance_Of (Node (Elmt)))
11608 ("interface actual must include progenitor&",
11609 Actual, Node (Elmt));
11614 end Validate_Derived_Interface_Type_Instance;
11616 ------------------------------------
11617 -- Validate_Derived_Type_Instance --
11618 ------------------------------------
11620 procedure Validate_Derived_Type_Instance is
11621 Actual_Discr : Entity_Id;
11622 Ancestor_Discr : Entity_Id;
11625 -- If the parent type in the generic declaration is itself a previous
11626 -- formal type, then it is local to the generic and absent from the
11627 -- analyzed generic definition. In that case the ancestor is the
11628 -- instance of the formal (which must have been instantiated
11629 -- previously), unless the ancestor is itself a formal derived type.
11630 -- In this latter case (which is the subject of Corrigendum 8652/0038
11631 -- (AI-202) the ancestor of the formals is the ancestor of its
11632 -- parent. Otherwise, the analyzed generic carries the parent type.
11633 -- If the parent type is defined in a previous formal package, then
11634 -- the scope of that formal package is that of the generic type
11635 -- itself, and it has already been mapped into the corresponding type
11636 -- in the actual package.
11638 -- Common case: parent type defined outside of the generic
11640 if Is_Entity_Name (Subtype_Mark (Def))
11641 and then Present (Entity (Subtype_Mark (Def)))
11643 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11645 -- Check whether parent is defined in a previous formal package
11648 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11651 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11653 -- The type may be a local derivation, or a type extension of a
11654 -- previous formal, or of a formal of a parent package.
11656 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11658 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11660 -- Check whether the parent is another derived formal type in the
11661 -- same generic unit.
11663 if Etype (A_Gen_T) /= A_Gen_T
11664 and then Is_Generic_Type (Etype (A_Gen_T))
11665 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11666 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11668 -- Locate ancestor of parent from the subtype declaration
11669 -- created for the actual.
11675 Decl := First (Actual_Decls);
11676 while Present (Decl) loop
11677 if Nkind (Decl) = N_Subtype_Declaration
11678 and then Chars (Defining_Identifier (Decl)) =
11679 Chars (Etype (A_Gen_T))
11681 Ancestor := Generic_Parent_Type (Decl);
11689 pragma Assert (Present (Ancestor));
11691 -- The ancestor itself may be a previous formal that has been
11694 Ancestor := Get_Instance_Of (Ancestor);
11698 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11701 -- An unusual case: the actual is a type declared in a parent unit,
11702 -- but is not a formal type so there is no instance_of for it.
11703 -- Retrieve it by analyzing the record extension.
11705 elsif Is_Child_Unit (Scope (A_Gen_T))
11706 and then In_Open_Scopes (Scope (Act_T))
11707 and then Is_Generic_Instance (Scope (Act_T))
11709 Analyze (Subtype_Mark (Def));
11710 Ancestor := Entity (Subtype_Mark (Def));
11713 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11716 -- If the formal derived type has pragma Preelaborable_Initialization
11717 -- then the actual type must have preelaborable initialization.
11719 if Known_To_Have_Preelab_Init (A_Gen_T)
11720 and then not Has_Preelaborable_Initialization (Act_T)
11723 ("actual for & must have preelaborable initialization",
11727 -- Ada 2005 (AI-251)
11729 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11730 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11732 ("(Ada 2005) expected type implementing & in instantiation",
11736 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11738 ("expect type derived from & in instantiation",
11739 Actual, First_Subtype (Ancestor));
11740 Abandon_Instantiation (Actual);
11743 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11744 -- that the formal type declaration has been rewritten as a private
11747 if Ada_Version >= Ada_2005
11748 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11749 and then Synchronized_Present (Parent (A_Gen_T))
11751 -- The actual must be a synchronized tagged type
11753 if not Is_Tagged_Type (Act_T) then
11755 ("actual of synchronized type must be tagged", Actual);
11756 Abandon_Instantiation (Actual);
11758 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11759 and then Nkind (Type_Definition (Parent (Act_T))) =
11760 N_Derived_Type_Definition
11761 and then not Synchronized_Present
11762 (Type_Definition (Parent (Act_T)))
11765 ("actual of synchronized type must be synchronized", Actual);
11766 Abandon_Instantiation (Actual);
11770 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11771 -- removes the second instance of the phrase "or allow pass by copy".
11773 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11775 ("cannot have atomic actual type for non-atomic formal type",
11778 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11780 ("cannot have volatile actual type for non-volatile formal type",
11784 -- It should not be necessary to check for unknown discriminants on
11785 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11786 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11787 -- needs fixing. ???
11789 if not Is_Indefinite_Subtype (A_Gen_T)
11790 and then not Unknown_Discriminants_Present (Formal)
11791 and then Is_Indefinite_Subtype (Act_T)
11793 Error_Msg_N ("actual subtype must be constrained", Actual);
11794 Abandon_Instantiation (Actual);
11797 if not Unknown_Discriminants_Present (Formal) then
11798 if Is_Constrained (Ancestor) then
11799 if not Is_Constrained (Act_T) then
11800 Error_Msg_N ("actual subtype must be constrained", Actual);
11801 Abandon_Instantiation (Actual);
11804 -- Ancestor is unconstrained, Check if generic formal and actual
11805 -- agree on constrainedness. The check only applies to array types
11806 -- and discriminated types.
11808 elsif Is_Constrained (Act_T) then
11809 if Ekind (Ancestor) = E_Access_Type
11810 or else (not Is_Constrained (A_Gen_T)
11811 and then Is_Composite_Type (A_Gen_T))
11813 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11814 Abandon_Instantiation (Actual);
11817 -- A class-wide type is only allowed if the formal has unknown
11820 elsif Is_Class_Wide_Type (Act_T)
11821 and then not Has_Unknown_Discriminants (Ancestor)
11824 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11825 Abandon_Instantiation (Actual);
11827 -- Otherwise, the formal and actual must have the same number
11828 -- of discriminants and each discriminant of the actual must
11829 -- correspond to a discriminant of the formal.
11831 elsif Has_Discriminants (Act_T)
11832 and then not Has_Unknown_Discriminants (Act_T)
11833 and then Has_Discriminants (Ancestor)
11835 Actual_Discr := First_Discriminant (Act_T);
11836 Ancestor_Discr := First_Discriminant (Ancestor);
11837 while Present (Actual_Discr)
11838 and then Present (Ancestor_Discr)
11840 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11841 No (Corresponding_Discriminant (Actual_Discr))
11844 ("discriminant & does not correspond "
11845 & "to ancestor discriminant", Actual, Actual_Discr);
11846 Abandon_Instantiation (Actual);
11849 Next_Discriminant (Actual_Discr);
11850 Next_Discriminant (Ancestor_Discr);
11853 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11855 ("actual for & must have same number of discriminants",
11857 Abandon_Instantiation (Actual);
11860 -- This case should be caught by the earlier check for
11861 -- constrainedness, but the check here is added for completeness.
11863 elsif Has_Discriminants (Act_T)
11864 and then not Has_Unknown_Discriminants (Act_T)
11867 ("actual for & must not have discriminants", Actual, Gen_T);
11868 Abandon_Instantiation (Actual);
11870 elsif Has_Discriminants (Ancestor) then
11872 ("actual for & must have known discriminants", Actual, Gen_T);
11873 Abandon_Instantiation (Actual);
11876 if not Subtypes_Statically_Compatible
11877 (Act_T, Ancestor, Formal_Derived_Matching => True)
11880 ("constraint on actual is incompatible with formal", Actual);
11881 Abandon_Instantiation (Actual);
11885 -- If the formal and actual types are abstract, check that there
11886 -- are no abstract primitives of the actual type that correspond to
11887 -- nonabstract primitives of the formal type (second sentence of
11890 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11891 Check_Abstract_Primitives : declare
11892 Gen_Prims : constant Elist_Id :=
11893 Primitive_Operations (A_Gen_T);
11894 Gen_Elmt : Elmt_Id;
11895 Gen_Subp : Entity_Id;
11896 Anc_Subp : Entity_Id;
11897 Anc_Formal : Entity_Id;
11898 Anc_F_Type : Entity_Id;
11900 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11901 Act_Elmt : Elmt_Id;
11902 Act_Subp : Entity_Id;
11903 Act_Formal : Entity_Id;
11904 Act_F_Type : Entity_Id;
11906 Subprograms_Correspond : Boolean;
11908 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11909 -- Returns true if T2 is derived directly or indirectly from
11910 -- T1, including derivations from interfaces. T1 and T2 are
11911 -- required to be specific tagged base types.
11913 ------------------------
11914 -- Is_Tagged_Ancestor --
11915 ------------------------
11917 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11919 Intfc_Elmt : Elmt_Id;
11922 -- The predicate is satisfied if the types are the same
11927 -- If we've reached the top of the derivation chain then
11928 -- we know that T1 is not an ancestor of T2.
11930 elsif Etype (T2) = T2 then
11933 -- Proceed to check T2's immediate parent
11935 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11938 -- Finally, check to see if T1 is an ancestor of any of T2's
11942 Intfc_Elmt := First_Elmt (Interfaces (T2));
11943 while Present (Intfc_Elmt) loop
11944 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11948 Next_Elmt (Intfc_Elmt);
11953 end Is_Tagged_Ancestor;
11955 -- Start of processing for Check_Abstract_Primitives
11958 -- Loop over all of the formal derived type's primitives
11960 Gen_Elmt := First_Elmt (Gen_Prims);
11961 while Present (Gen_Elmt) loop
11962 Gen_Subp := Node (Gen_Elmt);
11964 -- If the primitive of the formal is not abstract, then
11965 -- determine whether there is a corresponding primitive of
11966 -- the actual type that's abstract.
11968 if not Is_Abstract_Subprogram (Gen_Subp) then
11969 Act_Elmt := First_Elmt (Act_Prims);
11970 while Present (Act_Elmt) loop
11971 Act_Subp := Node (Act_Elmt);
11973 -- If we find an abstract primitive of the actual,
11974 -- then we need to test whether it corresponds to the
11975 -- subprogram from which the generic formal primitive
11978 if Is_Abstract_Subprogram (Act_Subp) then
11979 Anc_Subp := Alias (Gen_Subp);
11981 -- Test whether we have a corresponding primitive
11982 -- by comparing names, kinds, formal types, and
11985 if Chars (Anc_Subp) = Chars (Act_Subp)
11986 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11988 Anc_Formal := First_Formal (Anc_Subp);
11989 Act_Formal := First_Formal (Act_Subp);
11990 while Present (Anc_Formal)
11991 and then Present (Act_Formal)
11993 Anc_F_Type := Etype (Anc_Formal);
11994 Act_F_Type := Etype (Act_Formal);
11996 if Ekind (Anc_F_Type) =
11997 E_Anonymous_Access_Type
11999 Anc_F_Type := Designated_Type (Anc_F_Type);
12001 if Ekind (Act_F_Type) =
12002 E_Anonymous_Access_Type
12005 Designated_Type (Act_F_Type);
12011 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12016 Anc_F_Type := Base_Type (Anc_F_Type);
12017 Act_F_Type := Base_Type (Act_F_Type);
12019 -- If the formal is controlling, then the
12020 -- the type of the actual primitive's formal
12021 -- must be derived directly or indirectly
12022 -- from the type of the ancestor primitive's
12025 if Is_Controlling_Formal (Anc_Formal) then
12026 if not Is_Tagged_Ancestor
12027 (Anc_F_Type, Act_F_Type)
12032 -- Otherwise the types of the formals must
12035 elsif Anc_F_Type /= Act_F_Type then
12039 Next_Entity (Anc_Formal);
12040 Next_Entity (Act_Formal);
12043 -- If we traversed through all of the formals
12044 -- then so far the subprograms correspond, so
12045 -- now check that any result types correspond.
12047 if No (Anc_Formal) and then No (Act_Formal) then
12048 Subprograms_Correspond := True;
12050 if Ekind (Act_Subp) = E_Function then
12051 Anc_F_Type := Etype (Anc_Subp);
12052 Act_F_Type := Etype (Act_Subp);
12054 if Ekind (Anc_F_Type) =
12055 E_Anonymous_Access_Type
12058 Designated_Type (Anc_F_Type);
12060 if Ekind (Act_F_Type) =
12061 E_Anonymous_Access_Type
12064 Designated_Type (Act_F_Type);
12066 Subprograms_Correspond := False;
12071 = E_Anonymous_Access_Type
12073 Subprograms_Correspond := False;
12076 Anc_F_Type := Base_Type (Anc_F_Type);
12077 Act_F_Type := Base_Type (Act_F_Type);
12079 -- Now either the result types must be
12080 -- the same or, if the result type is
12081 -- controlling, the result type of the
12082 -- actual primitive must descend from the
12083 -- result type of the ancestor primitive.
12085 if Subprograms_Correspond
12086 and then Anc_F_Type /= Act_F_Type
12088 Has_Controlling_Result (Anc_Subp)
12089 and then not Is_Tagged_Ancestor
12090 (Anc_F_Type, Act_F_Type)
12092 Subprograms_Correspond := False;
12096 -- Found a matching subprogram belonging to
12097 -- formal ancestor type, so actual subprogram
12098 -- corresponds and this violates 3.9.3(9).
12100 if Subprograms_Correspond then
12102 ("abstract subprogram & overrides "
12103 & "nonabstract subprogram of ancestor",
12110 Next_Elmt (Act_Elmt);
12114 Next_Elmt (Gen_Elmt);
12116 end Check_Abstract_Primitives;
12119 -- Verify that limitedness matches. If parent is a limited
12120 -- interface then the generic formal is not unless declared
12121 -- explicitly so. If not declared limited, the actual cannot be
12122 -- limited (see AI05-0087).
12124 -- Even though this AI is a binding interpretation, we enable the
12125 -- check only in Ada 2012 mode, because this improper construct
12126 -- shows up in user code and in existing B-tests.
12128 if Is_Limited_Type (Act_T)
12129 and then not Is_Limited_Type (A_Gen_T)
12130 and then Ada_Version >= Ada_2012
12132 if In_Instance then
12136 ("actual for non-limited & cannot be a limited type",
12138 Explain_Limited_Type (Act_T, Actual);
12139 Abandon_Instantiation (Actual);
12142 end Validate_Derived_Type_Instance;
12144 ----------------------------------------
12145 -- Validate_Discriminated_Formal_Type --
12146 ----------------------------------------
12148 procedure Validate_Discriminated_Formal_Type is
12149 Formal_Discr : Entity_Id;
12150 Actual_Discr : Entity_Id;
12151 Formal_Subt : Entity_Id;
12154 if Has_Discriminants (A_Gen_T) then
12155 if not Has_Discriminants (Act_T) then
12157 ("actual for & must have discriminants", Actual, Gen_T);
12158 Abandon_Instantiation (Actual);
12160 elsif Is_Constrained (Act_T) then
12162 ("actual for & must be unconstrained", Actual, Gen_T);
12163 Abandon_Instantiation (Actual);
12166 Formal_Discr := First_Discriminant (A_Gen_T);
12167 Actual_Discr := First_Discriminant (Act_T);
12168 while Formal_Discr /= Empty loop
12169 if Actual_Discr = Empty then
12171 ("discriminants on actual do not match formal",
12173 Abandon_Instantiation (Actual);
12176 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12178 -- Access discriminants match if designated types do
12180 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12181 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12182 E_Anonymous_Access_Type
12185 (Designated_Type (Base_Type (Formal_Subt))) =
12186 Designated_Type (Base_Type (Etype (Actual_Discr)))
12190 elsif Base_Type (Formal_Subt) /=
12191 Base_Type (Etype (Actual_Discr))
12194 ("types of actual discriminants must match formal",
12196 Abandon_Instantiation (Actual);
12198 elsif not Subtypes_Statically_Match
12199 (Formal_Subt, Etype (Actual_Discr))
12200 and then Ada_Version >= Ada_95
12203 ("subtypes of actual discriminants must match formal",
12205 Abandon_Instantiation (Actual);
12208 Next_Discriminant (Formal_Discr);
12209 Next_Discriminant (Actual_Discr);
12212 if Actual_Discr /= Empty then
12214 ("discriminants on actual do not match formal",
12216 Abandon_Instantiation (Actual);
12220 end Validate_Discriminated_Formal_Type;
12222 ---------------------------------------
12223 -- Validate_Incomplete_Type_Instance --
12224 ---------------------------------------
12226 procedure Validate_Incomplete_Type_Instance is
12228 if not Is_Tagged_Type (Act_T)
12229 and then Is_Tagged_Type (A_Gen_T)
12232 ("actual for & must be a tagged type", Actual, Gen_T);
12235 Validate_Discriminated_Formal_Type;
12236 end Validate_Incomplete_Type_Instance;
12238 --------------------------------------
12239 -- Validate_Interface_Type_Instance --
12240 --------------------------------------
12242 procedure Validate_Interface_Type_Instance is
12244 if not Is_Interface (Act_T) then
12246 ("actual for formal interface type must be an interface",
12249 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12250 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12251 or else Is_Protected_Interface (A_Gen_T) /=
12252 Is_Protected_Interface (Act_T)
12253 or else Is_Synchronized_Interface (A_Gen_T) /=
12254 Is_Synchronized_Interface (Act_T)
12257 ("actual for interface& does not match (RM 12.5.5(4))",
12260 end Validate_Interface_Type_Instance;
12262 ------------------------------------
12263 -- Validate_Private_Type_Instance --
12264 ------------------------------------
12266 procedure Validate_Private_Type_Instance is
12268 if Is_Limited_Type (Act_T)
12269 and then not Is_Limited_Type (A_Gen_T)
12271 if In_Instance then
12275 ("actual for non-limited & cannot be a limited type", Actual,
12277 Explain_Limited_Type (Act_T, Actual);
12278 Abandon_Instantiation (Actual);
12281 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12282 and then not Has_Preelaborable_Initialization (Act_T)
12285 ("actual for & must have preelaborable initialization", Actual,
12288 elsif Is_Indefinite_Subtype (Act_T)
12289 and then not Is_Indefinite_Subtype (A_Gen_T)
12290 and then Ada_Version >= Ada_95
12293 ("actual for & must be a definite subtype", Actual, Gen_T);
12295 elsif not Is_Tagged_Type (Act_T)
12296 and then Is_Tagged_Type (A_Gen_T)
12299 ("actual for & must be a tagged type", Actual, Gen_T);
12302 Validate_Discriminated_Formal_Type;
12304 end Validate_Private_Type_Instance;
12306 -- Start of processing for Instantiate_Type
12309 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12310 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12311 return New_List (Error);
12313 elsif not Is_Entity_Name (Actual)
12314 or else not Is_Type (Entity (Actual))
12317 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12318 Abandon_Instantiation (Actual);
12321 Act_T := Entity (Actual);
12323 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12324 -- as a generic actual parameter if the corresponding formal type
12325 -- does not have a known_discriminant_part, or is a formal derived
12326 -- type that is an Unchecked_Union type.
12328 if Is_Unchecked_Union (Base_Type (Act_T)) then
12329 if not Has_Discriminants (A_Gen_T)
12330 or else (Is_Derived_Type (A_Gen_T)
12331 and then Is_Unchecked_Union (A_Gen_T))
12335 Error_Msg_N ("unchecked union cannot be the actual for a "
12336 & "discriminated formal type", Act_T);
12341 -- Deal with fixed/floating restrictions
12343 if Is_Floating_Point_Type (Act_T) then
12344 Check_Restriction (No_Floating_Point, Actual);
12345 elsif Is_Fixed_Point_Type (Act_T) then
12346 Check_Restriction (No_Fixed_Point, Actual);
12349 -- Deal with error of using incomplete type as generic actual.
12350 -- This includes limited views of a type, even if the non-limited
12351 -- view may be available.
12353 if Ekind (Act_T) = E_Incomplete_Type
12354 or else (Is_Class_Wide_Type (Act_T)
12355 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12357 -- If the formal is an incomplete type, the actual can be
12358 -- incomplete as well.
12360 if Ekind (A_Gen_T) = E_Incomplete_Type then
12363 elsif Is_Class_Wide_Type (Act_T)
12364 or else No (Full_View (Act_T))
12366 Error_Msg_N ("premature use of incomplete type", Actual);
12367 Abandon_Instantiation (Actual);
12369 Act_T := Full_View (Act_T);
12370 Set_Entity (Actual, Act_T);
12372 if Has_Private_Component (Act_T) then
12374 ("premature use of type with private component", Actual);
12378 -- Deal with error of premature use of private type as generic actual
12380 elsif Is_Private_Type (Act_T)
12381 and then Is_Private_Type (Base_Type (Act_T))
12382 and then not Is_Generic_Type (Act_T)
12383 and then not Is_Derived_Type (Act_T)
12384 and then No (Full_View (Root_Type (Act_T)))
12386 -- If the formal is an incomplete type, the actual can be
12387 -- private or incomplete as well.
12389 if Ekind (A_Gen_T) = E_Incomplete_Type then
12392 Error_Msg_N ("premature use of private type", Actual);
12395 elsif Has_Private_Component (Act_T) then
12397 ("premature use of type with private component", Actual);
12400 Set_Instance_Of (A_Gen_T, Act_T);
12402 -- If the type is generic, the class-wide type may also be used
12404 if Is_Tagged_Type (A_Gen_T)
12405 and then Is_Tagged_Type (Act_T)
12406 and then not Is_Class_Wide_Type (A_Gen_T)
12408 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12409 Class_Wide_Type (Act_T));
12412 if not Is_Abstract_Type (A_Gen_T)
12413 and then Is_Abstract_Type (Act_T)
12416 ("actual of non-abstract formal cannot be abstract", Actual);
12419 -- A generic scalar type is a first subtype for which we generate
12420 -- an anonymous base type. Indicate that the instance of this base
12421 -- is the base type of the actual.
12423 if Is_Scalar_Type (A_Gen_T) then
12424 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12428 if Error_Posted (Act_T) then
12431 case Nkind (Def) is
12432 when N_Formal_Private_Type_Definition =>
12433 Validate_Private_Type_Instance;
12435 when N_Formal_Incomplete_Type_Definition =>
12436 Validate_Incomplete_Type_Instance;
12438 when N_Formal_Derived_Type_Definition =>
12439 Validate_Derived_Type_Instance;
12441 when N_Formal_Discrete_Type_Definition =>
12442 if not Is_Discrete_Type (Act_T) then
12444 ("expect discrete type in instantiation of&",
12446 Abandon_Instantiation (Actual);
12449 Diagnose_Predicated_Actual;
12451 when N_Formal_Signed_Integer_Type_Definition =>
12452 if not Is_Signed_Integer_Type (Act_T) then
12454 ("expect signed integer type in instantiation of&",
12456 Abandon_Instantiation (Actual);
12459 Diagnose_Predicated_Actual;
12461 when N_Formal_Modular_Type_Definition =>
12462 if not Is_Modular_Integer_Type (Act_T) then
12464 ("expect modular type in instantiation of &",
12466 Abandon_Instantiation (Actual);
12469 Diagnose_Predicated_Actual;
12471 when N_Formal_Floating_Point_Definition =>
12472 if not Is_Floating_Point_Type (Act_T) then
12474 ("expect float type in instantiation of &", Actual, Gen_T);
12475 Abandon_Instantiation (Actual);
12478 when N_Formal_Ordinary_Fixed_Point_Definition =>
12479 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12481 ("expect ordinary fixed point type in instantiation of &",
12483 Abandon_Instantiation (Actual);
12486 when N_Formal_Decimal_Fixed_Point_Definition =>
12487 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12489 ("expect decimal type in instantiation of &",
12491 Abandon_Instantiation (Actual);
12494 when N_Array_Type_Definition =>
12495 Validate_Array_Type_Instance;
12497 when N_Access_To_Object_Definition =>
12498 Validate_Access_Type_Instance;
12500 when N_Access_Function_Definition |
12501 N_Access_Procedure_Definition =>
12502 Validate_Access_Subprogram_Instance;
12504 when N_Record_Definition =>
12505 Validate_Interface_Type_Instance;
12507 when N_Derived_Type_Definition =>
12508 Validate_Derived_Interface_Type_Instance;
12511 raise Program_Error;
12516 Subt := New_Copy (Gen_T);
12518 -- Use adjusted sloc of subtype name as the location for other nodes in
12519 -- the subtype declaration.
12521 Loc := Sloc (Subt);
12524 Make_Subtype_Declaration (Loc,
12525 Defining_Identifier => Subt,
12526 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12528 if Is_Private_Type (Act_T) then
12529 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12531 elsif Is_Access_Type (Act_T)
12532 and then Is_Private_Type (Designated_Type (Act_T))
12534 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12537 -- In Ada 2012 the actual may be a limited view. Indicate that
12538 -- the local subtype must be treated as such.
12540 if From_Limited_With (Act_T) then
12541 Set_Ekind (Subt, E_Incomplete_Subtype);
12542 Set_From_Limited_With (Subt);
12545 Decl_Nodes := New_List (Decl_Node);
12547 -- Flag actual derived types so their elaboration produces the
12548 -- appropriate renamings for the primitive operations of the ancestor.
12549 -- Flag actual for formal private types as well, to determine whether
12550 -- operations in the private part may override inherited operations.
12551 -- If the formal has an interface list, the ancestor is not the
12552 -- parent, but the analyzed formal that includes the interface
12553 -- operations of all its progenitors.
12555 -- Same treatment for formal private types, so we can check whether the
12556 -- type is tagged limited when validating derivations in the private
12557 -- part. (See AI05-096).
12559 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12560 if Present (Interface_List (Def)) then
12561 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12563 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12566 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12567 N_Formal_Incomplete_Type_Definition)
12569 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12572 -- If the actual is a synchronized type that implements an interface,
12573 -- the primitive operations are attached to the corresponding record,
12574 -- and we have to treat it as an additional generic actual, so that its
12575 -- primitive operations become visible in the instance. The task or
12576 -- protected type itself does not carry primitive operations.
12578 if Is_Concurrent_Type (Act_T)
12579 and then Is_Tagged_Type (Act_T)
12580 and then Present (Corresponding_Record_Type (Act_T))
12581 and then Present (Ancestor)
12582 and then Is_Interface (Ancestor)
12585 Corr_Rec : constant Entity_Id :=
12586 Corresponding_Record_Type (Act_T);
12587 New_Corr : Entity_Id;
12588 Corr_Decl : Node_Id;
12591 New_Corr := Make_Temporary (Loc, 'S');
12593 Make_Subtype_Declaration (Loc,
12594 Defining_Identifier => New_Corr,
12595 Subtype_Indication =>
12596 New_Occurrence_Of (Corr_Rec, Loc));
12597 Append_To (Decl_Nodes, Corr_Decl);
12599 if Ekind (Act_T) = E_Task_Type then
12600 Set_Ekind (Subt, E_Task_Subtype);
12602 Set_Ekind (Subt, E_Protected_Subtype);
12605 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12606 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12607 Set_Generic_Parent_Type (Decl_Node, Empty);
12611 -- For a floating-point type, capture dimension info if any, because
12612 -- the generated subtype declaration does not come from source and
12613 -- will not process dimensions.
12615 if Is_Floating_Point_Type (Act_T) then
12616 Copy_Dimensions (Act_T, Subt);
12620 end Instantiate_Type;
12622 ---------------------
12623 -- Is_In_Main_Unit --
12624 ---------------------
12626 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12627 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12628 Current_Unit : Node_Id;
12631 if Unum = Main_Unit then
12634 -- If the current unit is a subunit then it is either the main unit or
12635 -- is being compiled as part of the main unit.
12637 elsif Nkind (N) = N_Compilation_Unit then
12638 return Nkind (Unit (N)) = N_Subunit;
12641 Current_Unit := Parent (N);
12642 while Present (Current_Unit)
12643 and then Nkind (Current_Unit) /= N_Compilation_Unit
12645 Current_Unit := Parent (Current_Unit);
12648 -- The instantiation node is in the main unit, or else the current node
12649 -- (perhaps as the result of nested instantiations) is in the main unit,
12650 -- or in the declaration of the main unit, which in this last case must
12653 return Unum = Main_Unit
12654 or else Current_Unit = Cunit (Main_Unit)
12655 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12656 or else (Present (Library_Unit (Current_Unit))
12657 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12658 end Is_In_Main_Unit;
12660 ----------------------------
12661 -- Load_Parent_Of_Generic --
12662 ----------------------------
12664 procedure Load_Parent_Of_Generic
12667 Body_Optional : Boolean := False)
12669 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12670 Saved_Style_Check : constant Boolean := Style_Check;
12671 Saved_Warnings : constant Warning_Record := Save_Warnings;
12672 True_Parent : Node_Id;
12673 Inst_Node : Node_Id;
12675 Previous_Instances : constant Elist_Id := New_Elmt_List;
12677 procedure Collect_Previous_Instances (Decls : List_Id);
12678 -- Collect all instantiations in the given list of declarations, that
12679 -- precede the generic that we need to load. If the bodies of these
12680 -- instantiations are available, we must analyze them, to ensure that
12681 -- the public symbols generated are the same when the unit is compiled
12682 -- to generate code, and when it is compiled in the context of a unit
12683 -- that needs a particular nested instance. This process is applied to
12684 -- both package and subprogram instances.
12686 --------------------------------
12687 -- Collect_Previous_Instances --
12688 --------------------------------
12690 procedure Collect_Previous_Instances (Decls : List_Id) is
12694 Decl := First (Decls);
12695 while Present (Decl) loop
12696 if Sloc (Decl) >= Sloc (Inst_Node) then
12699 -- If Decl is an instantiation, then record it as requiring
12700 -- instantiation of the corresponding body, except if it is an
12701 -- abbreviated instantiation generated internally for conformance
12702 -- checking purposes only for the case of a formal package
12703 -- declared without a box (see Instantiate_Formal_Package). Such
12704 -- an instantiation does not generate any code (the actual code
12705 -- comes from actual) and thus does not need to be analyzed here.
12706 -- If the instantiation appears with a generic package body it is
12707 -- not analyzed here either.
12709 elsif Nkind (Decl) = N_Package_Instantiation
12710 and then not Is_Internal (Defining_Entity (Decl))
12712 Append_Elmt (Decl, Previous_Instances);
12714 -- For a subprogram instantiation, omit instantiations intrinsic
12715 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12717 elsif Nkind_In (Decl, N_Function_Instantiation,
12718 N_Procedure_Instantiation)
12719 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12721 Append_Elmt (Decl, Previous_Instances);
12723 elsif Nkind (Decl) = N_Package_Declaration then
12724 Collect_Previous_Instances
12725 (Visible_Declarations (Specification (Decl)));
12726 Collect_Previous_Instances
12727 (Private_Declarations (Specification (Decl)));
12729 -- Previous non-generic bodies may contain instances as well
12731 elsif Nkind (Decl) = N_Package_Body
12732 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12734 Collect_Previous_Instances (Declarations (Decl));
12736 elsif Nkind (Decl) = N_Subprogram_Body
12737 and then not Acts_As_Spec (Decl)
12738 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12740 Collect_Previous_Instances (Declarations (Decl));
12745 end Collect_Previous_Instances;
12747 -- Start of processing for Load_Parent_Of_Generic
12750 if not In_Same_Source_Unit (N, Spec)
12751 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12752 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12753 and then not Is_In_Main_Unit (Spec))
12755 -- Find body of parent of spec, and analyze it. A special case arises
12756 -- when the parent is an instantiation, that is to say when we are
12757 -- currently instantiating a nested generic. In that case, there is
12758 -- no separate file for the body of the enclosing instance. Instead,
12759 -- the enclosing body must be instantiated as if it were a pending
12760 -- instantiation, in order to produce the body for the nested generic
12761 -- we require now. Note that in that case the generic may be defined
12762 -- in a package body, the instance defined in the same package body,
12763 -- and the original enclosing body may not be in the main unit.
12765 Inst_Node := Empty;
12767 True_Parent := Parent (Spec);
12768 while Present (True_Parent)
12769 and then Nkind (True_Parent) /= N_Compilation_Unit
12771 if Nkind (True_Parent) = N_Package_Declaration
12773 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12775 -- Parent is a compilation unit that is an instantiation.
12776 -- Instantiation node has been replaced with package decl.
12778 Inst_Node := Original_Node (True_Parent);
12781 elsif Nkind (True_Parent) = N_Package_Declaration
12782 and then Present (Generic_Parent (Specification (True_Parent)))
12783 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12785 -- Parent is an instantiation within another specification.
12786 -- Declaration for instance has been inserted before original
12787 -- instantiation node. A direct link would be preferable?
12789 Inst_Node := Next (True_Parent);
12790 while Present (Inst_Node)
12791 and then Nkind (Inst_Node) /= N_Package_Instantiation
12796 -- If the instance appears within a generic, and the generic
12797 -- unit is defined within a formal package of the enclosing
12798 -- generic, there is no generic body available, and none
12799 -- needed. A more precise test should be used ???
12801 if No (Inst_Node) then
12808 True_Parent := Parent (True_Parent);
12812 -- Case where we are currently instantiating a nested generic
12814 if Present (Inst_Node) then
12815 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12817 -- Instantiation node and declaration of instantiated package
12818 -- were exchanged when only the declaration was needed.
12819 -- Restore instantiation node before proceeding with body.
12821 Set_Unit (Parent (True_Parent), Inst_Node);
12824 -- Now complete instantiation of enclosing body, if it appears in
12825 -- some other unit. If it appears in the current unit, the body
12826 -- will have been instantiated already.
12828 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12830 -- We need to determine the expander mode to instantiate the
12831 -- enclosing body. Because the generic body we need may use
12832 -- global entities declared in the enclosing package (including
12833 -- aggregates) it is in general necessary to compile this body
12834 -- with expansion enabled, except if we are within a generic
12835 -- package, in which case the usual generic rule applies.
12838 Exp_Status : Boolean := True;
12842 -- Loop through scopes looking for generic package
12844 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12845 while Present (Scop)
12846 and then Scop /= Standard_Standard
12848 if Ekind (Scop) = E_Generic_Package then
12849 Exp_Status := False;
12853 Scop := Scope (Scop);
12856 -- Collect previous instantiations in the unit that contains
12857 -- the desired generic.
12859 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12860 and then not Body_Optional
12864 Info : Pending_Body_Info;
12868 Par := Parent (Inst_Node);
12869 while Present (Par) loop
12870 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12871 Par := Parent (Par);
12874 pragma Assert (Present (Par));
12876 if Nkind (Par) = N_Package_Body then
12877 Collect_Previous_Instances (Declarations (Par));
12879 elsif Nkind (Par) = N_Package_Declaration then
12880 Collect_Previous_Instances
12881 (Visible_Declarations (Specification (Par)));
12882 Collect_Previous_Instances
12883 (Private_Declarations (Specification (Par)));
12886 -- Enclosing unit is a subprogram body. In this
12887 -- case all instance bodies are processed in order
12888 -- and there is no need to collect them separately.
12893 Decl := First_Elmt (Previous_Instances);
12894 while Present (Decl) loop
12896 (Inst_Node => Node (Decl),
12898 Instance_Spec (Node (Decl)),
12899 Expander_Status => Exp_Status,
12900 Current_Sem_Unit =>
12901 Get_Code_Unit (Sloc (Node (Decl))),
12902 Scope_Suppress => Scope_Suppress,
12903 Local_Suppress_Stack_Top =>
12904 Local_Suppress_Stack_Top,
12905 Version => Ada_Version,
12906 Version_Pragma => Ada_Version_Pragma,
12907 Warnings => Save_Warnings,
12908 SPARK_Mode => SPARK_Mode,
12909 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12911 -- Package instance
12914 Nkind (Node (Decl)) = N_Package_Instantiation
12916 Instantiate_Package_Body
12917 (Info, Body_Optional => True);
12919 -- Subprogram instance
12922 -- The instance_spec is in the wrapper package,
12923 -- usually followed by its local renaming
12924 -- declaration. See Build_Subprogram_Renaming
12929 (Last (Visible_Declarations
12930 (Specification (Info.Act_Decl))));
12933 N_Subprogram_Renaming_Declaration
12935 Decl := Prev (Decl);
12938 Info.Act_Decl := Decl;
12941 Instantiate_Subprogram_Body
12942 (Info, Body_Optional => True);
12950 Instantiate_Package_Body
12952 ((Inst_Node => Inst_Node,
12953 Act_Decl => True_Parent,
12954 Expander_Status => Exp_Status,
12955 Current_Sem_Unit => Get_Code_Unit
12956 (Sloc (Inst_Node)),
12957 Scope_Suppress => Scope_Suppress,
12958 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12959 Version => Ada_Version,
12960 Version_Pragma => Ada_Version_Pragma,
12961 Warnings => Save_Warnings,
12962 SPARK_Mode => SPARK_Mode,
12963 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12964 Body_Optional => Body_Optional);
12968 -- Case where we are not instantiating a nested generic
12971 Opt.Style_Check := False;
12972 Expander_Mode_Save_And_Set (True);
12973 Load_Needed_Body (Comp_Unit, OK);
12974 Opt.Style_Check := Saved_Style_Check;
12975 Restore_Warnings (Saved_Warnings);
12976 Expander_Mode_Restore;
12979 and then Unit_Requires_Body (Defining_Entity (Spec))
12980 and then not Body_Optional
12983 Bname : constant Unit_Name_Type :=
12984 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12987 -- In CodePeer mode, the missing body may make the analysis
12988 -- incomplete, but we do not treat it as fatal.
12990 if CodePeer_Mode then
12994 Error_Msg_Unit_1 := Bname;
12995 Error_Msg_N ("this instantiation requires$!", N);
12996 Error_Msg_File_1 :=
12997 Get_File_Name (Bname, Subunit => False);
12998 Error_Msg_N ("\but file{ was not found!", N);
12999 raise Unrecoverable_Error;
13006 -- If loading parent of the generic caused an instantiation circularity,
13007 -- we abandon compilation at this point, because otherwise in some cases
13008 -- we get into trouble with infinite recursions after this point.
13010 if Circularity_Detected then
13011 raise Unrecoverable_Error;
13013 end Load_Parent_Of_Generic;
13015 ---------------------------------
13016 -- Map_Formal_Package_Entities --
13017 ---------------------------------
13019 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13024 Set_Instance_Of (Form, Act);
13026 -- Traverse formal and actual package to map the corresponding entities.
13027 -- We skip over internal entities that may be generated during semantic
13028 -- analysis, and find the matching entities by name, given that they
13029 -- must appear in the same order.
13031 E1 := First_Entity (Form);
13032 E2 := First_Entity (Act);
13033 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13034 -- Could this test be a single condition??? Seems like it could, and
13035 -- isn't FPE (Form) a constant anyway???
13037 if not Is_Internal (E1)
13038 and then Present (Parent (E1))
13039 and then not Is_Class_Wide_Type (E1)
13040 and then not Is_Internal_Name (Chars (E1))
13042 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13049 Set_Instance_Of (E1, E2);
13051 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13052 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13055 if Is_Constrained (E1) then
13056 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13059 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13060 Map_Formal_Package_Entities (E1, E2);
13067 end Map_Formal_Package_Entities;
13069 -----------------------
13070 -- Move_Freeze_Nodes --
13071 -----------------------
13073 procedure Move_Freeze_Nodes
13074 (Out_Of : Entity_Id;
13079 Next_Decl : Node_Id;
13080 Next_Node : Node_Id := After;
13083 function Is_Outer_Type (T : Entity_Id) return Boolean;
13084 -- Check whether entity is declared in a scope external to that of the
13087 -------------------
13088 -- Is_Outer_Type --
13089 -------------------
13091 function Is_Outer_Type (T : Entity_Id) return Boolean is
13092 Scop : Entity_Id := Scope (T);
13095 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13099 while Scop /= Standard_Standard loop
13100 if Scop = Out_Of then
13103 Scop := Scope (Scop);
13111 -- Start of processing for Move_Freeze_Nodes
13118 -- First remove the freeze nodes that may appear before all other
13122 while Present (Decl)
13123 and then Nkind (Decl) = N_Freeze_Entity
13124 and then Is_Outer_Type (Entity (Decl))
13126 Decl := Remove_Head (L);
13127 Insert_After (Next_Node, Decl);
13128 Set_Analyzed (Decl, False);
13133 -- Next scan the list of declarations and remove each freeze node that
13134 -- appears ahead of the current node.
13136 while Present (Decl) loop
13137 while Present (Next (Decl))
13138 and then Nkind (Next (Decl)) = N_Freeze_Entity
13139 and then Is_Outer_Type (Entity (Next (Decl)))
13141 Next_Decl := Remove_Next (Decl);
13142 Insert_After (Next_Node, Next_Decl);
13143 Set_Analyzed (Next_Decl, False);
13144 Next_Node := Next_Decl;
13147 -- If the declaration is a nested package or concurrent type, then
13148 -- recurse. Nested generic packages will have been processed from the
13151 case Nkind (Decl) is
13152 when N_Package_Declaration =>
13153 Spec := Specification (Decl);
13155 when N_Task_Type_Declaration =>
13156 Spec := Task_Definition (Decl);
13158 when N_Protected_Type_Declaration =>
13159 Spec := Protected_Definition (Decl);
13165 if Present (Spec) then
13166 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13167 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13172 end Move_Freeze_Nodes;
13178 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13180 return Generic_Renamings.Table (E).Next_In_HTable;
13183 ------------------------
13184 -- Preanalyze_Actuals --
13185 ------------------------
13187 procedure Preanalyze_Actuals (N : Node_Id) is
13190 Errs : constant Int := Serious_Errors_Detected;
13192 Cur : Entity_Id := Empty;
13193 -- Current homograph of the instance name
13196 -- Saved visibility status of the current homograph
13199 Assoc := First (Generic_Associations (N));
13201 -- If the instance is a child unit, its name may hide an outer homonym,
13202 -- so make it invisible to perform name resolution on the actuals.
13204 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13206 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13208 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13210 if Is_Compilation_Unit (Cur) then
13211 Vis := Is_Immediately_Visible (Cur);
13212 Set_Is_Immediately_Visible (Cur, False);
13218 while Present (Assoc) loop
13219 if Nkind (Assoc) /= N_Others_Choice then
13220 Act := Explicit_Generic_Actual_Parameter (Assoc);
13222 -- Within a nested instantiation, a defaulted actual is an empty
13223 -- association, so nothing to analyze. If the subprogram actual
13224 -- is an attribute, analyze prefix only, because actual is not a
13225 -- complete attribute reference.
13227 -- If actual is an allocator, analyze expression only. The full
13228 -- analysis can generate code, and if instance is a compilation
13229 -- unit we have to wait until the package instance is installed
13230 -- to have a proper place to insert this code.
13232 -- String literals may be operators, but at this point we do not
13233 -- know whether the actual is a formal subprogram or a string.
13238 elsif Nkind (Act) = N_Attribute_Reference then
13239 Analyze (Prefix (Act));
13241 elsif Nkind (Act) = N_Explicit_Dereference then
13242 Analyze (Prefix (Act));
13244 elsif Nkind (Act) = N_Allocator then
13246 Expr : constant Node_Id := Expression (Act);
13249 if Nkind (Expr) = N_Subtype_Indication then
13250 Analyze (Subtype_Mark (Expr));
13252 -- Analyze separately each discriminant constraint, when
13253 -- given with a named association.
13259 Constr := First (Constraints (Constraint (Expr)));
13260 while Present (Constr) loop
13261 if Nkind (Constr) = N_Discriminant_Association then
13262 Analyze (Expression (Constr));
13276 elsif Nkind (Act) /= N_Operator_Symbol then
13280 if Errs /= Serious_Errors_Detected then
13282 -- Do a minimal analysis of the generic, to prevent spurious
13283 -- warnings complaining about the generic being unreferenced,
13284 -- before abandoning the instantiation.
13286 Analyze (Name (N));
13288 if Is_Entity_Name (Name (N))
13289 and then Etype (Name (N)) /= Any_Type
13291 Generate_Reference (Entity (Name (N)), Name (N));
13292 Set_Is_Instantiated (Entity (Name (N)));
13295 if Present (Cur) then
13297 -- For the case of a child instance hiding an outer homonym,
13298 -- provide additional warning which might explain the error.
13300 Set_Is_Immediately_Visible (Cur, Vis);
13302 ("& hides outer unit with the same name??",
13303 N, Defining_Unit_Name (N));
13306 Abandon_Instantiation (Act);
13313 if Present (Cur) then
13314 Set_Is_Immediately_Visible (Cur, Vis);
13316 end Preanalyze_Actuals;
13318 -------------------
13319 -- Remove_Parent --
13320 -------------------
13322 procedure Remove_Parent (In_Body : Boolean := False) is
13323 S : Entity_Id := Current_Scope;
13324 -- S is the scope containing the instantiation just completed. The scope
13325 -- stack contains the parent instances of the instantiation, followed by
13334 -- After child instantiation is complete, remove from scope stack the
13335 -- extra copy of the current scope, and then remove parent instances.
13337 if not In_Body then
13340 while Current_Scope /= S loop
13341 P := Current_Scope;
13342 End_Package_Scope (Current_Scope);
13344 if In_Open_Scopes (P) then
13345 E := First_Entity (P);
13346 while Present (E) loop
13347 Set_Is_Immediately_Visible (E, True);
13351 -- If instantiation is declared in a block, it is the enclosing
13352 -- scope that might be a parent instance. Note that only one
13353 -- block can be involved, because the parent instances have
13354 -- been installed within it.
13356 if Ekind (P) = E_Block then
13357 Cur_P := Scope (P);
13362 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13363 -- We are within an instance of some sibling. Retain
13364 -- visibility of parent, for proper subsequent cleanup, and
13365 -- reinstall private declarations as well.
13367 Set_In_Private_Part (P);
13368 Install_Private_Declarations (P);
13371 -- If the ultimate parent is a top-level unit recorded in
13372 -- Instance_Parent_Unit, then reset its visibility to what it was
13373 -- before instantiation. (It's not clear what the purpose is of
13374 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13375 -- present before the ultimate parent test was added.???)
13377 elsif not In_Open_Scopes (Scope (P))
13378 or else (P = Instance_Parent_Unit
13379 and then not Parent_Unit_Visible)
13381 Set_Is_Immediately_Visible (P, False);
13383 -- If the current scope is itself an instantiation of a generic
13384 -- nested within P, and we are in the private part of body of this
13385 -- instantiation, restore the full views of P, that were removed
13386 -- in End_Package_Scope above. This obscure case can occur when a
13387 -- subunit of a generic contains an instance of a child unit of
13388 -- its generic parent unit.
13390 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13392 Par : constant Entity_Id :=
13393 Generic_Parent (Package_Specification (S));
13396 and then P = Scope (Par)
13397 and then (In_Package_Body (S) or else In_Private_Part (S))
13399 Set_In_Private_Part (P);
13400 Install_Private_Declarations (P);
13406 -- Reset visibility of entities in the enclosing scope
13408 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13410 Hidden := First_Elmt (Hidden_Entities);
13411 while Present (Hidden) loop
13412 Set_Is_Immediately_Visible (Node (Hidden), True);
13413 Next_Elmt (Hidden);
13417 -- Each body is analyzed separately, and there is no context that
13418 -- needs preserving from one body instance to the next, so remove all
13419 -- parent scopes that have been installed.
13421 while Present (S) loop
13422 End_Package_Scope (S);
13423 Set_Is_Immediately_Visible (S, False);
13424 S := Current_Scope;
13425 exit when S = Standard_Standard;
13434 procedure Restore_Env is
13435 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13438 if No (Current_Instantiated_Parent.Act_Id) then
13439 -- Restore environment after subprogram inlining
13441 Restore_Private_Views (Empty);
13444 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13445 Exchanged_Views := Saved.Exchanged_Views;
13446 Hidden_Entities := Saved.Hidden_Entities;
13447 Current_Sem_Unit := Saved.Current_Sem_Unit;
13448 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13449 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13451 Restore_Opt_Config_Switches (Saved.Switches);
13453 Instance_Envs.Decrement_Last;
13456 ---------------------------
13457 -- Restore_Private_Views --
13458 ---------------------------
13460 procedure Restore_Private_Views
13461 (Pack_Id : Entity_Id;
13462 Is_Package : Boolean := True)
13467 Dep_Elmt : Elmt_Id;
13470 procedure Restore_Nested_Formal (Formal : Entity_Id);
13471 -- Hide the generic formals of formal packages declared with box which
13472 -- were reachable in the current instantiation.
13474 ---------------------------
13475 -- Restore_Nested_Formal --
13476 ---------------------------
13478 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13482 if Present (Renamed_Object (Formal))
13483 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13487 elsif Present (Associated_Formal_Package (Formal)) then
13488 Ent := First_Entity (Formal);
13489 while Present (Ent) loop
13490 exit when Ekind (Ent) = E_Package
13491 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13493 Set_Is_Hidden (Ent);
13494 Set_Is_Potentially_Use_Visible (Ent, False);
13496 -- If package, then recurse
13498 if Ekind (Ent) = E_Package then
13499 Restore_Nested_Formal (Ent);
13505 end Restore_Nested_Formal;
13507 -- Start of processing for Restore_Private_Views
13510 M := First_Elmt (Exchanged_Views);
13511 while Present (M) loop
13514 -- Subtypes of types whose views have been exchanged, and that are
13515 -- defined within the instance, were not on the Private_Dependents
13516 -- list on entry to the instance, so they have to be exchanged
13517 -- explicitly now, in order to remain consistent with the view of the
13520 if Ekind_In (Typ, E_Private_Type,
13521 E_Limited_Private_Type,
13522 E_Record_Type_With_Private)
13524 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13525 while Present (Dep_Elmt) loop
13526 Dep_Typ := Node (Dep_Elmt);
13528 if Scope (Dep_Typ) = Pack_Id
13529 and then Present (Full_View (Dep_Typ))
13531 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13532 Exchange_Declarations (Dep_Typ);
13535 Next_Elmt (Dep_Elmt);
13539 Exchange_Declarations (Node (M));
13543 if No (Pack_Id) then
13547 -- Make the generic formal parameters private, and make the formal types
13548 -- into subtypes of the actuals again.
13550 E := First_Entity (Pack_Id);
13551 while Present (E) loop
13552 Set_Is_Hidden (E, True);
13555 and then Nkind (Parent (E)) = N_Subtype_Declaration
13557 -- If the actual for E is itself a generic actual type from
13558 -- an enclosing instance, E is still a generic actual type
13559 -- outside of the current instance. This matter when resolving
13560 -- an overloaded call that may be ambiguous in the enclosing
13561 -- instance, when two of its actuals coincide.
13563 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13564 and then Is_Generic_Actual_Type
13565 (Entity (Subtype_Indication (Parent (E))))
13569 Set_Is_Generic_Actual_Type (E, False);
13572 -- An unusual case of aliasing: the actual may also be directly
13573 -- visible in the generic, and be private there, while it is fully
13574 -- visible in the context of the instance. The internal subtype
13575 -- is private in the instance but has full visibility like its
13576 -- parent in the enclosing scope. This enforces the invariant that
13577 -- the privacy status of all private dependents of a type coincide
13578 -- with that of the parent type. This can only happen when a
13579 -- generic child unit is instantiated within a sibling.
13581 if Is_Private_Type (E)
13582 and then not Is_Private_Type (Etype (E))
13584 Exchange_Declarations (E);
13587 elsif Ekind (E) = E_Package then
13589 -- The end of the renaming list is the renaming of the generic
13590 -- package itself. If the instance is a subprogram, all entities
13591 -- in the corresponding package are renamings. If this entity is
13592 -- a formal package, make its own formals private as well. The
13593 -- actual in this case is itself the renaming of an instantiation.
13594 -- If the entity is not a package renaming, it is the entity
13595 -- created to validate formal package actuals: ignore it.
13597 -- If the actual is itself a formal package for the enclosing
13598 -- generic, or the actual for such a formal package, it remains
13599 -- visible on exit from the instance, and therefore nothing needs
13600 -- to be done either, except to keep it accessible.
13602 if Is_Package and then Renamed_Object (E) = Pack_Id then
13605 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13609 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13611 Set_Is_Hidden (E, False);
13615 Act_P : constant Entity_Id := Renamed_Object (E);
13619 Id := First_Entity (Act_P);
13621 and then Id /= First_Private_Entity (Act_P)
13623 exit when Ekind (Id) = E_Package
13624 and then Renamed_Object (Id) = Act_P;
13626 Set_Is_Hidden (Id, True);
13627 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13629 if Ekind (Id) = E_Package then
13630 Restore_Nested_Formal (Id);
13641 end Restore_Private_Views;
13648 (Gen_Unit : Entity_Id;
13649 Act_Unit : Entity_Id)
13653 Set_Instance_Env (Gen_Unit, Act_Unit);
13656 ----------------------------
13657 -- Save_Global_References --
13658 ----------------------------
13660 procedure Save_Global_References (Templ : Node_Id) is
13662 -- ??? it is horrible to use global variables in highly recursive code
13665 -- The entity of the current associated node
13667 Gen_Scope : Entity_Id;
13668 -- The scope of the generic for which references are being saved
13671 -- The current associated node
13673 function Is_Global (E : Entity_Id) return Boolean;
13674 -- Check whether entity is defined outside of generic unit. Examine the
13675 -- scope of an entity, and the scope of the scope, etc, until we find
13676 -- either Standard, in which case the entity is global, or the generic
13677 -- unit itself, which indicates that the entity is local. If the entity
13678 -- is the generic unit itself, as in the case of a recursive call, or
13679 -- the enclosing generic unit, if different from the current scope, then
13680 -- it is local as well, because it will be replaced at the point of
13681 -- instantiation. On the other hand, if it is a reference to a child
13682 -- unit of a common ancestor, which appears in an instantiation, it is
13683 -- global because it is used to denote a specific compilation unit at
13684 -- the time the instantiations will be analyzed.
13686 procedure Reset_Entity (N : Node_Id);
13687 -- Save semantic information on global entity so that it is not resolved
13688 -- again at instantiation time.
13690 procedure Save_Entity_Descendants (N : Node_Id);
13691 -- Apply Save_Global_References to the two syntactic descendants of
13692 -- non-terminal nodes that carry an Associated_Node and are processed
13693 -- through Reset_Entity. Once the global entity (if any) has been
13694 -- captured together with its type, only two syntactic descendants need
13695 -- to be traversed to complete the processing of the tree rooted at N.
13696 -- This applies to Selected_Components, Expanded_Names, and to Operator
13697 -- nodes. N can also be a character literal, identifier, or operator
13698 -- symbol node, but the call has no effect in these cases.
13700 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13701 -- Default actuals in nested instances must be handled specially
13702 -- because there is no link to them from the original tree. When an
13703 -- actual subprogram is given by a default, we add an explicit generic
13704 -- association for it in the instantiation node. When we save the
13705 -- global references on the name of the instance, we recover the list
13706 -- of generic associations, and add an explicit one to the original
13707 -- generic tree, through which a global actual can be preserved.
13708 -- Similarly, if a child unit is instantiated within a sibling, in the
13709 -- context of the parent, we must preserve the identifier of the parent
13710 -- so that it can be properly resolved in a subsequent instantiation.
13712 procedure Save_Global_Descendant (D : Union_Id);
13713 -- Apply Save_References recursively to the descendents of node D
13715 procedure Save_References (N : Node_Id);
13716 -- This is the recursive procedure that does the work, once the
13717 -- enclosing generic scope has been established.
13723 function Is_Global (E : Entity_Id) return Boolean is
13726 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13727 -- Determine whether the parent node of a reference to a child unit
13728 -- denotes an instantiation or a formal package, in which case the
13729 -- reference to the child unit is global, even if it appears within
13730 -- the current scope (e.g. when the instance appears within the body
13731 -- of an ancestor).
13733 ----------------------
13734 -- Is_Instance_Node --
13735 ----------------------
13737 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13739 return Nkind (Decl) in N_Generic_Instantiation
13741 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13742 end Is_Instance_Node;
13744 -- Start of processing for Is_Global
13747 if E = Gen_Scope then
13750 elsif E = Standard_Standard then
13753 elsif Is_Child_Unit (E)
13754 and then (Is_Instance_Node (Parent (N2))
13755 or else (Nkind (Parent (N2)) = N_Expanded_Name
13756 and then N2 = Selector_Name (Parent (N2))
13758 Is_Instance_Node (Parent (Parent (N2)))))
13764 while Se /= Gen_Scope loop
13765 if Se = Standard_Standard then
13780 procedure Reset_Entity (N : Node_Id) is
13781 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13782 -- If the type of N2 is global to the generic unit, save the type in
13783 -- the generic node. Just as we perform name capture for explicit
13784 -- references within the generic, we must capture the global types
13785 -- of local entities because they may participate in resolution in
13788 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13789 -- Find the ultimate ancestor of the current unit. If it is not a
13790 -- generic unit, then the name of the current unit in the prefix of
13791 -- an expanded name must be replaced with its generic homonym to
13792 -- ensure that it will be properly resolved in an instance.
13794 ---------------------
13795 -- Set_Global_Type --
13796 ---------------------
13798 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13799 Typ : constant Entity_Id := Etype (N2);
13802 Set_Etype (N, Typ);
13804 -- If the entity of N is not the associated node, this is a
13805 -- nested generic and it has an associated node as well, whose
13806 -- type is already the full view (see below). Indicate that the
13807 -- original node has a private view.
13809 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13810 Set_Has_Private_View (N);
13813 -- If not a private type, nothing else to do
13815 if not Is_Private_Type (Typ) then
13816 if Is_Array_Type (Typ)
13817 and then Is_Private_Type (Component_Type (Typ))
13819 Set_Has_Private_View (N);
13822 -- If it is a derivation of a private type in a context where no
13823 -- full view is needed, nothing to do either.
13825 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13828 -- Otherwise mark the type for flipping and use the full view when
13832 Set_Has_Private_View (N);
13834 if Present (Full_View (Typ)) then
13835 Set_Etype (N2, Full_View (Typ));
13838 end Set_Global_Type;
13844 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13849 while Is_Child_Unit (Par) loop
13850 Par := Scope (Par);
13856 -- Start of processing for Reset_Entity
13859 N2 := Get_Associated_Node (N);
13862 if Present (E) then
13864 -- If the node is an entry call to an entry in an enclosing task,
13865 -- it is rewritten as a selected component. No global entity to
13866 -- preserve in this case, since the expansion will be redone in
13869 if not Nkind_In (E, N_Defining_Character_Literal,
13870 N_Defining_Identifier,
13871 N_Defining_Operator_Symbol)
13873 Set_Associated_Node (N, Empty);
13874 Set_Etype (N, Empty);
13878 -- If the entity is an itype created as a subtype of an access
13879 -- type with a null exclusion restore source entity for proper
13880 -- visibility. The itype will be created anew in the instance.
13883 and then Ekind (E) = E_Access_Subtype
13884 and then Is_Entity_Name (N)
13885 and then Chars (Etype (E)) = Chars (N)
13888 Set_Entity (N2, E);
13892 if Is_Global (E) then
13894 -- If the entity is a package renaming that is the prefix of
13895 -- an expanded name, it has been rewritten as the renamed
13896 -- package, which is necessary semantically but complicates
13897 -- ASIS tree traversal, so we recover the original entity to
13898 -- expose the renaming. Take into account that the context may
13899 -- be a nested generic, that the original node may itself have
13900 -- an associated node that had better be an entity, and that
13901 -- the current node is still a selected component.
13903 if Ekind (E) = E_Package
13904 and then Nkind (N) = N_Selected_Component
13905 and then Nkind (Parent (N)) = N_Expanded_Name
13906 and then Present (Original_Node (N2))
13907 and then Is_Entity_Name (Original_Node (N2))
13908 and then Present (Entity (Original_Node (N2)))
13910 if Is_Global (Entity (Original_Node (N2))) then
13911 N2 := Original_Node (N2);
13912 Set_Associated_Node (N, N2);
13913 Set_Global_Type (N, N2);
13915 -- Renaming is local, and will be resolved in instance
13918 Set_Associated_Node (N, Empty);
13919 Set_Etype (N, Empty);
13923 Set_Global_Type (N, N2);
13926 elsif Nkind (N) = N_Op_Concat
13927 and then Is_Generic_Type (Etype (N2))
13928 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13930 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13931 and then Is_Intrinsic_Subprogram (E)
13935 -- Entity is local. Mark generic node as unresolved. Note that now
13936 -- it does not have an entity.
13939 Set_Associated_Node (N, Empty);
13940 Set_Etype (N, Empty);
13943 if Nkind (Parent (N)) in N_Generic_Instantiation
13944 and then N = Name (Parent (N))
13946 Save_Global_Defaults (Parent (N), Parent (N2));
13949 elsif Nkind (Parent (N)) = N_Selected_Component
13950 and then Nkind (Parent (N2)) = N_Expanded_Name
13952 if Is_Global (Entity (Parent (N2))) then
13953 Change_Selected_Component_To_Expanded_Name (Parent (N));
13954 Set_Associated_Node (Parent (N), Parent (N2));
13955 Set_Global_Type (Parent (N), Parent (N2));
13956 Save_Entity_Descendants (N);
13958 -- If this is a reference to the current generic entity, replace
13959 -- by the name of the generic homonym of the current package. This
13960 -- is because in an instantiation Par.P.Q will not resolve to the
13961 -- name of the instance, whose enclosing scope is not necessarily
13962 -- Par. We use the generic homonym rather that the name of the
13963 -- generic itself because it may be hidden by a local declaration.
13965 elsif In_Open_Scopes (Entity (Parent (N2)))
13967 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13969 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13970 Rewrite (Parent (N),
13971 Make_Identifier (Sloc (N),
13973 Chars (Generic_Homonym (Entity (Parent (N2))))));
13975 Rewrite (Parent (N),
13976 Make_Identifier (Sloc (N),
13977 Chars => Chars (Selector_Name (Parent (N2)))));
13981 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13982 and then Parent (N) = Name (Parent (Parent (N)))
13984 Save_Global_Defaults
13985 (Parent (Parent (N)), Parent (Parent (N2)));
13988 -- A selected component may denote a static constant that has been
13989 -- folded. If the static constant is global to the generic, capture
13990 -- its value. Otherwise the folding will happen in any instantiation.
13992 elsif Nkind (Parent (N)) = N_Selected_Component
13993 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13995 if Present (Entity (Original_Node (Parent (N2))))
13996 and then Is_Global (Entity (Original_Node (Parent (N2))))
13998 Rewrite (Parent (N), New_Copy (Parent (N2)));
13999 Set_Analyzed (Parent (N), False);
14002 -- A selected component may be transformed into a parameterless
14003 -- function call. If the called entity is global, rewrite the node
14004 -- appropriately, i.e. as an extended name for the global entity.
14006 elsif Nkind (Parent (N)) = N_Selected_Component
14007 and then Nkind (Parent (N2)) = N_Function_Call
14008 and then N = Selector_Name (Parent (N))
14010 if No (Parameter_Associations (Parent (N2))) then
14011 if Is_Global (Entity (Name (Parent (N2)))) then
14012 Change_Selected_Component_To_Expanded_Name (Parent (N));
14013 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14014 Set_Global_Type (Parent (N), Name (Parent (N2)));
14015 Save_Entity_Descendants (N);
14018 Set_Is_Prefixed_Call (Parent (N));
14019 Set_Associated_Node (N, Empty);
14020 Set_Etype (N, Empty);
14023 -- In Ada 2005, X.F may be a call to a primitive operation,
14024 -- rewritten as F (X). This rewriting will be done again in an
14025 -- instance, so keep the original node. Global entities will be
14026 -- captured as for other constructs. Indicate that this must
14027 -- resolve as a call, to prevent accidental overloading in the
14028 -- instance, if both a component and a primitive operation appear
14032 Set_Is_Prefixed_Call (Parent (N));
14035 -- Entity is local. Reset in generic unit, so that node is resolved
14036 -- anew at the point of instantiation.
14039 Set_Associated_Node (N, Empty);
14040 Set_Etype (N, Empty);
14044 -----------------------------
14045 -- Save_Entity_Descendants --
14046 -----------------------------
14048 procedure Save_Entity_Descendants (N : Node_Id) is
14051 when N_Binary_Op =>
14052 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14053 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14056 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14058 when N_Expanded_Name |
14059 N_Selected_Component =>
14060 Save_Global_Descendant (Union_Id (Prefix (N)));
14061 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14063 when N_Identifier |
14064 N_Character_Literal |
14065 N_Operator_Symbol =>
14069 raise Program_Error;
14071 end Save_Entity_Descendants;
14073 --------------------------
14074 -- Save_Global_Defaults --
14075 --------------------------
14077 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14078 Loc : constant Source_Ptr := Sloc (N1);
14079 Assoc2 : constant List_Id := Generic_Associations (N2);
14080 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14087 Actual : Entity_Id;
14090 Assoc1 := Generic_Associations (N1);
14092 if Present (Assoc1) then
14093 Act1 := First (Assoc1);
14096 Set_Generic_Associations (N1, New_List);
14097 Assoc1 := Generic_Associations (N1);
14100 if Present (Assoc2) then
14101 Act2 := First (Assoc2);
14106 while Present (Act1) and then Present (Act2) loop
14111 -- Find the associations added for default subprograms
14113 if Present (Act2) then
14114 while Nkind (Act2) /= N_Generic_Association
14115 or else No (Entity (Selector_Name (Act2)))
14116 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14121 -- Add a similar association if the default is global. The
14122 -- renaming declaration for the actual has been analyzed, and
14123 -- its alias is the program it renames. Link the actual in the
14124 -- original generic tree with the node in the analyzed tree.
14126 while Present (Act2) loop
14127 Subp := Entity (Selector_Name (Act2));
14128 Def := Explicit_Generic_Actual_Parameter (Act2);
14130 -- Following test is defence against rubbish errors
14132 if No (Alias (Subp)) then
14136 -- Retrieve the resolved actual from the renaming declaration
14137 -- created for the instantiated formal.
14139 Actual := Entity (Name (Parent (Parent (Subp))));
14140 Set_Entity (Def, Actual);
14141 Set_Etype (Def, Etype (Actual));
14143 if Is_Global (Actual) then
14145 Make_Generic_Association (Loc,
14147 New_Occurrence_Of (Subp, Loc),
14148 Explicit_Generic_Actual_Parameter =>
14149 New_Occurrence_Of (Actual, Loc));
14151 Set_Associated_Node
14152 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14154 Append (Ndec, Assoc1);
14156 -- If there are other defaults, add a dummy association in case
14157 -- there are other defaulted formals with the same name.
14159 elsif Present (Next (Act2)) then
14161 Make_Generic_Association (Loc,
14163 New_Occurrence_Of (Subp, Loc),
14164 Explicit_Generic_Actual_Parameter => Empty);
14166 Append (Ndec, Assoc1);
14173 if Nkind (Name (N1)) = N_Identifier
14174 and then Is_Child_Unit (Gen_Id)
14175 and then Is_Global (Gen_Id)
14176 and then Is_Generic_Unit (Scope (Gen_Id))
14177 and then In_Open_Scopes (Scope (Gen_Id))
14179 -- This is an instantiation of a child unit within a sibling, so
14180 -- that the generic parent is in scope. An eventual instance must
14181 -- occur within the scope of an instance of the parent. Make name
14182 -- in instance into an expanded name, to preserve the identifier
14183 -- of the parent, so it can be resolved subsequently.
14185 Rewrite (Name (N2),
14186 Make_Expanded_Name (Loc,
14187 Chars => Chars (Gen_Id),
14188 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14189 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14190 Set_Entity (Name (N2), Gen_Id);
14192 Rewrite (Name (N1),
14193 Make_Expanded_Name (Loc,
14194 Chars => Chars (Gen_Id),
14195 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14196 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14198 Set_Associated_Node (Name (N1), Name (N2));
14199 Set_Associated_Node (Prefix (Name (N1)), Empty);
14200 Set_Associated_Node
14201 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14202 Set_Etype (Name (N1), Etype (Gen_Id));
14204 end Save_Global_Defaults;
14206 ----------------------------
14207 -- Save_Global_Descendant --
14208 ----------------------------
14210 procedure Save_Global_Descendant (D : Union_Id) is
14214 if D in Node_Range then
14215 if D = Union_Id (Empty) then
14218 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14219 Save_References (Node_Id (D));
14222 elsif D in List_Range then
14223 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14227 N1 := First (List_Id (D));
14228 while Present (N1) loop
14229 Save_References (N1);
14234 -- Element list or other non-node field, nothing to do
14239 end Save_Global_Descendant;
14241 ---------------------
14242 -- Save_References --
14243 ---------------------
14245 -- This is the recursive procedure that does the work once the enclosing
14246 -- generic scope has been established. We have to treat specially a
14247 -- number of node rewritings that are required by semantic processing
14248 -- and which change the kind of nodes in the generic copy: typically
14249 -- constant-folding, replacing an operator node by a string literal, or
14250 -- a selected component by an expanded name. In each of those cases, the
14251 -- transformation is propagated to the generic unit.
14253 procedure Save_References (N : Node_Id) is
14254 Loc : constant Source_Ptr := Sloc (N);
14256 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14257 -- Determine whether arbitrary node Nod requires delayed capture of
14258 -- global references within its aspect specifications.
14260 procedure Save_References_In_Aggregate (N : Node_Id);
14261 -- Save all global references in [extension] aggregate node N
14263 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14264 -- Save all global references in a character literal or operator
14265 -- symbol denoted by N.
14267 procedure Save_References_In_Descendants (N : Node_Id);
14268 -- Save all global references in all descendants of node N
14270 procedure Save_References_In_Identifier (N : Node_Id);
14271 -- Save all global references in identifier node N
14273 procedure Save_References_In_Operator (N : Node_Id);
14274 -- Save all global references in operator node N
14276 procedure Save_References_In_Pragma (Prag : Node_Id);
14277 -- Save all global references found within the expression of pragma
14280 ---------------------------
14281 -- Requires_Delayed_Save --
14282 ---------------------------
14284 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14286 -- Generic packages and subprograms require delayed capture of
14287 -- global references within their aspects due to the timing of
14288 -- annotation analysis.
14290 if Nkind_In (Nod, N_Generic_Package_Declaration,
14291 N_Generic_Subprogram_Declaration,
14293 N_Package_Body_Stub,
14295 N_Subprogram_Body_Stub)
14297 -- Since the capture of global references is done on the
14298 -- unanalyzed generic template, there is no information around
14299 -- to infer the context. Use the Associated_Entity linkages to
14300 -- peek into the analyzed generic copy and determine what the
14301 -- template corresponds to.
14303 if Nod = Templ then
14305 Is_Generic_Declaration_Or_Body
14306 (Unit_Declaration_Node
14307 (Associated_Entity (Defining_Entity (Nod))));
14309 -- Otherwise the generic unit being processed is not the top
14310 -- level template. It is safe to capture of global references
14311 -- within the generic unit because at this point the top level
14312 -- copy is fully analyzed.
14318 -- Otherwise capture the global references without interference
14323 end Requires_Delayed_Save;
14325 ----------------------------------
14326 -- Save_References_In_Aggregate --
14327 ----------------------------------
14329 procedure Save_References_In_Aggregate (N : Node_Id) is
14331 Qual : Node_Id := Empty;
14332 Typ : Entity_Id := Empty;
14334 use Atree.Unchecked_Access;
14335 -- This code section is part of implementing an untyped tree
14336 -- traversal, so it needs direct access to node fields.
14339 N2 := Get_Associated_Node (N);
14341 if Present (N2) then
14344 -- In an instance within a generic, use the name of the actual
14345 -- and not the original generic parameter. If the actual is
14346 -- global in the current generic it must be preserved for its
14349 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14350 and then Present (Generic_Parent_Type (Parent (Typ)))
14352 Typ := Base_Type (Typ);
14353 Set_Etype (N2, Typ);
14357 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14358 Set_Associated_Node (N, Empty);
14360 -- If the aggregate is an actual in a call, it has been
14361 -- resolved in the current context, to some local type. The
14362 -- enclosing call may have been disambiguated by the aggregate,
14363 -- and this disambiguation might fail at instantiation time
14364 -- because the type to which the aggregate did resolve is not
14365 -- preserved. In order to preserve some of this information,
14366 -- wrap the aggregate in a qualified expression, using the id
14367 -- of its type. For further disambiguation we qualify the type
14368 -- name with its scope (if visible) because both id's will have
14369 -- corresponding entities in an instance. This resolves most of
14370 -- the problems with missing type information on aggregates in
14374 and then Nkind (N2) = Nkind (N)
14375 and then Nkind (Parent (N2)) in N_Subprogram_Call
14376 and then Present (Typ)
14377 and then Comes_From_Source (Typ)
14379 Nam := Make_Identifier (Loc, Chars (Typ));
14381 if Is_Immediately_Visible (Scope (Typ)) then
14383 Make_Selected_Component (Loc,
14385 Make_Identifier (Loc, Chars (Scope (Typ))),
14386 Selector_Name => Nam);
14390 Make_Qualified_Expression (Loc,
14391 Subtype_Mark => Nam,
14392 Expression => Relocate_Node (N));
14396 Save_Global_Descendant (Field1 (N));
14397 Save_Global_Descendant (Field2 (N));
14398 Save_Global_Descendant (Field3 (N));
14399 Save_Global_Descendant (Field5 (N));
14401 if Present (Qual) then
14404 end Save_References_In_Aggregate;
14406 ----------------------------------------------
14407 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14408 ----------------------------------------------
14410 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14412 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14415 elsif Nkind (N) = N_Operator_Symbol
14416 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14418 Change_Operator_Symbol_To_String_Literal (N);
14420 end Save_References_In_Char_Lit_Or_Op_Symbol;
14422 ------------------------------------
14423 -- Save_References_In_Descendants --
14424 ------------------------------------
14426 procedure Save_References_In_Descendants (N : Node_Id) is
14427 use Atree.Unchecked_Access;
14428 -- This code section is part of implementing an untyped tree
14429 -- traversal, so it needs direct access to node fields.
14432 Save_Global_Descendant (Field1 (N));
14433 Save_Global_Descendant (Field2 (N));
14434 Save_Global_Descendant (Field3 (N));
14435 Save_Global_Descendant (Field4 (N));
14436 Save_Global_Descendant (Field5 (N));
14437 end Save_References_In_Descendants;
14439 -----------------------------------
14440 -- Save_References_In_Identifier --
14441 -----------------------------------
14443 procedure Save_References_In_Identifier (N : Node_Id) is
14445 -- The node did not undergo a transformation
14447 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14449 -- If this is a discriminant reference, always save it. It is
14450 -- used in the instance to find the corresponding discriminant
14451 -- positionally rather than by name.
14453 Set_Original_Discriminant
14454 (N, Original_Discriminant (Get_Associated_Node (N)));
14457 -- The analysis of the generic copy transformed the identifier
14458 -- into another construct. Propagate the changes to the template.
14461 N2 := Get_Associated_Node (N);
14463 -- The identifier denotes a call to a parameterless function.
14464 -- Mark the node as resolved when the function is external.
14466 if Nkind (N2) = N_Function_Call then
14467 E := Entity (Name (N2));
14469 if Present (E) and then Is_Global (E) then
14470 Set_Etype (N, Etype (N2));
14472 Set_Associated_Node (N, Empty);
14473 Set_Etype (N, Empty);
14476 -- The identifier denotes a named number that was constant
14477 -- folded. Preserve the original name for ASIS and undo the
14478 -- constant folding which will be repeated in the instance.
14480 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14481 and then Is_Entity_Name (Original_Node (N2))
14483 Set_Associated_Node (N, Original_Node (N2));
14486 -- The identifier resolved to a string literal. Propagate this
14487 -- information to the generic template.
14489 elsif Nkind (N2) = N_String_Literal then
14490 Rewrite (N, New_Copy (N2));
14492 -- The identifier is rewritten as a dereference if it is the
14493 -- prefix of an implicit dereference. Preserve the original
14494 -- tree as the analysis of the instance will expand the node
14495 -- again, but preserve the resolved entity if it is global.
14497 elsif Nkind (N2) = N_Explicit_Dereference then
14498 if Is_Entity_Name (Prefix (N2))
14499 and then Present (Entity (Prefix (N2)))
14500 and then Is_Global (Entity (Prefix (N2)))
14502 Set_Associated_Node (N, Prefix (N2));
14504 elsif Nkind (Prefix (N2)) = N_Function_Call
14505 and then Present (Entity (Name (Prefix (N2))))
14506 and then Is_Global (Entity (Name (Prefix (N2))))
14509 Make_Explicit_Dereference (Loc,
14511 Make_Function_Call (Loc,
14514 (Entity (Name (Prefix (N2))), Loc))));
14517 Set_Associated_Node (N, Empty);
14518 Set_Etype (N, Empty);
14521 -- The subtype mark of a nominally unconstrained object is
14522 -- rewritten as a subtype indication using the bounds of the
14523 -- expression. Recover the original subtype mark.
14525 elsif Nkind (N2) = N_Subtype_Indication
14526 and then Is_Entity_Name (Original_Node (N2))
14528 Set_Associated_Node (N, Original_Node (N2));
14532 end Save_References_In_Identifier;
14534 ---------------------------------
14535 -- Save_References_In_Operator --
14536 ---------------------------------
14538 procedure Save_References_In_Operator (N : Node_Id) is
14540 -- The node did not undergo a transformation
14542 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14543 if Nkind (N) = N_Op_Concat then
14544 Set_Is_Component_Left_Opnd (N,
14545 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14547 Set_Is_Component_Right_Opnd (N,
14548 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14553 -- The analysis of the generic copy transformed the operator into
14554 -- some other construct. Propagate the changes to the template.
14557 N2 := Get_Associated_Node (N);
14559 -- The operator resoved to a function call
14561 if Nkind (N2) = N_Function_Call then
14562 E := Entity (Name (N2));
14564 if Present (E) and then Is_Global (E) then
14565 Set_Etype (N, Etype (N2));
14567 Set_Associated_Node (N, Empty);
14568 Set_Etype (N, Empty);
14571 -- The operator was folded into a literal
14573 elsif Nkind_In (N2, N_Integer_Literal,
14577 if Present (Original_Node (N2))
14578 and then Nkind (Original_Node (N2)) = Nkind (N)
14580 -- Operation was constant-folded. Whenever possible,
14581 -- recover semantic information from unfolded node,
14584 Set_Associated_Node (N, Original_Node (N2));
14586 if Nkind (N) = N_Op_Concat then
14587 Set_Is_Component_Left_Opnd (N,
14588 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14589 Set_Is_Component_Right_Opnd (N,
14590 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14595 -- Propagate the constant folding back to the template
14598 Rewrite (N, New_Copy (N2));
14599 Set_Analyzed (N, False);
14602 -- The operator was folded into an enumeration literal. Retain
14603 -- the entity to avoid spurious ambiguities if it is overloaded
14604 -- at the point of instantiation or inlining.
14606 elsif Nkind (N2) = N_Identifier
14607 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14609 Rewrite (N, New_Copy (N2));
14610 Set_Analyzed (N, False);
14614 -- Complete the operands check if node has not been constant
14617 if Nkind (N) in N_Op then
14618 Save_Entity_Descendants (N);
14620 end Save_References_In_Operator;
14622 -------------------------------
14623 -- Save_References_In_Pragma --
14624 -------------------------------
14626 procedure Save_References_In_Pragma (Prag : Node_Id) is
14628 Do_Save : Boolean := True;
14630 use Atree.Unchecked_Access;
14631 -- This code section is part of implementing an untyped tree
14632 -- traversal, so it needs direct access to node fields.
14635 -- Do not save global references in pragmas generated from aspects
14636 -- because the pragmas will be regenerated at instantiation time.
14638 if From_Aspect_Specification (Prag) then
14641 -- The capture of global references within contract-related source
14642 -- pragmas associated with generic packages, subprograms or their
14643 -- respective bodies must be delayed due to timing of annotation
14644 -- analysis. Global references are still captured in routine
14645 -- Save_Global_References_In_Contract.
14647 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14648 if Is_Package_Contract_Annotation (Prag) then
14649 Context := Find_Related_Package_Or_Body (Prag);
14652 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14653 Context := Find_Related_Subprogram_Or_Body (Prag);
14656 -- The use of Original_Node accounts for the case when the
14657 -- related context is generic template.
14659 if Requires_Delayed_Save (Original_Node (Context)) then
14664 -- For all other cases, save all global references within the
14665 -- descendants, but skip the following semantic fields:
14667 -- Field1 - Next_Pragma
14668 -- Field3 - Corresponding_Aspect
14669 -- Field5 - Next_Rep_Item
14672 Save_Global_Descendant (Field2 (Prag));
14673 Save_Global_Descendant (Field4 (Prag));
14675 end Save_References_In_Pragma;
14677 -- Start of processing for Save_References
14685 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14686 Save_References_In_Aggregate (N);
14688 -- Character literals, operator symbols
14690 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14691 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14693 -- Defining identifiers
14695 elsif Nkind (N) in N_Entity then
14700 elsif Nkind (N) = N_Identifier then
14701 Save_References_In_Identifier (N);
14705 elsif Nkind (N) in N_Op then
14706 Save_References_In_Operator (N);
14710 elsif Nkind (N) = N_Pragma then
14711 Save_References_In_Pragma (N);
14714 Save_References_In_Descendants (N);
14717 -- Save all global references found within the aspect specifications
14718 -- of the related node.
14720 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14722 -- The capture of global references within aspects associated with
14723 -- generic packages, subprograms or their bodies must be delayed
14724 -- due to timing of annotation analysis. Global references are
14725 -- still captured in routine Save_Global_References_In_Contract.
14727 if Requires_Delayed_Save (N) then
14730 -- Otherwise save all global references within the aspects
14733 Save_Global_References_In_Aspects (N);
14736 end Save_References;
14738 -- Start of processing for Save_Global_References
14741 Gen_Scope := Current_Scope;
14743 -- If the generic unit is a child unit, references to entities in the
14744 -- parent are treated as local, because they will be resolved anew in
14745 -- the context of the instance of the parent.
14747 while Is_Child_Unit (Gen_Scope)
14748 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14750 Gen_Scope := Scope (Gen_Scope);
14753 Save_References (Templ);
14754 end Save_Global_References;
14756 ---------------------------------------
14757 -- Save_Global_References_In_Aspects --
14758 ---------------------------------------
14760 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14765 Asp := First (Aspect_Specifications (N));
14766 while Present (Asp) loop
14767 Expr := Expression (Asp);
14769 if Present (Expr) then
14770 Save_Global_References (Expr);
14775 end Save_Global_References_In_Aspects;
14777 ----------------------------------------
14778 -- Save_Global_References_In_Contract --
14779 ----------------------------------------
14781 procedure Save_Global_References_In_Contract
14783 Gen_Id : Entity_Id)
14785 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14786 -- Save all global references in contract-related source pragmas found
14787 -- in the list starting with pragma First_Prag.
14789 ------------------------------------
14790 -- Save_Global_References_In_List --
14791 ------------------------------------
14793 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14797 Prag := First_Prag;
14798 while Present (Prag) loop
14799 if Is_Generic_Contract_Pragma (Prag) then
14800 Save_Global_References (Prag);
14803 Prag := Next_Pragma (Prag);
14805 end Save_Global_References_In_List;
14809 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14811 -- Start of processing for Save_Global_References_In_Contract
14814 -- The entity of the analyzed generic copy must be on the scope stack
14815 -- to ensure proper detection of global references.
14817 Push_Scope (Gen_Id);
14819 if Permits_Aspect_Specifications (Templ)
14820 and then Has_Aspects (Templ)
14822 Save_Global_References_In_Aspects (Templ);
14825 if Present (Items) then
14826 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14827 Save_Global_References_In_List (Contract_Test_Cases (Items));
14828 Save_Global_References_In_List (Classifications (Items));
14832 end Save_Global_References_In_Contract;
14834 --------------------------------------
14835 -- Set_Copied_Sloc_For_Inlined_Body --
14836 --------------------------------------
14838 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14840 Create_Instantiation_Source (N, E, True, S_Adjustment);
14841 end Set_Copied_Sloc_For_Inlined_Body;
14843 ---------------------
14844 -- Set_Instance_Of --
14845 ---------------------
14847 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14849 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14850 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14851 Generic_Renamings.Increment_Last;
14852 end Set_Instance_Of;
14854 --------------------
14855 -- Set_Next_Assoc --
14856 --------------------
14858 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14860 Generic_Renamings.Table (E).Next_In_HTable := Next;
14861 end Set_Next_Assoc;
14863 -------------------
14864 -- Start_Generic --
14865 -------------------
14867 procedure Start_Generic is
14869 -- ??? More things could be factored out in this routine.
14870 -- Should probably be done at a later stage.
14872 Generic_Flags.Append (Inside_A_Generic);
14873 Inside_A_Generic := True;
14875 Expander_Mode_Save_And_Set (False);
14878 ----------------------
14879 -- Set_Instance_Env --
14880 ----------------------
14882 procedure Set_Instance_Env
14883 (Gen_Unit : Entity_Id;
14884 Act_Unit : Entity_Id)
14886 Assertion_Status : constant Boolean := Assertions_Enabled;
14887 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14888 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14891 -- Regardless of the current mode, predefined units are analyzed in the
14892 -- most current Ada mode, and earlier version Ada checks do not apply
14893 -- to predefined units. Nothing needs to be done for non-internal units.
14894 -- These are always analyzed in the current mode.
14896 if Is_Internal_File_Name
14897 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14898 Renamings_Included => True)
14900 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14902 -- In Ada2012 we may want to enable assertions in an instance of a
14903 -- predefined unit, in which case we need to preserve the current
14904 -- setting for the Assertions_Enabled flag. This will become more
14905 -- critical when pre/postconditions are added to predefined units,
14906 -- as is already the case for some numeric libraries.
14908 if Ada_Version >= Ada_2012 then
14909 Assertions_Enabled := Assertion_Status;
14912 -- SPARK_Mode for an instance is the one applicable at the point of
14915 SPARK_Mode := Save_SPARK_Mode;
14916 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14918 -- Make sure dynamic elaboration checks are off in SPARK Mode
14920 if SPARK_Mode = On then
14921 Dynamic_Elaboration_Checks := False;
14925 Current_Instantiated_Parent :=
14926 (Gen_Id => Gen_Unit,
14927 Act_Id => Act_Unit,
14928 Next_In_HTable => Assoc_Null);
14929 end Set_Instance_Env;
14935 procedure Switch_View (T : Entity_Id) is
14936 BT : constant Entity_Id := Base_Type (T);
14937 Priv_Elmt : Elmt_Id := No_Elmt;
14938 Priv_Sub : Entity_Id;
14941 -- T may be private but its base type may have been exchanged through
14942 -- some other occurrence, in which case there is nothing to switch
14943 -- besides T itself. Note that a private dependent subtype of a private
14944 -- type might not have been switched even if the base type has been,
14945 -- because of the last branch of Check_Private_View (see comment there).
14947 if not Is_Private_Type (BT) then
14948 Prepend_Elmt (Full_View (T), Exchanged_Views);
14949 Exchange_Declarations (T);
14953 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14955 if Present (Full_View (BT)) then
14956 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14957 Exchange_Declarations (BT);
14960 while Present (Priv_Elmt) loop
14961 Priv_Sub := (Node (Priv_Elmt));
14963 -- We avoid flipping the subtype if the Etype of its full view is
14964 -- private because this would result in a malformed subtype. This
14965 -- occurs when the Etype of the subtype full view is the full view of
14966 -- the base type (and since the base types were just switched, the
14967 -- subtype is pointing to the wrong view). This is currently the case
14968 -- for tagged record types, access types (maybe more?) and needs to
14969 -- be resolved. ???
14971 if Present (Full_View (Priv_Sub))
14972 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14974 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14975 Exchange_Declarations (Priv_Sub);
14978 Next_Elmt (Priv_Elmt);
14986 function True_Parent (N : Node_Id) return Node_Id is
14988 if Nkind (Parent (N)) = N_Subunit then
14989 return Parent (Corresponding_Stub (Parent (N)));
14995 -----------------------------
14996 -- Valid_Default_Attribute --
14997 -----------------------------
14999 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15000 Attr_Id : constant Attribute_Id :=
15001 Get_Attribute_Id (Attribute_Name (Def));
15002 T : constant Entity_Id := Entity (Prefix (Def));
15003 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15009 if No (T) or else T = Any_Id then
15014 F := First_Formal (Nam);
15015 while Present (F) loop
15016 Num_F := Num_F + 1;
15021 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15022 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15023 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15024 Attribute_Unbiased_Rounding =>
15027 and then Is_Floating_Point_Type (T);
15029 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15030 Attribute_Value | Attribute_Wide_Image |
15031 Attribute_Wide_Value =>
15032 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15034 when Attribute_Max | Attribute_Min =>
15035 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15037 when Attribute_Input =>
15038 OK := (Is_Fun and then Num_F = 1);
15040 when Attribute_Output | Attribute_Read | Attribute_Write =>
15041 OK := (not Is_Fun and then Num_F = 2);
15049 ("attribute reference has wrong profile for subprogram", Def);
15051 end Valid_Default_Attribute;