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; Inst : Entity_Id := Empty);
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 -- If Inst is present, it is the entity of the package instance. This
835 -- entity is marked as having a limited_view actual when some actual is
836 -- a limited view. This is used to place the instance body properly.
838 procedure Remove_Parent (In_Body : Boolean := False);
839 -- Reverse effect after instantiation of child is complete
841 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
842 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
845 procedure Save_Global_References_In_Aspects (N : Node_Id);
846 -- Save all global references found within the expressions of all aspects
847 -- that appear on node N.
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
880 type Assoc_Ptr is new Int;
882 Assoc_Null : constant Assoc_Ptr := -1;
887 Next_In_HTable : Assoc_Ptr;
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
903 -- Hash table for associations
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
921 Get_Key => Get_Gen_Id,
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1027 procedure Abandon_Instantiation (N : Node_Id) is
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1033 --------------------------
1034 -- Analyze_Associations --
1035 --------------------------
1037 function Analyze_Associations
1040 F_Copy : List_Id) return List_Id
1042 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1043 Assoc : constant List_Id := New_List;
1044 Default_Actuals : constant List_Id := New_List;
1045 Gen_Unit : constant Entity_Id :=
1046 Defining_Entity (Parent (F_Copy));
1050 Analyzed_Formal : Node_Id;
1051 First_Named : Node_Id := Empty;
1055 Saved_Formal : Node_Id;
1057 Default_Formals : constant List_Id := New_List;
1058 -- If an Others_Choice is present, some of the formals may be defaulted.
1059 -- To simplify the treatment of visibility in an instance, we introduce
1060 -- individual defaults for each such formal. These defaults are
1061 -- appended to the list of associations and replace the Others_Choice.
1063 Found_Assoc : Node_Id;
1064 -- Association for the current formal being match. Empty if there are
1065 -- no remaining actuals, or if there is no named association with the
1066 -- name of the formal.
1068 Is_Named_Assoc : Boolean;
1069 Num_Matched : Int := 0;
1070 Num_Actuals : Int := 0;
1072 Others_Present : Boolean := False;
1073 Others_Choice : Node_Id := Empty;
1074 -- In Ada 2005, indicates partial parameterization of a formal
1075 -- package. As usual an other association must be last in the list.
1077 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1078 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1079 -- cannot have a named association for it. AI05-0025 extends this rule
1080 -- to formals of formal packages by AI05-0025, and it also applies to
1081 -- box-initialized formals.
1083 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1084 -- Determine whether the parameter types and the return type of Subp
1085 -- are fully defined at the point of instantiation.
1087 function Matching_Actual
1089 A_F : Entity_Id) return Node_Id;
1090 -- Find actual that corresponds to a given a formal parameter. If the
1091 -- actuals are positional, return the next one, if any. If the actuals
1092 -- are named, scan the parameter associations to find the right one.
1093 -- A_F is the corresponding entity in the analyzed generic,which is
1094 -- placed on the selector name for ASIS use.
1096 -- In Ada 2005, a named association may be given with a box, in which
1097 -- case Matching_Actual sets Found_Assoc to the generic association,
1098 -- but return Empty for the actual itself. In this case the code below
1099 -- creates a corresponding declaration for the formal.
1101 function Partial_Parameterization return Boolean;
1102 -- Ada 2005: if no match is found for a given formal, check if the
1103 -- association for it includes a box, or whether the associations
1104 -- include an Others clause.
1106 procedure Process_Default (F : Entity_Id);
1107 -- Add a copy of the declaration of generic formal F to the list of
1108 -- associations, and add an explicit box association for F if there
1109 -- is none yet, and the default comes from an Others_Choice.
1111 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1112 -- Determine whether Subp renames one of the subprograms defined in the
1113 -- generated package Standard.
1115 procedure Set_Analyzed_Formal;
1116 -- Find the node in the generic copy that corresponds to a given formal.
1117 -- The semantic information on this node is used to perform legality
1118 -- checks on the actuals. Because semantic analysis can introduce some
1119 -- anonymous entities or modify the declaration node itself, the
1120 -- correspondence between the two lists is not one-one. In addition to
1121 -- anonymous types, the presence a formal equality will introduce an
1122 -- implicit declaration for the corresponding inequality.
1124 ----------------------------------------
1125 -- Check_Overloaded_Formal_Subprogram --
1126 ----------------------------------------
1128 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1129 Temp_Formal : Entity_Id;
1132 Temp_Formal := First (Formals);
1133 while Present (Temp_Formal) loop
1134 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1135 and then Temp_Formal /= Formal
1137 Chars (Defining_Unit_Name (Specification (Formal))) =
1138 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1140 if Present (Found_Assoc) then
1142 ("named association not allowed for overloaded formal",
1147 ("named association not allowed for overloaded formal",
1151 Abandon_Instantiation (Instantiation_Node);
1156 end Check_Overloaded_Formal_Subprogram;
1158 -------------------------------
1159 -- Has_Fully_Defined_Profile --
1160 -------------------------------
1162 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1163 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1164 -- Determine whethet type Typ is fully defined
1166 ---------------------------
1167 -- Is_Fully_Defined_Type --
1168 ---------------------------
1170 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1172 -- A private type without a full view is not fully defined
1174 if Is_Private_Type (Typ)
1175 and then No (Full_View (Typ))
1179 -- An incomplete type is never fully defined
1181 elsif Is_Incomplete_Type (Typ) then
1184 -- All other types are fully defined
1189 end Is_Fully_Defined_Type;
1191 -- Local declarations
1195 -- Start of processing for Has_Fully_Defined_Profile
1198 -- Check the parameters
1200 Param := First_Formal (Subp);
1201 while Present (Param) loop
1202 if not Is_Fully_Defined_Type (Etype (Param)) then
1206 Next_Formal (Param);
1209 -- Check the return type
1211 return Is_Fully_Defined_Type (Etype (Subp));
1212 end Has_Fully_Defined_Profile;
1214 ---------------------
1215 -- Matching_Actual --
1216 ---------------------
1218 function Matching_Actual
1220 A_F : Entity_Id) return Node_Id
1226 Is_Named_Assoc := False;
1228 -- End of list of purely positional parameters
1230 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1231 Found_Assoc := Empty;
1234 -- Case of positional parameter corresponding to current formal
1236 elsif No (Selector_Name (Actual)) then
1237 Found_Assoc := Actual;
1238 Act := Explicit_Generic_Actual_Parameter (Actual);
1239 Num_Matched := Num_Matched + 1;
1242 -- Otherwise scan list of named actuals to find the one with the
1243 -- desired name. All remaining actuals have explicit names.
1246 Is_Named_Assoc := True;
1247 Found_Assoc := Empty;
1251 while Present (Actual) loop
1252 if Chars (Selector_Name (Actual)) = Chars (F) then
1253 Set_Entity (Selector_Name (Actual), A_F);
1254 Set_Etype (Selector_Name (Actual), Etype (A_F));
1255 Generate_Reference (A_F, Selector_Name (Actual));
1256 Found_Assoc := Actual;
1257 Act := Explicit_Generic_Actual_Parameter (Actual);
1258 Num_Matched := Num_Matched + 1;
1266 -- Reset for subsequent searches. In most cases the named
1267 -- associations are in order. If they are not, we reorder them
1268 -- to avoid scanning twice the same actual. This is not just a
1269 -- question of efficiency: there may be multiple defaults with
1270 -- boxes that have the same name. In a nested instantiation we
1271 -- insert actuals for those defaults, and cannot rely on their
1272 -- names to disambiguate them.
1274 if Actual = First_Named then
1277 elsif Present (Actual) then
1278 Insert_Before (First_Named, Remove_Next (Prev));
1281 Actual := First_Named;
1284 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1285 Set_Used_As_Generic_Actual (Entity (Act));
1289 end Matching_Actual;
1291 ------------------------------
1292 -- Partial_Parameterization --
1293 ------------------------------
1295 function Partial_Parameterization return Boolean is
1297 return Others_Present
1298 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1299 end Partial_Parameterization;
1301 ---------------------
1302 -- Process_Default --
1303 ---------------------
1305 procedure Process_Default (F : Entity_Id) is
1306 Loc : constant Source_Ptr := Sloc (I_Node);
1307 F_Id : constant Entity_Id := Defining_Entity (F);
1313 -- Append copy of formal declaration to associations, and create new
1314 -- defining identifier for it.
1316 Decl := New_Copy_Tree (F);
1317 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1319 if Nkind (F) in N_Formal_Subprogram_Declaration then
1320 Set_Defining_Unit_Name (Specification (Decl), Id);
1323 Set_Defining_Identifier (Decl, Id);
1326 Append (Decl, Assoc);
1328 if No (Found_Assoc) then
1330 Make_Generic_Association (Loc,
1332 New_Occurrence_Of (Id, Loc),
1333 Explicit_Generic_Actual_Parameter => Empty);
1334 Set_Box_Present (Default);
1335 Append (Default, Default_Formals);
1337 end Process_Default;
1339 ---------------------------------
1340 -- Renames_Standard_Subprogram --
1341 ---------------------------------
1343 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1348 while Present (Id) loop
1349 if Scope (Id) = Standard_Standard then
1357 end Renames_Standard_Subprogram;
1359 -------------------------
1360 -- Set_Analyzed_Formal --
1361 -------------------------
1363 procedure Set_Analyzed_Formal is
1367 while Present (Analyzed_Formal) loop
1368 Kind := Nkind (Analyzed_Formal);
1370 case Nkind (Formal) is
1372 when N_Formal_Subprogram_Declaration =>
1373 exit when Kind in N_Formal_Subprogram_Declaration
1376 (Defining_Unit_Name (Specification (Formal))) =
1378 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1380 when N_Formal_Package_Declaration =>
1381 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1382 N_Generic_Package_Declaration,
1383 N_Package_Declaration);
1385 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1389 -- Skip freeze nodes, and nodes inserted to replace
1390 -- unrecognized pragmas.
1393 Kind not in N_Formal_Subprogram_Declaration
1394 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1398 and then Chars (Defining_Identifier (Formal)) =
1399 Chars (Defining_Identifier (Analyzed_Formal));
1402 Next (Analyzed_Formal);
1404 end Set_Analyzed_Formal;
1406 -- Start of processing for Analyze_Associations
1409 Actuals := Generic_Associations (I_Node);
1411 if Present (Actuals) then
1413 -- Check for an Others choice, indicating a partial parameterization
1414 -- for a formal package.
1416 Actual := First (Actuals);
1417 while Present (Actual) loop
1418 if Nkind (Actual) = N_Others_Choice then
1419 Others_Present := True;
1420 Others_Choice := Actual;
1422 if Present (Next (Actual)) then
1423 Error_Msg_N ("others must be last association", Actual);
1426 -- This subprogram is used both for formal packages and for
1427 -- instantiations. For the latter, associations must all be
1430 if Nkind (I_Node) /= N_Formal_Package_Declaration
1431 and then Comes_From_Source (I_Node)
1434 ("others association not allowed in an instance",
1438 -- In any case, nothing to do after the others association
1442 elsif Box_Present (Actual)
1443 and then Comes_From_Source (I_Node)
1444 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1447 ("box association not allowed in an instance", Actual);
1453 -- If named associations are present, save first named association
1454 -- (it may of course be Empty) to facilitate subsequent name search.
1456 First_Named := First (Actuals);
1457 while Present (First_Named)
1458 and then Nkind (First_Named) /= N_Others_Choice
1459 and then No (Selector_Name (First_Named))
1461 Num_Actuals := Num_Actuals + 1;
1466 Named := First_Named;
1467 while Present (Named) loop
1468 if Nkind (Named) /= N_Others_Choice
1469 and then No (Selector_Name (Named))
1471 Error_Msg_N ("invalid positional actual after named one", Named);
1472 Abandon_Instantiation (Named);
1475 -- A named association may lack an actual parameter, if it was
1476 -- introduced for a default subprogram that turns out to be local
1477 -- to the outer instantiation.
1479 if Nkind (Named) /= N_Others_Choice
1480 and then Present (Explicit_Generic_Actual_Parameter (Named))
1482 Num_Actuals := Num_Actuals + 1;
1488 if Present (Formals) then
1489 Formal := First_Non_Pragma (Formals);
1490 Analyzed_Formal := First_Non_Pragma (F_Copy);
1492 if Present (Actuals) then
1493 Actual := First (Actuals);
1495 -- All formals should have default values
1501 while Present (Formal) loop
1502 Set_Analyzed_Formal;
1503 Saved_Formal := Next_Non_Pragma (Formal);
1505 case Nkind (Formal) is
1506 when N_Formal_Object_Declaration =>
1509 (Defining_Identifier (Formal),
1510 Defining_Identifier (Analyzed_Formal));
1512 if No (Match) and then Partial_Parameterization then
1513 Process_Default (Formal);
1517 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1520 -- For a defaulted in_parameter, create an entry in the
1521 -- the list of defaulted actuals, for GNATProve use. Do
1522 -- not included these defaults for an instance nested
1523 -- within a generic, because the defaults are also used
1524 -- in the analysis of the enclosing generic, and only
1525 -- defaulted subprograms are relevant there.
1527 if No (Match) and then not Inside_A_Generic then
1528 Append_To (Default_Actuals,
1529 Make_Generic_Association (Sloc (I_Node),
1532 (Defining_Identifier (Formal), Sloc (I_Node)),
1533 Explicit_Generic_Actual_Parameter =>
1534 New_Copy_Tree (Default_Expression (Formal))));
1538 -- If the object is a call to an expression function, this
1539 -- is a freezing point for it.
1541 if Is_Entity_Name (Match)
1542 and then Present (Entity (Match))
1544 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1545 = N_Expression_Function
1547 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1550 when N_Formal_Type_Declaration =>
1553 (Defining_Identifier (Formal),
1554 Defining_Identifier (Analyzed_Formal));
1557 if Partial_Parameterization then
1558 Process_Default (Formal);
1561 Error_Msg_Sloc := Sloc (Gen_Unit);
1564 Instantiation_Node, Defining_Identifier (Formal));
1566 ("\in instantiation of & declared#",
1567 Instantiation_Node, Gen_Unit);
1568 Abandon_Instantiation (Instantiation_Node);
1575 (Formal, Match, Analyzed_Formal, Assoc),
1578 -- An instantiation is a freeze point for the actuals,
1579 -- unless this is a rewritten formal package, or the
1580 -- formal is an Ada 2012 formal incomplete type.
1582 if Nkind (I_Node) = N_Formal_Package_Declaration
1584 (Ada_Version >= Ada_2012
1586 Ekind (Defining_Identifier (Analyzed_Formal)) =
1592 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1596 -- A remote access-to-class-wide type is not a legal actual
1597 -- for a generic formal of an access type (E.2.2(17/2)).
1598 -- In GNAT an exception to this rule is introduced when
1599 -- the formal is marked as remote using implementation
1600 -- defined aspect/pragma Remote_Access_Type. In that case
1601 -- the actual must be remote as well.
1603 -- If the current instantiation is the construction of a
1604 -- local copy for a formal package the actuals may be
1605 -- defaulted, and there is no matching actual to check.
1607 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1609 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1610 N_Access_To_Object_Definition
1611 and then Present (Match)
1614 Formal_Ent : constant Entity_Id :=
1615 Defining_Identifier (Analyzed_Formal);
1617 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1618 = Is_Remote_Types (Formal_Ent)
1620 -- Remoteness of formal and actual match
1624 elsif Is_Remote_Types (Formal_Ent) then
1626 -- Remote formal, non-remote actual
1629 ("actual for& must be remote", Match, Formal_Ent);
1632 -- Non-remote formal, remote actual
1635 ("actual for& may not be remote",
1641 when N_Formal_Subprogram_Declaration =>
1644 (Defining_Unit_Name (Specification (Formal)),
1645 Defining_Unit_Name (Specification (Analyzed_Formal)));
1647 -- If the formal subprogram has the same name as another
1648 -- formal subprogram of the generic, then a named
1649 -- association is illegal (12.3(9)). Exclude named
1650 -- associations that are generated for a nested instance.
1653 and then Is_Named_Assoc
1654 and then Comes_From_Source (Found_Assoc)
1656 Check_Overloaded_Formal_Subprogram (Formal);
1659 -- If there is no corresponding actual, this may be case
1660 -- of partial parameterization, or else the formal has a
1661 -- default or a box.
1663 if No (Match) and then Partial_Parameterization then
1664 Process_Default (Formal);
1666 if Nkind (I_Node) = N_Formal_Package_Declaration then
1667 Check_Overloaded_Formal_Subprogram (Formal);
1672 Instantiate_Formal_Subprogram
1673 (Formal, Match, Analyzed_Formal));
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package.
1678 if Nkind (I_Node) /= N_Formal_Package_Declaration
1679 and then Nkind (Match) = N_Identifier
1680 and then Is_Subprogram (Entity (Match))
1682 -- The actual subprogram may rename a routine defined
1683 -- in Standard. Avoid freezing such renamings because
1684 -- subprograms coming from Standard cannot be frozen.
1687 not Renames_Standard_Subprogram (Entity (Match))
1689 -- If the actual subprogram comes from a different
1690 -- unit, it is already frozen, either by a body in
1691 -- that unit or by the end of the declarative part
1692 -- of the unit. This check avoids the freezing of
1693 -- subprograms defined in Standard which are used
1694 -- as generic actuals.
1696 and then In_Same_Code_Unit (Entity (Match), I_Node)
1697 and then Has_Fully_Defined_Profile (Entity (Match))
1699 -- Mark the subprogram as having a delayed freeze
1700 -- since this may be an out-of-order action.
1702 Set_Has_Delayed_Freeze (Entity (Match));
1703 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1707 -- If this is a nested generic, preserve default for later
1708 -- instantiations. We do this as well for GNATProve use,
1709 -- so that the list of generic associations is complete.
1711 if No (Match) and then Box_Present (Formal) then
1713 Subp : constant Entity_Id :=
1714 Defining_Unit_Name (Specification (Last (Assoc)));
1717 Append_To (Default_Actuals,
1718 Make_Generic_Association (Sloc (I_Node),
1720 New_Occurrence_Of (Subp, Sloc (I_Node)),
1721 Explicit_Generic_Actual_Parameter =>
1722 New_Occurrence_Of (Subp, Sloc (I_Node))));
1726 when N_Formal_Package_Declaration =>
1729 (Defining_Identifier (Formal),
1730 Defining_Identifier (Original_Node (Analyzed_Formal)));
1733 if Partial_Parameterization then
1734 Process_Default (Formal);
1737 Error_Msg_Sloc := Sloc (Gen_Unit);
1740 Instantiation_Node, Defining_Identifier (Formal));
1742 ("\in instantiation of & declared#",
1743 Instantiation_Node, Gen_Unit);
1745 Abandon_Instantiation (Instantiation_Node);
1751 (Instantiate_Formal_Package
1752 (Formal, Match, Analyzed_Formal),
1756 -- For use type and use package appearing in the generic part,
1757 -- we have already copied them, so we can just move them where
1758 -- they belong (we mustn't recopy them since this would mess up
1759 -- the Sloc values).
1761 when N_Use_Package_Clause |
1762 N_Use_Type_Clause =>
1763 if Nkind (Original_Node (I_Node)) =
1764 N_Formal_Package_Declaration
1766 Append (New_Copy_Tree (Formal), Assoc);
1769 Append (Formal, Assoc);
1773 raise Program_Error;
1777 Formal := Saved_Formal;
1778 Next_Non_Pragma (Analyzed_Formal);
1781 if Num_Actuals > Num_Matched then
1782 Error_Msg_Sloc := Sloc (Gen_Unit);
1784 if Present (Selector_Name (Actual)) then
1786 ("unmatched actual &", Actual, Selector_Name (Actual));
1788 ("\in instantiation of & declared#", Actual, Gen_Unit);
1791 ("unmatched actual in instantiation of & declared#",
1796 elsif Present (Actuals) then
1798 ("too many actuals in generic instantiation", Instantiation_Node);
1801 -- An instantiation freezes all generic actuals. The only exceptions
1802 -- to this are incomplete types and subprograms which are not fully
1803 -- defined at the point of instantiation.
1806 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1808 while Present (Elmt) loop
1809 Freeze_Before (I_Node, Node (Elmt));
1814 -- If there are default subprograms, normalize the tree by adding
1815 -- explicit associations for them. This is required if the instance
1816 -- appears within a generic.
1818 if not Is_Empty_List (Default_Actuals) then
1823 Default := First (Default_Actuals);
1824 while Present (Default) loop
1825 Mark_Rewrite_Insertion (Default);
1829 if No (Actuals) then
1830 Set_Generic_Associations (I_Node, Default_Actuals);
1832 Append_List_To (Actuals, Default_Actuals);
1837 -- If this is a formal package, normalize the parameter list by adding
1838 -- explicit box associations for the formals that are covered by an
1841 if not Is_Empty_List (Default_Formals) then
1842 Append_List (Default_Formals, Formals);
1846 end Analyze_Associations;
1848 -------------------------------
1849 -- Analyze_Formal_Array_Type --
1850 -------------------------------
1852 procedure Analyze_Formal_Array_Type
1853 (T : in out Entity_Id;
1859 -- Treated like a non-generic array declaration, with additional
1864 if Nkind (Def) = N_Constrained_Array_Definition then
1865 DSS := First (Discrete_Subtype_Definitions (Def));
1866 while Present (DSS) loop
1867 if Nkind_In (DSS, N_Subtype_Indication,
1869 N_Attribute_Reference)
1871 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1878 Array_Type_Declaration (T, Def);
1879 Set_Is_Generic_Type (Base_Type (T));
1881 if Ekind (Component_Type (T)) = E_Incomplete_Type
1882 and then No (Full_View (Component_Type (T)))
1884 Error_Msg_N ("premature usage of incomplete type", Def);
1886 -- Check that range constraint is not allowed on the component type
1887 -- of a generic formal array type (AARM 12.5.3(3))
1889 elsif Is_Internal (Component_Type (T))
1890 and then Present (Subtype_Indication (Component_Definition (Def)))
1891 and then Nkind (Original_Node
1892 (Subtype_Indication (Component_Definition (Def)))) =
1893 N_Subtype_Indication
1896 ("in a formal, a subtype indication can only be "
1897 & "a subtype mark (RM 12.5.3(3))",
1898 Subtype_Indication (Component_Definition (Def)));
1901 end Analyze_Formal_Array_Type;
1903 ---------------------------------------------
1904 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1905 ---------------------------------------------
1907 -- As for other generic types, we create a valid type representation with
1908 -- legal but arbitrary attributes, whose values are never considered
1909 -- static. For all scalar types we introduce an anonymous base type, with
1910 -- the same attributes. We choose the corresponding integer type to be
1911 -- Standard_Integer.
1912 -- Here and in other similar routines, the Sloc of the generated internal
1913 -- type must be the same as the sloc of the defining identifier of the
1914 -- formal type declaration, to provide proper source navigation.
1916 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1920 Loc : constant Source_Ptr := Sloc (Def);
1922 Base : constant Entity_Id :=
1924 (E_Decimal_Fixed_Point_Type,
1926 Sloc (Defining_Identifier (Parent (Def))), 'G');
1928 Int_Base : constant Entity_Id := Standard_Integer;
1929 Delta_Val : constant Ureal := Ureal_1;
1930 Digs_Val : constant Uint := Uint_6;
1932 function Make_Dummy_Bound return Node_Id;
1933 -- Return a properly typed universal real literal to use as a bound
1935 ----------------------
1936 -- Make_Dummy_Bound --
1937 ----------------------
1939 function Make_Dummy_Bound return Node_Id is
1940 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1942 Set_Etype (Bound, Universal_Real);
1944 end Make_Dummy_Bound;
1946 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1951 Set_Etype (Base, Base);
1952 Set_Size_Info (Base, Int_Base);
1953 Set_RM_Size (Base, RM_Size (Int_Base));
1954 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1955 Set_Digits_Value (Base, Digs_Val);
1956 Set_Delta_Value (Base, Delta_Val);
1957 Set_Small_Value (Base, Delta_Val);
1958 Set_Scalar_Range (Base,
1960 Low_Bound => Make_Dummy_Bound,
1961 High_Bound => Make_Dummy_Bound));
1963 Set_Is_Generic_Type (Base);
1964 Set_Parent (Base, Parent (Def));
1966 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1967 Set_Etype (T, Base);
1968 Set_Size_Info (T, Int_Base);
1969 Set_RM_Size (T, RM_Size (Int_Base));
1970 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1971 Set_Digits_Value (T, Digs_Val);
1972 Set_Delta_Value (T, Delta_Val);
1973 Set_Small_Value (T, Delta_Val);
1974 Set_Scalar_Range (T, Scalar_Range (Base));
1975 Set_Is_Constrained (T);
1977 Check_Restriction (No_Fixed_Point, Def);
1978 end Analyze_Formal_Decimal_Fixed_Point_Type;
1980 -------------------------------------------
1981 -- Analyze_Formal_Derived_Interface_Type --
1982 -------------------------------------------
1984 procedure Analyze_Formal_Derived_Interface_Type
1989 Loc : constant Source_Ptr := Sloc (Def);
1992 -- Rewrite as a type declaration of a derived type. This ensures that
1993 -- the interface list and primitive operations are properly captured.
1996 Make_Full_Type_Declaration (Loc,
1997 Defining_Identifier => T,
1998 Type_Definition => Def));
2000 Set_Is_Generic_Type (T);
2001 end Analyze_Formal_Derived_Interface_Type;
2003 ---------------------------------
2004 -- Analyze_Formal_Derived_Type --
2005 ---------------------------------
2007 procedure Analyze_Formal_Derived_Type
2012 Loc : constant Source_Ptr := Sloc (Def);
2013 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2017 Set_Is_Generic_Type (T);
2019 if Private_Present (Def) then
2021 Make_Private_Extension_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Discriminant_Specifications => Discriminant_Specifications (N),
2024 Unknown_Discriminants_Present => Unk_Disc,
2025 Subtype_Indication => Subtype_Mark (Def),
2026 Interface_List => Interface_List (Def));
2028 Set_Abstract_Present (New_N, Abstract_Present (Def));
2029 Set_Limited_Present (New_N, Limited_Present (Def));
2030 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2034 Make_Full_Type_Declaration (Loc,
2035 Defining_Identifier => T,
2036 Discriminant_Specifications =>
2037 Discriminant_Specifications (Parent (T)),
2039 Make_Derived_Type_Definition (Loc,
2040 Subtype_Indication => Subtype_Mark (Def)));
2042 Set_Abstract_Present
2043 (Type_Definition (New_N), Abstract_Present (Def));
2045 (Type_Definition (New_N), Limited_Present (Def));
2052 if not Is_Composite_Type (T) then
2054 ("unknown discriminants not allowed for elementary types", N);
2056 Set_Has_Unknown_Discriminants (T);
2057 Set_Is_Constrained (T, False);
2061 -- If the parent type has a known size, so does the formal, which makes
2062 -- legal representation clauses that involve the formal.
2064 Set_Size_Known_At_Compile_Time
2065 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2066 end Analyze_Formal_Derived_Type;
2068 ----------------------------------
2069 -- Analyze_Formal_Discrete_Type --
2070 ----------------------------------
2072 -- The operations defined for a discrete types are those of an enumeration
2073 -- type. The size is set to an arbitrary value, for use in analyzing the
2076 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2077 Loc : constant Source_Ptr := Sloc (Def);
2081 Base : constant Entity_Id :=
2083 (E_Floating_Point_Type, Current_Scope,
2084 Sloc (Defining_Identifier (Parent (Def))), 'G');
2088 Set_Ekind (T, E_Enumeration_Subtype);
2089 Set_Etype (T, Base);
2092 Set_Is_Generic_Type (T);
2093 Set_Is_Constrained (T);
2095 -- For semantic analysis, the bounds of the type must be set to some
2096 -- non-static value. The simplest is to create attribute nodes for those
2097 -- bounds, that refer to the type itself. These bounds are never
2098 -- analyzed but serve as place-holders.
2101 Make_Attribute_Reference (Loc,
2102 Attribute_Name => Name_First,
2103 Prefix => New_Occurrence_Of (T, Loc));
2107 Make_Attribute_Reference (Loc,
2108 Attribute_Name => Name_Last,
2109 Prefix => New_Occurrence_Of (T, Loc));
2112 Set_Scalar_Range (T,
2117 Set_Ekind (Base, E_Enumeration_Type);
2118 Set_Etype (Base, Base);
2119 Init_Size (Base, 8);
2120 Init_Alignment (Base);
2121 Set_Is_Generic_Type (Base);
2122 Set_Scalar_Range (Base, Scalar_Range (T));
2123 Set_Parent (Base, Parent (Def));
2124 end Analyze_Formal_Discrete_Type;
2126 ----------------------------------
2127 -- Analyze_Formal_Floating_Type --
2128 ---------------------------------
2130 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2131 Base : constant Entity_Id :=
2133 (E_Floating_Point_Type, Current_Scope,
2134 Sloc (Defining_Identifier (Parent (Def))), 'G');
2137 -- The various semantic attributes are taken from the predefined type
2138 -- Float, just so that all of them are initialized. Their values are
2139 -- never used because no constant folding or expansion takes place in
2140 -- the generic itself.
2143 Set_Ekind (T, E_Floating_Point_Subtype);
2144 Set_Etype (T, Base);
2145 Set_Size_Info (T, (Standard_Float));
2146 Set_RM_Size (T, RM_Size (Standard_Float));
2147 Set_Digits_Value (T, Digits_Value (Standard_Float));
2148 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2149 Set_Is_Constrained (T);
2151 Set_Is_Generic_Type (Base);
2152 Set_Etype (Base, Base);
2153 Set_Size_Info (Base, (Standard_Float));
2154 Set_RM_Size (Base, RM_Size (Standard_Float));
2155 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2156 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2157 Set_Parent (Base, Parent (Def));
2159 Check_Restriction (No_Floating_Point, Def);
2160 end Analyze_Formal_Floating_Type;
2162 -----------------------------------
2163 -- Analyze_Formal_Interface_Type;--
2164 -----------------------------------
2166 procedure Analyze_Formal_Interface_Type
2171 Loc : constant Source_Ptr := Sloc (N);
2176 Make_Full_Type_Declaration (Loc,
2177 Defining_Identifier => T,
2178 Type_Definition => Def);
2182 Set_Is_Generic_Type (T);
2183 end Analyze_Formal_Interface_Type;
2185 ---------------------------------
2186 -- Analyze_Formal_Modular_Type --
2187 ---------------------------------
2189 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2191 -- Apart from their entity kind, generic modular types are treated like
2192 -- signed integer types, and have the same attributes.
2194 Analyze_Formal_Signed_Integer_Type (T, Def);
2195 Set_Ekind (T, E_Modular_Integer_Subtype);
2196 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2198 end Analyze_Formal_Modular_Type;
2200 ---------------------------------------
2201 -- Analyze_Formal_Object_Declaration --
2202 ---------------------------------------
2204 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2205 E : constant Node_Id := Default_Expression (N);
2206 Id : constant Node_Id := Defining_Identifier (N);
2213 -- Determine the mode of the formal object
2215 if Out_Present (N) then
2216 K := E_Generic_In_Out_Parameter;
2218 if not In_Present (N) then
2219 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2223 K := E_Generic_In_Parameter;
2226 if Present (Subtype_Mark (N)) then
2227 Find_Type (Subtype_Mark (N));
2228 T := Entity (Subtype_Mark (N));
2230 -- Verify that there is no redundant null exclusion
2232 if Null_Exclusion_Present (N) then
2233 if not Is_Access_Type (T) then
2235 ("null exclusion can only apply to an access type", N);
2237 elsif Can_Never_Be_Null (T) then
2239 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2243 -- Ada 2005 (AI-423): Formal object with an access definition
2246 Check_Access_Definition (N);
2247 T := Access_Definition
2249 N => Access_Definition (N));
2252 if Ekind (T) = E_Incomplete_Type then
2254 Error_Node : Node_Id;
2257 if Present (Subtype_Mark (N)) then
2258 Error_Node := Subtype_Mark (N);
2260 Check_Access_Definition (N);
2261 Error_Node := Access_Definition (N);
2264 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2268 if K = E_Generic_In_Parameter then
2270 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2272 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2274 ("generic formal of mode IN must not be of limited type", N);
2275 Explain_Limited_Type (T, N);
2278 if Is_Abstract_Type (T) then
2280 ("generic formal of mode IN must not be of abstract type", N);
2284 Preanalyze_Spec_Expression (E, T);
2286 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2288 ("initialization not allowed for limited types", E);
2289 Explain_Limited_Type (T, E);
2296 -- Case of generic IN OUT parameter
2299 -- If the formal has an unconstrained type, construct its actual
2300 -- subtype, as is done for subprogram formals. In this fashion, all
2301 -- its uses can refer to specific bounds.
2306 if (Is_Array_Type (T) and then not Is_Constrained (T))
2307 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2310 Non_Freezing_Ref : constant Node_Id :=
2311 New_Occurrence_Of (Id, Sloc (Id));
2315 -- Make sure the actual subtype doesn't generate bogus freezing
2317 Set_Must_Not_Freeze (Non_Freezing_Ref);
2318 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2319 Insert_Before_And_Analyze (N, Decl);
2320 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2323 Set_Actual_Subtype (Id, T);
2328 ("initialization not allowed for `IN OUT` formals", N);
2332 if Has_Aspects (N) then
2333 Analyze_Aspect_Specifications (N, Id);
2335 end Analyze_Formal_Object_Declaration;
2337 ----------------------------------------------
2338 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2339 ----------------------------------------------
2341 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2345 Loc : constant Source_Ptr := Sloc (Def);
2346 Base : constant Entity_Id :=
2348 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2349 Sloc (Defining_Identifier (Parent (Def))), 'G');
2352 -- The semantic attributes are set for completeness only, their values
2353 -- will never be used, since all properties of the type are non-static.
2356 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2357 Set_Etype (T, Base);
2358 Set_Size_Info (T, Standard_Integer);
2359 Set_RM_Size (T, RM_Size (Standard_Integer));
2360 Set_Small_Value (T, Ureal_1);
2361 Set_Delta_Value (T, Ureal_1);
2362 Set_Scalar_Range (T,
2364 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2365 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2366 Set_Is_Constrained (T);
2368 Set_Is_Generic_Type (Base);
2369 Set_Etype (Base, Base);
2370 Set_Size_Info (Base, Standard_Integer);
2371 Set_RM_Size (Base, RM_Size (Standard_Integer));
2372 Set_Small_Value (Base, Ureal_1);
2373 Set_Delta_Value (Base, Ureal_1);
2374 Set_Scalar_Range (Base, Scalar_Range (T));
2375 Set_Parent (Base, Parent (Def));
2377 Check_Restriction (No_Fixed_Point, Def);
2378 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2380 ----------------------------------------
2381 -- Analyze_Formal_Package_Declaration --
2382 ----------------------------------------
2384 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2385 Loc : constant Source_Ptr := Sloc (N);
2386 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2388 Gen_Id : constant Node_Id := Name (N);
2390 Gen_Unit : Entity_Id;
2392 Parent_Installed : Boolean := False;
2394 Parent_Instance : Entity_Id;
2395 Renaming_In_Par : Entity_Id;
2396 Associations : Boolean := True;
2398 Vis_Prims_List : Elist_Id := No_Elist;
2399 -- List of primitives made temporarily visible in the instantiation
2400 -- to match the visibility of the formal type
2402 function Build_Local_Package return Node_Id;
2403 -- The formal package is rewritten so that its parameters are replaced
2404 -- with corresponding declarations. For parameters with bona fide
2405 -- associations these declarations are created by Analyze_Associations
2406 -- as for a regular instantiation. For boxed parameters, we preserve
2407 -- the formal declarations and analyze them, in order to introduce
2408 -- entities of the right kind in the environment of the formal.
2410 -------------------------
2411 -- Build_Local_Package --
2412 -------------------------
2414 function Build_Local_Package return Node_Id is
2416 Pack_Decl : Node_Id;
2419 -- Within the formal, the name of the generic package is a renaming
2420 -- of the formal (as for a regular instantiation).
2423 Make_Package_Declaration (Loc,
2426 (Specification (Original_Node (Gen_Decl)),
2427 Empty, Instantiating => True));
2430 Make_Package_Renaming_Declaration (Loc,
2431 Defining_Unit_Name =>
2432 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2433 Name => New_Occurrence_Of (Formal, Loc));
2435 if Nkind (Gen_Id) = N_Identifier
2436 and then Chars (Gen_Id) = Chars (Pack_Id)
2439 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2442 -- If the formal is declared with a box, or with an others choice,
2443 -- create corresponding declarations for all entities in the formal
2444 -- part, so that names with the proper types are available in the
2445 -- specification of the formal package.
2447 -- On the other hand, if there are no associations, then all the
2448 -- formals must have defaults, and this will be checked by the
2449 -- call to Analyze_Associations.
2452 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2455 Formal_Decl : Node_Id;
2458 -- TBA : for a formal package, need to recurse ???
2463 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2464 while Present (Formal_Decl) loop
2466 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2471 -- If generic associations are present, use Analyze_Associations to
2472 -- create the proper renaming declarations.
2476 Act_Tree : constant Node_Id :=
2478 (Original_Node (Gen_Decl), Empty,
2479 Instantiating => True);
2482 Generic_Renamings.Set_Last (0);
2483 Generic_Renamings_HTable.Reset;
2484 Instantiation_Node := N;
2487 Analyze_Associations
2488 (I_Node => Original_Node (N),
2489 Formals => Generic_Formal_Declarations (Act_Tree),
2490 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2492 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2496 Append (Renaming, To => Decls);
2498 -- Add generated declarations ahead of local declarations in
2501 if No (Visible_Declarations (Specification (Pack_Decl))) then
2502 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2505 (First (Visible_Declarations (Specification (Pack_Decl))),
2510 end Build_Local_Package;
2512 -- Start of processing for Analyze_Formal_Package_Declaration
2515 Check_Text_IO_Special_Unit (Gen_Id);
2518 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2519 Gen_Unit := Entity (Gen_Id);
2521 -- Check for a formal package that is a package renaming
2523 if Present (Renamed_Object (Gen_Unit)) then
2525 -- Indicate that unit is used, before replacing it with renamed
2526 -- entity for use below.
2528 if In_Extended_Main_Source_Unit (N) then
2529 Set_Is_Instantiated (Gen_Unit);
2530 Generate_Reference (Gen_Unit, N);
2533 Gen_Unit := Renamed_Object (Gen_Unit);
2536 if Ekind (Gen_Unit) /= E_Generic_Package then
2537 Error_Msg_N ("expect generic package name", Gen_Id);
2541 elsif Gen_Unit = Current_Scope then
2543 ("generic package cannot be used as a formal package of itself",
2548 elsif In_Open_Scopes (Gen_Unit) then
2549 if Is_Compilation_Unit (Gen_Unit)
2550 and then Is_Child_Unit (Current_Scope)
2552 -- Special-case the error when the formal is a parent, and
2553 -- continue analysis to minimize cascaded errors.
2556 ("generic parent cannot be used as formal package "
2557 & "of a child unit", Gen_Id);
2561 ("generic package cannot be used as a formal package "
2562 & "within itself", Gen_Id);
2568 -- Check that name of formal package does not hide name of generic,
2569 -- or its leading prefix. This check must be done separately because
2570 -- the name of the generic has already been analyzed.
2573 Gen_Name : Entity_Id;
2577 while Nkind (Gen_Name) = N_Expanded_Name loop
2578 Gen_Name := Prefix (Gen_Name);
2581 if Chars (Gen_Name) = Chars (Pack_Id) then
2583 ("& is hidden within declaration of formal package",
2589 or else No (Generic_Associations (N))
2590 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2592 Associations := False;
2595 -- If there are no generic associations, the generic parameters appear
2596 -- as local entities and are instantiated like them. We copy the generic
2597 -- package declaration as if it were an instantiation, and analyze it
2598 -- like a regular package, except that we treat the formals as
2599 -- additional visible components.
2601 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2603 if In_Extended_Main_Source_Unit (N) then
2604 Set_Is_Instantiated (Gen_Unit);
2605 Generate_Reference (Gen_Unit, N);
2608 Formal := New_Copy (Pack_Id);
2609 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2612 -- Make local generic without formals. The formals will be replaced
2613 -- with internal declarations.
2615 New_N := Build_Local_Package;
2617 -- If there are errors in the parameter list, Analyze_Associations
2618 -- raises Instantiation_Error. Patch the declaration to prevent
2619 -- further exception propagation.
2622 when Instantiation_Error =>
2624 Enter_Name (Formal);
2625 Set_Ekind (Formal, E_Variable);
2626 Set_Etype (Formal, Any_Type);
2627 Restore_Hidden_Primitives (Vis_Prims_List);
2629 if Parent_Installed then
2637 Set_Defining_Unit_Name (Specification (New_N), Formal);
2638 Set_Generic_Parent (Specification (N), Gen_Unit);
2639 Set_Instance_Env (Gen_Unit, Formal);
2640 Set_Is_Generic_Instance (Formal);
2642 Enter_Name (Formal);
2643 Set_Ekind (Formal, E_Package);
2644 Set_Etype (Formal, Standard_Void_Type);
2645 Set_Inner_Instances (Formal, New_Elmt_List);
2646 Push_Scope (Formal);
2648 -- Manually set the SPARK_Mode from the context because the package
2649 -- declaration is never analyzed.
2651 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2652 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2653 Set_SPARK_Pragma_Inherited (Formal);
2654 Set_SPARK_Aux_Pragma_Inherited (Formal);
2656 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2658 -- Similarly, we have to make the name of the formal visible in the
2659 -- parent instance, to resolve properly fully qualified names that
2660 -- may appear in the generic unit. The parent instance has been
2661 -- placed on the scope stack ahead of the current scope.
2663 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2666 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2667 Set_Ekind (Renaming_In_Par, E_Package);
2668 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2669 Set_Scope (Renaming_In_Par, Parent_Instance);
2670 Set_Parent (Renaming_In_Par, Parent (Formal));
2671 Set_Renamed_Object (Renaming_In_Par, Formal);
2672 Append_Entity (Renaming_In_Par, Parent_Instance);
2675 Analyze (Specification (N));
2677 -- The formals for which associations are provided are not visible
2678 -- outside of the formal package. The others are still declared by a
2679 -- formal parameter declaration.
2681 -- If there are no associations, the only local entity to hide is the
2682 -- generated package renaming itself.
2688 E := First_Entity (Formal);
2689 while Present (E) loop
2690 if Associations and then not Is_Generic_Formal (E) then
2694 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2703 End_Package_Scope (Formal);
2704 Restore_Hidden_Primitives (Vis_Prims_List);
2706 if Parent_Installed then
2712 -- Inside the generic unit, the formal package is a regular package, but
2713 -- no body is needed for it. Note that after instantiation, the defining
2714 -- unit name we need is in the new tree and not in the original (see
2715 -- Package_Instantiation). A generic formal package is an instance, and
2716 -- can be used as an actual for an inner instance.
2718 Set_Has_Completion (Formal, True);
2720 -- Add semantic information to the original defining identifier.
2723 Set_Ekind (Pack_Id, E_Package);
2724 Set_Etype (Pack_Id, Standard_Void_Type);
2725 Set_Scope (Pack_Id, Scope (Formal));
2726 Set_Has_Completion (Pack_Id, True);
2729 if Has_Aspects (N) then
2730 Analyze_Aspect_Specifications (N, Pack_Id);
2732 end Analyze_Formal_Package_Declaration;
2734 ---------------------------------
2735 -- Analyze_Formal_Private_Type --
2736 ---------------------------------
2738 procedure Analyze_Formal_Private_Type
2744 New_Private_Type (N, T, Def);
2746 -- Set the size to an arbitrary but legal value
2748 Set_Size_Info (T, Standard_Integer);
2749 Set_RM_Size (T, RM_Size (Standard_Integer));
2750 end Analyze_Formal_Private_Type;
2752 ------------------------------------
2753 -- Analyze_Formal_Incomplete_Type --
2754 ------------------------------------
2756 procedure Analyze_Formal_Incomplete_Type
2762 Set_Ekind (T, E_Incomplete_Type);
2764 Set_Private_Dependents (T, New_Elmt_List);
2766 if Tagged_Present (Def) then
2767 Set_Is_Tagged_Type (T);
2768 Make_Class_Wide_Type (T);
2769 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2771 end Analyze_Formal_Incomplete_Type;
2773 ----------------------------------------
2774 -- Analyze_Formal_Signed_Integer_Type --
2775 ----------------------------------------
2777 procedure Analyze_Formal_Signed_Integer_Type
2781 Base : constant Entity_Id :=
2783 (E_Signed_Integer_Type,
2785 Sloc (Defining_Identifier (Parent (Def))), 'G');
2790 Set_Ekind (T, E_Signed_Integer_Subtype);
2791 Set_Etype (T, Base);
2792 Set_Size_Info (T, Standard_Integer);
2793 Set_RM_Size (T, RM_Size (Standard_Integer));
2794 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2795 Set_Is_Constrained (T);
2797 Set_Is_Generic_Type (Base);
2798 Set_Size_Info (Base, Standard_Integer);
2799 Set_RM_Size (Base, RM_Size (Standard_Integer));
2800 Set_Etype (Base, Base);
2801 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2802 Set_Parent (Base, Parent (Def));
2803 end Analyze_Formal_Signed_Integer_Type;
2805 -------------------------------------------
2806 -- Analyze_Formal_Subprogram_Declaration --
2807 -------------------------------------------
2809 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2810 Spec : constant Node_Id := Specification (N);
2811 Def : constant Node_Id := Default_Name (N);
2812 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2820 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2821 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2825 Analyze_Subprogram_Declaration (N);
2826 Set_Is_Formal_Subprogram (Nam);
2827 Set_Has_Completion (Nam);
2829 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2830 Set_Is_Abstract_Subprogram (Nam);
2832 Set_Is_Dispatching_Operation (Nam);
2834 -- A formal abstract procedure cannot have a null default
2835 -- (RM 12.6(4.1/2)).
2837 if Nkind (Spec) = N_Procedure_Specification
2838 and then Null_Present (Spec)
2841 ("a formal abstract subprogram cannot default to null", Spec);
2845 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2847 if No (Ctrl_Type) then
2849 ("abstract formal subprogram must have a controlling type",
2852 elsif Ada_Version >= Ada_2012
2853 and then Is_Incomplete_Type (Ctrl_Type)
2856 ("controlling type of abstract formal subprogram cannot "
2857 & "be incomplete type", N, Ctrl_Type);
2860 Check_Controlling_Formals (Ctrl_Type, Nam);
2865 -- Default name is resolved at the point of instantiation
2867 if Box_Present (N) then
2870 -- Else default is bound at the point of generic declaration
2872 elsif Present (Def) then
2873 if Nkind (Def) = N_Operator_Symbol then
2874 Find_Direct_Name (Def);
2876 elsif Nkind (Def) /= N_Attribute_Reference then
2880 -- For an attribute reference, analyze the prefix and verify
2881 -- that it has the proper profile for the subprogram.
2883 Analyze (Prefix (Def));
2884 Valid_Default_Attribute (Nam, Def);
2888 -- Default name may be overloaded, in which case the interpretation
2889 -- with the correct profile must be selected, as for a renaming.
2890 -- If the definition is an indexed component, it must denote a
2891 -- member of an entry family. If it is a selected component, it
2892 -- can be a protected operation.
2894 if Etype (Def) = Any_Type then
2897 elsif Nkind (Def) = N_Selected_Component then
2898 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2899 Error_Msg_N ("expect valid subprogram name as default", Def);
2902 elsif Nkind (Def) = N_Indexed_Component then
2903 if Is_Entity_Name (Prefix (Def)) then
2904 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2905 Error_Msg_N ("expect valid subprogram name as default", Def);
2908 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2909 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2912 Error_Msg_N ("expect valid subprogram name as default", Def);
2916 Error_Msg_N ("expect valid subprogram name as default", Def);
2920 elsif Nkind (Def) = N_Character_Literal then
2922 -- Needs some type checks: subprogram should be parameterless???
2924 Resolve (Def, (Etype (Nam)));
2926 elsif not Is_Entity_Name (Def)
2927 or else not Is_Overloadable (Entity (Def))
2929 Error_Msg_N ("expect valid subprogram name as default", Def);
2932 elsif not Is_Overloaded (Def) then
2933 Subp := Entity (Def);
2936 Error_Msg_N ("premature usage of formal subprogram", Def);
2938 elsif not Entity_Matches_Spec (Subp, Nam) then
2939 Error_Msg_N ("no visible entity matches specification", Def);
2942 -- More than one interpretation, so disambiguate as for a renaming
2947 I1 : Interp_Index := 0;
2953 Get_First_Interp (Def, I, It);
2954 while Present (It.Nam) loop
2955 if Entity_Matches_Spec (It.Nam, Nam) then
2956 if Subp /= Any_Id then
2957 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2959 if It1 = No_Interp then
2960 Error_Msg_N ("ambiguous default subprogram", Def);
2973 Get_Next_Interp (I, It);
2977 if Subp /= Any_Id then
2979 -- Subprogram found, generate reference to it
2981 Set_Entity (Def, Subp);
2982 Generate_Reference (Subp, Def);
2985 Error_Msg_N ("premature usage of formal subprogram", Def);
2987 elsif Ekind (Subp) /= E_Operator then
2988 Check_Mode_Conformant (Subp, Nam);
2992 Error_Msg_N ("no visible subprogram matches specification", N);
2998 if Has_Aspects (N) then
2999 Analyze_Aspect_Specifications (N, Nam);
3002 end Analyze_Formal_Subprogram_Declaration;
3004 -------------------------------------
3005 -- Analyze_Formal_Type_Declaration --
3006 -------------------------------------
3008 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3009 Def : constant Node_Id := Formal_Type_Definition (N);
3013 T := Defining_Identifier (N);
3015 if Present (Discriminant_Specifications (N))
3016 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3019 ("discriminants not allowed for this formal type", T);
3022 -- Enter the new name, and branch to specific routine
3025 when N_Formal_Private_Type_Definition =>
3026 Analyze_Formal_Private_Type (N, T, Def);
3028 when N_Formal_Derived_Type_Definition =>
3029 Analyze_Formal_Derived_Type (N, T, Def);
3031 when N_Formal_Incomplete_Type_Definition =>
3032 Analyze_Formal_Incomplete_Type (T, Def);
3034 when N_Formal_Discrete_Type_Definition =>
3035 Analyze_Formal_Discrete_Type (T, Def);
3037 when N_Formal_Signed_Integer_Type_Definition =>
3038 Analyze_Formal_Signed_Integer_Type (T, Def);
3040 when N_Formal_Modular_Type_Definition =>
3041 Analyze_Formal_Modular_Type (T, Def);
3043 when N_Formal_Floating_Point_Definition =>
3044 Analyze_Formal_Floating_Type (T, Def);
3046 when N_Formal_Ordinary_Fixed_Point_Definition =>
3047 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3049 when N_Formal_Decimal_Fixed_Point_Definition =>
3050 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3052 when N_Array_Type_Definition =>
3053 Analyze_Formal_Array_Type (T, Def);
3055 when N_Access_To_Object_Definition |
3056 N_Access_Function_Definition |
3057 N_Access_Procedure_Definition =>
3058 Analyze_Generic_Access_Type (T, Def);
3060 -- Ada 2005: a interface declaration is encoded as an abstract
3061 -- record declaration or a abstract type derivation.
3063 when N_Record_Definition =>
3064 Analyze_Formal_Interface_Type (N, T, Def);
3066 when N_Derived_Type_Definition =>
3067 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3073 raise Program_Error;
3077 Set_Is_Generic_Type (T);
3079 if Has_Aspects (N) then
3080 Analyze_Aspect_Specifications (N, T);
3082 end Analyze_Formal_Type_Declaration;
3084 ------------------------------------
3085 -- Analyze_Function_Instantiation --
3086 ------------------------------------
3088 procedure Analyze_Function_Instantiation (N : Node_Id) is
3090 Analyze_Subprogram_Instantiation (N, E_Function);
3091 end Analyze_Function_Instantiation;
3093 ---------------------------------
3094 -- Analyze_Generic_Access_Type --
3095 ---------------------------------
3097 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3101 if Nkind (Def) = N_Access_To_Object_Definition then
3102 Access_Type_Declaration (T, Def);
3104 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3105 and then No (Full_View (Designated_Type (T)))
3106 and then not Is_Generic_Type (Designated_Type (T))
3108 Error_Msg_N ("premature usage of incomplete type", Def);
3110 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3112 ("only a subtype mark is allowed in a formal", Def);
3116 Access_Subprogram_Declaration (T, Def);
3118 end Analyze_Generic_Access_Type;
3120 ---------------------------------
3121 -- Analyze_Generic_Formal_Part --
3122 ---------------------------------
3124 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3125 Gen_Parm_Decl : Node_Id;
3128 -- The generic formals are processed in the scope of the generic unit,
3129 -- where they are immediately visible. The scope is installed by the
3132 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3133 while Present (Gen_Parm_Decl) loop
3134 Analyze (Gen_Parm_Decl);
3135 Next (Gen_Parm_Decl);
3138 Generate_Reference_To_Generic_Formals (Current_Scope);
3139 end Analyze_Generic_Formal_Part;
3141 ------------------------------------------
3142 -- Analyze_Generic_Package_Declaration --
3143 ------------------------------------------
3145 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3146 Loc : constant Source_Ptr := Sloc (N);
3147 Decls : constant List_Id :=
3148 Visible_Declarations (Specification (N));
3153 Save_Parent : Node_Id;
3156 Check_SPARK_05_Restriction ("generic is not allowed", N);
3158 -- We introduce a renaming of the enclosing package, to have a usable
3159 -- entity as the prefix of an expanded name for a local entity of the
3160 -- form Par.P.Q, where P is the generic package. This is because a local
3161 -- entity named P may hide it, so that the usual visibility rules in
3162 -- the instance will not resolve properly.
3165 Make_Package_Renaming_Declaration (Loc,
3166 Defining_Unit_Name =>
3167 Make_Defining_Identifier (Loc,
3168 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3170 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3172 if Present (Decls) then
3173 Decl := First (Decls);
3174 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3178 if Present (Decl) then
3179 Insert_Before (Decl, Renaming);
3181 Append (Renaming, Visible_Declarations (Specification (N)));
3185 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3188 -- Create copy of generic unit, and save for instantiation. If the unit
3189 -- is a child unit, do not copy the specifications for the parent, which
3190 -- are not part of the generic tree.
3192 Save_Parent := Parent_Spec (N);
3193 Set_Parent_Spec (N, Empty);
3195 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3196 Set_Parent_Spec (New_N, Save_Parent);
3199 -- Once the contents of the generic copy and the template are swapped,
3200 -- do the same for their respective aspect specifications.
3202 Exchange_Aspects (N, New_N);
3204 -- Collect all contract-related source pragmas found within the template
3205 -- and attach them to the contract of the package spec. This contract is
3206 -- used in the capture of global references within annotations.
3208 Create_Generic_Contract (N);
3210 Id := Defining_Entity (N);
3211 Generate_Definition (Id);
3213 -- Expansion is not applied to generic units
3218 Set_Ekind (Id, E_Generic_Package);
3219 Set_Etype (Id, Standard_Void_Type);
3221 -- A generic package declared within a Ghost region is rendered Ghost
3222 -- (SPARK RM 6.9(2)).
3224 if Ghost_Mode > None then
3225 Set_Is_Ghost_Entity (Id);
3228 -- Analyze aspects now, so that generated pragmas appear in the
3229 -- declarations before building and analyzing the generic copy.
3231 if Has_Aspects (N) then
3232 Analyze_Aspect_Specifications (N, Id);
3236 Enter_Generic_Scope (Id);
3237 Set_Inner_Instances (Id, New_Elmt_List);
3239 Set_Categorization_From_Pragmas (N);
3240 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3242 -- Link the declaration of the generic homonym in the generic copy to
3243 -- the package it renames, so that it is always resolved properly.
3245 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3246 Set_Entity (Associated_Node (Name (Renaming)), Id);
3248 -- For a library unit, we have reconstructed the entity for the unit,
3249 -- and must reset it in the library tables.
3251 if Nkind (Parent (N)) = N_Compilation_Unit then
3252 Set_Cunit_Entity (Current_Sem_Unit, Id);
3255 Analyze_Generic_Formal_Part (N);
3257 -- After processing the generic formals, analysis proceeds as for a
3258 -- non-generic package.
3260 Analyze (Specification (N));
3262 Validate_Categorization_Dependency (N, Id);
3266 End_Package_Scope (Id);
3267 Exit_Generic_Scope (Id);
3269 if Nkind (Parent (N)) /= N_Compilation_Unit then
3270 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3271 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3272 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3275 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3276 Validate_RT_RAT_Component (N);
3278 -- If this is a spec without a body, check that generic parameters
3281 if not Body_Required (Parent (N)) then
3282 Check_References (Id);
3286 -- If there is a specified storage pool in the context, create an
3287 -- aspect on the package declaration, so that it is used in any
3288 -- instance that does not override it.
3290 if Present (Default_Pool) then
3296 Make_Aspect_Specification (Loc,
3297 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3298 Expression => New_Copy (Default_Pool));
3300 if No (Aspect_Specifications (Specification (N))) then
3301 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3303 Append (ASN, Aspect_Specifications (Specification (N)));
3307 end Analyze_Generic_Package_Declaration;
3309 --------------------------------------------
3310 -- Analyze_Generic_Subprogram_Declaration --
3311 --------------------------------------------
3313 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3317 Result_Type : Entity_Id;
3318 Save_Parent : Node_Id;
3323 Check_SPARK_05_Restriction ("generic is not allowed", N);
3325 -- Create copy of generic unit, and save for instantiation. If the unit
3326 -- is a child unit, do not copy the specifications for the parent, which
3327 -- are not part of the generic tree.
3329 Save_Parent := Parent_Spec (N);
3330 Set_Parent_Spec (N, Empty);
3332 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3333 Set_Parent_Spec (New_N, Save_Parent);
3336 -- Once the contents of the generic copy and the template are swapped,
3337 -- do the same for their respective aspect specifications.
3339 Exchange_Aspects (N, New_N);
3341 -- Collect all contract-related source pragmas found within the template
3342 -- and attach them to the contract of the subprogram spec. This contract
3343 -- is used in the capture of global references within annotations.
3345 Create_Generic_Contract (N);
3347 Spec := Specification (N);
3348 Id := Defining_Entity (Spec);
3349 Generate_Definition (Id);
3351 if Nkind (Id) = N_Defining_Operator_Symbol then
3353 ("operator symbol not allowed for generic subprogram", Id);
3359 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3361 -- Analyze the aspects of the generic copy to ensure that all generated
3362 -- pragmas (if any) perform their semantic effects.
3364 if Has_Aspects (N) then
3365 Analyze_Aspect_Specifications (N, Id);
3369 Enter_Generic_Scope (Id);
3370 Set_Inner_Instances (Id, New_Elmt_List);
3371 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3373 Analyze_Generic_Formal_Part (N);
3375 Formals := Parameter_Specifications (Spec);
3377 if Nkind (Spec) = N_Function_Specification then
3378 Set_Ekind (Id, E_Generic_Function);
3380 Set_Ekind (Id, E_Generic_Procedure);
3383 if Present (Formals) then
3384 Process_Formals (Formals, Spec);
3387 if Nkind (Spec) = N_Function_Specification then
3388 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3389 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3390 Set_Etype (Id, Result_Type);
3392 -- Check restriction imposed by AI05-073: a generic function
3393 -- cannot return an abstract type or an access to such.
3395 -- This is a binding interpretation should it apply to earlier
3396 -- versions of Ada as well as Ada 2012???
3398 if Is_Abstract_Type (Designated_Type (Result_Type))
3399 and then Ada_Version >= Ada_2012
3402 ("generic function cannot have an access result "
3403 & "that designates an abstract type", Spec);
3407 Find_Type (Result_Definition (Spec));
3408 Typ := Entity (Result_Definition (Spec));
3410 if Is_Abstract_Type (Typ)
3411 and then Ada_Version >= Ada_2012
3414 ("generic function cannot have abstract result type", Spec);
3417 -- If a null exclusion is imposed on the result type, then create
3418 -- a null-excluding itype (an access subtype) and use it as the
3419 -- function's Etype.
3421 if Is_Access_Type (Typ)
3422 and then Null_Exclusion_Present (Spec)
3425 Create_Null_Excluding_Itype
3427 Related_Nod => Spec,
3428 Scope_Id => Defining_Unit_Name (Spec)));
3430 Set_Etype (Id, Typ);
3435 Set_Etype (Id, Standard_Void_Type);
3438 -- A generic subprogram declared within a Ghost region is rendered Ghost
3439 -- (SPARK RM 6.9(2)).
3441 if Ghost_Mode > None then
3442 Set_Is_Ghost_Entity (Id);
3445 -- For a library unit, we have reconstructed the entity for the unit,
3446 -- and must reset it in the library tables. We also make sure that
3447 -- Body_Required is set properly in the original compilation unit node.
3449 if Nkind (Parent (N)) = N_Compilation_Unit then
3450 Set_Cunit_Entity (Current_Sem_Unit, Id);
3451 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3454 Set_Categorization_From_Pragmas (N);
3455 Validate_Categorization_Dependency (N, Id);
3457 -- Capture all global references that occur within the profile of the
3458 -- generic subprogram. Aspects are not part of this processing because
3459 -- they must be delayed. If processed now, Save_Global_References will
3460 -- destroy the Associated_Node links and prevent the capture of global
3461 -- references when the contract of the generic subprogram is analyzed.
3463 Save_Global_References (Original_Node (N));
3467 Exit_Generic_Scope (Id);
3468 Generate_Reference_To_Formals (Id);
3470 List_Inherited_Pre_Post_Aspects (Id);
3471 end Analyze_Generic_Subprogram_Declaration;
3473 -----------------------------------
3474 -- Analyze_Package_Instantiation --
3475 -----------------------------------
3477 procedure Analyze_Package_Instantiation (N : Node_Id) is
3478 Loc : constant Source_Ptr := Sloc (N);
3479 Gen_Id : constant Node_Id := Name (N);
3482 Act_Decl_Name : Node_Id;
3483 Act_Decl_Id : Entity_Id;
3489 Gen_Unit : Entity_Id;
3491 Is_Actual_Pack : constant Boolean :=
3492 Is_Internal (Defining_Entity (N));
3494 Env_Installed : Boolean := False;
3495 Parent_Installed : Boolean := False;
3496 Renaming_List : List_Id;
3497 Unit_Renaming : Node_Id;
3498 Needs_Body : Boolean;
3499 Inline_Now : Boolean := False;
3500 Has_Inline_Always : Boolean := False;
3502 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3503 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3505 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3506 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3507 -- Save the SPARK_Mode-related data for restore on exit
3509 Save_Style_Check : constant Boolean := Style_Check;
3510 -- Save style check mode for restore on exit
3512 procedure Delay_Descriptors (E : Entity_Id);
3513 -- Delay generation of subprogram descriptors for given entity
3515 function Might_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- we instantiate the body. This may cause superfluous instantiations,
3518 -- but it is simpler than detecting the need for the body at the point
3519 -- of inlining, when the context of the instance is not available.
3521 -----------------------
3522 -- Delay_Descriptors --
3523 -----------------------
3525 procedure Delay_Descriptors (E : Entity_Id) is
3527 if not Delay_Subprogram_Descriptors (E) then
3528 Set_Delay_Subprogram_Descriptors (E);
3529 Pending_Descriptor.Append (E);
3531 end Delay_Descriptors;
3533 -----------------------
3534 -- Might_Inline_Subp --
3535 -----------------------
3537 function Might_Inline_Subp return Boolean is
3541 if not Inline_Processing_Required then
3545 E := First_Entity (Gen_Unit);
3546 while Present (E) loop
3547 if Is_Subprogram (E) and then Is_Inlined (E) then
3548 -- Remember if there are any subprograms with Inline_Always
3550 if Has_Pragma_Inline_Always (E) then
3551 Has_Inline_Always := True;
3562 end Might_Inline_Subp;
3564 -- Local declarations
3566 Vis_Prims_List : Elist_Id := No_Elist;
3567 -- List of primitives made temporarily visible in the instantiation
3568 -- to match the visibility of the formal type
3570 -- Start of processing for Analyze_Package_Instantiation
3573 Check_SPARK_05_Restriction ("generic is not allowed", N);
3575 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3576 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3578 Check_Text_IO_Special_Unit (Name (N));
3580 -- Make node global for error reporting
3582 Instantiation_Node := N;
3584 -- Turn off style checking in instances. If the check is enabled on the
3585 -- generic unit, a warning in an instance would just be noise. If not
3586 -- enabled on the generic, then a warning in an instance is just wrong.
3588 Style_Check := False;
3590 -- Case of instantiation of a generic package
3592 if Nkind (N) = N_Package_Instantiation then
3593 Act_Decl_Id := New_Copy (Defining_Entity (N));
3594 Set_Comes_From_Source (Act_Decl_Id, True);
3596 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3598 Make_Defining_Program_Unit_Name (Loc,
3600 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3601 Defining_Identifier => Act_Decl_Id);
3603 Act_Decl_Name := Act_Decl_Id;
3606 -- Case of instantiation of a formal package
3609 Act_Decl_Id := Defining_Identifier (N);
3610 Act_Decl_Name := Act_Decl_Id;
3613 Generate_Definition (Act_Decl_Id);
3614 Set_Ekind (Act_Decl_Id, E_Package);
3616 -- Initialize list of incomplete actuals before analysis
3618 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3620 Preanalyze_Actuals (N, Act_Decl_Id);
3623 Env_Installed := True;
3625 -- Reset renaming map for formal types. The mapping is established
3626 -- when analyzing the generic associations, but some mappings are
3627 -- inherited from formal packages of parent units, and these are
3628 -- constructed when the parents are installed.
3630 Generic_Renamings.Set_Last (0);
3631 Generic_Renamings_HTable.Reset;
3633 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3634 Gen_Unit := Entity (Gen_Id);
3636 -- Verify that it is the name of a generic package
3638 -- A visibility glitch: if the instance is a child unit and the generic
3639 -- is the generic unit of a parent instance (i.e. both the parent and
3640 -- the child units are instances of the same package) the name now
3641 -- denotes the renaming within the parent, not the intended generic
3642 -- unit. See if there is a homonym that is the desired generic. The
3643 -- renaming declaration must be visible inside the instance of the
3644 -- child, but not when analyzing the name in the instantiation itself.
3646 if Ekind (Gen_Unit) = E_Package
3647 and then Present (Renamed_Entity (Gen_Unit))
3648 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3649 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3650 and then Present (Homonym (Gen_Unit))
3652 Gen_Unit := Homonym (Gen_Unit);
3655 if Etype (Gen_Unit) = Any_Type then
3659 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3661 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3663 if From_Limited_With (Gen_Unit) then
3665 ("cannot instantiate a limited withed package", Gen_Id);
3668 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3675 if In_Extended_Main_Source_Unit (N) then
3676 Set_Is_Instantiated (Gen_Unit);
3677 Generate_Reference (Gen_Unit, N);
3679 if Present (Renamed_Object (Gen_Unit)) then
3680 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3681 Generate_Reference (Renamed_Object (Gen_Unit), N);
3685 if Nkind (Gen_Id) = N_Identifier
3686 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3689 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3691 elsif Nkind (Gen_Id) = N_Expanded_Name
3692 and then Is_Child_Unit (Gen_Unit)
3693 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3694 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3697 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3700 Set_Entity (Gen_Id, Gen_Unit);
3702 -- If generic is a renaming, get original generic unit
3704 if Present (Renamed_Object (Gen_Unit))
3705 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3707 Gen_Unit := Renamed_Object (Gen_Unit);
3710 -- Verify that there are no circular instantiations
3712 if In_Open_Scopes (Gen_Unit) then
3713 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3717 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3718 Error_Msg_Node_2 := Current_Scope;
3720 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3721 Circularity_Detected := True;
3726 -- If the context of the instance is subject to SPARK_Mode "off" or
3727 -- the annotation is altogether missing, set the global flag which
3728 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3731 if SPARK_Mode /= On then
3732 Ignore_Pragma_SPARK_Mode := True;
3735 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3736 Gen_Spec := Specification (Gen_Decl);
3738 -- Initialize renamings map, for error checking, and the list that
3739 -- holds private entities whose views have changed between generic
3740 -- definition and instantiation. If this is the instance created to
3741 -- validate an actual package, the instantiation environment is that
3742 -- of the enclosing instance.
3744 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3746 -- Copy original generic tree, to produce text for instantiation
3750 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3752 Act_Spec := Specification (Act_Tree);
3754 -- If this is the instance created to validate an actual package,
3755 -- only the formals matter, do not examine the package spec itself.
3757 if Is_Actual_Pack then
3758 Set_Visible_Declarations (Act_Spec, New_List);
3759 Set_Private_Declarations (Act_Spec, New_List);
3763 Analyze_Associations
3765 Formals => Generic_Formal_Declarations (Act_Tree),
3766 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3768 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3770 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3771 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3772 Set_Is_Generic_Instance (Act_Decl_Id);
3773 Set_Generic_Parent (Act_Spec, Gen_Unit);
3775 -- References to the generic in its own declaration or its body are
3776 -- references to the instance. Add a renaming declaration for the
3777 -- generic unit itself. This declaration, as well as the renaming
3778 -- declarations for the generic formals, must remain private to the
3779 -- unit: the formals, because this is the language semantics, and
3780 -- the unit because its use is an artifact of the implementation.
3783 Make_Package_Renaming_Declaration (Loc,
3784 Defining_Unit_Name =>
3785 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3786 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3788 Append (Unit_Renaming, Renaming_List);
3790 -- The renaming declarations are the first local declarations of the
3793 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3795 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3797 Set_Visible_Declarations (Act_Spec, Renaming_List);
3800 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3802 -- Propagate the aspect specifications from the package declaration
3803 -- template to the instantiated version of the package declaration.
3805 if Has_Aspects (Act_Tree) then
3806 Set_Aspect_Specifications (Act_Decl,
3807 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3810 -- The generic may have a generated Default_Storage_Pool aspect,
3811 -- set at the point of generic declaration. If the instance has
3812 -- that aspect, it overrides the one inherited from the generic.
3814 if Has_Aspects (Gen_Spec) then
3815 if No (Aspect_Specifications (N)) then
3816 Set_Aspect_Specifications (N,
3818 (Aspect_Specifications (Gen_Spec))));
3822 ASN1, ASN2 : Node_Id;
3825 ASN1 := First (Aspect_Specifications (N));
3826 while Present (ASN1) loop
3827 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3829 -- If generic carries a default storage pool, remove
3830 -- it in favor of the instance one.
3832 ASN2 := First (Aspect_Specifications (Gen_Spec));
3833 while Present (ASN2) loop
3834 if Chars (Identifier (ASN2)) =
3835 Name_Default_Storage_Pool
3848 Prepend_List_To (Aspect_Specifications (N),
3850 (Aspect_Specifications (Gen_Spec))));
3855 -- Save the instantiation node, for subsequent instantiation of the
3856 -- body, if there is one and we are generating code for the current
3857 -- unit. Mark unit as having a body (avoids premature error message).
3859 -- We instantiate the body if we are generating code, if we are
3860 -- generating cross-reference information, or if we are building
3861 -- trees for ASIS use or GNATprove use.
3864 Enclosing_Body_Present : Boolean := False;
3865 -- If the generic unit is not a compilation unit, then a body may
3866 -- be present in its parent even if none is required. We create a
3867 -- tentative pending instantiation for the body, which will be
3868 -- discarded if none is actually present.
3873 if Scope (Gen_Unit) /= Standard_Standard
3874 and then not Is_Child_Unit (Gen_Unit)
3876 Scop := Scope (Gen_Unit);
3877 while Present (Scop) and then Scop /= Standard_Standard loop
3878 if Unit_Requires_Body (Scop) then
3879 Enclosing_Body_Present := True;
3882 elsif In_Open_Scopes (Scop)
3883 and then In_Package_Body (Scop)
3885 Enclosing_Body_Present := True;
3889 exit when Is_Compilation_Unit (Scop);
3890 Scop := Scope (Scop);
3894 -- If front-end inlining is enabled or there are any subprograms
3895 -- marked with Inline_Always, and this is a unit for which code
3896 -- will be generated, we instantiate the body at once.
3898 -- This is done if the instance is not the main unit, and if the
3899 -- generic is not a child unit of another generic, to avoid scope
3900 -- problems and the reinstallation of parent instances.
3903 and then (not Is_Child_Unit (Gen_Unit)
3904 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3905 and then Might_Inline_Subp
3906 and then not Is_Actual_Pack
3908 if not Back_End_Inlining
3909 and then (Front_End_Inlining or else Has_Inline_Always)
3910 and then (Is_In_Main_Unit (N)
3911 or else In_Main_Context (Current_Scope))
3912 and then Nkind (Parent (N)) /= N_Compilation_Unit
3916 -- In configurable_run_time mode we force the inlining of
3917 -- predefined subprograms marked Inline_Always, to minimize
3918 -- the use of the run-time library.
3920 elsif Is_Predefined_File_Name
3921 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3922 and then Configurable_Run_Time_Mode
3923 and then Nkind (Parent (N)) /= N_Compilation_Unit
3928 -- If the current scope is itself an instance within a child
3929 -- unit, there will be duplications in the scope stack, and the
3930 -- unstacking mechanism in Inline_Instance_Body will fail.
3931 -- This loses some rare cases of optimization, and might be
3932 -- improved some day, if we can find a proper abstraction for
3933 -- "the complete compilation context" that can be saved and
3936 if Is_Generic_Instance (Current_Scope) then
3938 Curr_Unit : constant Entity_Id :=
3939 Cunit_Entity (Current_Sem_Unit);
3941 if Curr_Unit /= Current_Scope
3942 and then Is_Child_Unit (Curr_Unit)
3944 Inline_Now := False;
3951 (Unit_Requires_Body (Gen_Unit)
3952 or else Enclosing_Body_Present
3953 or else Present (Corresponding_Body (Gen_Decl)))
3954 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3955 and then not Is_Actual_Pack
3956 and then not Inline_Now
3957 and then (Operating_Mode = Generate_Code
3959 -- Need comment for this check ???
3961 or else (Operating_Mode = Check_Semantics
3962 and then (ASIS_Mode or GNATprove_Mode)));
3964 -- If front-end inlining is enabled or there are any subprograms
3965 -- marked with Inline_Always, do not instantiate body when within
3966 -- a generic context.
3968 if ((Front_End_Inlining or else Has_Inline_Always)
3969 and then not Expander_Active)
3970 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3972 Needs_Body := False;
3975 -- If the current context is generic, and the package being
3976 -- instantiated is declared within a formal package, there is no
3977 -- body to instantiate until the enclosing generic is instantiated
3978 -- and there is an actual for the formal package. If the formal
3979 -- package has parameters, we build a regular package instance for
3980 -- it, that precedes the original formal package declaration.
3982 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3984 Decl : constant Node_Id :=
3986 (Unit_Declaration_Node (Scope (Gen_Unit)));
3988 if Nkind (Decl) = N_Formal_Package_Declaration
3989 or else (Nkind (Decl) = N_Package_Declaration
3990 and then Is_List_Member (Decl)
3991 and then Present (Next (Decl))
3993 Nkind (Next (Decl)) =
3994 N_Formal_Package_Declaration)
3996 Needs_Body := False;
4002 -- For RCI unit calling stubs, we omit the instance body if the
4003 -- instance is the RCI library unit itself.
4005 -- However there is a special case for nested instances: in this case
4006 -- we do generate the instance body, as it might be required, e.g.
4007 -- because it provides stream attributes for some type used in the
4008 -- profile of a remote subprogram. This is consistent with 12.3(12),
4009 -- which indicates that the instance body occurs at the place of the
4010 -- instantiation, and thus is part of the RCI declaration, which is
4011 -- present on all client partitions (this is E.2.3(18)).
4013 -- Note that AI12-0002 may make it illegal at some point to have
4014 -- stream attributes defined in an RCI unit, in which case this
4015 -- special case will become unnecessary. In the meantime, there
4016 -- is known application code in production that depends on this
4017 -- being possible, so we definitely cannot eliminate the body in
4018 -- the case of nested instances for the time being.
4020 -- When we generate a nested instance body, calling stubs for any
4021 -- relevant subprogram will be be inserted immediately after the
4022 -- subprogram declarations, and will take precedence over the
4023 -- subsequent (original) body. (The stub and original body will be
4024 -- complete homographs, but this is permitted in an instance).
4025 -- (Could we do better and remove the original body???)
4027 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4028 and then Comes_From_Source (N)
4029 and then Nkind (Parent (N)) = N_Compilation_Unit
4031 Needs_Body := False;
4036 -- Here is a defence against a ludicrous number of instantiations
4037 -- caused by a circular set of instantiation attempts.
4039 if Pending_Instantiations.Last > Maximum_Instantiations then
4040 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4041 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4042 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4043 raise Unrecoverable_Error;
4046 -- Indicate that the enclosing scopes contain an instantiation,
4047 -- and that cleanup actions should be delayed until after the
4048 -- instance body is expanded.
4050 Check_Forward_Instantiation (Gen_Decl);
4051 if Nkind (N) = N_Package_Instantiation then
4053 Enclosing_Master : Entity_Id;
4056 -- Loop to search enclosing masters
4058 Enclosing_Master := Current_Scope;
4059 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4060 if Ekind (Enclosing_Master) = E_Package then
4061 if Is_Compilation_Unit (Enclosing_Master) then
4062 if In_Package_Body (Enclosing_Master) then
4064 (Body_Entity (Enclosing_Master));
4073 Enclosing_Master := Scope (Enclosing_Master);
4076 elsif Is_Generic_Unit (Enclosing_Master)
4077 or else Ekind (Enclosing_Master) = E_Void
4079 -- Cleanup actions will eventually be performed on the
4080 -- enclosing subprogram or package instance, if any.
4081 -- Enclosing scope is void in the formal part of a
4082 -- generic subprogram.
4087 if Ekind (Enclosing_Master) = E_Entry
4089 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4091 if not Expander_Active then
4095 Protected_Body_Subprogram (Enclosing_Master);
4099 Set_Delay_Cleanups (Enclosing_Master);
4101 while Ekind (Enclosing_Master) = E_Block loop
4102 Enclosing_Master := Scope (Enclosing_Master);
4105 if Is_Subprogram (Enclosing_Master) then
4106 Delay_Descriptors (Enclosing_Master);
4108 elsif Is_Task_Type (Enclosing_Master) then
4110 TBP : constant Node_Id :=
4111 Get_Task_Body_Procedure
4114 if Present (TBP) then
4115 Delay_Descriptors (TBP);
4116 Set_Delay_Cleanups (TBP);
4123 end loop Scope_Loop;
4126 -- Make entry in table
4128 Pending_Instantiations.Append
4130 Act_Decl => Act_Decl,
4131 Expander_Status => Expander_Active,
4132 Current_Sem_Unit => Current_Sem_Unit,
4133 Scope_Suppress => Scope_Suppress,
4134 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4135 Version => Ada_Version,
4136 Version_Pragma => Ada_Version_Pragma,
4137 Warnings => Save_Warnings,
4138 SPARK_Mode => SPARK_Mode,
4139 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4143 Set_Categorization_From_Pragmas (Act_Decl);
4145 if Parent_Installed then
4149 Set_Instance_Spec (N, Act_Decl);
4151 -- If not a compilation unit, insert the package declaration before
4152 -- the original instantiation node.
4154 if Nkind (Parent (N)) /= N_Compilation_Unit then
4155 Mark_Rewrite_Insertion (Act_Decl);
4156 Insert_Before (N, Act_Decl);
4158 if Has_Aspects (N) then
4159 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4161 -- The pragma created for a Default_Storage_Pool aspect must
4162 -- appear ahead of the declarations in the instance spec.
4163 -- Analysis has placed it after the instance node, so remove
4164 -- it and reinsert it properly now.
4167 ASN : constant Node_Id := First (Aspect_Specifications (N));
4168 A_Name : constant Name_Id := Chars (Identifier (ASN));
4172 if A_Name = Name_Default_Storage_Pool then
4173 if No (Visible_Declarations (Act_Spec)) then
4174 Set_Visible_Declarations (Act_Spec, New_List);
4178 while Present (Decl) loop
4179 if Nkind (Decl) = N_Pragma then
4181 Prepend (Decl, Visible_Declarations (Act_Spec));
4193 -- For an instantiation that is a compilation unit, place
4194 -- declaration on current node so context is complete for analysis
4195 -- (including nested instantiations). If this is the main unit,
4196 -- the declaration eventually replaces the instantiation node.
4197 -- If the instance body is created later, it replaces the
4198 -- instance node, and the declaration is attached to it
4199 -- (see Build_Instance_Compilation_Unit_Nodes).
4202 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4204 -- The entity for the current unit is the newly created one,
4205 -- and all semantic information is attached to it.
4207 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4209 -- If this is the main unit, replace the main entity as well
4211 if Current_Sem_Unit = Main_Unit then
4212 Main_Unit_Entity := Act_Decl_Id;
4216 Set_Unit (Parent (N), Act_Decl);
4217 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4218 Set_Package_Instantiation (Act_Decl_Id, N);
4220 -- Process aspect specifications of the instance node, if any, to
4221 -- take into account categorization pragmas before analyzing the
4224 if Has_Aspects (N) then
4225 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4229 Set_Unit (Parent (N), N);
4230 Set_Body_Required (Parent (N), False);
4232 -- We never need elaboration checks on instantiations, since by
4233 -- definition, the body instantiation is elaborated at the same
4234 -- time as the spec instantiation.
4236 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4237 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4240 Check_Elab_Instantiation (N);
4242 if ABE_Is_Certain (N) and then Needs_Body then
4243 Pending_Instantiations.Decrement_Last;
4246 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4248 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4249 First_Private_Entity (Act_Decl_Id));
4251 -- If the instantiation will receive a body, the unit will be
4252 -- transformed into a package body, and receive its own elaboration
4253 -- entity. Otherwise, the nature of the unit is now a package
4256 if Nkind (Parent (N)) = N_Compilation_Unit
4257 and then not Needs_Body
4259 Rewrite (N, Act_Decl);
4262 if Present (Corresponding_Body (Gen_Decl))
4263 or else Unit_Requires_Body (Gen_Unit)
4265 Set_Has_Completion (Act_Decl_Id);
4268 Check_Formal_Packages (Act_Decl_Id);
4270 Restore_Hidden_Primitives (Vis_Prims_List);
4271 Restore_Private_Views (Act_Decl_Id);
4273 Inherit_Context (Gen_Decl, N);
4275 if Parent_Installed then
4280 Env_Installed := False;
4283 Validate_Categorization_Dependency (N, Act_Decl_Id);
4285 -- There used to be a check here to prevent instantiations in local
4286 -- contexts if the No_Local_Allocators restriction was active. This
4287 -- check was removed by a binding interpretation in AI-95-00130/07,
4288 -- but we retain the code for documentation purposes.
4290 -- if Ekind (Act_Decl_Id) /= E_Void
4291 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4293 -- Check_Restriction (No_Local_Allocators, N);
4297 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4300 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4301 -- be used as defining identifiers for a formal package and for the
4302 -- corresponding expanded package.
4304 if Nkind (N) = N_Formal_Package_Declaration then
4305 Act_Decl_Id := New_Copy (Defining_Entity (N));
4306 Set_Comes_From_Source (Act_Decl_Id, True);
4307 Set_Is_Generic_Instance (Act_Decl_Id, False);
4308 Set_Defining_Identifier (N, Act_Decl_Id);
4311 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4312 SPARK_Mode := Save_SM;
4313 SPARK_Mode_Pragma := Save_SMP;
4314 Style_Check := Save_Style_Check;
4316 if SPARK_Mode = On then
4317 Dynamic_Elaboration_Checks := False;
4320 -- Check that if N is an instantiation of System.Dim_Float_IO or
4321 -- System.Dim_Integer_IO, the formal type has a dimension system.
4323 if Nkind (N) = N_Package_Instantiation
4324 and then Is_Dim_IO_Package_Instantiation (N)
4327 Assoc : constant Node_Id := First (Generic_Associations (N));
4329 if not Has_Dimension_System
4330 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4332 Error_Msg_N ("type with a dimension system expected", Assoc);
4338 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4339 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4343 when Instantiation_Error =>
4344 if Parent_Installed then
4348 if Env_Installed then
4352 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4353 SPARK_Mode := Save_SM;
4354 SPARK_Mode_Pragma := Save_SMP;
4355 Style_Check := Save_Style_Check;
4357 if SPARK_Mode = On then
4358 Dynamic_Elaboration_Checks := False;
4360 end Analyze_Package_Instantiation;
4362 --------------------------
4363 -- Inline_Instance_Body --
4364 --------------------------
4366 procedure Inline_Instance_Body
4368 Gen_Unit : Entity_Id;
4371 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4372 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4373 Gen_Comp : constant Entity_Id :=
4374 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4376 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4377 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4378 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4379 -- to provide a clean environment for analysis of the inlined body will
4380 -- eliminate any previously set SPARK_Mode.
4382 Scope_Stack_Depth : constant Int :=
4383 Scope_Stack.Last - Scope_Stack.First + 1;
4385 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4386 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4387 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4388 Curr_Scope : Entity_Id := Empty;
4390 Num_Inner : Int := 0;
4391 Num_Scopes : Int := 0;
4392 N_Instances : Int := 0;
4393 Removed : Boolean := False;
4398 -- Case of generic unit defined in another unit. We must remove the
4399 -- complete context of the current unit to install that of the generic.
4401 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4403 -- Add some comments for the following two loops ???
4406 while Present (S) and then S /= Standard_Standard loop
4408 Num_Scopes := Num_Scopes + 1;
4410 Use_Clauses (Num_Scopes) :=
4412 (Scope_Stack.Last - Num_Scopes + 1).
4414 End_Use_Clauses (Use_Clauses (Num_Scopes));
4416 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4417 or else Scope_Stack.Table
4418 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4421 exit when Is_Generic_Instance (S)
4422 and then (In_Package_Body (S)
4423 or else Ekind (S) = E_Procedure
4424 or else Ekind (S) = E_Function);
4428 Vis := Is_Immediately_Visible (Gen_Comp);
4430 -- Find and save all enclosing instances
4435 and then S /= Standard_Standard
4437 if Is_Generic_Instance (S) then
4438 N_Instances := N_Instances + 1;
4439 Instances (N_Instances) := S;
4441 exit when In_Package_Body (S);
4447 -- Remove context of current compilation unit, unless we are within a
4448 -- nested package instantiation, in which case the context has been
4449 -- removed previously.
4451 -- If current scope is the body of a child unit, remove context of
4452 -- spec as well. If an enclosing scope is an instance body, the
4453 -- context has already been removed, but the entities in the body
4454 -- must be made invisible as well.
4457 while Present (S) and then S /= Standard_Standard loop
4458 if Is_Generic_Instance (S)
4459 and then (In_Package_Body (S)
4460 or else Ekind_In (S, E_Procedure, E_Function))
4462 -- We still have to remove the entities of the enclosing
4463 -- instance from direct visibility.
4468 E := First_Entity (S);
4469 while Present (E) loop
4470 Set_Is_Immediately_Visible (E, False);
4479 or else (Ekind (Curr_Unit) = E_Package_Body
4480 and then S = Spec_Entity (Curr_Unit))
4481 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4482 and then S = Corresponding_Spec
4483 (Unit_Declaration_Node (Curr_Unit)))
4487 -- Remove entities in current scopes from visibility, so that
4488 -- instance body is compiled in a clean environment.
4490 List := Save_Scope_Stack (Handle_Use => False);
4492 if Is_Child_Unit (S) then
4494 -- Remove child unit from stack, as well as inner scopes.
4495 -- Removing the context of a child unit removes parent units
4498 while Current_Scope /= S loop
4499 Num_Inner := Num_Inner + 1;
4500 Inner_Scopes (Num_Inner) := Current_Scope;
4505 Remove_Context (Curr_Comp);
4509 Remove_Context (Curr_Comp);
4512 if Ekind (Curr_Unit) = E_Package_Body then
4513 Remove_Context (Library_Unit (Curr_Comp));
4520 pragma Assert (Num_Inner < Num_Scopes);
4522 -- The inlined package body must be analyzed with the SPARK_Mode of
4523 -- the enclosing context, otherwise the body may cause bogus errors
4524 -- if a configuration SPARK_Mode pragma in in effect.
4526 Push_Scope (Standard_Standard);
4527 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4528 Instantiate_Package_Body
4531 Act_Decl => Act_Decl,
4532 Expander_Status => Expander_Active,
4533 Current_Sem_Unit => Current_Sem_Unit,
4534 Scope_Suppress => Scope_Suppress,
4535 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4536 Version => Ada_Version,
4537 Version_Pragma => Ada_Version_Pragma,
4538 Warnings => Save_Warnings,
4539 SPARK_Mode => Save_SM,
4540 SPARK_Mode_Pragma => Save_SMP)),
4541 Inlined_Body => True);
4547 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4549 -- Reset Generic_Instance flag so that use clauses can be installed
4550 -- in the proper order. (See Use_One_Package for effect of enclosing
4551 -- instances on processing of use clauses).
4553 for J in 1 .. N_Instances loop
4554 Set_Is_Generic_Instance (Instances (J), False);
4558 Install_Context (Curr_Comp);
4560 if Present (Curr_Scope)
4561 and then Is_Child_Unit (Curr_Scope)
4563 Push_Scope (Curr_Scope);
4564 Set_Is_Immediately_Visible (Curr_Scope);
4566 -- Finally, restore inner scopes as well
4568 for J in reverse 1 .. Num_Inner loop
4569 Push_Scope (Inner_Scopes (J));
4573 Restore_Scope_Stack (List, Handle_Use => False);
4575 if Present (Curr_Scope)
4577 (In_Private_Part (Curr_Scope)
4578 or else In_Package_Body (Curr_Scope))
4580 -- Install private declaration of ancestor units, which are
4581 -- currently available. Restore_Scope_Stack and Install_Context
4582 -- only install the visible part of parents.
4587 Par := Scope (Curr_Scope);
4588 while (Present (Par)) and then Par /= Standard_Standard loop
4589 Install_Private_Declarations (Par);
4596 -- Restore use clauses. For a child unit, use clauses in the parents
4597 -- are restored when installing the context, so only those in inner
4598 -- scopes (and those local to the child unit itself) need to be
4599 -- installed explicitly.
4601 if Is_Child_Unit (Curr_Unit) and then Removed then
4602 for J in reverse 1 .. Num_Inner + 1 loop
4603 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4605 Install_Use_Clauses (Use_Clauses (J));
4609 for J in reverse 1 .. Num_Scopes loop
4610 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4612 Install_Use_Clauses (Use_Clauses (J));
4616 -- Restore status of instances. If one of them is a body, make its
4617 -- local entities visible again.
4624 for J in 1 .. N_Instances loop
4625 Inst := Instances (J);
4626 Set_Is_Generic_Instance (Inst, True);
4628 if In_Package_Body (Inst)
4629 or else Ekind_In (S, E_Procedure, E_Function)
4631 E := First_Entity (Instances (J));
4632 while Present (E) loop
4633 Set_Is_Immediately_Visible (E);
4640 -- If generic unit is in current unit, current context is correct. Note
4641 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4642 -- enclosing scopes were removed.
4645 Instantiate_Package_Body
4648 Act_Decl => Act_Decl,
4649 Expander_Status => Expander_Active,
4650 Current_Sem_Unit => Current_Sem_Unit,
4651 Scope_Suppress => Scope_Suppress,
4652 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4653 Version => Ada_Version,
4654 Version_Pragma => Ada_Version_Pragma,
4655 Warnings => Save_Warnings,
4656 SPARK_Mode => SPARK_Mode,
4657 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4658 Inlined_Body => True);
4660 end Inline_Instance_Body;
4662 -------------------------------------
4663 -- Analyze_Procedure_Instantiation --
4664 -------------------------------------
4666 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4668 Analyze_Subprogram_Instantiation (N, E_Procedure);
4669 end Analyze_Procedure_Instantiation;
4671 -----------------------------------
4672 -- Need_Subprogram_Instance_Body --
4673 -----------------------------------
4675 function Need_Subprogram_Instance_Body
4677 Subp : Entity_Id) return Boolean
4680 -- Must be inlined (or inlined renaming)
4682 if (Is_In_Main_Unit (N)
4683 or else Is_Inlined (Subp)
4684 or else Is_Inlined (Alias (Subp)))
4686 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4688 and then (Operating_Mode = Generate_Code
4689 or else (Operating_Mode = Check_Semantics
4690 and then (ASIS_Mode or GNATprove_Mode)))
4692 -- The body is needed when generating code (full expansion), in ASIS
4693 -- mode for other tools, and in GNATprove mode (special expansion) for
4694 -- formal verification of the body itself.
4696 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4698 -- No point in inlining if ABE is inevitable
4700 and then not ABE_Is_Certain (N)
4702 -- Or if subprogram is eliminated
4704 and then not Is_Eliminated (Subp)
4706 Pending_Instantiations.Append
4708 Act_Decl => Unit_Declaration_Node (Subp),
4709 Expander_Status => Expander_Active,
4710 Current_Sem_Unit => Current_Sem_Unit,
4711 Scope_Suppress => Scope_Suppress,
4712 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4713 Version => Ada_Version,
4714 Version_Pragma => Ada_Version_Pragma,
4715 Warnings => Save_Warnings,
4716 SPARK_Mode => SPARK_Mode,
4717 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4720 -- Here if not inlined, or we ignore the inlining
4725 end Need_Subprogram_Instance_Body;
4727 --------------------------------------
4728 -- Analyze_Subprogram_Instantiation --
4729 --------------------------------------
4731 procedure Analyze_Subprogram_Instantiation
4735 Loc : constant Source_Ptr := Sloc (N);
4736 Gen_Id : constant Node_Id := Name (N);
4738 Anon_Id : constant Entity_Id :=
4739 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4740 Chars => New_External_Name
4741 (Chars (Defining_Entity (N)), 'R'));
4743 Act_Decl_Id : Entity_Id;
4748 Env_Installed : Boolean := False;
4749 Gen_Unit : Entity_Id;
4751 Pack_Id : Entity_Id;
4752 Parent_Installed : Boolean := False;
4754 Renaming_List : List_Id;
4755 -- The list of declarations that link formals and actuals of the
4756 -- instance. These are subtype declarations for formal types, and
4757 -- renaming declarations for other formals. The subprogram declaration
4758 -- for the instance is then appended to the list, and the last item on
4759 -- the list is the renaming declaration for the instance.
4761 procedure Analyze_Instance_And_Renamings;
4762 -- The instance must be analyzed in a context that includes the mappings
4763 -- of generic parameters into actuals. We create a package declaration
4764 -- for this purpose, and a subprogram with an internal name within the
4765 -- package. The subprogram instance is simply an alias for the internal
4766 -- subprogram, declared in the current scope.
4768 procedure Build_Subprogram_Renaming;
4769 -- If the subprogram is recursive, there are occurrences of the name of
4770 -- the generic within the body, which must resolve to the current
4771 -- instance. We add a renaming declaration after the declaration, which
4772 -- is available in the instance body, as well as in the analysis of
4773 -- aspects that appear in the generic. This renaming declaration is
4774 -- inserted after the instance declaration which it renames.
4776 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4777 -- Instantiate all source pragmas found in the contract of the generic
4778 -- subprogram declaration template denoted by Templ. The instantiated
4779 -- pragmas are added to list Renaming_List.
4781 ------------------------------------
4782 -- Analyze_Instance_And_Renamings --
4783 ------------------------------------
4785 procedure Analyze_Instance_And_Renamings is
4786 Def_Ent : constant Entity_Id := Defining_Entity (N);
4787 Pack_Decl : Node_Id;
4790 if Nkind (Parent (N)) = N_Compilation_Unit then
4792 -- For the case of a compilation unit, the container package has
4793 -- the same name as the instantiation, to insure that the binder
4794 -- calls the elaboration procedure with the right name. Copy the
4795 -- entity of the instance, which may have compilation level flags
4796 -- (e.g. Is_Child_Unit) set.
4798 Pack_Id := New_Copy (Def_Ent);
4801 -- Otherwise we use the name of the instantiation concatenated
4802 -- with its source position to ensure uniqueness if there are
4803 -- several instantiations with the same name.
4806 Make_Defining_Identifier (Loc,
4807 Chars => New_External_Name
4808 (Related_Id => Chars (Def_Ent),
4810 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4814 Make_Package_Declaration (Loc,
4815 Specification => Make_Package_Specification (Loc,
4816 Defining_Unit_Name => Pack_Id,
4817 Visible_Declarations => Renaming_List,
4818 End_Label => Empty));
4820 Set_Instance_Spec (N, Pack_Decl);
4821 Set_Is_Generic_Instance (Pack_Id);
4822 Set_Debug_Info_Needed (Pack_Id);
4824 -- Case of not a compilation unit
4826 if Nkind (Parent (N)) /= N_Compilation_Unit then
4827 Mark_Rewrite_Insertion (Pack_Decl);
4828 Insert_Before (N, Pack_Decl);
4829 Set_Has_Completion (Pack_Id);
4831 -- Case of an instantiation that is a compilation unit
4833 -- Place declaration on current node so context is complete for
4834 -- analysis (including nested instantiations), and for use in a
4835 -- context_clause (see Analyze_With_Clause).
4838 Set_Unit (Parent (N), Pack_Decl);
4839 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4842 Analyze (Pack_Decl);
4843 Check_Formal_Packages (Pack_Id);
4844 Set_Is_Generic_Instance (Pack_Id, False);
4846 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4849 -- Body of the enclosing package is supplied when instantiating the
4850 -- subprogram body, after semantic analysis is completed.
4852 if Nkind (Parent (N)) = N_Compilation_Unit then
4854 -- Remove package itself from visibility, so it does not
4855 -- conflict with subprogram.
4857 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4859 -- Set name and scope of internal subprogram so that the proper
4860 -- external name will be generated. The proper scope is the scope
4861 -- of the wrapper package. We need to generate debugging info for
4862 -- the internal subprogram, so set flag accordingly.
4864 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4865 Set_Scope (Anon_Id, Scope (Pack_Id));
4867 -- Mark wrapper package as referenced, to avoid spurious warnings
4868 -- if the instantiation appears in various with_ clauses of
4869 -- subunits of the main unit.
4871 Set_Referenced (Pack_Id);
4874 Set_Is_Generic_Instance (Anon_Id);
4875 Set_Debug_Info_Needed (Anon_Id);
4876 Act_Decl_Id := New_Copy (Anon_Id);
4878 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4879 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4880 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4882 -- Subprogram instance comes from source only if generic does
4884 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4886 -- The signature may involve types that are not frozen yet, but the
4887 -- subprogram will be frozen at the point the wrapper package is
4888 -- frozen, so it does not need its own freeze node. In fact, if one
4889 -- is created, it might conflict with the freezing actions from the
4892 Set_Has_Delayed_Freeze (Anon_Id, False);
4894 -- If the instance is a child unit, mark the Id accordingly. Mark
4895 -- the anonymous entity as well, which is the real subprogram and
4896 -- which is used when the instance appears in a context clause.
4897 -- Similarly, propagate the Is_Eliminated flag to handle properly
4898 -- nested eliminated subprograms.
4900 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4901 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4902 New_Overloaded_Entity (Act_Decl_Id);
4903 Check_Eliminated (Act_Decl_Id);
4904 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4906 -- In compilation unit case, kill elaboration checks on the
4907 -- instantiation, since they are never needed -- the body is
4908 -- instantiated at the same point as the spec.
4910 if Nkind (Parent (N)) = N_Compilation_Unit then
4911 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4912 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4913 Set_Is_Compilation_Unit (Anon_Id);
4915 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4918 -- The instance is not a freezing point for the new subprogram
4920 Set_Is_Frozen (Act_Decl_Id, False);
4922 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4923 Valid_Operator_Definition (Act_Decl_Id);
4926 Set_Alias (Act_Decl_Id, Anon_Id);
4927 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4928 Set_Has_Completion (Act_Decl_Id);
4929 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4931 if Nkind (Parent (N)) = N_Compilation_Unit then
4932 Set_Body_Required (Parent (N), False);
4934 end Analyze_Instance_And_Renamings;
4936 -------------------------------
4937 -- Build_Subprogram_Renaming --
4938 -------------------------------
4940 procedure Build_Subprogram_Renaming is
4941 Renaming_Decl : Node_Id;
4942 Unit_Renaming : Node_Id;
4946 Make_Subprogram_Renaming_Declaration (Loc,
4949 (Specification (Original_Node (Gen_Decl)),
4951 Instantiating => True),
4952 Name => New_Occurrence_Of (Anon_Id, Loc));
4954 -- The generic may be a a child unit. The renaming needs an
4955 -- identifier with the proper name.
4957 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4958 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4960 -- If there is a formal subprogram with the same name as the unit
4961 -- itself, do not add this renaming declaration, to prevent
4962 -- ambiguities when there is a call with that name in the body.
4963 -- This is a partial and ugly fix for one ACATS test. ???
4965 Renaming_Decl := First (Renaming_List);
4966 while Present (Renaming_Decl) loop
4967 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4969 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4974 Next (Renaming_Decl);
4977 if No (Renaming_Decl) then
4978 Append (Unit_Renaming, Renaming_List);
4980 end Build_Subprogram_Renaming;
4982 -------------------------------------
4983 -- Instantiate_Subprogram_Contract --
4984 -------------------------------------
4986 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
4987 procedure Instantiate_Pragmas (First_Prag : Node_Id);
4988 -- Instantiate all contract-related source pragmas found in the list
4989 -- starting with pragma First_Prag. Each instantiated pragma is added
4990 -- to list Renaming_List.
4992 -------------------------
4993 -- Instantiate_Pragmas --
4994 -------------------------
4996 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
4997 Inst_Prag : Node_Id;
5002 while Present (Prag) loop
5003 if Is_Generic_Contract_Pragma (Prag) then
5005 Copy_Generic_Node (Prag, Empty, Instantiating => True);
5007 Set_Analyzed (Inst_Prag, False);
5008 Append_To (Renaming_List, Inst_Prag);
5011 Prag := Next_Pragma (Prag);
5013 end Instantiate_Pragmas;
5017 Items : constant Node_Id := Contract (Defining_Entity (Templ));
5019 -- Start of processing for Instantiate_Subprogram_Contract
5022 if Present (Items) then
5023 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5024 Instantiate_Pragmas (Contract_Test_Cases (Items));
5025 Instantiate_Pragmas (Classifications (Items));
5027 end Instantiate_Subprogram_Contract;
5031 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5032 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5034 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5035 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5036 -- Save the SPARK_Mode-related data for restore on exit
5038 Vis_Prims_List : Elist_Id := No_Elist;
5039 -- List of primitives made temporarily visible in the instantiation
5040 -- to match the visibility of the formal type
5042 -- Start of processing for Analyze_Subprogram_Instantiation
5045 Check_SPARK_05_Restriction ("generic is not allowed", N);
5047 -- Very first thing: check for special Text_IO unit in case we are
5048 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5049 -- such an instantiation is bogus (these are packages, not subprograms),
5050 -- but we get a better error message if we do this.
5052 Check_Text_IO_Special_Unit (Gen_Id);
5054 -- Make node global for error reporting
5056 Instantiation_Node := N;
5058 -- For package instantiations we turn off style checks, because they
5059 -- will have been emitted in the generic. For subprogram instantiations
5060 -- we want to apply at least the check on overriding indicators so we
5061 -- do not modify the style check status.
5063 -- The renaming declarations for the actuals do not come from source and
5064 -- will not generate spurious warnings.
5066 Preanalyze_Actuals (N);
5069 Env_Installed := True;
5070 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5071 Gen_Unit := Entity (Gen_Id);
5073 Generate_Reference (Gen_Unit, Gen_Id);
5075 if Nkind (Gen_Id) = N_Identifier
5076 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5079 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5082 if Etype (Gen_Unit) = Any_Type then
5087 -- Verify that it is a generic subprogram of the right kind, and that
5088 -- it does not lead to a circular instantiation.
5090 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5092 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5094 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5096 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5098 elsif In_Open_Scopes (Gen_Unit) then
5099 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5102 -- If the context of the instance is subject to SPARK_Mode "off" or
5103 -- the annotation is altogether missing, set the global flag which
5104 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5107 if SPARK_Mode /= On then
5108 Ignore_Pragma_SPARK_Mode := True;
5111 Set_Entity (Gen_Id, Gen_Unit);
5112 Set_Is_Instantiated (Gen_Unit);
5114 if In_Extended_Main_Source_Unit (N) then
5115 Generate_Reference (Gen_Unit, N);
5118 -- If renaming, get original unit
5120 if Present (Renamed_Object (Gen_Unit))
5121 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5124 Gen_Unit := Renamed_Object (Gen_Unit);
5125 Set_Is_Instantiated (Gen_Unit);
5126 Generate_Reference (Gen_Unit, N);
5129 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5130 Error_Msg_Node_2 := Current_Scope;
5132 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5133 Circularity_Detected := True;
5134 Restore_Hidden_Primitives (Vis_Prims_List);
5138 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5140 -- Initialize renamings map, for error checking
5142 Generic_Renamings.Set_Last (0);
5143 Generic_Renamings_HTable.Reset;
5145 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5147 -- Copy original generic tree, to produce text for instantiation
5151 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5153 -- Inherit overriding indicator from instance node
5155 Act_Spec := Specification (Act_Tree);
5156 Set_Must_Override (Act_Spec, Must_Override (N));
5157 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5160 Analyze_Associations
5162 Formals => Generic_Formal_Declarations (Act_Tree),
5163 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5165 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5167 -- The subprogram itself cannot contain a nested instance, so the
5168 -- current parent is left empty.
5170 Set_Instance_Env (Gen_Unit, Empty);
5172 -- Build the subprogram declaration, which does not appear in the
5173 -- generic template, and give it a sloc consistent with that of the
5176 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5177 Set_Generic_Parent (Act_Spec, Gen_Unit);
5179 Make_Subprogram_Declaration (Sloc (Act_Spec),
5180 Specification => Act_Spec);
5182 -- The aspects have been copied previously, but they have to be
5183 -- linked explicitly to the new subprogram declaration. Explicit
5184 -- pre/postconditions on the instance are analyzed below, in a
5187 Move_Aspects (Act_Tree, To => Act_Decl);
5188 Set_Categorization_From_Pragmas (Act_Decl);
5190 if Parent_Installed then
5194 Append (Act_Decl, Renaming_List);
5196 -- Contract-related source pragmas that follow a generic subprogram
5197 -- must be instantiated explicitly because they are not part of the
5198 -- subprogram template.
5200 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5201 Build_Subprogram_Renaming;
5203 Analyze_Instance_And_Renamings;
5205 -- If the generic is marked Import (Intrinsic), then so is the
5206 -- instance. This indicates that there is no body to instantiate. If
5207 -- generic is marked inline, so it the instance, and the anonymous
5208 -- subprogram it renames. If inlined, or else if inlining is enabled
5209 -- for the compilation, we generate the instance body even if it is
5210 -- not within the main unit.
5212 if Is_Intrinsic_Subprogram (Gen_Unit) then
5213 Set_Is_Intrinsic_Subprogram (Anon_Id);
5214 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5216 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5217 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5221 -- Inherit convention from generic unit. Intrinsic convention, as for
5222 -- an instance of unchecked conversion, is not inherited because an
5223 -- explicit Ada instance has been created.
5225 if Has_Convention_Pragma (Gen_Unit)
5226 and then Convention (Gen_Unit) /= Convention_Intrinsic
5228 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5229 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5232 Generate_Definition (Act_Decl_Id);
5234 -- Inherit all inlining-related flags which apply to the generic in
5235 -- the subprogram and its declaration.
5237 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5238 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5240 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5241 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5243 Set_Has_Pragma_Inline_Always
5244 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5245 Set_Has_Pragma_Inline_Always
5246 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5248 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5249 Check_Elab_Instantiation (N);
5252 if Is_Dispatching_Operation (Act_Decl_Id)
5253 and then Ada_Version >= Ada_2005
5259 Formal := First_Formal (Act_Decl_Id);
5260 while Present (Formal) loop
5261 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5262 and then Is_Controlling_Formal (Formal)
5263 and then not Can_Never_Be_Null (Formal)
5266 ("access parameter& is controlling,", N, Formal);
5268 ("\corresponding parameter of & must be "
5269 & "explicitly null-excluding", N, Gen_Id);
5272 Next_Formal (Formal);
5277 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5279 Validate_Categorization_Dependency (N, Act_Decl_Id);
5281 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5282 Inherit_Context (Gen_Decl, N);
5284 Restore_Private_Views (Pack_Id, False);
5286 -- If the context requires a full instantiation, mark node for
5287 -- subsequent construction of the body.
5289 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5290 Check_Forward_Instantiation (Gen_Decl);
5292 -- The wrapper package is always delayed, because it does not
5293 -- constitute a freeze point, but to insure that the freeze
5294 -- node is placed properly, it is created directly when
5295 -- instantiating the body (otherwise the freeze node might
5296 -- appear to early for nested instantiations).
5298 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5300 -- For ASIS purposes, indicate that the wrapper package has
5301 -- replaced the instantiation node.
5303 Rewrite (N, Unit (Parent (N)));
5304 Set_Unit (Parent (N), N);
5307 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5309 -- Replace instance node for library-level instantiations of
5310 -- intrinsic subprograms, for ASIS use.
5312 Rewrite (N, Unit (Parent (N)));
5313 Set_Unit (Parent (N), N);
5316 if Parent_Installed then
5320 Restore_Hidden_Primitives (Vis_Prims_List);
5322 Env_Installed := False;
5323 Generic_Renamings.Set_Last (0);
5324 Generic_Renamings_HTable.Reset;
5326 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5327 SPARK_Mode := Save_SM;
5328 SPARK_Mode_Pragma := Save_SMP;
5330 if SPARK_Mode = On then
5331 Dynamic_Elaboration_Checks := False;
5337 if Has_Aspects (N) then
5338 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5342 when Instantiation_Error =>
5343 if Parent_Installed then
5347 if Env_Installed then
5351 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5352 SPARK_Mode := Save_SM;
5353 SPARK_Mode_Pragma := Save_SMP;
5355 if SPARK_Mode = On then
5356 Dynamic_Elaboration_Checks := False;
5358 end Analyze_Subprogram_Instantiation;
5360 -------------------------
5361 -- Get_Associated_Node --
5362 -------------------------
5364 function Get_Associated_Node (N : Node_Id) return Node_Id is
5368 Assoc := Associated_Node (N);
5370 if Nkind (Assoc) /= Nkind (N) then
5373 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5377 -- If the node is part of an inner generic, it may itself have been
5378 -- remapped into a further generic copy. Associated_Node is otherwise
5379 -- used for the entity of the node, and will be of a different node
5380 -- kind, or else N has been rewritten as a literal or function call.
5382 while Present (Associated_Node (Assoc))
5383 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5385 Assoc := Associated_Node (Assoc);
5388 -- Follow and additional link in case the final node was rewritten.
5389 -- This can only happen with nested generic units.
5391 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5392 and then Present (Associated_Node (Assoc))
5393 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5394 N_Explicit_Dereference,
5399 Assoc := Associated_Node (Assoc);
5402 -- An additional special case: an unconstrained type in an object
5403 -- declaration may have been rewritten as a local subtype constrained
5404 -- by the expression in the declaration. We need to recover the
5405 -- original entity which may be global.
5407 if Present (Original_Node (Assoc))
5408 and then Nkind (Parent (N)) = N_Object_Declaration
5410 Assoc := Original_Node (Assoc);
5415 end Get_Associated_Node;
5417 ----------------------------
5418 -- Build_Function_Wrapper --
5419 ----------------------------
5421 function Build_Function_Wrapper
5422 (Formal_Subp : Entity_Id;
5423 Actual_Subp : Entity_Id) return Node_Id
5425 Loc : constant Source_Ptr := Sloc (Current_Scope);
5426 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5429 Func_Name : Node_Id;
5431 Parm_Type : Node_Id;
5432 Profile : List_Id := New_List;
5439 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5441 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5442 Set_Ekind (Func, E_Function);
5443 Set_Is_Generic_Actual_Subprogram (Func);
5445 Actuals := New_List;
5446 Profile := New_List;
5448 Act_F := First_Formal (Actual_Subp);
5449 Form_F := First_Formal (Formal_Subp);
5450 while Present (Form_F) loop
5452 -- Create new formal for profile of wrapper, and add a reference
5453 -- to it in the list of actuals for the enclosing call. The name
5454 -- must be that of the formal in the formal subprogram, because
5455 -- calls to it in the generic body may use named associations.
5457 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5460 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5463 Make_Parameter_Specification (Loc,
5464 Defining_Identifier => New_F,
5465 Parameter_Type => Parm_Type));
5467 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5468 Next_Formal (Form_F);
5470 if Present (Act_F) then
5471 Next_Formal (Act_F);
5476 Make_Function_Specification (Loc,
5477 Defining_Unit_Name => Func,
5478 Parameter_Specifications => Profile,
5479 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5482 Make_Expression_Function (Loc,
5483 Specification => Spec,
5485 Make_Function_Call (Loc,
5487 Parameter_Associations => Actuals));
5490 end Build_Function_Wrapper;
5492 ----------------------------
5493 -- Build_Operator_Wrapper --
5494 ----------------------------
5496 function Build_Operator_Wrapper
5497 (Formal_Subp : Entity_Id;
5498 Actual_Subp : Entity_Id) return Node_Id
5500 Loc : constant Source_Ptr := Sloc (Current_Scope);
5501 Ret_Type : constant Entity_Id :=
5502 Get_Instance_Of (Etype (Formal_Subp));
5503 Op_Type : constant Entity_Id :=
5504 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5505 Is_Binary : constant Boolean :=
5506 Present (Next_Formal (First_Formal (Formal_Subp)));
5517 Op_Name := Chars (Actual_Subp);
5519 -- Create entities for wrapper function and its formals
5521 F1 := Make_Temporary (Loc, 'A');
5522 F2 := Make_Temporary (Loc, 'B');
5523 L := New_Occurrence_Of (F1, Loc);
5524 R := New_Occurrence_Of (F2, Loc);
5526 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5527 Set_Ekind (Func, E_Function);
5528 Set_Is_Generic_Actual_Subprogram (Func);
5531 Make_Function_Specification (Loc,
5532 Defining_Unit_Name => Func,
5533 Parameter_Specifications => New_List (
5534 Make_Parameter_Specification (Loc,
5535 Defining_Identifier => F1,
5536 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5537 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5540 Append_To (Parameter_Specifications (Spec),
5541 Make_Parameter_Specification (Loc,
5542 Defining_Identifier => F2,
5543 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5546 -- Build expression as a function call, or as an operator node
5547 -- that corresponds to the name of the actual, starting with
5548 -- binary operators.
5550 if Op_Name not in Any_Operator_Name then
5552 Make_Function_Call (Loc,
5554 New_Occurrence_Of (Actual_Subp, Loc),
5555 Parameter_Associations => New_List (L));
5558 Append_To (Parameter_Associations (Expr), R);
5563 elsif Is_Binary then
5564 if Op_Name = Name_Op_And then
5565 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5566 elsif Op_Name = Name_Op_Or then
5567 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5568 elsif Op_Name = Name_Op_Xor then
5569 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5570 elsif Op_Name = Name_Op_Eq then
5571 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5572 elsif Op_Name = Name_Op_Ne then
5573 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5574 elsif Op_Name = Name_Op_Le then
5575 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5576 elsif Op_Name = Name_Op_Gt then
5577 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5578 elsif Op_Name = Name_Op_Ge then
5579 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5580 elsif Op_Name = Name_Op_Lt then
5581 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5582 elsif Op_Name = Name_Op_Add then
5583 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5584 elsif Op_Name = Name_Op_Subtract then
5585 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5586 elsif Op_Name = Name_Op_Concat then
5587 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5588 elsif Op_Name = Name_Op_Multiply then
5589 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5590 elsif Op_Name = Name_Op_Divide then
5591 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5592 elsif Op_Name = Name_Op_Mod then
5593 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5594 elsif Op_Name = Name_Op_Rem then
5595 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5596 elsif Op_Name = Name_Op_Expon then
5597 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5603 if Op_Name = Name_Op_Add then
5604 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5605 elsif Op_Name = Name_Op_Subtract then
5606 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5607 elsif Op_Name = Name_Op_Abs then
5608 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5609 elsif Op_Name = Name_Op_Not then
5610 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5615 Make_Expression_Function (Loc,
5616 Specification => Spec,
5617 Expression => Expr);
5620 end Build_Operator_Wrapper;
5622 -------------------------------------------
5623 -- Build_Instance_Compilation_Unit_Nodes --
5624 -------------------------------------------
5626 procedure Build_Instance_Compilation_Unit_Nodes
5631 Decl_Cunit : Node_Id;
5632 Body_Cunit : Node_Id;
5634 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5635 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5638 -- A new compilation unit node is built for the instance declaration
5641 Make_Compilation_Unit (Sloc (N),
5642 Context_Items => Empty_List,
5644 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5646 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5648 -- The new compilation unit is linked to its body, but both share the
5649 -- same file, so we do not set Body_Required on the new unit so as not
5650 -- to create a spurious dependency on a non-existent body in the ali.
5651 -- This simplifies CodePeer unit traversal.
5653 -- We use the original instantiation compilation unit as the resulting
5654 -- compilation unit of the instance, since this is the main unit.
5656 Rewrite (N, Act_Body);
5658 -- Propagate the aspect specifications from the package body template to
5659 -- the instantiated version of the package body.
5661 if Has_Aspects (Act_Body) then
5662 Set_Aspect_Specifications
5663 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5666 Body_Cunit := Parent (N);
5668 -- The two compilation unit nodes are linked by the Library_Unit field
5670 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5671 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5673 -- Preserve the private nature of the package if needed
5675 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5677 -- If the instance is not the main unit, its context, categorization
5678 -- and elaboration entity are not relevant to the compilation.
5680 if Body_Cunit /= Cunit (Main_Unit) then
5681 Make_Instance_Unit (Body_Cunit, In_Main => False);
5685 -- The context clause items on the instantiation, which are now attached
5686 -- to the body compilation unit (since the body overwrote the original
5687 -- instantiation node), semantically belong on the spec, so copy them
5688 -- there. It's harmless to leave them on the body as well. In fact one
5689 -- could argue that they belong in both places.
5691 Citem := First (Context_Items (Body_Cunit));
5692 while Present (Citem) loop
5693 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5697 -- Propagate categorization flags on packages, so that they appear in
5698 -- the ali file for the spec of the unit.
5700 if Ekind (New_Main) = E_Package then
5701 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5702 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5703 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5704 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5705 Set_Is_Remote_Call_Interface
5706 (Old_Main, Is_Remote_Call_Interface (New_Main));
5709 -- Make entry in Units table, so that binder can generate call to
5710 -- elaboration procedure for body, if any.
5712 Make_Instance_Unit (Body_Cunit, In_Main => True);
5713 Main_Unit_Entity := New_Main;
5714 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5716 -- Build elaboration entity, since the instance may certainly generate
5717 -- elaboration code requiring a flag for protection.
5719 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5720 end Build_Instance_Compilation_Unit_Nodes;
5722 -----------------------------
5723 -- Check_Access_Definition --
5724 -----------------------------
5726 procedure Check_Access_Definition (N : Node_Id) is
5729 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5731 end Check_Access_Definition;
5733 -----------------------------------
5734 -- Check_Formal_Package_Instance --
5735 -----------------------------------
5737 -- If the formal has specific parameters, they must match those of the
5738 -- actual. Both of them are instances, and the renaming declarations for
5739 -- their formal parameters appear in the same order in both. The analyzed
5740 -- formal has been analyzed in the context of the current instance.
5742 procedure Check_Formal_Package_Instance
5743 (Formal_Pack : Entity_Id;
5744 Actual_Pack : Entity_Id)
5746 E1 : Entity_Id := First_Entity (Actual_Pack);
5747 E2 : Entity_Id := First_Entity (Formal_Pack);
5752 procedure Check_Mismatch (B : Boolean);
5753 -- Common error routine for mismatch between the parameters of the
5754 -- actual instance and those of the formal package.
5756 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5757 -- The formal may come from a nested formal package, and the actual may
5758 -- have been constant-folded. To determine whether the two denote the
5759 -- same entity we may have to traverse several definitions to recover
5760 -- the ultimate entity that they refer to.
5762 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5763 -- Similarly, if the formal comes from a nested formal package, the
5764 -- actual may designate the formal through multiple renamings, which
5765 -- have to be followed to determine the original variable in question.
5767 --------------------
5768 -- Check_Mismatch --
5769 --------------------
5771 procedure Check_Mismatch (B : Boolean) is
5772 Kind : constant Node_Kind := Nkind (Parent (E2));
5775 if Kind = N_Formal_Type_Declaration then
5778 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5779 N_Formal_Package_Declaration)
5780 or else Kind in N_Formal_Subprogram_Declaration
5784 -- Ada 2012: If both formal and actual are incomplete types they
5787 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5792 ("actual for & in actual instance does not match formal",
5793 Parent (Actual_Pack), E1);
5797 --------------------------------
5798 -- Same_Instantiated_Constant --
5799 --------------------------------
5801 function Same_Instantiated_Constant
5802 (E1, E2 : Entity_Id) return Boolean
5808 while Present (Ent) loop
5812 elsif Ekind (Ent) /= E_Constant then
5815 elsif Is_Entity_Name (Constant_Value (Ent)) then
5816 if Entity (Constant_Value (Ent)) = E1 then
5819 Ent := Entity (Constant_Value (Ent));
5822 -- The actual may be a constant that has been folded. Recover
5825 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5826 Ent := Entity (Original_Node (Constant_Value (Ent)));
5834 end Same_Instantiated_Constant;
5836 --------------------------------
5837 -- Same_Instantiated_Variable --
5838 --------------------------------
5840 function Same_Instantiated_Variable
5841 (E1, E2 : Entity_Id) return Boolean
5843 function Original_Entity (E : Entity_Id) return Entity_Id;
5844 -- Follow chain of renamings to the ultimate ancestor
5846 ---------------------
5847 -- Original_Entity --
5848 ---------------------
5850 function Original_Entity (E : Entity_Id) return Entity_Id is
5855 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5856 and then Present (Renamed_Object (Orig))
5857 and then Is_Entity_Name (Renamed_Object (Orig))
5859 Orig := Entity (Renamed_Object (Orig));
5863 end Original_Entity;
5865 -- Start of processing for Same_Instantiated_Variable
5868 return Ekind (E1) = Ekind (E2)
5869 and then Original_Entity (E1) = Original_Entity (E2);
5870 end Same_Instantiated_Variable;
5872 -- Start of processing for Check_Formal_Package_Instance
5875 while Present (E1) and then Present (E2) loop
5876 exit when Ekind (E1) = E_Package
5877 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5879 -- If the formal is the renaming of the formal package, this
5880 -- is the end of its formal part, which may occur before the
5881 -- end of the formal part in the actual in the presence of
5882 -- defaulted parameters in the formal package.
5884 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5885 and then Renamed_Entity (E2) = Scope (E2);
5887 -- The analysis of the actual may generate additional internal
5888 -- entities. If the formal is defaulted, there is no corresponding
5889 -- analysis and the internal entities must be skipped, until we
5890 -- find corresponding entities again.
5892 if Comes_From_Source (E2)
5893 and then not Comes_From_Source (E1)
5894 and then Chars (E1) /= Chars (E2)
5896 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5904 -- If the formal entity comes from a formal declaration, it was
5905 -- defaulted in the formal package, and no check is needed on it.
5907 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5910 -- Ditto for defaulted formal subprograms.
5912 elsif Is_Overloadable (E1)
5913 and then Nkind (Unit_Declaration_Node (E2)) in
5914 N_Formal_Subprogram_Declaration
5918 elsif Is_Type (E1) then
5920 -- Subtypes must statically match. E1, E2 are the local entities
5921 -- that are subtypes of the actuals. Itypes generated for other
5922 -- parameters need not be checked, the check will be performed
5923 -- on the parameters themselves.
5925 -- If E2 is a formal type declaration, it is a defaulted parameter
5926 -- and needs no checking.
5928 if not Is_Itype (E1) and then not Is_Itype (E2) then
5931 or else Etype (E1) /= Etype (E2)
5932 or else not Subtypes_Statically_Match (E1, E2));
5935 elsif Ekind (E1) = E_Constant then
5937 -- IN parameters must denote the same static value, or the same
5938 -- constant, or the literal null.
5940 Expr1 := Expression (Parent (E1));
5942 if Ekind (E2) /= E_Constant then
5943 Check_Mismatch (True);
5946 Expr2 := Expression (Parent (E2));
5949 if Is_OK_Static_Expression (Expr1) then
5950 if not Is_OK_Static_Expression (Expr2) then
5951 Check_Mismatch (True);
5953 elsif Is_Discrete_Type (Etype (E1)) then
5955 V1 : constant Uint := Expr_Value (Expr1);
5956 V2 : constant Uint := Expr_Value (Expr2);
5958 Check_Mismatch (V1 /= V2);
5961 elsif Is_Real_Type (Etype (E1)) then
5963 V1 : constant Ureal := Expr_Value_R (Expr1);
5964 V2 : constant Ureal := Expr_Value_R (Expr2);
5966 Check_Mismatch (V1 /= V2);
5969 elsif Is_String_Type (Etype (E1))
5970 and then Nkind (Expr1) = N_String_Literal
5972 if Nkind (Expr2) /= N_String_Literal then
5973 Check_Mismatch (True);
5976 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5980 elsif Is_Entity_Name (Expr1) then
5981 if Is_Entity_Name (Expr2) then
5982 if Entity (Expr1) = Entity (Expr2) then
5986 (not Same_Instantiated_Constant
5987 (Entity (Expr1), Entity (Expr2)));
5991 Check_Mismatch (True);
5994 elsif Is_Entity_Name (Original_Node (Expr1))
5995 and then Is_Entity_Name (Expr2)
5996 and then Same_Instantiated_Constant
5997 (Entity (Original_Node (Expr1)), Entity (Expr2))
6001 elsif Nkind (Expr1) = N_Null then
6002 Check_Mismatch (Nkind (Expr1) /= N_Null);
6005 Check_Mismatch (True);
6008 elsif Ekind (E1) = E_Variable then
6009 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6011 elsif Ekind (E1) = E_Package then
6013 (Ekind (E1) /= Ekind (E2)
6014 or else Renamed_Object (E1) /= Renamed_Object (E2));
6016 elsif Is_Overloadable (E1) then
6018 -- Verify that the actual subprograms match. Note that actuals
6019 -- that are attributes are rewritten as subprograms. If the
6020 -- subprogram in the formal package is defaulted, no check is
6021 -- needed. Note that this can only happen in Ada 2005 when the
6022 -- formal package can be partially parameterized.
6024 if Nkind (Unit_Declaration_Node (E1)) =
6025 N_Subprogram_Renaming_Declaration
6026 and then From_Default (Unit_Declaration_Node (E1))
6030 -- If the formal package has an "others" box association that
6031 -- covers this formal, there is no need for a check either.
6033 elsif Nkind (Unit_Declaration_Node (E2)) in
6034 N_Formal_Subprogram_Declaration
6035 and then Box_Present (Unit_Declaration_Node (E2))
6039 -- No check needed if subprogram is a defaulted null procedure
6041 elsif No (Alias (E2))
6042 and then Ekind (E2) = E_Procedure
6044 Null_Present (Specification (Unit_Declaration_Node (E2)))
6048 -- Otherwise the actual in the formal and the actual in the
6049 -- instantiation of the formal must match, up to renamings.
6053 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6057 raise Program_Error;
6064 end Check_Formal_Package_Instance;
6066 ---------------------------
6067 -- Check_Formal_Packages --
6068 ---------------------------
6070 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6072 Formal_P : Entity_Id;
6075 -- Iterate through the declarations in the instance, looking for package
6076 -- renaming declarations that denote instances of formal packages. Stop
6077 -- when we find the renaming of the current package itself. The
6078 -- declaration for a formal package without a box is followed by an
6079 -- internal entity that repeats the instantiation.
6081 E := First_Entity (P_Id);
6082 while Present (E) loop
6083 if Ekind (E) = E_Package then
6084 if Renamed_Object (E) = P_Id then
6087 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6090 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6091 Formal_P := Next_Entity (E);
6092 Check_Formal_Package_Instance (Formal_P, E);
6094 -- After checking, remove the internal validating package. It
6095 -- is only needed for semantic checks, and as it may contain
6096 -- generic formal declarations it should not reach gigi.
6098 Remove (Unit_Declaration_Node (Formal_P));
6104 end Check_Formal_Packages;
6106 ---------------------------------
6107 -- Check_Forward_Instantiation --
6108 ---------------------------------
6110 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6112 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6115 -- The instantiation appears before the generic body if we are in the
6116 -- scope of the unit containing the generic, either in its spec or in
6117 -- the package body, and before the generic body.
6119 if Ekind (Gen_Comp) = E_Package_Body then
6120 Gen_Comp := Spec_Entity (Gen_Comp);
6123 if In_Open_Scopes (Gen_Comp)
6124 and then No (Corresponding_Body (Decl))
6129 and then not Is_Compilation_Unit (S)
6130 and then not Is_Child_Unit (S)
6132 if Ekind (S) = E_Package then
6133 Set_Has_Forward_Instantiation (S);
6139 end Check_Forward_Instantiation;
6141 ---------------------------
6142 -- Check_Generic_Actuals --
6143 ---------------------------
6145 -- The visibility of the actuals may be different between the point of
6146 -- generic instantiation and the instantiation of the body.
6148 procedure Check_Generic_Actuals
6149 (Instance : Entity_Id;
6150 Is_Formal_Box : Boolean)
6155 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6156 -- For a formal that is an array type, the component type is often a
6157 -- previous formal in the same unit. The privacy status of the component
6158 -- type will have been examined earlier in the traversal of the
6159 -- corresponding actuals, and this status should not be modified for
6160 -- the array (sub)type itself. However, if the base type of the array
6161 -- (sub)type is private, its full view must be restored in the body to
6162 -- be consistent with subsequent index subtypes, etc.
6164 -- To detect this case we have to rescan the list of formals, which is
6165 -- usually short enough to ignore the resulting inefficiency.
6167 -----------------------------
6168 -- Denotes_Previous_Actual --
6169 -----------------------------
6171 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6175 Prev := First_Entity (Instance);
6176 while Present (Prev) loop
6178 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6179 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6180 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6193 end Denotes_Previous_Actual;
6195 -- Start of processing for Check_Generic_Actuals
6198 E := First_Entity (Instance);
6199 while Present (E) loop
6201 and then Nkind (Parent (E)) = N_Subtype_Declaration
6202 and then Scope (Etype (E)) /= Instance
6203 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6205 if Is_Array_Type (E)
6206 and then not Is_Private_Type (Etype (E))
6207 and then Denotes_Previous_Actual (Component_Type (E))
6211 Check_Private_View (Subtype_Indication (Parent (E)));
6214 Set_Is_Generic_Actual_Type (E, True);
6215 Set_Is_Hidden (E, False);
6216 Set_Is_Potentially_Use_Visible (E,
6219 -- We constructed the generic actual type as a subtype of the
6220 -- supplied type. This means that it normally would not inherit
6221 -- subtype specific attributes of the actual, which is wrong for
6222 -- the generic case.
6224 Astype := Ancestor_Subtype (E);
6228 -- This can happen when E is an itype that is the full view of
6229 -- a private type completed, e.g. with a constrained array. In
6230 -- that case, use the first subtype, which will carry size
6231 -- information. The base type itself is unconstrained and will
6234 Astype := First_Subtype (E);
6237 Set_Size_Info (E, (Astype));
6238 Set_RM_Size (E, RM_Size (Astype));
6239 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6241 if Is_Discrete_Or_Fixed_Point_Type (E) then
6242 Set_RM_Size (E, RM_Size (Astype));
6244 -- In nested instances, the base type of an access actual may
6245 -- itself be private, and need to be exchanged.
6247 elsif Is_Access_Type (E)
6248 and then Is_Private_Type (Etype (E))
6251 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6254 elsif Ekind (E) = E_Package then
6256 -- If this is the renaming for the current instance, we're done.
6257 -- Otherwise it is a formal package. If the corresponding formal
6258 -- was declared with a box, the (instantiations of the) generic
6259 -- formal part are also visible. Otherwise, ignore the entity
6260 -- created to validate the actuals.
6262 if Renamed_Object (E) = Instance then
6265 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6268 -- The visibility of a formal of an enclosing generic is already
6271 elsif Denotes_Formal_Package (E) then
6274 elsif Present (Associated_Formal_Package (E))
6275 and then not Is_Generic_Formal (E)
6277 if Box_Present (Parent (Associated_Formal_Package (E))) then
6278 Check_Generic_Actuals (Renamed_Object (E), True);
6281 Check_Generic_Actuals (Renamed_Object (E), False);
6284 Set_Is_Hidden (E, False);
6287 -- If this is a subprogram instance (in a wrapper package) the
6288 -- actual is fully visible.
6290 elsif Is_Wrapper_Package (Instance) then
6291 Set_Is_Hidden (E, False);
6293 -- If the formal package is declared with a box, or if the formal
6294 -- parameter is defaulted, it is visible in the body.
6296 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6297 Set_Is_Hidden (E, False);
6300 if Ekind (E) = E_Constant then
6302 -- If the type of the actual is a private type declared in the
6303 -- enclosing scope of the generic unit, the body of the generic
6304 -- sees the full view of the type (because it has to appear in
6305 -- the corresponding package body). If the type is private now,
6306 -- exchange views to restore the proper visiblity in the instance.
6309 Typ : constant Entity_Id := Base_Type (Etype (E));
6310 -- The type of the actual
6315 Parent_Scope : Entity_Id;
6316 -- The enclosing scope of the generic unit
6319 if Is_Wrapper_Package (Instance) then
6323 (Unit_Declaration_Node
6324 (Related_Instance (Instance))));
6327 Generic_Parent (Package_Specification (Instance));
6330 Parent_Scope := Scope (Gen_Id);
6332 -- The exchange is only needed if the generic is defined
6333 -- within a package which is not a common ancestor of the
6334 -- scope of the instance, and is not already in scope.
6336 if Is_Private_Type (Typ)
6337 and then Scope (Typ) = Parent_Scope
6338 and then Scope (Instance) /= Parent_Scope
6339 and then Ekind (Parent_Scope) = E_Package
6340 and then not Is_Child_Unit (Gen_Id)
6344 -- If the type of the entity is a subtype, it may also have
6345 -- to be made visible, together with the base type of its
6346 -- full view, after exchange.
6348 if Is_Private_Type (Etype (E)) then
6349 Switch_View (Etype (E));
6350 Switch_View (Base_Type (Etype (E)));
6358 end Check_Generic_Actuals;
6360 ------------------------------
6361 -- Check_Generic_Child_Unit --
6362 ------------------------------
6364 procedure Check_Generic_Child_Unit
6366 Parent_Installed : in out Boolean)
6368 Loc : constant Source_Ptr := Sloc (Gen_Id);
6369 Gen_Par : Entity_Id := Empty;
6371 Inst_Par : Entity_Id;
6374 function Find_Generic_Child
6376 Id : Node_Id) return Entity_Id;
6377 -- Search generic parent for possible child unit with the given name
6379 function In_Enclosing_Instance return Boolean;
6380 -- Within an instance of the parent, the child unit may be denoted by
6381 -- a simple name, or an abbreviated expanded name. Examine enclosing
6382 -- scopes to locate a possible parent instantiation.
6384 ------------------------
6385 -- Find_Generic_Child --
6386 ------------------------
6388 function Find_Generic_Child
6390 Id : Node_Id) return Entity_Id
6395 -- If entity of name is already set, instance has already been
6396 -- resolved, e.g. in an enclosing instantiation.
6398 if Present (Entity (Id)) then
6399 if Scope (Entity (Id)) = Scop then
6406 E := First_Entity (Scop);
6407 while Present (E) loop
6408 if Chars (E) = Chars (Id)
6409 and then Is_Child_Unit (E)
6411 if Is_Child_Unit (E)
6412 and then not Is_Visible_Lib_Unit (E)
6415 ("generic child unit& is not visible", Gen_Id, E);
6427 end Find_Generic_Child;
6429 ---------------------------
6430 -- In_Enclosing_Instance --
6431 ---------------------------
6433 function In_Enclosing_Instance return Boolean is
6434 Enclosing_Instance : Node_Id;
6435 Instance_Decl : Node_Id;
6438 -- We do not inline any call that contains instantiations, except
6439 -- for instantiations of Unchecked_Conversion, so if we are within
6440 -- an inlined body the current instance does not require parents.
6442 if In_Inlined_Body then
6443 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6447 -- Loop to check enclosing scopes
6449 Enclosing_Instance := Current_Scope;
6450 while Present (Enclosing_Instance) loop
6451 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6453 if Ekind (Enclosing_Instance) = E_Package
6454 and then Is_Generic_Instance (Enclosing_Instance)
6456 (Generic_Parent (Specification (Instance_Decl)))
6458 -- Check whether the generic we are looking for is a child of
6461 E := Find_Generic_Child
6462 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6463 exit when Present (E);
6469 Enclosing_Instance := Scope (Enclosing_Instance);
6481 Make_Expanded_Name (Loc,
6483 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6484 Selector_Name => New_Occurrence_Of (E, Loc)));
6486 Set_Entity (Gen_Id, E);
6487 Set_Etype (Gen_Id, Etype (E));
6488 Parent_Installed := False; -- Already in scope.
6491 end In_Enclosing_Instance;
6493 -- Start of processing for Check_Generic_Child_Unit
6496 -- If the name of the generic is given by a selected component, it may
6497 -- be the name of a generic child unit, and the prefix is the name of an
6498 -- instance of the parent, in which case the child unit must be visible.
6499 -- If this instance is not in scope, it must be placed there and removed
6500 -- after instantiation, because what is being instantiated is not the
6501 -- original child, but the corresponding child present in the instance
6504 -- If the child is instantiated within the parent, it can be given by
6505 -- a simple name. In this case the instance is already in scope, but
6506 -- the child generic must be recovered from the generic parent as well.
6508 if Nkind (Gen_Id) = N_Selected_Component then
6509 S := Selector_Name (Gen_Id);
6510 Analyze (Prefix (Gen_Id));
6511 Inst_Par := Entity (Prefix (Gen_Id));
6513 if Ekind (Inst_Par) = E_Package
6514 and then Present (Renamed_Object (Inst_Par))
6516 Inst_Par := Renamed_Object (Inst_Par);
6519 if Ekind (Inst_Par) = E_Package then
6520 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6521 Gen_Par := Generic_Parent (Parent (Inst_Par));
6523 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6525 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6527 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6530 elsif Ekind (Inst_Par) = E_Generic_Package
6531 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6533 -- A formal package may be a real child package, and not the
6534 -- implicit instance within a parent. In this case the child is
6535 -- not visible and has to be retrieved explicitly as well.
6537 Gen_Par := Inst_Par;
6540 if Present (Gen_Par) then
6542 -- The prefix denotes an instantiation. The entity itself may be a
6543 -- nested generic, or a child unit.
6545 E := Find_Generic_Child (Gen_Par, S);
6548 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6549 Set_Entity (Gen_Id, E);
6550 Set_Etype (Gen_Id, Etype (E));
6552 Set_Etype (S, Etype (E));
6554 -- Indicate that this is a reference to the parent
6556 if In_Extended_Main_Source_Unit (Gen_Id) then
6557 Set_Is_Instantiated (Inst_Par);
6560 -- A common mistake is to replicate the naming scheme of a
6561 -- hierarchy by instantiating a generic child directly, rather
6562 -- than the implicit child in a parent instance:
6564 -- generic .. package Gpar is ..
6565 -- generic .. package Gpar.Child is ..
6566 -- package Par is new Gpar ();
6569 -- package Par.Child is new Gpar.Child ();
6570 -- rather than Par.Child
6572 -- In this case the instantiation is within Par, which is an
6573 -- instance, but Gpar does not denote Par because we are not IN
6574 -- the instance of Gpar, so this is illegal. The test below
6575 -- recognizes this particular case.
6577 if Is_Child_Unit (E)
6578 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6579 and then (not In_Instance
6580 or else Nkind (Parent (Parent (Gen_Id))) =
6584 ("prefix of generic child unit must be instance of parent",
6588 if not In_Open_Scopes (Inst_Par)
6589 and then Nkind (Parent (Gen_Id)) not in
6590 N_Generic_Renaming_Declaration
6592 Install_Parent (Inst_Par);
6593 Parent_Installed := True;
6595 elsif In_Open_Scopes (Inst_Par) then
6597 -- If the parent is already installed, install the actuals
6598 -- for its formal packages. This is necessary when the child
6599 -- instance is a child of the parent instance: in this case,
6600 -- the parent is placed on the scope stack but the formal
6601 -- packages are not made visible.
6603 Install_Formal_Packages (Inst_Par);
6607 -- If the generic parent does not contain an entity that
6608 -- corresponds to the selector, the instance doesn't either.
6609 -- Analyzing the node will yield the appropriate error message.
6610 -- If the entity is not a child unit, then it is an inner
6611 -- generic in the parent.
6619 if Is_Child_Unit (Entity (Gen_Id))
6621 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6622 and then not In_Open_Scopes (Inst_Par)
6624 Install_Parent (Inst_Par);
6625 Parent_Installed := True;
6627 -- The generic unit may be the renaming of the implicit child
6628 -- present in an instance. In that case the parent instance is
6629 -- obtained from the name of the renamed entity.
6631 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6632 and then Present (Renamed_Entity (Entity (Gen_Id)))
6633 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6636 Renamed_Package : constant Node_Id :=
6637 Name (Parent (Entity (Gen_Id)));
6639 if Nkind (Renamed_Package) = N_Expanded_Name then
6640 Inst_Par := Entity (Prefix (Renamed_Package));
6641 Install_Parent (Inst_Par);
6642 Parent_Installed := True;
6648 elsif Nkind (Gen_Id) = N_Expanded_Name then
6650 -- Entity already present, analyze prefix, whose meaning may be
6651 -- an instance in the current context. If it is an instance of
6652 -- a relative within another, the proper parent may still have
6653 -- to be installed, if they are not of the same generation.
6655 Analyze (Prefix (Gen_Id));
6657 -- In the unlikely case that a local declaration hides the name
6658 -- of the parent package, locate it on the homonym chain. If the
6659 -- context is an instance of the parent, the renaming entity is
6662 Inst_Par := Entity (Prefix (Gen_Id));
6663 while Present (Inst_Par)
6664 and then not Is_Package_Or_Generic_Package (Inst_Par)
6666 Inst_Par := Homonym (Inst_Par);
6669 pragma Assert (Present (Inst_Par));
6670 Set_Entity (Prefix (Gen_Id), Inst_Par);
6672 if In_Enclosing_Instance then
6675 elsif Present (Entity (Gen_Id))
6676 and then Is_Child_Unit (Entity (Gen_Id))
6677 and then not In_Open_Scopes (Inst_Par)
6679 Install_Parent (Inst_Par);
6680 Parent_Installed := True;
6683 elsif In_Enclosing_Instance then
6685 -- The child unit is found in some enclosing scope
6692 -- If this is the renaming of the implicit child in a parent
6693 -- instance, recover the parent name and install it.
6695 if Is_Entity_Name (Gen_Id) then
6696 E := Entity (Gen_Id);
6698 if Is_Generic_Unit (E)
6699 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6700 and then Is_Child_Unit (Renamed_Object (E))
6701 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6702 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6704 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6705 Inst_Par := Entity (Prefix (Gen_Id));
6707 if not In_Open_Scopes (Inst_Par) then
6708 Install_Parent (Inst_Par);
6709 Parent_Installed := True;
6712 -- If it is a child unit of a non-generic parent, it may be
6713 -- use-visible and given by a direct name. Install parent as
6716 elsif Is_Generic_Unit (E)
6717 and then Is_Child_Unit (E)
6719 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6720 and then not Is_Generic_Unit (Scope (E))
6722 if not In_Open_Scopes (Scope (E)) then
6723 Install_Parent (Scope (E));
6724 Parent_Installed := True;
6729 end Check_Generic_Child_Unit;
6731 -----------------------------
6732 -- Check_Hidden_Child_Unit --
6733 -----------------------------
6735 procedure Check_Hidden_Child_Unit
6737 Gen_Unit : Entity_Id;
6738 Act_Decl_Id : Entity_Id)
6740 Gen_Id : constant Node_Id := Name (N);
6743 if Is_Child_Unit (Gen_Unit)
6744 and then Is_Child_Unit (Act_Decl_Id)
6745 and then Nkind (Gen_Id) = N_Expanded_Name
6746 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6747 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6749 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6751 ("generic unit & is implicitly declared in &",
6752 Defining_Unit_Name (N), Gen_Unit);
6753 Error_Msg_N ("\instance must have different name",
6754 Defining_Unit_Name (N));
6756 end Check_Hidden_Child_Unit;
6758 ------------------------
6759 -- Check_Private_View --
6760 ------------------------
6762 procedure Check_Private_View (N : Node_Id) is
6763 T : constant Entity_Id := Etype (N);
6767 -- Exchange views if the type was not private in the generic but is
6768 -- private at the point of instantiation. Do not exchange views if
6769 -- the scope of the type is in scope. This can happen if both generic
6770 -- and instance are sibling units, or if type is defined in a parent.
6771 -- In this case the visibility of the type will be correct for all
6775 BT := Base_Type (T);
6777 if Is_Private_Type (T)
6778 and then not Has_Private_View (N)
6779 and then Present (Full_View (T))
6780 and then not In_Open_Scopes (Scope (T))
6782 -- In the generic, the full type was visible. Save the private
6783 -- entity, for subsequent exchange.
6787 elsif Has_Private_View (N)
6788 and then not Is_Private_Type (T)
6789 and then not Has_Been_Exchanged (T)
6790 and then Etype (Get_Associated_Node (N)) /= T
6792 -- Only the private declaration was visible in the generic. If
6793 -- the type appears in a subtype declaration, the subtype in the
6794 -- instance must have a view compatible with that of its parent,
6795 -- which must be exchanged (see corresponding code in Restore_
6796 -- Private_Views). Otherwise, if the type is defined in a parent
6797 -- unit, leave full visibility within instance, which is safe.
6799 if In_Open_Scopes (Scope (Base_Type (T)))
6800 and then not Is_Private_Type (Base_Type (T))
6801 and then Comes_From_Source (Base_Type (T))
6805 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6806 or else not In_Private_Part (Scope (Base_Type (T)))
6808 Prepend_Elmt (T, Exchanged_Views);
6809 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6812 -- For composite types with inconsistent representation exchange
6813 -- component types accordingly.
6815 elsif Is_Access_Type (T)
6816 and then Is_Private_Type (Designated_Type (T))
6817 and then not Has_Private_View (N)
6818 and then Present (Full_View (Designated_Type (T)))
6820 Switch_View (Designated_Type (T));
6822 elsif Is_Array_Type (T) then
6823 if Is_Private_Type (Component_Type (T))
6824 and then not Has_Private_View (N)
6825 and then Present (Full_View (Component_Type (T)))
6827 Switch_View (Component_Type (T));
6830 -- The normal exchange mechanism relies on the setting of a
6831 -- flag on the reference in the generic. However, an additional
6832 -- mechanism is needed for types that are not explicitly
6833 -- mentioned in the generic, but may be needed in expanded code
6834 -- in the instance. This includes component types of arrays and
6835 -- designated types of access types. This processing must also
6836 -- include the index types of arrays which we take care of here.
6843 Indx := First_Index (T);
6844 while Present (Indx) loop
6845 Typ := Base_Type (Etype (Indx));
6847 if Is_Private_Type (Typ)
6848 and then Present (Full_View (Typ))
6857 elsif Is_Private_Type (T)
6858 and then Present (Full_View (T))
6859 and then Is_Array_Type (Full_View (T))
6860 and then Is_Private_Type (Component_Type (Full_View (T)))
6864 -- Finally, a non-private subtype may have a private base type, which
6865 -- must be exchanged for consistency. This can happen when a package
6866 -- body is instantiated, when the scope stack is empty but in fact
6867 -- the subtype and the base type are declared in an enclosing scope.
6869 -- Note that in this case we introduce an inconsistency in the view
6870 -- set, because we switch the base type BT, but there could be some
6871 -- private dependent subtypes of BT which remain unswitched. Such
6872 -- subtypes might need to be switched at a later point (see specific
6873 -- provision for that case in Switch_View).
6875 elsif not Is_Private_Type (T)
6876 and then not Has_Private_View (N)
6877 and then Is_Private_Type (BT)
6878 and then Present (Full_View (BT))
6879 and then not Is_Generic_Type (BT)
6880 and then not In_Open_Scopes (BT)
6882 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6883 Exchange_Declarations (BT);
6886 end Check_Private_View;
6888 -----------------------------
6889 -- Check_Hidden_Primitives --
6890 -----------------------------
6892 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6895 Result : Elist_Id := No_Elist;
6898 if No (Assoc_List) then
6902 -- Traverse the list of associations between formals and actuals
6903 -- searching for renamings of tagged types
6905 Actual := First (Assoc_List);
6906 while Present (Actual) loop
6907 if Nkind (Actual) = N_Subtype_Declaration then
6908 Gen_T := Generic_Parent_Type (Actual);
6910 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6912 -- Traverse the list of primitives of the actual types
6913 -- searching for hidden primitives that are visible in the
6914 -- corresponding generic formal; leave them visible and
6915 -- append them to Result to restore their decoration later.
6917 Install_Hidden_Primitives
6918 (Prims_List => Result,
6920 Act_T => Entity (Subtype_Indication (Actual)));
6928 end Check_Hidden_Primitives;
6930 --------------------------
6931 -- Contains_Instance_Of --
6932 --------------------------
6934 function Contains_Instance_Of
6937 N : Node_Id) return Boolean
6945 -- Verify that there are no circular instantiations. We check whether
6946 -- the unit contains an instance of the current scope or some enclosing
6947 -- scope (in case one of the instances appears in a subunit). Longer
6948 -- circularities involving subunits might seem too pathological to
6949 -- consider, but they were not too pathological for the authors of
6950 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6951 -- enclosing generic scopes as containing an instance.
6954 -- Within a generic subprogram body, the scope is not generic, to
6955 -- allow for recursive subprograms. Use the declaration to determine
6956 -- whether this is a generic unit.
6958 if Ekind (Scop) = E_Generic_Package
6959 or else (Is_Subprogram (Scop)
6960 and then Nkind (Unit_Declaration_Node (Scop)) =
6961 N_Generic_Subprogram_Declaration)
6963 Elmt := First_Elmt (Inner_Instances (Inner));
6965 while Present (Elmt) loop
6966 if Node (Elmt) = Scop then
6967 Error_Msg_Node_2 := Inner;
6969 ("circular Instantiation: & instantiated within &!",
6973 elsif Node (Elmt) = Inner then
6976 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6977 Error_Msg_Node_2 := Inner;
6979 ("circular Instantiation: & instantiated within &!",
6987 -- Indicate that Inner is being instantiated within Scop
6989 Append_Elmt (Inner, Inner_Instances (Scop));
6992 if Scop = Standard_Standard then
6995 Scop := Scope (Scop);
7000 end Contains_Instance_Of;
7002 -----------------------
7003 -- Copy_Generic_Node --
7004 -----------------------
7006 function Copy_Generic_Node
7008 Parent_Id : Node_Id;
7009 Instantiating : Boolean) return Node_Id
7014 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7015 -- Check the given value of one of the Fields referenced by the current
7016 -- node to determine whether to copy it recursively. The field may hold
7017 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7018 -- Char) in which case it need not be copied.
7020 procedure Copy_Descendants;
7021 -- Common utility for various nodes
7023 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7024 -- Make copy of element list
7026 function Copy_Generic_List
7028 Parent_Id : Node_Id) return List_Id;
7029 -- Apply Copy_Node recursively to the members of a node list
7031 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7032 -- True if an identifier is part of the defining program unit name of
7033 -- a child unit. The entity of such an identifier must be kept (for
7034 -- ASIS use) even though as the name of an enclosing generic it would
7035 -- otherwise not be preserved in the generic tree.
7037 ----------------------
7038 -- Copy_Descendants --
7039 ----------------------
7041 procedure Copy_Descendants is
7042 use Atree.Unchecked_Access;
7043 -- This code section is part of the implementation of an untyped
7044 -- tree traversal, so it needs direct access to node fields.
7047 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7048 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7049 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7050 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7051 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7052 end Copy_Descendants;
7054 -----------------------------
7055 -- Copy_Generic_Descendant --
7056 -----------------------------
7058 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7060 if D = Union_Id (Empty) then
7063 elsif D in Node_Range then
7065 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7067 elsif D in List_Range then
7068 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7070 elsif D in Elist_Range then
7071 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7073 -- Nothing else is copyable (e.g. Uint values), return as is
7078 end Copy_Generic_Descendant;
7080 ------------------------
7081 -- Copy_Generic_Elist --
7082 ------------------------
7084 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7091 M := First_Elmt (E);
7092 while Present (M) loop
7094 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7103 end Copy_Generic_Elist;
7105 -----------------------
7106 -- Copy_Generic_List --
7107 -----------------------
7109 function Copy_Generic_List
7111 Parent_Id : Node_Id) return List_Id
7119 Set_Parent (New_L, Parent_Id);
7122 while Present (N) loop
7123 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7132 end Copy_Generic_List;
7134 ---------------------------
7135 -- In_Defining_Unit_Name --
7136 ---------------------------
7138 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7141 Present (Parent (Nam))
7142 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7144 (Nkind (Parent (Nam)) = N_Expanded_Name
7145 and then In_Defining_Unit_Name (Parent (Nam))));
7146 end In_Defining_Unit_Name;
7148 -- Start of processing for Copy_Generic_Node
7155 New_N := New_Copy (N);
7157 -- Copy aspects if present
7159 if Has_Aspects (N) then
7160 Set_Has_Aspects (New_N, False);
7161 Set_Aspect_Specifications
7162 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7165 if Instantiating then
7166 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7169 if not Is_List_Member (N) then
7170 Set_Parent (New_N, Parent_Id);
7173 -- Special casing for identifiers and other entity names and operators
7175 if Nkind_In (New_N, N_Character_Literal,
7179 or else Nkind (New_N) in N_Op
7181 if not Instantiating then
7183 -- Link both nodes in order to assign subsequently the entity of
7184 -- the copy to the original node, in case this is a global
7187 Set_Associated_Node (N, New_N);
7189 -- If we are within an instantiation, this is a nested generic
7190 -- that has already been analyzed at the point of definition.
7191 -- We must preserve references that were global to the enclosing
7192 -- parent at that point. Other occurrences, whether global or
7193 -- local to the current generic, must be resolved anew, so we
7194 -- reset the entity in the generic copy. A global reference has a
7195 -- smaller depth than the parent, or else the same depth in case
7196 -- both are distinct compilation units.
7198 -- A child unit is implicitly declared within the enclosing parent
7199 -- but is in fact global to it, and must be preserved.
7201 -- It is also possible for Current_Instantiated_Parent to be
7202 -- defined, and for this not to be a nested generic, namely if
7203 -- the unit is loaded through Rtsfind. In that case, the entity of
7204 -- New_N is only a link to the associated node, and not a defining
7207 -- The entities for parent units in the defining_program_unit of a
7208 -- generic child unit are established when the context of the unit
7209 -- is first analyzed, before the generic copy is made. They are
7210 -- preserved in the copy for use in ASIS queries.
7212 Ent := Entity (New_N);
7214 if No (Current_Instantiated_Parent.Gen_Id) then
7216 or else Nkind (Ent) /= N_Defining_Identifier
7217 or else not In_Defining_Unit_Name (N)
7219 Set_Associated_Node (New_N, Empty);
7224 not Nkind_In (Ent, N_Defining_Identifier,
7225 N_Defining_Character_Literal,
7226 N_Defining_Operator_Symbol)
7227 or else No (Scope (Ent))
7229 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7230 and then not Is_Child_Unit (Ent))
7232 (Scope_Depth (Scope (Ent)) >
7233 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7235 Get_Source_Unit (Ent) =
7236 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7238 Set_Associated_Node (New_N, Empty);
7241 -- Case of instantiating identifier or some other name or operator
7244 -- If the associated node is still defined, the entity in it
7245 -- is global, and must be copied to the instance. If this copy
7246 -- is being made for a body to inline, it is applied to an
7247 -- instantiated tree, and the entity is already present and
7248 -- must be also preserved.
7251 Assoc : constant Node_Id := Get_Associated_Node (N);
7254 if Present (Assoc) then
7255 if Nkind (Assoc) = Nkind (N) then
7256 Set_Entity (New_N, Entity (Assoc));
7257 Check_Private_View (N);
7259 -- The name in the call may be a selected component if the
7260 -- call has not been analyzed yet, as may be the case for
7261 -- pre/post conditions in a generic unit.
7263 elsif Nkind (Assoc) = N_Function_Call
7264 and then Is_Entity_Name (Name (Assoc))
7266 Set_Entity (New_N, Entity (Name (Assoc)));
7268 elsif Nkind_In (Assoc, N_Defining_Identifier,
7269 N_Defining_Character_Literal,
7270 N_Defining_Operator_Symbol)
7271 and then Expander_Active
7273 -- Inlining case: we are copying a tree that contains
7274 -- global entities, which are preserved in the copy to be
7275 -- used for subsequent inlining.
7280 Set_Entity (New_N, Empty);
7286 -- For expanded name, we must copy the Prefix and Selector_Name
7288 if Nkind (N) = N_Expanded_Name then
7290 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7292 Set_Selector_Name (New_N,
7293 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7295 -- For operators, we must copy the right operand
7297 elsif Nkind (N) in N_Op then
7298 Set_Right_Opnd (New_N,
7299 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7301 -- And for binary operators, the left operand as well
7303 if Nkind (N) in N_Binary_Op then
7304 Set_Left_Opnd (New_N,
7305 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7309 -- Establish a link between an entity from the generic template and the
7310 -- corresponding entity in the generic copy to be analyzed.
7312 elsif Nkind (N) in N_Entity then
7313 if not Instantiating then
7314 Set_Associated_Entity (N, New_N);
7317 -- Clear any existing link the copy may inherit from the replicated
7318 -- generic template entity.
7320 Set_Associated_Entity (New_N, Empty);
7322 -- Special casing for stubs
7324 elsif Nkind (N) in N_Body_Stub then
7326 -- In any case, we must copy the specification or defining
7327 -- identifier as appropriate.
7329 if Nkind (N) = N_Subprogram_Body_Stub then
7330 Set_Specification (New_N,
7331 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7334 Set_Defining_Identifier (New_N,
7336 (Defining_Identifier (N), New_N, Instantiating));
7339 -- If we are not instantiating, then this is where we load and
7340 -- analyze subunits, i.e. at the point where the stub occurs. A
7341 -- more permissive system might defer this analysis to the point
7342 -- of instantiation, but this seems too complicated for now.
7344 if not Instantiating then
7346 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7348 Unum : Unit_Number_Type;
7352 -- Make sure that, if it is a subunit of the main unit that is
7353 -- preprocessed and if -gnateG is specified, the preprocessed
7354 -- file will be written.
7356 Lib.Analysing_Subunit_Of_Main :=
7357 Lib.In_Extended_Main_Source_Unit (N);
7360 (Load_Name => Subunit_Name,
7364 Lib.Analysing_Subunit_Of_Main := False;
7366 -- If the proper body is not found, a warning message will be
7367 -- emitted when analyzing the stub, or later at the point of
7368 -- instantiation. Here we just leave the stub as is.
7370 if Unum = No_Unit then
7371 Subunits_Missing := True;
7372 goto Subunit_Not_Found;
7375 Subunit := Cunit (Unum);
7377 if Nkind (Unit (Subunit)) /= N_Subunit then
7379 ("found child unit instead of expected SEPARATE subunit",
7381 Error_Msg_Sloc := Sloc (N);
7382 Error_Msg_N ("\to complete stub #", Subunit);
7383 goto Subunit_Not_Found;
7386 -- We must create a generic copy of the subunit, in order to
7387 -- perform semantic analysis on it, and we must replace the
7388 -- stub in the original generic unit with the subunit, in order
7389 -- to preserve non-local references within.
7391 -- Only the proper body needs to be copied. Library_Unit and
7392 -- context clause are simply inherited by the generic copy.
7393 -- Note that the copy (which may be recursive if there are
7394 -- nested subunits) must be done first, before attaching it to
7395 -- the enclosing generic.
7399 (Proper_Body (Unit (Subunit)),
7400 Empty, Instantiating => False);
7402 -- Now place the original proper body in the original generic
7403 -- unit. This is a body, not a compilation unit.
7405 Rewrite (N, Proper_Body (Unit (Subunit)));
7406 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7407 Set_Was_Originally_Stub (N);
7409 -- Finally replace the body of the subunit with its copy, and
7410 -- make this new subunit into the library unit of the generic
7411 -- copy, which does not have stubs any longer.
7413 Set_Proper_Body (Unit (Subunit), New_Body);
7414 Set_Library_Unit (New_N, Subunit);
7415 Inherit_Context (Unit (Subunit), N);
7418 -- If we are instantiating, this must be an error case, since
7419 -- otherwise we would have replaced the stub node by the proper body
7420 -- that corresponds. So just ignore it in the copy (i.e. we have
7421 -- copied it, and that is good enough).
7427 <<Subunit_Not_Found>> null;
7429 -- If the node is a compilation unit, it is the subunit of a stub, which
7430 -- has been loaded already (see code below). In this case, the library
7431 -- unit field of N points to the parent unit (which is a compilation
7432 -- unit) and need not (and cannot) be copied.
7434 -- When the proper body of the stub is analyzed, the library_unit link
7435 -- is used to establish the proper context (see sem_ch10).
7437 -- The other fields of a compilation unit are copied as usual
7439 elsif Nkind (N) = N_Compilation_Unit then
7441 -- This code can only be executed when not instantiating, because in
7442 -- the copy made for an instantiation, the compilation unit node has
7443 -- disappeared at the point that a stub is replaced by its proper
7446 pragma Assert (not Instantiating);
7448 Set_Context_Items (New_N,
7449 Copy_Generic_List (Context_Items (N), New_N));
7452 Copy_Generic_Node (Unit (N), New_N, False));
7454 Set_First_Inlined_Subprogram (New_N,
7456 (First_Inlined_Subprogram (N), New_N, False));
7458 Set_Aux_Decls_Node (New_N,
7459 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7461 -- For an assignment node, the assignment is known to be semantically
7462 -- legal if we are instantiating the template. This avoids incorrect
7463 -- diagnostics in generated code.
7465 elsif Nkind (N) = N_Assignment_Statement then
7467 -- Copy name and expression fields in usual manner
7470 Copy_Generic_Node (Name (N), New_N, Instantiating));
7472 Set_Expression (New_N,
7473 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7475 if Instantiating then
7476 Set_Assignment_OK (Name (New_N), True);
7479 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7480 if not Instantiating then
7481 Set_Associated_Node (N, New_N);
7484 if Present (Get_Associated_Node (N))
7485 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7487 -- In the generic the aggregate has some composite type. If at
7488 -- the point of instantiation the type has a private view,
7489 -- install the full view (and that of its ancestors, if any).
7492 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7496 if Present (T) and then Is_Private_Type (T) then
7501 and then Is_Tagged_Type (T)
7502 and then Is_Derived_Type (T)
7504 Rt := Root_Type (T);
7509 if Is_Private_Type (T) then
7520 -- Do not copy the associated node, which points to the generic copy
7521 -- of the aggregate.
7524 use Atree.Unchecked_Access;
7525 -- This code section is part of the implementation of an untyped
7526 -- tree traversal, so it needs direct access to node fields.
7529 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7530 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7531 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7532 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7535 -- Allocators do not have an identifier denoting the access type, so we
7536 -- must locate it through the expression to check whether the views are
7539 elsif Nkind (N) = N_Allocator
7540 and then Nkind (Expression (N)) = N_Qualified_Expression
7541 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7542 and then Instantiating
7545 T : constant Node_Id :=
7546 Get_Associated_Node (Subtype_Mark (Expression (N)));
7552 -- Retrieve the allocator node in the generic copy
7554 Acc_T := Etype (Parent (Parent (T)));
7556 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7557 Switch_View (Acc_T);
7564 -- For a proper body, we must catch the case of a proper body that
7565 -- replaces a stub. This represents the point at which a separate
7566 -- compilation unit, and hence template file, may be referenced, so we
7567 -- must make a new source instantiation entry for the template of the
7568 -- subunit, and ensure that all nodes in the subunit are adjusted using
7569 -- this new source instantiation entry.
7571 elsif Nkind (N) in N_Proper_Body then
7573 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7576 if Instantiating and then Was_Originally_Stub (N) then
7577 Create_Instantiation_Source
7578 (Instantiation_Node,
7579 Defining_Entity (N),
7584 -- Now copy the fields of the proper body, using the new
7585 -- adjustment factor if one was needed as per test above.
7589 -- Restore the original adjustment factor in case changed
7591 S_Adjustment := Save_Adjustment;
7594 elsif Nkind (N) = N_Pragma and then Instantiating then
7596 -- Do not copy Comment or Ident pragmas their content is relevant to
7597 -- the generic unit, not to the instantiating unit.
7599 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7600 New_N := Make_Null_Statement (Sloc (N));
7602 -- Do not copy pragmas generated from aspects because the pragmas do
7603 -- not carry any semantic information, plus they will be regenerated
7606 elsif From_Aspect_Specification (N) then
7607 New_N := Make_Null_Statement (Sloc (N));
7613 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7615 -- No descendant fields need traversing
7619 elsif Nkind (N) = N_String_Literal
7620 and then Present (Etype (N))
7621 and then Instantiating
7623 -- If the string is declared in an outer scope, the string_literal
7624 -- subtype created for it may have the wrong scope. Force reanalysis
7625 -- of the constant to generate a new itype in the proper context.
7627 Set_Etype (New_N, Empty);
7628 Set_Analyzed (New_N, False);
7630 -- For the remaining nodes, copy their descendants recursively
7635 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7636 Set_Generic_Parent (Specification (New_N), N);
7638 -- Should preserve Corresponding_Spec??? (12.3(14))
7643 end Copy_Generic_Node;
7645 ----------------------------
7646 -- Denotes_Formal_Package --
7647 ----------------------------
7649 function Denotes_Formal_Package
7651 On_Exit : Boolean := False;
7652 Instance : Entity_Id := Empty) return Boolean
7655 Scop : constant Entity_Id := Scope (Pack);
7658 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7659 -- The package in question may be an actual for a previous formal
7660 -- package P of the current instance, so examine its actuals as well.
7661 -- This must be recursive over other formal packages.
7663 ----------------------------------
7664 -- Is_Actual_Of_Previous_Formal --
7665 ----------------------------------
7667 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7671 E1 := First_Entity (P);
7672 while Present (E1) and then E1 /= Instance loop
7673 if Ekind (E1) = E_Package
7674 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7676 if Renamed_Object (E1) = Pack then
7679 elsif E1 = P or else Renamed_Object (E1) = P then
7682 elsif Is_Actual_Of_Previous_Formal (E1) then
7691 end Is_Actual_Of_Previous_Formal;
7693 -- Start of processing for Denotes_Formal_Package
7699 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7701 Par := Current_Instantiated_Parent.Act_Id;
7704 if Ekind (Scop) = E_Generic_Package
7705 or else Nkind (Unit_Declaration_Node (Scop)) =
7706 N_Generic_Subprogram_Declaration
7710 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7711 N_Formal_Package_Declaration
7719 -- Check whether this package is associated with a formal package of
7720 -- the enclosing instantiation. Iterate over the list of renamings.
7722 E := First_Entity (Par);
7723 while Present (E) loop
7724 if Ekind (E) /= E_Package
7725 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7729 elsif Renamed_Object (E) = Par then
7732 elsif Renamed_Object (E) = Pack then
7735 elsif Is_Actual_Of_Previous_Formal (E) then
7745 end Denotes_Formal_Package;
7751 procedure End_Generic is
7753 -- ??? More things could be factored out in this routine. Should
7754 -- probably be done at a later stage.
7756 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7757 Generic_Flags.Decrement_Last;
7759 Expander_Mode_Restore;
7766 function Earlier (N1, N2 : Node_Id) return Boolean is
7767 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7768 -- Find distance from given node to enclosing compilation unit
7774 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7777 and then Nkind (P) /= N_Compilation_Unit
7779 P := True_Parent (P);
7784 -- Local declarations
7793 -- Start of processing for Earlier
7796 Find_Depth (P1, D1);
7797 Find_Depth (P2, D2);
7807 P1 := True_Parent (P1);
7812 P2 := True_Parent (P2);
7816 -- At this point P1 and P2 are at the same distance from the root.
7817 -- We examine their parents until we find a common declarative list.
7818 -- If we reach the root, N1 and N2 do not descend from the same
7819 -- declarative list (e.g. one is nested in the declarative part and
7820 -- the other is in a block in the statement part) and the earlier
7821 -- one is already frozen.
7823 while not Is_List_Member (P1)
7824 or else not Is_List_Member (P2)
7825 or else List_Containing (P1) /= List_Containing (P2)
7827 P1 := True_Parent (P1);
7828 P2 := True_Parent (P2);
7830 if Nkind (Parent (P1)) = N_Subunit then
7831 P1 := Corresponding_Stub (Parent (P1));
7834 if Nkind (Parent (P2)) = N_Subunit then
7835 P2 := Corresponding_Stub (Parent (P2));
7843 -- Expanded code usually shares the source location of the original
7844 -- construct it was generated for. This however may not necessarely
7845 -- reflect the true location of the code within the tree.
7847 -- Before comparing the slocs of the two nodes, make sure that we are
7848 -- working with correct source locations. Assume that P1 is to the left
7849 -- of P2. If either one does not come from source, traverse the common
7850 -- list heading towards the other node and locate the first source
7854 -- ----+===+===+--------------+===+===+----
7855 -- expanded code expanded code
7857 if not Comes_From_Source (P1) then
7858 while Present (P1) loop
7860 -- Neither P2 nor a source statement were located during the
7861 -- search. If we reach the end of the list, then P1 does not
7862 -- occur earlier than P2.
7865 -- start --- P2 ----- P1 --- end
7867 if No (Next (P1)) then
7870 -- We encounter P2 while going to the right of the list. This
7871 -- means that P1 does indeed appear earlier.
7874 -- start --- P1 ===== P2 --- end
7875 -- expanded code in between
7880 -- No need to look any further since we have located a source
7883 elsif Comes_From_Source (P1) then
7893 if not Comes_From_Source (P2) then
7894 while Present (P2) loop
7896 -- Neither P1 nor a source statement were located during the
7897 -- search. If we reach the start of the list, then P1 does not
7898 -- occur earlier than P2.
7901 -- start --- P2 --- P1 --- end
7903 if No (Prev (P2)) then
7906 -- We encounter P1 while going to the left of the list. This
7907 -- means that P1 does indeed appear earlier.
7910 -- start --- P1 ===== P2 --- end
7911 -- expanded code in between
7916 -- No need to look any further since we have located a source
7919 elsif Comes_From_Source (P2) then
7929 -- At this point either both nodes came from source or we approximated
7930 -- their source locations through neighbouring source statements.
7932 T1 := Top_Level_Location (Sloc (P1));
7933 T2 := Top_Level_Location (Sloc (P2));
7935 -- When two nodes come from the same instance, they have identical top
7936 -- level locations. To determine proper relation within the tree, check
7937 -- their locations within the template.
7940 return Sloc (P1) < Sloc (P2);
7942 -- The two nodes either come from unrelated instances or do not come
7943 -- from instantiated code at all.
7950 ----------------------
7951 -- Find_Actual_Type --
7952 ----------------------
7954 function Find_Actual_Type
7956 Gen_Type : Entity_Id) return Entity_Id
7958 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7962 -- Special processing only applies to child units
7964 if not Is_Child_Unit (Gen_Scope) then
7965 return Get_Instance_Of (Typ);
7967 -- If designated or component type is itself a formal of the child unit,
7968 -- its instance is available.
7970 elsif Scope (Typ) = Gen_Scope then
7971 return Get_Instance_Of (Typ);
7973 -- If the array or access type is not declared in the parent unit,
7974 -- no special processing needed.
7976 elsif not Is_Generic_Type (Typ)
7977 and then Scope (Gen_Scope) /= Scope (Typ)
7979 return Get_Instance_Of (Typ);
7981 -- Otherwise, retrieve designated or component type by visibility
7984 T := Current_Entity (Typ);
7985 while Present (T) loop
7986 if In_Open_Scopes (Scope (T)) then
7988 elsif Is_Generic_Actual_Type (T) then
7997 end Find_Actual_Type;
7999 ----------------------------
8000 -- Freeze_Subprogram_Body --
8001 ----------------------------
8003 procedure Freeze_Subprogram_Body
8004 (Inst_Node : Node_Id;
8006 Pack_Id : Entity_Id)
8008 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8009 Par : constant Entity_Id := Scope (Gen_Unit);
8015 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8016 -- Find innermost package body that encloses the given node, and which
8017 -- is not a compilation unit. Freeze nodes for the instance, or for its
8018 -- enclosing body, may be inserted after the enclosing_body of the
8019 -- generic unit. Used to determine proper placement of freeze node for
8020 -- both package and subprogram instances.
8022 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8023 -- Find entity for given package body, and locate or create a freeze
8026 ----------------------------
8027 -- Enclosing_Package_Body --
8028 ----------------------------
8030 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8036 and then Nkind (Parent (P)) /= N_Compilation_Unit
8038 if Nkind (P) = N_Package_Body then
8039 if Nkind (Parent (P)) = N_Subunit then
8040 return Corresponding_Stub (Parent (P));
8046 P := True_Parent (P);
8050 end Enclosing_Package_Body;
8052 -------------------------
8053 -- Package_Freeze_Node --
8054 -------------------------
8056 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8060 if Nkind (B) = N_Package_Body then
8061 Id := Corresponding_Spec (B);
8062 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8063 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8066 Ensure_Freeze_Node (Id);
8067 return Freeze_Node (Id);
8068 end Package_Freeze_Node;
8070 -- Start of processing of Freeze_Subprogram_Body
8073 -- If the instance and the generic body appear within the same unit, and
8074 -- the instance precedes the generic, the freeze node for the instance
8075 -- must appear after that of the generic. If the generic is nested
8076 -- within another instance I2, then current instance must be frozen
8077 -- after I2. In both cases, the freeze nodes are those of enclosing
8078 -- packages. Otherwise, the freeze node is placed at the end of the
8079 -- current declarative part.
8081 Enc_G := Enclosing_Package_Body (Gen_Body);
8082 Enc_I := Enclosing_Package_Body (Inst_Node);
8083 Ensure_Freeze_Node (Pack_Id);
8084 F_Node := Freeze_Node (Pack_Id);
8086 if Is_Generic_Instance (Par)
8087 and then Present (Freeze_Node (Par))
8088 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8090 -- The parent was a premature instantiation. Insert freeze node at
8091 -- the end the current declarative part.
8093 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8094 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8096 -- Handle the following case:
8098 -- package Parent_Inst is new ...
8101 -- procedure P ... -- this body freezes Parent_Inst
8103 -- package Inst is new ...
8105 -- In this particular scenario, the freeze node for Inst must be
8106 -- inserted in the same manner as that of Parent_Inst - before the
8107 -- next source body or at the end of the declarative list (body not
8108 -- available). If body P did not exist and Parent_Inst was frozen
8109 -- after Inst, either by a body following Inst or at the end of the
8110 -- declarative region, the freeze node for Inst must be inserted
8111 -- after that of Parent_Inst. This relation is established by
8112 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8114 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8115 List_Containing (Inst_Node)
8116 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8118 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8121 Insert_After (Freeze_Node (Par), F_Node);
8124 -- The body enclosing the instance should be frozen after the body that
8125 -- includes the generic, because the body of the instance may make
8126 -- references to entities therein. If the two are not in the same
8127 -- declarative part, or if the one enclosing the instance is frozen
8128 -- already, freeze the instance at the end of the current declarative
8131 elsif Is_Generic_Instance (Par)
8132 and then Present (Freeze_Node (Par))
8133 and then Present (Enc_I)
8135 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8137 (Nkind (Enc_I) = N_Package_Body
8139 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8141 -- The enclosing package may contain several instances. Rather
8142 -- than computing the earliest point at which to insert its freeze
8143 -- node, we place it at the end of the declarative part of the
8144 -- parent of the generic.
8146 Insert_Freeze_Node_For_Instance
8147 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8150 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8152 elsif Present (Enc_G)
8153 and then Present (Enc_I)
8154 and then Enc_G /= Enc_I
8155 and then Earlier (Inst_Node, Gen_Body)
8157 if Nkind (Enc_G) = N_Package_Body then
8159 Corresponding_Spec (Enc_G);
8160 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8162 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8165 -- Freeze package that encloses instance, and place node after the
8166 -- package that encloses generic. If enclosing package is already
8167 -- frozen we have to assume it is at the proper place. This may be a
8168 -- potential ABE that requires dynamic checking. Do not add a freeze
8169 -- node if the package that encloses the generic is inside the body
8170 -- that encloses the instance, because the freeze node would be in
8171 -- the wrong scope. Additional contortions needed if the bodies are
8172 -- within a subunit.
8175 Enclosing_Body : Node_Id;
8178 if Nkind (Enc_I) = N_Package_Body_Stub then
8179 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8181 Enclosing_Body := Enc_I;
8184 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8185 Insert_Freeze_Node_For_Instance
8186 (Enc_G, Package_Freeze_Node (Enc_I));
8190 -- Freeze enclosing subunit before instance
8192 Ensure_Freeze_Node (E_G_Id);
8194 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8195 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8198 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8201 -- If none of the above, insert freeze node at the end of the current
8202 -- declarative part.
8204 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8206 end Freeze_Subprogram_Body;
8212 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8214 return Generic_Renamings.Table (E).Gen_Id;
8217 ---------------------
8218 -- Get_Instance_Of --
8219 ---------------------
8221 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8222 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8225 if Res /= Assoc_Null then
8226 return Generic_Renamings.Table (Res).Act_Id;
8229 -- On exit, entity is not instantiated: not a generic parameter, or
8230 -- else parameter of an inner generic unit.
8234 end Get_Instance_Of;
8236 ------------------------------------
8237 -- Get_Package_Instantiation_Node --
8238 ------------------------------------
8240 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8241 Decl : Node_Id := Unit_Declaration_Node (A);
8245 -- If the Package_Instantiation attribute has been set on the package
8246 -- entity, then use it directly when it (or its Original_Node) refers
8247 -- to an N_Package_Instantiation node. In principle it should be
8248 -- possible to have this field set in all cases, which should be
8249 -- investigated, and would allow this function to be significantly
8252 Inst := Package_Instantiation (A);
8254 if Present (Inst) then
8255 if Nkind (Inst) = N_Package_Instantiation then
8258 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8259 return Original_Node (Inst);
8263 -- If the instantiation is a compilation unit that does not need body
8264 -- then the instantiation node has been rewritten as a package
8265 -- declaration for the instance, and we return the original node.
8267 -- If it is a compilation unit and the instance node has not been
8268 -- rewritten, then it is still the unit of the compilation. Finally, if
8269 -- a body is present, this is a parent of the main unit whose body has
8270 -- been compiled for inlining purposes, and the instantiation node has
8271 -- been rewritten with the instance body.
8273 -- Otherwise the instantiation node appears after the declaration. If
8274 -- the entity is a formal package, the declaration may have been
8275 -- rewritten as a generic declaration (in the case of a formal with box)
8276 -- or left as a formal package declaration if it has actuals, and is
8277 -- found with a forward search.
8279 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8280 if Nkind (Decl) = N_Package_Declaration
8281 and then Present (Corresponding_Body (Decl))
8283 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8286 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8287 return Original_Node (Decl);
8289 return Unit (Parent (Decl));
8292 elsif Nkind (Decl) = N_Package_Declaration
8293 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8295 return Original_Node (Decl);
8298 Inst := Next (Decl);
8299 while not Nkind_In (Inst, N_Package_Instantiation,
8300 N_Formal_Package_Declaration)
8307 end Get_Package_Instantiation_Node;
8309 ------------------------
8310 -- Has_Been_Exchanged --
8311 ------------------------
8313 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8317 Next := First_Elmt (Exchanged_Views);
8318 while Present (Next) loop
8319 if Full_View (Node (Next)) = E then
8327 end Has_Been_Exchanged;
8333 function Hash (F : Entity_Id) return HTable_Range is
8335 return HTable_Range (F mod HTable_Size);
8338 ------------------------
8339 -- Hide_Current_Scope --
8340 ------------------------
8342 procedure Hide_Current_Scope is
8343 C : constant Entity_Id := Current_Scope;
8347 Set_Is_Hidden_Open_Scope (C);
8349 E := First_Entity (C);
8350 while Present (E) loop
8351 if Is_Immediately_Visible (E) then
8352 Set_Is_Immediately_Visible (E, False);
8353 Append_Elmt (E, Hidden_Entities);
8359 -- Make the scope name invisible as well. This is necessary, but might
8360 -- conflict with calls to Rtsfind later on, in case the scope is a
8361 -- predefined one. There is no clean solution to this problem, so for
8362 -- now we depend on the user not redefining Standard itself in one of
8363 -- the parent units.
8365 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8366 Set_Is_Immediately_Visible (C, False);
8367 Append_Elmt (C, Hidden_Entities);
8370 end Hide_Current_Scope;
8376 procedure Init_Env is
8377 Saved : Instance_Env;
8380 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8381 Saved.Exchanged_Views := Exchanged_Views;
8382 Saved.Hidden_Entities := Hidden_Entities;
8383 Saved.Current_Sem_Unit := Current_Sem_Unit;
8384 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8385 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8387 -- Save configuration switches. These may be reset if the unit is a
8388 -- predefined unit, and the current mode is not Ada 2005.
8390 Save_Opt_Config_Switches (Saved.Switches);
8392 Instance_Envs.Append (Saved);
8394 Exchanged_Views := New_Elmt_List;
8395 Hidden_Entities := New_Elmt_List;
8397 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8398 -- this is set properly in Set_Instance_Env.
8400 Current_Instantiated_Parent :=
8401 (Current_Scope, Current_Scope, Assoc_Null);
8404 ------------------------------
8405 -- In_Same_Declarative_Part --
8406 ------------------------------
8408 function In_Same_Declarative_Part
8410 Inst : Node_Id) return Boolean
8412 Decls : constant Node_Id := Parent (F_Node);
8416 Nod := Parent (Inst);
8417 while Present (Nod) loop
8421 elsif Nkind_In (Nod, N_Subprogram_Body,
8423 N_Package_Declaration,
8430 elsif Nkind (Nod) = N_Subunit then
8431 Nod := Corresponding_Stub (Nod);
8433 elsif Nkind (Nod) = N_Compilation_Unit then
8437 Nod := Parent (Nod);
8442 end In_Same_Declarative_Part;
8444 ---------------------
8445 -- In_Main_Context --
8446 ---------------------
8448 function In_Main_Context (E : Entity_Id) return Boolean is
8454 if not Is_Compilation_Unit (E)
8455 or else Ekind (E) /= E_Package
8456 or else In_Private_Part (E)
8461 Context := Context_Items (Cunit (Main_Unit));
8463 Clause := First (Context);
8464 while Present (Clause) loop
8465 if Nkind (Clause) = N_With_Clause then
8466 Nam := Name (Clause);
8468 -- If the current scope is part of the context of the main unit,
8469 -- analysis of the corresponding with_clause is not complete, and
8470 -- the entity is not set. We use the Chars field directly, which
8471 -- might produce false positives in rare cases, but guarantees
8472 -- that we produce all the instance bodies we will need.
8474 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8475 or else (Nkind (Nam) = N_Selected_Component
8476 and then Chars (Selector_Name (Nam)) = Chars (E))
8486 end In_Main_Context;
8488 ---------------------
8489 -- Inherit_Context --
8490 ---------------------
8492 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8493 Current_Context : List_Id;
8494 Current_Unit : Node_Id;
8503 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8505 -- The inherited context is attached to the enclosing compilation
8506 -- unit. This is either the main unit, or the declaration for the
8507 -- main unit (in case the instantiation appears within the package
8508 -- declaration and the main unit is its body).
8510 Current_Unit := Parent (Inst);
8511 while Present (Current_Unit)
8512 and then Nkind (Current_Unit) /= N_Compilation_Unit
8514 Current_Unit := Parent (Current_Unit);
8517 Current_Context := Context_Items (Current_Unit);
8519 Item := First (Context_Items (Parent (Gen_Decl)));
8520 while Present (Item) loop
8521 if Nkind (Item) = N_With_Clause then
8522 Lib_Unit := Library_Unit (Item);
8524 -- Take care to prevent direct cyclic with's
8526 if Lib_Unit /= Current_Unit then
8528 -- Do not add a unit if it is already in the context
8530 Clause := First (Current_Context);
8532 while Present (Clause) loop
8533 if Nkind (Clause) = N_With_Clause and then
8534 Library_Unit (Clause) = Lib_Unit
8544 New_I := New_Copy (Item);
8545 Set_Implicit_With (New_I, True);
8546 Set_Implicit_With_From_Instantiation (New_I, True);
8547 Append (New_I, Current_Context);
8555 end Inherit_Context;
8561 procedure Initialize is
8563 Generic_Renamings.Init;
8566 Generic_Renamings_HTable.Reset;
8567 Circularity_Detected := False;
8568 Exchanged_Views := No_Elist;
8569 Hidden_Entities := No_Elist;
8572 -------------------------------------
8573 -- Insert_Freeze_Node_For_Instance --
8574 -------------------------------------
8576 procedure Insert_Freeze_Node_For_Instance
8585 function Enclosing_Body (N : Node_Id) return Node_Id;
8586 -- Find enclosing package or subprogram body, if any. Freeze node may
8587 -- be placed at end of current declarative list if previous instance
8588 -- and current one have different enclosing bodies.
8590 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8591 -- Find the local instance, if any, that declares the generic that is
8592 -- being instantiated. If present, the freeze node for this instance
8593 -- must follow the freeze node for the previous instance.
8595 --------------------
8596 -- Enclosing_Body --
8597 --------------------
8599 function Enclosing_Body (N : Node_Id) return Node_Id is
8605 and then Nkind (Parent (P)) /= N_Compilation_Unit
8607 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8608 if Nkind (Parent (P)) = N_Subunit then
8609 return Corresponding_Stub (Parent (P));
8615 P := True_Parent (P);
8621 -----------------------
8622 -- Previous_Instance --
8623 -----------------------
8625 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8630 while Present (S) and then S /= Standard_Standard loop
8631 if Is_Generic_Instance (S)
8632 and then In_Same_Source_Unit (S, N)
8641 end Previous_Instance;
8643 -- Start of processing for Insert_Freeze_Node_For_Instance
8646 if not Is_List_Member (F_Node) then
8648 Decls := List_Containing (N);
8649 Inst := Entity (F_Node);
8650 Par_N := Parent (Decls);
8652 -- When processing a subprogram instantiation, utilize the actual
8653 -- subprogram instantiation rather than its package wrapper as it
8654 -- carries all the context information.
8656 if Is_Wrapper_Package (Inst) then
8657 Inst := Related_Instance (Inst);
8660 -- If this is a package instance, check whether the generic is
8661 -- declared in a previous instance and the current instance is
8662 -- not within the previous one.
8664 if Present (Generic_Parent (Parent (Inst)))
8665 and then Is_In_Main_Unit (N)
8668 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8669 Par_I : constant Entity_Id :=
8671 (Generic_Parent (Parent (Inst)));
8676 and then Earlier (N, Freeze_Node (Par_I))
8678 Scop := Scope (Inst);
8680 -- If the current instance is within the one that contains
8681 -- the generic, the freeze node for the current one must
8682 -- appear in the current declarative part. Ditto, if the
8683 -- current instance is within another package instance or
8684 -- within a body that does not enclose the current instance.
8685 -- In these three cases the freeze node of the previous
8686 -- instance is not relevant.
8688 while Present (Scop) and then Scop /= Standard_Standard loop
8689 exit when Scop = Par_I
8691 (Is_Generic_Instance (Scop)
8692 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8693 Scop := Scope (Scop);
8696 -- Previous instance encloses current instance
8698 if Scop = Par_I then
8701 -- If the next node is a source body we must freeze in
8702 -- the current scope as well.
8704 elsif Present (Next (N))
8705 and then Nkind_In (Next (N), N_Subprogram_Body,
8707 and then Comes_From_Source (Next (N))
8711 -- Current instance is within an unrelated instance
8713 elsif Is_Generic_Instance (Scop) then
8716 -- Current instance is within an unrelated body
8718 elsif Present (Enclosing_N)
8719 and then Enclosing_N /= Enclosing_Body (Par_I)
8724 Insert_After (Freeze_Node (Par_I), F_Node);
8731 -- When the instantiation occurs in a package declaration, append the
8732 -- freeze node to the private declarations (if any).
8734 if Nkind (Par_N) = N_Package_Specification
8735 and then Decls = Visible_Declarations (Par_N)
8736 and then Present (Private_Declarations (Par_N))
8737 and then not Is_Empty_List (Private_Declarations (Par_N))
8739 Decls := Private_Declarations (Par_N);
8740 Decl := First (Decls);
8743 -- Determine the proper freeze point of a package instantiation. We
8744 -- adhere to the general rule of a package or subprogram body causing
8745 -- freezing of anything before it in the same declarative region. In
8746 -- this case, the proper freeze point of a package instantiation is
8747 -- before the first source body which follows, or before a stub. This
8748 -- ensures that entities coming from the instance are already frozen
8749 -- and usable in source bodies.
8751 if Nkind (Par_N) /= N_Package_Declaration
8752 and then Ekind (Inst) = E_Package
8753 and then Is_Generic_Instance (Inst)
8755 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8757 while Present (Decl) loop
8758 if (Nkind (Decl) in N_Unit_Body
8760 Nkind (Decl) in N_Body_Stub)
8761 and then Comes_From_Source (Decl)
8763 Insert_Before (Decl, F_Node);
8771 -- In a package declaration, or if no previous body, insert at end
8774 Set_Sloc (F_Node, Sloc (Last (Decls)));
8775 Insert_After (Last (Decls), F_Node);
8777 end Insert_Freeze_Node_For_Instance;
8783 procedure Install_Body
8784 (Act_Body : Node_Id;
8789 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8790 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8791 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8792 Par : constant Entity_Id := Scope (Gen_Id);
8793 Gen_Unit : constant Node_Id :=
8794 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8795 Orig_Body : Node_Id := Gen_Body;
8797 Body_Unit : Node_Id;
8799 Must_Delay : Boolean;
8801 function In_Same_Enclosing_Subp return Boolean;
8802 -- Check whether instance and generic body are within same subprogram.
8804 function True_Sloc (N : Node_Id) return Source_Ptr;
8805 -- If the instance is nested inside a generic unit, the Sloc of the
8806 -- instance indicates the place of the original definition, not the
8807 -- point of the current enclosing instance. Pending a better usage of
8808 -- Slocs to indicate instantiation places, we determine the place of
8809 -- origin of a node by finding the maximum sloc of any ancestor node.
8810 -- Why is this not equivalent to Top_Level_Location ???
8812 ----------------------------
8813 -- In_Same_Enclosing_Subp --
8814 ----------------------------
8816 function In_Same_Enclosing_Subp return Boolean is
8821 Scop := Scope (Act_Id);
8822 while Scop /= Standard_Standard
8823 and then not Is_Overloadable (Scop)
8825 Scop := Scope (Scop);
8828 if Scop = Standard_Standard then
8834 Scop := Scope (Gen_Id);
8835 while Scop /= Standard_Standard loop
8839 Scop := Scope (Scop);
8844 end In_Same_Enclosing_Subp;
8850 function True_Sloc (N : Node_Id) return Source_Ptr is
8857 while Present (N1) and then N1 /= Act_Unit loop
8858 if Sloc (N1) > Res then
8868 -- Start of processing for Install_Body
8871 -- Handle first the case of an instance with incomplete actual types.
8872 -- The instance body cannot be placed after the declaration because
8873 -- full views have not been seen yet. Any use of the non-limited views
8874 -- in the instance body requires the presence of a regular with_clause
8875 -- in the enclosing unit, and will fail if this with_clause is missing.
8876 -- We place the instance body at the beginning of the enclosing body,
8877 -- which is the unit being compiled. The freeze node for the instance
8878 -- is then placed after the instance body.
8880 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8881 and then Expander_Active
8882 and then Ekind (Scope (Act_Id)) = E_Package
8885 Scop : constant Entity_Id := Scope (Act_Id);
8886 Body_Id : constant Node_Id :=
8887 Corresponding_Body (Unit_Declaration_Node (Scop));
8890 Ensure_Freeze_Node (Act_Id);
8891 F_Node := Freeze_Node (Act_Id);
8892 if Present (Body_Id) then
8893 Set_Is_Frozen (Act_Id, False);
8894 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8895 if Is_List_Member (F_Node) then
8899 Insert_After (Act_Body, F_Node);
8905 -- If the body is a subunit, the freeze point is the corresponding stub
8906 -- in the current compilation, not the subunit itself.
8908 if Nkind (Parent (Gen_Body)) = N_Subunit then
8909 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8911 Orig_Body := Gen_Body;
8914 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8916 -- If the instantiation and the generic definition appear in the same
8917 -- package declaration, this is an early instantiation. If they appear
8918 -- in the same declarative part, it is an early instantiation only if
8919 -- the generic body appears textually later, and the generic body is
8920 -- also in the main unit.
8922 -- If instance is nested within a subprogram, and the generic body
8923 -- is not, the instance is delayed because the enclosing body is. If
8924 -- instance and body are within the same scope, or the same subprogram
8925 -- body, indicate explicitly that the instance is delayed.
8928 (Gen_Unit = Act_Unit
8929 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8930 N_Generic_Package_Declaration)
8931 or else (Gen_Unit = Body_Unit
8932 and then True_Sloc (N) < Sloc (Orig_Body)))
8933 and then Is_In_Main_Unit (Gen_Unit)
8934 and then (Scope (Act_Id) = Scope (Gen_Id)
8935 or else In_Same_Enclosing_Subp));
8937 -- If this is an early instantiation, the freeze node is placed after
8938 -- the generic body. Otherwise, if the generic appears in an instance,
8939 -- we cannot freeze the current instance until the outer one is frozen.
8940 -- This is only relevant if the current instance is nested within some
8941 -- inner scope not itself within the outer instance. If this scope is
8942 -- a package body in the same declarative part as the outer instance,
8943 -- then that body needs to be frozen after the outer instance. Finally,
8944 -- if no delay is needed, we place the freeze node at the end of the
8945 -- current declarative part.
8947 if Expander_Active then
8948 Ensure_Freeze_Node (Act_Id);
8949 F_Node := Freeze_Node (Act_Id);
8952 Insert_After (Orig_Body, F_Node);
8954 elsif Is_Generic_Instance (Par)
8955 and then Present (Freeze_Node (Par))
8956 and then Scope (Act_Id) /= Par
8958 -- Freeze instance of inner generic after instance of enclosing
8961 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8963 -- Handle the following case:
8965 -- package Parent_Inst is new ...
8968 -- procedure P ... -- this body freezes Parent_Inst
8970 -- package Inst is new ...
8972 -- In this particular scenario, the freeze node for Inst must
8973 -- be inserted in the same manner as that of Parent_Inst,
8974 -- before the next source body or at the end of the declarative
8975 -- list (body not available). If body P did not exist and
8976 -- Parent_Inst was frozen after Inst, either by a body
8977 -- following Inst or at the end of the declarative region,
8978 -- the freeze node for Inst must be inserted after that of
8979 -- Parent_Inst. This relation is established by comparing
8980 -- the Slocs of Parent_Inst freeze node and Inst.
8982 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8984 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8986 Insert_Freeze_Node_For_Instance (N, F_Node);
8988 Insert_After (Freeze_Node (Par), F_Node);
8991 -- Freeze package enclosing instance of inner generic after
8992 -- instance of enclosing generic.
8994 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8995 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8998 Enclosing : Entity_Id;
9001 Enclosing := Corresponding_Spec (Parent (N));
9003 if No (Enclosing) then
9004 Enclosing := Defining_Entity (Parent (N));
9007 Insert_Freeze_Node_For_Instance (N, F_Node);
9008 Ensure_Freeze_Node (Enclosing);
9010 if not Is_List_Member (Freeze_Node (Enclosing)) then
9012 -- The enclosing context is a subunit, insert the freeze
9013 -- node after the stub.
9015 if Nkind (Parent (Parent (N))) = N_Subunit then
9016 Insert_Freeze_Node_For_Instance
9017 (Corresponding_Stub (Parent (Parent (N))),
9018 Freeze_Node (Enclosing));
9020 -- The enclosing context is a package with a stub body
9021 -- which has already been replaced by the real body.
9022 -- Insert the freeze node after the actual body.
9024 elsif Ekind (Enclosing) = E_Package
9025 and then Present (Body_Entity (Enclosing))
9026 and then Was_Originally_Stub
9027 (Parent (Body_Entity (Enclosing)))
9029 Insert_Freeze_Node_For_Instance
9030 (Parent (Body_Entity (Enclosing)),
9031 Freeze_Node (Enclosing));
9033 -- The parent instance has been frozen before the body of
9034 -- the enclosing package, insert the freeze node after
9037 elsif List_Containing (Freeze_Node (Par)) =
9038 List_Containing (Parent (N))
9039 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9041 Insert_Freeze_Node_For_Instance
9042 (Parent (N), Freeze_Node (Enclosing));
9046 (Freeze_Node (Par), Freeze_Node (Enclosing));
9052 Insert_Freeze_Node_For_Instance (N, F_Node);
9056 Insert_Freeze_Node_For_Instance (N, F_Node);
9060 Set_Is_Frozen (Act_Id);
9061 Insert_Before (N, Act_Body);
9062 Mark_Rewrite_Insertion (Act_Body);
9065 -----------------------------
9066 -- Install_Formal_Packages --
9067 -----------------------------
9069 procedure Install_Formal_Packages (Par : Entity_Id) is
9072 Gen_E : Entity_Id := Empty;
9075 E := First_Entity (Par);
9077 -- If we are installing an instance parent, locate the formal packages
9078 -- of its generic parent.
9080 if Is_Generic_Instance (Par) then
9081 Gen := Generic_Parent (Package_Specification (Par));
9082 Gen_E := First_Entity (Gen);
9085 while Present (E) loop
9086 if Ekind (E) = E_Package
9087 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9089 -- If this is the renaming for the parent instance, done
9091 if Renamed_Object (E) = Par then
9094 -- The visibility of a formal of an enclosing generic is already
9097 elsif Denotes_Formal_Package (E) then
9100 elsif Present (Associated_Formal_Package (E)) then
9101 Check_Generic_Actuals (Renamed_Object (E), True);
9102 Set_Is_Hidden (E, False);
9104 -- Find formal package in generic unit that corresponds to
9105 -- (instance of) formal package in instance.
9107 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9108 Next_Entity (Gen_E);
9111 if Present (Gen_E) then
9112 Map_Formal_Package_Entities (Gen_E, E);
9119 if Present (Gen_E) then
9120 Next_Entity (Gen_E);
9123 end Install_Formal_Packages;
9125 --------------------
9126 -- Install_Parent --
9127 --------------------
9129 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9130 Ancestors : constant Elist_Id := New_Elmt_List;
9131 S : constant Entity_Id := Current_Scope;
9132 Inst_Par : Entity_Id;
9133 First_Par : Entity_Id;
9134 Inst_Node : Node_Id;
9135 Gen_Par : Entity_Id;
9136 First_Gen : Entity_Id;
9139 procedure Install_Noninstance_Specs (Par : Entity_Id);
9140 -- Install the scopes of noninstance parent units ending with Par
9142 procedure Install_Spec (Par : Entity_Id);
9143 -- The child unit is within the declarative part of the parent, so the
9144 -- declarations within the parent are immediately visible.
9146 -------------------------------
9147 -- Install_Noninstance_Specs --
9148 -------------------------------
9150 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9153 and then Par /= Standard_Standard
9154 and then not In_Open_Scopes (Par)
9156 Install_Noninstance_Specs (Scope (Par));
9159 end Install_Noninstance_Specs;
9165 procedure Install_Spec (Par : Entity_Id) is
9166 Spec : constant Node_Id := Package_Specification (Par);
9169 -- If this parent of the child instance is a top-level unit,
9170 -- then record the unit and its visibility for later resetting in
9171 -- Remove_Parent. We exclude units that are generic instances, as we
9172 -- only want to record this information for the ultimate top-level
9173 -- noninstance parent (is that always correct???).
9175 if Scope (Par) = Standard_Standard
9176 and then not Is_Generic_Instance (Par)
9178 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9179 Instance_Parent_Unit := Par;
9182 -- Open the parent scope and make it and its declarations visible.
9183 -- If this point is not within a body, then only the visible
9184 -- declarations should be made visible, and installation of the
9185 -- private declarations is deferred until the appropriate point
9186 -- within analysis of the spec being instantiated (see the handling
9187 -- of parent visibility in Analyze_Package_Specification). This is
9188 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9189 -- private view problems that occur when compiling instantiations of
9190 -- a generic child of that package (Generic_Dispatching_Constructor).
9191 -- If the instance freezes a tagged type, inlinings of operations
9192 -- from Ada.Tags may need the full view of type Tag. If inlining took
9193 -- proper account of establishing visibility of inlined subprograms'
9194 -- parents then it should be possible to remove this
9195 -- special check. ???
9198 Set_Is_Immediately_Visible (Par);
9199 Install_Visible_Declarations (Par);
9200 Set_Use (Visible_Declarations (Spec));
9202 if In_Body or else Is_RTU (Par, Ada_Tags) then
9203 Install_Private_Declarations (Par);
9204 Set_Use (Private_Declarations (Spec));
9208 -- Start of processing for Install_Parent
9211 -- We need to install the parent instance to compile the instantiation
9212 -- of the child, but the child instance must appear in the current
9213 -- scope. Given that we cannot place the parent above the current scope
9214 -- in the scope stack, we duplicate the current scope and unstack both
9215 -- after the instantiation is complete.
9217 -- If the parent is itself the instantiation of a child unit, we must
9218 -- also stack the instantiation of its parent, and so on. Each such
9219 -- ancestor is the prefix of the name in a prior instantiation.
9221 -- If this is a nested instance, the parent unit itself resolves to
9222 -- a renaming of the parent instance, whose declaration we need.
9224 -- Finally, the parent may be a generic (not an instance) when the
9225 -- child unit appears as a formal package.
9229 if Present (Renamed_Entity (Inst_Par)) then
9230 Inst_Par := Renamed_Entity (Inst_Par);
9233 First_Par := Inst_Par;
9235 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9237 First_Gen := Gen_Par;
9239 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9241 -- Load grandparent instance as well
9243 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9245 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9246 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9248 if Present (Renamed_Entity (Inst_Par)) then
9249 Inst_Par := Renamed_Entity (Inst_Par);
9252 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9254 if Present (Gen_Par) then
9255 Prepend_Elmt (Inst_Par, Ancestors);
9258 -- Parent is not the name of an instantiation
9260 Install_Noninstance_Specs (Inst_Par);
9271 if Present (First_Gen) then
9272 Append_Elmt (First_Par, Ancestors);
9274 Install_Noninstance_Specs (First_Par);
9277 if not Is_Empty_Elmt_List (Ancestors) then
9278 Elmt := First_Elmt (Ancestors);
9279 while Present (Elmt) loop
9280 Install_Spec (Node (Elmt));
9281 Install_Formal_Packages (Node (Elmt));
9291 -------------------------------
9292 -- Install_Hidden_Primitives --
9293 -------------------------------
9295 procedure Install_Hidden_Primitives
9296 (Prims_List : in out Elist_Id;
9301 List : Elist_Id := No_Elist;
9302 Prim_G_Elmt : Elmt_Id;
9303 Prim_A_Elmt : Elmt_Id;
9308 -- No action needed in case of serious errors because we cannot trust
9309 -- in the order of primitives
9311 if Serious_Errors_Detected > 0 then
9314 -- No action possible if we don't have available the list of primitive
9318 or else not Is_Record_Type (Gen_T)
9319 or else not Is_Tagged_Type (Gen_T)
9320 or else not Is_Record_Type (Act_T)
9321 or else not Is_Tagged_Type (Act_T)
9325 -- There is no need to handle interface types since their primitives
9328 elsif Is_Interface (Gen_T) then
9332 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9334 if not Is_Class_Wide_Type (Act_T) then
9335 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9337 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9341 -- Skip predefined primitives in the generic formal
9343 while Present (Prim_G_Elmt)
9344 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9346 Next_Elmt (Prim_G_Elmt);
9349 -- Skip predefined primitives in the generic actual
9351 while Present (Prim_A_Elmt)
9352 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9354 Next_Elmt (Prim_A_Elmt);
9357 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9359 Prim_G := Node (Prim_G_Elmt);
9360 Prim_A := Node (Prim_A_Elmt);
9362 -- There is no need to handle interface primitives because their
9363 -- primitives are not hidden
9365 exit when Present (Interface_Alias (Prim_G));
9367 -- Here we install one hidden primitive
9369 if Chars (Prim_G) /= Chars (Prim_A)
9370 and then Has_Suffix (Prim_A, 'P')
9371 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9373 Set_Chars (Prim_A, Chars (Prim_G));
9374 Append_New_Elmt (Prim_A, To => List);
9377 Next_Elmt (Prim_A_Elmt);
9378 Next_Elmt (Prim_G_Elmt);
9381 -- Append the elements to the list of temporarily visible primitives
9382 -- avoiding duplicates.
9384 if Present (List) then
9385 if No (Prims_List) then
9386 Prims_List := New_Elmt_List;
9389 Elmt := First_Elmt (List);
9390 while Present (Elmt) loop
9391 Append_Unique_Elmt (Node (Elmt), Prims_List);
9395 end Install_Hidden_Primitives;
9397 -------------------------------
9398 -- Restore_Hidden_Primitives --
9399 -------------------------------
9401 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9402 Prim_Elmt : Elmt_Id;
9406 if Prims_List /= No_Elist then
9407 Prim_Elmt := First_Elmt (Prims_List);
9408 while Present (Prim_Elmt) loop
9409 Prim := Node (Prim_Elmt);
9410 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9411 Next_Elmt (Prim_Elmt);
9414 Prims_List := No_Elist;
9416 end Restore_Hidden_Primitives;
9418 --------------------------------
9419 -- Instantiate_Formal_Package --
9420 --------------------------------
9422 function Instantiate_Formal_Package
9425 Analyzed_Formal : Node_Id) return List_Id
9427 Loc : constant Source_Ptr := Sloc (Actual);
9428 Actual_Pack : Entity_Id;
9429 Formal_Pack : Entity_Id;
9430 Gen_Parent : Entity_Id;
9433 Parent_Spec : Node_Id;
9435 procedure Find_Matching_Actual
9437 Act : in out Entity_Id);
9438 -- We need to associate each formal entity in the formal package with
9439 -- the corresponding entity in the actual package. The actual package
9440 -- has been analyzed and possibly expanded, and as a result there is
9441 -- no one-to-one correspondence between the two lists (for example,
9442 -- the actual may include subtypes, itypes, and inherited primitive
9443 -- operations, interspersed among the renaming declarations for the
9444 -- actuals). We retrieve the corresponding actual by name because each
9445 -- actual has the same name as the formal, and they do appear in the
9448 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9449 -- Retrieve entity of defining entity of generic formal parameter.
9450 -- Only the declarations of formals need to be considered when
9451 -- linking them to actuals, but the declarative list may include
9452 -- internal entities generated during analysis, and those are ignored.
9454 procedure Match_Formal_Entity
9455 (Formal_Node : Node_Id;
9456 Formal_Ent : Entity_Id;
9457 Actual_Ent : Entity_Id);
9458 -- Associates the formal entity with the actual. In the case where
9459 -- Formal_Ent is a formal package, this procedure iterates through all
9460 -- of its formals and enters associations between the actuals occurring
9461 -- in the formal package's corresponding actual package (given by
9462 -- Actual_Ent) and the formal package's formal parameters. This
9463 -- procedure recurses if any of the parameters is itself a package.
9465 function Is_Instance_Of
9466 (Act_Spec : Entity_Id;
9467 Gen_Anc : Entity_Id) return Boolean;
9468 -- The actual can be an instantiation of a generic within another
9469 -- instance, in which case there is no direct link from it to the
9470 -- original generic ancestor. In that case, we recognize that the
9471 -- ultimate ancestor is the same by examining names and scopes.
9473 procedure Process_Nested_Formal (Formal : Entity_Id);
9474 -- If the current formal is declared with a box, its own formals are
9475 -- visible in the instance, as they were in the generic, and their
9476 -- Hidden flag must be reset. If some of these formals are themselves
9477 -- packages declared with a box, the processing must be recursive.
9479 --------------------------
9480 -- Find_Matching_Actual --
9481 --------------------------
9483 procedure Find_Matching_Actual
9485 Act : in out Entity_Id)
9487 Formal_Ent : Entity_Id;
9490 case Nkind (Original_Node (F)) is
9491 when N_Formal_Object_Declaration |
9492 N_Formal_Type_Declaration =>
9493 Formal_Ent := Defining_Identifier (F);
9495 while Chars (Act) /= Chars (Formal_Ent) loop
9499 when N_Formal_Subprogram_Declaration |
9500 N_Formal_Package_Declaration |
9501 N_Package_Declaration |
9502 N_Generic_Package_Declaration =>
9503 Formal_Ent := Defining_Entity (F);
9505 while Chars (Act) /= Chars (Formal_Ent) loop
9510 raise Program_Error;
9512 end Find_Matching_Actual;
9514 -------------------------
9515 -- Match_Formal_Entity --
9516 -------------------------
9518 procedure Match_Formal_Entity
9519 (Formal_Node : Node_Id;
9520 Formal_Ent : Entity_Id;
9521 Actual_Ent : Entity_Id)
9523 Act_Pkg : Entity_Id;
9526 Set_Instance_Of (Formal_Ent, Actual_Ent);
9528 if Ekind (Actual_Ent) = E_Package then
9530 -- Record associations for each parameter
9532 Act_Pkg := Actual_Ent;
9535 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9544 -- Retrieve the actual given in the formal package declaration
9546 Actual := Entity (Name (Original_Node (Formal_Node)));
9548 -- The actual in the formal package declaration may be a
9549 -- renamed generic package, in which case we want to retrieve
9550 -- the original generic in order to traverse its formal part.
9552 if Present (Renamed_Entity (Actual)) then
9553 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9555 Gen_Decl := Unit_Declaration_Node (Actual);
9558 Formals := Generic_Formal_Declarations (Gen_Decl);
9560 if Present (Formals) then
9561 F_Node := First_Non_Pragma (Formals);
9566 while Present (A_Ent)
9567 and then Present (F_Node)
9568 and then A_Ent /= First_Private_Entity (Act_Pkg)
9570 F_Ent := Get_Formal_Entity (F_Node);
9572 if Present (F_Ent) then
9574 -- This is a formal of the original package. Record
9575 -- association and recurse.
9577 Find_Matching_Actual (F_Node, A_Ent);
9578 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9579 Next_Entity (A_Ent);
9582 Next_Non_Pragma (F_Node);
9586 end Match_Formal_Entity;
9588 -----------------------
9589 -- Get_Formal_Entity --
9590 -----------------------
9592 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9593 Kind : constant Node_Kind := Nkind (Original_Node (N));
9596 when N_Formal_Object_Declaration =>
9597 return Defining_Identifier (N);
9599 when N_Formal_Type_Declaration =>
9600 return Defining_Identifier (N);
9602 when N_Formal_Subprogram_Declaration =>
9603 return Defining_Unit_Name (Specification (N));
9605 when N_Formal_Package_Declaration =>
9606 return Defining_Identifier (Original_Node (N));
9608 when N_Generic_Package_Declaration =>
9609 return Defining_Identifier (Original_Node (N));
9611 -- All other declarations are introduced by semantic analysis and
9612 -- have no match in the actual.
9617 end Get_Formal_Entity;
9619 --------------------
9620 -- Is_Instance_Of --
9621 --------------------
9623 function Is_Instance_Of
9624 (Act_Spec : Entity_Id;
9625 Gen_Anc : Entity_Id) return Boolean
9627 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9630 if No (Gen_Par) then
9633 -- Simplest case: the generic parent of the actual is the formal
9635 elsif Gen_Par = Gen_Anc then
9638 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9641 -- The actual may be obtained through several instantiations. Its
9642 -- scope must itself be an instance of a generic declared in the
9643 -- same scope as the formal. Any other case is detected above.
9645 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9649 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9653 ---------------------------
9654 -- Process_Nested_Formal --
9655 ---------------------------
9657 procedure Process_Nested_Formal (Formal : Entity_Id) is
9661 if Present (Associated_Formal_Package (Formal))
9662 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9664 Ent := First_Entity (Formal);
9665 while Present (Ent) loop
9666 Set_Is_Hidden (Ent, False);
9667 Set_Is_Visible_Formal (Ent);
9668 Set_Is_Potentially_Use_Visible
9669 (Ent, Is_Potentially_Use_Visible (Formal));
9671 if Ekind (Ent) = E_Package then
9672 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9673 Process_Nested_Formal (Ent);
9679 end Process_Nested_Formal;
9681 -- Start of processing for Instantiate_Formal_Package
9686 if not Is_Entity_Name (Actual)
9687 or else Ekind (Entity (Actual)) /= E_Package
9690 ("expect package instance to instantiate formal", Actual);
9691 Abandon_Instantiation (Actual);
9692 raise Program_Error;
9695 Actual_Pack := Entity (Actual);
9696 Set_Is_Instantiated (Actual_Pack);
9698 -- The actual may be a renamed package, or an outer generic formal
9699 -- package whose instantiation is converted into a renaming.
9701 if Present (Renamed_Object (Actual_Pack)) then
9702 Actual_Pack := Renamed_Object (Actual_Pack);
9705 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9706 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9707 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9710 Generic_Parent (Specification (Analyzed_Formal));
9712 Defining_Unit_Name (Specification (Analyzed_Formal));
9715 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9716 Parent_Spec := Package_Specification (Actual_Pack);
9718 Parent_Spec := Parent (Actual_Pack);
9721 if Gen_Parent = Any_Id then
9723 ("previous error in declaration of formal package", Actual);
9724 Abandon_Instantiation (Actual);
9727 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9733 ("actual parameter must be instance of&", Actual, Gen_Parent);
9734 Abandon_Instantiation (Actual);
9737 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9738 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9741 Make_Package_Renaming_Declaration (Loc,
9742 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9743 Name => New_Occurrence_Of (Actual_Pack, Loc));
9745 Set_Associated_Formal_Package
9746 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9747 Decls := New_List (Nod);
9749 -- If the formal F has a box, then the generic declarations are
9750 -- visible in the generic G. In an instance of G, the corresponding
9751 -- entities in the actual for F (which are the actuals for the
9752 -- instantiation of the generic that F denotes) must also be made
9753 -- visible for analysis of the current instance. On exit from the
9754 -- current instance, those entities are made private again. If the
9755 -- actual is currently in use, these entities are also use-visible.
9757 -- The loop through the actual entities also steps through the formal
9758 -- entities and enters associations from formals to actuals into the
9759 -- renaming map. This is necessary to properly handle checking of
9760 -- actual parameter associations for later formals that depend on
9761 -- actuals declared in the formal package.
9763 -- In Ada 2005, partial parameterization requires that we make
9764 -- visible the actuals corresponding to formals that were defaulted
9765 -- in the formal package. There formals are identified because they
9766 -- remain formal generics within the formal package, rather than
9767 -- being renamings of the actuals supplied.
9770 Gen_Decl : constant Node_Id :=
9771 Unit_Declaration_Node (Gen_Parent);
9772 Formals : constant List_Id :=
9773 Generic_Formal_Declarations (Gen_Decl);
9775 Actual_Ent : Entity_Id;
9776 Actual_Of_Formal : Node_Id;
9777 Formal_Node : Node_Id;
9778 Formal_Ent : Entity_Id;
9781 if Present (Formals) then
9782 Formal_Node := First_Non_Pragma (Formals);
9784 Formal_Node := Empty;
9787 Actual_Ent := First_Entity (Actual_Pack);
9789 First (Visible_Declarations (Specification (Analyzed_Formal)));
9790 while Present (Actual_Ent)
9791 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9793 if Present (Formal_Node) then
9794 Formal_Ent := Get_Formal_Entity (Formal_Node);
9796 if Present (Formal_Ent) then
9797 Find_Matching_Actual (Formal_Node, Actual_Ent);
9798 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9800 -- We iterate at the same time over the actuals of the
9801 -- local package created for the formal, to determine
9802 -- which one of the formals of the original generic were
9803 -- defaulted in the formal. The corresponding actual
9804 -- entities are visible in the enclosing instance.
9806 if Box_Present (Formal)
9808 (Present (Actual_Of_Formal)
9811 (Get_Formal_Entity (Actual_Of_Formal)))
9813 Set_Is_Hidden (Actual_Ent, False);
9814 Set_Is_Visible_Formal (Actual_Ent);
9815 Set_Is_Potentially_Use_Visible
9816 (Actual_Ent, In_Use (Actual_Pack));
9818 if Ekind (Actual_Ent) = E_Package then
9819 Process_Nested_Formal (Actual_Ent);
9823 Set_Is_Hidden (Actual_Ent);
9824 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9828 Next_Non_Pragma (Formal_Node);
9829 Next (Actual_Of_Formal);
9832 -- No further formals to match, but the generic part may
9833 -- contain inherited operation that are not hidden in the
9834 -- enclosing instance.
9836 Next_Entity (Actual_Ent);
9840 -- Inherited subprograms generated by formal derived types are
9841 -- also visible if the types are.
9843 Actual_Ent := First_Entity (Actual_Pack);
9844 while Present (Actual_Ent)
9845 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9847 if Is_Overloadable (Actual_Ent)
9849 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9851 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9853 Set_Is_Hidden (Actual_Ent, False);
9854 Set_Is_Potentially_Use_Visible
9855 (Actual_Ent, In_Use (Actual_Pack));
9858 Next_Entity (Actual_Ent);
9862 -- If the formal is not declared with a box, reanalyze it as an
9863 -- abbreviated instantiation, to verify the matching rules of 12.7.
9864 -- The actual checks are performed after the generic associations
9865 -- have been analyzed, to guarantee the same visibility for this
9866 -- instantiation and for the actuals.
9868 -- In Ada 2005, the generic associations for the formal can include
9869 -- defaulted parameters. These are ignored during check. This
9870 -- internal instantiation is removed from the tree after conformance
9871 -- checking, because it contains formal declarations for those
9872 -- defaulted parameters, and those should not reach the back-end.
9874 if not Box_Present (Formal) then
9876 I_Pack : constant Entity_Id :=
9877 Make_Temporary (Sloc (Actual), 'P');
9880 Set_Is_Internal (I_Pack);
9883 Make_Package_Instantiation (Sloc (Actual),
9884 Defining_Unit_Name => I_Pack,
9887 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9888 Generic_Associations => Generic_Associations (Formal)));
9894 end Instantiate_Formal_Package;
9896 -----------------------------------
9897 -- Instantiate_Formal_Subprogram --
9898 -----------------------------------
9900 function Instantiate_Formal_Subprogram
9903 Analyzed_Formal : Node_Id) return Node_Id
9905 Analyzed_S : constant Entity_Id :=
9906 Defining_Unit_Name (Specification (Analyzed_Formal));
9907 Formal_Sub : constant Entity_Id :=
9908 Defining_Unit_Name (Specification (Formal));
9910 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9911 -- If the generic is a child unit, the parent has been installed on the
9912 -- scope stack, but a default subprogram cannot resolve to something
9913 -- on the parent because that parent is not really part of the visible
9914 -- context (it is there to resolve explicit local entities). If the
9915 -- default has resolved in this way, we remove the entity from immediate
9916 -- visibility and analyze the node again to emit an error message or
9917 -- find another visible candidate.
9919 procedure Valid_Actual_Subprogram (Act : Node_Id);
9920 -- Perform legality check and raise exception on failure
9922 -----------------------
9923 -- From_Parent_Scope --
9924 -----------------------
9926 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9927 Gen_Scope : Node_Id;
9930 Gen_Scope := Scope (Analyzed_S);
9931 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9932 if Scope (Subp) = Scope (Gen_Scope) then
9936 Gen_Scope := Scope (Gen_Scope);
9940 end From_Parent_Scope;
9942 -----------------------------
9943 -- Valid_Actual_Subprogram --
9944 -----------------------------
9946 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9950 if Is_Entity_Name (Act) then
9951 Act_E := Entity (Act);
9953 elsif Nkind (Act) = N_Selected_Component
9954 and then Is_Entity_Name (Selector_Name (Act))
9956 Act_E := Entity (Selector_Name (Act));
9962 if (Present (Act_E) and then Is_Overloadable (Act_E))
9963 or else Nkind_In (Act, N_Attribute_Reference,
9964 N_Indexed_Component,
9965 N_Character_Literal,
9966 N_Explicit_Dereference)
9972 ("expect subprogram or entry name in instantiation of &",
9973 Instantiation_Node, Formal_Sub);
9974 Abandon_Instantiation (Instantiation_Node);
9975 end Valid_Actual_Subprogram;
9979 Decl_Node : Node_Id;
9983 New_Subp : Entity_Id;
9985 -- Start of processing for Instantiate_Formal_Subprogram
9988 New_Spec := New_Copy_Tree (Specification (Formal));
9990 -- The tree copy has created the proper instantiation sloc for the
9991 -- new specification. Use this location for all other constructed
9994 Loc := Sloc (Defining_Unit_Name (New_Spec));
9996 -- Create new entity for the actual (New_Copy_Tree does not), and
9997 -- indicate that it is an actual.
9999 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10000 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10001 Set_Is_Generic_Actual_Subprogram (New_Subp);
10002 Set_Defining_Unit_Name (New_Spec, New_Subp);
10004 -- Create new entities for the each of the formals in the specification
10005 -- of the renaming declaration built for the actual.
10007 if Present (Parameter_Specifications (New_Spec)) then
10013 F := First (Parameter_Specifications (New_Spec));
10014 while Present (F) loop
10015 F_Id := Defining_Identifier (F);
10017 Set_Defining_Identifier (F,
10018 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10024 -- Find entity of actual. If the actual is an attribute reference, it
10025 -- cannot be resolved here (its formal is missing) but is handled
10026 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10027 -- fully resolved subsequently, when the renaming declaration for the
10028 -- formal is analyzed. If it is an explicit dereference, resolve the
10029 -- prefix but not the actual itself, to prevent interpretation as call.
10031 if Present (Actual) then
10032 Loc := Sloc (Actual);
10033 Set_Sloc (New_Spec, Loc);
10035 if Nkind (Actual) = N_Operator_Symbol then
10036 Find_Direct_Name (Actual);
10038 elsif Nkind (Actual) = N_Explicit_Dereference then
10039 Analyze (Prefix (Actual));
10041 elsif Nkind (Actual) /= N_Attribute_Reference then
10045 Valid_Actual_Subprogram (Actual);
10048 elsif Present (Default_Name (Formal)) then
10049 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10050 N_Selected_Component,
10051 N_Indexed_Component,
10052 N_Character_Literal)
10053 and then Present (Entity (Default_Name (Formal)))
10055 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10057 Nam := New_Copy (Default_Name (Formal));
10058 Set_Sloc (Nam, Loc);
10061 elsif Box_Present (Formal) then
10063 -- Actual is resolved at the point of instantiation. Create an
10064 -- identifier or operator with the same name as the formal.
10066 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10068 Make_Operator_Symbol (Loc,
10069 Chars => Chars (Formal_Sub),
10070 Strval => No_String);
10072 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10075 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10076 and then Null_Present (Specification (Formal))
10078 -- Generate null body for procedure, for use in the instance
10081 Make_Subprogram_Body (Loc,
10082 Specification => New_Spec,
10083 Declarations => New_List,
10084 Handled_Statement_Sequence =>
10085 Make_Handled_Sequence_Of_Statements (Loc,
10086 Statements => New_List (Make_Null_Statement (Loc))));
10088 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10092 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10094 ("missing actual&", Instantiation_Node, Formal_Sub);
10096 ("\in instantiation of & declared#",
10097 Instantiation_Node, Scope (Analyzed_S));
10098 Abandon_Instantiation (Instantiation_Node);
10102 Make_Subprogram_Renaming_Declaration (Loc,
10103 Specification => New_Spec,
10106 -- If we do not have an actual and the formal specified <> then set to
10107 -- get proper default.
10109 if No (Actual) and then Box_Present (Formal) then
10110 Set_From_Default (Decl_Node);
10113 -- Gather possible interpretations for the actual before analyzing the
10114 -- instance. If overloaded, it will be resolved when analyzing the
10115 -- renaming declaration.
10117 if Box_Present (Formal) and then No (Actual) then
10120 if Is_Child_Unit (Scope (Analyzed_S))
10121 and then Present (Entity (Nam))
10123 if not Is_Overloaded (Nam) then
10124 if From_Parent_Scope (Entity (Nam)) then
10125 Set_Is_Immediately_Visible (Entity (Nam), False);
10126 Set_Entity (Nam, Empty);
10127 Set_Etype (Nam, Empty);
10130 Set_Is_Immediately_Visible (Entity (Nam));
10139 Get_First_Interp (Nam, I, It);
10140 while Present (It.Nam) loop
10141 if From_Parent_Scope (It.Nam) then
10145 Get_Next_Interp (I, It);
10152 -- The generic instantiation freezes the actual. This can only be done
10153 -- once the actual is resolved, in the analysis of the renaming
10154 -- declaration. To make the formal subprogram entity available, we set
10155 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10156 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10157 -- of formal abstract subprograms.
10159 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10161 -- We cannot analyze the renaming declaration, and thus find the actual,
10162 -- until all the actuals are assembled in the instance. For subsequent
10163 -- checks of other actuals, indicate the node that will hold the
10164 -- instance of this formal.
10166 Set_Instance_Of (Analyzed_S, Nam);
10168 if Nkind (Actual) = N_Selected_Component
10169 and then Is_Task_Type (Etype (Prefix (Actual)))
10170 and then not Is_Frozen (Etype (Prefix (Actual)))
10172 -- The renaming declaration will create a body, which must appear
10173 -- outside of the instantiation, We move the renaming declaration
10174 -- out of the instance, and create an additional renaming inside,
10175 -- to prevent freezing anomalies.
10178 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10181 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10182 Insert_Before (Instantiation_Node, Decl_Node);
10183 Analyze (Decl_Node);
10185 -- Now create renaming within the instance
10188 Make_Subprogram_Renaming_Declaration (Loc,
10189 Specification => New_Copy_Tree (New_Spec),
10190 Name => New_Occurrence_Of (Anon_Id, Loc));
10192 Set_Defining_Unit_Name (Specification (Decl_Node),
10193 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10198 end Instantiate_Formal_Subprogram;
10200 ------------------------
10201 -- Instantiate_Object --
10202 ------------------------
10204 function Instantiate_Object
10207 Analyzed_Formal : Node_Id) return List_Id
10209 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10210 A_Gen_Obj : constant Entity_Id :=
10211 Defining_Identifier (Analyzed_Formal);
10212 Acc_Def : Node_Id := Empty;
10213 Act_Assoc : constant Node_Id := Parent (Actual);
10214 Actual_Decl : Node_Id := Empty;
10215 Decl_Node : Node_Id;
10218 List : constant List_Id := New_List;
10219 Loc : constant Source_Ptr := Sloc (Actual);
10220 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10221 Subt_Decl : Node_Id := Empty;
10222 Subt_Mark : Node_Id := Empty;
10224 function Copy_Access_Def return Node_Id;
10225 -- If formal is an anonymous access, copy access definition of formal
10226 -- for generated object declaration.
10228 ---------------------
10229 -- Copy_Access_Def --
10230 ---------------------
10232 function Copy_Access_Def return Node_Id is
10234 Def := New_Copy_Tree (Acc_Def);
10236 -- In addition, if formal is an access to subprogram we need to
10237 -- generate new formals for the signature of the default, so that
10238 -- the tree is properly formatted for ASIS use.
10240 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10242 Par_Spec : Node_Id;
10245 First (Parameter_Specifications
10246 (Access_To_Subprogram_Definition (Def)));
10247 while Present (Par_Spec) loop
10248 Set_Defining_Identifier (Par_Spec,
10249 Make_Defining_Identifier (Sloc (Acc_Def),
10250 Chars => Chars (Defining_Identifier (Par_Spec))));
10257 end Copy_Access_Def;
10259 -- Start of processing for Instantiate_Object
10262 -- Formal may be an anonymous access
10264 if Present (Subtype_Mark (Formal)) then
10265 Subt_Mark := Subtype_Mark (Formal);
10267 Check_Access_Definition (Formal);
10268 Acc_Def := Access_Definition (Formal);
10271 -- Sloc for error message on missing actual
10273 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10275 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10276 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10279 Set_Parent (List, Parent (Actual));
10283 if Out_Present (Formal) then
10285 -- An IN OUT generic actual must be a name. The instantiation is a
10286 -- renaming declaration. The actual is the name being renamed. We
10287 -- use the actual directly, rather than a copy, because it is not
10288 -- used further in the list of actuals, and because a copy or a use
10289 -- of relocate_node is incorrect if the instance is nested within a
10290 -- generic. In order to simplify ASIS searches, the Generic_Parent
10291 -- field links the declaration to the generic association.
10293 if No (Actual) then
10295 ("missing actual &",
10296 Instantiation_Node, Gen_Obj);
10298 ("\in instantiation of & declared#",
10299 Instantiation_Node, Scope (A_Gen_Obj));
10300 Abandon_Instantiation (Instantiation_Node);
10303 if Present (Subt_Mark) then
10305 Make_Object_Renaming_Declaration (Loc,
10306 Defining_Identifier => New_Copy (Gen_Obj),
10307 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10310 else pragma Assert (Present (Acc_Def));
10312 Make_Object_Renaming_Declaration (Loc,
10313 Defining_Identifier => New_Copy (Gen_Obj),
10314 Access_Definition => New_Copy_Tree (Acc_Def),
10318 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10320 -- The analysis of the actual may produce Insert_Action nodes, so
10321 -- the declaration must have a context in which to attach them.
10323 Append (Decl_Node, List);
10326 -- Return if the analysis of the actual reported some error
10328 if Etype (Actual) = Any_Type then
10332 -- This check is performed here because Analyze_Object_Renaming will
10333 -- not check it when Comes_From_Source is False. Note though that the
10334 -- check for the actual being the name of an object will be performed
10335 -- in Analyze_Object_Renaming.
10337 if Is_Object_Reference (Actual)
10338 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10341 ("illegal discriminant-dependent component for in out parameter",
10345 -- The actual has to be resolved in order to check that it is a
10346 -- variable (due to cases such as F (1), where F returns access to
10347 -- an array, and for overloaded prefixes).
10349 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10351 -- If the type of the formal is not itself a formal, and the current
10352 -- unit is a child unit, the formal type must be declared in a
10353 -- parent, and must be retrieved by visibility.
10355 if Ftyp = Orig_Ftyp
10356 and then Is_Generic_Unit (Scope (Ftyp))
10357 and then Is_Child_Unit (Scope (A_Gen_Obj))
10360 Temp : constant Node_Id :=
10361 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10363 Set_Entity (Temp, Empty);
10365 Ftyp := Entity (Temp);
10369 if Is_Private_Type (Ftyp)
10370 and then not Is_Private_Type (Etype (Actual))
10371 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10372 or else Base_Type (Etype (Actual)) = Ftyp)
10374 -- If the actual has the type of the full view of the formal, or
10375 -- else a non-private subtype of the formal, then the visibility
10376 -- of the formal type has changed. Add to the actuals a subtype
10377 -- declaration that will force the exchange of views in the body
10378 -- of the instance as well.
10381 Make_Subtype_Declaration (Loc,
10382 Defining_Identifier => Make_Temporary (Loc, 'P'),
10383 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10385 Prepend (Subt_Decl, List);
10387 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10388 Exchange_Declarations (Ftyp);
10391 Resolve (Actual, Ftyp);
10393 if not Denotes_Variable (Actual) then
10394 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10396 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10398 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10399 -- the type of the actual shall resolve to a specific anonymous
10402 if Ada_Version < Ada_2005
10403 or else Ekind (Base_Type (Ftyp)) /=
10404 E_Anonymous_Access_Type
10405 or else Ekind (Base_Type (Etype (Actual))) /=
10406 E_Anonymous_Access_Type
10409 ("type of actual does not match type of&", Actual, Gen_Obj);
10413 Note_Possible_Modification (Actual, Sure => True);
10415 -- Check for instantiation of atomic/volatile actual for
10416 -- non-atomic/volatile formal (RM C.6 (12)).
10418 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10420 ("cannot instantiate non-atomic formal object "
10421 & "with atomic actual", Actual);
10423 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10426 ("cannot instantiate non-volatile formal object "
10427 & "with volatile actual", Actual);
10430 -- Formal in-parameter
10433 -- The instantiation of a generic formal in-parameter is constant
10434 -- declaration. The actual is the expression for that declaration.
10435 -- Its type is a full copy of the type of the formal. This may be
10436 -- an access to subprogram, for which we need to generate entities
10437 -- for the formals in the new signature.
10439 if Present (Actual) then
10440 if Present (Subt_Mark) then
10441 Def := New_Copy_Tree (Subt_Mark);
10442 else pragma Assert (Present (Acc_Def));
10443 Def := Copy_Access_Def;
10447 Make_Object_Declaration (Loc,
10448 Defining_Identifier => New_Copy (Gen_Obj),
10449 Constant_Present => True,
10450 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10451 Object_Definition => Def,
10452 Expression => Actual);
10454 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10456 -- A generic formal object of a tagged type is defined to be
10457 -- aliased so the new constant must also be treated as aliased.
10459 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10460 Set_Aliased_Present (Decl_Node);
10463 Append (Decl_Node, List);
10465 -- No need to repeat (pre-)analysis of some expression nodes
10466 -- already handled in Preanalyze_Actuals.
10468 if Nkind (Actual) /= N_Allocator then
10471 -- Return if the analysis of the actual reported some error
10473 if Etype (Actual) = Any_Type then
10479 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10483 Typ := Get_Instance_Of (Formal_Type);
10485 -- If the actual appears in the current or an enclosing scope,
10486 -- use its type directly. This is relevant if it has an actual
10487 -- subtype that is distinct from its nominal one. This cannot
10488 -- be done in general because the type of the actual may
10489 -- depend on other actuals, and only be fully determined when
10490 -- the enclosing instance is analyzed.
10492 if Present (Etype (Actual))
10493 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10495 Freeze_Before (Instantiation_Node, Etype (Actual));
10497 Freeze_Before (Instantiation_Node, Typ);
10500 -- If the actual is an aggregate, perform name resolution on
10501 -- its components (the analysis of an aggregate does not do it)
10502 -- to capture local names that may be hidden if the generic is
10505 if Nkind (Actual) = N_Aggregate then
10506 Preanalyze_And_Resolve (Actual, Typ);
10509 if Is_Limited_Type (Typ)
10510 and then not OK_For_Limited_Init (Typ, Actual)
10513 ("initialization not allowed for limited types", Actual);
10514 Explain_Limited_Type (Typ, Actual);
10518 elsif Present (Default_Expression (Formal)) then
10520 -- Use default to construct declaration
10522 if Present (Subt_Mark) then
10523 Def := New_Copy (Subt_Mark);
10524 else pragma Assert (Present (Acc_Def));
10525 Def := Copy_Access_Def;
10529 Make_Object_Declaration (Sloc (Formal),
10530 Defining_Identifier => New_Copy (Gen_Obj),
10531 Constant_Present => True,
10532 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10533 Object_Definition => Def,
10534 Expression => New_Copy_Tree
10535 (Default_Expression (Formal)));
10537 Append (Decl_Node, List);
10538 Set_Analyzed (Expression (Decl_Node), False);
10541 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10542 Error_Msg_NE ("\in instantiation of & declared#",
10543 Instantiation_Node, Scope (A_Gen_Obj));
10545 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10547 -- Create dummy constant declaration so that instance can be
10548 -- analyzed, to minimize cascaded visibility errors.
10550 if Present (Subt_Mark) then
10552 else pragma Assert (Present (Acc_Def));
10557 Make_Object_Declaration (Loc,
10558 Defining_Identifier => New_Copy (Gen_Obj),
10559 Constant_Present => True,
10560 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10561 Object_Definition => New_Copy (Def),
10563 Make_Attribute_Reference (Sloc (Gen_Obj),
10564 Attribute_Name => Name_First,
10565 Prefix => New_Copy (Def)));
10567 Append (Decl_Node, List);
10570 Abandon_Instantiation (Instantiation_Node);
10575 if Nkind (Actual) in N_Has_Entity then
10576 Actual_Decl := Parent (Entity (Actual));
10579 -- Ada 2005 (AI-423): For a formal object declaration with a null
10580 -- exclusion or an access definition that has a null exclusion: If the
10581 -- actual matching the formal object declaration denotes a generic
10582 -- formal object of another generic unit G, and the instantiation
10583 -- containing the actual occurs within the body of G or within the body
10584 -- of a generic unit declared within the declarative region of G, then
10585 -- the declaration of the formal object of G must have a null exclusion.
10586 -- Otherwise, the subtype of the actual matching the formal object
10587 -- declaration shall exclude null.
10589 if Ada_Version >= Ada_2005
10590 and then Present (Actual_Decl)
10591 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10592 N_Object_Declaration)
10593 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10594 and then not Has_Null_Exclusion (Actual_Decl)
10595 and then Has_Null_Exclusion (Analyzed_Formal)
10597 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10599 ("actual must exclude null to match generic formal#", Actual);
10602 -- An effectively volatile object cannot be used as an actual in
10603 -- a generic instance. The following check is only relevant when
10604 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10607 and then Present (Actual)
10608 and then Is_Effectively_Volatile_Object (Actual)
10611 ("volatile object cannot act as actual in generic instantiation "
10612 & "(SPARK RM 7.1.3(8))", Actual);
10616 end Instantiate_Object;
10618 ------------------------------
10619 -- Instantiate_Package_Body --
10620 ------------------------------
10622 procedure Instantiate_Package_Body
10623 (Body_Info : Pending_Body_Info;
10624 Inlined_Body : Boolean := False;
10625 Body_Optional : Boolean := False)
10627 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10628 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10629 Loc : constant Source_Ptr := Sloc (Inst_Node);
10631 Gen_Id : constant Node_Id := Name (Inst_Node);
10632 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10633 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10634 Act_Spec : constant Node_Id := Specification (Act_Decl);
10635 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10637 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10638 Save_Style_Check : constant Boolean := Style_Check;
10640 Act_Body : Node_Id;
10641 Act_Body_Id : Entity_Id;
10642 Act_Body_Name : Node_Id;
10643 Gen_Body : Node_Id;
10644 Gen_Body_Id : Node_Id;
10645 Par_Ent : Entity_Id := Empty;
10646 Par_Vis : Boolean := False;
10648 Parent_Installed : Boolean := False;
10650 Vis_Prims_List : Elist_Id := No_Elist;
10651 -- List of primitives made temporarily visible in the instantiation
10652 -- to match the visibility of the formal type
10654 procedure Check_Initialized_Types;
10655 -- In a generic package body, an entity of a generic private type may
10656 -- appear uninitialized. This is suspicious, unless the actual is a
10657 -- fully initialized type.
10659 -----------------------------
10660 -- Check_Initialized_Types --
10661 -----------------------------
10663 procedure Check_Initialized_Types is
10665 Formal : Entity_Id;
10666 Actual : Entity_Id;
10667 Uninit_Var : Entity_Id;
10670 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10671 while Present (Decl) loop
10672 Uninit_Var := Empty;
10674 if Nkind (Decl) = N_Private_Extension_Declaration then
10675 Uninit_Var := Uninitialized_Variable (Decl);
10677 elsif Nkind (Decl) = N_Formal_Type_Declaration
10678 and then Nkind (Formal_Type_Definition (Decl)) =
10679 N_Formal_Private_Type_Definition
10682 Uninitialized_Variable (Formal_Type_Definition (Decl));
10685 if Present (Uninit_Var) then
10686 Formal := Defining_Identifier (Decl);
10687 Actual := First_Entity (Act_Decl_Id);
10689 -- For each formal there is a subtype declaration that renames
10690 -- the actual and has the same name as the formal. Locate the
10691 -- formal for warning message about uninitialized variables
10692 -- in the generic, for which the actual type should be a fully
10693 -- initialized type.
10695 while Present (Actual) loop
10696 exit when Ekind (Actual) = E_Package
10697 and then Present (Renamed_Object (Actual));
10699 if Chars (Actual) = Chars (Formal)
10700 and then not Is_Scalar_Type (Actual)
10701 and then not Is_Fully_Initialized_Type (Actual)
10702 and then Warn_On_No_Value_Assigned
10704 Error_Msg_Node_2 := Formal;
10706 ("generic unit has uninitialized variable& of "
10707 & "formal private type &?v?", Actual, Uninit_Var);
10709 ("actual type for& should be fully initialized type?v?",
10714 Next_Entity (Actual);
10720 end Check_Initialized_Types;
10722 -- Start of processing for Instantiate_Package_Body
10725 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10727 -- The instance body may already have been processed, as the parent of
10728 -- another instance that is inlined (Load_Parent_Of_Generic).
10730 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10734 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10736 -- Re-establish the state of information on which checks are suppressed.
10737 -- This information was set in Body_Info at the point of instantiation,
10738 -- and now we restore it so that the instance is compiled using the
10739 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10741 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10742 Scope_Suppress := Body_Info.Scope_Suppress;
10743 Opt.Ada_Version := Body_Info.Version;
10744 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10745 Restore_Warnings (Body_Info.Warnings);
10746 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10747 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10749 if No (Gen_Body_Id) then
10751 -- Do not look for parent of generic body if none is required.
10752 -- This may happen when the routine is called as part of the
10753 -- Pending_Instantiations processing, when nested instances
10754 -- may precede the one generated from the main unit.
10756 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10757 and then Body_Optional
10761 Load_Parent_Of_Generic
10762 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10763 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10767 -- Establish global variable for sloc adjustment and for error recovery
10768 -- In the case of an instance body for an instantiation with actuals
10769 -- from a limited view, the instance body is placed at the beginning
10770 -- of the enclosing package body: use the body entity as the source
10771 -- location for nodes of the instance body.
10773 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10775 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10776 Body_Id : constant Node_Id :=
10777 Corresponding_Body (Unit_Declaration_Node (Scop));
10780 Instantiation_Node := Body_Id;
10783 Instantiation_Node := Inst_Node;
10786 if Present (Gen_Body_Id) then
10787 Save_Env (Gen_Unit, Act_Decl_Id);
10788 Style_Check := False;
10790 -- If the context of the instance is subject to SPARK_Mode "off" or
10791 -- the annotation is altogether missing, set the global flag which
10792 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10795 if SPARK_Mode /= On then
10796 Ignore_Pragma_SPARK_Mode := True;
10799 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10800 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10802 Create_Instantiation_Source
10803 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10807 (Original_Node (Gen_Body), Empty, Instantiating => True);
10809 -- Build new name (possibly qualified) for body declaration
10811 Act_Body_Id := New_Copy (Act_Decl_Id);
10813 -- Some attributes of spec entity are not inherited by body entity
10815 Set_Handler_Records (Act_Body_Id, No_List);
10817 if Nkind (Defining_Unit_Name (Act_Spec)) =
10818 N_Defining_Program_Unit_Name
10821 Make_Defining_Program_Unit_Name (Loc,
10822 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10823 Defining_Identifier => Act_Body_Id);
10825 Act_Body_Name := Act_Body_Id;
10828 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10830 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10831 Check_Generic_Actuals (Act_Decl_Id, False);
10832 Check_Initialized_Types;
10834 -- Install primitives hidden at the point of the instantiation but
10835 -- visible when processing the generic formals
10841 E := First_Entity (Act_Decl_Id);
10842 while Present (E) loop
10844 and then Is_Generic_Actual_Type (E)
10845 and then Is_Tagged_Type (E)
10847 Install_Hidden_Primitives
10848 (Prims_List => Vis_Prims_List,
10849 Gen_T => Generic_Parent_Type (Parent (E)),
10857 -- If it is a child unit, make the parent instance (which is an
10858 -- instance of the parent of the generic) visible. The parent
10859 -- instance is the prefix of the name of the generic unit.
10861 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10862 and then Nkind (Gen_Id) = N_Expanded_Name
10864 Par_Ent := Entity (Prefix (Gen_Id));
10865 Par_Vis := Is_Immediately_Visible (Par_Ent);
10866 Install_Parent (Par_Ent, In_Body => True);
10867 Parent_Installed := True;
10869 elsif Is_Child_Unit (Gen_Unit) then
10870 Par_Ent := Scope (Gen_Unit);
10871 Par_Vis := Is_Immediately_Visible (Par_Ent);
10872 Install_Parent (Par_Ent, In_Body => True);
10873 Parent_Installed := True;
10876 -- If the instantiation is a library unit, and this is the main unit,
10877 -- then build the resulting compilation unit nodes for the instance.
10878 -- If this is a compilation unit but it is not the main unit, then it
10879 -- is the body of a unit in the context, that is being compiled
10880 -- because it is encloses some inlined unit or another generic unit
10881 -- being instantiated. In that case, this body is not part of the
10882 -- current compilation, and is not attached to the tree, but its
10883 -- parent must be set for analysis.
10885 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10887 -- Replace instance node with body of instance, and create new
10888 -- node for corresponding instance declaration.
10890 Build_Instance_Compilation_Unit_Nodes
10891 (Inst_Node, Act_Body, Act_Decl);
10892 Analyze (Inst_Node);
10894 if Parent (Inst_Node) = Cunit (Main_Unit) then
10896 -- If the instance is a child unit itself, then set the scope
10897 -- of the expanded body to be the parent of the instantiation
10898 -- (ensuring that the fully qualified name will be generated
10899 -- for the elaboration subprogram).
10901 if Nkind (Defining_Unit_Name (Act_Spec)) =
10902 N_Defining_Program_Unit_Name
10904 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10908 -- Case where instantiation is not a library unit
10911 -- If this is an early instantiation, i.e. appears textually
10912 -- before the corresponding body and must be elaborated first,
10913 -- indicate that the body instance is to be delayed.
10915 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10917 -- Now analyze the body. We turn off all checks if this is an
10918 -- internal unit, since there is no reason to have checks on for
10919 -- any predefined run-time library code. All such code is designed
10920 -- to be compiled with checks off.
10922 -- Note that we do NOT apply this criterion to children of GNAT
10923 -- The latter units must suppress checks explicitly if needed.
10925 if Is_Predefined_File_Name
10926 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10928 Analyze (Act_Body, Suppress => All_Checks);
10930 Analyze (Act_Body);
10934 Inherit_Context (Gen_Body, Inst_Node);
10936 -- Remove the parent instances if they have been placed on the scope
10937 -- stack to compile the body.
10939 if Parent_Installed then
10940 Remove_Parent (In_Body => True);
10942 -- Restore the previous visibility of the parent
10944 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10947 Restore_Hidden_Primitives (Vis_Prims_List);
10948 Restore_Private_Views (Act_Decl_Id);
10950 -- Remove the current unit from visibility if this is an instance
10951 -- that is not elaborated on the fly for inlining purposes.
10953 if not Inlined_Body then
10954 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10958 Ignore_Pragma_SPARK_Mode := Save_IPSM;
10959 Style_Check := Save_Style_Check;
10961 -- If we have no body, and the unit requires a body, then complain. This
10962 -- complaint is suppressed if we have detected other errors (since a
10963 -- common reason for missing the body is that it had errors).
10964 -- In CodePeer mode, a warning has been emitted already, no need for
10965 -- further messages.
10967 elsif Unit_Requires_Body (Gen_Unit)
10968 and then not Body_Optional
10970 if CodePeer_Mode then
10973 elsif Serious_Errors_Detected = 0 then
10975 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10977 -- Don't attempt to perform any cleanup actions if some other error
10978 -- was already detected, since this can cause blowups.
10984 -- Case of package that does not need a body
10987 -- If the instantiation of the declaration is a library unit, rewrite
10988 -- the original package instantiation as a package declaration in the
10989 -- compilation unit node.
10991 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10992 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10993 Rewrite (Inst_Node, Act_Decl);
10995 -- Generate elaboration entity, in case spec has elaboration code.
10996 -- This cannot be done when the instance is analyzed, because it
10997 -- is not known yet whether the body exists.
10999 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11000 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11002 -- If the instantiation is not a library unit, then append the
11003 -- declaration to the list of implicitly generated entities, unless
11004 -- it is already a list member which means that it was already
11007 elsif not Is_List_Member (Act_Decl) then
11008 Mark_Rewrite_Insertion (Act_Decl);
11009 Insert_Before (Inst_Node, Act_Decl);
11013 Expander_Mode_Restore;
11014 end Instantiate_Package_Body;
11016 ---------------------------------
11017 -- Instantiate_Subprogram_Body --
11018 ---------------------------------
11020 procedure Instantiate_Subprogram_Body
11021 (Body_Info : Pending_Body_Info;
11022 Body_Optional : Boolean := False)
11024 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11025 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11026 Loc : constant Source_Ptr := Sloc (Inst_Node);
11027 Gen_Id : constant Node_Id := Name (Inst_Node);
11028 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11029 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11030 Anon_Id : constant Entity_Id :=
11031 Defining_Unit_Name (Specification (Act_Decl));
11032 Pack_Id : constant Entity_Id :=
11033 Defining_Unit_Name (Parent (Act_Decl));
11035 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11036 Saved_Style_Check : constant Boolean := Style_Check;
11037 Saved_Warnings : constant Warning_Record := Save_Warnings;
11039 Act_Body : Node_Id;
11040 Gen_Body : Node_Id;
11041 Gen_Body_Id : Node_Id;
11042 Pack_Body : Node_Id;
11043 Par_Ent : Entity_Id := Empty;
11044 Par_Vis : Boolean := False;
11045 Ret_Expr : Node_Id;
11047 Parent_Installed : Boolean := False;
11050 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11052 -- Subprogram body may have been created already because of an inline
11053 -- pragma, or because of multiple elaborations of the enclosing package
11054 -- when several instances of the subprogram appear in the main unit.
11056 if Present (Corresponding_Body (Act_Decl)) then
11060 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11062 -- Re-establish the state of information on which checks are suppressed.
11063 -- This information was set in Body_Info at the point of instantiation,
11064 -- and now we restore it so that the instance is compiled using the
11065 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11067 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11068 Scope_Suppress := Body_Info.Scope_Suppress;
11069 Opt.Ada_Version := Body_Info.Version;
11070 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11071 Restore_Warnings (Body_Info.Warnings);
11072 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11073 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11075 if No (Gen_Body_Id) then
11077 -- For imported generic subprogram, no body to compile, complete
11078 -- the spec entity appropriately.
11080 if Is_Imported (Gen_Unit) then
11081 Set_Is_Imported (Anon_Id);
11082 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
11083 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11084 Set_Convention (Anon_Id, Convention (Gen_Unit));
11085 Set_Has_Completion (Anon_Id);
11088 -- For other cases, compile the body
11091 Load_Parent_Of_Generic
11092 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11093 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11097 Instantiation_Node := Inst_Node;
11099 if Present (Gen_Body_Id) then
11100 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11102 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11104 -- Either body is not present, or context is non-expanding, as
11105 -- when compiling a subunit. Mark the instance as completed, and
11106 -- diagnose a missing body when needed.
11109 and then Operating_Mode = Generate_Code
11112 ("missing proper body for instantiation", Gen_Body);
11115 Set_Has_Completion (Anon_Id);
11119 Save_Env (Gen_Unit, Anon_Id);
11120 Style_Check := False;
11122 -- If the context of the instance is subject to SPARK_Mode "off" or
11123 -- the annotation is altogether missing, set the global flag which
11124 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11127 if SPARK_Mode /= On then
11128 Ignore_Pragma_SPARK_Mode := True;
11131 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11132 Create_Instantiation_Source
11140 (Original_Node (Gen_Body), Empty, Instantiating => True);
11142 -- Create proper defining name for the body, to correspond to
11143 -- the one in the spec.
11145 Set_Defining_Unit_Name (Specification (Act_Body),
11146 Make_Defining_Identifier
11147 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11148 Set_Corresponding_Spec (Act_Body, Anon_Id);
11149 Set_Has_Completion (Anon_Id);
11150 Check_Generic_Actuals (Pack_Id, False);
11152 -- Generate a reference to link the visible subprogram instance to
11153 -- the generic body, which for navigation purposes is the only
11154 -- available source for the instance.
11157 (Related_Instance (Pack_Id),
11158 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11160 -- If it is a child unit, make the parent instance (which is an
11161 -- instance of the parent of the generic) visible. The parent
11162 -- instance is the prefix of the name of the generic unit.
11164 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11165 and then Nkind (Gen_Id) = N_Expanded_Name
11167 Par_Ent := Entity (Prefix (Gen_Id));
11168 Par_Vis := Is_Immediately_Visible (Par_Ent);
11169 Install_Parent (Par_Ent, In_Body => True);
11170 Parent_Installed := True;
11172 elsif Is_Child_Unit (Gen_Unit) then
11173 Par_Ent := Scope (Gen_Unit);
11174 Par_Vis := Is_Immediately_Visible (Par_Ent);
11175 Install_Parent (Par_Ent, In_Body => True);
11176 Parent_Installed := True;
11179 -- Subprogram body is placed in the body of wrapper package,
11180 -- whose spec contains the subprogram declaration as well as
11181 -- the renaming declarations for the generic parameters.
11184 Make_Package_Body (Loc,
11185 Defining_Unit_Name => New_Copy (Pack_Id),
11186 Declarations => New_List (Act_Body));
11188 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11190 -- If the instantiation is a library unit, then build resulting
11191 -- compilation unit nodes for the instance. The declaration of
11192 -- the enclosing package is the grandparent of the subprogram
11193 -- declaration. First replace the instantiation node as the unit
11194 -- of the corresponding compilation.
11196 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11197 if Parent (Inst_Node) = Cunit (Main_Unit) then
11198 Set_Unit (Parent (Inst_Node), Inst_Node);
11199 Build_Instance_Compilation_Unit_Nodes
11200 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11201 Analyze (Inst_Node);
11203 Set_Parent (Pack_Body, Parent (Inst_Node));
11204 Analyze (Pack_Body);
11208 Insert_Before (Inst_Node, Pack_Body);
11209 Mark_Rewrite_Insertion (Pack_Body);
11210 Analyze (Pack_Body);
11212 if Expander_Active then
11213 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11217 Inherit_Context (Gen_Body, Inst_Node);
11219 Restore_Private_Views (Pack_Id, False);
11221 if Parent_Installed then
11222 Remove_Parent (In_Body => True);
11224 -- Restore the previous visibility of the parent
11226 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11230 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11231 Style_Check := Saved_Style_Check;
11232 Restore_Warnings (Saved_Warnings);
11234 -- Body not found. Error was emitted already. If there were no previous
11235 -- errors, this may be an instance whose scope is a premature instance.
11236 -- In that case we must insure that the (legal) program does raise
11237 -- program error if executed. We generate a subprogram body for this
11238 -- purpose. See DEC ac30vso.
11240 -- Should not reference proprietary DEC tests in comments ???
11242 elsif Serious_Errors_Detected = 0
11243 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11245 if Body_Optional then
11248 elsif Ekind (Anon_Id) = E_Procedure then
11250 Make_Subprogram_Body (Loc,
11252 Make_Procedure_Specification (Loc,
11253 Defining_Unit_Name =>
11254 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11255 Parameter_Specifications =>
11257 (Parameter_Specifications (Parent (Anon_Id)))),
11259 Declarations => Empty_List,
11260 Handled_Statement_Sequence =>
11261 Make_Handled_Sequence_Of_Statements (Loc,
11264 Make_Raise_Program_Error (Loc,
11266 PE_Access_Before_Elaboration))));
11270 Make_Raise_Program_Error (Loc,
11271 Reason => PE_Access_Before_Elaboration);
11273 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11274 Set_Analyzed (Ret_Expr);
11277 Make_Subprogram_Body (Loc,
11279 Make_Function_Specification (Loc,
11280 Defining_Unit_Name =>
11281 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11282 Parameter_Specifications =>
11284 (Parameter_Specifications (Parent (Anon_Id))),
11285 Result_Definition =>
11286 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11288 Declarations => Empty_List,
11289 Handled_Statement_Sequence =>
11290 Make_Handled_Sequence_Of_Statements (Loc,
11293 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11297 Make_Package_Body (Loc,
11298 Defining_Unit_Name => New_Copy (Pack_Id),
11299 Declarations => New_List (Act_Body));
11301 Insert_After (Inst_Node, Pack_Body);
11302 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11303 Analyze (Pack_Body);
11306 Expander_Mode_Restore;
11307 end Instantiate_Subprogram_Body;
11309 ----------------------
11310 -- Instantiate_Type --
11311 ----------------------
11313 function Instantiate_Type
11316 Analyzed_Formal : Node_Id;
11317 Actual_Decls : List_Id) return List_Id
11319 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11320 A_Gen_T : constant Entity_Id :=
11321 Defining_Identifier (Analyzed_Formal);
11322 Ancestor : Entity_Id := Empty;
11323 Def : constant Node_Id := Formal_Type_Definition (Formal);
11325 Decl_Node : Node_Id;
11326 Decl_Nodes : List_Id;
11330 procedure Diagnose_Predicated_Actual;
11331 -- There are a number of constructs in which a discrete type with
11332 -- predicates is illegal, e.g. as an index in an array type declaration.
11333 -- If a generic type is used is such a construct in a generic package
11334 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11335 -- of the generic contract that the actual cannot have predicates.
11337 procedure Validate_Array_Type_Instance;
11338 procedure Validate_Access_Subprogram_Instance;
11339 procedure Validate_Access_Type_Instance;
11340 procedure Validate_Derived_Type_Instance;
11341 procedure Validate_Derived_Interface_Type_Instance;
11342 procedure Validate_Discriminated_Formal_Type;
11343 procedure Validate_Interface_Type_Instance;
11344 procedure Validate_Private_Type_Instance;
11345 procedure Validate_Incomplete_Type_Instance;
11346 -- These procedures perform validation tests for the named case.
11347 -- Validate_Discriminated_Formal_Type is shared by formal private
11348 -- types and Ada 2012 formal incomplete types.
11350 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11351 -- Check that base types are the same and that the subtypes match
11352 -- statically. Used in several of the above.
11354 ---------------------------------
11355 -- Diagnose_Predicated_Actual --
11356 ---------------------------------
11358 procedure Diagnose_Predicated_Actual is
11360 if No_Predicate_On_Actual (A_Gen_T)
11361 and then Has_Predicates (Act_T)
11364 ("actual for& cannot be a type with predicate",
11365 Instantiation_Node, A_Gen_T);
11367 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11368 and then Has_Predicates (Act_T)
11369 and then not Has_Static_Predicate_Aspect (Act_T)
11372 ("actual for& cannot be a type with a dynamic predicate",
11373 Instantiation_Node, A_Gen_T);
11375 end Diagnose_Predicated_Actual;
11377 --------------------
11378 -- Subtypes_Match --
11379 --------------------
11381 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11382 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11385 -- Some detailed comments would be useful here ???
11387 return ((Base_Type (T) = Act_T
11388 or else Base_Type (T) = Base_Type (Act_T))
11389 and then Subtypes_Statically_Match (T, Act_T))
11391 or else (Is_Class_Wide_Type (Gen_T)
11392 and then Is_Class_Wide_Type (Act_T)
11393 and then Subtypes_Match
11394 (Get_Instance_Of (Root_Type (Gen_T)),
11395 Root_Type (Act_T)))
11398 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11399 E_Anonymous_Access_Type)
11400 and then Ekind (Act_T) = Ekind (Gen_T)
11401 and then Subtypes_Statically_Match
11402 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11403 end Subtypes_Match;
11405 -----------------------------------------
11406 -- Validate_Access_Subprogram_Instance --
11407 -----------------------------------------
11409 procedure Validate_Access_Subprogram_Instance is
11411 if not Is_Access_Type (Act_T)
11412 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11415 ("expect access type in instantiation of &", Actual, Gen_T);
11416 Abandon_Instantiation (Actual);
11419 -- According to AI05-288, actuals for access_to_subprograms must be
11420 -- subtype conformant with the generic formal. Previous to AI05-288
11421 -- only mode conformance was required.
11423 -- This is a binding interpretation that applies to previous versions
11424 -- of the language, no need to maintain previous weaker checks.
11426 Check_Subtype_Conformant
11427 (Designated_Type (Act_T),
11428 Designated_Type (A_Gen_T),
11432 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11433 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11435 ("protected access type not allowed for formal &",
11439 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11441 ("expect protected access type for formal &",
11445 -- If the formal has a specified convention (which in most cases
11446 -- will be StdCall) verify that the actual has the same convention.
11448 if Has_Convention_Pragma (A_Gen_T)
11449 and then Convention (A_Gen_T) /= Convention (Act_T)
11451 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11453 ("actual for formal & must have convention %", Actual, Gen_T);
11455 end Validate_Access_Subprogram_Instance;
11457 -----------------------------------
11458 -- Validate_Access_Type_Instance --
11459 -----------------------------------
11461 procedure Validate_Access_Type_Instance is
11462 Desig_Type : constant Entity_Id :=
11463 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11464 Desig_Act : Entity_Id;
11467 if not Is_Access_Type (Act_T) then
11469 ("expect access type in instantiation of &", Actual, Gen_T);
11470 Abandon_Instantiation (Actual);
11473 if Is_Access_Constant (A_Gen_T) then
11474 if not Is_Access_Constant (Act_T) then
11476 ("actual type must be access-to-constant type", Actual);
11477 Abandon_Instantiation (Actual);
11480 if Is_Access_Constant (Act_T) then
11482 ("actual type must be access-to-variable type", Actual);
11483 Abandon_Instantiation (Actual);
11485 elsif Ekind (A_Gen_T) = E_General_Access_Type
11486 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11488 Error_Msg_N -- CODEFIX
11489 ("actual must be general access type!", Actual);
11490 Error_Msg_NE -- CODEFIX
11491 ("add ALL to }!", Actual, Act_T);
11492 Abandon_Instantiation (Actual);
11496 -- The designated subtypes, that is to say the subtypes introduced
11497 -- by an access type declaration (and not by a subtype declaration)
11500 Desig_Act := Designated_Type (Base_Type (Act_T));
11502 -- The designated type may have been introduced through a limited_
11503 -- with clause, in which case retrieve the non-limited view. This
11504 -- applies to incomplete types as well as to class-wide types.
11506 if From_Limited_With (Desig_Act) then
11507 Desig_Act := Available_View (Desig_Act);
11510 if not Subtypes_Match (Desig_Type, Desig_Act) then
11512 ("designated type of actual does not match that of formal &",
11515 if not Predicates_Match (Desig_Type, Desig_Act) then
11516 Error_Msg_N ("\predicates do not match", Actual);
11519 Abandon_Instantiation (Actual);
11521 elsif Is_Access_Type (Designated_Type (Act_T))
11522 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11524 Is_Constrained (Designated_Type (Desig_Type))
11527 ("designated type of actual does not match that of formal &",
11530 if not Predicates_Match (Desig_Type, Desig_Act) then
11531 Error_Msg_N ("\predicates do not match", Actual);
11534 Abandon_Instantiation (Actual);
11537 -- Ada 2005: null-exclusion indicators of the two types must agree
11539 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11541 ("non null exclusion of actual and formal & do not match",
11544 end Validate_Access_Type_Instance;
11546 ----------------------------------
11547 -- Validate_Array_Type_Instance --
11548 ----------------------------------
11550 procedure Validate_Array_Type_Instance is
11555 function Formal_Dimensions return Int;
11556 -- Count number of dimensions in array type formal
11558 -----------------------
11559 -- Formal_Dimensions --
11560 -----------------------
11562 function Formal_Dimensions return Int is
11567 if Nkind (Def) = N_Constrained_Array_Definition then
11568 Index := First (Discrete_Subtype_Definitions (Def));
11570 Index := First (Subtype_Marks (Def));
11573 while Present (Index) loop
11575 Next_Index (Index);
11579 end Formal_Dimensions;
11581 -- Start of processing for Validate_Array_Type_Instance
11584 if not Is_Array_Type (Act_T) then
11586 ("expect array type in instantiation of &", Actual, Gen_T);
11587 Abandon_Instantiation (Actual);
11589 elsif Nkind (Def) = N_Constrained_Array_Definition then
11590 if not (Is_Constrained (Act_T)) then
11592 ("expect constrained array in instantiation of &",
11594 Abandon_Instantiation (Actual);
11598 if Is_Constrained (Act_T) then
11600 ("expect unconstrained array in instantiation of &",
11602 Abandon_Instantiation (Actual);
11606 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11608 ("dimensions of actual do not match formal &", Actual, Gen_T);
11609 Abandon_Instantiation (Actual);
11612 I1 := First_Index (A_Gen_T);
11613 I2 := First_Index (Act_T);
11614 for J in 1 .. Formal_Dimensions loop
11616 -- If the indexes of the actual were given by a subtype_mark,
11617 -- the index was transformed into a range attribute. Retrieve
11618 -- the original type mark for checking.
11620 if Is_Entity_Name (Original_Node (I2)) then
11621 T2 := Entity (Original_Node (I2));
11626 if not Subtypes_Match
11627 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11630 ("index types of actual do not match those of formal &",
11632 Abandon_Instantiation (Actual);
11639 -- Check matching subtypes. Note that there are complex visibility
11640 -- issues when the generic is a child unit and some aspect of the
11641 -- generic type is declared in a parent unit of the generic. We do
11642 -- the test to handle this special case only after a direct check
11643 -- for static matching has failed. The case where both the component
11644 -- type and the array type are separate formals, and the component
11645 -- type is a private view may also require special checking in
11649 (Component_Type (A_Gen_T), Component_Type (Act_T))
11652 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11653 Component_Type (Act_T))
11658 ("component subtype of actual does not match that of formal &",
11660 Abandon_Instantiation (Actual);
11663 if Has_Aliased_Components (A_Gen_T)
11664 and then not Has_Aliased_Components (Act_T)
11667 ("actual must have aliased components to match formal type &",
11670 end Validate_Array_Type_Instance;
11672 -----------------------------------------------
11673 -- Validate_Derived_Interface_Type_Instance --
11674 -----------------------------------------------
11676 procedure Validate_Derived_Interface_Type_Instance is
11677 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11681 -- First apply interface instance checks
11683 Validate_Interface_Type_Instance;
11685 -- Verify that immediate parent interface is an ancestor of
11689 and then not Interface_Present_In_Ancestor (Act_T, Par)
11692 ("interface actual must include progenitor&", Actual, Par);
11695 -- Now verify that the actual includes all other ancestors of
11698 Elmt := First_Elmt (Interfaces (A_Gen_T));
11699 while Present (Elmt) loop
11700 if not Interface_Present_In_Ancestor
11701 (Act_T, Get_Instance_Of (Node (Elmt)))
11704 ("interface actual must include progenitor&",
11705 Actual, Node (Elmt));
11710 end Validate_Derived_Interface_Type_Instance;
11712 ------------------------------------
11713 -- Validate_Derived_Type_Instance --
11714 ------------------------------------
11716 procedure Validate_Derived_Type_Instance is
11717 Actual_Discr : Entity_Id;
11718 Ancestor_Discr : Entity_Id;
11721 -- If the parent type in the generic declaration is itself a previous
11722 -- formal type, then it is local to the generic and absent from the
11723 -- analyzed generic definition. In that case the ancestor is the
11724 -- instance of the formal (which must have been instantiated
11725 -- previously), unless the ancestor is itself a formal derived type.
11726 -- In this latter case (which is the subject of Corrigendum 8652/0038
11727 -- (AI-202) the ancestor of the formals is the ancestor of its
11728 -- parent. Otherwise, the analyzed generic carries the parent type.
11729 -- If the parent type is defined in a previous formal package, then
11730 -- the scope of that formal package is that of the generic type
11731 -- itself, and it has already been mapped into the corresponding type
11732 -- in the actual package.
11734 -- Common case: parent type defined outside of the generic
11736 if Is_Entity_Name (Subtype_Mark (Def))
11737 and then Present (Entity (Subtype_Mark (Def)))
11739 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11741 -- Check whether parent is defined in a previous formal package
11744 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11747 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11749 -- The type may be a local derivation, or a type extension of a
11750 -- previous formal, or of a formal of a parent package.
11752 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11754 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11756 -- Check whether the parent is another derived formal type in the
11757 -- same generic unit.
11759 if Etype (A_Gen_T) /= A_Gen_T
11760 and then Is_Generic_Type (Etype (A_Gen_T))
11761 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11762 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11764 -- Locate ancestor of parent from the subtype declaration
11765 -- created for the actual.
11771 Decl := First (Actual_Decls);
11772 while Present (Decl) loop
11773 if Nkind (Decl) = N_Subtype_Declaration
11774 and then Chars (Defining_Identifier (Decl)) =
11775 Chars (Etype (A_Gen_T))
11777 Ancestor := Generic_Parent_Type (Decl);
11785 pragma Assert (Present (Ancestor));
11787 -- The ancestor itself may be a previous formal that has been
11790 Ancestor := Get_Instance_Of (Ancestor);
11794 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11797 -- Check whether parent is a previous formal of the current generic
11799 elsif Is_Derived_Type (A_Gen_T)
11800 and then Is_Generic_Type (Etype (A_Gen_T))
11801 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11803 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11805 -- An unusual case: the actual is a type declared in a parent unit,
11806 -- but is not a formal type so there is no instance_of for it.
11807 -- Retrieve it by analyzing the record extension.
11809 elsif Is_Child_Unit (Scope (A_Gen_T))
11810 and then In_Open_Scopes (Scope (Act_T))
11811 and then Is_Generic_Instance (Scope (Act_T))
11813 Analyze (Subtype_Mark (Def));
11814 Ancestor := Entity (Subtype_Mark (Def));
11817 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11820 -- If the formal derived type has pragma Preelaborable_Initialization
11821 -- then the actual type must have preelaborable initialization.
11823 if Known_To_Have_Preelab_Init (A_Gen_T)
11824 and then not Has_Preelaborable_Initialization (Act_T)
11827 ("actual for & must have preelaborable initialization",
11831 -- Ada 2005 (AI-251)
11833 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11834 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11836 ("(Ada 2005) expected type implementing & in instantiation",
11840 -- Finally verify that the (instance of) the ancestor is an ancestor
11843 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11845 ("expect type derived from & in instantiation",
11846 Actual, First_Subtype (Ancestor));
11847 Abandon_Instantiation (Actual);
11850 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11851 -- that the formal type declaration has been rewritten as a private
11854 if Ada_Version >= Ada_2005
11855 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11856 and then Synchronized_Present (Parent (A_Gen_T))
11858 -- The actual must be a synchronized tagged type
11860 if not Is_Tagged_Type (Act_T) then
11862 ("actual of synchronized type must be tagged", Actual);
11863 Abandon_Instantiation (Actual);
11865 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11866 and then Nkind (Type_Definition (Parent (Act_T))) =
11867 N_Derived_Type_Definition
11868 and then not Synchronized_Present
11869 (Type_Definition (Parent (Act_T)))
11872 ("actual of synchronized type must be synchronized", Actual);
11873 Abandon_Instantiation (Actual);
11877 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11878 -- removes the second instance of the phrase "or allow pass by copy".
11880 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11882 ("cannot have atomic actual type for non-atomic formal type",
11885 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11887 ("cannot have volatile actual type for non-volatile formal type",
11891 -- It should not be necessary to check for unknown discriminants on
11892 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11893 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11894 -- needs fixing. ???
11896 if Is_Definite_Subtype (A_Gen_T)
11897 and then not Unknown_Discriminants_Present (Formal)
11898 and then not Is_Definite_Subtype (Act_T)
11900 Error_Msg_N ("actual subtype must be constrained", Actual);
11901 Abandon_Instantiation (Actual);
11904 if not Unknown_Discriminants_Present (Formal) then
11905 if Is_Constrained (Ancestor) then
11906 if not Is_Constrained (Act_T) then
11907 Error_Msg_N ("actual subtype must be constrained", Actual);
11908 Abandon_Instantiation (Actual);
11911 -- Ancestor is unconstrained, Check if generic formal and actual
11912 -- agree on constrainedness. The check only applies to array types
11913 -- and discriminated types.
11915 elsif Is_Constrained (Act_T) then
11916 if Ekind (Ancestor) = E_Access_Type
11917 or else (not Is_Constrained (A_Gen_T)
11918 and then Is_Composite_Type (A_Gen_T))
11920 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11921 Abandon_Instantiation (Actual);
11924 -- A class-wide type is only allowed if the formal has unknown
11927 elsif Is_Class_Wide_Type (Act_T)
11928 and then not Has_Unknown_Discriminants (Ancestor)
11931 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11932 Abandon_Instantiation (Actual);
11934 -- Otherwise, the formal and actual must have the same number
11935 -- of discriminants and each discriminant of the actual must
11936 -- correspond to a discriminant of the formal.
11938 elsif Has_Discriminants (Act_T)
11939 and then not Has_Unknown_Discriminants (Act_T)
11940 and then Has_Discriminants (Ancestor)
11942 Actual_Discr := First_Discriminant (Act_T);
11943 Ancestor_Discr := First_Discriminant (Ancestor);
11944 while Present (Actual_Discr)
11945 and then Present (Ancestor_Discr)
11947 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11948 No (Corresponding_Discriminant (Actual_Discr))
11951 ("discriminant & does not correspond "
11952 & "to ancestor discriminant", Actual, Actual_Discr);
11953 Abandon_Instantiation (Actual);
11956 Next_Discriminant (Actual_Discr);
11957 Next_Discriminant (Ancestor_Discr);
11960 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11962 ("actual for & must have same number of discriminants",
11964 Abandon_Instantiation (Actual);
11967 -- This case should be caught by the earlier check for
11968 -- constrainedness, but the check here is added for completeness.
11970 elsif Has_Discriminants (Act_T)
11971 and then not Has_Unknown_Discriminants (Act_T)
11974 ("actual for & must not have discriminants", Actual, Gen_T);
11975 Abandon_Instantiation (Actual);
11977 elsif Has_Discriminants (Ancestor) then
11979 ("actual for & must have known discriminants", Actual, Gen_T);
11980 Abandon_Instantiation (Actual);
11983 if not Subtypes_Statically_Compatible
11984 (Act_T, Ancestor, Formal_Derived_Matching => True)
11987 ("constraint on actual is incompatible with formal", Actual);
11988 Abandon_Instantiation (Actual);
11992 -- If the formal and actual types are abstract, check that there
11993 -- are no abstract primitives of the actual type that correspond to
11994 -- nonabstract primitives of the formal type (second sentence of
11997 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11998 Check_Abstract_Primitives : declare
11999 Gen_Prims : constant Elist_Id :=
12000 Primitive_Operations (A_Gen_T);
12001 Gen_Elmt : Elmt_Id;
12002 Gen_Subp : Entity_Id;
12003 Anc_Subp : Entity_Id;
12004 Anc_Formal : Entity_Id;
12005 Anc_F_Type : Entity_Id;
12007 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12008 Act_Elmt : Elmt_Id;
12009 Act_Subp : Entity_Id;
12010 Act_Formal : Entity_Id;
12011 Act_F_Type : Entity_Id;
12013 Subprograms_Correspond : Boolean;
12015 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12016 -- Returns true if T2 is derived directly or indirectly from
12017 -- T1, including derivations from interfaces. T1 and T2 are
12018 -- required to be specific tagged base types.
12020 ------------------------
12021 -- Is_Tagged_Ancestor --
12022 ------------------------
12024 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12026 Intfc_Elmt : Elmt_Id;
12029 -- The predicate is satisfied if the types are the same
12034 -- If we've reached the top of the derivation chain then
12035 -- we know that T1 is not an ancestor of T2.
12037 elsif Etype (T2) = T2 then
12040 -- Proceed to check T2's immediate parent
12042 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12045 -- Finally, check to see if T1 is an ancestor of any of T2's
12049 Intfc_Elmt := First_Elmt (Interfaces (T2));
12050 while Present (Intfc_Elmt) loop
12051 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12055 Next_Elmt (Intfc_Elmt);
12060 end Is_Tagged_Ancestor;
12062 -- Start of processing for Check_Abstract_Primitives
12065 -- Loop over all of the formal derived type's primitives
12067 Gen_Elmt := First_Elmt (Gen_Prims);
12068 while Present (Gen_Elmt) loop
12069 Gen_Subp := Node (Gen_Elmt);
12071 -- If the primitive of the formal is not abstract, then
12072 -- determine whether there is a corresponding primitive of
12073 -- the actual type that's abstract.
12075 if not Is_Abstract_Subprogram (Gen_Subp) then
12076 Act_Elmt := First_Elmt (Act_Prims);
12077 while Present (Act_Elmt) loop
12078 Act_Subp := Node (Act_Elmt);
12080 -- If we find an abstract primitive of the actual,
12081 -- then we need to test whether it corresponds to the
12082 -- subprogram from which the generic formal primitive
12085 if Is_Abstract_Subprogram (Act_Subp) then
12086 Anc_Subp := Alias (Gen_Subp);
12088 -- Test whether we have a corresponding primitive
12089 -- by comparing names, kinds, formal types, and
12092 if Chars (Anc_Subp) = Chars (Act_Subp)
12093 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12095 Anc_Formal := First_Formal (Anc_Subp);
12096 Act_Formal := First_Formal (Act_Subp);
12097 while Present (Anc_Formal)
12098 and then Present (Act_Formal)
12100 Anc_F_Type := Etype (Anc_Formal);
12101 Act_F_Type := Etype (Act_Formal);
12103 if Ekind (Anc_F_Type) =
12104 E_Anonymous_Access_Type
12106 Anc_F_Type := Designated_Type (Anc_F_Type);
12108 if Ekind (Act_F_Type) =
12109 E_Anonymous_Access_Type
12112 Designated_Type (Act_F_Type);
12118 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12123 Anc_F_Type := Base_Type (Anc_F_Type);
12124 Act_F_Type := Base_Type (Act_F_Type);
12126 -- If the formal is controlling, then the
12127 -- the type of the actual primitive's formal
12128 -- must be derived directly or indirectly
12129 -- from the type of the ancestor primitive's
12132 if Is_Controlling_Formal (Anc_Formal) then
12133 if not Is_Tagged_Ancestor
12134 (Anc_F_Type, Act_F_Type)
12139 -- Otherwise the types of the formals must
12142 elsif Anc_F_Type /= Act_F_Type then
12146 Next_Entity (Anc_Formal);
12147 Next_Entity (Act_Formal);
12150 -- If we traversed through all of the formals
12151 -- then so far the subprograms correspond, so
12152 -- now check that any result types correspond.
12154 if No (Anc_Formal) and then No (Act_Formal) then
12155 Subprograms_Correspond := True;
12157 if Ekind (Act_Subp) = E_Function then
12158 Anc_F_Type := Etype (Anc_Subp);
12159 Act_F_Type := Etype (Act_Subp);
12161 if Ekind (Anc_F_Type) =
12162 E_Anonymous_Access_Type
12165 Designated_Type (Anc_F_Type);
12167 if Ekind (Act_F_Type) =
12168 E_Anonymous_Access_Type
12171 Designated_Type (Act_F_Type);
12173 Subprograms_Correspond := False;
12178 = E_Anonymous_Access_Type
12180 Subprograms_Correspond := False;
12183 Anc_F_Type := Base_Type (Anc_F_Type);
12184 Act_F_Type := Base_Type (Act_F_Type);
12186 -- Now either the result types must be
12187 -- the same or, if the result type is
12188 -- controlling, the result type of the
12189 -- actual primitive must descend from the
12190 -- result type of the ancestor primitive.
12192 if Subprograms_Correspond
12193 and then Anc_F_Type /= Act_F_Type
12195 Has_Controlling_Result (Anc_Subp)
12196 and then not Is_Tagged_Ancestor
12197 (Anc_F_Type, Act_F_Type)
12199 Subprograms_Correspond := False;
12203 -- Found a matching subprogram belonging to
12204 -- formal ancestor type, so actual subprogram
12205 -- corresponds and this violates 3.9.3(9).
12207 if Subprograms_Correspond then
12209 ("abstract subprogram & overrides "
12210 & "nonabstract subprogram of ancestor",
12217 Next_Elmt (Act_Elmt);
12221 Next_Elmt (Gen_Elmt);
12223 end Check_Abstract_Primitives;
12226 -- Verify that limitedness matches. If parent is a limited
12227 -- interface then the generic formal is not unless declared
12228 -- explicitly so. If not declared limited, the actual cannot be
12229 -- limited (see AI05-0087).
12231 -- Even though this AI is a binding interpretation, we enable the
12232 -- check only in Ada 2012 mode, because this improper construct
12233 -- shows up in user code and in existing B-tests.
12235 if Is_Limited_Type (Act_T)
12236 and then not Is_Limited_Type (A_Gen_T)
12237 and then Ada_Version >= Ada_2012
12239 if In_Instance then
12243 ("actual for non-limited & cannot be a limited type",
12245 Explain_Limited_Type (Act_T, Actual);
12246 Abandon_Instantiation (Actual);
12249 end Validate_Derived_Type_Instance;
12251 ----------------------------------------
12252 -- Validate_Discriminated_Formal_Type --
12253 ----------------------------------------
12255 procedure Validate_Discriminated_Formal_Type is
12256 Formal_Discr : Entity_Id;
12257 Actual_Discr : Entity_Id;
12258 Formal_Subt : Entity_Id;
12261 if Has_Discriminants (A_Gen_T) then
12262 if not Has_Discriminants (Act_T) then
12264 ("actual for & must have discriminants", Actual, Gen_T);
12265 Abandon_Instantiation (Actual);
12267 elsif Is_Constrained (Act_T) then
12269 ("actual for & must be unconstrained", Actual, Gen_T);
12270 Abandon_Instantiation (Actual);
12273 Formal_Discr := First_Discriminant (A_Gen_T);
12274 Actual_Discr := First_Discriminant (Act_T);
12275 while Formal_Discr /= Empty loop
12276 if Actual_Discr = Empty then
12278 ("discriminants on actual do not match formal",
12280 Abandon_Instantiation (Actual);
12283 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12285 -- Access discriminants match if designated types do
12287 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12288 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12289 E_Anonymous_Access_Type
12292 (Designated_Type (Base_Type (Formal_Subt))) =
12293 Designated_Type (Base_Type (Etype (Actual_Discr)))
12297 elsif Base_Type (Formal_Subt) /=
12298 Base_Type (Etype (Actual_Discr))
12301 ("types of actual discriminants must match formal",
12303 Abandon_Instantiation (Actual);
12305 elsif not Subtypes_Statically_Match
12306 (Formal_Subt, Etype (Actual_Discr))
12307 and then Ada_Version >= Ada_95
12310 ("subtypes of actual discriminants must match formal",
12312 Abandon_Instantiation (Actual);
12315 Next_Discriminant (Formal_Discr);
12316 Next_Discriminant (Actual_Discr);
12319 if Actual_Discr /= Empty then
12321 ("discriminants on actual do not match formal",
12323 Abandon_Instantiation (Actual);
12327 end Validate_Discriminated_Formal_Type;
12329 ---------------------------------------
12330 -- Validate_Incomplete_Type_Instance --
12331 ---------------------------------------
12333 procedure Validate_Incomplete_Type_Instance is
12335 if not Is_Tagged_Type (Act_T)
12336 and then Is_Tagged_Type (A_Gen_T)
12339 ("actual for & must be a tagged type", Actual, Gen_T);
12342 Validate_Discriminated_Formal_Type;
12343 end Validate_Incomplete_Type_Instance;
12345 --------------------------------------
12346 -- Validate_Interface_Type_Instance --
12347 --------------------------------------
12349 procedure Validate_Interface_Type_Instance is
12351 if not Is_Interface (Act_T) then
12353 ("actual for formal interface type must be an interface",
12356 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12357 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12358 or else Is_Protected_Interface (A_Gen_T) /=
12359 Is_Protected_Interface (Act_T)
12360 or else Is_Synchronized_Interface (A_Gen_T) /=
12361 Is_Synchronized_Interface (Act_T)
12364 ("actual for interface& does not match (RM 12.5.5(4))",
12367 end Validate_Interface_Type_Instance;
12369 ------------------------------------
12370 -- Validate_Private_Type_Instance --
12371 ------------------------------------
12373 procedure Validate_Private_Type_Instance is
12375 if Is_Limited_Type (Act_T)
12376 and then not Is_Limited_Type (A_Gen_T)
12378 if In_Instance then
12382 ("actual for non-limited & cannot be a limited type", Actual,
12384 Explain_Limited_Type (Act_T, Actual);
12385 Abandon_Instantiation (Actual);
12388 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12389 and then not Has_Preelaborable_Initialization (Act_T)
12392 ("actual for & must have preelaborable initialization", Actual,
12395 elsif not Is_Definite_Subtype (Act_T)
12396 and then Is_Definite_Subtype (A_Gen_T)
12397 and then Ada_Version >= Ada_95
12400 ("actual for & must be a definite subtype", Actual, Gen_T);
12402 elsif not Is_Tagged_Type (Act_T)
12403 and then Is_Tagged_Type (A_Gen_T)
12406 ("actual for & must be a tagged type", Actual, Gen_T);
12409 Validate_Discriminated_Formal_Type;
12411 end Validate_Private_Type_Instance;
12413 -- Start of processing for Instantiate_Type
12416 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12417 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12418 return New_List (Error);
12420 elsif not Is_Entity_Name (Actual)
12421 or else not Is_Type (Entity (Actual))
12424 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12425 Abandon_Instantiation (Actual);
12428 Act_T := Entity (Actual);
12430 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12431 -- as a generic actual parameter if the corresponding formal type
12432 -- does not have a known_discriminant_part, or is a formal derived
12433 -- type that is an Unchecked_Union type.
12435 if Is_Unchecked_Union (Base_Type (Act_T)) then
12436 if not Has_Discriminants (A_Gen_T)
12437 or else (Is_Derived_Type (A_Gen_T)
12438 and then Is_Unchecked_Union (A_Gen_T))
12442 Error_Msg_N ("unchecked union cannot be the actual for a "
12443 & "discriminated formal type", Act_T);
12448 -- Deal with fixed/floating restrictions
12450 if Is_Floating_Point_Type (Act_T) then
12451 Check_Restriction (No_Floating_Point, Actual);
12452 elsif Is_Fixed_Point_Type (Act_T) then
12453 Check_Restriction (No_Fixed_Point, Actual);
12456 -- Deal with error of using incomplete type as generic actual.
12457 -- This includes limited views of a type, even if the non-limited
12458 -- view may be available.
12460 if Ekind (Act_T) = E_Incomplete_Type
12461 or else (Is_Class_Wide_Type (Act_T)
12462 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12464 -- If the formal is an incomplete type, the actual can be
12465 -- incomplete as well.
12467 if Ekind (A_Gen_T) = E_Incomplete_Type then
12470 elsif Is_Class_Wide_Type (Act_T)
12471 or else No (Full_View (Act_T))
12473 Error_Msg_N ("premature use of incomplete type", Actual);
12474 Abandon_Instantiation (Actual);
12476 Act_T := Full_View (Act_T);
12477 Set_Entity (Actual, Act_T);
12479 if Has_Private_Component (Act_T) then
12481 ("premature use of type with private component", Actual);
12485 -- Deal with error of premature use of private type as generic actual
12487 elsif Is_Private_Type (Act_T)
12488 and then Is_Private_Type (Base_Type (Act_T))
12489 and then not Is_Generic_Type (Act_T)
12490 and then not Is_Derived_Type (Act_T)
12491 and then No (Full_View (Root_Type (Act_T)))
12493 -- If the formal is an incomplete type, the actual can be
12494 -- private or incomplete as well.
12496 if Ekind (A_Gen_T) = E_Incomplete_Type then
12499 Error_Msg_N ("premature use of private type", Actual);
12502 elsif Has_Private_Component (Act_T) then
12504 ("premature use of type with private component", Actual);
12507 Set_Instance_Of (A_Gen_T, Act_T);
12509 -- If the type is generic, the class-wide type may also be used
12511 if Is_Tagged_Type (A_Gen_T)
12512 and then Is_Tagged_Type (Act_T)
12513 and then not Is_Class_Wide_Type (A_Gen_T)
12515 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12516 Class_Wide_Type (Act_T));
12519 if not Is_Abstract_Type (A_Gen_T)
12520 and then Is_Abstract_Type (Act_T)
12523 ("actual of non-abstract formal cannot be abstract", Actual);
12526 -- A generic scalar type is a first subtype for which we generate
12527 -- an anonymous base type. Indicate that the instance of this base
12528 -- is the base type of the actual.
12530 if Is_Scalar_Type (A_Gen_T) then
12531 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12535 if Error_Posted (Act_T) then
12538 case Nkind (Def) is
12539 when N_Formal_Private_Type_Definition =>
12540 Validate_Private_Type_Instance;
12542 when N_Formal_Incomplete_Type_Definition =>
12543 Validate_Incomplete_Type_Instance;
12545 when N_Formal_Derived_Type_Definition =>
12546 Validate_Derived_Type_Instance;
12548 when N_Formal_Discrete_Type_Definition =>
12549 if not Is_Discrete_Type (Act_T) then
12551 ("expect discrete type in instantiation of&",
12553 Abandon_Instantiation (Actual);
12556 Diagnose_Predicated_Actual;
12558 when N_Formal_Signed_Integer_Type_Definition =>
12559 if not Is_Signed_Integer_Type (Act_T) then
12561 ("expect signed integer type in instantiation of&",
12563 Abandon_Instantiation (Actual);
12566 Diagnose_Predicated_Actual;
12568 when N_Formal_Modular_Type_Definition =>
12569 if not Is_Modular_Integer_Type (Act_T) then
12571 ("expect modular type in instantiation of &",
12573 Abandon_Instantiation (Actual);
12576 Diagnose_Predicated_Actual;
12578 when N_Formal_Floating_Point_Definition =>
12579 if not Is_Floating_Point_Type (Act_T) then
12581 ("expect float type in instantiation of &", Actual, Gen_T);
12582 Abandon_Instantiation (Actual);
12585 when N_Formal_Ordinary_Fixed_Point_Definition =>
12586 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12588 ("expect ordinary fixed point type in instantiation of &",
12590 Abandon_Instantiation (Actual);
12593 when N_Formal_Decimal_Fixed_Point_Definition =>
12594 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12596 ("expect decimal type in instantiation of &",
12598 Abandon_Instantiation (Actual);
12601 when N_Array_Type_Definition =>
12602 Validate_Array_Type_Instance;
12604 when N_Access_To_Object_Definition =>
12605 Validate_Access_Type_Instance;
12607 when N_Access_Function_Definition |
12608 N_Access_Procedure_Definition =>
12609 Validate_Access_Subprogram_Instance;
12611 when N_Record_Definition =>
12612 Validate_Interface_Type_Instance;
12614 when N_Derived_Type_Definition =>
12615 Validate_Derived_Interface_Type_Instance;
12618 raise Program_Error;
12623 Subt := New_Copy (Gen_T);
12625 -- Use adjusted sloc of subtype name as the location for other nodes in
12626 -- the subtype declaration.
12628 Loc := Sloc (Subt);
12631 Make_Subtype_Declaration (Loc,
12632 Defining_Identifier => Subt,
12633 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12635 if Is_Private_Type (Act_T) then
12636 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12638 elsif Is_Access_Type (Act_T)
12639 and then Is_Private_Type (Designated_Type (Act_T))
12641 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12644 -- In Ada 2012 the actual may be a limited view. Indicate that
12645 -- the local subtype must be treated as such.
12647 if From_Limited_With (Act_T) then
12648 Set_Ekind (Subt, E_Incomplete_Subtype);
12649 Set_From_Limited_With (Subt);
12652 Decl_Nodes := New_List (Decl_Node);
12654 -- Flag actual derived types so their elaboration produces the
12655 -- appropriate renamings for the primitive operations of the ancestor.
12656 -- Flag actual for formal private types as well, to determine whether
12657 -- operations in the private part may override inherited operations.
12658 -- If the formal has an interface list, the ancestor is not the
12659 -- parent, but the analyzed formal that includes the interface
12660 -- operations of all its progenitors.
12662 -- Same treatment for formal private types, so we can check whether the
12663 -- type is tagged limited when validating derivations in the private
12664 -- part. (See AI05-096).
12666 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12667 if Present (Interface_List (Def)) then
12668 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12670 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12673 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12674 N_Formal_Incomplete_Type_Definition)
12676 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12679 -- If the actual is a synchronized type that implements an interface,
12680 -- the primitive operations are attached to the corresponding record,
12681 -- and we have to treat it as an additional generic actual, so that its
12682 -- primitive operations become visible in the instance. The task or
12683 -- protected type itself does not carry primitive operations.
12685 if Is_Concurrent_Type (Act_T)
12686 and then Is_Tagged_Type (Act_T)
12687 and then Present (Corresponding_Record_Type (Act_T))
12688 and then Present (Ancestor)
12689 and then Is_Interface (Ancestor)
12692 Corr_Rec : constant Entity_Id :=
12693 Corresponding_Record_Type (Act_T);
12694 New_Corr : Entity_Id;
12695 Corr_Decl : Node_Id;
12698 New_Corr := Make_Temporary (Loc, 'S');
12700 Make_Subtype_Declaration (Loc,
12701 Defining_Identifier => New_Corr,
12702 Subtype_Indication =>
12703 New_Occurrence_Of (Corr_Rec, Loc));
12704 Append_To (Decl_Nodes, Corr_Decl);
12706 if Ekind (Act_T) = E_Task_Type then
12707 Set_Ekind (Subt, E_Task_Subtype);
12709 Set_Ekind (Subt, E_Protected_Subtype);
12712 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12713 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12714 Set_Generic_Parent_Type (Decl_Node, Empty);
12718 -- For a floating-point type, capture dimension info if any, because
12719 -- the generated subtype declaration does not come from source and
12720 -- will not process dimensions.
12722 if Is_Floating_Point_Type (Act_T) then
12723 Copy_Dimensions (Act_T, Subt);
12727 end Instantiate_Type;
12729 ---------------------
12730 -- Is_In_Main_Unit --
12731 ---------------------
12733 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12734 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12735 Current_Unit : Node_Id;
12738 if Unum = Main_Unit then
12741 -- If the current unit is a subunit then it is either the main unit or
12742 -- is being compiled as part of the main unit.
12744 elsif Nkind (N) = N_Compilation_Unit then
12745 return Nkind (Unit (N)) = N_Subunit;
12748 Current_Unit := Parent (N);
12749 while Present (Current_Unit)
12750 and then Nkind (Current_Unit) /= N_Compilation_Unit
12752 Current_Unit := Parent (Current_Unit);
12755 -- The instantiation node is in the main unit, or else the current node
12756 -- (perhaps as the result of nested instantiations) is in the main unit,
12757 -- or in the declaration of the main unit, which in this last case must
12760 return Unum = Main_Unit
12761 or else Current_Unit = Cunit (Main_Unit)
12762 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12763 or else (Present (Library_Unit (Current_Unit))
12764 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12765 end Is_In_Main_Unit;
12767 ----------------------------
12768 -- Load_Parent_Of_Generic --
12769 ----------------------------
12771 procedure Load_Parent_Of_Generic
12774 Body_Optional : Boolean := False)
12776 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12777 Saved_Style_Check : constant Boolean := Style_Check;
12778 Saved_Warnings : constant Warning_Record := Save_Warnings;
12779 True_Parent : Node_Id;
12780 Inst_Node : Node_Id;
12782 Previous_Instances : constant Elist_Id := New_Elmt_List;
12784 procedure Collect_Previous_Instances (Decls : List_Id);
12785 -- Collect all instantiations in the given list of declarations, that
12786 -- precede the generic that we need to load. If the bodies of these
12787 -- instantiations are available, we must analyze them, to ensure that
12788 -- the public symbols generated are the same when the unit is compiled
12789 -- to generate code, and when it is compiled in the context of a unit
12790 -- that needs a particular nested instance. This process is applied to
12791 -- both package and subprogram instances.
12793 --------------------------------
12794 -- Collect_Previous_Instances --
12795 --------------------------------
12797 procedure Collect_Previous_Instances (Decls : List_Id) is
12801 Decl := First (Decls);
12802 while Present (Decl) loop
12803 if Sloc (Decl) >= Sloc (Inst_Node) then
12806 -- If Decl is an instantiation, then record it as requiring
12807 -- instantiation of the corresponding body, except if it is an
12808 -- abbreviated instantiation generated internally for conformance
12809 -- checking purposes only for the case of a formal package
12810 -- declared without a box (see Instantiate_Formal_Package). Such
12811 -- an instantiation does not generate any code (the actual code
12812 -- comes from actual) and thus does not need to be analyzed here.
12813 -- If the instantiation appears with a generic package body it is
12814 -- not analyzed here either.
12816 elsif Nkind (Decl) = N_Package_Instantiation
12817 and then not Is_Internal (Defining_Entity (Decl))
12819 Append_Elmt (Decl, Previous_Instances);
12821 -- For a subprogram instantiation, omit instantiations intrinsic
12822 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12824 elsif Nkind_In (Decl, N_Function_Instantiation,
12825 N_Procedure_Instantiation)
12826 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12828 Append_Elmt (Decl, Previous_Instances);
12830 elsif Nkind (Decl) = N_Package_Declaration then
12831 Collect_Previous_Instances
12832 (Visible_Declarations (Specification (Decl)));
12833 Collect_Previous_Instances
12834 (Private_Declarations (Specification (Decl)));
12836 -- Previous non-generic bodies may contain instances as well
12838 elsif Nkind (Decl) = N_Package_Body
12839 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12841 Collect_Previous_Instances (Declarations (Decl));
12843 elsif Nkind (Decl) = N_Subprogram_Body
12844 and then not Acts_As_Spec (Decl)
12845 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12847 Collect_Previous_Instances (Declarations (Decl));
12852 end Collect_Previous_Instances;
12854 -- Start of processing for Load_Parent_Of_Generic
12857 if not In_Same_Source_Unit (N, Spec)
12858 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12859 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12860 and then not Is_In_Main_Unit (Spec))
12862 -- Find body of parent of spec, and analyze it. A special case arises
12863 -- when the parent is an instantiation, that is to say when we are
12864 -- currently instantiating a nested generic. In that case, there is
12865 -- no separate file for the body of the enclosing instance. Instead,
12866 -- the enclosing body must be instantiated as if it were a pending
12867 -- instantiation, in order to produce the body for the nested generic
12868 -- we require now. Note that in that case the generic may be defined
12869 -- in a package body, the instance defined in the same package body,
12870 -- and the original enclosing body may not be in the main unit.
12872 Inst_Node := Empty;
12874 True_Parent := Parent (Spec);
12875 while Present (True_Parent)
12876 and then Nkind (True_Parent) /= N_Compilation_Unit
12878 if Nkind (True_Parent) = N_Package_Declaration
12880 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12882 -- Parent is a compilation unit that is an instantiation.
12883 -- Instantiation node has been replaced with package decl.
12885 Inst_Node := Original_Node (True_Parent);
12888 elsif Nkind (True_Parent) = N_Package_Declaration
12889 and then Present (Generic_Parent (Specification (True_Parent)))
12890 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12892 -- Parent is an instantiation within another specification.
12893 -- Declaration for instance has been inserted before original
12894 -- instantiation node. A direct link would be preferable?
12896 Inst_Node := Next (True_Parent);
12897 while Present (Inst_Node)
12898 and then Nkind (Inst_Node) /= N_Package_Instantiation
12903 -- If the instance appears within a generic, and the generic
12904 -- unit is defined within a formal package of the enclosing
12905 -- generic, there is no generic body available, and none
12906 -- needed. A more precise test should be used ???
12908 if No (Inst_Node) then
12915 True_Parent := Parent (True_Parent);
12919 -- Case where we are currently instantiating a nested generic
12921 if Present (Inst_Node) then
12922 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12924 -- Instantiation node and declaration of instantiated package
12925 -- were exchanged when only the declaration was needed.
12926 -- Restore instantiation node before proceeding with body.
12928 Set_Unit (Parent (True_Parent), Inst_Node);
12931 -- Now complete instantiation of enclosing body, if it appears in
12932 -- some other unit. If it appears in the current unit, the body
12933 -- will have been instantiated already.
12935 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12937 -- We need to determine the expander mode to instantiate the
12938 -- enclosing body. Because the generic body we need may use
12939 -- global entities declared in the enclosing package (including
12940 -- aggregates) it is in general necessary to compile this body
12941 -- with expansion enabled, except if we are within a generic
12942 -- package, in which case the usual generic rule applies.
12945 Exp_Status : Boolean := True;
12949 -- Loop through scopes looking for generic package
12951 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12952 while Present (Scop)
12953 and then Scop /= Standard_Standard
12955 if Ekind (Scop) = E_Generic_Package then
12956 Exp_Status := False;
12960 Scop := Scope (Scop);
12963 -- Collect previous instantiations in the unit that contains
12964 -- the desired generic.
12966 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12967 and then not Body_Optional
12971 Info : Pending_Body_Info;
12975 Par := Parent (Inst_Node);
12976 while Present (Par) loop
12977 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12978 Par := Parent (Par);
12981 pragma Assert (Present (Par));
12983 if Nkind (Par) = N_Package_Body then
12984 Collect_Previous_Instances (Declarations (Par));
12986 elsif Nkind (Par) = N_Package_Declaration then
12987 Collect_Previous_Instances
12988 (Visible_Declarations (Specification (Par)));
12989 Collect_Previous_Instances
12990 (Private_Declarations (Specification (Par)));
12993 -- Enclosing unit is a subprogram body. In this
12994 -- case all instance bodies are processed in order
12995 -- and there is no need to collect them separately.
13000 Decl := First_Elmt (Previous_Instances);
13001 while Present (Decl) loop
13003 (Inst_Node => Node (Decl),
13005 Instance_Spec (Node (Decl)),
13006 Expander_Status => Exp_Status,
13007 Current_Sem_Unit =>
13008 Get_Code_Unit (Sloc (Node (Decl))),
13009 Scope_Suppress => Scope_Suppress,
13010 Local_Suppress_Stack_Top =>
13011 Local_Suppress_Stack_Top,
13012 Version => Ada_Version,
13013 Version_Pragma => Ada_Version_Pragma,
13014 Warnings => Save_Warnings,
13015 SPARK_Mode => SPARK_Mode,
13016 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13018 -- Package instance
13021 Nkind (Node (Decl)) = N_Package_Instantiation
13023 Instantiate_Package_Body
13024 (Info, Body_Optional => True);
13026 -- Subprogram instance
13029 -- The instance_spec is in the wrapper package,
13030 -- usually followed by its local renaming
13031 -- declaration. See Build_Subprogram_Renaming
13036 (Last (Visible_Declarations
13037 (Specification (Info.Act_Decl))));
13040 N_Subprogram_Renaming_Declaration
13042 Decl := Prev (Decl);
13045 Info.Act_Decl := Decl;
13048 Instantiate_Subprogram_Body
13049 (Info, Body_Optional => True);
13057 Instantiate_Package_Body
13059 ((Inst_Node => Inst_Node,
13060 Act_Decl => True_Parent,
13061 Expander_Status => Exp_Status,
13062 Current_Sem_Unit => Get_Code_Unit
13063 (Sloc (Inst_Node)),
13064 Scope_Suppress => Scope_Suppress,
13065 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13066 Version => Ada_Version,
13067 Version_Pragma => Ada_Version_Pragma,
13068 Warnings => Save_Warnings,
13069 SPARK_Mode => SPARK_Mode,
13070 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13071 Body_Optional => Body_Optional);
13075 -- Case where we are not instantiating a nested generic
13078 Opt.Style_Check := False;
13079 Expander_Mode_Save_And_Set (True);
13080 Load_Needed_Body (Comp_Unit, OK);
13081 Opt.Style_Check := Saved_Style_Check;
13082 Restore_Warnings (Saved_Warnings);
13083 Expander_Mode_Restore;
13086 and then Unit_Requires_Body (Defining_Entity (Spec))
13087 and then not Body_Optional
13090 Bname : constant Unit_Name_Type :=
13091 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13094 -- In CodePeer mode, the missing body may make the analysis
13095 -- incomplete, but we do not treat it as fatal.
13097 if CodePeer_Mode then
13101 Error_Msg_Unit_1 := Bname;
13102 Error_Msg_N ("this instantiation requires$!", N);
13103 Error_Msg_File_1 :=
13104 Get_File_Name (Bname, Subunit => False);
13105 Error_Msg_N ("\but file{ was not found!", N);
13106 raise Unrecoverable_Error;
13113 -- If loading parent of the generic caused an instantiation circularity,
13114 -- we abandon compilation at this point, because otherwise in some cases
13115 -- we get into trouble with infinite recursions after this point.
13117 if Circularity_Detected then
13118 raise Unrecoverable_Error;
13120 end Load_Parent_Of_Generic;
13122 ---------------------------------
13123 -- Map_Formal_Package_Entities --
13124 ---------------------------------
13126 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13131 Set_Instance_Of (Form, Act);
13133 -- Traverse formal and actual package to map the corresponding entities.
13134 -- We skip over internal entities that may be generated during semantic
13135 -- analysis, and find the matching entities by name, given that they
13136 -- must appear in the same order.
13138 E1 := First_Entity (Form);
13139 E2 := First_Entity (Act);
13140 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13141 -- Could this test be a single condition??? Seems like it could, and
13142 -- isn't FPE (Form) a constant anyway???
13144 if not Is_Internal (E1)
13145 and then Present (Parent (E1))
13146 and then not Is_Class_Wide_Type (E1)
13147 and then not Is_Internal_Name (Chars (E1))
13149 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13156 Set_Instance_Of (E1, E2);
13158 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13159 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13162 if Is_Constrained (E1) then
13163 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13166 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13167 Map_Formal_Package_Entities (E1, E2);
13174 end Map_Formal_Package_Entities;
13176 -----------------------
13177 -- Move_Freeze_Nodes --
13178 -----------------------
13180 procedure Move_Freeze_Nodes
13181 (Out_Of : Entity_Id;
13186 Next_Decl : Node_Id;
13187 Next_Node : Node_Id := After;
13190 function Is_Outer_Type (T : Entity_Id) return Boolean;
13191 -- Check whether entity is declared in a scope external to that of the
13194 -------------------
13195 -- Is_Outer_Type --
13196 -------------------
13198 function Is_Outer_Type (T : Entity_Id) return Boolean is
13199 Scop : Entity_Id := Scope (T);
13202 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13206 while Scop /= Standard_Standard loop
13207 if Scop = Out_Of then
13210 Scop := Scope (Scop);
13218 -- Start of processing for Move_Freeze_Nodes
13225 -- First remove the freeze nodes that may appear before all other
13229 while Present (Decl)
13230 and then Nkind (Decl) = N_Freeze_Entity
13231 and then Is_Outer_Type (Entity (Decl))
13233 Decl := Remove_Head (L);
13234 Insert_After (Next_Node, Decl);
13235 Set_Analyzed (Decl, False);
13240 -- Next scan the list of declarations and remove each freeze node that
13241 -- appears ahead of the current node.
13243 while Present (Decl) loop
13244 while Present (Next (Decl))
13245 and then Nkind (Next (Decl)) = N_Freeze_Entity
13246 and then Is_Outer_Type (Entity (Next (Decl)))
13248 Next_Decl := Remove_Next (Decl);
13249 Insert_After (Next_Node, Next_Decl);
13250 Set_Analyzed (Next_Decl, False);
13251 Next_Node := Next_Decl;
13254 -- If the declaration is a nested package or concurrent type, then
13255 -- recurse. Nested generic packages will have been processed from the
13258 case Nkind (Decl) is
13259 when N_Package_Declaration =>
13260 Spec := Specification (Decl);
13262 when N_Task_Type_Declaration =>
13263 Spec := Task_Definition (Decl);
13265 when N_Protected_Type_Declaration =>
13266 Spec := Protected_Definition (Decl);
13272 if Present (Spec) then
13273 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13274 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13279 end Move_Freeze_Nodes;
13285 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13287 return Generic_Renamings.Table (E).Next_In_HTable;
13290 ------------------------
13291 -- Preanalyze_Actuals --
13292 ------------------------
13294 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13297 Errs : constant Int := Serious_Errors_Detected;
13299 Cur : Entity_Id := Empty;
13300 -- Current homograph of the instance name
13303 -- Saved visibility status of the current homograph
13306 Assoc := First (Generic_Associations (N));
13308 -- If the instance is a child unit, its name may hide an outer homonym,
13309 -- so make it invisible to perform name resolution on the actuals.
13311 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13313 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13315 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13317 if Is_Compilation_Unit (Cur) then
13318 Vis := Is_Immediately_Visible (Cur);
13319 Set_Is_Immediately_Visible (Cur, False);
13325 while Present (Assoc) loop
13326 if Nkind (Assoc) /= N_Others_Choice then
13327 Act := Explicit_Generic_Actual_Parameter (Assoc);
13329 -- Within a nested instantiation, a defaulted actual is an empty
13330 -- association, so nothing to analyze. If the subprogram actual
13331 -- is an attribute, analyze prefix only, because actual is not a
13332 -- complete attribute reference.
13334 -- If actual is an allocator, analyze expression only. The full
13335 -- analysis can generate code, and if instance is a compilation
13336 -- unit we have to wait until the package instance is installed
13337 -- to have a proper place to insert this code.
13339 -- String literals may be operators, but at this point we do not
13340 -- know whether the actual is a formal subprogram or a string.
13345 elsif Nkind (Act) = N_Attribute_Reference then
13346 Analyze (Prefix (Act));
13348 elsif Nkind (Act) = N_Explicit_Dereference then
13349 Analyze (Prefix (Act));
13351 elsif Nkind (Act) = N_Allocator then
13353 Expr : constant Node_Id := Expression (Act);
13356 if Nkind (Expr) = N_Subtype_Indication then
13357 Analyze (Subtype_Mark (Expr));
13359 -- Analyze separately each discriminant constraint, when
13360 -- given with a named association.
13366 Constr := First (Constraints (Constraint (Expr)));
13367 while Present (Constr) loop
13368 if Nkind (Constr) = N_Discriminant_Association then
13369 Analyze (Expression (Constr));
13383 elsif Nkind (Act) /= N_Operator_Symbol then
13386 if Is_Entity_Name (Act)
13387 and then Is_Type (Entity (Act))
13388 and then From_Limited_With (Entity (Act))
13390 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13394 if Errs /= Serious_Errors_Detected then
13396 -- Do a minimal analysis of the generic, to prevent spurious
13397 -- warnings complaining about the generic being unreferenced,
13398 -- before abandoning the instantiation.
13400 Analyze (Name (N));
13402 if Is_Entity_Name (Name (N))
13403 and then Etype (Name (N)) /= Any_Type
13405 Generate_Reference (Entity (Name (N)), Name (N));
13406 Set_Is_Instantiated (Entity (Name (N)));
13409 if Present (Cur) then
13411 -- For the case of a child instance hiding an outer homonym,
13412 -- provide additional warning which might explain the error.
13414 Set_Is_Immediately_Visible (Cur, Vis);
13416 ("& hides outer unit with the same name??",
13417 N, Defining_Unit_Name (N));
13420 Abandon_Instantiation (Act);
13427 if Present (Cur) then
13428 Set_Is_Immediately_Visible (Cur, Vis);
13430 end Preanalyze_Actuals;
13432 -------------------
13433 -- Remove_Parent --
13434 -------------------
13436 procedure Remove_Parent (In_Body : Boolean := False) is
13437 S : Entity_Id := Current_Scope;
13438 -- S is the scope containing the instantiation just completed. The scope
13439 -- stack contains the parent instances of the instantiation, followed by
13448 -- After child instantiation is complete, remove from scope stack the
13449 -- extra copy of the current scope, and then remove parent instances.
13451 if not In_Body then
13454 while Current_Scope /= S loop
13455 P := Current_Scope;
13456 End_Package_Scope (Current_Scope);
13458 if In_Open_Scopes (P) then
13459 E := First_Entity (P);
13460 while Present (E) loop
13461 Set_Is_Immediately_Visible (E, True);
13465 -- If instantiation is declared in a block, it is the enclosing
13466 -- scope that might be a parent instance. Note that only one
13467 -- block can be involved, because the parent instances have
13468 -- been installed within it.
13470 if Ekind (P) = E_Block then
13471 Cur_P := Scope (P);
13476 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13477 -- We are within an instance of some sibling. Retain
13478 -- visibility of parent, for proper subsequent cleanup, and
13479 -- reinstall private declarations as well.
13481 Set_In_Private_Part (P);
13482 Install_Private_Declarations (P);
13485 -- If the ultimate parent is a top-level unit recorded in
13486 -- Instance_Parent_Unit, then reset its visibility to what it was
13487 -- before instantiation. (It's not clear what the purpose is of
13488 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13489 -- present before the ultimate parent test was added.???)
13491 elsif not In_Open_Scopes (Scope (P))
13492 or else (P = Instance_Parent_Unit
13493 and then not Parent_Unit_Visible)
13495 Set_Is_Immediately_Visible (P, False);
13497 -- If the current scope is itself an instantiation of a generic
13498 -- nested within P, and we are in the private part of body of this
13499 -- instantiation, restore the full views of P, that were removed
13500 -- in End_Package_Scope above. This obscure case can occur when a
13501 -- subunit of a generic contains an instance of a child unit of
13502 -- its generic parent unit.
13504 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13506 Par : constant Entity_Id :=
13507 Generic_Parent (Package_Specification (S));
13510 and then P = Scope (Par)
13511 and then (In_Package_Body (S) or else In_Private_Part (S))
13513 Set_In_Private_Part (P);
13514 Install_Private_Declarations (P);
13520 -- Reset visibility of entities in the enclosing scope
13522 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13524 Hidden := First_Elmt (Hidden_Entities);
13525 while Present (Hidden) loop
13526 Set_Is_Immediately_Visible (Node (Hidden), True);
13527 Next_Elmt (Hidden);
13531 -- Each body is analyzed separately, and there is no context that
13532 -- needs preserving from one body instance to the next, so remove all
13533 -- parent scopes that have been installed.
13535 while Present (S) loop
13536 End_Package_Scope (S);
13537 Set_Is_Immediately_Visible (S, False);
13538 S := Current_Scope;
13539 exit when S = Standard_Standard;
13548 procedure Restore_Env is
13549 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13552 if No (Current_Instantiated_Parent.Act_Id) then
13553 -- Restore environment after subprogram inlining
13555 Restore_Private_Views (Empty);
13558 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13559 Exchanged_Views := Saved.Exchanged_Views;
13560 Hidden_Entities := Saved.Hidden_Entities;
13561 Current_Sem_Unit := Saved.Current_Sem_Unit;
13562 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13563 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13565 Restore_Opt_Config_Switches (Saved.Switches);
13567 Instance_Envs.Decrement_Last;
13570 ---------------------------
13571 -- Restore_Private_Views --
13572 ---------------------------
13574 procedure Restore_Private_Views
13575 (Pack_Id : Entity_Id;
13576 Is_Package : Boolean := True)
13581 Dep_Elmt : Elmt_Id;
13584 procedure Restore_Nested_Formal (Formal : Entity_Id);
13585 -- Hide the generic formals of formal packages declared with box which
13586 -- were reachable in the current instantiation.
13588 ---------------------------
13589 -- Restore_Nested_Formal --
13590 ---------------------------
13592 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13596 if Present (Renamed_Object (Formal))
13597 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13601 elsif Present (Associated_Formal_Package (Formal)) then
13602 Ent := First_Entity (Formal);
13603 while Present (Ent) loop
13604 exit when Ekind (Ent) = E_Package
13605 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13607 Set_Is_Hidden (Ent);
13608 Set_Is_Potentially_Use_Visible (Ent, False);
13610 -- If package, then recurse
13612 if Ekind (Ent) = E_Package then
13613 Restore_Nested_Formal (Ent);
13619 end Restore_Nested_Formal;
13621 -- Start of processing for Restore_Private_Views
13624 M := First_Elmt (Exchanged_Views);
13625 while Present (M) loop
13628 -- Subtypes of types whose views have been exchanged, and that are
13629 -- defined within the instance, were not on the Private_Dependents
13630 -- list on entry to the instance, so they have to be exchanged
13631 -- explicitly now, in order to remain consistent with the view of the
13634 if Ekind_In (Typ, E_Private_Type,
13635 E_Limited_Private_Type,
13636 E_Record_Type_With_Private)
13638 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13639 while Present (Dep_Elmt) loop
13640 Dep_Typ := Node (Dep_Elmt);
13642 if Scope (Dep_Typ) = Pack_Id
13643 and then Present (Full_View (Dep_Typ))
13645 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13646 Exchange_Declarations (Dep_Typ);
13649 Next_Elmt (Dep_Elmt);
13653 Exchange_Declarations (Node (M));
13657 if No (Pack_Id) then
13661 -- Make the generic formal parameters private, and make the formal types
13662 -- into subtypes of the actuals again.
13664 E := First_Entity (Pack_Id);
13665 while Present (E) loop
13666 Set_Is_Hidden (E, True);
13669 and then Nkind (Parent (E)) = N_Subtype_Declaration
13671 -- If the actual for E is itself a generic actual type from
13672 -- an enclosing instance, E is still a generic actual type
13673 -- outside of the current instance. This matter when resolving
13674 -- an overloaded call that may be ambiguous in the enclosing
13675 -- instance, when two of its actuals coincide.
13677 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13678 and then Is_Generic_Actual_Type
13679 (Entity (Subtype_Indication (Parent (E))))
13683 Set_Is_Generic_Actual_Type (E, False);
13686 -- An unusual case of aliasing: the actual may also be directly
13687 -- visible in the generic, and be private there, while it is fully
13688 -- visible in the context of the instance. The internal subtype
13689 -- is private in the instance but has full visibility like its
13690 -- parent in the enclosing scope. This enforces the invariant that
13691 -- the privacy status of all private dependents of a type coincide
13692 -- with that of the parent type. This can only happen when a
13693 -- generic child unit is instantiated within a sibling.
13695 if Is_Private_Type (E)
13696 and then not Is_Private_Type (Etype (E))
13698 Exchange_Declarations (E);
13701 elsif Ekind (E) = E_Package then
13703 -- The end of the renaming list is the renaming of the generic
13704 -- package itself. If the instance is a subprogram, all entities
13705 -- in the corresponding package are renamings. If this entity is
13706 -- a formal package, make its own formals private as well. The
13707 -- actual in this case is itself the renaming of an instantiation.
13708 -- If the entity is not a package renaming, it is the entity
13709 -- created to validate formal package actuals: ignore it.
13711 -- If the actual is itself a formal package for the enclosing
13712 -- generic, or the actual for such a formal package, it remains
13713 -- visible on exit from the instance, and therefore nothing needs
13714 -- to be done either, except to keep it accessible.
13716 if Is_Package and then Renamed_Object (E) = Pack_Id then
13719 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13723 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13725 Set_Is_Hidden (E, False);
13729 Act_P : constant Entity_Id := Renamed_Object (E);
13733 Id := First_Entity (Act_P);
13735 and then Id /= First_Private_Entity (Act_P)
13737 exit when Ekind (Id) = E_Package
13738 and then Renamed_Object (Id) = Act_P;
13740 Set_Is_Hidden (Id, True);
13741 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13743 if Ekind (Id) = E_Package then
13744 Restore_Nested_Formal (Id);
13755 end Restore_Private_Views;
13762 (Gen_Unit : Entity_Id;
13763 Act_Unit : Entity_Id)
13767 Set_Instance_Env (Gen_Unit, Act_Unit);
13770 ----------------------------
13771 -- Save_Global_References --
13772 ----------------------------
13774 procedure Save_Global_References (Templ : Node_Id) is
13776 -- ??? it is horrible to use global variables in highly recursive code
13779 -- The entity of the current associated node
13781 Gen_Scope : Entity_Id;
13782 -- The scope of the generic for which references are being saved
13785 -- The current associated node
13787 function Is_Global (E : Entity_Id) return Boolean;
13788 -- Check whether entity is defined outside of generic unit. Examine the
13789 -- scope of an entity, and the scope of the scope, etc, until we find
13790 -- either Standard, in which case the entity is global, or the generic
13791 -- unit itself, which indicates that the entity is local. If the entity
13792 -- is the generic unit itself, as in the case of a recursive call, or
13793 -- the enclosing generic unit, if different from the current scope, then
13794 -- it is local as well, because it will be replaced at the point of
13795 -- instantiation. On the other hand, if it is a reference to a child
13796 -- unit of a common ancestor, which appears in an instantiation, it is
13797 -- global because it is used to denote a specific compilation unit at
13798 -- the time the instantiations will be analyzed.
13800 procedure Reset_Entity (N : Node_Id);
13801 -- Save semantic information on global entity so that it is not resolved
13802 -- again at instantiation time.
13804 procedure Save_Entity_Descendants (N : Node_Id);
13805 -- Apply Save_Global_References to the two syntactic descendants of
13806 -- non-terminal nodes that carry an Associated_Node and are processed
13807 -- through Reset_Entity. Once the global entity (if any) has been
13808 -- captured together with its type, only two syntactic descendants need
13809 -- to be traversed to complete the processing of the tree rooted at N.
13810 -- This applies to Selected_Components, Expanded_Names, and to Operator
13811 -- nodes. N can also be a character literal, identifier, or operator
13812 -- symbol node, but the call has no effect in these cases.
13814 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13815 -- Default actuals in nested instances must be handled specially
13816 -- because there is no link to them from the original tree. When an
13817 -- actual subprogram is given by a default, we add an explicit generic
13818 -- association for it in the instantiation node. When we save the
13819 -- global references on the name of the instance, we recover the list
13820 -- of generic associations, and add an explicit one to the original
13821 -- generic tree, through which a global actual can be preserved.
13822 -- Similarly, if a child unit is instantiated within a sibling, in the
13823 -- context of the parent, we must preserve the identifier of the parent
13824 -- so that it can be properly resolved in a subsequent instantiation.
13826 procedure Save_Global_Descendant (D : Union_Id);
13827 -- Apply Save_References recursively to the descendents of node D
13829 procedure Save_References (N : Node_Id);
13830 -- This is the recursive procedure that does the work, once the
13831 -- enclosing generic scope has been established.
13837 function Is_Global (E : Entity_Id) return Boolean is
13840 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13841 -- Determine whether the parent node of a reference to a child unit
13842 -- denotes an instantiation or a formal package, in which case the
13843 -- reference to the child unit is global, even if it appears within
13844 -- the current scope (e.g. when the instance appears within the body
13845 -- of an ancestor).
13847 ----------------------
13848 -- Is_Instance_Node --
13849 ----------------------
13851 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13853 return Nkind (Decl) in N_Generic_Instantiation
13855 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13856 end Is_Instance_Node;
13858 -- Start of processing for Is_Global
13861 if E = Gen_Scope then
13864 elsif E = Standard_Standard then
13867 elsif Is_Child_Unit (E)
13868 and then (Is_Instance_Node (Parent (N2))
13869 or else (Nkind (Parent (N2)) = N_Expanded_Name
13870 and then N2 = Selector_Name (Parent (N2))
13872 Is_Instance_Node (Parent (Parent (N2)))))
13878 while Se /= Gen_Scope loop
13879 if Se = Standard_Standard then
13894 procedure Reset_Entity (N : Node_Id) is
13895 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13896 -- If the type of N2 is global to the generic unit, save the type in
13897 -- the generic node. Just as we perform name capture for explicit
13898 -- references within the generic, we must capture the global types
13899 -- of local entities because they may participate in resolution in
13902 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13903 -- Find the ultimate ancestor of the current unit. If it is not a
13904 -- generic unit, then the name of the current unit in the prefix of
13905 -- an expanded name must be replaced with its generic homonym to
13906 -- ensure that it will be properly resolved in an instance.
13908 ---------------------
13909 -- Set_Global_Type --
13910 ---------------------
13912 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13913 Typ : constant Entity_Id := Etype (N2);
13916 Set_Etype (N, Typ);
13918 -- If the entity of N is not the associated node, this is a
13919 -- nested generic and it has an associated node as well, whose
13920 -- type is already the full view (see below). Indicate that the
13921 -- original node has a private view.
13923 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13924 Set_Has_Private_View (N);
13927 -- If not a private type, nothing else to do
13929 if not Is_Private_Type (Typ) then
13930 if Is_Array_Type (Typ)
13931 and then Is_Private_Type (Component_Type (Typ))
13933 Set_Has_Private_View (N);
13936 -- If it is a derivation of a private type in a context where no
13937 -- full view is needed, nothing to do either.
13939 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13942 -- Otherwise mark the type for flipping and use the full view when
13946 Set_Has_Private_View (N);
13948 if Present (Full_View (Typ)) then
13949 Set_Etype (N2, Full_View (Typ));
13952 end Set_Global_Type;
13958 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13963 while Is_Child_Unit (Par) loop
13964 Par := Scope (Par);
13970 -- Start of processing for Reset_Entity
13973 N2 := Get_Associated_Node (N);
13976 if Present (E) then
13978 -- If the node is an entry call to an entry in an enclosing task,
13979 -- it is rewritten as a selected component. No global entity to
13980 -- preserve in this case, since the expansion will be redone in
13983 if not Nkind_In (E, N_Defining_Character_Literal,
13984 N_Defining_Identifier,
13985 N_Defining_Operator_Symbol)
13987 Set_Associated_Node (N, Empty);
13988 Set_Etype (N, Empty);
13992 -- If the entity is an itype created as a subtype of an access
13993 -- type with a null exclusion restore source entity for proper
13994 -- visibility. The itype will be created anew in the instance.
13997 and then Ekind (E) = E_Access_Subtype
13998 and then Is_Entity_Name (N)
13999 and then Chars (Etype (E)) = Chars (N)
14002 Set_Entity (N2, E);
14006 if Is_Global (E) then
14008 -- If the entity is a package renaming that is the prefix of
14009 -- an expanded name, it has been rewritten as the renamed
14010 -- package, which is necessary semantically but complicates
14011 -- ASIS tree traversal, so we recover the original entity to
14012 -- expose the renaming. Take into account that the context may
14013 -- be a nested generic, that the original node may itself have
14014 -- an associated node that had better be an entity, and that
14015 -- the current node is still a selected component.
14017 if Ekind (E) = E_Package
14018 and then Nkind (N) = N_Selected_Component
14019 and then Nkind (Parent (N)) = N_Expanded_Name
14020 and then Present (Original_Node (N2))
14021 and then Is_Entity_Name (Original_Node (N2))
14022 and then Present (Entity (Original_Node (N2)))
14024 if Is_Global (Entity (Original_Node (N2))) then
14025 N2 := Original_Node (N2);
14026 Set_Associated_Node (N, N2);
14027 Set_Global_Type (N, N2);
14029 -- Renaming is local, and will be resolved in instance
14032 Set_Associated_Node (N, Empty);
14033 Set_Etype (N, Empty);
14037 Set_Global_Type (N, N2);
14040 elsif Nkind (N) = N_Op_Concat
14041 and then Is_Generic_Type (Etype (N2))
14042 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14044 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14045 and then Is_Intrinsic_Subprogram (E)
14049 -- Entity is local. Mark generic node as unresolved. Note that now
14050 -- it does not have an entity.
14053 Set_Associated_Node (N, Empty);
14054 Set_Etype (N, Empty);
14057 if Nkind (Parent (N)) in N_Generic_Instantiation
14058 and then N = Name (Parent (N))
14060 Save_Global_Defaults (Parent (N), Parent (N2));
14063 elsif Nkind (Parent (N)) = N_Selected_Component
14064 and then Nkind (Parent (N2)) = N_Expanded_Name
14066 if Is_Global (Entity (Parent (N2))) then
14067 Change_Selected_Component_To_Expanded_Name (Parent (N));
14068 Set_Associated_Node (Parent (N), Parent (N2));
14069 Set_Global_Type (Parent (N), Parent (N2));
14070 Save_Entity_Descendants (N);
14072 -- If this is a reference to the current generic entity, replace
14073 -- by the name of the generic homonym of the current package. This
14074 -- is because in an instantiation Par.P.Q will not resolve to the
14075 -- name of the instance, whose enclosing scope is not necessarily
14076 -- Par. We use the generic homonym rather that the name of the
14077 -- generic itself because it may be hidden by a local declaration.
14079 elsif In_Open_Scopes (Entity (Parent (N2)))
14081 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14083 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14084 Rewrite (Parent (N),
14085 Make_Identifier (Sloc (N),
14087 Chars (Generic_Homonym (Entity (Parent (N2))))));
14089 Rewrite (Parent (N),
14090 Make_Identifier (Sloc (N),
14091 Chars => Chars (Selector_Name (Parent (N2)))));
14095 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14096 and then Parent (N) = Name (Parent (Parent (N)))
14098 Save_Global_Defaults
14099 (Parent (Parent (N)), Parent (Parent (N2)));
14102 -- A selected component may denote a static constant that has been
14103 -- folded. If the static constant is global to the generic, capture
14104 -- its value. Otherwise the folding will happen in any instantiation.
14106 elsif Nkind (Parent (N)) = N_Selected_Component
14107 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14109 if Present (Entity (Original_Node (Parent (N2))))
14110 and then Is_Global (Entity (Original_Node (Parent (N2))))
14112 Rewrite (Parent (N), New_Copy (Parent (N2)));
14113 Set_Analyzed (Parent (N), False);
14116 -- A selected component may be transformed into a parameterless
14117 -- function call. If the called entity is global, rewrite the node
14118 -- appropriately, i.e. as an extended name for the global entity.
14120 elsif Nkind (Parent (N)) = N_Selected_Component
14121 and then Nkind (Parent (N2)) = N_Function_Call
14122 and then N = Selector_Name (Parent (N))
14124 if No (Parameter_Associations (Parent (N2))) then
14125 if Is_Global (Entity (Name (Parent (N2)))) then
14126 Change_Selected_Component_To_Expanded_Name (Parent (N));
14127 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14128 Set_Global_Type (Parent (N), Name (Parent (N2)));
14129 Save_Entity_Descendants (N);
14132 Set_Is_Prefixed_Call (Parent (N));
14133 Set_Associated_Node (N, Empty);
14134 Set_Etype (N, Empty);
14137 -- In Ada 2005, X.F may be a call to a primitive operation,
14138 -- rewritten as F (X). This rewriting will be done again in an
14139 -- instance, so keep the original node. Global entities will be
14140 -- captured as for other constructs. Indicate that this must
14141 -- resolve as a call, to prevent accidental overloading in the
14142 -- instance, if both a component and a primitive operation appear
14146 Set_Is_Prefixed_Call (Parent (N));
14149 -- Entity is local. Reset in generic unit, so that node is resolved
14150 -- anew at the point of instantiation.
14153 Set_Associated_Node (N, Empty);
14154 Set_Etype (N, Empty);
14158 -----------------------------
14159 -- Save_Entity_Descendants --
14160 -----------------------------
14162 procedure Save_Entity_Descendants (N : Node_Id) is
14165 when N_Binary_Op =>
14166 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14167 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14170 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14172 when N_Expanded_Name |
14173 N_Selected_Component =>
14174 Save_Global_Descendant (Union_Id (Prefix (N)));
14175 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14177 when N_Identifier |
14178 N_Character_Literal |
14179 N_Operator_Symbol =>
14183 raise Program_Error;
14185 end Save_Entity_Descendants;
14187 --------------------------
14188 -- Save_Global_Defaults --
14189 --------------------------
14191 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14192 Loc : constant Source_Ptr := Sloc (N1);
14193 Assoc2 : constant List_Id := Generic_Associations (N2);
14194 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14201 Actual : Entity_Id;
14204 Assoc1 := Generic_Associations (N1);
14206 if Present (Assoc1) then
14207 Act1 := First (Assoc1);
14210 Set_Generic_Associations (N1, New_List);
14211 Assoc1 := Generic_Associations (N1);
14214 if Present (Assoc2) then
14215 Act2 := First (Assoc2);
14220 while Present (Act1) and then Present (Act2) loop
14225 -- Find the associations added for default subprograms
14227 if Present (Act2) then
14228 while Nkind (Act2) /= N_Generic_Association
14229 or else No (Entity (Selector_Name (Act2)))
14230 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14235 -- Add a similar association if the default is global. The
14236 -- renaming declaration for the actual has been analyzed, and
14237 -- its alias is the program it renames. Link the actual in the
14238 -- original generic tree with the node in the analyzed tree.
14240 while Present (Act2) loop
14241 Subp := Entity (Selector_Name (Act2));
14242 Def := Explicit_Generic_Actual_Parameter (Act2);
14244 -- Following test is defence against rubbish errors
14246 if No (Alias (Subp)) then
14250 -- Retrieve the resolved actual from the renaming declaration
14251 -- created for the instantiated formal.
14253 Actual := Entity (Name (Parent (Parent (Subp))));
14254 Set_Entity (Def, Actual);
14255 Set_Etype (Def, Etype (Actual));
14257 if Is_Global (Actual) then
14259 Make_Generic_Association (Loc,
14261 New_Occurrence_Of (Subp, Loc),
14262 Explicit_Generic_Actual_Parameter =>
14263 New_Occurrence_Of (Actual, Loc));
14265 Set_Associated_Node
14266 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14268 Append (Ndec, Assoc1);
14270 -- If there are other defaults, add a dummy association in case
14271 -- there are other defaulted formals with the same name.
14273 elsif Present (Next (Act2)) then
14275 Make_Generic_Association (Loc,
14277 New_Occurrence_Of (Subp, Loc),
14278 Explicit_Generic_Actual_Parameter => Empty);
14280 Append (Ndec, Assoc1);
14287 if Nkind (Name (N1)) = N_Identifier
14288 and then Is_Child_Unit (Gen_Id)
14289 and then Is_Global (Gen_Id)
14290 and then Is_Generic_Unit (Scope (Gen_Id))
14291 and then In_Open_Scopes (Scope (Gen_Id))
14293 -- This is an instantiation of a child unit within a sibling, so
14294 -- that the generic parent is in scope. An eventual instance must
14295 -- occur within the scope of an instance of the parent. Make name
14296 -- in instance into an expanded name, to preserve the identifier
14297 -- of the parent, so it can be resolved subsequently.
14299 Rewrite (Name (N2),
14300 Make_Expanded_Name (Loc,
14301 Chars => Chars (Gen_Id),
14302 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14303 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14304 Set_Entity (Name (N2), Gen_Id);
14306 Rewrite (Name (N1),
14307 Make_Expanded_Name (Loc,
14308 Chars => Chars (Gen_Id),
14309 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14310 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14312 Set_Associated_Node (Name (N1), Name (N2));
14313 Set_Associated_Node (Prefix (Name (N1)), Empty);
14314 Set_Associated_Node
14315 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14316 Set_Etype (Name (N1), Etype (Gen_Id));
14318 end Save_Global_Defaults;
14320 ----------------------------
14321 -- Save_Global_Descendant --
14322 ----------------------------
14324 procedure Save_Global_Descendant (D : Union_Id) is
14328 if D in Node_Range then
14329 if D = Union_Id (Empty) then
14332 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14333 Save_References (Node_Id (D));
14336 elsif D in List_Range then
14337 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14341 N1 := First (List_Id (D));
14342 while Present (N1) loop
14343 Save_References (N1);
14348 -- Element list or other non-node field, nothing to do
14353 end Save_Global_Descendant;
14355 ---------------------
14356 -- Save_References --
14357 ---------------------
14359 -- This is the recursive procedure that does the work once the enclosing
14360 -- generic scope has been established. We have to treat specially a
14361 -- number of node rewritings that are required by semantic processing
14362 -- and which change the kind of nodes in the generic copy: typically
14363 -- constant-folding, replacing an operator node by a string literal, or
14364 -- a selected component by an expanded name. In each of those cases, the
14365 -- transformation is propagated to the generic unit.
14367 procedure Save_References (N : Node_Id) is
14368 Loc : constant Source_Ptr := Sloc (N);
14370 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14371 -- Determine whether arbitrary node Nod requires delayed capture of
14372 -- global references within its aspect specifications.
14374 procedure Save_References_In_Aggregate (N : Node_Id);
14375 -- Save all global references in [extension] aggregate node N
14377 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14378 -- Save all global references in a character literal or operator
14379 -- symbol denoted by N.
14381 procedure Save_References_In_Descendants (N : Node_Id);
14382 -- Save all global references in all descendants of node N
14384 procedure Save_References_In_Identifier (N : Node_Id);
14385 -- Save all global references in identifier node N
14387 procedure Save_References_In_Operator (N : Node_Id);
14388 -- Save all global references in operator node N
14390 procedure Save_References_In_Pragma (Prag : Node_Id);
14391 -- Save all global references found within the expression of pragma
14394 ---------------------------
14395 -- Requires_Delayed_Save --
14396 ---------------------------
14398 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14400 -- Generic packages and subprograms require delayed capture of
14401 -- global references within their aspects due to the timing of
14402 -- annotation analysis.
14404 if Nkind_In (Nod, N_Generic_Package_Declaration,
14405 N_Generic_Subprogram_Declaration,
14407 N_Package_Body_Stub,
14409 N_Subprogram_Body_Stub)
14411 -- Since the capture of global references is done on the
14412 -- unanalyzed generic template, there is no information around
14413 -- to infer the context. Use the Associated_Entity linkages to
14414 -- peek into the analyzed generic copy and determine what the
14415 -- template corresponds to.
14417 if Nod = Templ then
14419 Is_Generic_Declaration_Or_Body
14420 (Unit_Declaration_Node
14421 (Associated_Entity (Defining_Entity (Nod))));
14423 -- Otherwise the generic unit being processed is not the top
14424 -- level template. It is safe to capture of global references
14425 -- within the generic unit because at this point the top level
14426 -- copy is fully analyzed.
14432 -- Otherwise capture the global references without interference
14437 end Requires_Delayed_Save;
14439 ----------------------------------
14440 -- Save_References_In_Aggregate --
14441 ----------------------------------
14443 procedure Save_References_In_Aggregate (N : Node_Id) is
14445 Qual : Node_Id := Empty;
14446 Typ : Entity_Id := Empty;
14448 use Atree.Unchecked_Access;
14449 -- This code section is part of implementing an untyped tree
14450 -- traversal, so it needs direct access to node fields.
14453 N2 := Get_Associated_Node (N);
14455 if Present (N2) then
14458 -- In an instance within a generic, use the name of the actual
14459 -- and not the original generic parameter. If the actual is
14460 -- global in the current generic it must be preserved for its
14463 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14464 and then Present (Generic_Parent_Type (Parent (Typ)))
14466 Typ := Base_Type (Typ);
14467 Set_Etype (N2, Typ);
14471 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14472 Set_Associated_Node (N, Empty);
14474 -- If the aggregate is an actual in a call, it has been
14475 -- resolved in the current context, to some local type. The
14476 -- enclosing call may have been disambiguated by the aggregate,
14477 -- and this disambiguation might fail at instantiation time
14478 -- because the type to which the aggregate did resolve is not
14479 -- preserved. In order to preserve some of this information,
14480 -- wrap the aggregate in a qualified expression, using the id
14481 -- of its type. For further disambiguation we qualify the type
14482 -- name with its scope (if visible) because both id's will have
14483 -- corresponding entities in an instance. This resolves most of
14484 -- the problems with missing type information on aggregates in
14488 and then Nkind (N2) = Nkind (N)
14489 and then Nkind (Parent (N2)) in N_Subprogram_Call
14490 and then Present (Typ)
14491 and then Comes_From_Source (Typ)
14493 Nam := Make_Identifier (Loc, Chars (Typ));
14495 if Is_Immediately_Visible (Scope (Typ)) then
14497 Make_Selected_Component (Loc,
14499 Make_Identifier (Loc, Chars (Scope (Typ))),
14500 Selector_Name => Nam);
14504 Make_Qualified_Expression (Loc,
14505 Subtype_Mark => Nam,
14506 Expression => Relocate_Node (N));
14510 Save_Global_Descendant (Field1 (N));
14511 Save_Global_Descendant (Field2 (N));
14512 Save_Global_Descendant (Field3 (N));
14513 Save_Global_Descendant (Field5 (N));
14515 if Present (Qual) then
14518 end Save_References_In_Aggregate;
14520 ----------------------------------------------
14521 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14522 ----------------------------------------------
14524 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14526 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14529 elsif Nkind (N) = N_Operator_Symbol
14530 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14532 Change_Operator_Symbol_To_String_Literal (N);
14534 end Save_References_In_Char_Lit_Or_Op_Symbol;
14536 ------------------------------------
14537 -- Save_References_In_Descendants --
14538 ------------------------------------
14540 procedure Save_References_In_Descendants (N : Node_Id) is
14541 use Atree.Unchecked_Access;
14542 -- This code section is part of implementing an untyped tree
14543 -- traversal, so it needs direct access to node fields.
14546 Save_Global_Descendant (Field1 (N));
14547 Save_Global_Descendant (Field2 (N));
14548 Save_Global_Descendant (Field3 (N));
14549 Save_Global_Descendant (Field4 (N));
14550 Save_Global_Descendant (Field5 (N));
14551 end Save_References_In_Descendants;
14553 -----------------------------------
14554 -- Save_References_In_Identifier --
14555 -----------------------------------
14557 procedure Save_References_In_Identifier (N : Node_Id) is
14559 -- The node did not undergo a transformation
14561 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14563 -- If this is a discriminant reference, always save it. It is
14564 -- used in the instance to find the corresponding discriminant
14565 -- positionally rather than by name.
14567 Set_Original_Discriminant
14568 (N, Original_Discriminant (Get_Associated_Node (N)));
14571 -- The analysis of the generic copy transformed the identifier
14572 -- into another construct. Propagate the changes to the template.
14575 N2 := Get_Associated_Node (N);
14577 -- The identifier denotes a call to a parameterless function.
14578 -- Mark the node as resolved when the function is external.
14580 if Nkind (N2) = N_Function_Call then
14581 E := Entity (Name (N2));
14583 if Present (E) and then Is_Global (E) then
14584 Set_Etype (N, Etype (N2));
14586 Set_Associated_Node (N, Empty);
14587 Set_Etype (N, Empty);
14590 -- The identifier denotes a named number that was constant
14591 -- folded. Preserve the original name for ASIS and undo the
14592 -- constant folding which will be repeated in the instance.
14594 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14595 and then Is_Entity_Name (Original_Node (N2))
14597 Set_Associated_Node (N, Original_Node (N2));
14600 -- The identifier resolved to a string literal. Propagate this
14601 -- information to the generic template.
14603 elsif Nkind (N2) = N_String_Literal then
14604 Rewrite (N, New_Copy (N2));
14606 -- The identifier is rewritten as a dereference if it is the
14607 -- prefix of an implicit dereference. Preserve the original
14608 -- tree as the analysis of the instance will expand the node
14609 -- again, but preserve the resolved entity if it is global.
14611 elsif Nkind (N2) = N_Explicit_Dereference then
14612 if Is_Entity_Name (Prefix (N2))
14613 and then Present (Entity (Prefix (N2)))
14614 and then Is_Global (Entity (Prefix (N2)))
14616 Set_Associated_Node (N, Prefix (N2));
14618 elsif Nkind (Prefix (N2)) = N_Function_Call
14619 and then Present (Entity (Name (Prefix (N2))))
14620 and then Is_Global (Entity (Name (Prefix (N2))))
14623 Make_Explicit_Dereference (Loc,
14625 Make_Function_Call (Loc,
14628 (Entity (Name (Prefix (N2))), Loc))));
14631 Set_Associated_Node (N, Empty);
14632 Set_Etype (N, Empty);
14635 -- The subtype mark of a nominally unconstrained object is
14636 -- rewritten as a subtype indication using the bounds of the
14637 -- expression. Recover the original subtype mark.
14639 elsif Nkind (N2) = N_Subtype_Indication
14640 and then Is_Entity_Name (Original_Node (N2))
14642 Set_Associated_Node (N, Original_Node (N2));
14646 end Save_References_In_Identifier;
14648 ---------------------------------
14649 -- Save_References_In_Operator --
14650 ---------------------------------
14652 procedure Save_References_In_Operator (N : Node_Id) is
14654 -- The node did not undergo a transformation
14656 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14657 if Nkind (N) = N_Op_Concat then
14658 Set_Is_Component_Left_Opnd (N,
14659 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14661 Set_Is_Component_Right_Opnd (N,
14662 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14667 -- The analysis of the generic copy transformed the operator into
14668 -- some other construct. Propagate the changes to the template.
14671 N2 := Get_Associated_Node (N);
14673 -- The operator resoved to a function call
14675 if Nkind (N2) = N_Function_Call then
14676 E := Entity (Name (N2));
14678 if Present (E) and then Is_Global (E) then
14679 Set_Etype (N, Etype (N2));
14681 Set_Associated_Node (N, Empty);
14682 Set_Etype (N, Empty);
14685 -- The operator was folded into a literal
14687 elsif Nkind_In (N2, N_Integer_Literal,
14691 if Present (Original_Node (N2))
14692 and then Nkind (Original_Node (N2)) = Nkind (N)
14694 -- Operation was constant-folded. Whenever possible,
14695 -- recover semantic information from unfolded node,
14698 Set_Associated_Node (N, Original_Node (N2));
14700 if Nkind (N) = N_Op_Concat then
14701 Set_Is_Component_Left_Opnd (N,
14702 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14703 Set_Is_Component_Right_Opnd (N,
14704 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14709 -- Propagate the constant folding back to the template
14712 Rewrite (N, New_Copy (N2));
14713 Set_Analyzed (N, False);
14716 -- The operator was folded into an enumeration literal. Retain
14717 -- the entity to avoid spurious ambiguities if it is overloaded
14718 -- at the point of instantiation or inlining.
14720 elsif Nkind (N2) = N_Identifier
14721 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14723 Rewrite (N, New_Copy (N2));
14724 Set_Analyzed (N, False);
14728 -- Complete the operands check if node has not been constant
14731 if Nkind (N) in N_Op then
14732 Save_Entity_Descendants (N);
14734 end Save_References_In_Operator;
14736 -------------------------------
14737 -- Save_References_In_Pragma --
14738 -------------------------------
14740 procedure Save_References_In_Pragma (Prag : Node_Id) is
14742 Do_Save : Boolean := True;
14744 use Atree.Unchecked_Access;
14745 -- This code section is part of implementing an untyped tree
14746 -- traversal, so it needs direct access to node fields.
14749 -- Do not save global references in pragmas generated from aspects
14750 -- because the pragmas will be regenerated at instantiation time.
14752 if From_Aspect_Specification (Prag) then
14755 -- The capture of global references within contract-related source
14756 -- pragmas associated with generic packages, subprograms or their
14757 -- respective bodies must be delayed due to timing of annotation
14758 -- analysis. Global references are still captured in routine
14759 -- Save_Global_References_In_Contract.
14761 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14762 if Is_Package_Contract_Annotation (Prag) then
14763 Context := Find_Related_Package_Or_Body (Prag);
14766 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14767 Context := Find_Related_Subprogram_Or_Body (Prag);
14770 -- The use of Original_Node accounts for the case when the
14771 -- related context is generic template.
14773 if Requires_Delayed_Save (Original_Node (Context)) then
14778 -- For all other cases, save all global references within the
14779 -- descendants, but skip the following semantic fields:
14781 -- Field1 - Next_Pragma
14782 -- Field3 - Corresponding_Aspect
14783 -- Field5 - Next_Rep_Item
14786 Save_Global_Descendant (Field2 (Prag));
14787 Save_Global_Descendant (Field4 (Prag));
14789 end Save_References_In_Pragma;
14791 -- Start of processing for Save_References
14799 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14800 Save_References_In_Aggregate (N);
14802 -- Character literals, operator symbols
14804 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14805 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14807 -- Defining identifiers
14809 elsif Nkind (N) in N_Entity then
14814 elsif Nkind (N) = N_Identifier then
14815 Save_References_In_Identifier (N);
14819 elsif Nkind (N) in N_Op then
14820 Save_References_In_Operator (N);
14824 elsif Nkind (N) = N_Pragma then
14825 Save_References_In_Pragma (N);
14828 Save_References_In_Descendants (N);
14831 -- Save all global references found within the aspect specifications
14832 -- of the related node.
14834 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14836 -- The capture of global references within aspects associated with
14837 -- generic packages, subprograms or their bodies must be delayed
14838 -- due to timing of annotation analysis. Global references are
14839 -- still captured in routine Save_Global_References_In_Contract.
14841 if Requires_Delayed_Save (N) then
14844 -- Otherwise save all global references within the aspects
14847 Save_Global_References_In_Aspects (N);
14850 end Save_References;
14852 -- Start of processing for Save_Global_References
14855 Gen_Scope := Current_Scope;
14857 -- If the generic unit is a child unit, references to entities in the
14858 -- parent are treated as local, because they will be resolved anew in
14859 -- the context of the instance of the parent.
14861 while Is_Child_Unit (Gen_Scope)
14862 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14864 Gen_Scope := Scope (Gen_Scope);
14867 Save_References (Templ);
14868 end Save_Global_References;
14870 ---------------------------------------
14871 -- Save_Global_References_In_Aspects --
14872 ---------------------------------------
14874 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14879 Asp := First (Aspect_Specifications (N));
14880 while Present (Asp) loop
14881 Expr := Expression (Asp);
14883 if Present (Expr) then
14884 Save_Global_References (Expr);
14889 end Save_Global_References_In_Aspects;
14891 ----------------------------------------
14892 -- Save_Global_References_In_Contract --
14893 ----------------------------------------
14895 procedure Save_Global_References_In_Contract
14897 Gen_Id : Entity_Id)
14899 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14900 -- Save all global references in contract-related source pragmas found
14901 -- in the list starting with pragma First_Prag.
14903 ------------------------------------
14904 -- Save_Global_References_In_List --
14905 ------------------------------------
14907 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14911 Prag := First_Prag;
14912 while Present (Prag) loop
14913 if Is_Generic_Contract_Pragma (Prag) then
14914 Save_Global_References (Prag);
14917 Prag := Next_Pragma (Prag);
14919 end Save_Global_References_In_List;
14923 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14925 -- Start of processing for Save_Global_References_In_Contract
14928 -- The entity of the analyzed generic copy must be on the scope stack
14929 -- to ensure proper detection of global references.
14931 Push_Scope (Gen_Id);
14933 if Permits_Aspect_Specifications (Templ)
14934 and then Has_Aspects (Templ)
14936 Save_Global_References_In_Aspects (Templ);
14939 if Present (Items) then
14940 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14941 Save_Global_References_In_List (Contract_Test_Cases (Items));
14942 Save_Global_References_In_List (Classifications (Items));
14946 end Save_Global_References_In_Contract;
14948 --------------------------------------
14949 -- Set_Copied_Sloc_For_Inlined_Body --
14950 --------------------------------------
14952 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14954 Create_Instantiation_Source (N, E, True, S_Adjustment);
14955 end Set_Copied_Sloc_For_Inlined_Body;
14957 ---------------------
14958 -- Set_Instance_Of --
14959 ---------------------
14961 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14963 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14964 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14965 Generic_Renamings.Increment_Last;
14966 end Set_Instance_Of;
14968 --------------------
14969 -- Set_Next_Assoc --
14970 --------------------
14972 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14974 Generic_Renamings.Table (E).Next_In_HTable := Next;
14975 end Set_Next_Assoc;
14977 -------------------
14978 -- Start_Generic --
14979 -------------------
14981 procedure Start_Generic is
14983 -- ??? More things could be factored out in this routine.
14984 -- Should probably be done at a later stage.
14986 Generic_Flags.Append (Inside_A_Generic);
14987 Inside_A_Generic := True;
14989 Expander_Mode_Save_And_Set (False);
14992 ----------------------
14993 -- Set_Instance_Env --
14994 ----------------------
14996 procedure Set_Instance_Env
14997 (Gen_Unit : Entity_Id;
14998 Act_Unit : Entity_Id)
15000 Assertion_Status : constant Boolean := Assertions_Enabled;
15001 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15002 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15005 -- Regardless of the current mode, predefined units are analyzed in the
15006 -- most current Ada mode, and earlier version Ada checks do not apply
15007 -- to predefined units. Nothing needs to be done for non-internal units.
15008 -- These are always analyzed in the current mode.
15010 if Is_Internal_File_Name
15011 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15012 Renamings_Included => True)
15014 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15016 -- In Ada2012 we may want to enable assertions in an instance of a
15017 -- predefined unit, in which case we need to preserve the current
15018 -- setting for the Assertions_Enabled flag. This will become more
15019 -- critical when pre/postconditions are added to predefined units,
15020 -- as is already the case for some numeric libraries.
15022 if Ada_Version >= Ada_2012 then
15023 Assertions_Enabled := Assertion_Status;
15026 -- SPARK_Mode for an instance is the one applicable at the point of
15029 SPARK_Mode := Save_SPARK_Mode;
15030 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15032 -- Make sure dynamic elaboration checks are off in SPARK Mode
15034 if SPARK_Mode = On then
15035 Dynamic_Elaboration_Checks := False;
15039 Current_Instantiated_Parent :=
15040 (Gen_Id => Gen_Unit,
15041 Act_Id => Act_Unit,
15042 Next_In_HTable => Assoc_Null);
15043 end Set_Instance_Env;
15049 procedure Switch_View (T : Entity_Id) is
15050 BT : constant Entity_Id := Base_Type (T);
15051 Priv_Elmt : Elmt_Id := No_Elmt;
15052 Priv_Sub : Entity_Id;
15055 -- T may be private but its base type may have been exchanged through
15056 -- some other occurrence, in which case there is nothing to switch
15057 -- besides T itself. Note that a private dependent subtype of a private
15058 -- type might not have been switched even if the base type has been,
15059 -- because of the last branch of Check_Private_View (see comment there).
15061 if not Is_Private_Type (BT) then
15062 Prepend_Elmt (Full_View (T), Exchanged_Views);
15063 Exchange_Declarations (T);
15067 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15069 if Present (Full_View (BT)) then
15070 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15071 Exchange_Declarations (BT);
15074 while Present (Priv_Elmt) loop
15075 Priv_Sub := (Node (Priv_Elmt));
15077 -- We avoid flipping the subtype if the Etype of its full view is
15078 -- private because this would result in a malformed subtype. This
15079 -- occurs when the Etype of the subtype full view is the full view of
15080 -- the base type (and since the base types were just switched, the
15081 -- subtype is pointing to the wrong view). This is currently the case
15082 -- for tagged record types, access types (maybe more?) and needs to
15083 -- be resolved. ???
15085 if Present (Full_View (Priv_Sub))
15086 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15088 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15089 Exchange_Declarations (Priv_Sub);
15092 Next_Elmt (Priv_Elmt);
15100 function True_Parent (N : Node_Id) return Node_Id is
15102 if Nkind (Parent (N)) = N_Subunit then
15103 return Parent (Corresponding_Stub (Parent (N)));
15109 -----------------------------
15110 -- Valid_Default_Attribute --
15111 -----------------------------
15113 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15114 Attr_Id : constant Attribute_Id :=
15115 Get_Attribute_Id (Attribute_Name (Def));
15116 T : constant Entity_Id := Entity (Prefix (Def));
15117 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15123 if No (T) or else T = Any_Id then
15128 F := First_Formal (Nam);
15129 while Present (F) loop
15130 Num_F := Num_F + 1;
15135 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15136 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15137 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15138 Attribute_Unbiased_Rounding =>
15141 and then Is_Floating_Point_Type (T);
15143 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15144 Attribute_Value | Attribute_Wide_Image |
15145 Attribute_Wide_Value =>
15146 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15148 when Attribute_Max | Attribute_Min =>
15149 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15151 when Attribute_Input =>
15152 OK := (Is_Fun and then Num_F = 1);
15154 when Attribute_Output | Attribute_Read | Attribute_Write =>
15155 OK := (not Is_Fun and then Num_F = 2);
15163 ("attribute reference has wrong profile for subprogram", Def);
15165 end Valid_Default_Attribute;