1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
106 -- | |==============>| |
107 -- |___________| global |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
168 -- type Semi_Global is ... -- global to inner.
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 Circularity_Detected : Boolean := False;
244 -- It should really be reset upon encountering a new main unit, but in
245 -- practice we do not use multiple main units so this is not critical.
247 -----------------------------------------
248 -- Implementation of Generic Contracts --
249 -----------------------------------------
251 -- A "contract" is a collection of aspects and pragmas that either verify a
252 -- property of a construct at runtime or classify the data flow to and from
253 -- the construct in some fashion.
255 -- Generic packages, subprograms and their respective bodies may be subject
256 -- to the following contract-related aspects or pragmas collectively known
259 -- package subprogram [body]
260 -- Abstract_State Contract_Cases
261 -- Initial_Condition Depends
262 -- Initializes Extensions_Visible
265 -- Refined_State Post_Class
273 -- Subprogram_Variant
276 -- Most package contract annotations utilize forward references to classify
277 -- data declared within the package [body]. Subprogram annotations then use
278 -- the classifications to further refine them. These inter dependencies are
279 -- problematic with respect to the implementation of generics because their
280 -- analysis, capture of global references and instantiation does not mesh
281 -- well with the existing mechanism.
283 -- 1) Analysis of generic contracts is carried out the same way non-generic
284 -- contracts are analyzed:
286 -- 1.1) General rule - a contract is analyzed after all related aspects
287 -- and pragmas are analyzed. This is done by routines
289 -- Analyze_Package_Body_Contract
290 -- Analyze_Package_Contract
291 -- Analyze_Subprogram_Body_Contract
292 -- Analyze_Subprogram_Contract
294 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
297 -- 1.3) Compilation unit body - the contract is analyzed at the end of
298 -- the body declaration list.
300 -- 1.4) Package - the contract is analyzed at the end of the private or
301 -- visible declarations, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
304 -- 1.5) Package body - the contract is analyzed at the end of the body
305 -- declaration list, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
308 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
309 -- package or a subprogram, then its contract is analyzed at the end of
310 -- the enclosing declarations, otherwise the subprogram is a compilation
313 -- 1.7) Subprogram body - if the subprogram body is declared inside a
314 -- block, a package body or a subprogram body, then its contract is
315 -- analyzed at the end of the enclosing declarations, otherwise the
316 -- subprogram is a compilation unit 1.3).
318 -- 2) Capture of global references within contracts is done after capturing
319 -- global references within the generic template. There are two reasons for
320 -- this delay - pragma annotations are not part of the generic template in
321 -- the case of a generic subprogram declaration, and analysis of contracts
324 -- Contract-related source pragmas within generic templates are prepared
325 -- for delayed capture of global references by routine
327 -- Create_Generic_Contract
329 -- The routine associates these pragmas with the contract of the template.
330 -- In the case of a generic subprogram declaration, the routine creates
331 -- generic templates for the pragmas declared after the subprogram because
332 -- they are not part of the template.
334 -- generic -- template starts
335 -- procedure Gen_Proc (Input : Integer); -- template ends
336 -- pragma Precondition (Input > 0); -- requires own template
338 -- 2.1) The capture of global references with aspect specifications and
339 -- source pragmas that apply to a generic unit must be suppressed when
340 -- the generic template is being processed because the contracts have not
341 -- been analyzed yet. Any attempts to capture global references at that
342 -- point will destroy the Associated_Node linkages and leave the template
343 -- undecorated. This delay is controlled by routine
345 -- Requires_Delayed_Save
347 -- 2.2) The real capture of global references within a contract is done
348 -- after the contract has been analyzed, by routine
350 -- Save_Global_References_In_Contract
352 -- 3) The instantiation of a generic contract occurs as part of the
353 -- instantiation of the contract owner. Generic subprogram declarations
354 -- require additional processing when the contract is specified by pragmas
355 -- because the pragmas are not part of the generic template. This is done
358 -- Instantiate_Subprogram_Contract
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formals into this local package. The result is a
385 -- package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back end, so it can freely violate the placement
388 -- rules for generic formal declarations.
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formals. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted, i.e.
401 -- which still appear as generic formals.
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
407 -----------------------
408 -- Local subprograms --
409 -----------------------
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
424 -- The following procedures treat other kinds of formal parameters
426 procedure Analyze_Formal_Derived_Interface_Type
431 procedure Analyze_Formal_Derived_Type
436 procedure Analyze_Formal_Interface_Type
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Private_Type
460 -- Creates a new private type, which does not require completion
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
471 function Analyze_Associations
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
481 procedure Analyze_Subprogram_Instantiation
485 procedure Build_Instance_Compilation_Unit_Nodes
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
499 function Build_Subprogram_Decl_Wrapper
500 (Formal_Subp : Entity_Id) return Node_Id;
501 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
502 -- At the point of instantiation these contracts apply to uses of
503 -- the actual subprogram. This is implemented by creating wrapper
504 -- subprograms instead of the renamings previously used to link
505 -- formal subprograms and the corresponding actuals. If the actual
506 -- is not an entity (e.g. an attribute reference) a renaming is
507 -- created to handle the expansion of the attribute.
509 function Build_Subprogram_Body_Wrapper
510 (Formal_Subp : Entity_Id;
511 Actual_Name : Node_Id) return Node_Id;
512 -- The body of the wrapper is a call to the actual, with the generated
513 -- pre/postconditon checks added.
515 procedure Check_Access_Definition (N : Node_Id);
516 -- Subsidiary routine to null exclusion processing. Perform an assertion
517 -- check on Ada version and the presence of an access definition in N.
519 procedure Check_Formal_Packages (P_Id : Entity_Id);
520 -- Apply the following to all formal packages in generic associations.
521 -- Restore the visibility of the formals of the instance that are not
522 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
523 -- created for formal instances that are not defaulted.
525 procedure Check_Formal_Package_Instance
526 (Formal_Pack : Entity_Id;
527 Actual_Pack : Entity_Id);
528 -- Verify that the actuals of the actual instance match the actuals of
529 -- the template for a formal package that is not declared with a box.
531 procedure Check_Forward_Instantiation (Decl : Node_Id);
532 -- If the generic is a local entity and the corresponding body has not
533 -- been seen yet, flag enclosing packages to indicate that it will be
534 -- elaborated after the generic body. Subprograms declared in the same
535 -- package cannot be inlined by the front end because front-end inlining
536 -- requires a strict linear order of elaboration.
538 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
539 -- Check if some association between formals and actuals requires to make
540 -- visible primitives of a tagged type, and make those primitives visible.
541 -- Return the list of primitives whose visibility is modified (to restore
542 -- their visibility later through Restore_Hidden_Primitives). If no
543 -- candidate is found then return No_Elist.
545 procedure Check_Hidden_Child_Unit
547 Gen_Unit : Entity_Id;
548 Act_Decl_Id : Entity_Id);
549 -- If the generic unit is an implicit child instance within a parent
550 -- instance, we need to make an explicit test that it is not hidden by
551 -- a child instance of the same name and parent.
553 procedure Check_Generic_Actuals
554 (Instance : Entity_Id;
555 Is_Formal_Box : Boolean);
556 -- Similar to previous one. Check the actuals in the instantiation,
557 -- whose views can change between the point of instantiation and the point
558 -- of instantiation of the body. In addition, mark the generic renamings
559 -- as generic actuals, so that they are not compatible with other actuals.
560 -- Recurse on an actual that is a formal package whose declaration has
563 function Contains_Instance_Of
566 N : Node_Id) return Boolean;
567 -- Inner is instantiated within the generic Outer. Check whether Inner
568 -- directly or indirectly contains an instance of Outer or of one of its
569 -- parents, in the case of a subunit. Each generic unit holds a list of
570 -- the entities instantiated within (at any depth). This procedure
571 -- determines whether the set of such lists contains a cycle, i.e. an
572 -- illegal circular instantiation.
574 function Denotes_Formal_Package
576 On_Exit : Boolean := False;
577 Instance : Entity_Id := Empty) return Boolean;
578 -- Returns True if E is a formal package of an enclosing generic, or
579 -- the actual for such a formal in an enclosing instantiation. If such
580 -- a package is used as a formal in an nested generic, or as an actual
581 -- in a nested instantiation, the visibility of ITS formals should not
582 -- be modified. When called from within Restore_Private_Views, the flag
583 -- On_Exit is true, to indicate that the search for a possible enclosing
584 -- instance should ignore the current one. In that case Instance denotes
585 -- the declaration for which this is an actual. This declaration may be
586 -- an instantiation in the source, or the internal instantiation that
587 -- corresponds to the actual for a formal package.
589 function Earlier (N1, N2 : Node_Id) return Boolean;
590 -- Yields True if N1 and N2 appear in the same compilation unit,
591 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
592 -- traversal of the tree for the unit. Used to determine the placement
593 -- of freeze nodes for instance bodies that may depend on other instances.
595 function Find_Actual_Type
597 Gen_Type : Entity_Id) return Entity_Id;
598 -- When validating the actual types of a child instance, check whether
599 -- the formal is a formal type of the parent unit, and retrieve the current
600 -- actual for it. Typ is the entity in the analyzed formal type declaration
601 -- (component or index type of an array type, or designated type of an
602 -- access formal) and Gen_Type is the enclosing analyzed formal array
603 -- or access type. The desired actual may be a formal of a parent, or may
604 -- be declared in a formal package of a parent. In both cases it is a
605 -- generic actual type because it appears within a visible instance.
606 -- Finally, it may be declared in a parent unit without being a formal
607 -- of that unit, in which case it must be retrieved by visibility.
608 -- Ambiguities may still arise if two homonyms are declared in two formal
609 -- packages, and the prefix of the formal type may be needed to resolve
610 -- the ambiguity in the instance ???
612 procedure Freeze_Subprogram_Body
613 (Inst_Node : Node_Id;
615 Pack_Id : Entity_Id);
616 -- The generic body may appear textually after the instance, including
617 -- in the proper body of a stub, or within a different package instance.
618 -- Given that the instance can only be elaborated after the generic, we
619 -- place freeze_nodes for the instance and/or for packages that may enclose
620 -- the instance and the generic, so that the back-end can establish the
621 -- proper order of elaboration.
623 function Get_Associated_Node (N : Node_Id) return Node_Id;
624 -- In order to propagate semantic information back from the analyzed copy
625 -- to the original generic, we maintain links between selected nodes in the
626 -- generic and their corresponding copies. At the end of generic analysis,
627 -- the routine Save_Global_References traverses the generic tree, examines
628 -- the semantic information, and preserves the links to those nodes that
629 -- contain global information. At instantiation, the information from the
630 -- associated node is placed on the new copy, so that name resolution is
633 -- Three kinds of source nodes have associated nodes:
635 -- a) those that can reference (denote) entities, that is identifiers,
636 -- character literals, expanded_names, operator symbols, operators,
637 -- and attribute reference nodes. These nodes have an Entity field
638 -- and are the set of nodes that are in N_Has_Entity.
640 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
642 -- c) selected components (N_Selected_Component)
644 -- For the first class, the associated node preserves the entity if it is
645 -- global. If the generic contains nested instantiations, the associated
646 -- node itself has been recopied, and a chain of them must be followed.
648 -- For aggregates, the associated node allows retrieval of the type, which
649 -- may otherwise not appear in the generic. The view of this type may be
650 -- different between generic and instantiation, and the full view can be
651 -- installed before the instantiation is analyzed. For aggregates of type
652 -- extensions, the same view exchange may have to be performed for some of
653 -- the ancestor types, if their view is private at the point of
656 -- Nodes that are selected components in the parse tree may be rewritten
657 -- as expanded names after resolution, and must be treated as potential
658 -- entity holders, which is why they also have an Associated_Node.
660 -- Nodes that do not come from source, such as freeze nodes, do not appear
661 -- in the generic tree, and need not have an associated node.
663 -- The associated node is stored in the Associated_Node field. Note that
664 -- this field overlaps Entity, which is fine, because the whole point is
665 -- that we don't need or want the normal Entity field in this situation.
667 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
668 -- Traverse the Exchanged_Views list to see if a type was private
669 -- and has already been flipped during this phase of instantiation.
671 function Has_Contracts (Decl : Node_Id) return Boolean;
672 -- Determine whether a formal subprogram has a Pre- or Postcondition,
673 -- in which case a subprogram wrapper has to be built for the actual.
675 procedure Hide_Current_Scope;
676 -- When instantiating a generic child unit, the parent context must be
677 -- present, but the instance and all entities that may be generated
678 -- must be inserted in the current scope. We leave the current scope
679 -- on the stack, but make its entities invisible to avoid visibility
680 -- problems. This is reversed at the end of the instantiation. This is
681 -- not done for the instantiation of the bodies, which only require the
682 -- instances of the generic parents to be in scope.
684 function In_Main_Context (E : Entity_Id) return Boolean;
685 -- Check whether an instantiation is in the context of the main unit.
686 -- Used to determine whether its body should be elaborated to allow
687 -- front-end inlining.
689 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
690 -- Add the context clause of the unit containing a generic unit to a
691 -- compilation unit that is, or contains, an instantiation.
694 -- Establish environment for subsequent instantiation. Separated from
695 -- Save_Env because data-structures for visibility handling must be
696 -- initialized before call to Check_Generic_Child_Unit.
698 procedure Inline_Instance_Body
700 Gen_Unit : Entity_Id;
702 -- If front-end inlining is requested, instantiate the package body,
703 -- and preserve the visibility of its compilation unit, to insure
704 -- that successive instantiations succeed.
706 procedure Insert_Freeze_Node_For_Instance
709 -- N denotes a package or a subprogram instantiation and F_Node is the
710 -- associated freeze node. Insert the freeze node before the first source
711 -- body which follows immediately after N. If no such body is found, the
712 -- freeze node is inserted at the end of the declarative region which
715 procedure Install_Body
720 -- If the instantiation happens textually before the body of the generic,
721 -- the instantiation of the body must be analyzed after the generic body,
722 -- and not at the point of instantiation. Such early instantiations can
723 -- happen if the generic and the instance appear in a package declaration
724 -- because the generic body can only appear in the corresponding package
725 -- body. Early instantiations can also appear if generic, instance and
726 -- body are all in the declarative part of a subprogram or entry. Entities
727 -- of packages that are early instantiations are delayed, and their freeze
728 -- node appears after the generic body. This rather complex machinery is
729 -- needed when nested instantiations are present, because the source does
730 -- not carry any indication of where the corresponding instance bodies must
731 -- be installed and frozen.
733 procedure Install_Formal_Packages (Par : Entity_Id);
734 -- Install the visible part of any formal of the parent that is a formal
735 -- package. Note that for the case of a formal package with a box, this
736 -- includes the formal part of the formal package (12.7(10/2)).
738 procedure Install_Hidden_Primitives
739 (Prims_List : in out Elist_Id;
742 -- Remove suffix 'P' from hidden primitives of Act_T to match the
743 -- visibility of primitives of Gen_T. The list of primitives to which
744 -- the suffix is removed is added to Prims_List to restore them later.
746 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
747 -- When compiling an instance of a child unit the parent (which is
748 -- itself an instance) is an enclosing scope that must be made
749 -- immediately visible. This procedure is also used to install the non-
750 -- generic parent of a generic child unit when compiling its body, so
751 -- that full views of types in the parent are made visible.
753 -- The functions Instantiate_XXX perform various legality checks and build
754 -- the declarations for instantiated generic parameters. In all of these
755 -- Formal is the entity in the generic unit, Actual is the entity of
756 -- expression in the generic associations, and Analyzed_Formal is the
757 -- formal in the generic copy, which contains the semantic information to
758 -- be used to validate the actual.
760 function Instantiate_Object
763 Analyzed_Formal : Node_Id) return List_Id;
765 function Instantiate_Type
768 Analyzed_Formal : Node_Id;
769 Actual_Decls : List_Id) return List_Id;
771 function Instantiate_Formal_Subprogram
774 Analyzed_Formal : Node_Id) return Node_Id;
776 function Instantiate_Formal_Package
779 Analyzed_Formal : Node_Id) return List_Id;
780 -- If the formal package is declared with a box, special visibility rules
781 -- apply to its formals: they are in the visible part of the package. This
782 -- is true in the declarative region of the formal package, that is to say
783 -- in the enclosing generic or instantiation. For an instantiation, the
784 -- parameters of the formal package are made visible in an explicit step.
785 -- Furthermore, if the actual has a visible USE clause, these formals must
786 -- be made potentially use-visible as well. On exit from the enclosing
787 -- instantiation, the reverse must be done.
789 -- For a formal package declared without a box, there are conformance rules
790 -- that apply to the actuals in the generic declaration and the actuals of
791 -- the actual package in the enclosing instantiation. The simplest way to
792 -- apply these rules is to repeat the instantiation of the formal package
793 -- in the context of the enclosing instance, and compare the generic
794 -- associations of this instantiation with those of the actual package.
795 -- This internal instantiation only needs to contain the renamings of the
796 -- formals: the visible and private declarations themselves need not be
799 -- In Ada 2005, the formal package may be only partially parameterized.
800 -- In that case the visibility step must make visible those actuals whose
801 -- corresponding formals were given with a box. A final complication
802 -- involves inherited operations from formal derived types, which must
803 -- be visible if the type is.
805 function Is_In_Main_Unit (N : Node_Id) return Boolean;
806 -- Test if given node is in the main unit
808 procedure Load_Parent_Of_Generic
811 Body_Optional : Boolean := False);
812 -- If the generic appears in a separate non-generic library unit, load the
813 -- corresponding body to retrieve the body of the generic. N is the node
814 -- for the generic instantiation, Spec is the generic package declaration.
816 -- Body_Optional is a flag that indicates that the body is being loaded to
817 -- ensure that temporaries are generated consistently when there are other
818 -- instances in the current declarative part that precede the one being
819 -- loaded. In that case a missing body is acceptable.
821 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
822 -- Within the generic part, entities in the formal package are
823 -- visible. To validate subsequent type declarations, indicate
824 -- the correspondence between the entities in the analyzed formal,
825 -- and the entities in the actual package. There are three packages
826 -- involved in the instantiation of a formal package: the parent
827 -- generic P1 which appears in the generic declaration, the fake
828 -- instantiation P2 which appears in the analyzed generic, and whose
829 -- visible entities may be used in subsequent formals, and the actual
830 -- P3 in the instance. To validate subsequent formals, me indicate
831 -- that the entities in P2 are mapped into those of P3. The mapping of
832 -- entities has to be done recursively for nested packages.
834 procedure Move_Freeze_Nodes
838 -- Freeze nodes can be generated in the analysis of a generic unit, but
839 -- will not be seen by the back-end. It is necessary to move those nodes
840 -- to the enclosing scope if they freeze an outer entity. We place them
841 -- at the end of the enclosing generic package, which is semantically
844 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
845 -- Analyze actuals to perform name resolution. Full resolution is done
846 -- later, when the expected types are known, but names have to be captured
847 -- before installing parents of generics, that are not visible for the
848 -- actuals themselves.
850 -- If Inst is present, it is the entity of the package instance. This
851 -- entity is marked as having a limited_view actual when some actual is
852 -- a limited view. This is used to place the instance body properly.
854 procedure Provide_Completing_Bodies (N : Node_Id);
855 -- Generate completing bodies for all subprograms found within package or
856 -- subprogram declaration N.
858 procedure Remove_Parent (In_Body : Boolean := False);
859 -- Reverse effect after instantiation of child is complete
861 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
862 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
865 procedure Set_Instance_Env
866 (Gen_Unit : Entity_Id;
867 Act_Unit : Entity_Id);
868 -- Save current instance on saved environment, to be used to determine
869 -- the global status of entities in nested instances. Part of Save_Env.
870 -- called after verifying that the generic unit is legal for the instance,
871 -- The procedure also examines whether the generic unit is a predefined
872 -- unit, in order to set configuration switches accordingly. As a result
873 -- the procedure must be called after analyzing and freezing the actuals.
875 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
876 -- Associate analyzed generic parameter with corresponding instance. Used
877 -- for semantic checks at instantiation time.
879 function True_Parent (N : Node_Id) return Node_Id;
880 -- For a subunit, return parent of corresponding stub, else return
883 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
884 -- Verify that an attribute that appears as the default for a formal
885 -- subprogram is a function or procedure with the correct profile.
887 -------------------------------------------
888 -- Data Structures for Generic Renamings --
889 -------------------------------------------
891 -- The map Generic_Renamings associates generic entities with their
892 -- corresponding actuals. Currently used to validate type instances. It
893 -- will eventually be used for all generic parameters to eliminate the
894 -- need for overload resolution in the instance.
896 type Assoc_Ptr is new Int;
898 Assoc_Null : constant Assoc_Ptr := -1;
903 Next_In_HTable : Assoc_Ptr;
906 package Generic_Renamings is new Table.Table
907 (Table_Component_Type => Assoc,
908 Table_Index_Type => Assoc_Ptr,
909 Table_Low_Bound => 0,
911 Table_Increment => 100,
912 Table_Name => "Generic_Renamings");
914 -- Variable to hold enclosing instantiation. When the environment is
915 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
917 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
919 -- Hash table for associations
921 HTable_Size : constant := 37;
922 type HTable_Range is range 0 .. HTable_Size - 1;
924 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
925 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
926 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
927 function Hash (F : Entity_Id) return HTable_Range;
929 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
930 Header_Num => HTable_Range,
932 Elmt_Ptr => Assoc_Ptr,
933 Null_Ptr => Assoc_Null,
934 Set_Next => Set_Next_Assoc,
937 Get_Key => Get_Gen_Id,
941 Exchanged_Views : Elist_Id;
942 -- This list holds the private views that have been exchanged during
943 -- instantiation to restore the visibility of the generic declaration.
944 -- (see comments above). After instantiation, the current visibility is
945 -- reestablished by means of a traversal of this list.
947 Hidden_Entities : Elist_Id;
948 -- This list holds the entities of the current scope that are removed
949 -- from immediate visibility when instantiating a child unit. Their
950 -- visibility is restored in Remove_Parent.
952 -- Because instantiations can be recursive, the following must be saved
953 -- on entry and restored on exit from an instantiation (spec or body).
954 -- This is done by the two procedures Save_Env and Restore_Env. For
955 -- package and subprogram instantiations (but not for the body instances)
956 -- the action of Save_Env is done in two steps: Init_Env is called before
957 -- Check_Generic_Child_Unit, because setting the parent instances requires
958 -- that the visibility data structures be properly initialized. Once the
959 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
961 Parent_Unit_Visible : Boolean := False;
962 -- Parent_Unit_Visible is used when the generic is a child unit, and
963 -- indicates whether the ultimate parent of the generic is visible in the
964 -- instantiation environment. It is used to reset the visibility of the
965 -- parent at the end of the instantiation (see Remove_Parent).
967 Instance_Parent_Unit : Entity_Id := Empty;
968 -- This records the ultimate parent unit of an instance of a generic
969 -- child unit and is used in conjunction with Parent_Unit_Visible to
970 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
972 type Instance_Env is record
973 Instantiated_Parent : Assoc;
974 Exchanged_Views : Elist_Id;
975 Hidden_Entities : Elist_Id;
976 Current_Sem_Unit : Unit_Number_Type;
977 Parent_Unit_Visible : Boolean := False;
978 Instance_Parent_Unit : Entity_Id := Empty;
979 Switches : Config_Switches_Type;
982 package Instance_Envs is new Table.Table (
983 Table_Component_Type => Instance_Env,
984 Table_Index_Type => Int,
985 Table_Low_Bound => 0,
987 Table_Increment => 100,
988 Table_Name => "Instance_Envs");
990 procedure Restore_Private_Views
991 (Pack_Id : Entity_Id;
992 Is_Package : Boolean := True);
993 -- Restore the private views of external types, and unmark the generic
994 -- renamings of actuals, so that they become compatible subtypes again.
995 -- For subprograms, Pack_Id is the package constructed to hold the
998 procedure Switch_View (T : Entity_Id);
999 -- Switch the partial and full views of a type and its private
1000 -- dependents (i.e. its subtypes and derived types).
1002 ------------------------------------
1003 -- Structures for Error Reporting --
1004 ------------------------------------
1006 Instantiation_Node : Node_Id;
1007 -- Used by subprograms that validate instantiation of formal parameters
1008 -- where there might be no actual on which to place the error message.
1009 -- Also used to locate the instantiation node for generic subunits.
1011 Instantiation_Error : exception;
1012 -- When there is a semantic error in the generic parameter matching,
1013 -- there is no point in continuing the instantiation, because the
1014 -- number of cascaded errors is unpredictable. This exception aborts
1015 -- the instantiation process altogether.
1017 S_Adjustment : Sloc_Adjustment;
1018 -- Offset created for each node in an instantiation, in order to keep
1019 -- track of the source position of the instantiation in each of its nodes.
1020 -- A subsequent semantic error or warning on a construct of the instance
1021 -- points to both places: the original generic node, and the point of
1022 -- instantiation. See Sinput and Sinput.L for additional details.
1024 ------------------------------------------------------------
1025 -- Data structure for keeping track when inside a Generic --
1026 ------------------------------------------------------------
1028 -- The following table is used to save values of the Inside_A_Generic
1029 -- flag (see spec of Sem) when they are saved by Start_Generic.
1031 package Generic_Flags is new Table.Table (
1032 Table_Component_Type => Boolean,
1033 Table_Index_Type => Int,
1034 Table_Low_Bound => 0,
1035 Table_Initial => 32,
1036 Table_Increment => 200,
1037 Table_Name => "Generic_Flags");
1039 ---------------------------
1040 -- Abandon_Instantiation --
1041 ---------------------------
1043 procedure Abandon_Instantiation (N : Node_Id) is
1045 Error_Msg_N ("\instantiation abandoned!", N);
1046 raise Instantiation_Error;
1047 end Abandon_Instantiation;
1049 ----------------------------------
1050 -- Adjust_Inherited_Pragma_Sloc --
1051 ----------------------------------
1053 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1055 Adjust_Instantiation_Sloc (N, S_Adjustment);
1056 end Adjust_Inherited_Pragma_Sloc;
1058 --------------------------
1059 -- Analyze_Associations --
1060 --------------------------
1062 function Analyze_Associations
1065 F_Copy : List_Id) return List_Id
1067 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1068 Assoc_List : constant List_Id := New_List;
1069 Default_Actuals : constant List_Id := New_List;
1070 Gen_Unit : constant Entity_Id :=
1071 Defining_Entity (Parent (F_Copy));
1075 Analyzed_Formal : Node_Id;
1076 First_Named : Node_Id := Empty;
1080 Saved_Formal : Node_Id;
1082 Default_Formals : constant List_Id := New_List;
1083 -- If an Others_Choice is present, some of the formals may be defaulted.
1084 -- To simplify the treatment of visibility in an instance, we introduce
1085 -- individual defaults for each such formal. These defaults are
1086 -- appended to the list of associations and replace the Others_Choice.
1088 Found_Assoc : Node_Id;
1089 -- Association for the current formal being match. Empty if there are
1090 -- no remaining actuals, or if there is no named association with the
1091 -- name of the formal.
1093 Is_Named_Assoc : Boolean;
1094 Num_Matched : Nat := 0;
1095 Num_Actuals : Nat := 0;
1097 Others_Present : Boolean := False;
1098 Others_Choice : Node_Id := Empty;
1099 -- In Ada 2005, indicates partial parameterization of a formal
1100 -- package. As usual an other association must be last in the list.
1102 procedure Build_Subprogram_Wrappers;
1103 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1104 -- subprograms. The implementation of making the formal into a renaming
1105 -- of the actual does not work, given that subprogram renaming cannot
1106 -- carry aspect specifications. Instead we must create subprogram
1107 -- wrappers whose body is a call to the actual, and whose declaration
1108 -- carries the aspects of the formal.
1110 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1111 -- Warn if an actual fixed-point type has user-defined arithmetic
1112 -- operations, but there is no corresponding formal in the generic,
1113 -- in which case the predefined operations will be used. This merits
1114 -- a warning because of the special semantics of fixed point ops.
1116 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1117 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1118 -- cannot have a named association for it. AI05-0025 extends this rule
1119 -- to formals of formal packages by AI05-0025, and it also applies to
1120 -- box-initialized formals.
1122 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1123 -- Determine whether the parameter types and the return type of Subp
1124 -- are fully defined at the point of instantiation.
1126 function Matching_Actual
1128 A_F : Entity_Id) return Node_Id;
1129 -- Find actual that corresponds to a given a formal parameter. If the
1130 -- actuals are positional, return the next one, if any. If the actuals
1131 -- are named, scan the parameter associations to find the right one.
1132 -- A_F is the corresponding entity in the analyzed generic, which is
1133 -- placed on the selector name.
1135 -- In Ada 2005, a named association may be given with a box, in which
1136 -- case Matching_Actual sets Found_Assoc to the generic association,
1137 -- but return Empty for the actual itself. In this case the code below
1138 -- creates a corresponding declaration for the formal.
1140 function Partial_Parameterization return Boolean;
1141 -- Ada 2005: if no match is found for a given formal, check if the
1142 -- association for it includes a box, or whether the associations
1143 -- include an Others clause.
1145 procedure Process_Default (F : Entity_Id);
1146 -- Add a copy of the declaration of generic formal F to the list of
1147 -- associations, and add an explicit box association for F if there
1148 -- is none yet, and the default comes from an Others_Choice.
1150 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1151 -- Determine whether Subp renames one of the subprograms defined in the
1152 -- generated package Standard.
1154 procedure Set_Analyzed_Formal;
1155 -- Find the node in the generic copy that corresponds to a given formal.
1156 -- The semantic information on this node is used to perform legality
1157 -- checks on the actuals. Because semantic analysis can introduce some
1158 -- anonymous entities or modify the declaration node itself, the
1159 -- correspondence between the two lists is not one-one. In addition to
1160 -- anonymous types, the presence a formal equality will introduce an
1161 -- implicit declaration for the corresponding inequality.
1163 -----------------------------------------
1164 -- procedure Build_Subprogram_Wrappers --
1165 -----------------------------------------
1167 procedure Build_Subprogram_Wrappers is
1168 Formal : constant Entity_Id :=
1169 Defining_Unit_Name (Specification (Analyzed_Formal));
1170 Aspect_Spec : Node_Id;
1171 Decl_Node : Node_Id;
1172 Actual_Name : Node_Id;
1175 -- Create declaration for wrapper subprogram
1176 -- The actual can be overloaded, in which case it will be
1177 -- resolved when the call in the wrapper body is analyzed.
1178 -- We attach the possible interpretations of the actual to
1179 -- the name to be used in the call in the wrapper body.
1181 if Is_Entity_Name (Match) then
1182 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1184 if Is_Overloaded (Match) then
1185 Save_Interps (Match, Actual_Name);
1189 -- Use renaming declaration created when analyzing actual.
1190 -- This may be incomplete if there are several formal
1191 -- subprograms whose actual is an attribute ???
1194 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1197 Actual_Name := New_Occurrence_Of
1198 (Defining_Entity (Renaming_Decl), Sloc (Match));
1199 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1203 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1205 -- Transfer aspect specifications from formal subprogram to wrapper
1207 Set_Aspect_Specifications (Decl_Node,
1208 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1210 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1211 while Present (Aspect_Spec) loop
1212 Set_Analyzed (Aspect_Spec, False);
1216 Append_To (Assoc_List, Decl_Node);
1218 -- Create corresponding body, and append it to association list
1219 -- that appears at the head of the declarations in the instance.
1220 -- The subprogram may be called in the analysis of subsequent
1223 Append_To (Assoc_List,
1224 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1225 end Build_Subprogram_Wrappers;
1227 ----------------------------------------
1228 -- Check_Overloaded_Formal_Subprogram --
1229 ----------------------------------------
1231 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1232 Temp_Formal : Entity_Id;
1235 Temp_Formal := First (Formals);
1236 while Present (Temp_Formal) loop
1237 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1238 and then Temp_Formal /= Formal
1240 Chars (Defining_Unit_Name (Specification (Formal))) =
1241 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1243 if Present (Found_Assoc) then
1245 ("named association not allowed for overloaded formal",
1250 ("named association not allowed for overloaded formal",
1254 Abandon_Instantiation (Instantiation_Node);
1259 end Check_Overloaded_Formal_Subprogram;
1261 -------------------------------
1262 -- Check_Fixed_Point_Actual --
1263 -------------------------------
1265 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1266 Typ : constant Entity_Id := Entity (Actual);
1267 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1273 -- Locate primitive operations of the type that are arithmetic
1276 Elem := First_Elmt (Prims);
1277 while Present (Elem) loop
1278 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1280 -- Check whether the generic unit has a formal subprogram of
1281 -- the same name. This does not check types but is good enough
1282 -- to justify a warning.
1284 Formal := First_Non_Pragma (Formals);
1285 Op := Alias (Node (Elem));
1287 while Present (Formal) loop
1288 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1289 and then Chars (Defining_Entity (Formal)) =
1294 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1300 -- Locate corresponding actual, and check whether it
1301 -- includes a fixed-point type.
1303 Assoc := First (Assoc_List);
1304 while Present (Assoc) loop
1306 Nkind (Assoc) = N_Package_Renaming_Declaration
1307 and then Chars (Defining_Unit_Name (Assoc)) =
1308 Chars (Defining_Identifier (Formal));
1313 if Present (Assoc) then
1315 -- If formal package declares a fixed-point type,
1316 -- and the user-defined operator is derived from
1317 -- a generic instance package, the fixed-point type
1318 -- does not use the corresponding predefined op.
1320 Ent := First_Entity (Entity (Name (Assoc)));
1321 while Present (Ent) loop
1322 if Is_Fixed_Point_Type (Ent)
1323 and then Present (Op)
1324 and then Is_Generic_Instance (Scope (Op))
1339 Error_Msg_Sloc := Sloc (Node (Elem));
1341 ("?instance uses predefined operation, not primitive "
1342 & "operation&#", Actual, Node (Elem));
1348 end Check_Fixed_Point_Actual;
1350 -------------------------------
1351 -- Has_Fully_Defined_Profile --
1352 -------------------------------
1354 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1355 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1356 -- Determine whethet type Typ is fully defined
1358 ---------------------------
1359 -- Is_Fully_Defined_Type --
1360 ---------------------------
1362 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1364 -- A private type without a full view is not fully defined
1366 if Is_Private_Type (Typ)
1367 and then No (Full_View (Typ))
1371 -- An incomplete type is never fully defined
1373 elsif Is_Incomplete_Type (Typ) then
1376 -- All other types are fully defined
1381 end Is_Fully_Defined_Type;
1383 -- Local declarations
1387 -- Start of processing for Has_Fully_Defined_Profile
1390 -- Check the parameters
1392 Param := First_Formal (Subp);
1393 while Present (Param) loop
1394 if not Is_Fully_Defined_Type (Etype (Param)) then
1398 Next_Formal (Param);
1401 -- Check the return type
1403 return Is_Fully_Defined_Type (Etype (Subp));
1404 end Has_Fully_Defined_Profile;
1406 ---------------------
1407 -- Matching_Actual --
1408 ---------------------
1410 function Matching_Actual
1412 A_F : Entity_Id) return Node_Id
1418 Is_Named_Assoc := False;
1420 -- End of list of purely positional parameters
1422 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1423 Found_Assoc := Empty;
1426 -- Case of positional parameter corresponding to current formal
1428 elsif No (Selector_Name (Actual)) then
1429 Found_Assoc := Actual;
1430 Act := Explicit_Generic_Actual_Parameter (Actual);
1431 Num_Matched := Num_Matched + 1;
1434 -- Otherwise scan list of named actuals to find the one with the
1435 -- desired name. All remaining actuals have explicit names.
1438 Is_Named_Assoc := True;
1439 Found_Assoc := Empty;
1443 while Present (Actual) loop
1444 if Nkind (Actual) = N_Others_Choice then
1445 Found_Assoc := Empty;
1448 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1449 Set_Entity (Selector_Name (Actual), A_F);
1450 Set_Etype (Selector_Name (Actual), Etype (A_F));
1451 Generate_Reference (A_F, Selector_Name (Actual));
1453 Found_Assoc := Actual;
1454 Act := Explicit_Generic_Actual_Parameter (Actual);
1455 Num_Matched := Num_Matched + 1;
1463 -- Reset for subsequent searches. In most cases the named
1464 -- associations are in order. If they are not, we reorder them
1465 -- to avoid scanning twice the same actual. This is not just a
1466 -- question of efficiency: there may be multiple defaults with
1467 -- boxes that have the same name. In a nested instantiation we
1468 -- insert actuals for those defaults, and cannot rely on their
1469 -- names to disambiguate them.
1471 if Actual = First_Named then
1474 elsif Present (Actual) then
1475 Insert_Before (First_Named, Remove_Next (Prev));
1478 Actual := First_Named;
1481 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1482 Set_Used_As_Generic_Actual (Entity (Act));
1486 end Matching_Actual;
1488 ------------------------------
1489 -- Partial_Parameterization --
1490 ------------------------------
1492 function Partial_Parameterization return Boolean is
1494 return Others_Present
1495 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1496 end Partial_Parameterization;
1498 ---------------------
1499 -- Process_Default --
1500 ---------------------
1502 procedure Process_Default (F : Entity_Id) is
1503 Loc : constant Source_Ptr := Sloc (I_Node);
1504 F_Id : constant Entity_Id := Defining_Entity (F);
1510 -- Append copy of formal declaration to associations, and create new
1511 -- defining identifier for it.
1513 Decl := New_Copy_Tree (F);
1514 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1516 if Nkind (F) in N_Formal_Subprogram_Declaration then
1517 Set_Defining_Unit_Name (Specification (Decl), Id);
1520 Set_Defining_Identifier (Decl, Id);
1523 Append (Decl, Assoc_List);
1525 if No (Found_Assoc) then
1527 Make_Generic_Association (Loc,
1529 New_Occurrence_Of (Id, Loc),
1530 Explicit_Generic_Actual_Parameter => Empty);
1531 Set_Box_Present (Default);
1532 Append (Default, Default_Formals);
1534 end Process_Default;
1536 ---------------------------------
1537 -- Renames_Standard_Subprogram --
1538 ---------------------------------
1540 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1545 while Present (Id) loop
1546 if Scope (Id) = Standard_Standard then
1554 end Renames_Standard_Subprogram;
1556 -------------------------
1557 -- Set_Analyzed_Formal --
1558 -------------------------
1560 procedure Set_Analyzed_Formal is
1564 while Present (Analyzed_Formal) loop
1565 Kind := Nkind (Analyzed_Formal);
1567 case Nkind (Formal) is
1568 when N_Formal_Subprogram_Declaration =>
1569 exit when Kind in N_Formal_Subprogram_Declaration
1572 (Defining_Unit_Name (Specification (Formal))) =
1574 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1576 when N_Formal_Package_Declaration =>
1577 exit when Kind in N_Formal_Package_Declaration
1578 | N_Generic_Package_Declaration
1579 | N_Package_Declaration;
1581 when N_Use_Package_Clause
1588 -- Skip freeze nodes, and nodes inserted to replace
1589 -- unrecognized pragmas.
1592 Kind not in N_Formal_Subprogram_Declaration
1593 and then Kind not in N_Subprogram_Declaration
1597 and then Chars (Defining_Identifier (Formal)) =
1598 Chars (Defining_Identifier (Analyzed_Formal));
1601 Next (Analyzed_Formal);
1603 end Set_Analyzed_Formal;
1605 -- Start of processing for Analyze_Associations
1608 Actuals := Generic_Associations (I_Node);
1610 if Present (Actuals) then
1612 -- Check for an Others choice, indicating a partial parameterization
1613 -- for a formal package.
1615 Actual := First (Actuals);
1616 while Present (Actual) loop
1617 if Nkind (Actual) = N_Others_Choice then
1618 Others_Present := True;
1619 Others_Choice := Actual;
1621 if Present (Next (Actual)) then
1622 Error_Msg_N ("others must be last association", Actual);
1625 -- This subprogram is used both for formal packages and for
1626 -- instantiations. For the latter, associations must all be
1629 if Nkind (I_Node) /= N_Formal_Package_Declaration
1630 and then Comes_From_Source (I_Node)
1633 ("others association not allowed in an instance",
1637 -- In any case, nothing to do after the others association
1641 elsif Box_Present (Actual)
1642 and then Comes_From_Source (I_Node)
1643 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1646 ("box association not allowed in an instance", Actual);
1652 -- If named associations are present, save first named association
1653 -- (it may of course be Empty) to facilitate subsequent name search.
1655 First_Named := First (Actuals);
1656 while Present (First_Named)
1657 and then Nkind (First_Named) /= N_Others_Choice
1658 and then No (Selector_Name (First_Named))
1660 Num_Actuals := Num_Actuals + 1;
1665 Named := First_Named;
1666 while Present (Named) loop
1667 if Nkind (Named) /= N_Others_Choice
1668 and then No (Selector_Name (Named))
1670 Error_Msg_N ("invalid positional actual after named one", Named);
1671 Abandon_Instantiation (Named);
1674 -- A named association may lack an actual parameter, if it was
1675 -- introduced for a default subprogram that turns out to be local
1676 -- to the outer instantiation. If it has a box association it must
1677 -- correspond to some formal in the generic.
1679 if Nkind (Named) /= N_Others_Choice
1680 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1681 or else Box_Present (Named))
1683 Num_Actuals := Num_Actuals + 1;
1689 if Present (Formals) then
1690 Formal := First_Non_Pragma (Formals);
1691 Analyzed_Formal := First_Non_Pragma (F_Copy);
1693 if Present (Actuals) then
1694 Actual := First (Actuals);
1696 -- All formals should have default values
1702 while Present (Formal) loop
1703 Set_Analyzed_Formal;
1704 Saved_Formal := Next_Non_Pragma (Formal);
1706 case Nkind (Formal) is
1707 when N_Formal_Object_Declaration =>
1710 (Defining_Identifier (Formal),
1711 Defining_Identifier (Analyzed_Formal));
1713 if No (Match) and then Partial_Parameterization then
1714 Process_Default (Formal);
1718 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1721 -- For a defaulted in_parameter, create an entry in the
1722 -- the list of defaulted actuals, for GNATprove use. Do
1723 -- not included these defaults for an instance nested
1724 -- within a generic, because the defaults are also used
1725 -- in the analysis of the enclosing generic, and only
1726 -- defaulted subprograms are relevant there.
1728 if No (Match) and then not Inside_A_Generic then
1729 Append_To (Default_Actuals,
1730 Make_Generic_Association (Sloc (I_Node),
1733 (Defining_Identifier (Formal), Sloc (I_Node)),
1734 Explicit_Generic_Actual_Parameter =>
1735 New_Copy_Tree (Default_Expression (Formal))));
1739 -- If the object is a call to an expression function, this
1740 -- is a freezing point for it.
1742 if Is_Entity_Name (Match)
1743 and then Present (Entity (Match))
1745 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1746 = N_Expression_Function
1748 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1751 when N_Formal_Type_Declaration =>
1754 (Defining_Identifier (Formal),
1755 Defining_Identifier (Analyzed_Formal));
1758 if Partial_Parameterization then
1759 Process_Default (Formal);
1762 Error_Msg_Sloc := Sloc (Gen_Unit);
1765 Instantiation_Node, Defining_Identifier (Formal));
1767 ("\in instantiation of & declared#",
1768 Instantiation_Node, Gen_Unit);
1769 Abandon_Instantiation (Instantiation_Node);
1776 (Formal, Match, Analyzed_Formal, Assoc_List),
1779 -- Warn when an actual is a fixed-point with user-
1780 -- defined promitives. The warning is superfluous
1781 -- if the formal is private, because there can be
1782 -- no arithmetic operations in the generic so there
1783 -- no danger of confusion.
1785 if Is_Fixed_Point_Type (Entity (Match))
1786 and then not Is_Private_Type
1787 (Defining_Identifier (Analyzed_Formal))
1789 Check_Fixed_Point_Actual (Match);
1792 -- An instantiation is a freeze point for the actuals,
1793 -- unless this is a rewritten formal package, or the
1794 -- formal is an Ada 2012 formal incomplete type.
1796 if Nkind (I_Node) = N_Formal_Package_Declaration
1798 (Ada_Version >= Ada_2012
1800 Ekind (Defining_Identifier (Analyzed_Formal)) =
1806 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1810 -- A remote access-to-class-wide type is not a legal actual
1811 -- for a generic formal of an access type (E.2.2(17/2)).
1812 -- In GNAT an exception to this rule is introduced when
1813 -- the formal is marked as remote using implementation
1814 -- defined aspect/pragma Remote_Access_Type. In that case
1815 -- the actual must be remote as well.
1817 -- If the current instantiation is the construction of a
1818 -- local copy for a formal package the actuals may be
1819 -- defaulted, and there is no matching actual to check.
1821 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1823 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1824 N_Access_To_Object_Definition
1825 and then Present (Match)
1828 Formal_Ent : constant Entity_Id :=
1829 Defining_Identifier (Analyzed_Formal);
1831 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1832 = Is_Remote_Types (Formal_Ent)
1834 -- Remoteness of formal and actual match
1838 elsif Is_Remote_Types (Formal_Ent) then
1840 -- Remote formal, non-remote actual
1843 ("actual for& must be remote", Match, Formal_Ent);
1846 -- Non-remote formal, remote actual
1849 ("actual for& may not be remote",
1855 when N_Formal_Subprogram_Declaration =>
1858 (Defining_Unit_Name (Specification (Formal)),
1859 Defining_Unit_Name (Specification (Analyzed_Formal)));
1861 -- If the formal subprogram has the same name as another
1862 -- formal subprogram of the generic, then a named
1863 -- association is illegal (12.3(9)). Exclude named
1864 -- associations that are generated for a nested instance.
1867 and then Is_Named_Assoc
1868 and then Comes_From_Source (Found_Assoc)
1870 Check_Overloaded_Formal_Subprogram (Formal);
1873 -- If there is no corresponding actual, this may be case
1874 -- of partial parameterization, or else the formal has a
1875 -- default or a box.
1877 if No (Match) and then Partial_Parameterization then
1878 Process_Default (Formal);
1880 if Nkind (I_Node) = N_Formal_Package_Declaration then
1881 Check_Overloaded_Formal_Subprogram (Formal);
1885 Append_To (Assoc_List,
1886 Instantiate_Formal_Subprogram
1887 (Formal, Match, Analyzed_Formal));
1889 -- If formal subprogram has contracts, create wrappers
1890 -- for it. This is an expansion activity that cannot
1891 -- take place e.g. within an enclosing generic unit.
1893 if Has_Contracts (Analyzed_Formal)
1894 and then Expander_Active
1896 Build_Subprogram_Wrappers;
1899 -- An instantiation is a freeze point for the actuals,
1900 -- unless this is a rewritten formal package.
1902 if Nkind (I_Node) /= N_Formal_Package_Declaration
1903 and then Nkind (Match) = N_Identifier
1904 and then Is_Subprogram (Entity (Match))
1906 -- The actual subprogram may rename a routine defined
1907 -- in Standard. Avoid freezing such renamings because
1908 -- subprograms coming from Standard cannot be frozen.
1911 not Renames_Standard_Subprogram (Entity (Match))
1913 -- If the actual subprogram comes from a different
1914 -- unit, it is already frozen, either by a body in
1915 -- that unit or by the end of the declarative part
1916 -- of the unit. This check avoids the freezing of
1917 -- subprograms defined in Standard which are used
1918 -- as generic actuals.
1920 and then In_Same_Code_Unit (Entity (Match), I_Node)
1921 and then Has_Fully_Defined_Profile (Entity (Match))
1923 -- Mark the subprogram as having a delayed freeze
1924 -- since this may be an out-of-order action.
1926 Set_Has_Delayed_Freeze (Entity (Match));
1927 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1931 -- If this is a nested generic, preserve default for later
1932 -- instantiations. We do this as well for GNATprove use,
1933 -- so that the list of generic associations is complete.
1935 if No (Match) and then Box_Present (Formal) then
1937 Subp : constant Entity_Id :=
1939 (Specification (Last (Assoc_List)));
1942 Append_To (Default_Actuals,
1943 Make_Generic_Association (Sloc (I_Node),
1945 New_Occurrence_Of (Subp, Sloc (I_Node)),
1946 Explicit_Generic_Actual_Parameter =>
1947 New_Occurrence_Of (Subp, Sloc (I_Node))));
1951 when N_Formal_Package_Declaration =>
1952 -- The name of the formal package may be hidden by the
1953 -- formal parameter itself.
1955 if Error_Posted (Analyzed_Formal) then
1956 Abandon_Instantiation (Instantiation_Node);
1961 (Defining_Identifier (Formal),
1963 (Original_Node (Analyzed_Formal)));
1967 if Partial_Parameterization then
1968 Process_Default (Formal);
1971 Error_Msg_Sloc := Sloc (Gen_Unit);
1974 Instantiation_Node, Defining_Identifier (Formal));
1976 ("\in instantiation of & declared#",
1977 Instantiation_Node, Gen_Unit);
1979 Abandon_Instantiation (Instantiation_Node);
1985 (Instantiate_Formal_Package
1986 (Formal, Match, Analyzed_Formal),
1989 -- Determine whether the actual package needs an explicit
1990 -- freeze node. This is only the case if the actual is
1991 -- declared in the same unit and has a body. Normally
1992 -- packages do not have explicit freeze nodes, and gigi
1993 -- only uses them to elaborate entities in a package
1996 Explicit_Freeze_Check : declare
1997 Actual : constant Entity_Id := Entity (Match);
1998 Gen_Par : Entity_Id;
2000 Needs_Freezing : Boolean;
2003 procedure Check_Generic_Parent;
2004 -- The actual may be an instantiation of a unit
2005 -- declared in a previous instantiation. If that
2006 -- one is also in the current compilation, it must
2007 -- itself be frozen before the actual. The actual
2008 -- may be an instantiation of a generic child unit,
2009 -- in which case the same applies to the instance
2010 -- of the parent which must be frozen before the
2012 -- Should this itself be recursive ???
2014 --------------------------
2015 -- Check_Generic_Parent --
2016 --------------------------
2018 procedure Check_Generic_Parent is
2019 Inst : constant Node_Id :=
2020 Next (Unit_Declaration_Node (Actual));
2026 if Nkind (Parent (Actual)) = N_Package_Specification
2028 Par := Scope (Generic_Parent (Parent (Actual)));
2030 if Is_Generic_Instance (Par) then
2033 -- If the actual is a child generic unit, check
2034 -- whether the instantiation of the parent is
2035 -- also local and must also be frozen now. We
2036 -- must retrieve the instance node to locate the
2037 -- parent instance if any.
2039 elsif Ekind (Par) = E_Generic_Package
2040 and then Is_Child_Unit (Gen_Par)
2041 and then Ekind (Scope (Gen_Par)) =
2044 if Nkind (Inst) = N_Package_Instantiation
2045 and then Nkind (Name (Inst)) =
2048 -- Retrieve entity of parent instance
2050 Par := Entity (Prefix (Name (Inst)));
2059 and then Is_Generic_Instance (Par)
2060 and then Scope (Par) = Current_Scope
2062 (No (Freeze_Node (Par))
2064 not Is_List_Member (Freeze_Node (Par)))
2066 Set_Has_Delayed_Freeze (Par);
2067 Append_Elmt (Par, Actuals_To_Freeze);
2069 end Check_Generic_Parent;
2071 -- Start of processing for Explicit_Freeze_Check
2074 if Present (Renamed_Entity (Actual)) then
2076 Generic_Parent (Specification
2077 (Unit_Declaration_Node
2078 (Renamed_Entity (Actual))));
2081 Generic_Parent (Specification
2082 (Unit_Declaration_Node (Actual)));
2085 if not Expander_Active
2086 or else not Has_Completion (Actual)
2087 or else not In_Same_Source_Unit (I_Node, Actual)
2088 or else Is_Frozen (Actual)
2090 (Present (Renamed_Entity (Actual))
2092 not In_Same_Source_Unit
2093 (I_Node, (Renamed_Entity (Actual))))
2098 -- Finally we want to exclude such freeze nodes
2099 -- from statement sequences, which freeze
2100 -- everything before them.
2101 -- Is this strictly necessary ???
2103 Needs_Freezing := True;
2106 while Present (S) loop
2107 if Ekind (S) in E_Block
2112 Needs_Freezing := False;
2119 if Needs_Freezing then
2120 Check_Generic_Parent;
2122 -- If the actual is a renaming of a proper
2123 -- instance of the formal package, indicate
2124 -- that it is the instance that must be frozen.
2126 if Nkind (Parent (Actual)) =
2127 N_Package_Renaming_Declaration
2129 Set_Has_Delayed_Freeze
2130 (Renamed_Entity (Actual));
2132 (Renamed_Entity (Actual),
2135 Set_Has_Delayed_Freeze (Actual);
2136 Append_Elmt (Actual, Actuals_To_Freeze);
2140 end Explicit_Freeze_Check;
2143 -- For use type and use package appearing in the generic part,
2144 -- we have already copied them, so we can just move them where
2145 -- they belong (we mustn't recopy them since this would mess up
2146 -- the Sloc values).
2148 when N_Use_Package_Clause
2151 if Nkind (Original_Node (I_Node)) =
2152 N_Formal_Package_Declaration
2154 Append (New_Copy_Tree (Formal), Assoc_List);
2157 Append (Formal, Assoc_List);
2161 raise Program_Error;
2164 Formal := Saved_Formal;
2165 Next_Non_Pragma (Analyzed_Formal);
2168 if Num_Actuals > Num_Matched then
2169 Error_Msg_Sloc := Sloc (Gen_Unit);
2171 if Present (Selector_Name (Actual)) then
2173 ("unmatched actual &", Actual, Selector_Name (Actual));
2175 ("\in instantiation of & declared#", Actual, Gen_Unit);
2178 ("unmatched actual in instantiation of & declared#",
2183 elsif Present (Actuals) then
2185 ("too many actuals in generic instantiation", Instantiation_Node);
2188 -- An instantiation freezes all generic actuals. The only exceptions
2189 -- to this are incomplete types and subprograms which are not fully
2190 -- defined at the point of instantiation.
2193 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2195 while Present (Elmt) loop
2196 Freeze_Before (I_Node, Node (Elmt));
2201 -- If there are default subprograms, normalize the tree by adding
2202 -- explicit associations for them. This is required if the instance
2203 -- appears within a generic.
2205 if not Is_Empty_List (Default_Actuals) then
2210 Default := First (Default_Actuals);
2211 while Present (Default) loop
2212 Mark_Rewrite_Insertion (Default);
2216 if No (Actuals) then
2217 Set_Generic_Associations (I_Node, Default_Actuals);
2219 Append_List_To (Actuals, Default_Actuals);
2224 -- If this is a formal package, normalize the parameter list by adding
2225 -- explicit box associations for the formals that are covered by an
2228 if not Is_Empty_List (Default_Formals) then
2229 Append_List (Default_Formals, Formals);
2233 end Analyze_Associations;
2235 -------------------------------
2236 -- Analyze_Formal_Array_Type --
2237 -------------------------------
2239 procedure Analyze_Formal_Array_Type
2240 (T : in out Entity_Id;
2246 -- Treated like a non-generic array declaration, with additional
2251 if Nkind (Def) = N_Constrained_Array_Definition then
2252 DSS := First (Discrete_Subtype_Definitions (Def));
2253 while Present (DSS) loop
2254 if Nkind (DSS) in N_Subtype_Indication
2256 | N_Attribute_Reference
2258 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2265 Array_Type_Declaration (T, Def);
2266 Set_Is_Generic_Type (Base_Type (T));
2268 if Ekind (Component_Type (T)) = E_Incomplete_Type
2269 and then No (Full_View (Component_Type (T)))
2271 Error_Msg_N ("premature usage of incomplete type", Def);
2273 -- Check that range constraint is not allowed on the component type
2274 -- of a generic formal array type (AARM 12.5.3(3))
2276 elsif Is_Internal (Component_Type (T))
2277 and then Present (Subtype_Indication (Component_Definition (Def)))
2278 and then Nkind (Original_Node
2279 (Subtype_Indication (Component_Definition (Def)))) =
2280 N_Subtype_Indication
2283 ("in a formal, a subtype indication can only be "
2284 & "a subtype mark (RM 12.5.3(3))",
2285 Subtype_Indication (Component_Definition (Def)));
2288 end Analyze_Formal_Array_Type;
2290 ---------------------------------------------
2291 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2292 ---------------------------------------------
2294 -- As for other generic types, we create a valid type representation with
2295 -- legal but arbitrary attributes, whose values are never considered
2296 -- static. For all scalar types we introduce an anonymous base type, with
2297 -- the same attributes. We choose the corresponding integer type to be
2298 -- Standard_Integer.
2299 -- Here and in other similar routines, the Sloc of the generated internal
2300 -- type must be the same as the sloc of the defining identifier of the
2301 -- formal type declaration, to provide proper source navigation.
2303 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2307 Loc : constant Source_Ptr := Sloc (Def);
2309 Base : constant Entity_Id :=
2311 (E_Decimal_Fixed_Point_Type,
2313 Sloc (Defining_Identifier (Parent (Def))), 'G');
2315 Int_Base : constant Entity_Id := Standard_Integer;
2316 Delta_Val : constant Ureal := Ureal_1;
2317 Digs_Val : constant Uint := Uint_6;
2319 function Make_Dummy_Bound return Node_Id;
2320 -- Return a properly typed universal real literal to use as a bound
2322 ----------------------
2323 -- Make_Dummy_Bound --
2324 ----------------------
2326 function Make_Dummy_Bound return Node_Id is
2327 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2329 Set_Etype (Bound, Universal_Real);
2331 end Make_Dummy_Bound;
2333 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2338 Set_Etype (Base, Base);
2339 Set_Size_Info (Base, Int_Base);
2340 Set_RM_Size (Base, RM_Size (Int_Base));
2341 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2342 Set_Digits_Value (Base, Digs_Val);
2343 Set_Delta_Value (Base, Delta_Val);
2344 Set_Small_Value (Base, Delta_Val);
2345 Set_Scalar_Range (Base,
2347 Low_Bound => Make_Dummy_Bound,
2348 High_Bound => Make_Dummy_Bound));
2350 Set_Is_Generic_Type (Base);
2351 Set_Parent (Base, Parent (Def));
2353 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2354 Set_Etype (T, Base);
2355 Set_Size_Info (T, Int_Base);
2356 Set_RM_Size (T, RM_Size (Int_Base));
2357 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2358 Set_Digits_Value (T, Digs_Val);
2359 Set_Delta_Value (T, Delta_Val);
2360 Set_Small_Value (T, Delta_Val);
2361 Set_Scalar_Range (T, Scalar_Range (Base));
2362 Set_Is_Constrained (T);
2364 Check_Restriction (No_Fixed_Point, Def);
2365 end Analyze_Formal_Decimal_Fixed_Point_Type;
2367 -------------------------------------------
2368 -- Analyze_Formal_Derived_Interface_Type --
2369 -------------------------------------------
2371 procedure Analyze_Formal_Derived_Interface_Type
2376 Loc : constant Source_Ptr := Sloc (Def);
2379 -- Rewrite as a type declaration of a derived type. This ensures that
2380 -- the interface list and primitive operations are properly captured.
2383 Make_Full_Type_Declaration (Loc,
2384 Defining_Identifier => T,
2385 Type_Definition => Def));
2387 Set_Is_Generic_Type (T);
2388 end Analyze_Formal_Derived_Interface_Type;
2390 ---------------------------------
2391 -- Analyze_Formal_Derived_Type --
2392 ---------------------------------
2394 procedure Analyze_Formal_Derived_Type
2399 Loc : constant Source_Ptr := Sloc (Def);
2400 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2404 Set_Is_Generic_Type (T);
2406 if Private_Present (Def) then
2408 Make_Private_Extension_Declaration (Loc,
2409 Defining_Identifier => T,
2410 Discriminant_Specifications => Discriminant_Specifications (N),
2411 Unknown_Discriminants_Present => Unk_Disc,
2412 Subtype_Indication => Subtype_Mark (Def),
2413 Interface_List => Interface_List (Def));
2415 Set_Abstract_Present (New_N, Abstract_Present (Def));
2416 Set_Limited_Present (New_N, Limited_Present (Def));
2417 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2421 Make_Full_Type_Declaration (Loc,
2422 Defining_Identifier => T,
2423 Discriminant_Specifications =>
2424 Discriminant_Specifications (Parent (T)),
2426 Make_Derived_Type_Definition (Loc,
2427 Subtype_Indication => Subtype_Mark (Def)));
2429 Set_Abstract_Present
2430 (Type_Definition (New_N), Abstract_Present (Def));
2432 (Type_Definition (New_N), Limited_Present (Def));
2439 if not Is_Composite_Type (T) then
2441 ("unknown discriminants not allowed for elementary types", N);
2443 Set_Has_Unknown_Discriminants (T);
2444 Set_Is_Constrained (T, False);
2448 -- If the parent type has a known size, so does the formal, which makes
2449 -- legal representation clauses that involve the formal.
2451 Set_Size_Known_At_Compile_Time
2452 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2453 end Analyze_Formal_Derived_Type;
2455 ----------------------------------
2456 -- Analyze_Formal_Discrete_Type --
2457 ----------------------------------
2459 -- The operations defined for a discrete types are those of an enumeration
2460 -- type. The size is set to an arbitrary value, for use in analyzing the
2463 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2464 Loc : constant Source_Ptr := Sloc (Def);
2468 Base : constant Entity_Id :=
2470 (E_Floating_Point_Type, Current_Scope,
2471 Sloc (Defining_Identifier (Parent (Def))), 'G');
2475 Set_Ekind (T, E_Enumeration_Subtype);
2476 Set_Etype (T, Base);
2479 Set_Is_Generic_Type (T);
2480 Set_Is_Constrained (T);
2482 -- For semantic analysis, the bounds of the type must be set to some
2483 -- non-static value. The simplest is to create attribute nodes for those
2484 -- bounds, that refer to the type itself. These bounds are never
2485 -- analyzed but serve as place-holders.
2488 Make_Attribute_Reference (Loc,
2489 Attribute_Name => Name_First,
2490 Prefix => New_Occurrence_Of (T, Loc));
2494 Make_Attribute_Reference (Loc,
2495 Attribute_Name => Name_Last,
2496 Prefix => New_Occurrence_Of (T, Loc));
2499 Set_Scalar_Range (T,
2504 Set_Ekind (Base, E_Enumeration_Type);
2505 Set_Etype (Base, Base);
2506 Init_Size (Base, 8);
2507 Init_Alignment (Base);
2508 Set_Is_Generic_Type (Base);
2509 Set_Scalar_Range (Base, Scalar_Range (T));
2510 Set_Parent (Base, Parent (Def));
2511 end Analyze_Formal_Discrete_Type;
2513 ----------------------------------
2514 -- Analyze_Formal_Floating_Type --
2515 ---------------------------------
2517 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2518 Base : constant Entity_Id :=
2520 (E_Floating_Point_Type, Current_Scope,
2521 Sloc (Defining_Identifier (Parent (Def))), 'G');
2524 -- The various semantic attributes are taken from the predefined type
2525 -- Float, just so that all of them are initialized. Their values are
2526 -- never used because no constant folding or expansion takes place in
2527 -- the generic itself.
2530 Set_Ekind (T, E_Floating_Point_Subtype);
2531 Set_Etype (T, Base);
2532 Set_Size_Info (T, (Standard_Float));
2533 Set_RM_Size (T, RM_Size (Standard_Float));
2534 Set_Digits_Value (T, Digits_Value (Standard_Float));
2535 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2536 Set_Is_Constrained (T);
2538 Set_Is_Generic_Type (Base);
2539 Set_Etype (Base, Base);
2540 Set_Size_Info (Base, (Standard_Float));
2541 Set_RM_Size (Base, RM_Size (Standard_Float));
2542 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2543 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2544 Set_Parent (Base, Parent (Def));
2546 Check_Restriction (No_Floating_Point, Def);
2547 end Analyze_Formal_Floating_Type;
2549 -----------------------------------
2550 -- Analyze_Formal_Interface_Type;--
2551 -----------------------------------
2553 procedure Analyze_Formal_Interface_Type
2558 Loc : constant Source_Ptr := Sloc (N);
2563 Make_Full_Type_Declaration (Loc,
2564 Defining_Identifier => T,
2565 Type_Definition => Def);
2569 Set_Is_Generic_Type (T);
2570 end Analyze_Formal_Interface_Type;
2572 ---------------------------------
2573 -- Analyze_Formal_Modular_Type --
2574 ---------------------------------
2576 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2578 -- Apart from their entity kind, generic modular types are treated like
2579 -- signed integer types, and have the same attributes.
2581 Analyze_Formal_Signed_Integer_Type (T, Def);
2582 Set_Ekind (T, E_Modular_Integer_Subtype);
2583 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2585 end Analyze_Formal_Modular_Type;
2587 ---------------------------------------
2588 -- Analyze_Formal_Object_Declaration --
2589 ---------------------------------------
2591 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2592 E : constant Node_Id := Default_Expression (N);
2593 Id : constant Node_Id := Defining_Identifier (N);
2600 -- Determine the mode of the formal object
2602 if Out_Present (N) then
2603 K := E_Generic_In_Out_Parameter;
2605 if not In_Present (N) then
2606 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2610 K := E_Generic_In_Parameter;
2613 if Present (Subtype_Mark (N)) then
2614 Find_Type (Subtype_Mark (N));
2615 T := Entity (Subtype_Mark (N));
2617 -- Verify that there is no redundant null exclusion
2619 if Null_Exclusion_Present (N) then
2620 if not Is_Access_Type (T) then
2622 ("null exclusion can only apply to an access type", N);
2624 elsif Can_Never_Be_Null (T) then
2626 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2630 -- Ada 2005 (AI-423): Formal object with an access definition
2633 Check_Access_Definition (N);
2634 T := Access_Definition
2636 N => Access_Definition (N));
2639 if Ekind (T) = E_Incomplete_Type then
2641 Error_Node : Node_Id;
2644 if Present (Subtype_Mark (N)) then
2645 Error_Node := Subtype_Mark (N);
2647 Check_Access_Definition (N);
2648 Error_Node := Access_Definition (N);
2651 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2655 if K = E_Generic_In_Parameter then
2657 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2659 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2661 ("generic formal of mode IN must not be of limited type", N);
2662 Explain_Limited_Type (T, N);
2665 if Is_Abstract_Type (T) then
2667 ("generic formal of mode IN must not be of abstract type", N);
2671 Preanalyze_Spec_Expression (E, T);
2673 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2675 ("initialization not allowed for limited types", E);
2676 Explain_Limited_Type (T, E);
2683 -- Case of generic IN OUT parameter
2686 -- If the formal has an unconstrained type, construct its actual
2687 -- subtype, as is done for subprogram formals. In this fashion, all
2688 -- its uses can refer to specific bounds.
2693 if (Is_Array_Type (T) and then not Is_Constrained (T))
2694 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2697 Non_Freezing_Ref : constant Node_Id :=
2698 New_Occurrence_Of (Id, Sloc (Id));
2702 -- Make sure the actual subtype doesn't generate bogus freezing
2704 Set_Must_Not_Freeze (Non_Freezing_Ref);
2705 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2706 Insert_Before_And_Analyze (N, Decl);
2707 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2710 Set_Actual_Subtype (Id, T);
2715 ("initialization not allowed for `IN OUT` formals", N);
2719 if Has_Aspects (N) then
2720 Analyze_Aspect_Specifications (N, Id);
2722 end Analyze_Formal_Object_Declaration;
2724 ----------------------------------------------
2725 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2726 ----------------------------------------------
2728 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2732 Loc : constant Source_Ptr := Sloc (Def);
2733 Base : constant Entity_Id :=
2735 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2736 Sloc (Defining_Identifier (Parent (Def))), 'G');
2739 -- The semantic attributes are set for completeness only, their values
2740 -- will never be used, since all properties of the type are non-static.
2743 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2744 Set_Etype (T, Base);
2745 Set_Size_Info (T, Standard_Integer);
2746 Set_RM_Size (T, RM_Size (Standard_Integer));
2747 Set_Small_Value (T, Ureal_1);
2748 Set_Delta_Value (T, Ureal_1);
2749 Set_Scalar_Range (T,
2751 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2752 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2753 Set_Is_Constrained (T);
2755 Set_Is_Generic_Type (Base);
2756 Set_Etype (Base, Base);
2757 Set_Size_Info (Base, Standard_Integer);
2758 Set_RM_Size (Base, RM_Size (Standard_Integer));
2759 Set_Small_Value (Base, Ureal_1);
2760 Set_Delta_Value (Base, Ureal_1);
2761 Set_Scalar_Range (Base, Scalar_Range (T));
2762 Set_Parent (Base, Parent (Def));
2764 Check_Restriction (No_Fixed_Point, Def);
2765 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2767 ----------------------------------------
2768 -- Analyze_Formal_Package_Declaration --
2769 ----------------------------------------
2771 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2772 Gen_Id : constant Node_Id := Name (N);
2773 Loc : constant Source_Ptr := Sloc (N);
2774 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2777 Gen_Unit : Entity_Id;
2780 Vis_Prims_List : Elist_Id := No_Elist;
2781 -- List of primitives made temporarily visible in the instantiation
2782 -- to match the visibility of the formal type.
2784 function Build_Local_Package return Node_Id;
2785 -- The formal package is rewritten so that its parameters are replaced
2786 -- with corresponding declarations. For parameters with bona fide
2787 -- associations these declarations are created by Analyze_Associations
2788 -- as for a regular instantiation. For boxed parameters, we preserve
2789 -- the formal declarations and analyze them, in order to introduce
2790 -- entities of the right kind in the environment of the formal.
2792 -------------------------
2793 -- Build_Local_Package --
2794 -------------------------
2796 function Build_Local_Package return Node_Id is
2798 Pack_Decl : Node_Id;
2801 -- Within the formal, the name of the generic package is a renaming
2802 -- of the formal (as for a regular instantiation).
2805 Make_Package_Declaration (Loc,
2808 (Specification (Original_Node (Gen_Decl)),
2809 Empty, Instantiating => True));
2812 Make_Package_Renaming_Declaration (Loc,
2813 Defining_Unit_Name =>
2814 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2815 Name => New_Occurrence_Of (Formal, Loc));
2817 if Nkind (Gen_Id) = N_Identifier
2818 and then Chars (Gen_Id) = Chars (Pack_Id)
2821 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2824 -- If the formal is declared with a box, or with an others choice,
2825 -- create corresponding declarations for all entities in the formal
2826 -- part, so that names with the proper types are available in the
2827 -- specification of the formal package.
2829 -- On the other hand, if there are no associations, then all the
2830 -- formals must have defaults, and this will be checked by the
2831 -- call to Analyze_Associations.
2834 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2837 Formal_Decl : Node_Id;
2840 -- TBA : for a formal package, need to recurse ???
2845 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2846 while Present (Formal_Decl) loop
2850 (Formal_Decl, Empty, Instantiating => True));
2855 -- If generic associations are present, use Analyze_Associations to
2856 -- create the proper renaming declarations.
2860 Act_Tree : constant Node_Id :=
2862 (Original_Node (Gen_Decl), Empty,
2863 Instantiating => True);
2866 Generic_Renamings.Set_Last (0);
2867 Generic_Renamings_HTable.Reset;
2868 Instantiation_Node := N;
2871 Analyze_Associations
2872 (I_Node => Original_Node (N),
2873 Formals => Generic_Formal_Declarations (Act_Tree),
2874 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2876 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2880 Append (Renaming, To => Decls);
2882 -- Add generated declarations ahead of local declarations in
2885 if No (Visible_Declarations (Specification (Pack_Decl))) then
2886 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2889 (First (Visible_Declarations (Specification (Pack_Decl))),
2894 end Build_Local_Package;
2898 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2899 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2901 Associations : Boolean := True;
2903 Parent_Installed : Boolean := False;
2904 Parent_Instance : Entity_Id;
2905 Renaming_In_Par : Entity_Id;
2907 -- Start of processing for Analyze_Formal_Package_Declaration
2910 Check_Text_IO_Special_Unit (Gen_Id);
2913 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2914 Gen_Unit := Entity (Gen_Id);
2916 -- Check for a formal package that is a package renaming
2918 if Present (Renamed_Object (Gen_Unit)) then
2920 -- Indicate that unit is used, before replacing it with renamed
2921 -- entity for use below.
2923 if In_Extended_Main_Source_Unit (N) then
2924 Set_Is_Instantiated (Gen_Unit);
2925 Generate_Reference (Gen_Unit, N);
2928 Gen_Unit := Renamed_Object (Gen_Unit);
2931 if Ekind (Gen_Unit) /= E_Generic_Package then
2932 Error_Msg_N ("expect generic package name", Gen_Id);
2936 elsif Gen_Unit = Current_Scope then
2938 ("generic package cannot be used as a formal package of itself",
2943 elsif In_Open_Scopes (Gen_Unit) then
2944 if Is_Compilation_Unit (Gen_Unit)
2945 and then Is_Child_Unit (Current_Scope)
2947 -- Special-case the error when the formal is a parent, and
2948 -- continue analysis to minimize cascaded errors.
2951 ("generic parent cannot be used as formal package of a child "
2956 ("generic package cannot be used as a formal package within "
2957 & "itself", Gen_Id);
2963 -- Check that name of formal package does not hide name of generic,
2964 -- or its leading prefix. This check must be done separately because
2965 -- the name of the generic has already been analyzed.
2968 Gen_Name : Entity_Id;
2972 while Nkind (Gen_Name) = N_Expanded_Name loop
2973 Gen_Name := Prefix (Gen_Name);
2976 if Chars (Gen_Name) = Chars (Pack_Id) then
2978 ("& is hidden within declaration of formal package",
2984 or else No (Generic_Associations (N))
2985 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2987 Associations := False;
2990 -- If there are no generic associations, the generic parameters appear
2991 -- as local entities and are instantiated like them. We copy the generic
2992 -- package declaration as if it were an instantiation, and analyze it
2993 -- like a regular package, except that we treat the formals as
2994 -- additional visible components.
2996 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2998 if In_Extended_Main_Source_Unit (N) then
2999 Set_Is_Instantiated (Gen_Unit);
3000 Generate_Reference (Gen_Unit, N);
3003 Formal := New_Copy (Pack_Id);
3004 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3006 -- Make local generic without formals. The formals will be replaced with
3007 -- internal declarations.
3010 New_N := Build_Local_Package;
3012 -- If there are errors in the parameter list, Analyze_Associations
3013 -- raises Instantiation_Error. Patch the declaration to prevent further
3014 -- exception propagation.
3017 when Instantiation_Error =>
3018 Enter_Name (Formal);
3019 Set_Ekind (Formal, E_Variable);
3020 Set_Etype (Formal, Any_Type);
3021 Restore_Hidden_Primitives (Vis_Prims_List);
3023 if Parent_Installed then
3031 Set_Defining_Unit_Name (Specification (New_N), Formal);
3032 Set_Generic_Parent (Specification (N), Gen_Unit);
3033 Set_Instance_Env (Gen_Unit, Formal);
3034 Set_Is_Generic_Instance (Formal);
3036 Enter_Name (Formal);
3037 Set_Ekind (Formal, E_Package);
3038 Set_Etype (Formal, Standard_Void_Type);
3039 Set_Inner_Instances (Formal, New_Elmt_List);
3041 -- It is unclear that any aspects can apply to a formal package
3042 -- declaration, given that they look like a hidden conformance
3043 -- requirement on the corresponding actual. However, Abstract_State
3044 -- must be treated specially because it generates declarations that
3045 -- must appear before other declarations in the specification and
3046 -- must be analyzed at once.
3048 if Present (Aspect_Specifications (Gen_Decl)) then
3049 if No (Aspect_Specifications (N)) then
3050 Set_Aspect_Specifications (N, New_List);
3051 Set_Has_Aspects (N);
3055 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3059 while Present (ASN) loop
3060 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3062 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3063 Set_Entity (New_A, Formal);
3064 Set_Analyzed (New_A, False);
3065 Append (New_A, Aspect_Specifications (N));
3066 Analyze_Aspect_Specifications (N, Formal);
3075 Push_Scope (Formal);
3077 -- Manually set the SPARK_Mode from the context because the package
3078 -- declaration is never analyzed.
3080 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3081 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3082 Set_SPARK_Pragma_Inherited (Formal);
3083 Set_SPARK_Aux_Pragma_Inherited (Formal);
3085 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3087 -- Similarly, we have to make the name of the formal visible in the
3088 -- parent instance, to resolve properly fully qualified names that
3089 -- may appear in the generic unit. The parent instance has been
3090 -- placed on the scope stack ahead of the current scope.
3092 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3095 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3096 Set_Ekind (Renaming_In_Par, E_Package);
3097 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3098 Set_Scope (Renaming_In_Par, Parent_Instance);
3099 Set_Parent (Renaming_In_Par, Parent (Formal));
3100 Set_Renamed_Object (Renaming_In_Par, Formal);
3101 Append_Entity (Renaming_In_Par, Parent_Instance);
3104 -- A formal package declaration behaves as a package instantiation with
3105 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3106 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3107 -- all SPARK_Mode pragmas within the generic_package_name.
3109 if SPARK_Mode /= On then
3110 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3112 -- Mark the formal spec in case the body is instantiated at a later
3113 -- pass. This preserves the original context in effect for the body.
3115 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3118 Analyze (Specification (N));
3120 -- The formals for which associations are provided are not visible
3121 -- outside of the formal package. The others are still declared by a
3122 -- formal parameter declaration.
3124 -- If there are no associations, the only local entity to hide is the
3125 -- generated package renaming itself.
3131 E := First_Entity (Formal);
3132 while Present (E) loop
3133 if Associations and then not Is_Generic_Formal (E) then
3137 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3146 End_Package_Scope (Formal);
3147 Restore_Hidden_Primitives (Vis_Prims_List);
3149 if Parent_Installed then
3155 -- Inside the generic unit, the formal package is a regular package, but
3156 -- no body is needed for it. Note that after instantiation, the defining
3157 -- unit name we need is in the new tree and not in the original (see
3158 -- Package_Instantiation). A generic formal package is an instance, and
3159 -- can be used as an actual for an inner instance.
3161 Set_Has_Completion (Formal, True);
3163 -- Add semantic information to the original defining identifier.
3165 Set_Ekind (Pack_Id, E_Package);
3166 Set_Etype (Pack_Id, Standard_Void_Type);
3167 Set_Scope (Pack_Id, Scope (Formal));
3168 Set_Has_Completion (Pack_Id, True);
3171 if Has_Aspects (N) then
3172 -- Unclear that any other aspects may appear here, snalyze them
3173 -- for completion, given that the grammar allows their appearance.
3175 Analyze_Aspect_Specifications (N, Pack_Id);
3178 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3179 end Analyze_Formal_Package_Declaration;
3181 ---------------------------------
3182 -- Analyze_Formal_Private_Type --
3183 ---------------------------------
3185 procedure Analyze_Formal_Private_Type
3191 New_Private_Type (N, T, Def);
3193 -- Set the size to an arbitrary but legal value
3195 Set_Size_Info (T, Standard_Integer);
3196 Set_RM_Size (T, RM_Size (Standard_Integer));
3197 end Analyze_Formal_Private_Type;
3199 ------------------------------------
3200 -- Analyze_Formal_Incomplete_Type --
3201 ------------------------------------
3203 procedure Analyze_Formal_Incomplete_Type
3209 Set_Ekind (T, E_Incomplete_Type);
3211 Set_Private_Dependents (T, New_Elmt_List);
3213 if Tagged_Present (Def) then
3214 Set_Is_Tagged_Type (T);
3215 Make_Class_Wide_Type (T);
3216 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3218 end Analyze_Formal_Incomplete_Type;
3220 ----------------------------------------
3221 -- Analyze_Formal_Signed_Integer_Type --
3222 ----------------------------------------
3224 procedure Analyze_Formal_Signed_Integer_Type
3228 Base : constant Entity_Id :=
3230 (E_Signed_Integer_Type,
3232 Sloc (Defining_Identifier (Parent (Def))), 'G');
3237 Set_Ekind (T, E_Signed_Integer_Subtype);
3238 Set_Etype (T, Base);
3239 Set_Size_Info (T, Standard_Integer);
3240 Set_RM_Size (T, RM_Size (Standard_Integer));
3241 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3242 Set_Is_Constrained (T);
3244 Set_Is_Generic_Type (Base);
3245 Set_Size_Info (Base, Standard_Integer);
3246 Set_RM_Size (Base, RM_Size (Standard_Integer));
3247 Set_Etype (Base, Base);
3248 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3249 Set_Parent (Base, Parent (Def));
3250 end Analyze_Formal_Signed_Integer_Type;
3252 -------------------------------------------
3253 -- Analyze_Formal_Subprogram_Declaration --
3254 -------------------------------------------
3256 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3257 Spec : constant Node_Id := Specification (N);
3258 Def : constant Node_Id := Default_Name (N);
3259 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3267 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3268 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3272 Analyze_Subprogram_Declaration (N);
3273 Set_Is_Formal_Subprogram (Nam);
3274 Set_Has_Completion (Nam);
3276 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3277 Set_Is_Abstract_Subprogram (Nam);
3279 Set_Is_Dispatching_Operation (Nam);
3281 -- A formal abstract procedure cannot have a null default
3282 -- (RM 12.6(4.1/2)).
3284 if Nkind (Spec) = N_Procedure_Specification
3285 and then Null_Present (Spec)
3288 ("a formal abstract subprogram cannot default to null", Spec);
3292 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3294 if No (Ctrl_Type) then
3296 ("abstract formal subprogram must have a controlling type",
3299 elsif Ada_Version >= Ada_2012
3300 and then Is_Incomplete_Type (Ctrl_Type)
3303 ("controlling type of abstract formal subprogram cannot "
3304 & "be incomplete type", N, Ctrl_Type);
3307 Check_Controlling_Formals (Ctrl_Type, Nam);
3312 -- Default name is resolved at the point of instantiation
3314 if Box_Present (N) then
3317 -- Else default is bound at the point of generic declaration
3319 elsif Present (Def) then
3320 if Nkind (Def) = N_Operator_Symbol then
3321 Find_Direct_Name (Def);
3323 elsif Nkind (Def) /= N_Attribute_Reference then
3327 -- For an attribute reference, analyze the prefix and verify
3328 -- that it has the proper profile for the subprogram.
3330 Analyze (Prefix (Def));
3331 Valid_Default_Attribute (Nam, Def);
3335 -- Default name may be overloaded, in which case the interpretation
3336 -- with the correct profile must be selected, as for a renaming.
3337 -- If the definition is an indexed component, it must denote a
3338 -- member of an entry family. If it is a selected component, it
3339 -- can be a protected operation.
3341 if Etype (Def) = Any_Type then
3344 elsif Nkind (Def) = N_Selected_Component then
3345 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3346 Error_Msg_N ("expect valid subprogram name as default", Def);
3349 elsif Nkind (Def) = N_Indexed_Component then
3350 if Is_Entity_Name (Prefix (Def)) then
3351 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3352 Error_Msg_N ("expect valid subprogram name as default", Def);
3355 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3356 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3359 Error_Msg_N ("expect valid subprogram name as default", Def);
3363 Error_Msg_N ("expect valid subprogram name as default", Def);
3367 elsif Nkind (Def) = N_Character_Literal then
3369 -- Needs some type checks: subprogram should be parameterless???
3371 Resolve (Def, (Etype (Nam)));
3373 elsif not Is_Entity_Name (Def)
3374 or else not Is_Overloadable (Entity (Def))
3376 Error_Msg_N ("expect valid subprogram name as default", Def);
3379 elsif not Is_Overloaded (Def) then
3380 Subp := Entity (Def);
3383 Error_Msg_N ("premature usage of formal subprogram", Def);
3385 elsif not Entity_Matches_Spec (Subp, Nam) then
3386 Error_Msg_N ("no visible entity matches specification", Def);
3389 -- More than one interpretation, so disambiguate as for a renaming
3394 I1 : Interp_Index := 0;
3400 Get_First_Interp (Def, I, It);
3401 while Present (It.Nam) loop
3402 if Entity_Matches_Spec (It.Nam, Nam) then
3403 if Subp /= Any_Id then
3404 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3406 if It1 = No_Interp then
3407 Error_Msg_N ("ambiguous default subprogram", Def);
3420 Get_Next_Interp (I, It);
3424 if Subp /= Any_Id then
3426 -- Subprogram found, generate reference to it
3428 Set_Entity (Def, Subp);
3429 Generate_Reference (Subp, Def);
3432 Error_Msg_N ("premature usage of formal subprogram", Def);
3434 elsif Ekind (Subp) /= E_Operator then
3435 Check_Mode_Conformant (Subp, Nam);
3439 Error_Msg_N ("no visible subprogram matches specification", N);
3445 if Has_Aspects (N) then
3446 Analyze_Aspect_Specifications (N, Nam);
3449 end Analyze_Formal_Subprogram_Declaration;
3451 -------------------------------------
3452 -- Analyze_Formal_Type_Declaration --
3453 -------------------------------------
3455 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3456 Def : constant Node_Id := Formal_Type_Definition (N);
3460 T := Defining_Identifier (N);
3462 if Present (Discriminant_Specifications (N))
3463 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3466 ("discriminants not allowed for this formal type", T);
3469 -- Enter the new name, and branch to specific routine
3472 when N_Formal_Private_Type_Definition =>
3473 Analyze_Formal_Private_Type (N, T, Def);
3475 when N_Formal_Derived_Type_Definition =>
3476 Analyze_Formal_Derived_Type (N, T, Def);
3478 when N_Formal_Incomplete_Type_Definition =>
3479 Analyze_Formal_Incomplete_Type (T, Def);
3481 when N_Formal_Discrete_Type_Definition =>
3482 Analyze_Formal_Discrete_Type (T, Def);
3484 when N_Formal_Signed_Integer_Type_Definition =>
3485 Analyze_Formal_Signed_Integer_Type (T, Def);
3487 when N_Formal_Modular_Type_Definition =>
3488 Analyze_Formal_Modular_Type (T, Def);
3490 when N_Formal_Floating_Point_Definition =>
3491 Analyze_Formal_Floating_Type (T, Def);
3493 when N_Formal_Ordinary_Fixed_Point_Definition =>
3494 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3496 when N_Formal_Decimal_Fixed_Point_Definition =>
3497 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3499 when N_Array_Type_Definition =>
3500 Analyze_Formal_Array_Type (T, Def);
3502 when N_Access_Function_Definition
3503 | N_Access_Procedure_Definition
3504 | N_Access_To_Object_Definition
3506 Analyze_Generic_Access_Type (T, Def);
3508 -- Ada 2005: a interface declaration is encoded as an abstract
3509 -- record declaration or a abstract type derivation.
3511 when N_Record_Definition =>
3512 Analyze_Formal_Interface_Type (N, T, Def);
3514 when N_Derived_Type_Definition =>
3515 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3521 raise Program_Error;
3524 -- A formal type declaration declares a type and its first
3527 Set_Is_Generic_Type (T);
3528 Set_Is_First_Subtype (T);
3530 if Has_Aspects (N) then
3531 Analyze_Aspect_Specifications (N, T);
3533 end Analyze_Formal_Type_Declaration;
3535 ------------------------------------
3536 -- Analyze_Function_Instantiation --
3537 ------------------------------------
3539 procedure Analyze_Function_Instantiation (N : Node_Id) is
3541 Analyze_Subprogram_Instantiation (N, E_Function);
3542 end Analyze_Function_Instantiation;
3544 ---------------------------------
3545 -- Analyze_Generic_Access_Type --
3546 ---------------------------------
3548 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3552 if Nkind (Def) = N_Access_To_Object_Definition then
3553 Access_Type_Declaration (T, Def);
3555 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3556 and then No (Full_View (Designated_Type (T)))
3557 and then not Is_Generic_Type (Designated_Type (T))
3559 Error_Msg_N ("premature usage of incomplete type", Def);
3561 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3563 ("only a subtype mark is allowed in a formal", Def);
3567 Access_Subprogram_Declaration (T, Def);
3569 end Analyze_Generic_Access_Type;
3571 ---------------------------------
3572 -- Analyze_Generic_Formal_Part --
3573 ---------------------------------
3575 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3576 Gen_Parm_Decl : Node_Id;
3579 -- The generic formals are processed in the scope of the generic unit,
3580 -- where they are immediately visible. The scope is installed by the
3583 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3584 while Present (Gen_Parm_Decl) loop
3585 Analyze (Gen_Parm_Decl);
3586 Next (Gen_Parm_Decl);
3589 Generate_Reference_To_Generic_Formals (Current_Scope);
3591 -- For Ada 2020, some formal parameters can carry aspects, which must
3592 -- be name-resolved at the end of the list of formal parameters (which
3593 -- has the semantics of a declaration list).
3595 Analyze_Contracts (Generic_Formal_Declarations (N));
3596 end Analyze_Generic_Formal_Part;
3598 ------------------------------------------
3599 -- Analyze_Generic_Package_Declaration --
3600 ------------------------------------------
3602 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3603 Decls : constant List_Id := Visible_Declarations (Specification (N));
3604 Loc : constant Source_Ptr := Sloc (N);
3610 Save_Parent : Node_Id;
3613 -- A generic may grant access to its private enclosing context depending
3614 -- on the placement of its corresponding body. From elaboration point of
3615 -- view, the flow of execution may enter this private context, and then
3616 -- reach an external unit, thus producing a dependency on that external
3617 -- unit. For such a path to be properly discovered and encoded in the
3618 -- ALI file of the main unit, let the ABE mechanism process the body of
3619 -- the main unit, and encode all relevant invocation constructs and the
3620 -- relations between them.
3622 Mark_Save_Invocation_Graph_Of_Body;
3624 -- We introduce a renaming of the enclosing package, to have a usable
3625 -- entity as the prefix of an expanded name for a local entity of the
3626 -- form Par.P.Q, where P is the generic package. This is because a local
3627 -- entity named P may hide it, so that the usual visibility rules in
3628 -- the instance will not resolve properly.
3631 Make_Package_Renaming_Declaration (Loc,
3632 Defining_Unit_Name =>
3633 Make_Defining_Identifier (Loc,
3634 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3636 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3638 -- The declaration is inserted before other declarations, but before
3639 -- pragmas that may be library-unit pragmas and must appear before other
3640 -- declarations. The pragma Compile_Time_Error is not in this class, and
3641 -- may contain an expression that includes such a qualified name, so the
3642 -- renaming declaration must appear before it.
3644 -- Are there other pragmas that require this special handling ???
3646 if Present (Decls) then
3647 Decl := First (Decls);
3648 while Present (Decl)
3649 and then Nkind (Decl) = N_Pragma
3650 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3655 if Present (Decl) then
3656 Insert_Before (Decl, Renaming);
3658 Append (Renaming, Visible_Declarations (Specification (N)));
3662 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3665 -- Create copy of generic unit, and save for instantiation. If the unit
3666 -- is a child unit, do not copy the specifications for the parent, which
3667 -- are not part of the generic tree.
3669 Save_Parent := Parent_Spec (N);
3670 Set_Parent_Spec (N, Empty);
3672 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3673 Set_Parent_Spec (New_N, Save_Parent);
3676 -- Once the contents of the generic copy and the template are swapped,
3677 -- do the same for their respective aspect specifications.
3679 Exchange_Aspects (N, New_N);
3681 -- Collect all contract-related source pragmas found within the template
3682 -- and attach them to the contract of the package spec. This contract is
3683 -- used in the capture of global references within annotations.
3685 Create_Generic_Contract (N);
3687 Id := Defining_Entity (N);
3688 Generate_Definition (Id);
3690 -- Expansion is not applied to generic units
3695 Set_Ekind (Id, E_Generic_Package);
3696 Set_Etype (Id, Standard_Void_Type);
3698 -- Set SPARK_Mode from context
3700 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3701 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3702 Set_SPARK_Pragma_Inherited (Id);
3703 Set_SPARK_Aux_Pragma_Inherited (Id);
3705 -- Preserve relevant elaboration-related attributes of the context which
3706 -- are no longer available or very expensive to recompute once analysis,
3707 -- resolution, and expansion are over.
3709 Mark_Elaboration_Attributes
3714 -- Analyze aspects now, so that generated pragmas appear in the
3715 -- declarations before building and analyzing the generic copy.
3717 if Has_Aspects (N) then
3718 Analyze_Aspect_Specifications (N, Id);
3722 Enter_Generic_Scope (Id);
3723 Set_Inner_Instances (Id, New_Elmt_List);
3725 Set_Categorization_From_Pragmas (N);
3726 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3728 -- Link the declaration of the generic homonym in the generic copy to
3729 -- the package it renames, so that it is always resolved properly.
3731 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3732 Set_Entity (Associated_Node (Name (Renaming)), Id);
3734 -- For a library unit, we have reconstructed the entity for the unit,
3735 -- and must reset it in the library tables.
3737 if Nkind (Parent (N)) = N_Compilation_Unit then
3738 Set_Cunit_Entity (Current_Sem_Unit, Id);
3741 Analyze_Generic_Formal_Part (N);
3743 -- After processing the generic formals, analysis proceeds as for a
3744 -- non-generic package.
3746 Analyze (Specification (N));
3748 Validate_Categorization_Dependency (N, Id);
3752 End_Package_Scope (Id);
3753 Exit_Generic_Scope (Id);
3755 -- If the generic appears within a package unit, the body of that unit
3756 -- has to be present for instantiation and inlining.
3758 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3759 Set_Body_Needed_For_Inlining
3760 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3763 if Nkind (Parent (N)) /= N_Compilation_Unit then
3764 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3765 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3766 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3769 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3770 Validate_RT_RAT_Component (N);
3772 -- If this is a spec without a body, check that generic parameters
3775 if not Body_Required (Parent (N)) then
3776 Check_References (Id);
3780 -- If there is a specified storage pool in the context, create an
3781 -- aspect on the package declaration, so that it is used in any
3782 -- instance that does not override it.
3784 if Present (Default_Pool) then
3790 Make_Aspect_Specification (Loc,
3791 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3792 Expression => New_Copy (Default_Pool));
3794 if No (Aspect_Specifications (Specification (N))) then
3795 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3797 Append (ASN, Aspect_Specifications (Specification (N)));
3801 end Analyze_Generic_Package_Declaration;
3803 --------------------------------------------
3804 -- Analyze_Generic_Subprogram_Declaration --
3805 --------------------------------------------
3807 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3811 Result_Type : Entity_Id;
3812 Save_Parent : Node_Id;
3817 -- A generic may grant access to its private enclosing context depending
3818 -- on the placement of its corresponding body. From elaboration point of
3819 -- view, the flow of execution may enter this private context, and then
3820 -- reach an external unit, thus producing a dependency on that external
3821 -- unit. For such a path to be properly discovered and encoded in the
3822 -- ALI file of the main unit, let the ABE mechanism process the body of
3823 -- the main unit, and encode all relevant invocation constructs and the
3824 -- relations between them.
3826 Mark_Save_Invocation_Graph_Of_Body;
3828 -- Create copy of generic unit, and save for instantiation. If the unit
3829 -- is a child unit, do not copy the specifications for the parent, which
3830 -- are not part of the generic tree.
3832 Save_Parent := Parent_Spec (N);
3833 Set_Parent_Spec (N, Empty);
3835 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3836 Set_Parent_Spec (New_N, Save_Parent);
3839 -- Once the contents of the generic copy and the template are swapped,
3840 -- do the same for their respective aspect specifications.
3842 Exchange_Aspects (N, New_N);
3844 -- Collect all contract-related source pragmas found within the template
3845 -- and attach them to the contract of the subprogram spec. This contract
3846 -- is used in the capture of global references within annotations.
3848 Create_Generic_Contract (N);
3850 Spec := Specification (N);
3851 Id := Defining_Entity (Spec);
3852 Generate_Definition (Id);
3854 if Nkind (Id) = N_Defining_Operator_Symbol then
3856 ("operator symbol not allowed for generic subprogram", Id);
3862 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3865 Enter_Generic_Scope (Id);
3866 Set_Inner_Instances (Id, New_Elmt_List);
3867 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3869 Analyze_Generic_Formal_Part (N);
3871 if Nkind (Spec) = N_Function_Specification then
3872 Set_Ekind (Id, E_Generic_Function);
3874 Set_Ekind (Id, E_Generic_Procedure);
3877 -- Set SPARK_Mode from context
3879 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3880 Set_SPARK_Pragma_Inherited (Id);
3882 -- Preserve relevant elaboration-related attributes of the context which
3883 -- are no longer available or very expensive to recompute once analysis,
3884 -- resolution, and expansion are over.
3886 Mark_Elaboration_Attributes
3891 Formals := Parameter_Specifications (Spec);
3893 if Present (Formals) then
3894 Process_Formals (Formals, Spec);
3897 if Nkind (Spec) = N_Function_Specification then
3898 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3899 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3900 Set_Etype (Id, Result_Type);
3902 -- Check restriction imposed by AI05-073: a generic function
3903 -- cannot return an abstract type or an access to such.
3905 -- This is a binding interpretation should it apply to earlier
3906 -- versions of Ada as well as Ada 2012???
3908 if Is_Abstract_Type (Designated_Type (Result_Type))
3909 and then Ada_Version >= Ada_2012
3912 ("generic function cannot have an access result "
3913 & "that designates an abstract type", Spec);
3917 Find_Type (Result_Definition (Spec));
3918 Typ := Entity (Result_Definition (Spec));
3920 if Is_Abstract_Type (Typ)
3921 and then Ada_Version >= Ada_2012
3924 ("generic function cannot have abstract result type", Spec);
3927 -- If a null exclusion is imposed on the result type, then create
3928 -- a null-excluding itype (an access subtype) and use it as the
3929 -- function's Etype.
3931 if Is_Access_Type (Typ)
3932 and then Null_Exclusion_Present (Spec)
3935 Create_Null_Excluding_Itype
3937 Related_Nod => Spec,
3938 Scope_Id => Defining_Unit_Name (Spec)));
3940 Set_Etype (Id, Typ);
3945 Set_Etype (Id, Standard_Void_Type);
3948 -- Analyze the aspects of the generic copy to ensure that all generated
3949 -- pragmas (if any) perform their semantic effects.
3951 if Has_Aspects (N) then
3952 Analyze_Aspect_Specifications (N, Id);
3955 -- For a library unit, we have reconstructed the entity for the unit,
3956 -- and must reset it in the library tables. We also make sure that
3957 -- Body_Required is set properly in the original compilation unit node.
3959 if Nkind (Parent (N)) = N_Compilation_Unit then
3960 Set_Cunit_Entity (Current_Sem_Unit, Id);
3961 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3964 -- If the generic appears within a package unit, the body of that unit
3965 -- has to be present for instantiation and inlining.
3967 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3968 and then Unit_Requires_Body (Id)
3970 Set_Body_Needed_For_Inlining
3971 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3974 Set_Categorization_From_Pragmas (N);
3975 Validate_Categorization_Dependency (N, Id);
3977 -- Capture all global references that occur within the profile of the
3978 -- generic subprogram. Aspects are not part of this processing because
3979 -- they must be delayed. If processed now, Save_Global_References will
3980 -- destroy the Associated_Node links and prevent the capture of global
3981 -- references when the contract of the generic subprogram is analyzed.
3983 Save_Global_References (Original_Node (N));
3987 Exit_Generic_Scope (Id);
3988 Generate_Reference_To_Formals (Id);
3990 List_Inherited_Pre_Post_Aspects (Id);
3991 end Analyze_Generic_Subprogram_Declaration;
3993 -----------------------------------
3994 -- Analyze_Package_Instantiation --
3995 -----------------------------------
3997 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3998 -- must be replaced by gotos which jump to the end of the routine in order
3999 -- to restore the Ghost and SPARK modes.
4001 procedure Analyze_Package_Instantiation (N : Node_Id) is
4002 Has_Inline_Always : Boolean := False;
4003 -- Set if the generic unit contains any subprograms with Inline_Always.
4004 -- Only relevant when back-end inlining is not enabled.
4006 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4007 -- Return True if inlining is active and Gen_Unit contains inlined
4008 -- subprograms. In this case, we may either instantiate the body when
4009 -- front-end inlining is enabled, or add a pending instantiation when
4010 -- back-end inlining is enabled. In the former case, this may cause
4011 -- superfluous instantiations, but in either case we need to perform
4012 -- the instantiation of the body in the context of the instance and
4013 -- not in that of the point of inlining.
4015 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4016 -- Return True if Gen_Unit needs to have its body instantiated in the
4017 -- context of N. This in particular excludes generic contexts.
4019 -----------------------
4020 -- Might_Inline_Subp --
4021 -----------------------
4023 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4027 if Inline_Processing_Required then
4028 -- No need to recompute the answer if we know it is positive
4029 -- and back-end inlining is enabled.
4031 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4035 E := First_Entity (Gen_Unit);
4036 while Present (E) loop
4037 if Is_Subprogram (E) and then Is_Inlined (E) then
4038 -- Remember if there are any subprograms with Inline_Always
4040 if Has_Pragma_Inline_Always (E) then
4041 Has_Inline_Always := True;
4044 Set_Is_Inlined (Gen_Unit);
4053 end Might_Inline_Subp;
4055 -------------------------------
4056 -- Needs_Body_Instantiated --
4057 -------------------------------
4059 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4061 -- No need to instantiate bodies in generic units
4063 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4067 -- If the instantiation is in the main unit, then the body is needed
4069 if Is_In_Main_Unit (N) then
4073 -- In GNATprove mode, never instantiate bodies outside of the main
4074 -- unit, as it does not use frontend/backend inlining in the way that
4075 -- GNAT does, so does not benefit from such instantiations. On the
4076 -- contrary, such instantiations may bring artificial constraints,
4077 -- as for example such bodies may require preprocessing.
4079 if GNATprove_Mode then
4083 -- If not, then again no need to instantiate bodies in generic units
4085 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4089 -- Here we have a special handling for back-end inlining: if inline
4090 -- processing is required, then we unconditionally want to have the
4091 -- body instantiated. The reason is that Might_Inline_Subp does not
4092 -- catch all the cases (as it does not recurse into nested packages)
4093 -- so this avoids the need to patch things up afterwards. Moreover,
4094 -- these instantiations are only performed on demand when back-end
4095 -- inlining is enabled, so this causes very little extra work.
4097 if Inline_Processing_Required and then Back_End_Inlining then
4101 -- We want to have the bodies instantiated in non-main units if
4102 -- they might contribute inlined subprograms.
4104 return Might_Inline_Subp (Gen_Unit);
4105 end Needs_Body_Instantiated;
4107 -- Local declarations
4109 Gen_Id : constant Node_Id := Name (N);
4110 Inst_Id : constant Entity_Id := Defining_Entity (N);
4111 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4112 Loc : constant Source_Ptr := Sloc (N);
4114 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4115 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4116 Saved_ISMP : constant Boolean :=
4117 Ignore_SPARK_Mode_Pragmas_In_Instance;
4118 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4119 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4120 -- Save the Ghost and SPARK mode-related data to restore on exit
4122 Saved_Style_Check : constant Boolean := Style_Check;
4123 -- Save style check mode for restore on exit
4126 Act_Decl_Name : Node_Id;
4127 Act_Decl_Id : Entity_Id;
4130 Env_Installed : Boolean := False;
4133 Gen_Unit : Entity_Id;
4134 Inline_Now : Boolean := False;
4135 Needs_Body : Boolean;
4136 Parent_Installed : Boolean := False;
4137 Renaming_List : List_Id;
4138 Unit_Renaming : Node_Id;
4140 Vis_Prims_List : Elist_Id := No_Elist;
4141 -- List of primitives made temporarily visible in the instantiation
4142 -- to match the visibility of the formal type
4144 -- Start of processing for Analyze_Package_Instantiation
4147 -- Preserve relevant elaboration-related attributes of the context which
4148 -- are no longer available or very expensive to recompute once analysis,
4149 -- resolution, and expansion are over.
4151 Mark_Elaboration_Attributes
4158 -- Very first thing: check for Text_IO special unit in case we are
4159 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4161 Check_Text_IO_Special_Unit (Name (N));
4163 -- Make node global for error reporting
4165 Instantiation_Node := N;
4167 -- Case of instantiation of a generic package
4169 if Nkind (N) = N_Package_Instantiation then
4170 Act_Decl_Id := New_Copy (Defining_Entity (N));
4171 Set_Comes_From_Source (Act_Decl_Id, True);
4173 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4175 Make_Defining_Program_Unit_Name (Loc,
4177 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4178 Defining_Identifier => Act_Decl_Id);
4180 Act_Decl_Name := Act_Decl_Id;
4183 -- Case of instantiation of a formal package
4186 Act_Decl_Id := Defining_Identifier (N);
4187 Act_Decl_Name := Act_Decl_Id;
4190 Generate_Definition (Act_Decl_Id);
4191 Set_Ekind (Act_Decl_Id, E_Package);
4193 -- Initialize list of incomplete actuals before analysis
4195 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4197 Preanalyze_Actuals (N, Act_Decl_Id);
4199 -- Turn off style checking in instances. If the check is enabled on the
4200 -- generic unit, a warning in an instance would just be noise. If not
4201 -- enabled on the generic, then a warning in an instance is just wrong.
4202 -- This must be done after analyzing the actuals, which do come from
4203 -- source and are subject to style checking.
4205 Style_Check := False;
4208 Env_Installed := True;
4210 -- Reset renaming map for formal types. The mapping is established
4211 -- when analyzing the generic associations, but some mappings are
4212 -- inherited from formal packages of parent units, and these are
4213 -- constructed when the parents are installed.
4215 Generic_Renamings.Set_Last (0);
4216 Generic_Renamings_HTable.Reset;
4218 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4219 Gen_Unit := Entity (Gen_Id);
4221 -- A package instantiation is Ghost when it is subject to pragma Ghost
4222 -- or the generic template is Ghost. Set the mode now to ensure that
4223 -- any nodes generated during analysis and expansion are marked as
4226 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4228 -- Verify that it is the name of a generic package
4230 -- A visibility glitch: if the instance is a child unit and the generic
4231 -- is the generic unit of a parent instance (i.e. both the parent and
4232 -- the child units are instances of the same package) the name now
4233 -- denotes the renaming within the parent, not the intended generic
4234 -- unit. See if there is a homonym that is the desired generic. The
4235 -- renaming declaration must be visible inside the instance of the
4236 -- child, but not when analyzing the name in the instantiation itself.
4238 if Ekind (Gen_Unit) = E_Package
4239 and then Present (Renamed_Entity (Gen_Unit))
4240 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4241 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4242 and then Present (Homonym (Gen_Unit))
4244 Gen_Unit := Homonym (Gen_Unit);
4247 if Etype (Gen_Unit) = Any_Type then
4251 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4253 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4255 if From_Limited_With (Gen_Unit) then
4257 ("cannot instantiate a limited withed package", Gen_Id);
4260 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4267 if In_Extended_Main_Source_Unit (N) then
4268 Set_Is_Instantiated (Gen_Unit);
4269 Generate_Reference (Gen_Unit, N);
4271 if Present (Renamed_Object (Gen_Unit)) then
4272 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4273 Generate_Reference (Renamed_Object (Gen_Unit), N);
4277 if Nkind (Gen_Id) = N_Identifier
4278 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4281 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4283 elsif Nkind (Gen_Id) = N_Expanded_Name
4284 and then Is_Child_Unit (Gen_Unit)
4285 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4286 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4289 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4292 Set_Entity (Gen_Id, Gen_Unit);
4294 -- If generic is a renaming, get original generic unit
4296 if Present (Renamed_Object (Gen_Unit))
4297 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4299 Gen_Unit := Renamed_Object (Gen_Unit);
4302 -- Verify that there are no circular instantiations
4304 if In_Open_Scopes (Gen_Unit) then
4305 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4309 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4310 Error_Msg_Node_2 := Current_Scope;
4312 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4313 Circularity_Detected := True;
4318 Set_Ekind (Inst_Id, E_Package);
4319 Set_Scope (Inst_Id, Current_Scope);
4321 -- If the context of the instance is subject to SPARK_Mode "off" or
4322 -- the annotation is altogether missing, set the global flag which
4323 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4326 if SPARK_Mode /= On then
4327 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4329 -- Mark the instance spec in case the body is instantiated at a
4330 -- later pass. This preserves the original context in effect for
4333 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4336 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4337 Gen_Spec := Specification (Gen_Decl);
4339 -- Initialize renamings map, for error checking, and the list that
4340 -- holds private entities whose views have changed between generic
4341 -- definition and instantiation. If this is the instance created to
4342 -- validate an actual package, the instantiation environment is that
4343 -- of the enclosing instance.
4345 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4347 -- Copy original generic tree, to produce text for instantiation
4351 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4353 Act_Spec := Specification (Act_Tree);
4355 -- If this is the instance created to validate an actual package,
4356 -- only the formals matter, do not examine the package spec itself.
4358 if Is_Actual_Pack then
4359 Set_Visible_Declarations (Act_Spec, New_List);
4360 Set_Private_Declarations (Act_Spec, New_List);
4364 Analyze_Associations
4366 Formals => Generic_Formal_Declarations (Act_Tree),
4367 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4369 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4371 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4372 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4373 Set_Is_Generic_Instance (Act_Decl_Id);
4374 Set_Generic_Parent (Act_Spec, Gen_Unit);
4376 -- References to the generic in its own declaration or its body are
4377 -- references to the instance. Add a renaming declaration for the
4378 -- generic unit itself. This declaration, as well as the renaming
4379 -- declarations for the generic formals, must remain private to the
4380 -- unit: the formals, because this is the language semantics, and
4381 -- the unit because its use is an artifact of the implementation.
4384 Make_Package_Renaming_Declaration (Loc,
4385 Defining_Unit_Name =>
4386 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4387 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4389 Append (Unit_Renaming, Renaming_List);
4391 -- The renaming declarations are the first local declarations of the
4394 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4396 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4398 Set_Visible_Declarations (Act_Spec, Renaming_List);
4401 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4403 -- Propagate the aspect specifications from the package declaration
4404 -- template to the instantiated version of the package declaration.
4406 if Has_Aspects (Act_Tree) then
4407 Set_Aspect_Specifications (Act_Decl,
4408 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4411 -- The generic may have a generated Default_Storage_Pool aspect,
4412 -- set at the point of generic declaration. If the instance has
4413 -- that aspect, it overrides the one inherited from the generic.
4415 if Has_Aspects (Gen_Spec) then
4416 if No (Aspect_Specifications (N)) then
4417 Set_Aspect_Specifications (N,
4419 (Aspect_Specifications (Gen_Spec))));
4423 Inherited_Aspects : constant List_Id :=
4425 (Aspect_Specifications (Gen_Spec));
4429 Pool_Present : Boolean := False;
4432 ASN1 := First (Aspect_Specifications (N));
4433 while Present (ASN1) loop
4434 if Chars (Identifier (ASN1)) =
4435 Name_Default_Storage_Pool
4437 Pool_Present := True;
4444 if Pool_Present then
4446 -- If generic carries a default storage pool, remove it
4447 -- in favor of the instance one.
4449 ASN2 := First (Inherited_Aspects);
4450 while Present (ASN2) loop
4451 if Chars (Identifier (ASN2)) =
4452 Name_Default_Storage_Pool
4463 (Aspect_Specifications (N), Inherited_Aspects);
4468 -- Save the instantiation node for a subsequent instantiation of the
4469 -- body if there is one and it needs to be instantiated here.
4471 -- We instantiate the body only if we are generating code, or if we
4472 -- are generating cross-reference information, or for GNATprove use.
4475 Enclosing_Body_Present : Boolean := False;
4476 -- If the generic unit is not a compilation unit, then a body may
4477 -- be present in its parent even if none is required. We create a
4478 -- tentative pending instantiation for the body, which will be
4479 -- discarded if none is actually present.
4484 if Scope (Gen_Unit) /= Standard_Standard
4485 and then not Is_Child_Unit (Gen_Unit)
4487 Scop := Scope (Gen_Unit);
4488 while Present (Scop) and then Scop /= Standard_Standard loop
4489 if Unit_Requires_Body (Scop) then
4490 Enclosing_Body_Present := True;
4493 elsif In_Open_Scopes (Scop)
4494 and then In_Package_Body (Scop)
4496 Enclosing_Body_Present := True;
4500 exit when Is_Compilation_Unit (Scop);
4501 Scop := Scope (Scop);
4505 -- If front-end inlining is enabled or there are any subprograms
4506 -- marked with Inline_Always, and this is a unit for which code
4507 -- will be generated, we instantiate the body at once.
4509 -- This is done if the instance is not the main unit, and if the
4510 -- generic is not a child unit of another generic, to avoid scope
4511 -- problems and the reinstallation of parent instances.
4514 and then (not Is_Child_Unit (Gen_Unit)
4515 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4516 and then Might_Inline_Subp (Gen_Unit)
4517 and then not Is_Actual_Pack
4519 if not Back_End_Inlining
4520 and then (Front_End_Inlining or else Has_Inline_Always)
4521 and then (Is_In_Main_Unit (N)
4522 or else In_Main_Context (Current_Scope))
4523 and then Nkind (Parent (N)) /= N_Compilation_Unit
4527 -- In configurable_run_time mode we force the inlining of
4528 -- predefined subprograms marked Inline_Always, to minimize
4529 -- the use of the run-time library.
4531 elsif In_Predefined_Unit (Gen_Decl)
4532 and then Configurable_Run_Time_Mode
4533 and then Nkind (Parent (N)) /= N_Compilation_Unit
4538 -- If the current scope is itself an instance within a child
4539 -- unit, there will be duplications in the scope stack, and the
4540 -- unstacking mechanism in Inline_Instance_Body will fail.
4541 -- This loses some rare cases of optimization, and might be
4542 -- improved some day, if we can find a proper abstraction for
4543 -- "the complete compilation context" that can be saved and
4546 if Is_Generic_Instance (Current_Scope) then
4548 Curr_Unit : constant Entity_Id :=
4549 Cunit_Entity (Current_Sem_Unit);
4551 if Curr_Unit /= Current_Scope
4552 and then Is_Child_Unit (Curr_Unit)
4554 Inline_Now := False;
4561 (Unit_Requires_Body (Gen_Unit)
4562 or else Enclosing_Body_Present
4563 or else Present (Corresponding_Body (Gen_Decl)))
4564 and then Needs_Body_Instantiated (Gen_Unit)
4565 and then not Is_Actual_Pack
4566 and then not Inline_Now
4567 and then (Operating_Mode = Generate_Code
4568 or else (Operating_Mode = Check_Semantics
4569 and then GNATprove_Mode));
4571 -- If front-end inlining is enabled or there are any subprograms
4572 -- marked with Inline_Always, do not instantiate body when within
4573 -- a generic context.
4575 if not Back_End_Inlining
4576 and then (Front_End_Inlining or else Has_Inline_Always)
4577 and then not Expander_Active
4579 Needs_Body := False;
4582 -- If the current context is generic, and the package being
4583 -- instantiated is declared within a formal package, there is no
4584 -- body to instantiate until the enclosing generic is instantiated
4585 -- and there is an actual for the formal package. If the formal
4586 -- package has parameters, we build a regular package instance for
4587 -- it, that precedes the original formal package declaration.
4589 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4591 Decl : constant Node_Id :=
4593 (Unit_Declaration_Node (Scope (Gen_Unit)));
4595 if Nkind (Decl) = N_Formal_Package_Declaration
4596 or else (Nkind (Decl) = N_Package_Declaration
4597 and then Is_List_Member (Decl)
4598 and then Present (Next (Decl))
4600 Nkind (Next (Decl)) =
4601 N_Formal_Package_Declaration)
4603 Needs_Body := False;
4609 -- For RCI unit calling stubs, we omit the instance body if the
4610 -- instance is the RCI library unit itself.
4612 -- However there is a special case for nested instances: in this case
4613 -- we do generate the instance body, as it might be required, e.g.
4614 -- because it provides stream attributes for some type used in the
4615 -- profile of a remote subprogram. This is consistent with 12.3(12),
4616 -- which indicates that the instance body occurs at the place of the
4617 -- instantiation, and thus is part of the RCI declaration, which is
4618 -- present on all client partitions (this is E.2.3(18)).
4620 -- Note that AI12-0002 may make it illegal at some point to have
4621 -- stream attributes defined in an RCI unit, in which case this
4622 -- special case will become unnecessary. In the meantime, there
4623 -- is known application code in production that depends on this
4624 -- being possible, so we definitely cannot eliminate the body in
4625 -- the case of nested instances for the time being.
4627 -- When we generate a nested instance body, calling stubs for any
4628 -- relevant subprogram will be inserted immediately after the
4629 -- subprogram declarations, and will take precedence over the
4630 -- subsequent (original) body. (The stub and original body will be
4631 -- complete homographs, but this is permitted in an instance).
4632 -- (Could we do better and remove the original body???)
4634 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4635 and then Comes_From_Source (N)
4636 and then Nkind (Parent (N)) = N_Compilation_Unit
4638 Needs_Body := False;
4642 -- Indicate that the enclosing scopes contain an instantiation,
4643 -- and that cleanup actions should be delayed until after the
4644 -- instance body is expanded.
4646 Check_Forward_Instantiation (Gen_Decl);
4647 if Nkind (N) = N_Package_Instantiation then
4649 Enclosing_Master : Entity_Id;
4652 -- Loop to search enclosing masters
4654 Enclosing_Master := Current_Scope;
4655 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4656 if Ekind (Enclosing_Master) = E_Package then
4657 if Is_Compilation_Unit (Enclosing_Master) then
4658 if In_Package_Body (Enclosing_Master) then
4659 Set_Delay_Subprogram_Descriptors
4660 (Body_Entity (Enclosing_Master));
4662 Set_Delay_Subprogram_Descriptors
4669 Enclosing_Master := Scope (Enclosing_Master);
4672 elsif Is_Generic_Unit (Enclosing_Master)
4673 or else Ekind (Enclosing_Master) = E_Void
4675 -- Cleanup actions will eventually be performed on the
4676 -- enclosing subprogram or package instance, if any.
4677 -- Enclosing scope is void in the formal part of a
4678 -- generic subprogram.
4683 if Ekind (Enclosing_Master) = E_Entry
4685 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4687 if not Expander_Active then
4691 Protected_Body_Subprogram (Enclosing_Master);
4695 Set_Delay_Cleanups (Enclosing_Master);
4697 while Ekind (Enclosing_Master) = E_Block loop
4698 Enclosing_Master := Scope (Enclosing_Master);
4701 if Is_Subprogram (Enclosing_Master) then
4702 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4704 elsif Is_Task_Type (Enclosing_Master) then
4706 TBP : constant Node_Id :=
4707 Get_Task_Body_Procedure
4710 if Present (TBP) then
4711 Set_Delay_Subprogram_Descriptors (TBP);
4712 Set_Delay_Cleanups (TBP);
4719 end loop Scope_Loop;
4722 -- Make entry in table
4724 Add_Pending_Instantiation (N, Act_Decl);
4728 Set_Categorization_From_Pragmas (Act_Decl);
4730 if Parent_Installed then
4734 Set_Instance_Spec (N, Act_Decl);
4736 -- If not a compilation unit, insert the package declaration before
4737 -- the original instantiation node.
4739 if Nkind (Parent (N)) /= N_Compilation_Unit then
4740 Mark_Rewrite_Insertion (Act_Decl);
4741 Insert_Before (N, Act_Decl);
4743 if Has_Aspects (N) then
4744 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4746 -- The pragma created for a Default_Storage_Pool aspect must
4747 -- appear ahead of the declarations in the instance spec.
4748 -- Analysis has placed it after the instance node, so remove
4749 -- it and reinsert it properly now.
4752 ASN : constant Node_Id := First (Aspect_Specifications (N));
4753 A_Name : constant Name_Id := Chars (Identifier (ASN));
4757 if A_Name = Name_Default_Storage_Pool then
4758 if No (Visible_Declarations (Act_Spec)) then
4759 Set_Visible_Declarations (Act_Spec, New_List);
4763 while Present (Decl) loop
4764 if Nkind (Decl) = N_Pragma then
4766 Prepend (Decl, Visible_Declarations (Act_Spec));
4778 -- For an instantiation that is a compilation unit, place
4779 -- declaration on current node so context is complete for analysis
4780 -- (including nested instantiations). If this is the main unit,
4781 -- the declaration eventually replaces the instantiation node.
4782 -- If the instance body is created later, it replaces the
4783 -- instance node, and the declaration is attached to it
4784 -- (see Build_Instance_Compilation_Unit_Nodes).
4787 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4789 -- The entity for the current unit is the newly created one,
4790 -- and all semantic information is attached to it.
4792 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4794 -- If this is the main unit, replace the main entity as well
4796 if Current_Sem_Unit = Main_Unit then
4797 Main_Unit_Entity := Act_Decl_Id;
4801 Set_Unit (Parent (N), Act_Decl);
4802 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4803 Set_Package_Instantiation (Act_Decl_Id, N);
4805 -- Process aspect specifications of the instance node, if any, to
4806 -- take into account categorization pragmas before analyzing the
4809 if Has_Aspects (N) then
4810 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4814 Set_Unit (Parent (N), N);
4815 Set_Body_Required (Parent (N), False);
4817 -- We never need elaboration checks on instantiations, since by
4818 -- definition, the body instantiation is elaborated at the same
4819 -- time as the spec instantiation.
4821 if Legacy_Elaboration_Checks then
4822 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4823 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4827 if Legacy_Elaboration_Checks then
4828 Check_Elab_Instantiation (N);
4831 -- Save the scenario for later examination by the ABE Processing
4834 Record_Elaboration_Scenario (N);
4836 -- The instantiation results in a guaranteed ABE
4838 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4839 -- Do not instantiate the corresponding body because gigi cannot
4840 -- handle certain types of premature instantiations.
4842 Remove_Dead_Instance (N);
4844 -- Create completing bodies for all subprogram declarations since
4845 -- their real bodies will not be instantiated.
4847 Provide_Completing_Bodies (Instance_Spec (N));
4850 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4852 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4853 First_Private_Entity (Act_Decl_Id));
4855 -- If the instantiation will receive a body, the unit will be
4856 -- transformed into a package body, and receive its own elaboration
4857 -- entity. Otherwise, the nature of the unit is now a package
4860 if Nkind (Parent (N)) = N_Compilation_Unit
4861 and then not Needs_Body
4863 Rewrite (N, Act_Decl);
4866 if Present (Corresponding_Body (Gen_Decl))
4867 or else Unit_Requires_Body (Gen_Unit)
4869 Set_Has_Completion (Act_Decl_Id);
4872 Check_Formal_Packages (Act_Decl_Id);
4874 Restore_Hidden_Primitives (Vis_Prims_List);
4875 Restore_Private_Views (Act_Decl_Id);
4877 Inherit_Context (Gen_Decl, N);
4879 if Parent_Installed then
4884 Env_Installed := False;
4887 Validate_Categorization_Dependency (N, Act_Decl_Id);
4889 -- There used to be a check here to prevent instantiations in local
4890 -- contexts if the No_Local_Allocators restriction was active. This
4891 -- check was removed by a binding interpretation in AI-95-00130/07,
4892 -- but we retain the code for documentation purposes.
4894 -- if Ekind (Act_Decl_Id) /= E_Void
4895 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4897 -- Check_Restriction (No_Local_Allocators, N);
4901 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4904 -- Check that if N is an instantiation of System.Dim_Float_IO or
4905 -- System.Dim_Integer_IO, the formal type has a dimension system.
4907 if Nkind (N) = N_Package_Instantiation
4908 and then Is_Dim_IO_Package_Instantiation (N)
4911 Assoc : constant Node_Id := First (Generic_Associations (N));
4913 if not Has_Dimension_System
4914 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4916 Error_Msg_N ("type with a dimension system expected", Assoc);
4922 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4923 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4926 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4927 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4928 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4929 Style_Check := Saved_Style_Check;
4932 when Instantiation_Error =>
4933 if Parent_Installed then
4937 if Env_Installed then
4941 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4942 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4943 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4944 Style_Check := Saved_Style_Check;
4945 end Analyze_Package_Instantiation;
4947 --------------------------
4948 -- Inline_Instance_Body --
4949 --------------------------
4951 -- WARNING: This routine manages SPARK regions. Return statements must be
4952 -- replaced by gotos which jump to the end of the routine and restore the
4955 procedure Inline_Instance_Body
4957 Gen_Unit : Entity_Id;
4960 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4962 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4963 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4964 Gen_Comp : constant Entity_Id :=
4965 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4967 Scope_Stack_Depth : constant Pos :=
4968 Scope_Stack.Last - Scope_Stack.First + 1;
4970 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4971 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4972 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4974 Curr_Scope : Entity_Id := Empty;
4975 List : Elist_Id := No_Elist; -- init to avoid warning
4976 N_Instances : Nat := 0;
4977 Num_Inner : Nat := 0;
4978 Num_Scopes : Nat := 0;
4979 Removed : Boolean := False;
4984 -- Case of generic unit defined in another unit. We must remove the
4985 -- complete context of the current unit to install that of the generic.
4987 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4989 -- Add some comments for the following two loops ???
4992 while Present (S) and then S /= Standard_Standard loop
4994 Num_Scopes := Num_Scopes + 1;
4996 Use_Clauses (Num_Scopes) :=
4998 (Scope_Stack.Last - Num_Scopes + 1).
5000 End_Use_Clauses (Use_Clauses (Num_Scopes));
5002 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5003 or else Scope_Stack.Table
5004 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5007 exit when Is_Generic_Instance (S)
5008 and then (In_Package_Body (S)
5009 or else Ekind (S) = E_Procedure
5010 or else Ekind (S) = E_Function);
5014 Vis := Is_Immediately_Visible (Gen_Comp);
5016 -- Find and save all enclosing instances
5021 and then S /= Standard_Standard
5023 if Is_Generic_Instance (S) then
5024 N_Instances := N_Instances + 1;
5025 Instances (N_Instances) := S;
5027 exit when In_Package_Body (S);
5033 -- Remove context of current compilation unit, unless we are within a
5034 -- nested package instantiation, in which case the context has been
5035 -- removed previously.
5037 -- If current scope is the body of a child unit, remove context of
5038 -- spec as well. If an enclosing scope is an instance body, the
5039 -- context has already been removed, but the entities in the body
5040 -- must be made invisible as well.
5043 while Present (S) and then S /= Standard_Standard loop
5044 if Is_Generic_Instance (S)
5045 and then (In_Package_Body (S)
5046 or else Ekind (S) in E_Procedure | E_Function)
5048 -- We still have to remove the entities of the enclosing
5049 -- instance from direct visibility.
5054 E := First_Entity (S);
5055 while Present (E) loop
5056 Set_Is_Immediately_Visible (E, False);
5065 or else (Ekind (Curr_Unit) = E_Package_Body
5066 and then S = Spec_Entity (Curr_Unit))
5067 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5068 and then S = Corresponding_Spec
5069 (Unit_Declaration_Node (Curr_Unit)))
5073 -- Remove entities in current scopes from visibility, so that
5074 -- instance body is compiled in a clean environment.
5076 List := Save_Scope_Stack (Handle_Use => False);
5078 if Is_Child_Unit (S) then
5080 -- Remove child unit from stack, as well as inner scopes.
5081 -- Removing the context of a child unit removes parent units
5084 while Current_Scope /= S loop
5085 Num_Inner := Num_Inner + 1;
5086 Inner_Scopes (Num_Inner) := Current_Scope;
5091 Remove_Context (Curr_Comp);
5095 Remove_Context (Curr_Comp);
5098 if Ekind (Curr_Unit) = E_Package_Body then
5099 Remove_Context (Library_Unit (Curr_Comp));
5106 pragma Assert (Num_Inner < Num_Scopes);
5108 Push_Scope (Standard_Standard);
5109 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5111 -- The inlined package body is analyzed with the configuration state
5112 -- of the context prior to the scope manipulations performed above.
5114 -- ??? shouldn't this also use the warning state of the context prior
5115 -- to the scope manipulations?
5117 Instantiate_Package_Body
5119 ((Act_Decl => Act_Decl,
5120 Config_Switches => Config_Attrs,
5121 Current_Sem_Unit => Current_Sem_Unit,
5122 Expander_Status => Expander_Active,
5124 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5125 Scope_Suppress => Scope_Suppress,
5126 Warnings => Save_Warnings)),
5127 Inlined_Body => True);
5133 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5135 -- Reset Generic_Instance flag so that use clauses can be installed
5136 -- in the proper order. (See Use_One_Package for effect of enclosing
5137 -- instances on processing of use clauses).
5139 for J in 1 .. N_Instances loop
5140 Set_Is_Generic_Instance (Instances (J), False);
5144 Install_Context (Curr_Comp, Chain => False);
5146 if Present (Curr_Scope)
5147 and then Is_Child_Unit (Curr_Scope)
5149 Push_Scope (Curr_Scope);
5150 Set_Is_Immediately_Visible (Curr_Scope);
5152 -- Finally, restore inner scopes as well
5154 for J in reverse 1 .. Num_Inner loop
5155 Push_Scope (Inner_Scopes (J));
5159 Restore_Scope_Stack (List, Handle_Use => False);
5161 if Present (Curr_Scope)
5163 (In_Private_Part (Curr_Scope)
5164 or else In_Package_Body (Curr_Scope))
5166 -- Install private declaration of ancestor units, which are
5167 -- currently available. Restore_Scope_Stack and Install_Context
5168 -- only install the visible part of parents.
5173 Par := Scope (Curr_Scope);
5174 while (Present (Par)) and then Par /= Standard_Standard loop
5175 Install_Private_Declarations (Par);
5182 -- Restore use clauses. For a child unit, use clauses in the parents
5183 -- are restored when installing the context, so only those in inner
5184 -- scopes (and those local to the child unit itself) need to be
5185 -- installed explicitly.
5187 if Is_Child_Unit (Curr_Unit) and then Removed then
5188 for J in reverse 1 .. Num_Inner + 1 loop
5189 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5191 Install_Use_Clauses (Use_Clauses (J));
5195 for J in reverse 1 .. Num_Scopes loop
5196 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5198 Install_Use_Clauses (Use_Clauses (J));
5202 -- Restore status of instances. If one of them is a body, make its
5203 -- local entities visible again.
5210 for J in 1 .. N_Instances loop
5211 Inst := Instances (J);
5212 Set_Is_Generic_Instance (Inst, True);
5214 if In_Package_Body (Inst)
5215 or else Ekind (S) in E_Procedure | E_Function
5217 E := First_Entity (Instances (J));
5218 while Present (E) loop
5219 Set_Is_Immediately_Visible (E);
5226 -- If generic unit is in current unit, current context is correct. Note
5227 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5228 -- enclosing scopes were removed.
5231 Instantiate_Package_Body
5233 ((Act_Decl => Act_Decl,
5234 Config_Switches => Save_Config_Switches,
5235 Current_Sem_Unit => Current_Sem_Unit,
5236 Expander_Status => Expander_Active,
5238 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5239 Scope_Suppress => Scope_Suppress,
5240 Warnings => Save_Warnings)),
5241 Inlined_Body => True);
5243 end Inline_Instance_Body;
5245 -------------------------------------
5246 -- Analyze_Procedure_Instantiation --
5247 -------------------------------------
5249 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5251 Analyze_Subprogram_Instantiation (N, E_Procedure);
5252 end Analyze_Procedure_Instantiation;
5254 -----------------------------------
5255 -- Need_Subprogram_Instance_Body --
5256 -----------------------------------
5258 function Need_Subprogram_Instance_Body
5260 Subp : Entity_Id) return Boolean
5262 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5263 -- Return True if E is an inlined subprogram, an inlined renaming or a
5264 -- subprogram nested in an inlined subprogram. The inlining machinery
5265 -- totally disregards nested subprograms since it considers that they
5266 -- will always be compiled if the parent is (see Inline.Is_Nested).
5268 ------------------------------------
5269 -- Is_Inlined_Or_Child_Of_Inlined --
5270 ------------------------------------
5272 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5276 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5281 while Scop /= Standard_Standard loop
5282 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5286 Scop := Scope (Scop);
5290 end Is_Inlined_Or_Child_Of_Inlined;
5293 -- Must be in the main unit or inlined (or child of inlined)
5295 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5297 -- Must be generating code or analyzing code in GNATprove mode
5299 and then (Operating_Mode = Generate_Code
5300 or else (Operating_Mode = Check_Semantics
5301 and then GNATprove_Mode))
5303 -- The body is needed when generating code (full expansion) and in
5304 -- in GNATprove mode (special expansion) for formal verification of
5307 and then (Expander_Active or GNATprove_Mode)
5309 -- No point in inlining if ABE is inevitable
5311 and then not Is_Known_Guaranteed_ABE (N)
5313 -- Or if subprogram is eliminated
5315 and then not Is_Eliminated (Subp)
5317 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5320 -- Here if not inlined, or we ignore the inlining
5325 end Need_Subprogram_Instance_Body;
5327 --------------------------------------
5328 -- Analyze_Subprogram_Instantiation --
5329 --------------------------------------
5331 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5332 -- must be replaced by gotos which jump to the end of the routine in order
5333 -- to restore the Ghost and SPARK modes.
5335 procedure Analyze_Subprogram_Instantiation
5339 Errs : constant Nat := Serious_Errors_Detected;
5340 Gen_Id : constant Node_Id := Name (N);
5341 Inst_Id : constant Entity_Id := Defining_Entity (N);
5342 Anon_Id : constant Entity_Id :=
5343 Make_Defining_Identifier (Sloc (Inst_Id),
5344 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5345 Loc : constant Source_Ptr := Sloc (N);
5347 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5352 Env_Installed : Boolean := False;
5353 Gen_Unit : Entity_Id;
5355 Pack_Id : Entity_Id;
5356 Parent_Installed : Boolean := False;
5358 Renaming_List : List_Id;
5359 -- The list of declarations that link formals and actuals of the
5360 -- instance. These are subtype declarations for formal types, and
5361 -- renaming declarations for other formals. The subprogram declaration
5362 -- for the instance is then appended to the list, and the last item on
5363 -- the list is the renaming declaration for the instance.
5365 procedure Analyze_Instance_And_Renamings;
5366 -- The instance must be analyzed in a context that includes the mappings
5367 -- of generic parameters into actuals. We create a package declaration
5368 -- for this purpose, and a subprogram with an internal name within the
5369 -- package. The subprogram instance is simply an alias for the internal
5370 -- subprogram, declared in the current scope.
5372 procedure Build_Subprogram_Renaming;
5373 -- If the subprogram is recursive, there are occurrences of the name of
5374 -- the generic within the body, which must resolve to the current
5375 -- instance. We add a renaming declaration after the declaration, which
5376 -- is available in the instance body, as well as in the analysis of
5377 -- aspects that appear in the generic. This renaming declaration is
5378 -- inserted after the instance declaration which it renames.
5380 ------------------------------------
5381 -- Analyze_Instance_And_Renamings --
5382 ------------------------------------
5384 procedure Analyze_Instance_And_Renamings is
5385 Def_Ent : constant Entity_Id := Defining_Entity (N);
5386 Pack_Decl : Node_Id;
5389 if Nkind (Parent (N)) = N_Compilation_Unit then
5391 -- For the case of a compilation unit, the container package has
5392 -- the same name as the instantiation, to insure that the binder
5393 -- calls the elaboration procedure with the right name. Copy the
5394 -- entity of the instance, which may have compilation level flags
5395 -- (e.g. Is_Child_Unit) set.
5397 Pack_Id := New_Copy (Def_Ent);
5400 -- Otherwise we use the name of the instantiation concatenated
5401 -- with its source position to ensure uniqueness if there are
5402 -- several instantiations with the same name.
5405 Make_Defining_Identifier (Loc,
5406 Chars => New_External_Name
5407 (Related_Id => Chars (Def_Ent),
5409 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5413 Make_Package_Declaration (Loc,
5414 Specification => Make_Package_Specification (Loc,
5415 Defining_Unit_Name => Pack_Id,
5416 Visible_Declarations => Renaming_List,
5417 End_Label => Empty));
5419 Set_Instance_Spec (N, Pack_Decl);
5420 Set_Is_Generic_Instance (Pack_Id);
5421 Set_Debug_Info_Needed (Pack_Id);
5423 -- Case of not a compilation unit
5425 if Nkind (Parent (N)) /= N_Compilation_Unit then
5426 Mark_Rewrite_Insertion (Pack_Decl);
5427 Insert_Before (N, Pack_Decl);
5428 Set_Has_Completion (Pack_Id);
5430 -- Case of an instantiation that is a compilation unit
5432 -- Place declaration on current node so context is complete for
5433 -- analysis (including nested instantiations), and for use in a
5434 -- context_clause (see Analyze_With_Clause).
5437 Set_Unit (Parent (N), Pack_Decl);
5438 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5441 Analyze (Pack_Decl);
5442 Check_Formal_Packages (Pack_Id);
5444 -- Body of the enclosing package is supplied when instantiating the
5445 -- subprogram body, after semantic analysis is completed.
5447 if Nkind (Parent (N)) = N_Compilation_Unit then
5449 -- Remove package itself from visibility, so it does not
5450 -- conflict with subprogram.
5452 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5454 -- Set name and scope of internal subprogram so that the proper
5455 -- external name will be generated. The proper scope is the scope
5456 -- of the wrapper package. We need to generate debugging info for
5457 -- the internal subprogram, so set flag accordingly.
5459 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5460 Set_Scope (Anon_Id, Scope (Pack_Id));
5462 -- Mark wrapper package as referenced, to avoid spurious warnings
5463 -- if the instantiation appears in various with_ clauses of
5464 -- subunits of the main unit.
5466 Set_Referenced (Pack_Id);
5469 Set_Is_Generic_Instance (Anon_Id);
5470 Set_Debug_Info_Needed (Anon_Id);
5471 Act_Decl_Id := New_Copy (Anon_Id);
5473 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5474 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5475 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5477 -- Subprogram instance comes from source only if generic does
5479 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5481 -- If the instance is a child unit, mark the Id accordingly. Mark
5482 -- the anonymous entity as well, which is the real subprogram and
5483 -- which is used when the instance appears in a context clause.
5484 -- Similarly, propagate the Is_Eliminated flag to handle properly
5485 -- nested eliminated subprograms.
5487 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5488 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5489 New_Overloaded_Entity (Act_Decl_Id);
5490 Check_Eliminated (Act_Decl_Id);
5491 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5493 if Nkind (Parent (N)) = N_Compilation_Unit then
5495 -- In compilation unit case, kill elaboration checks on the
5496 -- instantiation, since they are never needed - the body is
5497 -- instantiated at the same point as the spec.
5499 if Legacy_Elaboration_Checks then
5500 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5501 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5504 Set_Is_Compilation_Unit (Anon_Id);
5505 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5508 -- The instance is not a freezing point for the new subprogram.
5509 -- The anonymous subprogram may have a freeze node, created for
5510 -- some delayed aspects. This freeze node must not be inherited
5511 -- by the visible subprogram entity.
5513 Set_Is_Frozen (Act_Decl_Id, False);
5514 Set_Freeze_Node (Act_Decl_Id, Empty);
5516 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5517 Valid_Operator_Definition (Act_Decl_Id);
5520 Set_Alias (Act_Decl_Id, Anon_Id);
5521 Set_Has_Completion (Act_Decl_Id);
5522 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5524 if Nkind (Parent (N)) = N_Compilation_Unit then
5525 Set_Body_Required (Parent (N), False);
5527 end Analyze_Instance_And_Renamings;
5529 -------------------------------
5530 -- Build_Subprogram_Renaming --
5531 -------------------------------
5533 procedure Build_Subprogram_Renaming is
5534 Renaming_Decl : Node_Id;
5535 Unit_Renaming : Node_Id;
5539 Make_Subprogram_Renaming_Declaration (Loc,
5542 (Specification (Original_Node (Gen_Decl)),
5544 Instantiating => True),
5545 Name => New_Occurrence_Of (Anon_Id, Loc));
5547 -- The generic may be a child unit. The renaming needs an identifier
5548 -- with the proper name.
5550 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5551 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5553 -- If there is a formal subprogram with the same name as the unit
5554 -- itself, do not add this renaming declaration, to prevent
5555 -- ambiguities when there is a call with that name in the body.
5556 -- This is a partial and ugly fix for one ACATS test. ???
5558 Renaming_Decl := First (Renaming_List);
5559 while Present (Renaming_Decl) loop
5560 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5562 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5567 Next (Renaming_Decl);
5570 if No (Renaming_Decl) then
5571 Append (Unit_Renaming, Renaming_List);
5573 end Build_Subprogram_Renaming;
5577 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5578 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5579 Saved_ISMP : constant Boolean :=
5580 Ignore_SPARK_Mode_Pragmas_In_Instance;
5581 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5582 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5583 -- Save the Ghost and SPARK mode-related data to restore on exit
5585 Vis_Prims_List : Elist_Id := No_Elist;
5586 -- List of primitives made temporarily visible in the instantiation
5587 -- to match the visibility of the formal type
5589 -- Start of processing for Analyze_Subprogram_Instantiation
5592 -- Preserve relevant elaboration-related attributes of the context which
5593 -- are no longer available or very expensive to recompute once analysis,
5594 -- resolution, and expansion are over.
5596 Mark_Elaboration_Attributes
5603 -- Very first thing: check for special Text_IO unit in case we are
5604 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5605 -- such an instantiation is bogus (these are packages, not subprograms),
5606 -- but we get a better error message if we do this.
5608 Check_Text_IO_Special_Unit (Gen_Id);
5610 -- Make node global for error reporting
5612 Instantiation_Node := N;
5614 -- For package instantiations we turn off style checks, because they
5615 -- will have been emitted in the generic. For subprogram instantiations
5616 -- we want to apply at least the check on overriding indicators so we
5617 -- do not modify the style check status.
5619 -- The renaming declarations for the actuals do not come from source and
5620 -- will not generate spurious warnings.
5622 Preanalyze_Actuals (N);
5625 Env_Installed := True;
5626 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5627 Gen_Unit := Entity (Gen_Id);
5629 -- A subprogram instantiation is Ghost when it is subject to pragma
5630 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5631 -- that any nodes generated during analysis and expansion are marked as
5634 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5636 Generate_Reference (Gen_Unit, Gen_Id);
5638 if Nkind (Gen_Id) = N_Identifier
5639 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5642 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5645 if Etype (Gen_Unit) = Any_Type then
5650 -- Verify that it is a generic subprogram of the right kind, and that
5651 -- it does not lead to a circular instantiation.
5653 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5655 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5657 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5659 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5661 elsif In_Open_Scopes (Gen_Unit) then
5662 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5665 Set_Ekind (Inst_Id, K);
5666 Set_Scope (Inst_Id, Current_Scope);
5668 Set_Entity (Gen_Id, Gen_Unit);
5669 Set_Is_Instantiated (Gen_Unit);
5671 if In_Extended_Main_Source_Unit (N) then
5672 Generate_Reference (Gen_Unit, N);
5675 -- If renaming, get original unit
5677 if Present (Renamed_Object (Gen_Unit))
5678 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5680 Gen_Unit := Renamed_Object (Gen_Unit);
5681 Set_Is_Instantiated (Gen_Unit);
5682 Generate_Reference (Gen_Unit, N);
5685 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5686 Error_Msg_Node_2 := Current_Scope;
5688 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5689 Circularity_Detected := True;
5690 Restore_Hidden_Primitives (Vis_Prims_List);
5694 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5696 -- Initialize renamings map, for error checking
5698 Generic_Renamings.Set_Last (0);
5699 Generic_Renamings_HTable.Reset;
5701 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5703 -- Copy original generic tree, to produce text for instantiation
5707 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5709 -- Inherit overriding indicator from instance node
5711 Act_Spec := Specification (Act_Tree);
5712 Set_Must_Override (Act_Spec, Must_Override (N));
5713 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5716 Analyze_Associations
5718 Formals => Generic_Formal_Declarations (Act_Tree),
5719 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5721 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5723 -- The subprogram itself cannot contain a nested instance, so the
5724 -- current parent is left empty.
5726 Set_Instance_Env (Gen_Unit, Empty);
5728 -- Build the subprogram declaration, which does not appear in the
5729 -- generic template, and give it a sloc consistent with that of the
5732 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5733 Set_Generic_Parent (Act_Spec, Gen_Unit);
5735 Make_Subprogram_Declaration (Sloc (Act_Spec),
5736 Specification => Act_Spec);
5738 -- The aspects have been copied previously, but they have to be
5739 -- linked explicitly to the new subprogram declaration. Explicit
5740 -- pre/postconditions on the instance are analyzed below, in a
5743 Move_Aspects (Act_Tree, To => Act_Decl);
5744 Set_Categorization_From_Pragmas (Act_Decl);
5746 if Parent_Installed then
5750 Append (Act_Decl, Renaming_List);
5752 -- Contract-related source pragmas that follow a generic subprogram
5753 -- must be instantiated explicitly because they are not part of the
5754 -- subprogram template.
5756 Instantiate_Subprogram_Contract
5757 (Original_Node (Gen_Decl), Renaming_List);
5759 Build_Subprogram_Renaming;
5761 -- If the context of the instance is subject to SPARK_Mode "off" or
5762 -- the annotation is altogether missing, set the global flag which
5763 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5764 -- the instance. This should be done prior to analyzing the instance.
5766 if SPARK_Mode /= On then
5767 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5770 -- If the context of an instance is not subject to SPARK_Mode "off",
5771 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5772 -- the latter should be the one applicable to the instance.
5774 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5775 and then Saved_SM /= Off
5776 and then Present (SPARK_Pragma (Gen_Unit))
5778 Set_SPARK_Mode (Gen_Unit);
5781 Analyze_Instance_And_Renamings;
5783 -- Restore SPARK_Mode from the context after analysis of the package
5784 -- declaration, so that the SPARK_Mode on the generic spec does not
5785 -- apply to the pending instance for the instance body.
5787 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5788 and then Saved_SM /= Off
5789 and then Present (SPARK_Pragma (Gen_Unit))
5791 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5794 -- If the generic is marked Import (Intrinsic), then so is the
5795 -- instance. This indicates that there is no body to instantiate. If
5796 -- generic is marked inline, so it the instance, and the anonymous
5797 -- subprogram it renames. If inlined, or else if inlining is enabled
5798 -- for the compilation, we generate the instance body even if it is
5799 -- not within the main unit.
5801 if Is_Intrinsic_Subprogram (Gen_Unit) then
5802 Set_Is_Intrinsic_Subprogram (Anon_Id);
5803 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5805 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5806 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5810 -- Inherit convention from generic unit. Intrinsic convention, as for
5811 -- an instance of unchecked conversion, is not inherited because an
5812 -- explicit Ada instance has been created.
5814 if Has_Convention_Pragma (Gen_Unit)
5815 and then Convention (Gen_Unit) /= Convention_Intrinsic
5817 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5818 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5821 Generate_Definition (Act_Decl_Id);
5823 -- Inherit all inlining-related flags which apply to the generic in
5824 -- the subprogram and its declaration.
5826 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5827 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5829 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5830 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5832 Set_Has_Pragma_Inline_Always
5833 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5834 Set_Has_Pragma_Inline_Always
5835 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5837 Set_Has_Pragma_No_Inline
5838 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5839 Set_Has_Pragma_No_Inline
5840 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5842 -- Propagate No_Return if pragma applied to generic unit. This must
5843 -- be done explicitly because pragma does not appear in generic
5844 -- declaration (unlike the aspect case).
5846 if No_Return (Gen_Unit) then
5847 Set_No_Return (Act_Decl_Id);
5848 Set_No_Return (Anon_Id);
5851 -- Mark both the instance spec and the anonymous package in case the
5852 -- body is instantiated at a later pass. This preserves the original
5853 -- context in effect for the body.
5855 if SPARK_Mode /= On then
5856 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5857 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5860 if Legacy_Elaboration_Checks
5861 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5863 Check_Elab_Instantiation (N);
5866 -- Save the scenario for later examination by the ABE Processing
5869 Record_Elaboration_Scenario (N);
5871 -- The instantiation results in a guaranteed ABE. Create a completing
5872 -- body for the subprogram declaration because the real body will not
5875 if Is_Known_Guaranteed_ABE (N) then
5876 Provide_Completing_Bodies (Instance_Spec (N));
5879 if Is_Dispatching_Operation (Act_Decl_Id)
5880 and then Ada_Version >= Ada_2005
5886 Formal := First_Formal (Act_Decl_Id);
5887 while Present (Formal) loop
5888 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5889 and then Is_Controlling_Formal (Formal)
5890 and then not Can_Never_Be_Null (Formal)
5893 ("access parameter& is controlling,", N, Formal);
5895 ("\corresponding parameter of & must be explicitly "
5896 & "null-excluding", N, Gen_Id);
5899 Next_Formal (Formal);
5904 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5906 Validate_Categorization_Dependency (N, Act_Decl_Id);
5908 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5909 Inherit_Context (Gen_Decl, N);
5911 Restore_Private_Views (Pack_Id, False);
5913 -- If the context requires a full instantiation, mark node for
5914 -- subsequent construction of the body.
5916 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5917 Check_Forward_Instantiation (Gen_Decl);
5919 -- The wrapper package is always delayed, because it does not
5920 -- constitute a freeze point, but to insure that the freeze node
5921 -- is placed properly, it is created directly when instantiating
5922 -- the body (otherwise the freeze node might appear to early for
5923 -- nested instantiations).
5925 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5926 Rewrite (N, Unit (Parent (N)));
5927 Set_Unit (Parent (N), N);
5930 -- Replace instance node for library-level instantiations of
5931 -- intrinsic subprograms.
5933 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5934 Rewrite (N, Unit (Parent (N)));
5935 Set_Unit (Parent (N), N);
5938 if Parent_Installed then
5942 Restore_Hidden_Primitives (Vis_Prims_List);
5944 Env_Installed := False;
5945 Generic_Renamings.Set_Last (0);
5946 Generic_Renamings_HTable.Reset;
5950 -- Analyze aspects in declaration if no errors appear in the instance.
5952 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5953 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5956 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5957 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5958 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5961 when Instantiation_Error =>
5962 if Parent_Installed then
5966 if Env_Installed then
5970 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5971 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5972 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5973 end Analyze_Subprogram_Instantiation;
5975 -------------------------
5976 -- Get_Associated_Node --
5977 -------------------------
5979 function Get_Associated_Node (N : Node_Id) return Node_Id is
5983 Assoc := Associated_Node (N);
5985 if Nkind (Assoc) /= Nkind (N) then
5988 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5992 -- If the node is part of an inner generic, it may itself have been
5993 -- remapped into a further generic copy. Associated_Node is otherwise
5994 -- used for the entity of the node, and will be of a different node
5995 -- kind, or else N has been rewritten as a literal or function call.
5997 while Present (Associated_Node (Assoc))
5998 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6000 Assoc := Associated_Node (Assoc);
6003 -- Follow an additional link in case the final node was rewritten.
6004 -- This can only happen with nested generic units.
6006 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6007 and then Present (Associated_Node (Assoc))
6008 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6009 | N_Explicit_Dereference
6014 Assoc := Associated_Node (Assoc);
6017 -- An additional special case: an unconstrained type in an object
6018 -- declaration may have been rewritten as a local subtype constrained
6019 -- by the expression in the declaration. We need to recover the
6020 -- original entity, which may be global.
6022 if Present (Original_Node (Assoc))
6023 and then Nkind (Parent (N)) = N_Object_Declaration
6025 Assoc := Original_Node (Assoc);
6030 end Get_Associated_Node;
6032 ----------------------------
6033 -- Build_Function_Wrapper --
6034 ----------------------------
6036 function Build_Function_Wrapper
6037 (Formal_Subp : Entity_Id;
6038 Actual_Subp : Entity_Id) return Node_Id
6040 Loc : constant Source_Ptr := Sloc (Current_Scope);
6041 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6044 Func_Name : Node_Id;
6046 Parm_Type : Node_Id;
6047 Profile : List_Id := New_List;
6054 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6056 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6057 Set_Ekind (Func, E_Function);
6058 Set_Is_Generic_Actual_Subprogram (Func);
6060 Actuals := New_List;
6061 Profile := New_List;
6063 Act_F := First_Formal (Actual_Subp);
6064 Form_F := First_Formal (Formal_Subp);
6065 while Present (Form_F) loop
6067 -- Create new formal for profile of wrapper, and add a reference
6068 -- to it in the list of actuals for the enclosing call. The name
6069 -- must be that of the formal in the formal subprogram, because
6070 -- calls to it in the generic body may use named associations.
6072 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6075 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6078 Make_Parameter_Specification (Loc,
6079 Defining_Identifier => New_F,
6080 Parameter_Type => Parm_Type));
6082 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6083 Next_Formal (Form_F);
6085 if Present (Act_F) then
6086 Next_Formal (Act_F);
6091 Make_Function_Specification (Loc,
6092 Defining_Unit_Name => Func,
6093 Parameter_Specifications => Profile,
6094 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6097 Make_Expression_Function (Loc,
6098 Specification => Spec,
6100 Make_Function_Call (Loc,
6102 Parameter_Associations => Actuals));
6105 end Build_Function_Wrapper;
6107 ----------------------------
6108 -- Build_Operator_Wrapper --
6109 ----------------------------
6111 function Build_Operator_Wrapper
6112 (Formal_Subp : Entity_Id;
6113 Actual_Subp : Entity_Id) return Node_Id
6115 Loc : constant Source_Ptr := Sloc (Current_Scope);
6116 Ret_Type : constant Entity_Id :=
6117 Get_Instance_Of (Etype (Formal_Subp));
6118 Op_Type : constant Entity_Id :=
6119 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6120 Is_Binary : constant Boolean :=
6121 Present (Next_Formal (First_Formal (Formal_Subp)));
6124 Expr : Node_Id := Empty;
6132 Op_Name := Chars (Actual_Subp);
6134 -- Create entities for wrapper function and its formals
6136 F1 := Make_Temporary (Loc, 'A');
6137 F2 := Make_Temporary (Loc, 'B');
6138 L := New_Occurrence_Of (F1, Loc);
6139 R := New_Occurrence_Of (F2, Loc);
6141 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6142 Set_Ekind (Func, E_Function);
6143 Set_Is_Generic_Actual_Subprogram (Func);
6146 Make_Function_Specification (Loc,
6147 Defining_Unit_Name => Func,
6148 Parameter_Specifications => New_List (
6149 Make_Parameter_Specification (Loc,
6150 Defining_Identifier => F1,
6151 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6152 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6155 Append_To (Parameter_Specifications (Spec),
6156 Make_Parameter_Specification (Loc,
6157 Defining_Identifier => F2,
6158 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6161 -- Build expression as a function call, or as an operator node
6162 -- that corresponds to the name of the actual, starting with
6163 -- binary operators.
6165 if Op_Name not in Any_Operator_Name then
6167 Make_Function_Call (Loc,
6169 New_Occurrence_Of (Actual_Subp, Loc),
6170 Parameter_Associations => New_List (L));
6173 Append_To (Parameter_Associations (Expr), R);
6178 elsif Is_Binary then
6179 if Op_Name = Name_Op_And then
6180 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6181 elsif Op_Name = Name_Op_Or then
6182 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6183 elsif Op_Name = Name_Op_Xor then
6184 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6185 elsif Op_Name = Name_Op_Eq then
6186 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6187 elsif Op_Name = Name_Op_Ne then
6188 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6189 elsif Op_Name = Name_Op_Le then
6190 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6191 elsif Op_Name = Name_Op_Gt then
6192 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6193 elsif Op_Name = Name_Op_Ge then
6194 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6195 elsif Op_Name = Name_Op_Lt then
6196 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6197 elsif Op_Name = Name_Op_Add then
6198 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6199 elsif Op_Name = Name_Op_Subtract then
6200 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6201 elsif Op_Name = Name_Op_Concat then
6202 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6203 elsif Op_Name = Name_Op_Multiply then
6204 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6205 elsif Op_Name = Name_Op_Divide then
6206 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6207 elsif Op_Name = Name_Op_Mod then
6208 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6209 elsif Op_Name = Name_Op_Rem then
6210 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6211 elsif Op_Name = Name_Op_Expon then
6212 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6218 if Op_Name = Name_Op_Add then
6219 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6220 elsif Op_Name = Name_Op_Subtract then
6221 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6222 elsif Op_Name = Name_Op_Abs then
6223 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6224 elsif Op_Name = Name_Op_Not then
6225 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6230 Make_Expression_Function (Loc,
6231 Specification => Spec,
6232 Expression => Expr);
6235 end Build_Operator_Wrapper;
6237 -----------------------------------
6238 -- Build_Subprogram_Decl_Wrapper --
6239 -----------------------------------
6241 function Build_Subprogram_Decl_Wrapper
6242 (Formal_Subp : Entity_Id) return Node_Id
6244 Loc : constant Source_Ptr := Sloc (Current_Scope);
6245 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6248 Parm_Spec : Node_Id;
6249 Profile : List_Id := New_List;
6256 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6257 Set_Ekind (Subp, Ekind (Formal_Subp));
6258 Set_Is_Generic_Actual_Subprogram (Subp);
6260 Profile := Parameter_Specifications (
6262 (Specification (Unit_Declaration_Node (Formal_Subp))));
6264 Form_F := First_Formal (Formal_Subp);
6265 Parm_Spec := First (Profile);
6267 -- Create new entities for the formals. Reset entities so that
6268 -- parameter types are properly resolved when wrapper declaration
6271 while Present (Parm_Spec) loop
6272 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6273 Set_Defining_Identifier (Parm_Spec, New_F);
6274 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6276 Next_Formal (Form_F);
6279 if Ret_Type = Standard_Void_Type then
6281 Make_Procedure_Specification (Loc,
6282 Defining_Unit_Name => Subp,
6283 Parameter_Specifications => Profile);
6286 Make_Function_Specification (Loc,
6287 Defining_Unit_Name => Subp,
6288 Parameter_Specifications => Profile,
6289 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6293 Make_Subprogram_Declaration (Loc, Specification => Spec);
6296 end Build_Subprogram_Decl_Wrapper;
6298 -----------------------------------
6299 -- Build_Subprogram_Body_Wrapper --
6300 -----------------------------------
6302 function Build_Subprogram_Body_Wrapper
6303 (Formal_Subp : Entity_Id;
6304 Actual_Name : Node_Id) return Node_Id
6306 Loc : constant Source_Ptr := Sloc (Current_Scope);
6307 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6308 Spec_Node : constant Node_Id :=
6310 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6313 Body_Node : Node_Id;
6316 Actuals := New_List;
6317 Act := First (Parameter_Specifications (Spec_Node));
6319 while Present (Act) loop
6321 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6325 if Ret_Type = Standard_Void_Type then
6326 Stmt := Make_Procedure_Call_Statement (Loc,
6327 Name => Actual_Name,
6328 Parameter_Associations => Actuals);
6331 Stmt := Make_Simple_Return_Statement (Loc,
6333 Make_Function_Call (Loc,
6334 Name => Actual_Name,
6335 Parameter_Associations => Actuals));
6338 Body_Node := Make_Subprogram_Body (Loc,
6339 Specification => Spec_Node,
6340 Declarations => New_List,
6341 Handled_Statement_Sequence =>
6342 Make_Handled_Sequence_Of_Statements (Loc,
6343 Statements => New_List (Stmt)));
6346 end Build_Subprogram_Body_Wrapper;
6348 -------------------------------------------
6349 -- Build_Instance_Compilation_Unit_Nodes --
6350 -------------------------------------------
6352 procedure Build_Instance_Compilation_Unit_Nodes
6357 Decl_Cunit : Node_Id;
6358 Body_Cunit : Node_Id;
6360 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6361 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6364 -- A new compilation unit node is built for the instance declaration
6367 Make_Compilation_Unit (Sloc (N),
6368 Context_Items => Empty_List,
6370 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6372 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6374 -- The new compilation unit is linked to its body, but both share the
6375 -- same file, so we do not set Body_Required on the new unit so as not
6376 -- to create a spurious dependency on a non-existent body in the ali.
6377 -- This simplifies CodePeer unit traversal.
6379 -- We use the original instantiation compilation unit as the resulting
6380 -- compilation unit of the instance, since this is the main unit.
6382 Rewrite (N, Act_Body);
6384 -- Propagate the aspect specifications from the package body template to
6385 -- the instantiated version of the package body.
6387 if Has_Aspects (Act_Body) then
6388 Set_Aspect_Specifications
6389 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6392 Body_Cunit := Parent (N);
6394 -- The two compilation unit nodes are linked by the Library_Unit field
6396 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6397 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6399 -- Preserve the private nature of the package if needed
6401 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6403 -- If the instance is not the main unit, its context, categorization
6404 -- and elaboration entity are not relevant to the compilation.
6406 if Body_Cunit /= Cunit (Main_Unit) then
6407 Make_Instance_Unit (Body_Cunit, In_Main => False);
6411 -- The context clause items on the instantiation, which are now attached
6412 -- to the body compilation unit (since the body overwrote the original
6413 -- instantiation node), semantically belong on the spec, so copy them
6414 -- there. It's harmless to leave them on the body as well. In fact one
6415 -- could argue that they belong in both places.
6417 Citem := First (Context_Items (Body_Cunit));
6418 while Present (Citem) loop
6419 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6423 -- Propagate categorization flags on packages, so that they appear in
6424 -- the ali file for the spec of the unit.
6426 if Ekind (New_Main) = E_Package then
6427 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6428 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6429 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6430 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6431 Set_Is_Remote_Call_Interface
6432 (Old_Main, Is_Remote_Call_Interface (New_Main));
6435 -- Make entry in Units table, so that binder can generate call to
6436 -- elaboration procedure for body, if any.
6438 Make_Instance_Unit (Body_Cunit, In_Main => True);
6439 Main_Unit_Entity := New_Main;
6440 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6442 -- Build elaboration entity, since the instance may certainly generate
6443 -- elaboration code requiring a flag for protection.
6445 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6446 end Build_Instance_Compilation_Unit_Nodes;
6448 -----------------------------
6449 -- Check_Access_Definition --
6450 -----------------------------
6452 procedure Check_Access_Definition (N : Node_Id) is
6455 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6457 end Check_Access_Definition;
6459 -----------------------------------
6460 -- Check_Formal_Package_Instance --
6461 -----------------------------------
6463 -- If the formal has specific parameters, they must match those of the
6464 -- actual. Both of them are instances, and the renaming declarations for
6465 -- their formal parameters appear in the same order in both. The analyzed
6466 -- formal has been analyzed in the context of the current instance.
6468 procedure Check_Formal_Package_Instance
6469 (Formal_Pack : Entity_Id;
6470 Actual_Pack : Entity_Id)
6472 E1 : Entity_Id := First_Entity (Actual_Pack);
6473 E2 : Entity_Id := First_Entity (Formal_Pack);
6474 Prev_E1 : Entity_Id;
6479 procedure Check_Mismatch (B : Boolean);
6480 -- Common error routine for mismatch between the parameters of the
6481 -- actual instance and those of the formal package.
6483 function Is_Defaulted (Param : Entity_Id) return Boolean;
6484 -- If the formal package has partly box-initialized formals, skip
6485 -- conformance check for these formals. Previously the code assumed
6486 -- that box initialization for a formal package applied to all its
6487 -- formal parameters.
6489 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6490 -- The formal may come from a nested formal package, and the actual may
6491 -- have been constant-folded. To determine whether the two denote the
6492 -- same entity we may have to traverse several definitions to recover
6493 -- the ultimate entity that they refer to.
6495 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6496 -- The formal and the actual must be identical, but if both are
6497 -- given by attributes they end up renaming different generated bodies,
6498 -- and we must verify that the attributes themselves match.
6500 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6501 -- Similarly, if the formal comes from a nested formal package, the
6502 -- actual may designate the formal through multiple renamings, which
6503 -- have to be followed to determine the original variable in question.
6505 --------------------
6506 -- Check_Mismatch --
6507 --------------------
6509 procedure Check_Mismatch (B : Boolean) is
6510 -- A Formal_Type_Declaration for a derived private type is rewritten
6511 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6512 -- which is why we examine the original node.
6514 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6517 if Kind = N_Formal_Type_Declaration then
6520 elsif Kind in N_Formal_Object_Declaration
6521 | N_Formal_Package_Declaration
6522 | N_Formal_Subprogram_Declaration
6526 -- Ada 2012: If both formal and actual are incomplete types they
6529 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6534 ("actual for & in actual instance does not match formal",
6535 Parent (Actual_Pack), E1);
6543 function Is_Defaulted (Param : Entity_Id) return Boolean is
6548 First (Generic_Associations (Parent
6549 (Associated_Formal_Package (Actual_Pack))));
6551 while Present (Assoc) loop
6552 if Nkind (Assoc) = N_Others_Choice then
6555 elsif Nkind (Assoc) = N_Generic_Association
6556 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6558 return Box_Present (Assoc);
6567 --------------------------------
6568 -- Same_Instantiated_Constant --
6569 --------------------------------
6571 function Same_Instantiated_Constant
6572 (E1, E2 : Entity_Id) return Boolean
6578 while Present (Ent) loop
6582 elsif Ekind (Ent) /= E_Constant then
6585 elsif Is_Entity_Name (Constant_Value (Ent)) then
6586 if Entity (Constant_Value (Ent)) = E1 then
6589 Ent := Entity (Constant_Value (Ent));
6592 -- The actual may be a constant that has been folded. Recover
6595 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6596 Ent := Entity (Original_Node (Constant_Value (Ent)));
6604 end Same_Instantiated_Constant;
6606 --------------------------------
6607 -- Same_Instantiated_Function --
6608 --------------------------------
6610 function Same_Instantiated_Function
6611 (E1, E2 : Entity_Id) return Boolean
6615 if Alias (E1) = Alias (E2) then
6618 elsif Present (Alias (E2)) then
6619 U1 := Original_Node (Unit_Declaration_Node (E1));
6620 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6622 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6623 and then Nkind (Name (U1)) = N_Attribute_Reference
6625 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6626 and then Nkind (Name (U2)) = N_Attribute_Reference
6629 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6633 end Same_Instantiated_Function;
6635 --------------------------------
6636 -- Same_Instantiated_Variable --
6637 --------------------------------
6639 function Same_Instantiated_Variable
6640 (E1, E2 : Entity_Id) return Boolean
6642 function Original_Entity (E : Entity_Id) return Entity_Id;
6643 -- Follow chain of renamings to the ultimate ancestor
6645 ---------------------
6646 -- Original_Entity --
6647 ---------------------
6649 function Original_Entity (E : Entity_Id) return Entity_Id is
6654 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6655 and then Present (Renamed_Object (Orig))
6656 and then Is_Entity_Name (Renamed_Object (Orig))
6658 Orig := Entity (Renamed_Object (Orig));
6662 end Original_Entity;
6664 -- Start of processing for Same_Instantiated_Variable
6667 return Ekind (E1) = Ekind (E2)
6668 and then Original_Entity (E1) = Original_Entity (E2);
6669 end Same_Instantiated_Variable;
6671 -- Start of processing for Check_Formal_Package_Instance
6675 while Present (E1) and then Present (E2) loop
6676 exit when Ekind (E1) = E_Package
6677 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6679 -- If the formal is the renaming of the formal package, this
6680 -- is the end of its formal part, which may occur before the
6681 -- end of the formal part in the actual in the presence of
6682 -- defaulted parameters in the formal package.
6684 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6685 and then Renamed_Entity (E2) = Scope (E2);
6687 -- The analysis of the actual may generate additional internal
6688 -- entities. If the formal is defaulted, there is no corresponding
6689 -- analysis and the internal entities must be skipped, until we
6690 -- find corresponding entities again.
6692 if Comes_From_Source (E2)
6693 and then not Comes_From_Source (E1)
6694 and then Chars (E1) /= Chars (E2)
6696 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6704 -- Entities may be declared without full declaration, such as
6705 -- itypes and predefined operators (concatenation for arrays, eg).
6706 -- Skip it and keep the formal entity to find a later match for it.
6708 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6712 -- If the formal entity comes from a formal declaration, it was
6713 -- defaulted in the formal package, and no check is needed on it.
6715 elsif Nkind (Original_Node (Parent (E2))) in
6716 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6718 -- If the formal is a tagged type the corresponding class-wide
6719 -- type has been generated as well, and it must be skipped.
6721 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6727 -- Ditto for defaulted formal subprograms.
6729 elsif Is_Overloadable (E1)
6730 and then Nkind (Unit_Declaration_Node (E2)) in
6731 N_Formal_Subprogram_Declaration
6735 elsif Is_Defaulted (E1) then
6738 elsif Is_Type (E1) then
6740 -- Subtypes must statically match. E1, E2 are the local entities
6741 -- that are subtypes of the actuals. Itypes generated for other
6742 -- parameters need not be checked, the check will be performed
6743 -- on the parameters themselves.
6745 -- If E2 is a formal type declaration, it is a defaulted parameter
6746 -- and needs no checking.
6748 if not Is_Itype (E1) and then not Is_Itype (E2) then
6751 or else Etype (E1) /= Etype (E2)
6752 or else not Subtypes_Statically_Match (E1, E2));
6755 elsif Ekind (E1) = E_Constant then
6757 -- IN parameters must denote the same static value, or the same
6758 -- constant, or the literal null.
6760 Expr1 := Expression (Parent (E1));
6762 if Ekind (E2) /= E_Constant then
6763 Check_Mismatch (True);
6766 Expr2 := Expression (Parent (E2));
6769 if Is_OK_Static_Expression (Expr1) then
6770 if not Is_OK_Static_Expression (Expr2) then
6771 Check_Mismatch (True);
6773 elsif Is_Discrete_Type (Etype (E1)) then
6775 V1 : constant Uint := Expr_Value (Expr1);
6776 V2 : constant Uint := Expr_Value (Expr2);
6778 Check_Mismatch (V1 /= V2);
6781 elsif Is_Real_Type (Etype (E1)) then
6783 V1 : constant Ureal := Expr_Value_R (Expr1);
6784 V2 : constant Ureal := Expr_Value_R (Expr2);
6786 Check_Mismatch (V1 /= V2);
6789 elsif Is_String_Type (Etype (E1))
6790 and then Nkind (Expr1) = N_String_Literal
6792 if Nkind (Expr2) /= N_String_Literal then
6793 Check_Mismatch (True);
6796 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6800 elsif Is_Entity_Name (Expr1) then
6801 if Is_Entity_Name (Expr2) then
6802 if Entity (Expr1) = Entity (Expr2) then
6806 (not Same_Instantiated_Constant
6807 (Entity (Expr1), Entity (Expr2)));
6811 Check_Mismatch (True);
6814 elsif Is_Entity_Name (Original_Node (Expr1))
6815 and then Is_Entity_Name (Expr2)
6816 and then Same_Instantiated_Constant
6817 (Entity (Original_Node (Expr1)), Entity (Expr2))
6821 elsif Nkind (Expr1) = N_Null then
6822 Check_Mismatch (Nkind (Expr1) /= N_Null);
6825 Check_Mismatch (True);
6828 elsif Ekind (E1) = E_Variable then
6829 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6831 elsif Ekind (E1) = E_Package then
6833 (Ekind (E1) /= Ekind (E2)
6834 or else (Present (Renamed_Object (E2))
6835 and then Renamed_Object (E1) /=
6836 Renamed_Object (E2)));
6838 elsif Is_Overloadable (E1) then
6839 -- Verify that the actual subprograms match. Note that actuals
6840 -- that are attributes are rewritten as subprograms. If the
6841 -- subprogram in the formal package is defaulted, no check is
6842 -- needed. Note that this can only happen in Ada 2005 when the
6843 -- formal package can be partially parameterized.
6845 if Nkind (Unit_Declaration_Node (E1)) =
6846 N_Subprogram_Renaming_Declaration
6847 and then From_Default (Unit_Declaration_Node (E1))
6851 -- If the formal package has an "others" box association that
6852 -- covers this formal, there is no need for a check either.
6854 elsif Nkind (Unit_Declaration_Node (E2)) in
6855 N_Formal_Subprogram_Declaration
6856 and then Box_Present (Unit_Declaration_Node (E2))
6860 -- No check needed if subprogram is a defaulted null procedure
6862 elsif No (Alias (E2))
6863 and then Ekind (E2) = E_Procedure
6865 Null_Present (Specification (Unit_Declaration_Node (E2)))
6869 -- Otherwise the actual in the formal and the actual in the
6870 -- instantiation of the formal must match, up to renamings.
6874 (Ekind (E2) /= Ekind (E1)
6875 or else not Same_Instantiated_Function (E1, E2));
6879 raise Program_Error;
6887 end Check_Formal_Package_Instance;
6889 ---------------------------
6890 -- Check_Formal_Packages --
6891 ---------------------------
6893 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6895 Formal_P : Entity_Id;
6896 Formal_Decl : Node_Id;
6898 -- Iterate through the declarations in the instance, looking for package
6899 -- renaming declarations that denote instances of formal packages. Stop
6900 -- when we find the renaming of the current package itself. The
6901 -- declaration for a formal package without a box is followed by an
6902 -- internal entity that repeats the instantiation.
6904 E := First_Entity (P_Id);
6905 while Present (E) loop
6906 if Ekind (E) = E_Package then
6907 if Renamed_Object (E) = P_Id then
6910 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6914 Formal_Decl := Parent (Associated_Formal_Package (E));
6916 -- Nothing to check if the formal has a box or an others_clause
6917 -- (necessarily with a box), or no associations altogether
6919 if Box_Present (Formal_Decl)
6920 or else No (Generic_Associations (Formal_Decl))
6924 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6927 -- The internal validating package was generated but formal
6928 -- and instance are known to be compatible.
6930 Formal_P := Next_Entity (E);
6931 Remove (Unit_Declaration_Node (Formal_P));
6934 Formal_P := Next_Entity (E);
6936 -- If the instance is within an enclosing instance body
6937 -- there is no need to verify the legality of current formal
6938 -- packages because they were legal in the generic body.
6939 -- This optimization may be applicable elsewhere, and it
6940 -- also removes spurious errors that may arise with
6941 -- on-the-fly inlining and confusion between private and
6944 if not In_Instance_Body then
6945 Check_Formal_Package_Instance (Formal_P, E);
6948 -- Restore the visibility of formals of the formal instance
6949 -- that are not defaulted, and are hidden within the current
6950 -- generic. These formals may be visible within an enclosing
6956 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6957 while Present (Elmt) loop
6958 Set_Is_Hidden (Node (Elmt), False);
6963 -- After checking, remove the internal validating package.
6964 -- It is only needed for semantic checks, and as it may
6965 -- contain generic formal declarations it should not reach
6968 Remove (Unit_Declaration_Node (Formal_P));
6975 end Check_Formal_Packages;
6977 ---------------------------------
6978 -- Check_Forward_Instantiation --
6979 ---------------------------------
6981 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6983 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6986 -- The instantiation appears before the generic body if we are in the
6987 -- scope of the unit containing the generic, either in its spec or in
6988 -- the package body, and before the generic body.
6990 if Ekind (Gen_Comp) = E_Package_Body then
6991 Gen_Comp := Spec_Entity (Gen_Comp);
6994 if In_Open_Scopes (Gen_Comp)
6995 and then No (Corresponding_Body (Decl))
7000 and then not Is_Compilation_Unit (S)
7001 and then not Is_Child_Unit (S)
7003 if Ekind (S) = E_Package then
7004 Set_Has_Forward_Instantiation (S);
7010 end Check_Forward_Instantiation;
7012 ---------------------------
7013 -- Check_Generic_Actuals --
7014 ---------------------------
7016 -- The visibility of the actuals may be different between the point of
7017 -- generic instantiation and the instantiation of the body.
7019 procedure Check_Generic_Actuals
7020 (Instance : Entity_Id;
7021 Is_Formal_Box : Boolean)
7027 E := First_Entity (Instance);
7028 while Present (E) loop
7030 and then Nkind (Parent (E)) = N_Subtype_Declaration
7031 and then Scope (Etype (E)) /= Instance
7032 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7034 -- Restore the proper view of the actual from the information
7035 -- saved earlier by Instantiate_Type.
7037 Check_Private_View (Subtype_Indication (Parent (E)));
7039 -- If the actual is itself the formal of a parent instance,
7040 -- then also restore the proper view of its actual and so on.
7041 -- That's necessary for nested instantiations of the form
7044 -- type Component is private;
7045 -- type Array_Type is array (Positive range <>) of Component;
7048 -- when the outermost actuals have inconsistent views, because
7049 -- the Component_Type of Array_Type of the inner instantiations
7050 -- is the actual of Component of the outermost one and not that
7051 -- of the corresponding inner instantiations.
7053 Astype := Ancestor_Subtype (E);
7054 while Present (Astype)
7055 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7056 and then Present (Generic_Parent_Type (Parent (Astype)))
7057 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7059 Check_Private_View (Subtype_Indication (Parent (Astype)));
7060 Astype := Ancestor_Subtype (Astype);
7063 Set_Is_Generic_Actual_Type (E);
7065 if Is_Private_Type (E) and then Present (Full_View (E)) then
7066 Set_Is_Generic_Actual_Type (Full_View (E));
7069 Set_Is_Hidden (E, False);
7070 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7072 -- We constructed the generic actual type as a subtype of the
7073 -- supplied type. This means that it normally would not inherit
7074 -- subtype specific attributes of the actual, which is wrong for
7075 -- the generic case.
7077 Astype := Ancestor_Subtype (E);
7081 -- This can happen when E is an itype that is the full view of
7082 -- a private type completed, e.g. with a constrained array. In
7083 -- that case, use the first subtype, which will carry size
7084 -- information. The base type itself is unconstrained and will
7087 Astype := First_Subtype (E);
7090 Set_Size_Info (E, (Astype));
7091 Set_RM_Size (E, RM_Size (Astype));
7092 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7094 if Is_Discrete_Or_Fixed_Point_Type (E) then
7095 Set_RM_Size (E, RM_Size (Astype));
7098 elsif Ekind (E) = E_Package then
7100 -- If this is the renaming for the current instance, we're done.
7101 -- Otherwise it is a formal package. If the corresponding formal
7102 -- was declared with a box, the (instantiations of the) generic
7103 -- formal part are also visible. Otherwise, ignore the entity
7104 -- created to validate the actuals.
7106 if Renamed_Object (E) = Instance then
7109 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7112 -- The visibility of a formal of an enclosing generic is already
7115 elsif Denotes_Formal_Package (E) then
7118 elsif Present (Associated_Formal_Package (E))
7119 and then not Is_Generic_Formal (E)
7121 if Box_Present (Parent (Associated_Formal_Package (E))) then
7122 Check_Generic_Actuals (Renamed_Object (E), True);
7125 Check_Generic_Actuals (Renamed_Object (E), False);
7128 Set_Is_Hidden (E, False);
7131 -- If this is a subprogram instance (in a wrapper package) the
7132 -- actual is fully visible.
7134 elsif Is_Wrapper_Package (Instance) then
7135 Set_Is_Hidden (E, False);
7137 -- If the formal package is declared with a box, or if the formal
7138 -- parameter is defaulted, it is visible in the body.
7140 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7141 Set_Is_Hidden (E, False);
7144 if Ekind (E) = E_Constant then
7146 -- If the type of the actual is a private type declared in the
7147 -- enclosing scope of the generic unit, the body of the generic
7148 -- sees the full view of the type (because it has to appear in
7149 -- the corresponding package body). If the type is private now,
7150 -- exchange views to restore the proper visiblity in the instance.
7153 Typ : constant Entity_Id := Base_Type (Etype (E));
7154 -- The type of the actual
7159 Parent_Scope : Entity_Id;
7160 -- The enclosing scope of the generic unit
7163 if Is_Wrapper_Package (Instance) then
7167 (Unit_Declaration_Node
7168 (Related_Instance (Instance))));
7171 Generic_Parent (Package_Specification (Instance));
7174 Parent_Scope := Scope (Gen_Id);
7176 -- The exchange is only needed if the generic is defined
7177 -- within a package which is not a common ancestor of the
7178 -- scope of the instance, and is not already in scope.
7180 if Is_Private_Type (Typ)
7181 and then Scope (Typ) = Parent_Scope
7182 and then Scope (Instance) /= Parent_Scope
7183 and then Ekind (Parent_Scope) = E_Package
7184 and then not Is_Child_Unit (Gen_Id)
7188 -- If the type of the entity is a subtype, it may also have
7189 -- to be made visible, together with the base type of its
7190 -- full view, after exchange.
7192 if Is_Private_Type (Etype (E)) then
7193 Switch_View (Etype (E));
7194 Switch_View (Base_Type (Etype (E)));
7202 end Check_Generic_Actuals;
7204 ------------------------------
7205 -- Check_Generic_Child_Unit --
7206 ------------------------------
7208 procedure Check_Generic_Child_Unit
7210 Parent_Installed : in out Boolean)
7212 Loc : constant Source_Ptr := Sloc (Gen_Id);
7213 Gen_Par : Entity_Id := Empty;
7215 Inst_Par : Entity_Id;
7218 function Find_Generic_Child
7220 Id : Node_Id) return Entity_Id;
7221 -- Search generic parent for possible child unit with the given name
7223 function In_Enclosing_Instance return Boolean;
7224 -- Within an instance of the parent, the child unit may be denoted by
7225 -- a simple name, or an abbreviated expanded name. Examine enclosing
7226 -- scopes to locate a possible parent instantiation.
7228 ------------------------
7229 -- Find_Generic_Child --
7230 ------------------------
7232 function Find_Generic_Child
7234 Id : Node_Id) return Entity_Id
7239 -- If entity of name is already set, instance has already been
7240 -- resolved, e.g. in an enclosing instantiation.
7242 if Present (Entity (Id)) then
7243 if Scope (Entity (Id)) = Scop then
7250 E := First_Entity (Scop);
7251 while Present (E) loop
7252 if Chars (E) = Chars (Id)
7253 and then Is_Child_Unit (E)
7255 if Is_Child_Unit (E)
7256 and then not Is_Visible_Lib_Unit (E)
7259 ("generic child unit& is not visible", Gen_Id, E);
7271 end Find_Generic_Child;
7273 ---------------------------
7274 -- In_Enclosing_Instance --
7275 ---------------------------
7277 function In_Enclosing_Instance return Boolean is
7278 Enclosing_Instance : Node_Id;
7279 Instance_Decl : Node_Id;
7282 -- We do not inline any call that contains instantiations, except
7283 -- for instantiations of Unchecked_Conversion, so if we are within
7284 -- an inlined body the current instance does not require parents.
7286 if In_Inlined_Body then
7287 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7291 -- Loop to check enclosing scopes
7293 Enclosing_Instance := Current_Scope;
7294 while Present (Enclosing_Instance) loop
7295 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7297 if Ekind (Enclosing_Instance) = E_Package
7298 and then Is_Generic_Instance (Enclosing_Instance)
7300 (Generic_Parent (Specification (Instance_Decl)))
7302 -- Check whether the generic we are looking for is a child of
7305 E := Find_Generic_Child
7306 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7307 exit when Present (E);
7313 Enclosing_Instance := Scope (Enclosing_Instance);
7325 Make_Expanded_Name (Loc,
7327 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7328 Selector_Name => New_Occurrence_Of (E, Loc)));
7330 Set_Entity (Gen_Id, E);
7331 Set_Etype (Gen_Id, Etype (E));
7332 Parent_Installed := False; -- Already in scope.
7335 end In_Enclosing_Instance;
7337 -- Start of processing for Check_Generic_Child_Unit
7340 -- If the name of the generic is given by a selected component, it may
7341 -- be the name of a generic child unit, and the prefix is the name of an
7342 -- instance of the parent, in which case the child unit must be visible.
7343 -- If this instance is not in scope, it must be placed there and removed
7344 -- after instantiation, because what is being instantiated is not the
7345 -- original child, but the corresponding child present in the instance
7348 -- If the child is instantiated within the parent, it can be given by
7349 -- a simple name. In this case the instance is already in scope, but
7350 -- the child generic must be recovered from the generic parent as well.
7352 if Nkind (Gen_Id) = N_Selected_Component then
7353 S := Selector_Name (Gen_Id);
7354 Analyze (Prefix (Gen_Id));
7355 Inst_Par := Entity (Prefix (Gen_Id));
7357 if Ekind (Inst_Par) = E_Package
7358 and then Present (Renamed_Object (Inst_Par))
7360 Inst_Par := Renamed_Object (Inst_Par);
7363 if Ekind (Inst_Par) = E_Package then
7364 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7365 Gen_Par := Generic_Parent (Parent (Inst_Par));
7367 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7369 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7371 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7374 elsif Ekind (Inst_Par) = E_Generic_Package
7375 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7377 -- A formal package may be a real child package, and not the
7378 -- implicit instance within a parent. In this case the child is
7379 -- not visible and has to be retrieved explicitly as well.
7381 Gen_Par := Inst_Par;
7384 if Present (Gen_Par) then
7386 -- The prefix denotes an instantiation. The entity itself may be a
7387 -- nested generic, or a child unit.
7389 E := Find_Generic_Child (Gen_Par, S);
7392 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7393 Set_Entity (Gen_Id, E);
7394 Set_Etype (Gen_Id, Etype (E));
7396 Set_Etype (S, Etype (E));
7398 -- Indicate that this is a reference to the parent
7400 if In_Extended_Main_Source_Unit (Gen_Id) then
7401 Set_Is_Instantiated (Inst_Par);
7404 -- A common mistake is to replicate the naming scheme of a
7405 -- hierarchy by instantiating a generic child directly, rather
7406 -- than the implicit child in a parent instance:
7408 -- generic .. package Gpar is ..
7409 -- generic .. package Gpar.Child is ..
7410 -- package Par is new Gpar ();
7413 -- package Par.Child is new Gpar.Child ();
7414 -- rather than Par.Child
7416 -- In this case the instantiation is within Par, which is an
7417 -- instance, but Gpar does not denote Par because we are not IN
7418 -- the instance of Gpar, so this is illegal. The test below
7419 -- recognizes this particular case.
7421 if Is_Child_Unit (E)
7422 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7423 and then (not In_Instance
7424 or else Nkind (Parent (Parent (Gen_Id))) =
7428 ("prefix of generic child unit must be instance of parent",
7432 if not In_Open_Scopes (Inst_Par)
7433 and then Nkind (Parent (Gen_Id)) not in
7434 N_Generic_Renaming_Declaration
7436 Install_Parent (Inst_Par);
7437 Parent_Installed := True;
7439 elsif In_Open_Scopes (Inst_Par) then
7441 -- If the parent is already installed, install the actuals
7442 -- for its formal packages. This is necessary when the child
7443 -- instance is a child of the parent instance: in this case,
7444 -- the parent is placed on the scope stack but the formal
7445 -- packages are not made visible.
7447 Install_Formal_Packages (Inst_Par);
7451 -- If the generic parent does not contain an entity that
7452 -- corresponds to the selector, the instance doesn't either.
7453 -- Analyzing the node will yield the appropriate error message.
7454 -- If the entity is not a child unit, then it is an inner
7455 -- generic in the parent.
7463 if Is_Child_Unit (Entity (Gen_Id))
7465 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7466 and then not In_Open_Scopes (Inst_Par)
7468 Install_Parent (Inst_Par);
7469 Parent_Installed := True;
7471 -- The generic unit may be the renaming of the implicit child
7472 -- present in an instance. In that case the parent instance is
7473 -- obtained from the name of the renamed entity.
7475 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7476 and then Present (Renamed_Entity (Entity (Gen_Id)))
7477 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7480 Renamed_Package : constant Node_Id :=
7481 Name (Parent (Entity (Gen_Id)));
7483 if Nkind (Renamed_Package) = N_Expanded_Name then
7484 Inst_Par := Entity (Prefix (Renamed_Package));
7485 Install_Parent (Inst_Par);
7486 Parent_Installed := True;
7492 elsif Nkind (Gen_Id) = N_Expanded_Name then
7494 -- Entity already present, analyze prefix, whose meaning may be an
7495 -- instance in the current context. If it is an instance of a
7496 -- relative within another, the proper parent may still have to be
7497 -- installed, if they are not of the same generation.
7499 Analyze (Prefix (Gen_Id));
7501 -- Prevent cascaded errors
7503 if Etype (Prefix (Gen_Id)) = Any_Type then
7507 -- In the unlikely case that a local declaration hides the name of
7508 -- the parent package, locate it on the homonym chain. If the context
7509 -- is an instance of the parent, the renaming entity is flagged as
7512 Inst_Par := Entity (Prefix (Gen_Id));
7513 while Present (Inst_Par)
7514 and then not Is_Package_Or_Generic_Package (Inst_Par)
7516 Inst_Par := Homonym (Inst_Par);
7519 pragma Assert (Present (Inst_Par));
7520 Set_Entity (Prefix (Gen_Id), Inst_Par);
7522 if In_Enclosing_Instance then
7525 elsif Present (Entity (Gen_Id))
7526 and then No (Renamed_Entity (Entity (Gen_Id)))
7527 and then Is_Child_Unit (Entity (Gen_Id))
7528 and then not In_Open_Scopes (Inst_Par)
7530 Install_Parent (Inst_Par);
7531 Parent_Installed := True;
7533 -- Handle renaming of generic child unit
7535 elsif Present (Entity (Gen_Id))
7536 and then Present (Renamed_Entity (Entity (Gen_Id)))
7537 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7544 -- The entity of the renamed generic child unit does not
7545 -- have any reference to the instantiated parent. In order to
7546 -- locate it we traverse the scope containing the renaming
7547 -- declaration; the instance of the parent is available in
7548 -- the prefix of the renaming declaration. For example:
7551 -- package Inst_Par is new ...
7552 -- generic package Ren_Child renames Ins_Par.Child;
7557 -- package Inst_Child is new A.Ren_Child;
7560 E := First_Entity (Entity (Prefix (Gen_Id)));
7561 while Present (E) loop
7562 if Present (Renamed_Entity (E))
7564 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7566 Ren_Decl := Parent (E);
7567 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7569 if not In_Open_Scopes (Inst_Par) then
7570 Install_Parent (Inst_Par);
7571 Parent_Installed := True;
7577 E := Next_Entity (E);
7582 elsif In_Enclosing_Instance then
7584 -- The child unit is found in some enclosing scope
7591 -- If this is the renaming of the implicit child in a parent
7592 -- instance, recover the parent name and install it.
7594 if Is_Entity_Name (Gen_Id) then
7595 E := Entity (Gen_Id);
7597 if Is_Generic_Unit (E)
7598 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7599 and then Is_Child_Unit (Renamed_Object (E))
7600 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7601 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7603 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7604 Inst_Par := Entity (Prefix (Gen_Id));
7606 if not In_Open_Scopes (Inst_Par) then
7607 Install_Parent (Inst_Par);
7608 Parent_Installed := True;
7611 -- If it is a child unit of a non-generic parent, it may be
7612 -- use-visible and given by a direct name. Install parent as
7615 elsif Is_Generic_Unit (E)
7616 and then Is_Child_Unit (E)
7618 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7619 and then not Is_Generic_Unit (Scope (E))
7621 if not In_Open_Scopes (Scope (E)) then
7622 Install_Parent (Scope (E));
7623 Parent_Installed := True;
7628 end Check_Generic_Child_Unit;
7630 -----------------------------
7631 -- Check_Hidden_Child_Unit --
7632 -----------------------------
7634 procedure Check_Hidden_Child_Unit
7636 Gen_Unit : Entity_Id;
7637 Act_Decl_Id : Entity_Id)
7639 Gen_Id : constant Node_Id := Name (N);
7642 if Is_Child_Unit (Gen_Unit)
7643 and then Is_Child_Unit (Act_Decl_Id)
7644 and then Nkind (Gen_Id) = N_Expanded_Name
7645 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7646 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7648 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7650 ("generic unit & is implicitly declared in &",
7651 Defining_Unit_Name (N), Gen_Unit);
7652 Error_Msg_N ("\instance must have different name",
7653 Defining_Unit_Name (N));
7655 end Check_Hidden_Child_Unit;
7657 ------------------------
7658 -- Check_Private_View --
7659 ------------------------
7661 procedure Check_Private_View (N : Node_Id) is
7662 T : constant Entity_Id := Etype (N);
7666 -- Exchange views if the type was not private in the generic but is
7667 -- private at the point of instantiation. Do not exchange views if
7668 -- the scope of the type is in scope. This can happen if both generic
7669 -- and instance are sibling units, or if type is defined in a parent.
7670 -- In this case the visibility of the type will be correct for all
7674 BT := Base_Type (T);
7676 if Is_Private_Type (T)
7677 and then not Has_Private_View (N)
7678 and then Present (Full_View (T))
7679 and then not In_Open_Scopes (Scope (T))
7681 -- In the generic, the full declaration was visible
7685 elsif Has_Private_View (N)
7686 and then not Is_Private_Type (T)
7687 and then not Has_Been_Exchanged (T)
7688 and then (not In_Open_Scopes (Scope (T))
7689 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7691 -- In the generic, only the private declaration was visible
7693 -- If the type appears in a subtype declaration, the subtype in
7694 -- instance must have a view compatible with that of its parent,
7695 -- which must be exchanged (see corresponding code in Restore_
7696 -- Private_Views) so we make an exception to the open scope rule.
7698 Prepend_Elmt (T, Exchanged_Views);
7699 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7701 -- Finally, a non-private subtype may have a private base type, which
7702 -- must be exchanged for consistency. This can happen when a package
7703 -- body is instantiated, when the scope stack is empty but in fact
7704 -- the subtype and the base type are declared in an enclosing scope.
7706 -- Note that in this case we introduce an inconsistency in the view
7707 -- set, because we switch the base type BT, but there could be some
7708 -- private dependent subtypes of BT which remain unswitched. Such
7709 -- subtypes might need to be switched at a later point (see specific
7710 -- provision for that case in Switch_View).
7712 elsif not Is_Private_Type (T)
7713 and then not Has_Private_View (N)
7714 and then Is_Private_Type (BT)
7715 and then Present (Full_View (BT))
7716 and then not Is_Generic_Type (BT)
7717 and then not In_Open_Scopes (BT)
7719 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7720 Exchange_Declarations (BT);
7723 end Check_Private_View;
7725 -----------------------------
7726 -- Check_Hidden_Primitives --
7727 -----------------------------
7729 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7732 Result : Elist_Id := No_Elist;
7735 if No (Assoc_List) then
7739 -- Traverse the list of associations between formals and actuals
7740 -- searching for renamings of tagged types
7742 Actual := First (Assoc_List);
7743 while Present (Actual) loop
7744 if Nkind (Actual) = N_Subtype_Declaration then
7745 Gen_T := Generic_Parent_Type (Actual);
7747 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7749 -- Traverse the list of primitives of the actual types
7750 -- searching for hidden primitives that are visible in the
7751 -- corresponding generic formal; leave them visible and
7752 -- append them to Result to restore their decoration later.
7754 Install_Hidden_Primitives
7755 (Prims_List => Result,
7757 Act_T => Entity (Subtype_Indication (Actual)));
7765 end Check_Hidden_Primitives;
7767 --------------------------
7768 -- Contains_Instance_Of --
7769 --------------------------
7771 function Contains_Instance_Of
7774 N : Node_Id) return Boolean
7782 -- Verify that there are no circular instantiations. We check whether
7783 -- the unit contains an instance of the current scope or some enclosing
7784 -- scope (in case one of the instances appears in a subunit). Longer
7785 -- circularities involving subunits might seem too pathological to
7786 -- consider, but they were not too pathological for the authors of
7787 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7788 -- enclosing generic scopes as containing an instance.
7791 -- Within a generic subprogram body, the scope is not generic, to
7792 -- allow for recursive subprograms. Use the declaration to determine
7793 -- whether this is a generic unit.
7795 if Ekind (Scop) = E_Generic_Package
7796 or else (Is_Subprogram (Scop)
7797 and then Nkind (Unit_Declaration_Node (Scop)) =
7798 N_Generic_Subprogram_Declaration)
7800 Elmt := First_Elmt (Inner_Instances (Inner));
7802 while Present (Elmt) loop
7803 if Node (Elmt) = Scop then
7804 Error_Msg_Node_2 := Inner;
7806 ("circular Instantiation: & instantiated within &!",
7810 elsif Node (Elmt) = Inner then
7813 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7814 Error_Msg_Node_2 := Inner;
7816 ("circular Instantiation: & instantiated within &!",
7824 -- Indicate that Inner is being instantiated within Scop
7826 Append_Elmt (Inner, Inner_Instances (Scop));
7829 if Scop = Standard_Standard then
7832 Scop := Scope (Scop);
7837 end Contains_Instance_Of;
7839 -----------------------
7840 -- Copy_Generic_Node --
7841 -----------------------
7843 function Copy_Generic_Node
7845 Parent_Id : Node_Id;
7846 Instantiating : Boolean) return Node_Id
7851 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7852 -- Check the given value of one of the Fields referenced by the current
7853 -- node to determine whether to copy it recursively. The field may hold
7854 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7855 -- Char) in which case it need not be copied.
7857 procedure Copy_Descendants;
7858 -- Common utility for various nodes
7860 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7861 -- Make copy of element list
7863 function Copy_Generic_List
7865 Parent_Id : Node_Id) return List_Id;
7866 -- Apply Copy_Node recursively to the members of a node list
7868 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7869 -- True if an identifier is part of the defining program unit name of
7871 -- Consider removing this subprogram now that ASIS no longer uses it.
7873 ----------------------
7874 -- Copy_Descendants --
7875 ----------------------
7877 procedure Copy_Descendants is
7878 use Atree.Unchecked_Access;
7879 -- This code section is part of the implementation of an untyped
7880 -- tree traversal, so it needs direct access to node fields.
7883 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7884 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7885 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7886 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7887 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7888 end Copy_Descendants;
7890 -----------------------------
7891 -- Copy_Generic_Descendant --
7892 -----------------------------
7894 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7896 if D = Union_Id (Empty) then
7899 elsif D in Node_Range then
7901 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7903 elsif D in List_Range then
7904 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7906 elsif D in Elist_Range then
7907 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7909 -- Nothing else is copyable (e.g. Uint values), return as is
7914 end Copy_Generic_Descendant;
7916 ------------------------
7917 -- Copy_Generic_Elist --
7918 ------------------------
7920 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7927 M := First_Elmt (E);
7928 while Present (M) loop
7930 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7939 end Copy_Generic_Elist;
7941 -----------------------
7942 -- Copy_Generic_List --
7943 -----------------------
7945 function Copy_Generic_List
7947 Parent_Id : Node_Id) return List_Id
7955 Set_Parent (New_L, Parent_Id);
7958 while Present (N) loop
7959 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7968 end Copy_Generic_List;
7970 ---------------------------
7971 -- In_Defining_Unit_Name --
7972 ---------------------------
7974 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7977 Present (Parent (Nam))
7978 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7980 (Nkind (Parent (Nam)) = N_Expanded_Name
7981 and then In_Defining_Unit_Name (Parent (Nam))));
7982 end In_Defining_Unit_Name;
7984 -- Start of processing for Copy_Generic_Node
7991 New_N := New_Copy (N);
7993 -- Copy aspects if present
7995 if Has_Aspects (N) then
7996 Set_Has_Aspects (New_N, False);
7997 Set_Aspect_Specifications
7998 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
8001 -- If we are instantiating, we want to adjust the sloc based on the
8002 -- current S_Adjustment. However, if this is the root node of a subunit,
8003 -- we need to defer that adjustment to below (see "elsif Instantiating
8004 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8005 -- computed the adjustment.
8008 and then not (Nkind (N) in N_Proper_Body
8009 and then Was_Originally_Stub (N))
8011 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8014 if not Is_List_Member (N) then
8015 Set_Parent (New_N, Parent_Id);
8018 -- Special casing for identifiers and other entity names and operators
8020 if Nkind (New_N) in N_Character_Literal
8026 if not Instantiating then
8028 -- Link both nodes in order to assign subsequently the entity of
8029 -- the copy to the original node, in case this is a global
8032 Set_Associated_Node (N, New_N);
8034 -- If we are within an instantiation, this is a nested generic
8035 -- that has already been analyzed at the point of definition.
8036 -- We must preserve references that were global to the enclosing
8037 -- parent at that point. Other occurrences, whether global or
8038 -- local to the current generic, must be resolved anew, so we
8039 -- reset the entity in the generic copy. A global reference has a
8040 -- smaller depth than the parent, or else the same depth in case
8041 -- both are distinct compilation units.
8043 -- A child unit is implicitly declared within the enclosing parent
8044 -- but is in fact global to it, and must be preserved.
8046 -- It is also possible for Current_Instantiated_Parent to be
8047 -- defined, and for this not to be a nested generic, namely if
8048 -- the unit is loaded through Rtsfind. In that case, the entity of
8049 -- New_N is only a link to the associated node, and not a defining
8052 -- The entities for parent units in the defining_program_unit of a
8053 -- generic child unit are established when the context of the unit
8054 -- is first analyzed, before the generic copy is made. They are
8055 -- preserved in the copy for use in e.g. ASIS queries.
8057 Ent := Entity (New_N);
8059 if No (Current_Instantiated_Parent.Gen_Id) then
8061 or else Nkind (Ent) /= N_Defining_Identifier
8062 or else not In_Defining_Unit_Name (N)
8064 Set_Associated_Node (New_N, Empty);
8068 or else Nkind (Ent) not in N_Entity
8069 or else No (Scope (Ent))
8071 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8072 and then not Is_Child_Unit (Ent))
8074 (Scope_Depth (Scope (Ent)) >
8075 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8077 Get_Source_Unit (Ent) =
8078 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8080 Set_Associated_Node (New_N, Empty);
8083 -- Case of instantiating identifier or some other name or operator
8086 -- If the associated node is still defined, the entity in it
8087 -- is global, and must be copied to the instance. If this copy
8088 -- is being made for a body to inline, it is applied to an
8089 -- instantiated tree, and the entity is already present and
8090 -- must be also preserved.
8093 Assoc : constant Node_Id := Get_Associated_Node (N);
8096 if Present (Assoc) then
8097 if Nkind (Assoc) = Nkind (N) then
8098 Set_Entity (New_N, Entity (Assoc));
8099 Check_Private_View (N);
8101 -- Here we deal with a very peculiar case for which the
8102 -- Has_Private_View mechanism is not sufficient, because
8103 -- the reference to the type is implicit in the tree,
8104 -- that is to say, it's not referenced from a node but
8105 -- only from another type, namely through Component_Type.
8109 -- type Pt is private;
8112 -- type Ft is array (Positive range <>) of Pt;
8114 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8118 -- type Pt is new Boolean;
8121 -- package body P is
8122 -- package body G is
8123 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8125 -- if (F1 < F2) /= Lt then
8132 -- type Arr is array (Positive range <>) of P.Pt;
8134 -- package Inst is new P.G (Arr);
8136 -- Pt is a global type for the generic package G and it
8137 -- is not referenced in its body, but only as component
8138 -- type of Ft, which is a local type. This means that no
8139 -- references to Pt or Ft are seen during the copy of the
8140 -- body, the only reference to Pt being seen is when the
8141 -- actuals are checked by Check_Generic_Actuals, but Pt
8142 -- is still private at this point. In the end, the views
8143 -- of Pt are not switched in the body and, therefore, the
8144 -- array comparison is rejected because the component is
8147 -- Adding e.g. a dummy variable of type Pt in the body is
8148 -- sufficient to make everything work, so we generate an
8149 -- artificial reference to Pt on the fly and thus force
8150 -- the switching of views on the grounds that, if the
8151 -- comparison was accepted during the semantic analysis
8152 -- of the generic, this means that the component cannot
8153 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8155 if Nkind (Assoc) in N_Op_Compare
8156 and then Present (Etype (Left_Opnd (Assoc)))
8157 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8158 and then Present (Etype (Right_Opnd (Assoc)))
8159 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8162 Ltyp : constant Entity_Id :=
8163 Etype (Left_Opnd (Assoc));
8164 Rtyp : constant Entity_Id :=
8165 Etype (Right_Opnd (Assoc));
8167 if Is_Private_Type (Component_Type (Ltyp)) then
8169 (New_Occurrence_Of (Component_Type (Ltyp),
8172 if Is_Private_Type (Component_Type (Rtyp)) then
8174 (New_Occurrence_Of (Component_Type (Rtyp),
8179 -- Here is a similar case, for the Designated_Type of an
8180 -- access type that is present as target type in a type
8181 -- conversion from another access type. In this case, if
8182 -- the base types of the designated types are different
8183 -- and the conversion was accepted during the semantic
8184 -- analysis of the generic, this means that the target
8185 -- type cannot have been private (see Valid_Conversion).
8187 elsif Nkind (Assoc) = N_Identifier
8188 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8189 and then Subtype_Mark (Parent (Assoc)) = Assoc
8190 and then Present (Etype (Assoc))
8191 and then Is_Access_Type (Etype (Assoc))
8192 and then Present (Etype (Expression (Parent (Assoc))))
8194 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8197 Targ_Desig : constant Entity_Id :=
8198 Designated_Type (Etype (Assoc));
8199 Expr_Desig : constant Entity_Id :=
8201 (Etype (Expression (Parent (Assoc))));
8203 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8204 and then Is_Private_Type (Targ_Desig)
8207 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8212 -- The node is a reference to a global type and acts as the
8213 -- subtype mark of a qualified expression created in order
8214 -- to aid resolution of accidental overloading in instances.
8215 -- Since N is a reference to a type, the Associated_Node of
8216 -- N denotes an entity rather than another identifier. See
8217 -- Qualify_Universal_Operands for details.
8219 elsif Nkind (N) = N_Identifier
8220 and then Nkind (Parent (N)) = N_Qualified_Expression
8221 and then Subtype_Mark (Parent (N)) = N
8222 and then Is_Qualified_Universal_Literal (Parent (N))
8224 Set_Entity (New_N, Assoc);
8226 -- The name in the call may be a selected component if the
8227 -- call has not been analyzed yet, as may be the case for
8228 -- pre/post conditions in a generic unit.
8230 elsif Nkind (Assoc) = N_Function_Call
8231 and then Is_Entity_Name (Name (Assoc))
8233 Set_Entity (New_N, Entity (Name (Assoc)));
8235 elsif Nkind (Assoc) in N_Entity
8236 and then Expander_Active
8238 -- Inlining case: we are copying a tree that contains
8239 -- global entities, which are preserved in the copy to be
8240 -- used for subsequent inlining.
8245 Set_Entity (New_N, Empty);
8251 -- For expanded name, we must copy the Prefix and Selector_Name
8253 if Nkind (N) = N_Expanded_Name then
8255 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8257 Set_Selector_Name (New_N,
8258 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8260 -- For operators, copy the operands
8262 elsif Nkind (N) in N_Op then
8263 if Nkind (N) in N_Binary_Op then
8264 Set_Left_Opnd (New_N,
8265 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8268 Set_Right_Opnd (New_N,
8269 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8272 -- Establish a link between an entity from the generic template and the
8273 -- corresponding entity in the generic copy to be analyzed.
8275 elsif Nkind (N) in N_Entity then
8276 if not Instantiating then
8277 Set_Associated_Entity (N, New_N);
8280 -- Clear any existing link the copy may inherit from the replicated
8281 -- generic template entity.
8283 Set_Associated_Entity (New_N, Empty);
8285 -- Special casing for stubs
8287 elsif Nkind (N) in N_Body_Stub then
8289 -- In any case, we must copy the specification or defining
8290 -- identifier as appropriate.
8292 if Nkind (N) = N_Subprogram_Body_Stub then
8293 Set_Specification (New_N,
8294 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8297 Set_Defining_Identifier (New_N,
8299 (Defining_Identifier (N), New_N, Instantiating));
8302 -- If we are not instantiating, then this is where we load and
8303 -- analyze subunits, i.e. at the point where the stub occurs. A
8304 -- more permissive system might defer this analysis to the point
8305 -- of instantiation, but this seems too complicated for now.
8307 if not Instantiating then
8309 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8311 Unum : Unit_Number_Type;
8315 -- Make sure that, if it is a subunit of the main unit that is
8316 -- preprocessed and if -gnateG is specified, the preprocessed
8317 -- file will be written.
8319 Lib.Analysing_Subunit_Of_Main :=
8320 Lib.In_Extended_Main_Source_Unit (N);
8323 (Load_Name => Subunit_Name,
8327 Lib.Analysing_Subunit_Of_Main := False;
8329 -- If the proper body is not found, a warning message will be
8330 -- emitted when analyzing the stub, or later at the point of
8331 -- instantiation. Here we just leave the stub as is.
8333 if Unum = No_Unit then
8334 Subunits_Missing := True;
8335 goto Subunit_Not_Found;
8338 Subunit := Cunit (Unum);
8340 if Nkind (Unit (Subunit)) /= N_Subunit then
8342 ("found child unit instead of expected SEPARATE subunit",
8344 Error_Msg_Sloc := Sloc (N);
8345 Error_Msg_N ("\to complete stub #", Subunit);
8346 goto Subunit_Not_Found;
8349 -- We must create a generic copy of the subunit, in order to
8350 -- perform semantic analysis on it, and we must replace the
8351 -- stub in the original generic unit with the subunit, in order
8352 -- to preserve non-local references within.
8354 -- Only the proper body needs to be copied. Library_Unit and
8355 -- context clause are simply inherited by the generic copy.
8356 -- Note that the copy (which may be recursive if there are
8357 -- nested subunits) must be done first, before attaching it to
8358 -- the enclosing generic.
8362 (Proper_Body (Unit (Subunit)),
8363 Empty, Instantiating => False);
8365 -- Now place the original proper body in the original generic
8366 -- unit. This is a body, not a compilation unit.
8368 Rewrite (N, Proper_Body (Unit (Subunit)));
8369 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8370 Set_Was_Originally_Stub (N);
8372 -- Finally replace the body of the subunit with its copy, and
8373 -- make this new subunit into the library unit of the generic
8374 -- copy, which does not have stubs any longer.
8376 Set_Proper_Body (Unit (Subunit), New_Body);
8377 Set_Library_Unit (New_N, Subunit);
8378 Inherit_Context (Unit (Subunit), N);
8381 -- If we are instantiating, this must be an error case, since
8382 -- otherwise we would have replaced the stub node by the proper body
8383 -- that corresponds. So just ignore it in the copy (i.e. we have
8384 -- copied it, and that is good enough).
8390 <<Subunit_Not_Found>> null;
8392 -- If the node is a compilation unit, it is the subunit of a stub, which
8393 -- has been loaded already (see code below). In this case, the library
8394 -- unit field of N points to the parent unit (which is a compilation
8395 -- unit) and need not (and cannot) be copied.
8397 -- When the proper body of the stub is analyzed, the library_unit link
8398 -- is used to establish the proper context (see sem_ch10).
8400 -- The other fields of a compilation unit are copied as usual
8402 elsif Nkind (N) = N_Compilation_Unit then
8404 -- This code can only be executed when not instantiating, because in
8405 -- the copy made for an instantiation, the compilation unit node has
8406 -- disappeared at the point that a stub is replaced by its proper
8409 pragma Assert (not Instantiating);
8411 Set_Context_Items (New_N,
8412 Copy_Generic_List (Context_Items (N), New_N));
8415 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8417 Set_First_Inlined_Subprogram (New_N,
8419 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8424 (Aux_Decls_Node (N), New_N, Instantiating => False));
8426 -- For an assignment node, the assignment is known to be semantically
8427 -- legal if we are instantiating the template. This avoids incorrect
8428 -- diagnostics in generated code.
8430 elsif Nkind (N) = N_Assignment_Statement then
8432 -- Copy name and expression fields in usual manner
8435 Copy_Generic_Node (Name (N), New_N, Instantiating));
8437 Set_Expression (New_N,
8438 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8440 if Instantiating then
8441 Set_Assignment_OK (Name (New_N), True);
8444 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8445 if not Instantiating then
8446 Set_Associated_Node (N, New_N);
8449 if Present (Get_Associated_Node (N))
8450 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8452 -- In the generic the aggregate has some composite type. If at
8453 -- the point of instantiation the type has a private view,
8454 -- install the full view (and that of its ancestors, if any).
8457 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8461 if Present (T) and then Is_Private_Type (T) then
8466 and then Is_Tagged_Type (T)
8467 and then Is_Derived_Type (T)
8469 Rt := Root_Type (T);
8474 if Is_Private_Type (T) then
8485 -- Do not copy the associated node, which points to the generic copy
8486 -- of the aggregate.
8489 use Atree.Unchecked_Access;
8490 -- This code section is part of the implementation of an untyped
8491 -- tree traversal, so it needs direct access to node fields.
8494 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8495 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8496 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8497 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8500 -- Allocators do not have an identifier denoting the access type, so we
8501 -- must locate it through the expression to check whether the views are
8504 elsif Nkind (N) = N_Allocator
8505 and then Nkind (Expression (N)) = N_Qualified_Expression
8506 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8507 and then Instantiating
8510 T : constant Node_Id :=
8511 Get_Associated_Node (Subtype_Mark (Expression (N)));
8517 -- Retrieve the allocator node in the generic copy
8519 Acc_T := Etype (Parent (Parent (T)));
8521 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8522 Switch_View (Acc_T);
8529 -- For a proper body, we must catch the case of a proper body that
8530 -- replaces a stub. This represents the point at which a separate
8531 -- compilation unit, and hence template file, may be referenced, so we
8532 -- must make a new source instantiation entry for the template of the
8533 -- subunit, and ensure that all nodes in the subunit are adjusted using
8534 -- this new source instantiation entry.
8536 elsif Nkind (N) in N_Proper_Body then
8538 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8540 if Instantiating and then Was_Originally_Stub (N) then
8541 Create_Instantiation_Source
8542 (Instantiation_Node,
8543 Defining_Entity (N),
8546 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8549 -- Now copy the fields of the proper body, using the new
8550 -- adjustment factor if one was needed as per test above.
8554 -- Restore the original adjustment factor
8556 S_Adjustment := Save_Adjustment;
8559 elsif Nkind (N) = N_Pragma and then Instantiating then
8561 -- Do not copy Comment or Ident pragmas their content is relevant to
8562 -- the generic unit, not to the instantiating unit.
8564 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8565 New_N := Make_Null_Statement (Sloc (N));
8567 -- Do not copy pragmas generated from aspects because the pragmas do
8568 -- not carry any semantic information, plus they will be regenerated
8571 -- However, generating C we need to copy them since postconditions
8572 -- are inlined by the front end, and the front-end inlining machinery
8573 -- relies on this routine to perform inlining.
8575 elsif From_Aspect_Specification (N)
8576 and then not Modify_Tree_For_C
8578 New_N := Make_Null_Statement (Sloc (N));
8584 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8586 -- No descendant fields need traversing
8590 elsif Nkind (N) = N_String_Literal
8591 and then Present (Etype (N))
8592 and then Instantiating
8594 -- If the string is declared in an outer scope, the string_literal
8595 -- subtype created for it may have the wrong scope. Force reanalysis
8596 -- of the constant to generate a new itype in the proper context.
8598 Set_Etype (New_N, Empty);
8599 Set_Analyzed (New_N, False);
8601 -- For the remaining nodes, copy their descendants recursively
8606 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8607 Set_Generic_Parent (Specification (New_N), N);
8609 -- Should preserve Corresponding_Spec??? (12.3(14))
8613 -- Propagate dimensions if present, so that they are reflected in the
8616 if Nkind (N) in N_Has_Etype
8617 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8618 and then Present (Etype (N))
8619 and then Is_Floating_Point_Type (Etype (N))
8620 and then Has_Dimension_System (Etype (N))
8622 Copy_Dimensions (N, New_N);
8626 end Copy_Generic_Node;
8628 ----------------------------
8629 -- Denotes_Formal_Package --
8630 ----------------------------
8632 function Denotes_Formal_Package
8634 On_Exit : Boolean := False;
8635 Instance : Entity_Id := Empty) return Boolean
8638 Scop : constant Entity_Id := Scope (Pack);
8641 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8642 -- The package in question may be an actual for a previous formal
8643 -- package P of the current instance, so examine its actuals as well.
8644 -- This must be recursive over other formal packages.
8646 ----------------------------------
8647 -- Is_Actual_Of_Previous_Formal --
8648 ----------------------------------
8650 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8654 E1 := First_Entity (P);
8655 while Present (E1) and then E1 /= Instance loop
8656 if Ekind (E1) = E_Package
8657 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8659 if Renamed_Object (E1) = Pack then
8662 elsif E1 = P or else Renamed_Object (E1) = P then
8665 elsif Is_Actual_Of_Previous_Formal (E1) then
8674 end Is_Actual_Of_Previous_Formal;
8676 -- Start of processing for Denotes_Formal_Package
8682 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8684 Par := Current_Instantiated_Parent.Act_Id;
8687 if Ekind (Scop) = E_Generic_Package
8688 or else Nkind (Unit_Declaration_Node (Scop)) =
8689 N_Generic_Subprogram_Declaration
8693 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8694 N_Formal_Package_Declaration
8702 -- Check whether this package is associated with a formal package of
8703 -- the enclosing instantiation. Iterate over the list of renamings.
8705 E := First_Entity (Par);
8706 while Present (E) loop
8707 if Ekind (E) /= E_Package
8708 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8712 elsif Renamed_Object (E) = Par then
8715 elsif Renamed_Object (E) = Pack then
8718 elsif Is_Actual_Of_Previous_Formal (E) then
8728 end Denotes_Formal_Package;
8734 procedure End_Generic is
8736 -- ??? More things could be factored out in this routine. Should
8737 -- probably be done at a later stage.
8739 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8740 Generic_Flags.Decrement_Last;
8742 Expander_Mode_Restore;
8749 function Earlier (N1, N2 : Node_Id) return Boolean is
8750 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8751 -- Find distance from given node to enclosing compilation unit
8757 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8760 and then Nkind (P) /= N_Compilation_Unit
8762 P := True_Parent (P);
8767 -- Local declarations
8776 -- Start of processing for Earlier
8779 Find_Depth (P1, D1);
8780 Find_Depth (P2, D2);
8790 P1 := True_Parent (P1);
8795 P2 := True_Parent (P2);
8799 -- At this point P1 and P2 are at the same distance from the root.
8800 -- We examine their parents until we find a common declarative list.
8801 -- If we reach the root, N1 and N2 do not descend from the same
8802 -- declarative list (e.g. one is nested in the declarative part and
8803 -- the other is in a block in the statement part) and the earlier
8804 -- one is already frozen.
8806 while not Is_List_Member (P1)
8807 or else not Is_List_Member (P2)
8808 or else List_Containing (P1) /= List_Containing (P2)
8810 P1 := True_Parent (P1);
8811 P2 := True_Parent (P2);
8813 if Nkind (Parent (P1)) = N_Subunit then
8814 P1 := Corresponding_Stub (Parent (P1));
8817 if Nkind (Parent (P2)) = N_Subunit then
8818 P2 := Corresponding_Stub (Parent (P2));
8826 -- Expanded code usually shares the source location of the original
8827 -- construct it was generated for. This however may not necessarily
8828 -- reflect the true location of the code within the tree.
8830 -- Before comparing the slocs of the two nodes, make sure that we are
8831 -- working with correct source locations. Assume that P1 is to the left
8832 -- of P2. If either one does not come from source, traverse the common
8833 -- list heading towards the other node and locate the first source
8837 -- ----+===+===+--------------+===+===+----
8838 -- expanded code expanded code
8840 if not Comes_From_Source (P1) then
8841 while Present (P1) loop
8843 -- Neither P2 nor a source statement were located during the
8844 -- search. If we reach the end of the list, then P1 does not
8845 -- occur earlier than P2.
8848 -- start --- P2 ----- P1 --- end
8850 if No (Next (P1)) then
8853 -- We encounter P2 while going to the right of the list. This
8854 -- means that P1 does indeed appear earlier.
8857 -- start --- P1 ===== P2 --- end
8858 -- expanded code in between
8863 -- No need to look any further since we have located a source
8866 elsif Comes_From_Source (P1) then
8876 if not Comes_From_Source (P2) then
8877 while Present (P2) loop
8879 -- Neither P1 nor a source statement were located during the
8880 -- search. If we reach the start of the list, then P1 does not
8881 -- occur earlier than P2.
8884 -- start --- P2 --- P1 --- end
8886 if No (Prev (P2)) then
8889 -- We encounter P1 while going to the left of the list. This
8890 -- means that P1 does indeed appear earlier.
8893 -- start --- P1 ===== P2 --- end
8894 -- expanded code in between
8899 -- No need to look any further since we have located a source
8902 elsif Comes_From_Source (P2) then
8912 -- At this point either both nodes came from source or we approximated
8913 -- their source locations through neighboring source statements.
8915 T1 := Top_Level_Location (Sloc (P1));
8916 T2 := Top_Level_Location (Sloc (P2));
8918 -- When two nodes come from the same instance, they have identical top
8919 -- level locations. To determine proper relation within the tree, check
8920 -- their locations within the template.
8923 return Sloc (P1) < Sloc (P2);
8925 -- The two nodes either come from unrelated instances or do not come
8926 -- from instantiated code at all.
8933 ----------------------
8934 -- Find_Actual_Type --
8935 ----------------------
8937 function Find_Actual_Type
8939 Gen_Type : Entity_Id) return Entity_Id
8941 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8945 -- Special processing only applies to child units
8947 if not Is_Child_Unit (Gen_Scope) then
8948 return Get_Instance_Of (Typ);
8950 -- If designated or component type is itself a formal of the child unit,
8951 -- its instance is available.
8953 elsif Scope (Typ) = Gen_Scope then
8954 return Get_Instance_Of (Typ);
8956 -- If the array or access type is not declared in the parent unit,
8957 -- no special processing needed.
8959 elsif not Is_Generic_Type (Typ)
8960 and then Scope (Gen_Scope) /= Scope (Typ)
8962 return Get_Instance_Of (Typ);
8964 -- Otherwise, retrieve designated or component type by visibility
8967 T := Current_Entity (Typ);
8968 while Present (T) loop
8969 if In_Open_Scopes (Scope (T)) then
8971 elsif Is_Generic_Actual_Type (T) then
8980 end Find_Actual_Type;
8982 ----------------------------
8983 -- Freeze_Subprogram_Body --
8984 ----------------------------
8986 procedure Freeze_Subprogram_Body
8987 (Inst_Node : Node_Id;
8989 Pack_Id : Entity_Id)
8991 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8992 Par : constant Entity_Id := Scope (Gen_Unit);
8998 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8999 -- Find innermost package body that encloses the given node, and which
9000 -- is not a compilation unit. Freeze nodes for the instance, or for its
9001 -- enclosing body, may be inserted after the enclosing_body of the
9002 -- generic unit. Used to determine proper placement of freeze node for
9003 -- both package and subprogram instances.
9005 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9006 -- Find entity for given package body, and locate or create a freeze
9009 ----------------------------
9010 -- Enclosing_Package_Body --
9011 ----------------------------
9013 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9019 and then Nkind (Parent (P)) /= N_Compilation_Unit
9021 if Nkind (P) = N_Package_Body then
9022 if Nkind (Parent (P)) = N_Subunit then
9023 return Corresponding_Stub (Parent (P));
9029 P := True_Parent (P);
9033 end Enclosing_Package_Body;
9035 -------------------------
9036 -- Package_Freeze_Node --
9037 -------------------------
9039 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9043 if Nkind (B) = N_Package_Body then
9044 Id := Corresponding_Spec (B);
9045 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9046 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9049 Ensure_Freeze_Node (Id);
9050 return Freeze_Node (Id);
9051 end Package_Freeze_Node;
9053 -- Start of processing for Freeze_Subprogram_Body
9056 -- If the instance and the generic body appear within the same unit, and
9057 -- the instance precedes the generic, the freeze node for the instance
9058 -- must appear after that of the generic. If the generic is nested
9059 -- within another instance I2, then current instance must be frozen
9060 -- after I2. In both cases, the freeze nodes are those of enclosing
9061 -- packages. Otherwise, the freeze node is placed at the end of the
9062 -- current declarative part.
9064 Enc_G := Enclosing_Package_Body (Gen_Body);
9065 Enc_I := Enclosing_Package_Body (Inst_Node);
9066 Ensure_Freeze_Node (Pack_Id);
9067 F_Node := Freeze_Node (Pack_Id);
9069 if Is_Generic_Instance (Par)
9070 and then Present (Freeze_Node (Par))
9071 and then In_Same_Declarative_Part
9072 (Parent (Freeze_Node (Par)), Inst_Node)
9074 -- The parent was a premature instantiation. Insert freeze node at
9075 -- the end the current declarative part.
9077 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9078 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9080 -- Handle the following case:
9082 -- package Parent_Inst is new ...
9085 -- procedure P ... -- this body freezes Parent_Inst
9087 -- package Inst is new ...
9089 -- In this particular scenario, the freeze node for Inst must be
9090 -- inserted in the same manner as that of Parent_Inst - before the
9091 -- next source body or at the end of the declarative list (body not
9092 -- available). If body P did not exist and Parent_Inst was frozen
9093 -- after Inst, either by a body following Inst or at the end of the
9094 -- declarative region, the freeze node for Inst must be inserted
9095 -- after that of Parent_Inst. This relation is established by
9096 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9098 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9099 List_Containing (Inst_Node)
9100 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9102 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9105 Insert_After (Freeze_Node (Par), F_Node);
9108 -- The body enclosing the instance should be frozen after the body that
9109 -- includes the generic, because the body of the instance may make
9110 -- references to entities therein. If the two are not in the same
9111 -- declarative part, or if the one enclosing the instance is frozen
9112 -- already, freeze the instance at the end of the current declarative
9115 elsif Is_Generic_Instance (Par)
9116 and then Present (Freeze_Node (Par))
9117 and then Present (Enc_I)
9119 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9120 -- The enclosing package may contain several instances. Rather
9121 -- than computing the earliest point at which to insert its freeze
9122 -- node, we place it at the end of the declarative part of the
9123 -- parent of the generic.
9125 Insert_Freeze_Node_For_Instance
9126 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9129 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9131 elsif Present (Enc_G)
9132 and then Present (Enc_I)
9133 and then Enc_G /= Enc_I
9134 and then Earlier (Inst_Node, Gen_Body)
9136 -- Freeze package that encloses instance, and place node after the
9137 -- package that encloses generic. If enclosing package is already
9138 -- frozen we have to assume it is at the proper place. This may be a
9139 -- potential ABE that requires dynamic checking. Do not add a freeze
9140 -- node if the package that encloses the generic is inside the body
9141 -- that encloses the instance, because the freeze node would be in
9142 -- the wrong scope. Additional contortions needed if the bodies are
9143 -- within a subunit.
9146 Enclosing_Body : Node_Id;
9149 if Nkind (Enc_I) = N_Package_Body_Stub then
9150 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9152 Enclosing_Body := Enc_I;
9155 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9156 Insert_Freeze_Node_For_Instance
9157 (Enc_G, Package_Freeze_Node (Enc_I));
9161 -- Freeze enclosing subunit before instance
9163 Enc_G_F := Package_Freeze_Node (Enc_G);
9165 if not Is_List_Member (Enc_G_F) then
9166 Insert_After (Enc_G, Enc_G_F);
9169 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9172 -- If none of the above, insert freeze node at the end of the current
9173 -- declarative part.
9175 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9177 end Freeze_Subprogram_Body;
9183 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9185 return Generic_Renamings.Table (E).Gen_Id;
9188 ---------------------
9189 -- Get_Instance_Of --
9190 ---------------------
9192 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9193 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9196 if Res /= Assoc_Null then
9197 return Generic_Renamings.Table (Res).Act_Id;
9200 -- On exit, entity is not instantiated: not a generic parameter, or
9201 -- else parameter of an inner generic unit.
9205 end Get_Instance_Of;
9207 ---------------------------------
9208 -- Get_Unit_Instantiation_Node --
9209 ---------------------------------
9211 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9212 Decl : Node_Id := Unit_Declaration_Node (A);
9216 -- If the Package_Instantiation attribute has been set on the package
9217 -- entity, then use it directly when it (or its Original_Node) refers
9218 -- to an N_Package_Instantiation node. In principle it should be
9219 -- possible to have this field set in all cases, which should be
9220 -- investigated, and would allow this function to be significantly
9223 Inst := Package_Instantiation (A);
9225 if Present (Inst) then
9226 if Nkind (Inst) = N_Package_Instantiation then
9229 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9230 return Original_Node (Inst);
9234 -- If the instantiation is a compilation unit that does not need body
9235 -- then the instantiation node has been rewritten as a package
9236 -- declaration for the instance, and we return the original node.
9238 -- If it is a compilation unit and the instance node has not been
9239 -- rewritten, then it is still the unit of the compilation. Finally, if
9240 -- a body is present, this is a parent of the main unit whose body has
9241 -- been compiled for inlining purposes, and the instantiation node has
9242 -- been rewritten with the instance body.
9244 -- Otherwise the instantiation node appears after the declaration. If
9245 -- the entity is a formal package, the declaration may have been
9246 -- rewritten as a generic declaration (in the case of a formal with box)
9247 -- or left as a formal package declaration if it has actuals, and is
9248 -- found with a forward search.
9250 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9251 if Nkind (Decl) = N_Package_Declaration
9252 and then Present (Corresponding_Body (Decl))
9254 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9257 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9258 return Original_Node (Decl);
9260 return Unit (Parent (Decl));
9263 elsif Nkind (Decl) = N_Package_Declaration
9264 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9266 return Original_Node (Decl);
9269 Inst := Next (Decl);
9270 while Nkind (Inst) not in N_Formal_Package_Declaration
9271 | N_Function_Instantiation
9272 | N_Package_Instantiation
9273 | N_Procedure_Instantiation
9280 end Get_Unit_Instantiation_Node;
9282 ------------------------
9283 -- Has_Been_Exchanged --
9284 ------------------------
9286 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9290 Next := First_Elmt (Exchanged_Views);
9291 while Present (Next) loop
9292 if Full_View (Node (Next)) = E then
9300 end Has_Been_Exchanged;
9306 function Has_Contracts (Decl : Node_Id) return Boolean is
9307 A_List : constant List_Id := Aspect_Specifications (Decl);
9314 A_Spec := First (A_List);
9315 while Present (A_Spec) loop
9316 A_Id := Get_Aspect_Id (A_Spec);
9317 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9332 function Hash (F : Entity_Id) return HTable_Range is
9334 return HTable_Range (F mod HTable_Size);
9337 ------------------------
9338 -- Hide_Current_Scope --
9339 ------------------------
9341 procedure Hide_Current_Scope is
9342 C : constant Entity_Id := Current_Scope;
9346 Set_Is_Hidden_Open_Scope (C);
9348 E := First_Entity (C);
9349 while Present (E) loop
9350 if Is_Immediately_Visible (E) then
9351 Set_Is_Immediately_Visible (E, False);
9352 Append_Elmt (E, Hidden_Entities);
9358 -- Make the scope name invisible as well. This is necessary, but might
9359 -- conflict with calls to Rtsfind later on, in case the scope is a
9360 -- predefined one. There is no clean solution to this problem, so for
9361 -- now we depend on the user not redefining Standard itself in one of
9362 -- the parent units.
9364 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9365 Set_Is_Immediately_Visible (C, False);
9366 Append_Elmt (C, Hidden_Entities);
9369 end Hide_Current_Scope;
9375 procedure Init_Env is
9376 Saved : Instance_Env;
9379 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9380 Saved.Exchanged_Views := Exchanged_Views;
9381 Saved.Hidden_Entities := Hidden_Entities;
9382 Saved.Current_Sem_Unit := Current_Sem_Unit;
9383 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9384 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9386 -- Save configuration switches. These may be reset if the unit is a
9387 -- predefined unit, and the current mode is not Ada 2005.
9389 Saved.Switches := Save_Config_Switches;
9391 Instance_Envs.Append (Saved);
9393 Exchanged_Views := New_Elmt_List;
9394 Hidden_Entities := New_Elmt_List;
9396 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9397 -- this is set properly in Set_Instance_Env.
9399 Current_Instantiated_Parent :=
9400 (Current_Scope, Current_Scope, Assoc_Null);
9403 ---------------------
9404 -- In_Main_Context --
9405 ---------------------
9407 function In_Main_Context (E : Entity_Id) return Boolean is
9413 if not Is_Compilation_Unit (E)
9414 or else Ekind (E) /= E_Package
9415 or else In_Private_Part (E)
9420 Context := Context_Items (Cunit (Main_Unit));
9422 Clause := First (Context);
9423 while Present (Clause) loop
9424 if Nkind (Clause) = N_With_Clause then
9425 Nam := Name (Clause);
9427 -- If the current scope is part of the context of the main unit,
9428 -- analysis of the corresponding with_clause is not complete, and
9429 -- the entity is not set. We use the Chars field directly, which
9430 -- might produce false positives in rare cases, but guarantees
9431 -- that we produce all the instance bodies we will need.
9433 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9434 or else (Nkind (Nam) = N_Selected_Component
9435 and then Chars (Selector_Name (Nam)) = Chars (E))
9445 end In_Main_Context;
9447 ---------------------
9448 -- Inherit_Context --
9449 ---------------------
9451 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9452 Current_Context : List_Id;
9453 Current_Unit : Node_Id;
9462 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9464 -- The inherited context is attached to the enclosing compilation
9465 -- unit. This is either the main unit, or the declaration for the
9466 -- main unit (in case the instantiation appears within the package
9467 -- declaration and the main unit is its body).
9469 Current_Unit := Parent (Inst);
9470 while Present (Current_Unit)
9471 and then Nkind (Current_Unit) /= N_Compilation_Unit
9473 Current_Unit := Parent (Current_Unit);
9476 Current_Context := Context_Items (Current_Unit);
9478 Item := First (Context_Items (Parent (Gen_Decl)));
9479 while Present (Item) loop
9480 if Nkind (Item) = N_With_Clause then
9481 Lib_Unit := Library_Unit (Item);
9483 -- Take care to prevent direct cyclic with's
9485 if Lib_Unit /= Current_Unit then
9487 -- Do not add a unit if it is already in the context
9489 Clause := First (Current_Context);
9491 while Present (Clause) loop
9492 if Nkind (Clause) = N_With_Clause
9493 and then Library_Unit (Clause) = Lib_Unit
9503 New_I := New_Copy (Item);
9504 Set_Implicit_With (New_I);
9506 Append (New_I, Current_Context);
9514 end Inherit_Context;
9520 procedure Initialize is
9522 Generic_Renamings.Init;
9525 Generic_Renamings_HTable.Reset;
9526 Circularity_Detected := False;
9527 Exchanged_Views := No_Elist;
9528 Hidden_Entities := No_Elist;
9531 -------------------------------------
9532 -- Insert_Freeze_Node_For_Instance --
9533 -------------------------------------
9535 procedure Insert_Freeze_Node_For_Instance
9544 function Enclosing_Body (N : Node_Id) return Node_Id;
9545 -- Find enclosing package or subprogram body, if any. Freeze node may
9546 -- be placed at end of current declarative list if previous instance
9547 -- and current one have different enclosing bodies.
9549 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9550 -- Find the local instance, if any, that declares the generic that is
9551 -- being instantiated. If present, the freeze node for this instance
9552 -- must follow the freeze node for the previous instance.
9554 --------------------
9555 -- Enclosing_Body --
9556 --------------------
9558 function Enclosing_Body (N : Node_Id) return Node_Id is
9564 and then Nkind (Parent (P)) /= N_Compilation_Unit
9566 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9567 if Nkind (Parent (P)) = N_Subunit then
9568 return Corresponding_Stub (Parent (P));
9574 P := True_Parent (P);
9580 -----------------------
9581 -- Previous_Instance --
9582 -----------------------
9584 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9589 while Present (S) and then S /= Standard_Standard loop
9590 if Is_Generic_Instance (S)
9591 and then In_Same_Source_Unit (S, N)
9600 end Previous_Instance;
9602 -- Start of processing for Insert_Freeze_Node_For_Instance
9605 if not Is_List_Member (F_Node) then
9607 Decls := List_Containing (N);
9608 Inst := Entity (F_Node);
9609 Par_N := Parent (Decls);
9611 -- When processing a subprogram instantiation, utilize the actual
9612 -- subprogram instantiation rather than its package wrapper as it
9613 -- carries all the context information.
9615 if Is_Wrapper_Package (Inst) then
9616 Inst := Related_Instance (Inst);
9619 -- If this is a package instance, check whether the generic is
9620 -- declared in a previous instance and the current instance is
9621 -- not within the previous one.
9623 if Present (Generic_Parent (Parent (Inst)))
9624 and then Is_In_Main_Unit (N)
9627 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9628 Par_I : constant Entity_Id :=
9630 (Generic_Parent (Parent (Inst)));
9635 and then Earlier (N, Freeze_Node (Par_I))
9637 Scop := Scope (Inst);
9639 -- If the current instance is within the one that contains
9640 -- the generic, the freeze node for the current one must
9641 -- appear in the current declarative part. Ditto, if the
9642 -- current instance is within another package instance or
9643 -- within a body that does not enclose the current instance.
9644 -- In these three cases the freeze node of the previous
9645 -- instance is not relevant.
9647 while Present (Scop) and then Scop /= Standard_Standard loop
9648 exit when Scop = Par_I
9650 (Is_Generic_Instance (Scop)
9651 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9652 Scop := Scope (Scop);
9655 -- Previous instance encloses current instance
9657 if Scop = Par_I then
9660 -- If the next node is a source body we must freeze in
9661 -- the current scope as well.
9663 elsif Present (Next (N))
9664 and then Nkind (Next (N)) in N_Subprogram_Body
9666 and then Comes_From_Source (Next (N))
9670 -- Current instance is within an unrelated instance
9672 elsif Is_Generic_Instance (Scop) then
9675 -- Current instance is within an unrelated body
9677 elsif Present (Enclosing_N)
9678 and then Enclosing_N /= Enclosing_Body (Par_I)
9683 Insert_After (Freeze_Node (Par_I), F_Node);
9690 -- When the instantiation occurs in a package declaration, append the
9691 -- freeze node to the private declarations (if any).
9693 if Nkind (Par_N) = N_Package_Specification
9694 and then Decls = Visible_Declarations (Par_N)
9695 and then Present (Private_Declarations (Par_N))
9696 and then not Is_Empty_List (Private_Declarations (Par_N))
9698 Decls := Private_Declarations (Par_N);
9699 Decl := First (Decls);
9702 -- Determine the proper freeze point of a package instantiation. We
9703 -- adhere to the general rule of a package or subprogram body causing
9704 -- freezing of anything before it in the same declarative region. In
9705 -- this case, the proper freeze point of a package instantiation is
9706 -- before the first source body which follows, or before a stub. This
9707 -- ensures that entities coming from the instance are already frozen
9708 -- and usable in source bodies.
9710 if Nkind (Par_N) /= N_Package_Declaration
9711 and then Ekind (Inst) = E_Package
9712 and then Is_Generic_Instance (Inst)
9714 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9716 while Present (Decl) loop
9717 if (Nkind (Decl) in N_Unit_Body
9719 Nkind (Decl) in N_Body_Stub)
9720 and then Comes_From_Source (Decl)
9722 Insert_Before (Decl, F_Node);
9730 -- In a package declaration, or if no previous body, insert at end
9733 Set_Sloc (F_Node, Sloc (Last (Decls)));
9734 Insert_After (Last (Decls), F_Node);
9736 end Insert_Freeze_Node_For_Instance;
9742 procedure Install_Body
9743 (Act_Body : Node_Id;
9748 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9749 -- Check if the generic definition and the instantiation come from
9750 -- a common scope, in which case the instance must be frozen after
9751 -- the generic body.
9753 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9754 -- If the instance is nested inside a generic unit, the Sloc of the
9755 -- instance indicates the place of the original definition, not the
9756 -- point of the current enclosing instance. Pending a better usage of
9757 -- Slocs to indicate instantiation places, we determine the place of
9758 -- origin of a node by finding the maximum sloc of any ancestor node.
9759 -- Why is this not equivalent to Top_Level_Location ???
9765 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9766 Act_Scop : Entity_Id := Scope (Act_Id);
9767 Gen_Scop : Entity_Id := Scope (Gen_Id);
9770 while Act_Scop /= Standard_Standard
9771 and then Gen_Scop /= Standard_Standard
9773 if Act_Scop = Gen_Scop then
9777 Act_Scop := Scope (Act_Scop);
9778 Gen_Scop := Scope (Gen_Scop);
9788 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9795 while Present (N1) and then N1 /= Act_Unit loop
9796 if Sloc (N1) > Res then
9806 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9807 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9808 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9809 Par : constant Entity_Id := Scope (Gen_Id);
9810 Gen_Unit : constant Node_Id :=
9811 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9813 Body_Unit : Node_Id;
9815 Must_Delay : Boolean;
9816 Orig_Body : Node_Id := Gen_Body;
9818 -- Start of processing for Install_Body
9821 -- Handle first the case of an instance with incomplete actual types.
9822 -- The instance body cannot be placed after the declaration because
9823 -- full views have not been seen yet. Any use of the non-limited views
9824 -- in the instance body requires the presence of a regular with_clause
9825 -- in the enclosing unit, and will fail if this with_clause is missing.
9826 -- We place the instance body at the beginning of the enclosing body,
9827 -- which is the unit being compiled. The freeze node for the instance
9828 -- is then placed after the instance body.
9830 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9831 and then Expander_Active
9832 and then Ekind (Scope (Act_Id)) = E_Package
9835 Scop : constant Entity_Id := Scope (Act_Id);
9836 Body_Id : constant Node_Id :=
9837 Corresponding_Body (Unit_Declaration_Node (Scop));
9840 Ensure_Freeze_Node (Act_Id);
9841 F_Node := Freeze_Node (Act_Id);
9842 if Present (Body_Id) then
9843 Set_Is_Frozen (Act_Id, False);
9844 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9845 if Is_List_Member (F_Node) then
9849 Insert_After (Act_Body, F_Node);
9855 -- If the body is a subunit, the freeze point is the corresponding stub
9856 -- in the current compilation, not the subunit itself.
9858 if Nkind (Parent (Gen_Body)) = N_Subunit then
9859 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9861 Orig_Body := Gen_Body;
9864 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9866 -- If the instantiation and the generic definition appear in the same
9867 -- package declaration, this is an early instantiation. If they appear
9868 -- in the same declarative part, it is an early instantiation only if
9869 -- the generic body appears textually later, and the generic body is
9870 -- also in the main unit.
9872 -- If instance is nested within a subprogram, and the generic body
9873 -- is not, the instance is delayed because the enclosing body is. If
9874 -- instance and body are within the same scope, or the same subprogram
9875 -- body, indicate explicitly that the instance is delayed.
9878 (Gen_Unit = Act_Unit
9879 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9880 | N_Package_Declaration
9881 or else (Gen_Unit = Body_Unit
9882 and then True_Sloc (N, Act_Unit) <
9884 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9885 and then In_Same_Scope (Gen_Id, Act_Id));
9887 -- If this is an early instantiation, the freeze node is placed after
9888 -- the generic body. Otherwise, if the generic appears in an instance,
9889 -- we cannot freeze the current instance until the outer one is frozen.
9890 -- This is only relevant if the current instance is nested within some
9891 -- inner scope not itself within the outer instance. If this scope is
9892 -- a package body in the same declarative part as the outer instance,
9893 -- then that body needs to be frozen after the outer instance. Finally,
9894 -- if no delay is needed, we place the freeze node at the end of the
9895 -- current declarative part.
9898 and then (No (Freeze_Node (Act_Id))
9899 or else not Is_List_Member (Freeze_Node (Act_Id)))
9901 Ensure_Freeze_Node (Act_Id);
9902 F_Node := Freeze_Node (Act_Id);
9905 Insert_After (Orig_Body, F_Node);
9907 elsif Is_Generic_Instance (Par)
9908 and then Present (Freeze_Node (Par))
9909 and then Scope (Act_Id) /= Par
9911 -- Freeze instance of inner generic after instance of enclosing
9914 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9916 -- Handle the following case:
9918 -- package Parent_Inst is new ...
9921 -- procedure P ... -- this body freezes Parent_Inst
9923 -- package Inst is new ...
9925 -- In this particular scenario, the freeze node for Inst must
9926 -- be inserted in the same manner as that of Parent_Inst,
9927 -- before the next source body or at the end of the declarative
9928 -- list (body not available). If body P did not exist and
9929 -- Parent_Inst was frozen after Inst, either by a body
9930 -- following Inst or at the end of the declarative region,
9931 -- the freeze node for Inst must be inserted after that of
9932 -- Parent_Inst. This relation is established by comparing
9933 -- the Slocs of Parent_Inst freeze node and Inst.
9934 -- We examine the parents of the enclosing lists to handle
9935 -- the case where the parent instance is in the visible part
9936 -- of a package declaration, and the inner instance is in
9937 -- the corresponding private part.
9939 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9940 = Parent (List_Containing (N))
9941 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9943 Insert_Freeze_Node_For_Instance (N, F_Node);
9945 Insert_After (Freeze_Node (Par), F_Node);
9948 -- Freeze package enclosing instance of inner generic after
9949 -- instance of enclosing generic.
9951 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9952 and then In_Same_Declarative_Part
9953 (Parent (Freeze_Node (Par)), Parent (N))
9956 Enclosing : Entity_Id;
9959 Enclosing := Corresponding_Spec (Parent (N));
9961 if No (Enclosing) then
9962 Enclosing := Defining_Entity (Parent (N));
9965 Insert_Freeze_Node_For_Instance (N, F_Node);
9966 Ensure_Freeze_Node (Enclosing);
9968 if not Is_List_Member (Freeze_Node (Enclosing)) then
9970 -- The enclosing context is a subunit, insert the freeze
9971 -- node after the stub.
9973 if Nkind (Parent (Parent (N))) = N_Subunit then
9974 Insert_Freeze_Node_For_Instance
9975 (Corresponding_Stub (Parent (Parent (N))),
9976 Freeze_Node (Enclosing));
9978 -- The enclosing context is a package with a stub body
9979 -- which has already been replaced by the real body.
9980 -- Insert the freeze node after the actual body.
9982 elsif Ekind (Enclosing) = E_Package
9983 and then Present (Body_Entity (Enclosing))
9984 and then Was_Originally_Stub
9985 (Parent (Body_Entity (Enclosing)))
9987 Insert_Freeze_Node_For_Instance
9988 (Parent (Body_Entity (Enclosing)),
9989 Freeze_Node (Enclosing));
9991 -- The parent instance has been frozen before the body of
9992 -- the enclosing package, insert the freeze node after
9995 elsif List_Containing (Freeze_Node (Par)) =
9996 List_Containing (Parent (N))
9997 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9999 Insert_Freeze_Node_For_Instance
10000 (Parent (N), Freeze_Node (Enclosing));
10004 (Freeze_Node (Par), Freeze_Node (Enclosing));
10010 Insert_Freeze_Node_For_Instance (N, F_Node);
10014 Insert_Freeze_Node_For_Instance (N, F_Node);
10018 Set_Is_Frozen (Act_Id);
10019 Insert_Before (N, Act_Body);
10020 Mark_Rewrite_Insertion (Act_Body);
10023 -----------------------------
10024 -- Install_Formal_Packages --
10025 -----------------------------
10027 procedure Install_Formal_Packages (Par : Entity_Id) is
10030 Gen_E : Entity_Id := Empty;
10033 E := First_Entity (Par);
10035 -- If we are installing an instance parent, locate the formal packages
10036 -- of its generic parent.
10038 if Is_Generic_Instance (Par) then
10039 Gen := Generic_Parent (Package_Specification (Par));
10040 Gen_E := First_Entity (Gen);
10043 while Present (E) loop
10044 if Ekind (E) = E_Package
10045 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10047 -- If this is the renaming for the parent instance, done
10049 if Renamed_Object (E) = Par then
10052 -- The visibility of a formal of an enclosing generic is already
10055 elsif Denotes_Formal_Package (E) then
10058 elsif Present (Associated_Formal_Package (E)) then
10059 Check_Generic_Actuals (Renamed_Object (E), True);
10060 Set_Is_Hidden (E, False);
10062 -- Find formal package in generic unit that corresponds to
10063 -- (instance of) formal package in instance.
10065 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10066 Next_Entity (Gen_E);
10069 if Present (Gen_E) then
10070 Map_Formal_Package_Entities (Gen_E, E);
10077 if Present (Gen_E) then
10078 Next_Entity (Gen_E);
10081 end Install_Formal_Packages;
10083 --------------------
10084 -- Install_Parent --
10085 --------------------
10087 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10088 Ancestors : constant Elist_Id := New_Elmt_List;
10089 S : constant Entity_Id := Current_Scope;
10090 Inst_Par : Entity_Id;
10091 First_Par : Entity_Id;
10092 Inst_Node : Node_Id;
10093 Gen_Par : Entity_Id;
10094 First_Gen : Entity_Id;
10097 procedure Install_Noninstance_Specs (Par : Entity_Id);
10098 -- Install the scopes of noninstance parent units ending with Par
10100 procedure Install_Spec (Par : Entity_Id);
10101 -- The child unit is within the declarative part of the parent, so the
10102 -- declarations within the parent are immediately visible.
10104 -------------------------------
10105 -- Install_Noninstance_Specs --
10106 -------------------------------
10108 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10111 and then Par /= Standard_Standard
10112 and then not In_Open_Scopes (Par)
10114 Install_Noninstance_Specs (Scope (Par));
10115 Install_Spec (Par);
10117 end Install_Noninstance_Specs;
10123 procedure Install_Spec (Par : Entity_Id) is
10124 Spec : constant Node_Id := Package_Specification (Par);
10127 -- If this parent of the child instance is a top-level unit,
10128 -- then record the unit and its visibility for later resetting in
10129 -- Remove_Parent. We exclude units that are generic instances, as we
10130 -- only want to record this information for the ultimate top-level
10131 -- noninstance parent (is that always correct???).
10133 if Scope (Par) = Standard_Standard
10134 and then not Is_Generic_Instance (Par)
10136 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10137 Instance_Parent_Unit := Par;
10140 -- Open the parent scope and make it and its declarations visible.
10141 -- If this point is not within a body, then only the visible
10142 -- declarations should be made visible, and installation of the
10143 -- private declarations is deferred until the appropriate point
10144 -- within analysis of the spec being instantiated (see the handling
10145 -- of parent visibility in Analyze_Package_Specification). This is
10146 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10147 -- private view problems that occur when compiling instantiations of
10148 -- a generic child of that package (Generic_Dispatching_Constructor).
10149 -- If the instance freezes a tagged type, inlinings of operations
10150 -- from Ada.Tags may need the full view of type Tag. If inlining took
10151 -- proper account of establishing visibility of inlined subprograms'
10152 -- parents then it should be possible to remove this
10153 -- special check. ???
10156 Set_Is_Immediately_Visible (Par);
10157 Install_Visible_Declarations (Par);
10158 Set_Use (Visible_Declarations (Spec));
10160 if In_Body or else Is_RTU (Par, Ada_Tags) then
10161 Install_Private_Declarations (Par);
10162 Set_Use (Private_Declarations (Spec));
10166 -- Start of processing for Install_Parent
10169 -- We need to install the parent instance to compile the instantiation
10170 -- of the child, but the child instance must appear in the current
10171 -- scope. Given that we cannot place the parent above the current scope
10172 -- in the scope stack, we duplicate the current scope and unstack both
10173 -- after the instantiation is complete.
10175 -- If the parent is itself the instantiation of a child unit, we must
10176 -- also stack the instantiation of its parent, and so on. Each such
10177 -- ancestor is the prefix of the name in a prior instantiation.
10179 -- If this is a nested instance, the parent unit itself resolves to
10180 -- a renaming of the parent instance, whose declaration we need.
10182 -- Finally, the parent may be a generic (not an instance) when the
10183 -- child unit appears as a formal package.
10187 if Present (Renamed_Entity (Inst_Par)) then
10188 Inst_Par := Renamed_Entity (Inst_Par);
10191 First_Par := Inst_Par;
10193 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10195 First_Gen := Gen_Par;
10197 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10199 -- Load grandparent instance as well
10201 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10203 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10204 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10206 if Present (Renamed_Entity (Inst_Par)) then
10207 Inst_Par := Renamed_Entity (Inst_Par);
10210 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10212 if Present (Gen_Par) then
10213 Prepend_Elmt (Inst_Par, Ancestors);
10216 -- Parent is not the name of an instantiation
10218 Install_Noninstance_Specs (Inst_Par);
10229 if Present (First_Gen) then
10230 Append_Elmt (First_Par, Ancestors);
10232 Install_Noninstance_Specs (First_Par);
10235 if not Is_Empty_Elmt_List (Ancestors) then
10236 Elmt := First_Elmt (Ancestors);
10237 while Present (Elmt) loop
10238 Install_Spec (Node (Elmt));
10239 Install_Formal_Packages (Node (Elmt));
10244 if not In_Body then
10247 end Install_Parent;
10249 -------------------------------
10250 -- Install_Hidden_Primitives --
10251 -------------------------------
10253 procedure Install_Hidden_Primitives
10254 (Prims_List : in out Elist_Id;
10259 List : Elist_Id := No_Elist;
10260 Prim_G_Elmt : Elmt_Id;
10261 Prim_A_Elmt : Elmt_Id;
10266 -- No action needed in case of serious errors because we cannot trust
10267 -- in the order of primitives
10269 if Serious_Errors_Detected > 0 then
10272 -- No action possible if we don't have available the list of primitive
10276 or else not Is_Record_Type (Gen_T)
10277 or else not Is_Tagged_Type (Gen_T)
10278 or else not Is_Record_Type (Act_T)
10279 or else not Is_Tagged_Type (Act_T)
10283 -- There is no need to handle interface types since their primitives
10284 -- cannot be hidden
10286 elsif Is_Interface (Gen_T) then
10290 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10292 if not Is_Class_Wide_Type (Act_T) then
10293 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10295 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10299 -- Skip predefined primitives in the generic formal
10301 while Present (Prim_G_Elmt)
10302 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10304 Next_Elmt (Prim_G_Elmt);
10307 -- Skip predefined primitives in the generic actual
10309 while Present (Prim_A_Elmt)
10310 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10312 Next_Elmt (Prim_A_Elmt);
10315 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10317 Prim_G := Node (Prim_G_Elmt);
10318 Prim_A := Node (Prim_A_Elmt);
10320 -- There is no need to handle interface primitives because their
10321 -- primitives are not hidden
10323 exit when Present (Interface_Alias (Prim_G));
10325 -- Here we install one hidden primitive
10327 if Chars (Prim_G) /= Chars (Prim_A)
10328 and then Has_Suffix (Prim_A, 'P')
10329 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10331 Set_Chars (Prim_A, Chars (Prim_G));
10332 Append_New_Elmt (Prim_A, To => List);
10335 Next_Elmt (Prim_A_Elmt);
10336 Next_Elmt (Prim_G_Elmt);
10339 -- Append the elements to the list of temporarily visible primitives
10340 -- avoiding duplicates.
10342 if Present (List) then
10343 if No (Prims_List) then
10344 Prims_List := New_Elmt_List;
10347 Elmt := First_Elmt (List);
10348 while Present (Elmt) loop
10349 Append_Unique_Elmt (Node (Elmt), Prims_List);
10353 end Install_Hidden_Primitives;
10355 -------------------------------
10356 -- Restore_Hidden_Primitives --
10357 -------------------------------
10359 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10360 Prim_Elmt : Elmt_Id;
10364 if Prims_List /= No_Elist then
10365 Prim_Elmt := First_Elmt (Prims_List);
10366 while Present (Prim_Elmt) loop
10367 Prim := Node (Prim_Elmt);
10368 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10369 Next_Elmt (Prim_Elmt);
10372 Prims_List := No_Elist;
10374 end Restore_Hidden_Primitives;
10376 --------------------------------
10377 -- Instantiate_Formal_Package --
10378 --------------------------------
10380 function Instantiate_Formal_Package
10383 Analyzed_Formal : Node_Id) return List_Id
10385 Loc : constant Source_Ptr := Sloc (Actual);
10386 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10387 Actual_Pack : Entity_Id;
10388 Formal_Pack : Entity_Id;
10389 Gen_Parent : Entity_Id;
10392 Parent_Spec : Node_Id;
10394 procedure Find_Matching_Actual
10396 Act : in out Entity_Id);
10397 -- We need to associate each formal entity in the formal package with
10398 -- the corresponding entity in the actual package. The actual package
10399 -- has been analyzed and possibly expanded, and as a result there is
10400 -- no one-to-one correspondence between the two lists (for example,
10401 -- the actual may include subtypes, itypes, and inherited primitive
10402 -- operations, interspersed among the renaming declarations for the
10403 -- actuals). We retrieve the corresponding actual by name because each
10404 -- actual has the same name as the formal, and they do appear in the
10407 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10408 -- Retrieve entity of defining entity of generic formal parameter.
10409 -- Only the declarations of formals need to be considered when
10410 -- linking them to actuals, but the declarative list may include
10411 -- internal entities generated during analysis, and those are ignored.
10413 procedure Match_Formal_Entity
10414 (Formal_Node : Node_Id;
10415 Formal_Ent : Entity_Id;
10416 Actual_Ent : Entity_Id);
10417 -- Associates the formal entity with the actual. In the case where
10418 -- Formal_Ent is a formal package, this procedure iterates through all
10419 -- of its formals and enters associations between the actuals occurring
10420 -- in the formal package's corresponding actual package (given by
10421 -- Actual_Ent) and the formal package's formal parameters. This
10422 -- procedure recurses if any of the parameters is itself a package.
10424 function Is_Instance_Of
10425 (Act_Spec : Entity_Id;
10426 Gen_Anc : Entity_Id) return Boolean;
10427 -- The actual can be an instantiation of a generic within another
10428 -- instance, in which case there is no direct link from it to the
10429 -- original generic ancestor. In that case, we recognize that the
10430 -- ultimate ancestor is the same by examining names and scopes.
10432 procedure Process_Nested_Formal (Formal : Entity_Id);
10433 -- If the current formal is declared with a box, its own formals are
10434 -- visible in the instance, as they were in the generic, and their
10435 -- Hidden flag must be reset. If some of these formals are themselves
10436 -- packages declared with a box, the processing must be recursive.
10438 --------------------------
10439 -- Find_Matching_Actual --
10440 --------------------------
10442 procedure Find_Matching_Actual
10444 Act : in out Entity_Id)
10446 Formal_Ent : Entity_Id;
10449 case Nkind (Original_Node (F)) is
10450 when N_Formal_Object_Declaration
10451 | N_Formal_Type_Declaration
10453 Formal_Ent := Defining_Identifier (F);
10455 while Present (Act)
10456 and then Chars (Act) /= Chars (Formal_Ent)
10461 when N_Formal_Package_Declaration
10462 | N_Formal_Subprogram_Declaration
10463 | N_Generic_Package_Declaration
10464 | N_Package_Declaration
10466 Formal_Ent := Defining_Entity (F);
10468 while Present (Act)
10469 and then Chars (Act) /= Chars (Formal_Ent)
10475 raise Program_Error;
10477 end Find_Matching_Actual;
10479 -------------------------
10480 -- Match_Formal_Entity --
10481 -------------------------
10483 procedure Match_Formal_Entity
10484 (Formal_Node : Node_Id;
10485 Formal_Ent : Entity_Id;
10486 Actual_Ent : Entity_Id)
10488 Act_Pkg : Entity_Id;
10491 Set_Instance_Of (Formal_Ent, Actual_Ent);
10493 if Ekind (Actual_Ent) = E_Package then
10495 -- Record associations for each parameter
10497 Act_Pkg := Actual_Ent;
10500 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10504 Gen_Decl : Node_Id;
10506 Actual : Entity_Id;
10509 -- Retrieve the actual given in the formal package declaration
10511 Actual := Entity (Name (Original_Node (Formal_Node)));
10513 -- The actual in the formal package declaration may be a
10514 -- renamed generic package, in which case we want to retrieve
10515 -- the original generic in order to traverse its formal part.
10517 if Present (Renamed_Entity (Actual)) then
10518 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10520 Gen_Decl := Unit_Declaration_Node (Actual);
10523 Formals := Generic_Formal_Declarations (Gen_Decl);
10525 if Present (Formals) then
10526 F_Node := First_Non_Pragma (Formals);
10531 while Present (A_Ent)
10532 and then Present (F_Node)
10533 and then A_Ent /= First_Private_Entity (Act_Pkg)
10535 F_Ent := Get_Formal_Entity (F_Node);
10537 if Present (F_Ent) then
10539 -- This is a formal of the original package. Record
10540 -- association and recurse.
10542 Find_Matching_Actual (F_Node, A_Ent);
10543 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10544 Next_Entity (A_Ent);
10547 Next_Non_Pragma (F_Node);
10551 end Match_Formal_Entity;
10553 -----------------------
10554 -- Get_Formal_Entity --
10555 -----------------------
10557 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10558 Kind : constant Node_Kind := Nkind (Original_Node (N));
10561 when N_Formal_Object_Declaration =>
10562 return Defining_Identifier (N);
10564 when N_Formal_Type_Declaration =>
10565 return Defining_Identifier (N);
10567 when N_Formal_Subprogram_Declaration =>
10568 return Defining_Unit_Name (Specification (N));
10570 when N_Formal_Package_Declaration =>
10571 return Defining_Identifier (Original_Node (N));
10573 when N_Generic_Package_Declaration =>
10574 return Defining_Identifier (Original_Node (N));
10576 -- All other declarations are introduced by semantic analysis and
10577 -- have no match in the actual.
10582 end Get_Formal_Entity;
10584 --------------------
10585 -- Is_Instance_Of --
10586 --------------------
10588 function Is_Instance_Of
10589 (Act_Spec : Entity_Id;
10590 Gen_Anc : Entity_Id) return Boolean
10592 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10595 if No (Gen_Par) then
10598 -- Simplest case: the generic parent of the actual is the formal
10600 elsif Gen_Par = Gen_Anc then
10603 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10606 -- The actual may be obtained through several instantiations. Its
10607 -- scope must itself be an instance of a generic declared in the
10608 -- same scope as the formal. Any other case is detected above.
10610 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10614 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10616 end Is_Instance_Of;
10618 ---------------------------
10619 -- Process_Nested_Formal --
10620 ---------------------------
10622 procedure Process_Nested_Formal (Formal : Entity_Id) is
10626 if Present (Associated_Formal_Package (Formal))
10627 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10629 Ent := First_Entity (Formal);
10630 while Present (Ent) loop
10631 Set_Is_Hidden (Ent, False);
10632 Set_Is_Visible_Formal (Ent);
10633 Set_Is_Potentially_Use_Visible
10634 (Ent, Is_Potentially_Use_Visible (Formal));
10636 if Ekind (Ent) = E_Package then
10637 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10638 Process_Nested_Formal (Ent);
10644 end Process_Nested_Formal;
10646 -- Start of processing for Instantiate_Formal_Package
10651 -- The actual must be a package instance, or else a current instance
10652 -- such as a parent generic within the body of a generic child.
10654 if not Is_Entity_Name (Actual)
10655 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10658 ("expect package instance to instantiate formal", Actual);
10659 Abandon_Instantiation (Actual);
10660 raise Program_Error;
10663 Actual_Pack := Entity (Actual);
10664 Set_Is_Instantiated (Actual_Pack);
10666 -- The actual may be a renamed package, or an outer generic formal
10667 -- package whose instantiation is converted into a renaming.
10669 if Present (Renamed_Object (Actual_Pack)) then
10670 Actual_Pack := Renamed_Object (Actual_Pack);
10673 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10674 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10675 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10678 Generic_Parent (Specification (Analyzed_Formal));
10680 Defining_Unit_Name (Specification (Analyzed_Formal));
10683 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10684 Parent_Spec := Package_Specification (Actual_Pack);
10686 Parent_Spec := Parent (Actual_Pack);
10689 if Gen_Parent = Any_Id then
10691 ("previous error in declaration of formal package", Actual);
10692 Abandon_Instantiation (Actual);
10694 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10697 -- If this is the current instance of an enclosing generic, that unit
10698 -- is the generic package we need.
10700 elsif In_Open_Scopes (Actual_Pack)
10701 and then Ekind (Actual_Pack) = E_Generic_Package
10707 ("actual parameter must be instance of&", Actual, Gen_Parent);
10708 Abandon_Instantiation (Actual);
10711 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10712 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10715 Make_Package_Renaming_Declaration (Loc,
10716 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10717 Name => New_Occurrence_Of (Actual_Pack, Loc));
10719 Set_Associated_Formal_Package
10720 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10721 Decls := New_List (Nod);
10723 -- If the formal F has a box, then the generic declarations are
10724 -- visible in the generic G. In an instance of G, the corresponding
10725 -- entities in the actual for F (which are the actuals for the
10726 -- instantiation of the generic that F denotes) must also be made
10727 -- visible for analysis of the current instance. On exit from the
10728 -- current instance, those entities are made private again. If the
10729 -- actual is currently in use, these entities are also use-visible.
10731 -- The loop through the actual entities also steps through the formal
10732 -- entities and enters associations from formals to actuals into the
10733 -- renaming map. This is necessary to properly handle checking of
10734 -- actual parameter associations for later formals that depend on
10735 -- actuals declared in the formal package.
10737 -- In Ada 2005, partial parameterization requires that we make
10738 -- visible the actuals corresponding to formals that were defaulted
10739 -- in the formal package. There formals are identified because they
10740 -- remain formal generics within the formal package, rather than
10741 -- being renamings of the actuals supplied.
10744 Gen_Decl : constant Node_Id :=
10745 Unit_Declaration_Node (Gen_Parent);
10746 Formals : constant List_Id :=
10747 Generic_Formal_Declarations (Gen_Decl);
10749 Actual_Ent : Entity_Id;
10750 Actual_Of_Formal : Node_Id;
10751 Formal_Node : Node_Id;
10752 Formal_Ent : Entity_Id;
10755 if Present (Formals) then
10756 Formal_Node := First_Non_Pragma (Formals);
10758 Formal_Node := Empty;
10761 Actual_Ent := First_Entity (Actual_Pack);
10762 Actual_Of_Formal :=
10763 First (Visible_Declarations (Specification (Analyzed_Formal)));
10764 while Present (Actual_Ent)
10765 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10767 if Present (Formal_Node) then
10768 Formal_Ent := Get_Formal_Entity (Formal_Node);
10770 if Present (Formal_Ent) then
10771 Find_Matching_Actual (Formal_Node, Actual_Ent);
10772 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10774 -- We iterate at the same time over the actuals of the
10775 -- local package created for the formal, to determine
10776 -- which one of the formals of the original generic were
10777 -- defaulted in the formal. The corresponding actual
10778 -- entities are visible in the enclosing instance.
10780 if Box_Present (Formal)
10782 (Present (Actual_Of_Formal)
10785 (Get_Formal_Entity (Actual_Of_Formal)))
10787 Set_Is_Hidden (Actual_Ent, False);
10788 Set_Is_Visible_Formal (Actual_Ent);
10789 Set_Is_Potentially_Use_Visible
10790 (Actual_Ent, In_Use (Actual_Pack));
10792 if Ekind (Actual_Ent) = E_Package then
10793 Process_Nested_Formal (Actual_Ent);
10797 if not Is_Hidden (Actual_Ent) then
10798 Append_Elmt (Actual_Ent, Hidden_Formals);
10801 Set_Is_Hidden (Actual_Ent);
10802 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10806 Next_Non_Pragma (Formal_Node);
10807 Next (Actual_Of_Formal);
10810 -- No further formals to match, but the generic part may
10811 -- contain inherited operation that are not hidden in the
10812 -- enclosing instance.
10814 Next_Entity (Actual_Ent);
10818 -- Inherited subprograms generated by formal derived types are
10819 -- also visible if the types are.
10821 Actual_Ent := First_Entity (Actual_Pack);
10822 while Present (Actual_Ent)
10823 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10825 if Is_Overloadable (Actual_Ent)
10827 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10829 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10831 Set_Is_Hidden (Actual_Ent, False);
10832 Set_Is_Potentially_Use_Visible
10833 (Actual_Ent, In_Use (Actual_Pack));
10836 Next_Entity (Actual_Ent);
10839 -- No conformance to check if the generic has no formal parameters
10840 -- and the formal package has no generic associations.
10842 if Is_Empty_List (Formals)
10844 (Box_Present (Formal)
10845 or else No (Generic_Associations (Formal)))
10851 -- If the formal is not declared with a box, reanalyze it as an
10852 -- abbreviated instantiation, to verify the matching rules of 12.7.
10853 -- The actual checks are performed after the generic associations
10854 -- have been analyzed, to guarantee the same visibility for this
10855 -- instantiation and for the actuals.
10857 -- In Ada 2005, the generic associations for the formal can include
10858 -- defaulted parameters. These are ignored during check. This
10859 -- internal instantiation is removed from the tree after conformance
10860 -- checking, because it contains formal declarations for those
10861 -- defaulted parameters, and those should not reach the back-end.
10863 if not Box_Present (Formal) then
10865 I_Pack : constant Entity_Id :=
10866 Make_Temporary (Sloc (Actual), 'P');
10869 Set_Is_Internal (I_Pack);
10870 Set_Ekind (I_Pack, E_Package);
10871 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10874 Make_Package_Instantiation (Sloc (Actual),
10875 Defining_Unit_Name => I_Pack,
10878 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10879 Generic_Associations => Generic_Associations (Formal)));
10885 end Instantiate_Formal_Package;
10887 -----------------------------------
10888 -- Instantiate_Formal_Subprogram --
10889 -----------------------------------
10891 function Instantiate_Formal_Subprogram
10894 Analyzed_Formal : Node_Id) return Node_Id
10896 Analyzed_S : constant Entity_Id :=
10897 Defining_Unit_Name (Specification (Analyzed_Formal));
10898 Formal_Sub : constant Entity_Id :=
10899 Defining_Unit_Name (Specification (Formal));
10901 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10902 -- If the generic is a child unit, the parent has been installed on the
10903 -- scope stack, but a default subprogram cannot resolve to something
10904 -- on the parent because that parent is not really part of the visible
10905 -- context (it is there to resolve explicit local entities). If the
10906 -- default has resolved in this way, we remove the entity from immediate
10907 -- visibility and analyze the node again to emit an error message or
10908 -- find another visible candidate.
10910 procedure Valid_Actual_Subprogram (Act : Node_Id);
10911 -- Perform legality check and raise exception on failure
10913 -----------------------
10914 -- From_Parent_Scope --
10915 -----------------------
10917 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10918 Gen_Scope : Node_Id;
10921 Gen_Scope := Scope (Analyzed_S);
10922 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10923 if Scope (Subp) = Scope (Gen_Scope) then
10927 Gen_Scope := Scope (Gen_Scope);
10931 end From_Parent_Scope;
10933 -----------------------------
10934 -- Valid_Actual_Subprogram --
10935 -----------------------------
10937 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10941 if Is_Entity_Name (Act) then
10942 Act_E := Entity (Act);
10944 elsif Nkind (Act) = N_Selected_Component
10945 and then Is_Entity_Name (Selector_Name (Act))
10947 Act_E := Entity (Selector_Name (Act));
10953 if (Present (Act_E) and then Is_Overloadable (Act_E))
10954 or else Nkind (Act) in N_Attribute_Reference
10955 | N_Indexed_Component
10956 | N_Character_Literal
10957 | N_Explicit_Dereference
10963 ("expect subprogram or entry name in instantiation of &",
10964 Instantiation_Node, Formal_Sub);
10965 Abandon_Instantiation (Instantiation_Node);
10966 end Valid_Actual_Subprogram;
10970 Decl_Node : Node_Id;
10973 New_Spec : Node_Id;
10974 New_Subp : Entity_Id;
10976 -- Start of processing for Instantiate_Formal_Subprogram
10979 New_Spec := New_Copy_Tree (Specification (Formal));
10981 -- The tree copy has created the proper instantiation sloc for the
10982 -- new specification. Use this location for all other constructed
10985 Loc := Sloc (Defining_Unit_Name (New_Spec));
10987 -- Create new entity for the actual (New_Copy_Tree does not), and
10988 -- indicate that it is an actual.
10990 -- If the actual is not an entity (i.e. an attribute reference)
10991 -- and the formal includes aspect specifications for contracts,
10992 -- we create an internal name for the renaming declaration. The
10993 -- constructed wrapper contains a call to the entity in the renaming.
10994 -- This is an expansion activity, as is the wrapper creation.
10996 if Ada_Version >= Ada_2020
10997 and then Has_Contracts (Analyzed_Formal)
10998 and then not Is_Entity_Name (Actual)
10999 and then Expander_Active
11001 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11002 Set_Defining_Unit_Name (New_Spec, New_Subp);
11004 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11007 Set_Ekind (New_Subp, Ekind (Analyzed_S));
11008 Set_Is_Generic_Actual_Subprogram (New_Subp);
11009 Set_Defining_Unit_Name (New_Spec, New_Subp);
11011 -- Create new entities for the each of the formals in the specification
11012 -- of the renaming declaration built for the actual.
11014 if Present (Parameter_Specifications (New_Spec)) then
11020 F := First (Parameter_Specifications (New_Spec));
11021 while Present (F) loop
11022 F_Id := Defining_Identifier (F);
11024 Set_Defining_Identifier (F,
11025 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11031 -- Find entity of actual. If the actual is an attribute reference, it
11032 -- cannot be resolved here (its formal is missing) but is handled
11033 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11034 -- fully resolved subsequently, when the renaming declaration for the
11035 -- formal is analyzed. If it is an explicit dereference, resolve the
11036 -- prefix but not the actual itself, to prevent interpretation as call.
11038 if Present (Actual) then
11039 Loc := Sloc (Actual);
11040 Set_Sloc (New_Spec, Loc);
11042 if Nkind (Actual) = N_Operator_Symbol then
11043 Find_Direct_Name (Actual);
11045 elsif Nkind (Actual) = N_Explicit_Dereference then
11046 Analyze (Prefix (Actual));
11048 elsif Nkind (Actual) /= N_Attribute_Reference then
11052 Valid_Actual_Subprogram (Actual);
11055 elsif Present (Default_Name (Formal)) then
11056 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11057 | N_Selected_Component
11058 | N_Indexed_Component
11059 | N_Character_Literal
11060 and then Present (Entity (Default_Name (Formal)))
11062 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11064 Nam := New_Copy (Default_Name (Formal));
11065 Set_Sloc (Nam, Loc);
11068 elsif Box_Present (Formal) then
11070 -- Actual is resolved at the point of instantiation. Create an
11071 -- identifier or operator with the same name as the formal.
11073 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11075 Make_Operator_Symbol (Loc,
11076 Chars => Chars (Formal_Sub),
11077 Strval => No_String);
11079 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11082 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11083 and then Null_Present (Specification (Formal))
11085 -- Generate null body for procedure, for use in the instance
11088 Make_Subprogram_Body (Loc,
11089 Specification => New_Spec,
11090 Declarations => New_List,
11091 Handled_Statement_Sequence =>
11092 Make_Handled_Sequence_Of_Statements (Loc,
11093 Statements => New_List (Make_Null_Statement (Loc))));
11095 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11097 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11099 -- Eliminate the calls to it when optimization is enabled
11101 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11105 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11107 ("missing actual&", Instantiation_Node, Formal_Sub);
11109 ("\in instantiation of & declared#",
11110 Instantiation_Node, Scope (Analyzed_S));
11111 Abandon_Instantiation (Instantiation_Node);
11115 Make_Subprogram_Renaming_Declaration (Loc,
11116 Specification => New_Spec,
11119 -- If we do not have an actual and the formal specified <> then set to
11120 -- get proper default.
11122 if No (Actual) and then Box_Present (Formal) then
11123 Set_From_Default (Decl_Node);
11126 -- Gather possible interpretations for the actual before analyzing the
11127 -- instance. If overloaded, it will be resolved when analyzing the
11128 -- renaming declaration.
11130 if Box_Present (Formal) and then No (Actual) then
11133 if Is_Child_Unit (Scope (Analyzed_S))
11134 and then Present (Entity (Nam))
11136 if not Is_Overloaded (Nam) then
11137 if From_Parent_Scope (Entity (Nam)) then
11138 Set_Is_Immediately_Visible (Entity (Nam), False);
11139 Set_Entity (Nam, Empty);
11140 Set_Etype (Nam, Empty);
11143 Set_Is_Immediately_Visible (Entity (Nam));
11152 Get_First_Interp (Nam, I, It);
11153 while Present (It.Nam) loop
11154 if From_Parent_Scope (It.Nam) then
11158 Get_Next_Interp (I, It);
11165 -- The generic instantiation freezes the actual. This can only be done
11166 -- once the actual is resolved, in the analysis of the renaming
11167 -- declaration. To make the formal subprogram entity available, we set
11168 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11169 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11170 -- of formal abstract subprograms.
11172 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11174 -- We cannot analyze the renaming declaration, and thus find the actual,
11175 -- until all the actuals are assembled in the instance. For subsequent
11176 -- checks of other actuals, indicate the node that will hold the
11177 -- instance of this formal.
11179 Set_Instance_Of (Analyzed_S, Nam);
11181 if Nkind (Actual) = N_Selected_Component
11182 and then Is_Task_Type (Etype (Prefix (Actual)))
11183 and then not Is_Frozen (Etype (Prefix (Actual)))
11185 -- The renaming declaration will create a body, which must appear
11186 -- outside of the instantiation, We move the renaming declaration
11187 -- out of the instance, and create an additional renaming inside,
11188 -- to prevent freezing anomalies.
11191 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11194 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11195 Insert_Before (Instantiation_Node, Decl_Node);
11196 Analyze (Decl_Node);
11198 -- Now create renaming within the instance
11201 Make_Subprogram_Renaming_Declaration (Loc,
11202 Specification => New_Copy_Tree (New_Spec),
11203 Name => New_Occurrence_Of (Anon_Id, Loc));
11205 Set_Defining_Unit_Name (Specification (Decl_Node),
11206 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11211 end Instantiate_Formal_Subprogram;
11213 ------------------------
11214 -- Instantiate_Object --
11215 ------------------------
11217 function Instantiate_Object
11220 Analyzed_Formal : Node_Id) return List_Id
11222 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11223 A_Gen_Obj : constant Entity_Id :=
11224 Defining_Identifier (Analyzed_Formal);
11225 Acc_Def : Node_Id := Empty;
11226 Act_Assoc : constant Node_Id := Parent (Actual);
11227 Actual_Decl : Node_Id := Empty;
11228 Decl_Node : Node_Id;
11231 List : constant List_Id := New_List;
11232 Loc : constant Source_Ptr := Sloc (Actual);
11233 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11234 Subt_Decl : Node_Id := Empty;
11235 Subt_Mark : Node_Id := Empty;
11237 -- Start of processing for Instantiate_Object
11240 -- Formal may be an anonymous access
11242 if Present (Subtype_Mark (Formal)) then
11243 Subt_Mark := Subtype_Mark (Formal);
11245 Check_Access_Definition (Formal);
11246 Acc_Def := Access_Definition (Formal);
11249 -- Sloc for error message on missing actual
11251 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11253 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11254 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11257 Set_Parent (List, Parent (Actual));
11261 if Out_Present (Formal) then
11263 -- An IN OUT generic actual must be a name. The instantiation is a
11264 -- renaming declaration. The actual is the name being renamed. We
11265 -- use the actual directly, rather than a copy, because it is not
11266 -- used further in the list of actuals, and because a copy or a use
11267 -- of relocate_node is incorrect if the instance is nested within a
11268 -- generic. In order to simplify e.g. ASIS queries, the
11269 -- Generic_Parent field links the declaration to the generic
11272 if No (Actual) then
11274 ("missing actual &",
11275 Instantiation_Node, Gen_Obj);
11277 ("\in instantiation of & declared#",
11278 Instantiation_Node, Scope (A_Gen_Obj));
11279 Abandon_Instantiation (Instantiation_Node);
11282 if Present (Subt_Mark) then
11284 Make_Object_Renaming_Declaration (Loc,
11285 Defining_Identifier => New_Copy (Gen_Obj),
11286 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11289 else pragma Assert (Present (Acc_Def));
11291 Make_Object_Renaming_Declaration (Loc,
11292 Defining_Identifier => New_Copy (Gen_Obj),
11293 Access_Definition => New_Copy_Tree (Acc_Def),
11297 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11299 -- The analysis of the actual may produce Insert_Action nodes, so
11300 -- the declaration must have a context in which to attach them.
11302 Append (Decl_Node, List);
11305 -- Return if the analysis of the actual reported some error
11307 if Etype (Actual) = Any_Type then
11311 -- This check is performed here because Analyze_Object_Renaming will
11312 -- not check it when Comes_From_Source is False. Note though that the
11313 -- check for the actual being the name of an object will be performed
11314 -- in Analyze_Object_Renaming.
11316 if Is_Object_Reference (Actual)
11317 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11320 ("illegal discriminant-dependent component for in out parameter",
11324 -- The actual has to be resolved in order to check that it is a
11325 -- variable (due to cases such as F (1), where F returns access to
11326 -- an array, and for overloaded prefixes).
11328 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11330 -- If the type of the formal is not itself a formal, and the current
11331 -- unit is a child unit, the formal type must be declared in a
11332 -- parent, and must be retrieved by visibility.
11334 if Ftyp = Orig_Ftyp
11335 and then Is_Generic_Unit (Scope (Ftyp))
11336 and then Is_Child_Unit (Scope (A_Gen_Obj))
11339 Temp : constant Node_Id :=
11340 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11342 Set_Entity (Temp, Empty);
11344 Ftyp := Entity (Temp);
11348 if Is_Private_Type (Ftyp)
11349 and then not Is_Private_Type (Etype (Actual))
11350 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11351 or else Base_Type (Etype (Actual)) = Ftyp)
11353 -- If the actual has the type of the full view of the formal, or
11354 -- else a non-private subtype of the formal, then the visibility
11355 -- of the formal type has changed. Add to the actuals a subtype
11356 -- declaration that will force the exchange of views in the body
11357 -- of the instance as well.
11360 Make_Subtype_Declaration (Loc,
11361 Defining_Identifier => Make_Temporary (Loc, 'P'),
11362 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11364 Prepend (Subt_Decl, List);
11366 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11367 Exchange_Declarations (Ftyp);
11370 Resolve (Actual, Ftyp);
11372 if not Denotes_Variable (Actual) then
11373 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11375 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11377 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11378 -- the type of the actual shall resolve to a specific anonymous
11381 if Ada_Version < Ada_2005
11382 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11383 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11386 ("type of actual does not match type of&", Actual, Gen_Obj);
11390 Note_Possible_Modification (Actual, Sure => True);
11392 -- Check for instantiation with atomic/volatile/VFA object actual for
11393 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11395 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11397 ("cannot instantiate nonatomic formal & of mode in out",
11399 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11401 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11404 ("cannot instantiate nonvolatile formal & of mode in out",
11406 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11408 elsif Is_Volatile_Full_Access_Object (Actual)
11409 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11412 ("cannot instantiate nonfull access formal & of mode in out",
11415 ("\with full access object actual (RM C.6(12))", Actual);
11418 -- Check for instantiation on nonatomic subcomponent of a full access
11419 -- object in Ada 2020 (RM C.6 (12)).
11421 if Ada_Version >= Ada_2020
11422 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11423 and then not Is_Atomic_Object (Actual)
11426 ("cannot instantiate formal & of mode in out with actual",
11429 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11433 -- Check actual/formal compatibility with respect to the four
11434 -- volatility refinement aspects.
11437 Actual_Obj : Entity_Id;
11438 N : Node_Id := Actual;
11440 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11441 -- pointer dereference like component selection.
11443 if Is_Entity_Name (N) then
11444 Actual_Obj := Entity (N);
11449 when N_Indexed_Component
11450 | N_Selected_Component
11452 | N_Explicit_Dereference
11456 when N_Type_Conversion =>
11457 N := Expression (N);
11460 Actual_Obj := Etype (N);
11465 Check_Volatility_Compatibility
11466 (Actual_Obj, A_Gen_Obj, "actual object",
11467 "its corresponding formal object of mode in out",
11468 Srcpos_Bearer => Actual);
11471 -- Formal in-parameter
11474 -- The instantiation of a generic formal in-parameter is constant
11475 -- declaration. The actual is the expression for that declaration.
11476 -- Its type is a full copy of the type of the formal. This may be
11477 -- an access to subprogram, for which we need to generate entities
11478 -- for the formals in the new signature.
11480 if Present (Actual) then
11481 if Present (Subt_Mark) then
11482 Def := New_Copy_Tree (Subt_Mark);
11484 pragma Assert (Present (Acc_Def));
11485 Def := New_Copy_Tree (Acc_Def);
11489 Make_Object_Declaration (Loc,
11490 Defining_Identifier => New_Copy (Gen_Obj),
11491 Constant_Present => True,
11492 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11493 Object_Definition => Def,
11494 Expression => Actual);
11496 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11498 -- A generic formal object of a tagged type is defined to be
11499 -- aliased so the new constant must also be treated as aliased.
11501 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11502 Set_Aliased_Present (Decl_Node);
11505 Append (Decl_Node, List);
11507 -- No need to repeat (pre-)analysis of some expression nodes
11508 -- already handled in Preanalyze_Actuals.
11510 if Nkind (Actual) /= N_Allocator then
11513 -- Return if the analysis of the actual reported some error
11515 if Etype (Actual) = Any_Type then
11521 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11525 Typ := Get_Instance_Of (Formal_Type);
11527 -- If the actual appears in the current or an enclosing scope,
11528 -- use its type directly. This is relevant if it has an actual
11529 -- subtype that is distinct from its nominal one. This cannot
11530 -- be done in general because the type of the actual may
11531 -- depend on other actuals, and only be fully determined when
11532 -- the enclosing instance is analyzed.
11534 if Present (Etype (Actual))
11535 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11537 Freeze_Before (Instantiation_Node, Etype (Actual));
11539 Freeze_Before (Instantiation_Node, Typ);
11542 -- If the actual is an aggregate, perform name resolution on
11543 -- its components (the analysis of an aggregate does not do it)
11544 -- to capture local names that may be hidden if the generic is
11547 if Nkind (Actual) = N_Aggregate then
11548 Preanalyze_And_Resolve (Actual, Typ);
11551 if Is_Limited_Type (Typ)
11552 and then not OK_For_Limited_Init (Typ, Actual)
11555 ("initialization not allowed for limited types", Actual);
11556 Explain_Limited_Type (Typ, Actual);
11560 elsif Present (Default_Expression (Formal)) then
11562 -- Use default to construct declaration
11564 if Present (Subt_Mark) then
11565 Def := New_Copy (Subt_Mark);
11567 pragma Assert (Present (Acc_Def));
11568 Def := New_Copy_Tree (Acc_Def);
11572 Make_Object_Declaration (Sloc (Formal),
11573 Defining_Identifier => New_Copy (Gen_Obj),
11574 Constant_Present => True,
11575 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11576 Object_Definition => Def,
11577 Expression => New_Copy_Tree
11578 (Default_Expression (Formal)));
11580 Set_Corresponding_Generic_Association
11581 (Decl_Node, Expression (Decl_Node));
11583 Append (Decl_Node, List);
11584 Set_Analyzed (Expression (Decl_Node), False);
11587 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11588 Error_Msg_NE ("\in instantiation of & declared#",
11589 Instantiation_Node, Scope (A_Gen_Obj));
11591 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11593 -- Create dummy constant declaration so that instance can be
11594 -- analyzed, to minimize cascaded visibility errors.
11596 if Present (Subt_Mark) then
11598 else pragma Assert (Present (Acc_Def));
11603 Make_Object_Declaration (Loc,
11604 Defining_Identifier => New_Copy (Gen_Obj),
11605 Constant_Present => True,
11606 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11607 Object_Definition => New_Copy (Def),
11609 Make_Attribute_Reference (Sloc (Gen_Obj),
11610 Attribute_Name => Name_First,
11611 Prefix => New_Copy (Def)));
11613 Append (Decl_Node, List);
11616 Abandon_Instantiation (Instantiation_Node);
11621 if Nkind (Actual) in N_Has_Entity then
11622 Actual_Decl := Parent (Entity (Actual));
11625 -- Ada 2005 (AI-423) refined by AI12-0287:
11626 -- For an object_renaming_declaration with a null_exclusion or an
11627 -- access_definition that has a null_exclusion, the subtype of the
11628 -- object_name shall exclude null. In addition, if the
11629 -- object_renaming_declaration occurs within the body of a generic unit
11630 -- G or within the body of a generic unit declared within the
11631 -- declarative region of generic unit G, then:
11632 -- * if the object_name statically denotes a generic formal object of
11633 -- mode in out of G, then the declaration of that object shall have a
11635 -- * if the object_name statically denotes a call of a generic formal
11636 -- function of G, then the declaration of the result of that function
11637 -- shall have a null_exclusion.
11639 if Ada_Version >= Ada_2005
11640 and then Present (Actual_Decl)
11641 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11642 | N_Object_Declaration
11643 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11644 and then not Has_Null_Exclusion (Actual_Decl)
11645 and then Has_Null_Exclusion (Analyzed_Formal)
11646 and then Ekind (Defining_Identifier (Analyzed_Formal))
11647 = E_Generic_In_Out_Parameter
11648 and then ((In_Generic_Scope (Entity (Actual))
11649 and then In_Package_Body (Scope (Entity (Actual))))
11650 or else not Can_Never_Be_Null (Etype (Actual)))
11652 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11654 ("actual must exclude null to match generic formal#", Actual);
11657 -- An effectively volatile object cannot be used as an actual in a
11658 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11659 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11660 -- legality rule, and also verifies that the actual is an object.
11663 and then Present (Actual)
11664 and then Is_Object_Reference (Actual)
11665 and then Is_Effectively_Volatile_Object (Actual)
11666 and then not Is_Effectively_Volatile (A_Gen_Obj)
11669 ("volatile object cannot act as actual in generic instantiation",
11674 end Instantiate_Object;
11676 ------------------------------
11677 -- Instantiate_Package_Body --
11678 ------------------------------
11680 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11681 -- must be replaced by gotos which jump to the end of the routine in order
11682 -- to restore the Ghost and SPARK modes.
11684 procedure Instantiate_Package_Body
11685 (Body_Info : Pending_Body_Info;
11686 Inlined_Body : Boolean := False;
11687 Body_Optional : Boolean := False)
11689 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11690 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11691 Act_Spec : constant Node_Id := Specification (Act_Decl);
11692 Ctx_Parents : Elist_Id := No_Elist;
11693 Ctx_Top : Int := 0;
11694 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11695 Gen_Id : constant Node_Id := Name (Inst_Node);
11696 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11697 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11698 Loc : constant Source_Ptr := Sloc (Inst_Node);
11700 procedure Check_Initialized_Types;
11701 -- In a generic package body, an entity of a generic private type may
11702 -- appear uninitialized. This is suspicious, unless the actual is a
11703 -- fully initialized type.
11705 procedure Install_Parents_Of_Generic_Context
11706 (Inst_Scope : Entity_Id;
11707 Ctx_Parents : out Elist_Id);
11708 -- Inst_Scope is the scope where the instance appears within; when it
11709 -- appears within a generic child package G, this routine collects and
11710 -- installs the enclosing packages of G in the scopes stack; installed
11711 -- packages are returned in Ctx_Parents.
11713 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11714 -- Reverse effect after instantiation is complete
11716 -----------------------------
11717 -- Check_Initialized_Types --
11718 -----------------------------
11720 procedure Check_Initialized_Types is
11722 Formal : Entity_Id;
11723 Actual : Entity_Id;
11724 Uninit_Var : Entity_Id;
11727 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11728 while Present (Decl) loop
11729 Uninit_Var := Empty;
11731 if Nkind (Decl) = N_Private_Extension_Declaration then
11732 Uninit_Var := Uninitialized_Variable (Decl);
11734 elsif Nkind (Decl) = N_Formal_Type_Declaration
11735 and then Nkind (Formal_Type_Definition (Decl)) =
11736 N_Formal_Private_Type_Definition
11739 Uninitialized_Variable (Formal_Type_Definition (Decl));
11742 if Present (Uninit_Var) then
11743 Formal := Defining_Identifier (Decl);
11744 Actual := First_Entity (Act_Decl_Id);
11746 -- For each formal there is a subtype declaration that renames
11747 -- the actual and has the same name as the formal. Locate the
11748 -- formal for warning message about uninitialized variables
11749 -- in the generic, for which the actual type should be a fully
11750 -- initialized type.
11752 while Present (Actual) loop
11753 exit when Ekind (Actual) = E_Package
11754 and then Present (Renamed_Object (Actual));
11756 if Chars (Actual) = Chars (Formal)
11757 and then not Is_Scalar_Type (Actual)
11758 and then not Is_Fully_Initialized_Type (Actual)
11759 and then Warn_On_No_Value_Assigned
11761 Error_Msg_Node_2 := Formal;
11763 ("generic unit has uninitialized variable& of "
11764 & "formal private type &?v?", Actual, Uninit_Var);
11766 ("actual type for& should be fully initialized type?v?",
11771 Next_Entity (Actual);
11777 end Check_Initialized_Types;
11779 ----------------------------------------
11780 -- Install_Parents_Of_Generic_Context --
11781 ----------------------------------------
11783 procedure Install_Parents_Of_Generic_Context
11784 (Inst_Scope : Entity_Id;
11785 Ctx_Parents : out Elist_Id)
11791 Ctx_Parents := New_Elmt_List;
11793 -- Collect context parents (ie. parents where the instantiation
11794 -- appears within).
11797 while S /= Standard_Standard loop
11798 Prepend_Elmt (S, Ctx_Parents);
11802 -- Install enclosing parents
11804 Elmt := First_Elmt (Ctx_Parents);
11805 while Present (Elmt) loop
11806 Push_Scope (Node (Elmt));
11807 Set_Is_Immediately_Visible (Node (Elmt));
11810 end Install_Parents_Of_Generic_Context;
11812 ---------------------------------------
11813 -- Remove_Parents_Of_Generic_Context --
11814 ---------------------------------------
11816 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11820 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11822 Elmt := Last_Elmt (Ctx_Parents);
11823 while Present (Elmt) loop
11824 pragma Assert (Current_Scope = Node (Elmt));
11825 Set_Is_Immediately_Visible (Current_Scope, False);
11828 Remove_Last_Elmt (Ctx_Parents);
11829 Elmt := Last_Elmt (Ctx_Parents);
11831 end Remove_Parents_Of_Generic_Context;
11835 -- The following constants capture the context prior to instantiating
11836 -- the package body.
11838 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11839 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11840 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11841 Saved_ISMP : constant Boolean :=
11842 Ignore_SPARK_Mode_Pragmas_In_Instance;
11843 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11844 Local_Suppress_Stack_Top;
11845 Saved_SC : constant Boolean := Style_Check;
11846 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11847 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11848 Saved_SS : constant Suppress_Record := Scope_Suppress;
11849 Saved_Warn : constant Warning_Record := Save_Warnings;
11851 Act_Body : Node_Id;
11852 Act_Body_Id : Entity_Id;
11853 Act_Body_Name : Node_Id;
11854 Gen_Body : Node_Id;
11855 Gen_Body_Id : Node_Id;
11856 Par_Ent : Entity_Id := Empty;
11857 Par_Installed : Boolean := False;
11858 Par_Vis : Boolean := False;
11860 Scope_Check_Id : Entity_Id;
11861 Scope_Check_Last : Nat;
11862 -- Value of Current_Scope before calls to Install_Parents; used to check
11863 -- that scopes are correctly removed after instantiation.
11865 Vis_Prims_List : Elist_Id := No_Elist;
11866 -- List of primitives made temporarily visible in the instantiation
11867 -- to match the visibility of the formal type.
11869 -- Start of processing for Instantiate_Package_Body
11872 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11874 -- The instance body may already have been processed, as the parent of
11875 -- another instance that is inlined (Load_Parent_Of_Generic).
11877 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11881 -- The package being instantiated may be subject to pragma Ghost. Set
11882 -- the mode now to ensure that any nodes generated during instantiation
11883 -- are properly marked as Ghost.
11885 Set_Ghost_Mode (Act_Decl_Id);
11887 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11889 -- Re-establish the state of information on which checks are suppressed.
11890 -- This information was set in Body_Info at the point of instantiation,
11891 -- and now we restore it so that the instance is compiled using the
11892 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11894 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11895 Scope_Suppress := Body_Info.Scope_Suppress;
11897 Restore_Config_Switches (Body_Info.Config_Switches);
11898 Restore_Warnings (Body_Info.Warnings);
11900 if No (Gen_Body_Id) then
11902 -- Do not look for parent of generic body if none is required.
11903 -- This may happen when the routine is called as part of the
11904 -- Pending_Instantiations processing, when nested instances
11905 -- may precede the one generated from the main unit.
11907 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11908 and then Body_Optional
11912 Load_Parent_Of_Generic
11913 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11915 -- Surprisingly enough, loading the body of the parent can cause
11916 -- the body to be instantiated and the double instantiation needs
11917 -- to be prevented in order to avoid giving bogus semantic errors.
11919 -- This case can occur because of the Collect_Previous_Instances
11920 -- machinery of Load_Parent_Of_Generic, which will instantiate
11921 -- bodies that are deemed to be ahead of the body of the parent
11922 -- in the compilation unit. But the relative position of these
11923 -- bodies is computed using the mere comparison of their Sloc.
11925 -- Now suppose that you have two generic packages G and H, with
11926 -- G containing a mere instantiation of H:
11932 -- package Nested_G is
11943 -- package My_H is new H;
11947 -- and a third package Q instantiating G and Nested_G:
11953 -- package My_G is new G;
11955 -- package My_Nested_G is new My_G.My_H.Nested_G;
11959 -- The body to be instantiated is that of My_Nested_G and its
11960 -- parent is the instance My_G.My_H. This latter instantiation
11961 -- is done when My_G is analyzed, i.e. after the declarations
11962 -- of My_G and My_Nested_G have been parsed; as a result, the
11963 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11965 -- Therefore loading the body of My_G.My_H will cause the body
11966 -- of My_Nested_G to be instantiated because it is deemed to be
11967 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11968 -- will again be invoked on My_G.My_H, but this time with the
11969 -- Collect_Previous_Instances machinery disabled, so there is
11970 -- no endless mutual recursion and things are done in order.
11972 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11976 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11980 -- Establish global variable for sloc adjustment and for error recovery
11981 -- In the case of an instance body for an instantiation with actuals
11982 -- from a limited view, the instance body is placed at the beginning
11983 -- of the enclosing package body: use the body entity as the source
11984 -- location for nodes of the instance body.
11986 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11988 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11989 Body_Id : constant Node_Id :=
11990 Corresponding_Body (Unit_Declaration_Node (Scop));
11993 Instantiation_Node := Body_Id;
11996 Instantiation_Node := Inst_Node;
11999 if Present (Gen_Body_Id) then
12000 Save_Env (Gen_Unit, Act_Decl_Id);
12001 Style_Check := False;
12003 -- If the context of the instance is subject to SPARK_Mode "off", the
12004 -- annotation is missing, or the body is instantiated at a later pass
12005 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12006 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12009 if SPARK_Mode /= On
12010 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12012 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12015 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12016 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12018 Create_Instantiation_Source
12019 (Inst_Node, Gen_Body_Id, S_Adjustment);
12023 (Original_Node (Gen_Body), Empty, Instantiating => True);
12025 -- Create proper (possibly qualified) defining name for the body, to
12026 -- correspond to the one in the spec.
12029 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12030 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12032 -- Some attributes of spec entity are not inherited by body entity
12034 Set_Handler_Records (Act_Body_Id, No_List);
12036 if Nkind (Defining_Unit_Name (Act_Spec)) =
12037 N_Defining_Program_Unit_Name
12040 Make_Defining_Program_Unit_Name (Loc,
12042 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12043 Defining_Identifier => Act_Body_Id);
12045 Act_Body_Name := Act_Body_Id;
12048 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12050 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12051 Check_Generic_Actuals (Act_Decl_Id, False);
12052 Check_Initialized_Types;
12054 -- Install primitives hidden at the point of the instantiation but
12055 -- visible when processing the generic formals
12061 E := First_Entity (Act_Decl_Id);
12062 while Present (E) loop
12064 and then not Is_Itype (E)
12065 and then Is_Generic_Actual_Type (E)
12066 and then Is_Tagged_Type (E)
12068 Install_Hidden_Primitives
12069 (Prims_List => Vis_Prims_List,
12070 Gen_T => Generic_Parent_Type (Parent (E)),
12078 Scope_Check_Id := Current_Scope;
12079 Scope_Check_Last := Scope_Stack.Last;
12081 -- If the instantiation appears within a generic child some actual
12082 -- parameter may be the current instance of the enclosing generic
12086 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12089 if Is_Child_Unit (Inst_Scope)
12090 and then Ekind (Inst_Scope) = E_Generic_Package
12091 and then Present (Generic_Associations (Inst_Node))
12093 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12095 -- Hide them from visibility; required to avoid conflicts
12096 -- installing the parent instance.
12098 if Present (Ctx_Parents) then
12099 Push_Scope (Standard_Standard);
12100 Ctx_Top := Scope_Stack.Last;
12101 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12106 -- If it is a child unit, make the parent instance (which is an
12107 -- instance of the parent of the generic) visible. The parent
12108 -- instance is the prefix of the name of the generic unit.
12110 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12111 and then Nkind (Gen_Id) = N_Expanded_Name
12113 Par_Ent := Entity (Prefix (Gen_Id));
12114 Par_Vis := Is_Immediately_Visible (Par_Ent);
12115 Install_Parent (Par_Ent, In_Body => True);
12116 Par_Installed := True;
12118 elsif Is_Child_Unit (Gen_Unit) then
12119 Par_Ent := Scope (Gen_Unit);
12120 Par_Vis := Is_Immediately_Visible (Par_Ent);
12121 Install_Parent (Par_Ent, In_Body => True);
12122 Par_Installed := True;
12125 -- If the instantiation is a library unit, and this is the main unit,
12126 -- then build the resulting compilation unit nodes for the instance.
12127 -- If this is a compilation unit but it is not the main unit, then it
12128 -- is the body of a unit in the context, that is being compiled
12129 -- because it is encloses some inlined unit or another generic unit
12130 -- being instantiated. In that case, this body is not part of the
12131 -- current compilation, and is not attached to the tree, but its
12132 -- parent must be set for analysis.
12134 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12136 -- Replace instance node with body of instance, and create new
12137 -- node for corresponding instance declaration.
12139 Build_Instance_Compilation_Unit_Nodes
12140 (Inst_Node, Act_Body, Act_Decl);
12142 -- If the instantiation appears within a generic child package
12143 -- enable visibility of current instance of enclosing generic
12146 if Present (Ctx_Parents) then
12147 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12148 Analyze (Inst_Node);
12149 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12151 Analyze (Inst_Node);
12154 if Parent (Inst_Node) = Cunit (Main_Unit) then
12156 -- If the instance is a child unit itself, then set the scope
12157 -- of the expanded body to be the parent of the instantiation
12158 -- (ensuring that the fully qualified name will be generated
12159 -- for the elaboration subprogram).
12161 if Nkind (Defining_Unit_Name (Act_Spec)) =
12162 N_Defining_Program_Unit_Name
12164 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12168 -- Case where instantiation is not a library unit
12171 -- If this is an early instantiation, i.e. appears textually
12172 -- before the corresponding body and must be elaborated first,
12173 -- indicate that the body instance is to be delayed.
12175 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12177 -- If the instantiation appears within a generic child package
12178 -- enable visibility of current instance of enclosing generic
12181 if Present (Ctx_Parents) then
12182 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12183 Analyze (Act_Body);
12184 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12186 Analyze (Act_Body);
12190 Inherit_Context (Gen_Body, Inst_Node);
12192 if Par_Installed then
12193 Remove_Parent (In_Body => True);
12195 -- Restore the previous visibility of the parent
12197 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12200 -- Remove the parent instances if they have been placed on the scope
12201 -- stack to compile the body.
12203 if Present (Ctx_Parents) then
12204 pragma Assert (Scope_Stack.Last = Ctx_Top
12205 and then Current_Scope = Standard_Standard);
12208 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12211 pragma Assert (Current_Scope = Scope_Check_Id);
12212 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12214 Restore_Hidden_Primitives (Vis_Prims_List);
12216 -- Restore the private views that were made visible when the body of
12217 -- the instantiation was created. Note that, in the case where one of
12218 -- these private views is declared in the parent, there is a nesting
12219 -- issue with the calls to Install_Parent and Remove_Parent made in
12220 -- between above with In_Body set to True, because these calls also
12221 -- want to swap and restore this private view respectively. In this
12222 -- case, the call to Install_Parent does nothing, but the call to
12223 -- Remove_Parent does restore the private view, thus undercutting the
12224 -- call to Restore_Private_Views. That's OK under the condition that
12225 -- the two mechanisms swap exactly the same entities, in particular
12226 -- the private entities dependent on the primary private entities.
12228 Restore_Private_Views (Act_Decl_Id);
12230 -- Remove the current unit from visibility if this is an instance
12231 -- that is not elaborated on the fly for inlining purposes.
12233 if not Inlined_Body then
12234 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12239 -- If we have no body, and the unit requires a body, then complain. This
12240 -- complaint is suppressed if we have detected other errors (since a
12241 -- common reason for missing the body is that it had errors).
12242 -- In CodePeer mode, a warning has been emitted already, no need for
12243 -- further messages.
12245 elsif Unit_Requires_Body (Gen_Unit)
12246 and then not Body_Optional
12248 if CodePeer_Mode then
12251 elsif Serious_Errors_Detected = 0 then
12253 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12255 -- Don't attempt to perform any cleanup actions if some other error
12256 -- was already detected, since this can cause blowups.
12262 -- Case of package that does not need a body
12265 -- If the instantiation of the declaration is a library unit, rewrite
12266 -- the original package instantiation as a package declaration in the
12267 -- compilation unit node.
12269 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12270 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12271 Rewrite (Inst_Node, Act_Decl);
12273 -- Generate elaboration entity, in case spec has elaboration code.
12274 -- This cannot be done when the instance is analyzed, because it
12275 -- is not known yet whether the body exists.
12277 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12278 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12280 -- If the instantiation is not a library unit, then append the
12281 -- declaration to the list of implicitly generated entities, unless
12282 -- it is already a list member which means that it was already
12285 elsif not Is_List_Member (Act_Decl) then
12286 Mark_Rewrite_Insertion (Act_Decl);
12287 Insert_Before (Inst_Node, Act_Decl);
12293 -- Restore the context that was in effect prior to instantiating the
12296 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12297 Local_Suppress_Stack_Top := Saved_LSST;
12298 Scope_Suppress := Saved_SS;
12299 Style_Check := Saved_SC;
12301 Expander_Mode_Restore;
12302 Restore_Config_Switches (Saved_CS);
12303 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12304 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12305 Restore_Warnings (Saved_Warn);
12306 end Instantiate_Package_Body;
12308 ---------------------------------
12309 -- Instantiate_Subprogram_Body --
12310 ---------------------------------
12312 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12313 -- must be replaced by gotos which jump to the end of the routine in order
12314 -- to restore the Ghost and SPARK modes.
12316 procedure Instantiate_Subprogram_Body
12317 (Body_Info : Pending_Body_Info;
12318 Body_Optional : Boolean := False)
12320 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12321 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12322 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12323 Gen_Id : constant Node_Id := Name (Inst_Node);
12324 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12325 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12326 Loc : constant Source_Ptr := Sloc (Inst_Node);
12327 Pack_Id : constant Entity_Id :=
12328 Defining_Unit_Name (Parent (Act_Decl));
12330 -- The following constants capture the context prior to instantiating
12331 -- the subprogram body.
12333 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12334 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12335 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12336 Saved_ISMP : constant Boolean :=
12337 Ignore_SPARK_Mode_Pragmas_In_Instance;
12338 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12339 Local_Suppress_Stack_Top;
12340 Saved_SC : constant Boolean := Style_Check;
12341 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12342 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12343 Saved_SS : constant Suppress_Record := Scope_Suppress;
12344 Saved_Warn : constant Warning_Record := Save_Warnings;
12346 Act_Body : Node_Id;
12347 Act_Body_Id : Entity_Id;
12348 Gen_Body : Node_Id;
12349 Gen_Body_Id : Node_Id;
12350 Pack_Body : Node_Id;
12351 Par_Ent : Entity_Id := Empty;
12352 Par_Installed : Boolean := False;
12353 Par_Vis : Boolean := False;
12354 Ret_Expr : Node_Id;
12357 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12359 -- Subprogram body may have been created already because of an inline
12360 -- pragma, or because of multiple elaborations of the enclosing package
12361 -- when several instances of the subprogram appear in the main unit.
12363 if Present (Corresponding_Body (Act_Decl)) then
12367 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12368 -- the mode now to ensure that any nodes generated during instantiation
12369 -- are properly marked as Ghost.
12371 Set_Ghost_Mode (Act_Decl_Id);
12373 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12375 -- Re-establish the state of information on which checks are suppressed.
12376 -- This information was set in Body_Info at the point of instantiation,
12377 -- and now we restore it so that the instance is compiled using the
12378 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12380 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12381 Scope_Suppress := Body_Info.Scope_Suppress;
12383 Restore_Config_Switches (Body_Info.Config_Switches);
12384 Restore_Warnings (Body_Info.Warnings);
12386 if No (Gen_Body_Id) then
12388 -- For imported generic subprogram, no body to compile, complete
12389 -- the spec entity appropriately.
12391 if Is_Imported (Gen_Unit) then
12392 Set_Is_Imported (Act_Decl_Id);
12393 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12394 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12395 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12396 Set_Has_Completion (Act_Decl_Id);
12399 -- For other cases, compile the body
12402 Load_Parent_Of_Generic
12403 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12404 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12408 Instantiation_Node := Inst_Node;
12410 if Present (Gen_Body_Id) then
12411 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12413 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12415 -- Either body is not present, or context is non-expanding, as
12416 -- when compiling a subunit. Mark the instance as completed, and
12417 -- diagnose a missing body when needed.
12420 and then Operating_Mode = Generate_Code
12422 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12425 Set_Has_Completion (Act_Decl_Id);
12429 Save_Env (Gen_Unit, Act_Decl_Id);
12430 Style_Check := False;
12432 -- If the context of the instance is subject to SPARK_Mode "off", the
12433 -- annotation is missing, or the body is instantiated at a later pass
12434 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12435 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12438 if SPARK_Mode /= On
12439 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12441 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12444 -- If the context of an instance is not subject to SPARK_Mode "off",
12445 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12446 -- the latter should be the one applicable to the instance.
12448 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12449 and then SPARK_Mode /= Off
12450 and then Present (SPARK_Pragma (Gen_Body_Id))
12452 Set_SPARK_Mode (Gen_Body_Id);
12455 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12456 Create_Instantiation_Source
12463 (Original_Node (Gen_Body), Empty, Instantiating => True);
12465 -- Create proper defining name for the body, to correspond to the one
12469 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12471 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12472 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12474 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12475 Set_Has_Completion (Act_Decl_Id);
12476 Check_Generic_Actuals (Pack_Id, False);
12478 -- Generate a reference to link the visible subprogram instance to
12479 -- the generic body, which for navigation purposes is the only
12480 -- available source for the instance.
12483 (Related_Instance (Pack_Id),
12484 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12486 -- If it is a child unit, make the parent instance (which is an
12487 -- instance of the parent of the generic) visible. The parent
12488 -- instance is the prefix of the name of the generic unit.
12490 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12491 and then Nkind (Gen_Id) = N_Expanded_Name
12493 Par_Ent := Entity (Prefix (Gen_Id));
12494 Par_Vis := Is_Immediately_Visible (Par_Ent);
12495 Install_Parent (Par_Ent, In_Body => True);
12496 Par_Installed := True;
12498 elsif Is_Child_Unit (Gen_Unit) then
12499 Par_Ent := Scope (Gen_Unit);
12500 Par_Vis := Is_Immediately_Visible (Par_Ent);
12501 Install_Parent (Par_Ent, In_Body => True);
12502 Par_Installed := True;
12505 -- Subprogram body is placed in the body of wrapper package,
12506 -- whose spec contains the subprogram declaration as well as
12507 -- the renaming declarations for the generic parameters.
12510 Make_Package_Body (Loc,
12511 Defining_Unit_Name => New_Copy (Pack_Id),
12512 Declarations => New_List (Act_Body));
12514 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12516 -- If the instantiation is a library unit, then build resulting
12517 -- compilation unit nodes for the instance. The declaration of
12518 -- the enclosing package is the grandparent of the subprogram
12519 -- declaration. First replace the instantiation node as the unit
12520 -- of the corresponding compilation.
12522 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12523 if Parent (Inst_Node) = Cunit (Main_Unit) then
12524 Set_Unit (Parent (Inst_Node), Inst_Node);
12525 Build_Instance_Compilation_Unit_Nodes
12526 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12527 Analyze (Inst_Node);
12529 Set_Parent (Pack_Body, Parent (Inst_Node));
12530 Analyze (Pack_Body);
12534 Insert_Before (Inst_Node, Pack_Body);
12535 Mark_Rewrite_Insertion (Pack_Body);
12536 Analyze (Pack_Body);
12538 if Expander_Active then
12539 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12543 Inherit_Context (Gen_Body, Inst_Node);
12545 Restore_Private_Views (Pack_Id, False);
12547 if Par_Installed then
12548 Remove_Parent (In_Body => True);
12550 -- Restore the previous visibility of the parent
12552 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12557 -- Body not found. Error was emitted already. If there were no previous
12558 -- errors, this may be an instance whose scope is a premature instance.
12559 -- In that case we must insure that the (legal) program does raise
12560 -- program error if executed. We generate a subprogram body for this
12561 -- purpose. See DEC ac30vso.
12563 -- Should not reference proprietary DEC tests in comments ???
12565 elsif Serious_Errors_Detected = 0
12566 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12568 if Body_Optional then
12571 elsif Ekind (Act_Decl_Id) = E_Procedure then
12573 Make_Subprogram_Body (Loc,
12575 Make_Procedure_Specification (Loc,
12576 Defining_Unit_Name =>
12577 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12578 Parameter_Specifications =>
12580 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12582 Declarations => Empty_List,
12583 Handled_Statement_Sequence =>
12584 Make_Handled_Sequence_Of_Statements (Loc,
12585 Statements => New_List (
12586 Make_Raise_Program_Error (Loc,
12587 Reason => PE_Access_Before_Elaboration))));
12591 Make_Raise_Program_Error (Loc,
12592 Reason => PE_Access_Before_Elaboration);
12594 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12595 Set_Analyzed (Ret_Expr);
12598 Make_Subprogram_Body (Loc,
12600 Make_Function_Specification (Loc,
12601 Defining_Unit_Name =>
12602 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12603 Parameter_Specifications =>
12605 (Parameter_Specifications (Parent (Act_Decl_Id))),
12606 Result_Definition =>
12607 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12609 Declarations => Empty_List,
12610 Handled_Statement_Sequence =>
12611 Make_Handled_Sequence_Of_Statements (Loc,
12612 Statements => New_List (
12613 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12617 Make_Package_Body (Loc,
12618 Defining_Unit_Name => New_Copy (Pack_Id),
12619 Declarations => New_List (Act_Body));
12621 Insert_After (Inst_Node, Pack_Body);
12622 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12623 Analyze (Pack_Body);
12628 -- Restore the context that was in effect prior to instantiating the
12629 -- subprogram body.
12631 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12632 Local_Suppress_Stack_Top := Saved_LSST;
12633 Scope_Suppress := Saved_SS;
12634 Style_Check := Saved_SC;
12636 Expander_Mode_Restore;
12637 Restore_Config_Switches (Saved_CS);
12638 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12639 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12640 Restore_Warnings (Saved_Warn);
12641 end Instantiate_Subprogram_Body;
12643 ----------------------
12644 -- Instantiate_Type --
12645 ----------------------
12647 function Instantiate_Type
12650 Analyzed_Formal : Node_Id;
12651 Actual_Decls : List_Id) return List_Id
12653 A_Gen_T : constant Entity_Id :=
12654 Defining_Identifier (Analyzed_Formal);
12655 Def : constant Node_Id := Formal_Type_Definition (Formal);
12656 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12658 Ancestor : Entity_Id := Empty;
12659 Decl_Node : Node_Id;
12660 Decl_Nodes : List_Id;
12664 procedure Check_Shared_Variable_Control_Aspects;
12665 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12666 -- that may be specified for a formal type are obeyed by the actual.
12668 procedure Diagnose_Predicated_Actual;
12669 -- There are a number of constructs in which a discrete type with
12670 -- predicates is illegal, e.g. as an index in an array type declaration.
12671 -- If a generic type is used is such a construct in a generic package
12672 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12673 -- of the generic contract that the actual cannot have predicates.
12675 procedure Validate_Array_Type_Instance;
12676 procedure Validate_Access_Subprogram_Instance;
12677 procedure Validate_Access_Type_Instance;
12678 procedure Validate_Derived_Type_Instance;
12679 procedure Validate_Derived_Interface_Type_Instance;
12680 procedure Validate_Discriminated_Formal_Type;
12681 procedure Validate_Interface_Type_Instance;
12682 procedure Validate_Private_Type_Instance;
12683 procedure Validate_Incomplete_Type_Instance;
12684 -- These procedures perform validation tests for the named case.
12685 -- Validate_Discriminated_Formal_Type is shared by formal private
12686 -- types and Ada 2012 formal incomplete types.
12688 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12689 -- Check that base types are the same and that the subtypes match
12690 -- statically. Used in several of the above.
12692 --------------------------------------------
12693 -- Check_Shared_Variable_Control_Aspects --
12694 --------------------------------------------
12696 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12697 -- that may be specified for the formal are obeyed by the actual.
12698 -- If the formal is a derived type the aspect specifications must match.
12699 -- NOTE: AI12-0282 implies that matching of aspects is required between
12700 -- formal and actual in all cases, but this is too restrictive.
12701 -- In particular it violates a language design rule: a limited private
12702 -- indefinite formal can be matched by any actual. The current code
12703 -- reflects an older and more permissive version of RM C.6 (12/5).
12705 procedure Check_Shared_Variable_Control_Aspects is
12707 if Ada_Version >= Ada_2020 then
12708 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12710 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12712 elsif Is_Derived_Type (A_Gen_T)
12713 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12716 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12719 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12721 ("actual for& must have Volatile aspect",
12724 elsif Is_Derived_Type (A_Gen_T)
12725 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12728 ("actual for& has different Volatile aspect",
12732 -- We assume that an array type whose atomic component type
12733 -- is Atomic is equivalent to an array type with the explicit
12734 -- aspect Has_Atomic_Components. This is a reasonable inference
12735 -- from the intent of AI12-0282, and makes it legal to use an
12736 -- actual that does not have the identical aspect as the formal.
12737 -- Ditto for volatile components.
12740 Actual_Atomic_Comp : constant Boolean :=
12741 Has_Atomic_Components (Act_T)
12742 or else (Is_Array_Type (Act_T)
12743 and then Is_Atomic (Component_Type (Act_T)));
12745 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12747 ("formal and actual for& must agree on atomic components",
12753 Actual_Volatile_Comp : constant Boolean :=
12754 Has_Volatile_Components (Act_T)
12755 or else (Is_Array_Type (Act_T)
12756 and then Is_Volatile (Component_Type (Act_T)));
12758 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12761 ("actual for& must have volatile components",
12766 -- The following two aspects do not require exact matching,
12767 -- but only one-way agreement. See RM C.6.
12769 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12772 ("actual for& must have Independent aspect specified",
12776 if Has_Independent_Components (A_Gen_T)
12777 and then not Has_Independent_Components (Act_T)
12780 ("actual for& must have Independent_Components specified",
12784 -- Check actual/formal compatibility with respect to the four
12785 -- volatility refinement aspects.
12787 Check_Volatility_Compatibility
12789 "actual type", "its corresponding formal type",
12790 Srcpos_Bearer => Act_T);
12792 end Check_Shared_Variable_Control_Aspects;
12794 ---------------------------------
12795 -- Diagnose_Predicated_Actual --
12796 ---------------------------------
12798 procedure Diagnose_Predicated_Actual is
12800 if No_Predicate_On_Actual (A_Gen_T)
12801 and then Has_Predicates (Act_T)
12804 ("actual for& cannot be a type with predicate",
12805 Instantiation_Node, A_Gen_T);
12807 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12808 and then Has_Predicates (Act_T)
12809 and then not Has_Static_Predicate_Aspect (Act_T)
12812 ("actual for& cannot be a type with a dynamic predicate",
12813 Instantiation_Node, A_Gen_T);
12815 end Diagnose_Predicated_Actual;
12817 --------------------
12818 -- Subtypes_Match --
12819 --------------------
12821 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12822 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12825 -- Some detailed comments would be useful here ???
12827 return ((Base_Type (T) = Act_T
12828 or else Base_Type (T) = Base_Type (Act_T))
12829 and then Subtypes_Statically_Match (T, Act_T))
12831 or else (Is_Class_Wide_Type (Gen_T)
12832 and then Is_Class_Wide_Type (Act_T)
12833 and then Subtypes_Match
12834 (Get_Instance_Of (Root_Type (Gen_T)),
12835 Root_Type (Act_T)))
12838 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12839 | E_Anonymous_Access_Type
12840 and then Ekind (Act_T) = Ekind (Gen_T)
12841 and then Subtypes_Statically_Match
12842 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12843 end Subtypes_Match;
12845 -----------------------------------------
12846 -- Validate_Access_Subprogram_Instance --
12847 -----------------------------------------
12849 procedure Validate_Access_Subprogram_Instance is
12851 if not Is_Access_Type (Act_T)
12852 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12855 ("expect access type in instantiation of &", Actual, Gen_T);
12856 Abandon_Instantiation (Actual);
12859 -- According to AI05-288, actuals for access_to_subprograms must be
12860 -- subtype conformant with the generic formal. Previous to AI05-288
12861 -- only mode conformance was required.
12863 -- This is a binding interpretation that applies to previous versions
12864 -- of the language, no need to maintain previous weaker checks.
12866 Check_Subtype_Conformant
12867 (Designated_Type (Act_T),
12868 Designated_Type (A_Gen_T),
12872 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12873 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12875 ("protected access type not allowed for formal &",
12879 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12881 ("expect protected access type for formal &",
12885 -- If the formal has a specified convention (which in most cases
12886 -- will be StdCall) verify that the actual has the same convention.
12888 if Has_Convention_Pragma (A_Gen_T)
12889 and then Convention (A_Gen_T) /= Convention (Act_T)
12891 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12893 ("actual for formal & must have convention %", Actual, Gen_T);
12896 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12898 ("non null exclusion of actual and formal & do not match",
12901 end Validate_Access_Subprogram_Instance;
12903 -----------------------------------
12904 -- Validate_Access_Type_Instance --
12905 -----------------------------------
12907 procedure Validate_Access_Type_Instance is
12908 Desig_Type : constant Entity_Id :=
12909 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12910 Desig_Act : Entity_Id;
12913 if not Is_Access_Type (Act_T) then
12915 ("expect access type in instantiation of &", Actual, Gen_T);
12916 Abandon_Instantiation (Actual);
12919 if Is_Access_Constant (A_Gen_T) then
12920 if not Is_Access_Constant (Act_T) then
12922 ("actual type must be access-to-constant type", Actual);
12923 Abandon_Instantiation (Actual);
12926 if Is_Access_Constant (Act_T) then
12928 ("actual type must be access-to-variable type", Actual);
12929 Abandon_Instantiation (Actual);
12931 elsif Ekind (A_Gen_T) = E_General_Access_Type
12932 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12934 Error_Msg_N -- CODEFIX
12935 ("actual must be general access type!", Actual);
12936 Error_Msg_NE -- CODEFIX
12937 ("add ALL to }!", Actual, Act_T);
12938 Abandon_Instantiation (Actual);
12942 -- The designated subtypes, that is to say the subtypes introduced
12943 -- by an access type declaration (and not by a subtype declaration)
12946 Desig_Act := Designated_Type (Base_Type (Act_T));
12948 -- The designated type may have been introduced through a limited_
12949 -- with clause, in which case retrieve the non-limited view. This
12950 -- applies to incomplete types as well as to class-wide types.
12952 if From_Limited_With (Desig_Act) then
12953 Desig_Act := Available_View (Desig_Act);
12956 if not Subtypes_Match (Desig_Type, Desig_Act) then
12958 ("designated type of actual does not match that of formal &",
12961 if not Predicates_Match (Desig_Type, Desig_Act) then
12962 Error_Msg_N ("\predicates do not match", Actual);
12965 Abandon_Instantiation (Actual);
12967 elsif Is_Access_Type (Designated_Type (Act_T))
12968 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12970 Is_Constrained (Designated_Type (Desig_Type))
12973 ("designated type of actual does not match that of formal &",
12976 if not Predicates_Match (Desig_Type, Desig_Act) then
12977 Error_Msg_N ("\predicates do not match", Actual);
12980 Abandon_Instantiation (Actual);
12983 -- Ada 2005: null-exclusion indicators of the two types must agree
12985 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12987 ("non null exclusion of actual and formal & do not match",
12990 end Validate_Access_Type_Instance;
12992 ----------------------------------
12993 -- Validate_Array_Type_Instance --
12994 ----------------------------------
12996 procedure Validate_Array_Type_Instance is
13001 function Formal_Dimensions return Nat;
13002 -- Count number of dimensions in array type formal
13004 -----------------------
13005 -- Formal_Dimensions --
13006 -----------------------
13008 function Formal_Dimensions return Nat is
13013 if Nkind (Def) = N_Constrained_Array_Definition then
13014 Index := First (Discrete_Subtype_Definitions (Def));
13016 Index := First (Subtype_Marks (Def));
13019 while Present (Index) loop
13021 Next_Index (Index);
13025 end Formal_Dimensions;
13027 -- Start of processing for Validate_Array_Type_Instance
13030 if not Is_Array_Type (Act_T) then
13032 ("expect array type in instantiation of &", Actual, Gen_T);
13033 Abandon_Instantiation (Actual);
13035 elsif Nkind (Def) = N_Constrained_Array_Definition then
13036 if not (Is_Constrained (Act_T)) then
13038 ("expect constrained array in instantiation of &",
13040 Abandon_Instantiation (Actual);
13044 if Is_Constrained (Act_T) then
13046 ("expect unconstrained array in instantiation of &",
13048 Abandon_Instantiation (Actual);
13052 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13054 ("dimensions of actual do not match formal &", Actual, Gen_T);
13055 Abandon_Instantiation (Actual);
13058 I1 := First_Index (A_Gen_T);
13059 I2 := First_Index (Act_T);
13060 for J in 1 .. Formal_Dimensions loop
13062 -- If the indexes of the actual were given by a subtype_mark,
13063 -- the index was transformed into a range attribute. Retrieve
13064 -- the original type mark for checking.
13066 if Is_Entity_Name (Original_Node (I2)) then
13067 T2 := Entity (Original_Node (I2));
13072 if not Subtypes_Match
13073 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13076 ("index types of actual do not match those of formal &",
13078 Abandon_Instantiation (Actual);
13085 -- Check matching subtypes. Note that there are complex visibility
13086 -- issues when the generic is a child unit and some aspect of the
13087 -- generic type is declared in a parent unit of the generic. We do
13088 -- the test to handle this special case only after a direct check
13089 -- for static matching has failed. The case where both the component
13090 -- type and the array type are separate formals, and the component
13091 -- type is a private view may also require special checking in
13092 -- Subtypes_Match. Finally, we assume that a child instance where
13093 -- the component type comes from a formal of a parent instance is
13094 -- correct because the generic was correct. A more precise check
13095 -- seems too complex to install???
13098 (Component_Type (A_Gen_T), Component_Type (Act_T))
13101 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13102 Component_Type (Act_T))
13104 (not Inside_A_Generic
13105 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13110 ("component subtype of actual does not match that of formal &",
13112 Abandon_Instantiation (Actual);
13115 if Has_Aliased_Components (A_Gen_T)
13116 and then not Has_Aliased_Components (Act_T)
13119 ("actual must have aliased components to match formal type &",
13122 end Validate_Array_Type_Instance;
13124 -----------------------------------------------
13125 -- Validate_Derived_Interface_Type_Instance --
13126 -----------------------------------------------
13128 procedure Validate_Derived_Interface_Type_Instance is
13129 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13133 -- First apply interface instance checks
13135 Validate_Interface_Type_Instance;
13137 -- Verify that immediate parent interface is an ancestor of
13141 and then not Interface_Present_In_Ancestor (Act_T, Par)
13144 ("interface actual must include progenitor&", Actual, Par);
13147 -- Now verify that the actual includes all other ancestors of
13150 Elmt := First_Elmt (Interfaces (A_Gen_T));
13151 while Present (Elmt) loop
13152 if not Interface_Present_In_Ancestor
13153 (Act_T, Get_Instance_Of (Node (Elmt)))
13156 ("interface actual must include progenitor&",
13157 Actual, Node (Elmt));
13162 end Validate_Derived_Interface_Type_Instance;
13164 ------------------------------------
13165 -- Validate_Derived_Type_Instance --
13166 ------------------------------------
13168 procedure Validate_Derived_Type_Instance is
13169 Actual_Discr : Entity_Id;
13170 Ancestor_Discr : Entity_Id;
13173 -- Verify that the actual includes the progenitors of the formal,
13174 -- if any. The formal may depend on previous formals and their
13175 -- instance, so we must examine instance of interfaces if present.
13176 -- The actual may be an extension of an interface, in which case
13177 -- it does not appear in the interface list, so this must be
13178 -- checked separately.
13180 if Present (Interface_List (Def)) then
13181 if not Has_Interfaces (Act_T) then
13183 ("actual must implement all interfaces of formal&",
13188 Act_Iface_List : Elist_Id;
13190 Iface_Ent : Entity_Id;
13192 function Instance_Exists (I : Entity_Id) return Boolean;
13193 -- If the interface entity is declared in a generic unit,
13194 -- this can only be legal if we are within an instantiation
13195 -- of a child of that generic. There is currently no
13196 -- mechanism to relate an interface declared within a
13197 -- generic to the corresponding interface in an instance,
13198 -- so we traverse the list of interfaces of the actual,
13199 -- looking for a name match.
13201 ---------------------
13202 -- Instance_Exists --
13203 ---------------------
13205 function Instance_Exists (I : Entity_Id) return Boolean is
13206 Iface_Elmt : Elmt_Id;
13209 Iface_Elmt := First_Elmt (Act_Iface_List);
13210 while Present (Iface_Elmt) loop
13211 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13212 and then Chars (Node (Iface_Elmt)) = Chars (I)
13217 Next_Elmt (Iface_Elmt);
13221 end Instance_Exists;
13224 Iface := First (Abstract_Interface_List (A_Gen_T));
13225 Collect_Interfaces (Act_T, Act_Iface_List);
13227 while Present (Iface) loop
13228 Iface_Ent := Get_Instance_Of (Entity (Iface));
13230 if Is_Ancestor (Iface_Ent, Act_T)
13231 or else Is_Progenitor (Iface_Ent, Act_T)
13235 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13236 and then Instance_Exists (Iface_Ent)
13241 Error_Msg_Name_1 := Chars (Act_T);
13243 ("Actual% must implement interface&",
13244 Actual, Etype (Iface));
13253 -- If the parent type in the generic declaration is itself a previous
13254 -- formal type, then it is local to the generic and absent from the
13255 -- analyzed generic definition. In that case the ancestor is the
13256 -- instance of the formal (which must have been instantiated
13257 -- previously), unless the ancestor is itself a formal derived type.
13258 -- In this latter case (which is the subject of Corrigendum 8652/0038
13259 -- (AI-202) the ancestor of the formals is the ancestor of its
13260 -- parent. Otherwise, the analyzed generic carries the parent type.
13261 -- If the parent type is defined in a previous formal package, then
13262 -- the scope of that formal package is that of the generic type
13263 -- itself, and it has already been mapped into the corresponding type
13264 -- in the actual package.
13266 -- Common case: parent type defined outside of the generic
13268 if Is_Entity_Name (Subtype_Mark (Def))
13269 and then Present (Entity (Subtype_Mark (Def)))
13271 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13273 -- Check whether parent is defined in a previous formal package
13276 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13279 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13281 -- The type may be a local derivation, or a type extension of a
13282 -- previous formal, or of a formal of a parent package.
13284 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13286 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13288 -- Check whether the parent is another derived formal type in the
13289 -- same generic unit.
13291 if Etype (A_Gen_T) /= A_Gen_T
13292 and then Is_Generic_Type (Etype (A_Gen_T))
13293 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13294 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13296 -- Locate ancestor of parent from the subtype declaration
13297 -- created for the actual.
13303 Decl := First (Actual_Decls);
13304 while Present (Decl) loop
13305 if Nkind (Decl) = N_Subtype_Declaration
13306 and then Chars (Defining_Identifier (Decl)) =
13307 Chars (Etype (A_Gen_T))
13309 Ancestor := Generic_Parent_Type (Decl);
13317 pragma Assert (Present (Ancestor));
13319 -- The ancestor itself may be a previous formal that has been
13322 Ancestor := Get_Instance_Of (Ancestor);
13326 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13329 -- Check whether parent is a previous formal of the current generic
13331 elsif Is_Derived_Type (A_Gen_T)
13332 and then Is_Generic_Type (Etype (A_Gen_T))
13333 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13335 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13337 -- An unusual case: the actual is a type declared in a parent unit,
13338 -- but is not a formal type so there is no instance_of for it.
13339 -- Retrieve it by analyzing the record extension.
13341 elsif Is_Child_Unit (Scope (A_Gen_T))
13342 and then In_Open_Scopes (Scope (Act_T))
13343 and then Is_Generic_Instance (Scope (Act_T))
13345 Analyze (Subtype_Mark (Def));
13346 Ancestor := Entity (Subtype_Mark (Def));
13349 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13352 -- If the formal derived type has pragma Preelaborable_Initialization
13353 -- then the actual type must have preelaborable initialization.
13355 if Known_To_Have_Preelab_Init (A_Gen_T)
13356 and then not Has_Preelaborable_Initialization (Act_T)
13359 ("actual for & must have preelaborable initialization",
13363 -- Ada 2005 (AI-251)
13365 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13366 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13368 ("(Ada 2005) expected type implementing & in instantiation",
13372 -- Finally verify that the (instance of) the ancestor is an ancestor
13375 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13377 ("expect type derived from & in instantiation",
13378 Actual, First_Subtype (Ancestor));
13379 Abandon_Instantiation (Actual);
13382 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13383 -- that the formal type declaration has been rewritten as a private
13386 if Ada_Version >= Ada_2005
13387 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13388 and then Synchronized_Present (Parent (A_Gen_T))
13390 -- The actual must be a synchronized tagged type
13392 if not Is_Tagged_Type (Act_T) then
13394 ("actual of synchronized type must be tagged", Actual);
13395 Abandon_Instantiation (Actual);
13397 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13398 and then Nkind (Type_Definition (Parent (Act_T))) =
13399 N_Derived_Type_Definition
13400 and then not Synchronized_Present
13401 (Type_Definition (Parent (Act_T)))
13404 ("actual of synchronized type must be synchronized", Actual);
13405 Abandon_Instantiation (Actual);
13409 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13410 -- removes the second instance of the phrase "or allow pass by copy".
13412 -- For Ada 2020, the aspect may be specified explicitly for the
13413 -- formal regardless of whether an ancestor obeys it.
13415 if Is_Atomic (Act_T)
13416 and then not Is_Atomic (Ancestor)
13417 and then not Is_Atomic (A_Gen_T)
13420 ("cannot have atomic actual type for non-atomic formal type",
13423 elsif Is_Volatile (Act_T)
13424 and then not Is_Volatile (Ancestor)
13425 and then not Is_Volatile (A_Gen_T)
13428 ("cannot have volatile actual type for non-volatile formal type",
13432 -- It should not be necessary to check for unknown discriminants on
13433 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13434 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13435 -- needs fixing. ???
13437 if Is_Definite_Subtype (A_Gen_T)
13438 and then not Unknown_Discriminants_Present (Formal)
13439 and then not Is_Definite_Subtype (Act_T)
13441 Error_Msg_N ("actual subtype must be constrained", Actual);
13442 Abandon_Instantiation (Actual);
13445 if not Unknown_Discriminants_Present (Formal) then
13446 if Is_Constrained (Ancestor) then
13447 if not Is_Constrained (Act_T) then
13448 Error_Msg_N ("actual subtype must be constrained", Actual);
13449 Abandon_Instantiation (Actual);
13452 -- Ancestor is unconstrained, Check if generic formal and actual
13453 -- agree on constrainedness. The check only applies to array types
13454 -- and discriminated types.
13456 elsif Is_Constrained (Act_T) then
13457 if Ekind (Ancestor) = E_Access_Type
13458 or else (not Is_Constrained (A_Gen_T)
13459 and then Is_Composite_Type (A_Gen_T))
13461 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13462 Abandon_Instantiation (Actual);
13465 -- A class-wide type is only allowed if the formal has unknown
13468 elsif Is_Class_Wide_Type (Act_T)
13469 and then not Has_Unknown_Discriminants (Ancestor)
13472 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13473 Abandon_Instantiation (Actual);
13475 -- Otherwise, the formal and actual must have the same number
13476 -- of discriminants and each discriminant of the actual must
13477 -- correspond to a discriminant of the formal.
13479 elsif Has_Discriminants (Act_T)
13480 and then not Has_Unknown_Discriminants (Act_T)
13481 and then Has_Discriminants (Ancestor)
13483 Actual_Discr := First_Discriminant (Act_T);
13484 Ancestor_Discr := First_Discriminant (Ancestor);
13485 while Present (Actual_Discr)
13486 and then Present (Ancestor_Discr)
13488 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13489 No (Corresponding_Discriminant (Actual_Discr))
13492 ("discriminant & does not correspond "
13493 & "to ancestor discriminant", Actual, Actual_Discr);
13494 Abandon_Instantiation (Actual);
13497 Next_Discriminant (Actual_Discr);
13498 Next_Discriminant (Ancestor_Discr);
13501 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13503 ("actual for & must have same number of discriminants",
13505 Abandon_Instantiation (Actual);
13508 -- This case should be caught by the earlier check for
13509 -- constrainedness, but the check here is added for completeness.
13511 elsif Has_Discriminants (Act_T)
13512 and then not Has_Unknown_Discriminants (Act_T)
13515 ("actual for & must not have discriminants", Actual, Gen_T);
13516 Abandon_Instantiation (Actual);
13518 elsif Has_Discriminants (Ancestor) then
13520 ("actual for & must have known discriminants", Actual, Gen_T);
13521 Abandon_Instantiation (Actual);
13524 if not Subtypes_Statically_Compatible
13525 (Act_T, Ancestor, Formal_Derived_Matching => True)
13528 ("actual for & must be statically compatible with ancestor",
13531 if not Predicates_Compatible (Act_T, Ancestor) then
13533 ("\predicate on actual is not compatible with ancestor",
13537 Abandon_Instantiation (Actual);
13541 -- If the formal and actual types are abstract, check that there
13542 -- are no abstract primitives of the actual type that correspond to
13543 -- nonabstract primitives of the formal type (second sentence of
13546 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13547 Check_Abstract_Primitives : declare
13548 Gen_Prims : constant Elist_Id :=
13549 Primitive_Operations (A_Gen_T);
13550 Gen_Elmt : Elmt_Id;
13551 Gen_Subp : Entity_Id;
13552 Anc_Subp : Entity_Id;
13553 Anc_Formal : Entity_Id;
13554 Anc_F_Type : Entity_Id;
13556 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13557 Act_Elmt : Elmt_Id;
13558 Act_Subp : Entity_Id;
13559 Act_Formal : Entity_Id;
13560 Act_F_Type : Entity_Id;
13562 Subprograms_Correspond : Boolean;
13564 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13565 -- Returns true if T2 is derived directly or indirectly from
13566 -- T1, including derivations from interfaces. T1 and T2 are
13567 -- required to be specific tagged base types.
13569 ------------------------
13570 -- Is_Tagged_Ancestor --
13571 ------------------------
13573 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13575 Intfc_Elmt : Elmt_Id;
13578 -- The predicate is satisfied if the types are the same
13583 -- If we've reached the top of the derivation chain then
13584 -- we know that T1 is not an ancestor of T2.
13586 elsif Etype (T2) = T2 then
13589 -- Proceed to check T2's immediate parent
13591 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13594 -- Finally, check to see if T1 is an ancestor of any of T2's
13598 Intfc_Elmt := First_Elmt (Interfaces (T2));
13599 while Present (Intfc_Elmt) loop
13600 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13604 Next_Elmt (Intfc_Elmt);
13609 end Is_Tagged_Ancestor;
13611 -- Start of processing for Check_Abstract_Primitives
13614 -- Loop over all of the formal derived type's primitives
13616 Gen_Elmt := First_Elmt (Gen_Prims);
13617 while Present (Gen_Elmt) loop
13618 Gen_Subp := Node (Gen_Elmt);
13620 -- If the primitive of the formal is not abstract, then
13621 -- determine whether there is a corresponding primitive of
13622 -- the actual type that's abstract.
13624 if not Is_Abstract_Subprogram (Gen_Subp) then
13625 Act_Elmt := First_Elmt (Act_Prims);
13626 while Present (Act_Elmt) loop
13627 Act_Subp := Node (Act_Elmt);
13629 -- If we find an abstract primitive of the actual,
13630 -- then we need to test whether it corresponds to the
13631 -- subprogram from which the generic formal primitive
13634 if Is_Abstract_Subprogram (Act_Subp) then
13635 Anc_Subp := Alias (Gen_Subp);
13637 -- Test whether we have a corresponding primitive
13638 -- by comparing names, kinds, formal types, and
13641 if Chars (Anc_Subp) = Chars (Act_Subp)
13642 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13644 Anc_Formal := First_Formal (Anc_Subp);
13645 Act_Formal := First_Formal (Act_Subp);
13646 while Present (Anc_Formal)
13647 and then Present (Act_Formal)
13649 Anc_F_Type := Etype (Anc_Formal);
13650 Act_F_Type := Etype (Act_Formal);
13652 if Ekind (Anc_F_Type) =
13653 E_Anonymous_Access_Type
13655 Anc_F_Type := Designated_Type (Anc_F_Type);
13657 if Ekind (Act_F_Type) =
13658 E_Anonymous_Access_Type
13661 Designated_Type (Act_F_Type);
13667 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13672 Anc_F_Type := Base_Type (Anc_F_Type);
13673 Act_F_Type := Base_Type (Act_F_Type);
13675 -- If the formal is controlling, then the
13676 -- the type of the actual primitive's formal
13677 -- must be derived directly or indirectly
13678 -- from the type of the ancestor primitive's
13681 if Is_Controlling_Formal (Anc_Formal) then
13682 if not Is_Tagged_Ancestor
13683 (Anc_F_Type, Act_F_Type)
13688 -- Otherwise the types of the formals must
13691 elsif Anc_F_Type /= Act_F_Type then
13695 Next_Entity (Anc_Formal);
13696 Next_Entity (Act_Formal);
13699 -- If we traversed through all of the formals
13700 -- then so far the subprograms correspond, so
13701 -- now check that any result types correspond.
13703 if No (Anc_Formal) and then No (Act_Formal) then
13704 Subprograms_Correspond := True;
13706 if Ekind (Act_Subp) = E_Function then
13707 Anc_F_Type := Etype (Anc_Subp);
13708 Act_F_Type := Etype (Act_Subp);
13710 if Ekind (Anc_F_Type) =
13711 E_Anonymous_Access_Type
13714 Designated_Type (Anc_F_Type);
13716 if Ekind (Act_F_Type) =
13717 E_Anonymous_Access_Type
13720 Designated_Type (Act_F_Type);
13722 Subprograms_Correspond := False;
13727 = E_Anonymous_Access_Type
13729 Subprograms_Correspond := False;
13732 Anc_F_Type := Base_Type (Anc_F_Type);
13733 Act_F_Type := Base_Type (Act_F_Type);
13735 -- Now either the result types must be
13736 -- the same or, if the result type is
13737 -- controlling, the result type of the
13738 -- actual primitive must descend from the
13739 -- result type of the ancestor primitive.
13741 if Subprograms_Correspond
13742 and then Anc_F_Type /= Act_F_Type
13744 Has_Controlling_Result (Anc_Subp)
13745 and then not Is_Tagged_Ancestor
13746 (Anc_F_Type, Act_F_Type)
13748 Subprograms_Correspond := False;
13752 -- Found a matching subprogram belonging to
13753 -- formal ancestor type, so actual subprogram
13754 -- corresponds and this violates 3.9.3(9).
13756 if Subprograms_Correspond then
13758 ("abstract subprogram & overrides "
13759 & "nonabstract subprogram of ancestor",
13766 Next_Elmt (Act_Elmt);
13770 Next_Elmt (Gen_Elmt);
13772 end Check_Abstract_Primitives;
13775 -- Verify that limitedness matches. If parent is a limited
13776 -- interface then the generic formal is not unless declared
13777 -- explicitly so. If not declared limited, the actual cannot be
13778 -- limited (see AI05-0087).
13780 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13781 if not In_Instance then
13783 ("actual for non-limited & cannot be a limited type",
13785 Explain_Limited_Type (Act_T, Actual);
13786 Abandon_Instantiation (Actual);
13790 -- Check for AI12-0036
13793 Formal_Is_Private_Extension : constant Boolean :=
13794 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13796 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13799 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13800 if not In_Instance then
13801 if Actual_Is_Tagged then
13803 ("actual for & cannot be a tagged type", Actual, Gen_T);
13806 ("actual for & must be a tagged type", Actual, Gen_T);
13809 Abandon_Instantiation (Actual);
13813 end Validate_Derived_Type_Instance;
13815 ----------------------------------------
13816 -- Validate_Discriminated_Formal_Type --
13817 ----------------------------------------
13819 procedure Validate_Discriminated_Formal_Type is
13820 Formal_Discr : Entity_Id;
13821 Actual_Discr : Entity_Id;
13822 Formal_Subt : Entity_Id;
13825 if Has_Discriminants (A_Gen_T) then
13826 if not Has_Discriminants (Act_T) then
13828 ("actual for & must have discriminants", Actual, Gen_T);
13829 Abandon_Instantiation (Actual);
13831 elsif Is_Constrained (Act_T) then
13833 ("actual for & must be unconstrained", Actual, Gen_T);
13834 Abandon_Instantiation (Actual);
13837 Formal_Discr := First_Discriminant (A_Gen_T);
13838 Actual_Discr := First_Discriminant (Act_T);
13839 while Formal_Discr /= Empty loop
13840 if Actual_Discr = Empty then
13842 ("discriminants on actual do not match formal",
13844 Abandon_Instantiation (Actual);
13847 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13849 -- Access discriminants match if designated types do
13851 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13852 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13853 E_Anonymous_Access_Type
13856 (Designated_Type (Base_Type (Formal_Subt))) =
13857 Designated_Type (Base_Type (Etype (Actual_Discr)))
13861 elsif Base_Type (Formal_Subt) /=
13862 Base_Type (Etype (Actual_Discr))
13865 ("types of actual discriminants must match formal",
13867 Abandon_Instantiation (Actual);
13869 elsif not Subtypes_Statically_Match
13870 (Formal_Subt, Etype (Actual_Discr))
13871 and then Ada_Version >= Ada_95
13874 ("subtypes of actual discriminants must match formal",
13876 Abandon_Instantiation (Actual);
13879 Next_Discriminant (Formal_Discr);
13880 Next_Discriminant (Actual_Discr);
13883 if Actual_Discr /= Empty then
13885 ("discriminants on actual do not match formal",
13887 Abandon_Instantiation (Actual);
13891 end Validate_Discriminated_Formal_Type;
13893 ---------------------------------------
13894 -- Validate_Incomplete_Type_Instance --
13895 ---------------------------------------
13897 procedure Validate_Incomplete_Type_Instance is
13899 if not Is_Tagged_Type (Act_T)
13900 and then Is_Tagged_Type (A_Gen_T)
13903 ("actual for & must be a tagged type", Actual, Gen_T);
13906 Validate_Discriminated_Formal_Type;
13907 end Validate_Incomplete_Type_Instance;
13909 --------------------------------------
13910 -- Validate_Interface_Type_Instance --
13911 --------------------------------------
13913 procedure Validate_Interface_Type_Instance is
13915 if not Is_Interface (Act_T) then
13917 ("actual for formal interface type must be an interface",
13920 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13921 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13922 or else Is_Protected_Interface (A_Gen_T) /=
13923 Is_Protected_Interface (Act_T)
13924 or else Is_Synchronized_Interface (A_Gen_T) /=
13925 Is_Synchronized_Interface (Act_T)
13928 ("actual for interface& does not match (RM 12.5.5(4))",
13931 end Validate_Interface_Type_Instance;
13933 ------------------------------------
13934 -- Validate_Private_Type_Instance --
13935 ------------------------------------
13937 procedure Validate_Private_Type_Instance is
13939 if Is_Limited_Type (Act_T)
13940 and then not Is_Limited_Type (A_Gen_T)
13942 if In_Instance then
13946 ("actual for non-limited & cannot be a limited type", Actual,
13948 Explain_Limited_Type (Act_T, Actual);
13949 Abandon_Instantiation (Actual);
13952 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13953 and then not Has_Preelaborable_Initialization (Act_T)
13956 ("actual for & must have preelaborable initialization", Actual,
13959 elsif not Is_Definite_Subtype (Act_T)
13960 and then Is_Definite_Subtype (A_Gen_T)
13961 and then Ada_Version >= Ada_95
13964 ("actual for & must be a definite subtype", Actual, Gen_T);
13966 elsif not Is_Tagged_Type (Act_T)
13967 and then Is_Tagged_Type (A_Gen_T)
13970 ("actual for & must be a tagged type", Actual, Gen_T);
13973 Validate_Discriminated_Formal_Type;
13975 end Validate_Private_Type_Instance;
13977 -- Start of processing for Instantiate_Type
13980 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13981 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13982 return New_List (Error);
13984 elsif not Is_Entity_Name (Actual)
13985 or else not Is_Type (Entity (Actual))
13988 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13989 Abandon_Instantiation (Actual);
13992 Act_T := Entity (Actual);
13994 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13995 -- as a generic actual parameter if the corresponding formal type
13996 -- does not have a known_discriminant_part, or is a formal derived
13997 -- type that is an Unchecked_Union type.
13999 if Is_Unchecked_Union (Base_Type (Act_T)) then
14000 if not Has_Discriminants (A_Gen_T)
14001 or else (Is_Derived_Type (A_Gen_T)
14002 and then Is_Unchecked_Union (A_Gen_T))
14006 Error_Msg_N ("unchecked union cannot be the actual for a "
14007 & "discriminated formal type", Act_T);
14012 -- Deal with fixed/floating restrictions
14014 if Is_Floating_Point_Type (Act_T) then
14015 Check_Restriction (No_Floating_Point, Actual);
14016 elsif Is_Fixed_Point_Type (Act_T) then
14017 Check_Restriction (No_Fixed_Point, Actual);
14020 -- Deal with error of using incomplete type as generic actual.
14021 -- This includes limited views of a type, even if the non-limited
14022 -- view may be available.
14024 if Ekind (Act_T) = E_Incomplete_Type
14025 or else (Is_Class_Wide_Type (Act_T)
14026 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14028 -- If the formal is an incomplete type, the actual can be
14029 -- incomplete as well.
14031 if Ekind (A_Gen_T) = E_Incomplete_Type then
14034 elsif Is_Class_Wide_Type (Act_T)
14035 or else No (Full_View (Act_T))
14037 Error_Msg_N ("premature use of incomplete type", Actual);
14038 Abandon_Instantiation (Actual);
14040 Act_T := Full_View (Act_T);
14041 Set_Entity (Actual, Act_T);
14043 if Has_Private_Component (Act_T) then
14045 ("premature use of type with private component", Actual);
14049 -- Deal with error of premature use of private type as generic actual
14051 elsif Is_Private_Type (Act_T)
14052 and then Is_Private_Type (Base_Type (Act_T))
14053 and then not Is_Generic_Type (Act_T)
14054 and then not Is_Derived_Type (Act_T)
14055 and then No (Full_View (Root_Type (Act_T)))
14057 -- If the formal is an incomplete type, the actual can be
14058 -- private or incomplete as well.
14060 if Ekind (A_Gen_T) = E_Incomplete_Type then
14063 Error_Msg_N ("premature use of private type", Actual);
14066 elsif Has_Private_Component (Act_T) then
14068 ("premature use of type with private component", Actual);
14071 Set_Instance_Of (A_Gen_T, Act_T);
14073 -- If the type is generic, the class-wide type may also be used
14075 if Is_Tagged_Type (A_Gen_T)
14076 and then Is_Tagged_Type (Act_T)
14077 and then not Is_Class_Wide_Type (A_Gen_T)
14079 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14080 Class_Wide_Type (Act_T));
14083 if not Is_Abstract_Type (A_Gen_T)
14084 and then Is_Abstract_Type (Act_T)
14087 ("actual of non-abstract formal cannot be abstract", Actual);
14090 -- A generic scalar type is a first subtype for which we generate
14091 -- an anonymous base type. Indicate that the instance of this base
14092 -- is the base type of the actual.
14094 if Is_Scalar_Type (A_Gen_T) then
14095 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14099 Check_Shared_Variable_Control_Aspects;
14101 if Error_Posted (Act_T) then
14104 case Nkind (Def) is
14105 when N_Formal_Private_Type_Definition =>
14106 Validate_Private_Type_Instance;
14108 when N_Formal_Incomplete_Type_Definition =>
14109 Validate_Incomplete_Type_Instance;
14111 when N_Formal_Derived_Type_Definition =>
14112 Validate_Derived_Type_Instance;
14114 when N_Formal_Discrete_Type_Definition =>
14115 if not Is_Discrete_Type (Act_T) then
14117 ("expect discrete type in instantiation of&",
14119 Abandon_Instantiation (Actual);
14122 Diagnose_Predicated_Actual;
14124 when N_Formal_Signed_Integer_Type_Definition =>
14125 if not Is_Signed_Integer_Type (Act_T) then
14127 ("expect signed integer type in instantiation of&",
14129 Abandon_Instantiation (Actual);
14132 Diagnose_Predicated_Actual;
14134 when N_Formal_Modular_Type_Definition =>
14135 if not Is_Modular_Integer_Type (Act_T) then
14137 ("expect modular type in instantiation of &",
14139 Abandon_Instantiation (Actual);
14142 Diagnose_Predicated_Actual;
14144 when N_Formal_Floating_Point_Definition =>
14145 if not Is_Floating_Point_Type (Act_T) then
14147 ("expect float type in instantiation of &", Actual, Gen_T);
14148 Abandon_Instantiation (Actual);
14151 when N_Formal_Ordinary_Fixed_Point_Definition =>
14152 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14154 ("expect ordinary fixed point type in instantiation of &",
14156 Abandon_Instantiation (Actual);
14159 when N_Formal_Decimal_Fixed_Point_Definition =>
14160 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14162 ("expect decimal type in instantiation of &",
14164 Abandon_Instantiation (Actual);
14167 when N_Array_Type_Definition =>
14168 Validate_Array_Type_Instance;
14170 when N_Access_To_Object_Definition =>
14171 Validate_Access_Type_Instance;
14173 when N_Access_Function_Definition
14174 | N_Access_Procedure_Definition
14176 Validate_Access_Subprogram_Instance;
14178 when N_Record_Definition =>
14179 Validate_Interface_Type_Instance;
14181 when N_Derived_Type_Definition =>
14182 Validate_Derived_Interface_Type_Instance;
14185 raise Program_Error;
14189 Subt := New_Copy (Gen_T);
14191 -- Use adjusted sloc of subtype name as the location for other nodes in
14192 -- the subtype declaration.
14194 Loc := Sloc (Subt);
14197 Make_Subtype_Declaration (Loc,
14198 Defining_Identifier => Subt,
14199 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14201 -- Record whether the actual is private at this point, so that
14202 -- Check_Generic_Actuals can restore its proper view before the
14203 -- semantic analysis of the instance.
14205 if Is_Private_Type (Act_T) then
14206 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14209 -- In Ada 2012 the actual may be a limited view. Indicate that
14210 -- the local subtype must be treated as such.
14212 if From_Limited_With (Act_T) then
14213 Set_Ekind (Subt, E_Incomplete_Subtype);
14214 Set_From_Limited_With (Subt);
14217 Decl_Nodes := New_List (Decl_Node);
14219 -- Flag actual derived types so their elaboration produces the
14220 -- appropriate renamings for the primitive operations of the ancestor.
14221 -- Flag actual for formal private types as well, to determine whether
14222 -- operations in the private part may override inherited operations.
14223 -- If the formal has an interface list, the ancestor is not the
14224 -- parent, but the analyzed formal that includes the interface
14225 -- operations of all its progenitors.
14227 -- Same treatment for formal private types, so we can check whether the
14228 -- type is tagged limited when validating derivations in the private
14229 -- part. (See AI05-096).
14231 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14232 if Present (Interface_List (Def)) then
14233 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14235 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14238 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14239 | N_Formal_Incomplete_Type_Definition
14241 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14244 -- If the actual is a synchronized type that implements an interface,
14245 -- the primitive operations are attached to the corresponding record,
14246 -- and we have to treat it as an additional generic actual, so that its
14247 -- primitive operations become visible in the instance. The task or
14248 -- protected type itself does not carry primitive operations.
14250 if Is_Concurrent_Type (Act_T)
14251 and then Is_Tagged_Type (Act_T)
14252 and then Present (Corresponding_Record_Type (Act_T))
14253 and then Present (Ancestor)
14254 and then Is_Interface (Ancestor)
14257 Corr_Rec : constant Entity_Id :=
14258 Corresponding_Record_Type (Act_T);
14259 New_Corr : Entity_Id;
14260 Corr_Decl : Node_Id;
14263 New_Corr := Make_Temporary (Loc, 'S');
14265 Make_Subtype_Declaration (Loc,
14266 Defining_Identifier => New_Corr,
14267 Subtype_Indication =>
14268 New_Occurrence_Of (Corr_Rec, Loc));
14269 Append_To (Decl_Nodes, Corr_Decl);
14271 if Ekind (Act_T) = E_Task_Type then
14272 Set_Ekind (Subt, E_Task_Subtype);
14274 Set_Ekind (Subt, E_Protected_Subtype);
14277 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14278 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14279 Set_Generic_Parent_Type (Decl_Node, Empty);
14283 -- For a floating-point type, capture dimension info if any, because
14284 -- the generated subtype declaration does not come from source and
14285 -- will not process dimensions.
14287 if Is_Floating_Point_Type (Act_T) then
14288 Copy_Dimensions (Act_T, Subt);
14292 end Instantiate_Type;
14294 ---------------------
14295 -- Is_In_Main_Unit --
14296 ---------------------
14298 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14299 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14300 Current_Unit : Node_Id;
14303 if Unum = Main_Unit then
14306 -- If the current unit is a subunit then it is either the main unit or
14307 -- is being compiled as part of the main unit.
14309 elsif Nkind (N) = N_Compilation_Unit then
14310 return Nkind (Unit (N)) = N_Subunit;
14313 Current_Unit := Parent (N);
14314 while Present (Current_Unit)
14315 and then Nkind (Current_Unit) /= N_Compilation_Unit
14317 Current_Unit := Parent (Current_Unit);
14320 -- The instantiation node is in the main unit, or else the current node
14321 -- (perhaps as the result of nested instantiations) is in the main unit,
14322 -- or in the declaration of the main unit, which in this last case must
14326 Current_Unit = Cunit (Main_Unit)
14327 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14328 or else (Present (Current_Unit)
14329 and then Present (Library_Unit (Current_Unit))
14330 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14331 end Is_In_Main_Unit;
14333 ----------------------------
14334 -- Load_Parent_Of_Generic --
14335 ----------------------------
14337 procedure Load_Parent_Of_Generic
14340 Body_Optional : Boolean := False)
14342 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14343 Saved_Style_Check : constant Boolean := Style_Check;
14344 Saved_Warnings : constant Warning_Record := Save_Warnings;
14345 True_Parent : Node_Id;
14346 Inst_Node : Node_Id;
14348 Previous_Instances : constant Elist_Id := New_Elmt_List;
14350 procedure Collect_Previous_Instances (Decls : List_Id);
14351 -- Collect all instantiations in the given list of declarations, that
14352 -- precede the generic that we need to load. If the bodies of these
14353 -- instantiations are available, we must analyze them, to ensure that
14354 -- the public symbols generated are the same when the unit is compiled
14355 -- to generate code, and when it is compiled in the context of a unit
14356 -- that needs a particular nested instance. This process is applied to
14357 -- both package and subprogram instances.
14359 --------------------------------
14360 -- Collect_Previous_Instances --
14361 --------------------------------
14363 procedure Collect_Previous_Instances (Decls : List_Id) is
14367 Decl := First (Decls);
14368 while Present (Decl) loop
14369 if Sloc (Decl) >= Sloc (Inst_Node) then
14372 -- If Decl is an instantiation, then record it as requiring
14373 -- instantiation of the corresponding body, except if it is an
14374 -- abbreviated instantiation generated internally for conformance
14375 -- checking purposes only for the case of a formal package
14376 -- declared without a box (see Instantiate_Formal_Package). Such
14377 -- an instantiation does not generate any code (the actual code
14378 -- comes from actual) and thus does not need to be analyzed here.
14379 -- If the instantiation appears with a generic package body it is
14380 -- not analyzed here either.
14382 elsif Nkind (Decl) = N_Package_Instantiation
14383 and then not Is_Internal (Defining_Entity (Decl))
14385 Append_Elmt (Decl, Previous_Instances);
14387 -- For a subprogram instantiation, omit instantiations intrinsic
14388 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14390 elsif Nkind (Decl) in N_Function_Instantiation
14391 | N_Procedure_Instantiation
14392 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14394 Append_Elmt (Decl, Previous_Instances);
14396 elsif Nkind (Decl) = N_Package_Declaration then
14397 Collect_Previous_Instances
14398 (Visible_Declarations (Specification (Decl)));
14399 Collect_Previous_Instances
14400 (Private_Declarations (Specification (Decl)));
14402 -- Previous non-generic bodies may contain instances as well
14404 elsif Nkind (Decl) = N_Package_Body
14405 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14407 Collect_Previous_Instances (Declarations (Decl));
14409 elsif Nkind (Decl) = N_Subprogram_Body
14410 and then not Acts_As_Spec (Decl)
14411 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14413 Collect_Previous_Instances (Declarations (Decl));
14418 end Collect_Previous_Instances;
14420 -- Start of processing for Load_Parent_Of_Generic
14423 if not In_Same_Source_Unit (N, Spec)
14424 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14425 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14426 and then not Is_In_Main_Unit (Spec))
14428 -- Find body of parent of spec, and analyze it. A special case arises
14429 -- when the parent is an instantiation, that is to say when we are
14430 -- currently instantiating a nested generic. In that case, there is
14431 -- no separate file for the body of the enclosing instance. Instead,
14432 -- the enclosing body must be instantiated as if it were a pending
14433 -- instantiation, in order to produce the body for the nested generic
14434 -- we require now. Note that in that case the generic may be defined
14435 -- in a package body, the instance defined in the same package body,
14436 -- and the original enclosing body may not be in the main unit.
14438 Inst_Node := Empty;
14440 True_Parent := Parent (Spec);
14441 while Present (True_Parent)
14442 and then Nkind (True_Parent) /= N_Compilation_Unit
14444 if Nkind (True_Parent) = N_Package_Declaration
14446 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14448 -- Parent is a compilation unit that is an instantiation, and
14449 -- instantiation node has been replaced with package decl.
14451 Inst_Node := Original_Node (True_Parent);
14454 elsif Nkind (True_Parent) = N_Package_Declaration
14455 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14457 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14459 -- Parent is a compilation unit that is an instantiation, but
14460 -- instantiation node has not been replaced with package decl.
14462 Inst_Node := Unit (Parent (True_Parent));
14465 elsif Nkind (True_Parent) = N_Package_Declaration
14466 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14467 and then Present (Generic_Parent (Specification (True_Parent)))
14469 -- Parent is an instantiation within another specification.
14470 -- Declaration for instance has been inserted before original
14471 -- instantiation node. A direct link would be preferable?
14473 Inst_Node := Next (True_Parent);
14474 while Present (Inst_Node)
14475 and then Nkind (Inst_Node) /= N_Package_Instantiation
14480 -- If the instance appears within a generic, and the generic
14481 -- unit is defined within a formal package of the enclosing
14482 -- generic, there is no generic body available, and none
14483 -- needed. A more precise test should be used ???
14485 if No (Inst_Node) then
14491 -- If an ancestor of the generic comes from a formal package
14492 -- there is no source for the ancestor body. This is detected
14493 -- by examining the scope of the ancestor and its declaration.
14494 -- The body, if any is needed, will be available when the
14495 -- current unit (containing a formal package) is instantiated.
14497 elsif Nkind (True_Parent) = N_Package_Specification
14498 and then Present (Generic_Parent (True_Parent))
14500 (Original_Node (Unit_Declaration_Node
14501 (Scope (Generic_Parent (True_Parent)))))
14502 = N_Formal_Package_Declaration
14507 True_Parent := Parent (True_Parent);
14511 -- Case where we are currently instantiating a nested generic
14513 if Present (Inst_Node) then
14514 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14516 -- Instantiation node and declaration of instantiated package
14517 -- were exchanged when only the declaration was needed.
14518 -- Restore instantiation node before proceeding with body.
14520 Set_Unit (Parent (True_Parent), Inst_Node);
14523 -- Now complete instantiation of enclosing body, if it appears in
14524 -- some other unit. If it appears in the current unit, the body
14525 -- will have been instantiated already.
14527 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14529 -- We need to determine the expander mode to instantiate the
14530 -- enclosing body. Because the generic body we need may use
14531 -- global entities declared in the enclosing package (including
14532 -- aggregates) it is in general necessary to compile this body
14533 -- with expansion enabled, except if we are within a generic
14534 -- package, in which case the usual generic rule applies.
14537 Exp_Status : Boolean := True;
14541 -- Loop through scopes looking for generic package
14543 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14544 while Present (Scop)
14545 and then Scop /= Standard_Standard
14547 if Ekind (Scop) = E_Generic_Package then
14548 Exp_Status := False;
14552 Scop := Scope (Scop);
14555 -- Collect previous instantiations in the unit that contains
14556 -- the desired generic.
14558 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14559 and then not Body_Optional
14563 Info : Pending_Body_Info;
14567 Par := Parent (Inst_Node);
14568 while Present (Par) loop
14569 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14570 Par := Parent (Par);
14573 pragma Assert (Present (Par));
14575 if Nkind (Par) = N_Package_Body then
14576 Collect_Previous_Instances (Declarations (Par));
14578 elsif Nkind (Par) = N_Package_Declaration then
14579 Collect_Previous_Instances
14580 (Visible_Declarations (Specification (Par)));
14581 Collect_Previous_Instances
14582 (Private_Declarations (Specification (Par)));
14585 -- Enclosing unit is a subprogram body. In this
14586 -- case all instance bodies are processed in order
14587 -- and there is no need to collect them separately.
14592 Decl := First_Elmt (Previous_Instances);
14593 while Present (Decl) loop
14596 Instance_Spec (Node (Decl)),
14597 Config_Switches => Save_Config_Switches,
14598 Current_Sem_Unit =>
14599 Get_Code_Unit (Sloc (Node (Decl))),
14600 Expander_Status => Exp_Status,
14601 Inst_Node => Node (Decl),
14602 Local_Suppress_Stack_Top =>
14603 Local_Suppress_Stack_Top,
14604 Scope_Suppress => Scope_Suppress,
14605 Warnings => Save_Warnings);
14607 -- Package instance
14609 if Nkind (Node (Decl)) = N_Package_Instantiation
14611 Instantiate_Package_Body
14612 (Info, Body_Optional => True);
14614 -- Subprogram instance
14617 -- The instance_spec is in the wrapper package,
14618 -- usually followed by its local renaming
14619 -- declaration. See Build_Subprogram_Renaming
14620 -- for details. If the instance carries aspects,
14621 -- these result in the corresponding pragmas,
14622 -- inserted after the subprogram declaration.
14623 -- They must be skipped as well when retrieving
14624 -- the desired spec. Some of them may have been
14625 -- rewritten as null statements.
14626 -- A direct link would be more robust ???
14630 (Last (Visible_Declarations
14631 (Specification (Info.Act_Decl))));
14633 while Nkind (Decl) in
14636 N_Subprogram_Renaming_Declaration
14638 Decl := Prev (Decl);
14641 Info.Act_Decl := Decl;
14644 Instantiate_Subprogram_Body
14645 (Info, Body_Optional => True);
14653 Instantiate_Package_Body
14655 ((Act_Decl => True_Parent,
14656 Config_Switches => Save_Config_Switches,
14657 Current_Sem_Unit =>
14658 Get_Code_Unit (Sloc (Inst_Node)),
14659 Expander_Status => Exp_Status,
14660 Inst_Node => Inst_Node,
14661 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14662 Scope_Suppress => Scope_Suppress,
14663 Warnings => Save_Warnings)),
14664 Body_Optional => Body_Optional);
14668 -- Case where we are not instantiating a nested generic
14671 Opt.Style_Check := False;
14672 Expander_Mode_Save_And_Set (True);
14673 Load_Needed_Body (Comp_Unit, OK);
14674 Opt.Style_Check := Saved_Style_Check;
14675 Restore_Warnings (Saved_Warnings);
14676 Expander_Mode_Restore;
14679 and then Unit_Requires_Body (Defining_Entity (Spec))
14680 and then not Body_Optional
14683 Bname : constant Unit_Name_Type :=
14684 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14687 -- In CodePeer mode, the missing body may make the analysis
14688 -- incomplete, but we do not treat it as fatal.
14690 if CodePeer_Mode then
14694 Error_Msg_Unit_1 := Bname;
14695 Error_Msg_N ("this instantiation requires$!", N);
14696 Error_Msg_File_1 :=
14697 Get_File_Name (Bname, Subunit => False);
14698 Error_Msg_N ("\but file{ was not found!", N);
14699 raise Unrecoverable_Error;
14706 -- If loading parent of the generic caused an instantiation circularity,
14707 -- we abandon compilation at this point, because otherwise in some cases
14708 -- we get into trouble with infinite recursions after this point.
14710 if Circularity_Detected then
14711 raise Unrecoverable_Error;
14713 end Load_Parent_Of_Generic;
14715 ---------------------------------
14716 -- Map_Formal_Package_Entities --
14717 ---------------------------------
14719 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14724 Set_Instance_Of (Form, Act);
14726 -- Traverse formal and actual package to map the corresponding entities.
14727 -- We skip over internal entities that may be generated during semantic
14728 -- analysis, and find the matching entities by name, given that they
14729 -- must appear in the same order.
14731 E1 := First_Entity (Form);
14732 E2 := First_Entity (Act);
14733 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14734 -- Could this test be a single condition??? Seems like it could, and
14735 -- isn't FPE (Form) a constant anyway???
14737 if not Is_Internal (E1)
14738 and then Present (Parent (E1))
14739 and then not Is_Class_Wide_Type (E1)
14740 and then not Is_Internal_Name (Chars (E1))
14742 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14749 Set_Instance_Of (E1, E2);
14751 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14752 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14755 if Is_Constrained (E1) then
14756 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14759 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14760 Map_Formal_Package_Entities (E1, E2);
14767 end Map_Formal_Package_Entities;
14769 -----------------------
14770 -- Move_Freeze_Nodes --
14771 -----------------------
14773 procedure Move_Freeze_Nodes
14774 (Out_Of : Entity_Id;
14779 Next_Decl : Node_Id;
14780 Next_Node : Node_Id := After;
14783 function Is_Outer_Type (T : Entity_Id) return Boolean;
14784 -- Check whether entity is declared in a scope external to that of the
14787 -------------------
14788 -- Is_Outer_Type --
14789 -------------------
14791 function Is_Outer_Type (T : Entity_Id) return Boolean is
14792 Scop : Entity_Id := Scope (T);
14795 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14799 while Scop /= Standard_Standard loop
14800 if Scop = Out_Of then
14803 Scop := Scope (Scop);
14811 -- Start of processing for Move_Freeze_Nodes
14818 -- First remove the freeze nodes that may appear before all other
14822 while Present (Decl)
14823 and then Nkind (Decl) = N_Freeze_Entity
14824 and then Is_Outer_Type (Entity (Decl))
14826 Decl := Remove_Head (L);
14827 Insert_After (Next_Node, Decl);
14828 Set_Analyzed (Decl, False);
14833 -- Next scan the list of declarations and remove each freeze node that
14834 -- appears ahead of the current node.
14836 while Present (Decl) loop
14837 while Present (Next (Decl))
14838 and then Nkind (Next (Decl)) = N_Freeze_Entity
14839 and then Is_Outer_Type (Entity (Next (Decl)))
14841 Next_Decl := Remove_Next (Decl);
14842 Insert_After (Next_Node, Next_Decl);
14843 Set_Analyzed (Next_Decl, False);
14844 Next_Node := Next_Decl;
14847 -- If the declaration is a nested package or concurrent type, then
14848 -- recurse. Nested generic packages will have been processed from the
14851 case Nkind (Decl) is
14852 when N_Package_Declaration =>
14853 Spec := Specification (Decl);
14855 when N_Task_Type_Declaration =>
14856 Spec := Task_Definition (Decl);
14858 when N_Protected_Type_Declaration =>
14859 Spec := Protected_Definition (Decl);
14865 if Present (Spec) then
14866 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14867 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14872 end Move_Freeze_Nodes;
14878 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14880 return Generic_Renamings.Table (E).Next_In_HTable;
14883 ------------------------
14884 -- Preanalyze_Actuals --
14885 ------------------------
14887 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14888 procedure Perform_Appropriate_Analysis (N : Node_Id);
14889 -- Determine if the actuals we are analyzing come from a generic
14890 -- instantiation that is a library unit and dispatch accordingly.
14892 ----------------------------------
14893 -- Perform_Appropriate_Analysis --
14894 ----------------------------------
14896 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14898 -- When we have a library instantiation we cannot allow any expansion
14899 -- to occur, since there may be no place to put it. Instead, in that
14900 -- case we perform a preanalysis of the actual.
14902 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14907 end Perform_Appropriate_Analysis;
14911 Errs : constant Nat := Serious_Errors_Detected;
14916 Cur : Entity_Id := Empty;
14917 -- Current homograph of the instance name
14919 Vis : Boolean := False;
14920 -- Saved visibility status of the current homograph
14922 -- Start of processing for Preanalyze_Actuals
14925 Assoc := First (Generic_Associations (N));
14927 -- If the instance is a child unit, its name may hide an outer homonym,
14928 -- so make it invisible to perform name resolution on the actuals.
14930 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14932 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14934 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14936 if Is_Compilation_Unit (Cur) then
14937 Vis := Is_Immediately_Visible (Cur);
14938 Set_Is_Immediately_Visible (Cur, False);
14944 while Present (Assoc) loop
14945 if Nkind (Assoc) /= N_Others_Choice then
14946 Act := Explicit_Generic_Actual_Parameter (Assoc);
14948 -- Within a nested instantiation, a defaulted actual is an empty
14949 -- association, so nothing to analyze. If the subprogram actual
14950 -- is an attribute, analyze prefix only, because actual is not a
14951 -- complete attribute reference.
14953 -- If actual is an allocator, analyze expression only. The full
14954 -- analysis can generate code, and if instance is a compilation
14955 -- unit we have to wait until the package instance is installed
14956 -- to have a proper place to insert this code.
14958 -- String literals may be operators, but at this point we do not
14959 -- know whether the actual is a formal subprogram or a string.
14964 elsif Nkind (Act) = N_Attribute_Reference then
14965 Perform_Appropriate_Analysis (Prefix (Act));
14967 elsif Nkind (Act) = N_Explicit_Dereference then
14968 Perform_Appropriate_Analysis (Prefix (Act));
14970 elsif Nkind (Act) = N_Allocator then
14972 Expr : constant Node_Id := Expression (Act);
14975 if Nkind (Expr) = N_Subtype_Indication then
14976 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14978 -- Analyze separately each discriminant constraint, when
14979 -- given with a named association.
14985 Constr := First (Constraints (Constraint (Expr)));
14986 while Present (Constr) loop
14987 if Nkind (Constr) = N_Discriminant_Association then
14988 Perform_Appropriate_Analysis
14989 (Expression (Constr));
14991 Perform_Appropriate_Analysis (Constr);
14999 Perform_Appropriate_Analysis (Expr);
15003 elsif Nkind (Act) /= N_Operator_Symbol then
15004 Perform_Appropriate_Analysis (Act);
15006 -- Within a package instance, mark actuals that are limited
15007 -- views, so their use can be moved to the body of the
15010 if Is_Entity_Name (Act)
15011 and then Is_Type (Entity (Act))
15012 and then From_Limited_With (Entity (Act))
15013 and then Present (Inst)
15015 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15019 if Errs /= Serious_Errors_Detected then
15021 -- Do a minimal analysis of the generic, to prevent spurious
15022 -- warnings complaining about the generic being unreferenced,
15023 -- before abandoning the instantiation.
15025 Perform_Appropriate_Analysis (Name (N));
15027 if Is_Entity_Name (Name (N))
15028 and then Etype (Name (N)) /= Any_Type
15030 Generate_Reference (Entity (Name (N)), Name (N));
15031 Set_Is_Instantiated (Entity (Name (N)));
15034 if Present (Cur) then
15036 -- For the case of a child instance hiding an outer homonym,
15037 -- provide additional warning which might explain the error.
15039 Set_Is_Immediately_Visible (Cur, Vis);
15041 ("& hides outer unit with the same name??",
15042 N, Defining_Unit_Name (N));
15045 Abandon_Instantiation (Act);
15052 if Present (Cur) then
15053 Set_Is_Immediately_Visible (Cur, Vis);
15055 end Preanalyze_Actuals;
15057 -------------------------------
15058 -- Provide_Completing_Bodies --
15059 -------------------------------
15061 procedure Provide_Completing_Bodies (N : Node_Id) is
15062 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15063 -- Generate the completing body for subprogram declaration Subp_Decl
15065 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15066 -- Generating completing bodies for all subprograms found in declarative
15069 ---------------------------
15070 -- Build_Completing_Body --
15071 ---------------------------
15073 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15074 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15075 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15079 -- Nothing to do if the subprogram already has a completing body
15081 if Present (Corresponding_Body (Subp_Decl)) then
15084 -- Mark the function as having a valid return statement even though
15085 -- the body contains a single raise statement.
15087 elsif Ekind (Subp_Id) = E_Function then
15088 Set_Return_Present (Subp_Id);
15091 -- Clone the specification to obtain new entities and reset the only
15094 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15095 Set_Generic_Parent (Spec, Empty);
15098 -- function Func ... return ... is
15100 -- procedure Proc ... is
15102 -- raise Program_Error with "access before elaboration";
15105 Insert_After_And_Analyze (Subp_Decl,
15106 Make_Subprogram_Body (Loc,
15107 Specification => Spec,
15108 Declarations => New_List,
15109 Handled_Statement_Sequence =>
15110 Make_Handled_Sequence_Of_Statements (Loc,
15111 Statements => New_List (
15112 Make_Raise_Program_Error (Loc,
15113 Reason => PE_Access_Before_Elaboration)))));
15114 end Build_Completing_Body;
15116 ----------------------------------
15117 -- Provide_Completing_Bodies_In --
15118 ----------------------------------
15120 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15124 if Present (Decls) then
15125 Decl := First (Decls);
15126 while Present (Decl) loop
15127 Provide_Completing_Bodies (Decl);
15131 end Provide_Completing_Bodies_In;
15137 -- Start of processing for Provide_Completing_Bodies
15140 if Nkind (N) = N_Package_Declaration then
15141 Spec := Specification (N);
15143 Push_Scope (Defining_Entity (N));
15144 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15145 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15148 elsif Nkind (N) = N_Subprogram_Declaration then
15149 Build_Completing_Body (N);
15151 end Provide_Completing_Bodies;
15153 -------------------
15154 -- Remove_Parent --
15155 -------------------
15157 procedure Remove_Parent (In_Body : Boolean := False) is
15158 S : Entity_Id := Current_Scope;
15159 -- S is the scope containing the instantiation just completed. The scope
15160 -- stack contains the parent instances of the instantiation, followed by
15169 -- After child instantiation is complete, remove from scope stack the
15170 -- extra copy of the current scope, and then remove parent instances.
15172 if not In_Body then
15175 while Current_Scope /= S loop
15176 P := Current_Scope;
15177 End_Package_Scope (Current_Scope);
15179 if In_Open_Scopes (P) then
15180 E := First_Entity (P);
15181 while Present (E) loop
15182 Set_Is_Immediately_Visible (E, True);
15186 -- If instantiation is declared in a block, it is the enclosing
15187 -- scope that might be a parent instance. Note that only one
15188 -- block can be involved, because the parent instances have
15189 -- been installed within it.
15191 if Ekind (P) = E_Block then
15192 Cur_P := Scope (P);
15197 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15198 -- We are within an instance of some sibling. Retain
15199 -- visibility of parent, for proper subsequent cleanup, and
15200 -- reinstall private declarations as well.
15202 Set_In_Private_Part (P);
15203 Install_Private_Declarations (P);
15206 -- If the ultimate parent is a top-level unit recorded in
15207 -- Instance_Parent_Unit, then reset its visibility to what it was
15208 -- before instantiation. (It's not clear what the purpose is of
15209 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15210 -- present before the ultimate parent test was added.???)
15212 elsif not In_Open_Scopes (Scope (P))
15213 or else (P = Instance_Parent_Unit
15214 and then not Parent_Unit_Visible)
15216 Set_Is_Immediately_Visible (P, False);
15218 -- If the current scope is itself an instantiation of a generic
15219 -- nested within P, and we are in the private part of body of this
15220 -- instantiation, restore the full views of P, that were removed
15221 -- in End_Package_Scope above. This obscure case can occur when a
15222 -- subunit of a generic contains an instance of a child unit of
15223 -- its generic parent unit.
15225 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15227 Par : constant Entity_Id :=
15228 Generic_Parent (Package_Specification (S));
15231 and then P = Scope (Par)
15232 and then (In_Package_Body (S) or else In_Private_Part (S))
15234 Set_In_Private_Part (P);
15235 Install_Private_Declarations (P);
15241 -- Reset visibility of entities in the enclosing scope
15243 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15245 Hidden := First_Elmt (Hidden_Entities);
15246 while Present (Hidden) loop
15247 Set_Is_Immediately_Visible (Node (Hidden), True);
15248 Next_Elmt (Hidden);
15252 -- Each body is analyzed separately, and there is no context that
15253 -- needs preserving from one body instance to the next, so remove all
15254 -- parent scopes that have been installed.
15256 while Present (S) loop
15257 End_Package_Scope (S);
15258 Set_Is_Immediately_Visible (S, False);
15259 S := Current_Scope;
15260 exit when S = Standard_Standard;
15269 procedure Restore_Env is
15270 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15273 if No (Current_Instantiated_Parent.Act_Id) then
15274 -- Restore environment after subprogram inlining
15276 Restore_Private_Views (Empty);
15279 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15280 Exchanged_Views := Saved.Exchanged_Views;
15281 Hidden_Entities := Saved.Hidden_Entities;
15282 Current_Sem_Unit := Saved.Current_Sem_Unit;
15283 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15284 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15286 Restore_Config_Switches (Saved.Switches);
15288 Instance_Envs.Decrement_Last;
15291 ---------------------------
15292 -- Restore_Private_Views --
15293 ---------------------------
15295 procedure Restore_Private_Views
15296 (Pack_Id : Entity_Id;
15297 Is_Package : Boolean := True)
15302 Dep_Elmt : Elmt_Id;
15305 procedure Restore_Nested_Formal (Formal : Entity_Id);
15306 -- Hide the generic formals of formal packages declared with box which
15307 -- were reachable in the current instantiation.
15309 ---------------------------
15310 -- Restore_Nested_Formal --
15311 ---------------------------
15313 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15317 if Present (Renamed_Object (Formal))
15318 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15322 elsif Present (Associated_Formal_Package (Formal)) then
15323 Ent := First_Entity (Formal);
15324 while Present (Ent) loop
15325 exit when Ekind (Ent) = E_Package
15326 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15328 Set_Is_Hidden (Ent);
15329 Set_Is_Potentially_Use_Visible (Ent, False);
15331 -- If package, then recurse
15333 if Ekind (Ent) = E_Package then
15334 Restore_Nested_Formal (Ent);
15340 end Restore_Nested_Formal;
15342 -- Start of processing for Restore_Private_Views
15345 M := First_Elmt (Exchanged_Views);
15346 while Present (M) loop
15349 -- Subtypes of types whose views have been exchanged, and that are
15350 -- defined within the instance, were not on the Private_Dependents
15351 -- list on entry to the instance, so they have to be exchanged
15352 -- explicitly now, in order to remain consistent with the view of the
15355 if Ekind (Typ) in E_Private_Type
15356 | E_Limited_Private_Type
15357 | E_Record_Type_With_Private
15359 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15360 while Present (Dep_Elmt) loop
15361 Dep_Typ := Node (Dep_Elmt);
15363 if Scope (Dep_Typ) = Pack_Id
15364 and then Present (Full_View (Dep_Typ))
15366 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15367 Exchange_Declarations (Dep_Typ);
15370 Next_Elmt (Dep_Elmt);
15374 Exchange_Declarations (Node (M));
15378 if No (Pack_Id) then
15382 -- Make the generic formal parameters private, and make the formal types
15383 -- into subtypes of the actuals again.
15385 E := First_Entity (Pack_Id);
15386 while Present (E) loop
15387 Set_Is_Hidden (E, True);
15390 and then Nkind (Parent (E)) = N_Subtype_Declaration
15392 -- If the actual for E is itself a generic actual type from
15393 -- an enclosing instance, E is still a generic actual type
15394 -- outside of the current instance. This matter when resolving
15395 -- an overloaded call that may be ambiguous in the enclosing
15396 -- instance, when two of its actuals coincide.
15398 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15399 and then Is_Generic_Actual_Type
15400 (Entity (Subtype_Indication (Parent (E))))
15404 Set_Is_Generic_Actual_Type (E, False);
15406 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15407 -- flag also on the Full_View if the type is private, since it
15408 -- was set also on this Full_View. However, this flag is relied
15409 -- upon by Covers to spot "types exported from instantiations"
15410 -- which are implicit Full_Views built for instantiations made
15411 -- on private types and we get type mismatches if we do it when
15412 -- the block exchanging the declarations below triggers ???
15414 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15415 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15419 -- An unusual case of aliasing: the actual may also be directly
15420 -- visible in the generic, and be private there, while it is fully
15421 -- visible in the context of the instance. The internal subtype
15422 -- is private in the instance but has full visibility like its
15423 -- parent in the enclosing scope. This enforces the invariant that
15424 -- the privacy status of all private dependents of a type coincide
15425 -- with that of the parent type. This can only happen when a
15426 -- generic child unit is instantiated within a sibling.
15428 if Is_Private_Type (E)
15429 and then not Is_Private_Type (Etype (E))
15431 Exchange_Declarations (E);
15434 elsif Ekind (E) = E_Package then
15436 -- The end of the renaming list is the renaming of the generic
15437 -- package itself. If the instance is a subprogram, all entities
15438 -- in the corresponding package are renamings. If this entity is
15439 -- a formal package, make its own formals private as well. The
15440 -- actual in this case is itself the renaming of an instantiation.
15441 -- If the entity is not a package renaming, it is the entity
15442 -- created to validate formal package actuals: ignore it.
15444 -- If the actual is itself a formal package for the enclosing
15445 -- generic, or the actual for such a formal package, it remains
15446 -- visible on exit from the instance, and therefore nothing needs
15447 -- to be done either, except to keep it accessible.
15449 if Is_Package and then Renamed_Object (E) = Pack_Id then
15452 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15456 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15458 Set_Is_Hidden (E, False);
15462 Act_P : constant Entity_Id := Renamed_Object (E);
15466 Id := First_Entity (Act_P);
15468 and then Id /= First_Private_Entity (Act_P)
15470 exit when Ekind (Id) = E_Package
15471 and then Renamed_Object (Id) = Act_P;
15473 Set_Is_Hidden (Id, True);
15474 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15476 if Ekind (Id) = E_Package then
15477 Restore_Nested_Formal (Id);
15488 end Restore_Private_Views;
15495 (Gen_Unit : Entity_Id;
15496 Act_Unit : Entity_Id)
15500 Set_Instance_Env (Gen_Unit, Act_Unit);
15503 ----------------------------
15504 -- Save_Global_References --
15505 ----------------------------
15507 procedure Save_Global_References (Templ : Node_Id) is
15509 -- ??? it is horrible to use global variables in highly recursive code
15512 -- The entity of the current associated node
15514 Gen_Scope : Entity_Id;
15515 -- The scope of the generic for which references are being saved
15518 -- The current associated node
15520 function Is_Global (E : Entity_Id) return Boolean;
15521 -- Check whether entity is defined outside of generic unit. Examine the
15522 -- scope of an entity, and the scope of the scope, etc, until we find
15523 -- either Standard, in which case the entity is global, or the generic
15524 -- unit itself, which indicates that the entity is local. If the entity
15525 -- is the generic unit itself, as in the case of a recursive call, or
15526 -- the enclosing generic unit, if different from the current scope, then
15527 -- it is local as well, because it will be replaced at the point of
15528 -- instantiation. On the other hand, if it is a reference to a child
15529 -- unit of a common ancestor, which appears in an instantiation, it is
15530 -- global because it is used to denote a specific compilation unit at
15531 -- the time the instantiations will be analyzed.
15533 procedure Qualify_Universal_Operands
15535 Func_Call : Node_Id);
15536 -- Op denotes a binary or unary operator in generic template Templ. Node
15537 -- Func_Call is the function call alternative of the operator within the
15538 -- the analyzed copy of the template. Change each operand which yields a
15539 -- universal type by wrapping it into a qualified expression
15541 -- Actual_Typ'(Operand)
15543 -- where Actual_Typ is the type of corresponding actual parameter of
15544 -- Operand in Func_Call.
15546 procedure Reset_Entity (N : Node_Id);
15547 -- Save semantic information on global entity so that it is not resolved
15548 -- again at instantiation time.
15550 procedure Save_Entity_Descendants (N : Node_Id);
15551 -- Apply Save_Global_References to the two syntactic descendants of
15552 -- non-terminal nodes that carry an Associated_Node and are processed
15553 -- through Reset_Entity. Once the global entity (if any) has been
15554 -- captured together with its type, only two syntactic descendants need
15555 -- to be traversed to complete the processing of the tree rooted at N.
15556 -- This applies to Selected_Components, Expanded_Names, and to Operator
15557 -- nodes. N can also be a character literal, identifier, or operator
15558 -- symbol node, but the call has no effect in these cases.
15560 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15561 -- Default actuals in nested instances must be handled specially
15562 -- because there is no link to them from the original tree. When an
15563 -- actual subprogram is given by a default, we add an explicit generic
15564 -- association for it in the instantiation node. When we save the
15565 -- global references on the name of the instance, we recover the list
15566 -- of generic associations, and add an explicit one to the original
15567 -- generic tree, through which a global actual can be preserved.
15568 -- Similarly, if a child unit is instantiated within a sibling, in the
15569 -- context of the parent, we must preserve the identifier of the parent
15570 -- so that it can be properly resolved in a subsequent instantiation.
15572 procedure Save_Global_Descendant (D : Union_Id);
15573 -- Apply Save_References recursively to the descendants of node D
15575 procedure Save_References (N : Node_Id);
15576 -- This is the recursive procedure that does the work, once the
15577 -- enclosing generic scope has been established.
15583 function Is_Global (E : Entity_Id) return Boolean is
15586 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15587 -- Determine whether the parent node of a reference to a child unit
15588 -- denotes an instantiation or a formal package, in which case the
15589 -- reference to the child unit is global, even if it appears within
15590 -- the current scope (e.g. when the instance appears within the body
15591 -- of an ancestor).
15593 ----------------------
15594 -- Is_Instance_Node --
15595 ----------------------
15597 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15599 return Nkind (Decl) in N_Generic_Instantiation
15601 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15602 end Is_Instance_Node;
15604 -- Start of processing for Is_Global
15607 if E = Gen_Scope then
15610 elsif E = Standard_Standard then
15613 elsif Is_Child_Unit (E)
15614 and then (Is_Instance_Node (Parent (N2))
15615 or else (Nkind (Parent (N2)) = N_Expanded_Name
15616 and then N2 = Selector_Name (Parent (N2))
15618 Is_Instance_Node (Parent (Parent (N2)))))
15624 while Se /= Gen_Scope loop
15625 if Se = Standard_Standard then
15636 --------------------------------
15637 -- Qualify_Universal_Operands --
15638 --------------------------------
15640 procedure Qualify_Universal_Operands
15642 Func_Call : Node_Id)
15644 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15645 -- Rewrite operand Opnd as a qualified expression of the form
15647 -- Actual_Typ'(Opnd)
15649 -- where Actual is the corresponding actual parameter of Opnd in
15650 -- function call Func_Call.
15652 function Qualify_Type
15654 Typ : Entity_Id) return Node_Id;
15655 -- Qualify type Typ by creating a selected component of the form
15657 -- Scope_Of_Typ.Typ
15659 ---------------------
15660 -- Qualify_Operand --
15661 ---------------------
15663 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15664 Loc : constant Source_Ptr := Sloc (Opnd);
15665 Typ : constant Entity_Id := Etype (Actual);
15670 -- Qualify the operand when it is of a universal type. Note that
15671 -- the template is unanalyzed and it is not possible to directly
15672 -- query the type. This transformation is not done when the type
15673 -- of the actual is internally generated because the type will be
15674 -- regenerated in the instance.
15676 if Yields_Universal_Type (Opnd)
15677 and then Comes_From_Source (Typ)
15678 and then not Is_Hidden (Typ)
15680 -- The type of the actual may be a global reference. Save this
15681 -- information by creating a reference to it.
15683 if Is_Global (Typ) then
15684 Mark := New_Occurrence_Of (Typ, Loc);
15686 -- Otherwise rely on resolution to find the proper type within
15690 Mark := Qualify_Type (Loc, Typ);
15694 Make_Qualified_Expression (Loc,
15695 Subtype_Mark => Mark,
15696 Expression => Relocate_Node (Opnd));
15698 -- Mark the qualification to distinguish it from other source
15699 -- constructs and signal the instantiation mechanism that this
15700 -- node requires special processing. See Copy_Generic_Node for
15703 Set_Is_Qualified_Universal_Literal (Qual);
15705 Rewrite (Opnd, Qual);
15707 end Qualify_Operand;
15713 function Qualify_Type
15715 Typ : Entity_Id) return Node_Id
15717 Scop : constant Entity_Id := Scope (Typ);
15721 Result := Make_Identifier (Loc, Chars (Typ));
15723 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15725 Make_Selected_Component (Loc,
15726 Prefix => Make_Identifier (Loc, Chars (Scop)),
15727 Selector_Name => Result);
15735 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15737 -- Start of processing for Qualify_Universal_Operands
15740 if Nkind (Op) in N_Binary_Op then
15741 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15742 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15744 elsif Nkind (Op) in N_Unary_Op then
15745 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15747 end Qualify_Universal_Operands;
15753 procedure Reset_Entity (N : Node_Id) is
15754 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15755 -- If the type of N2 is global to the generic unit, save the type in
15756 -- the generic node. Just as we perform name capture for explicit
15757 -- references within the generic, we must capture the global types
15758 -- of local entities because they may participate in resolution in
15761 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15762 -- Find the ultimate ancestor of the current unit. If it is not a
15763 -- generic unit, then the name of the current unit in the prefix of
15764 -- an expanded name must be replaced with its generic homonym to
15765 -- ensure that it will be properly resolved in an instance.
15767 ---------------------
15768 -- Set_Global_Type --
15769 ---------------------
15771 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15772 Typ : constant Entity_Id := Etype (N2);
15775 Set_Etype (N, Typ);
15777 -- If the entity of N is not the associated node, this is a
15778 -- nested generic and it has an associated node as well, whose
15779 -- type is already the full view (see below). Indicate that the
15780 -- original node has a private view.
15782 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15783 Set_Has_Private_View (N);
15786 -- If not a private type, nothing else to do
15788 if not Is_Private_Type (Typ) then
15791 -- If it is a derivation of a private type in a context where no
15792 -- full view is needed, nothing to do either.
15794 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15797 -- Otherwise mark the type for flipping and use the full view when
15801 Set_Has_Private_View (N);
15803 if Present (Full_View (Typ)) then
15804 Set_Etype (N2, Full_View (Typ));
15808 if Is_Floating_Point_Type (Typ)
15809 and then Has_Dimension_System (Typ)
15811 Copy_Dimensions (N2, N);
15813 end Set_Global_Type;
15819 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15824 while Is_Child_Unit (Par) loop
15825 Par := Scope (Par);
15831 -- Start of processing for Reset_Entity
15834 N2 := Get_Associated_Node (N);
15837 if Present (E) then
15839 -- If the node is an entry call to an entry in an enclosing task,
15840 -- it is rewritten as a selected component. No global entity to
15841 -- preserve in this case, since the expansion will be redone in
15844 if Nkind (E) not in N_Entity then
15845 Set_Associated_Node (N, Empty);
15846 Set_Etype (N, Empty);
15850 -- If the entity is an itype created as a subtype of an access
15851 -- type with a null exclusion restore source entity for proper
15852 -- visibility. The itype will be created anew in the instance.
15855 and then Ekind (E) = E_Access_Subtype
15856 and then Is_Entity_Name (N)
15857 and then Chars (Etype (E)) = Chars (N)
15860 Set_Entity (N2, E);
15864 if Is_Global (E) then
15865 Set_Global_Type (N, N2);
15867 elsif Nkind (N) = N_Op_Concat
15868 and then Is_Generic_Type (Etype (N2))
15869 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15871 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15872 and then Is_Intrinsic_Subprogram (E)
15876 -- Entity is local. Mark generic node as unresolved. Note that now
15877 -- it does not have an entity.
15880 Set_Associated_Node (N, Empty);
15881 Set_Etype (N, Empty);
15884 if Nkind (Parent (N)) in N_Generic_Instantiation
15885 and then N = Name (Parent (N))
15887 Save_Global_Defaults (Parent (N), Parent (N2));
15890 elsif Nkind (Parent (N)) = N_Selected_Component
15891 and then Nkind (Parent (N2)) = N_Expanded_Name
15893 -- In case of previous errors, the tree might be malformed
15895 if No (Entity (Parent (N2))) then
15898 elsif Is_Global (Entity (Parent (N2))) then
15899 Change_Selected_Component_To_Expanded_Name (Parent (N));
15900 Set_Associated_Node (Parent (N), Parent (N2));
15901 Set_Global_Type (Parent (N), Parent (N2));
15902 Save_Entity_Descendants (N);
15904 -- If this is a reference to the current generic entity, replace
15905 -- by the name of the generic homonym of the current package. This
15906 -- is because in an instantiation Par.P.Q will not resolve to the
15907 -- name of the instance, whose enclosing scope is not necessarily
15908 -- Par. We use the generic homonym rather that the name of the
15909 -- generic itself because it may be hidden by a local declaration.
15911 elsif In_Open_Scopes (Entity (Parent (N2)))
15913 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15915 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15916 Rewrite (Parent (N),
15917 Make_Identifier (Sloc (N),
15919 Chars (Generic_Homonym (Entity (Parent (N2))))));
15921 Rewrite (Parent (N),
15922 Make_Identifier (Sloc (N),
15923 Chars => Chars (Selector_Name (Parent (N2)))));
15927 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15928 and then Parent (N) = Name (Parent (Parent (N)))
15930 Save_Global_Defaults
15931 (Parent (Parent (N)), Parent (Parent (N2)));
15934 -- A selected component may denote a static constant that has been
15935 -- folded. If the static constant is global to the generic, capture
15936 -- its value. Otherwise the folding will happen in any instantiation.
15938 elsif Nkind (Parent (N)) = N_Selected_Component
15939 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15941 if Present (Entity (Original_Node (Parent (N2))))
15942 and then Is_Global (Entity (Original_Node (Parent (N2))))
15944 Rewrite (Parent (N), New_Copy (Parent (N2)));
15945 Set_Analyzed (Parent (N), False);
15948 -- A selected component may be transformed into a parameterless
15949 -- function call. If the called entity is global, rewrite the node
15950 -- appropriately, i.e. as an extended name for the global entity.
15952 elsif Nkind (Parent (N)) = N_Selected_Component
15953 and then Nkind (Parent (N2)) = N_Function_Call
15954 and then N = Selector_Name (Parent (N))
15956 if No (Parameter_Associations (Parent (N2))) then
15957 if Is_Global (Entity (Name (Parent (N2)))) then
15958 Change_Selected_Component_To_Expanded_Name (Parent (N));
15959 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15960 Set_Global_Type (Parent (N), Name (Parent (N2)));
15961 Save_Entity_Descendants (N);
15964 Set_Is_Prefixed_Call (Parent (N));
15965 Set_Associated_Node (N, Empty);
15966 Set_Etype (N, Empty);
15969 -- In Ada 2005, X.F may be a call to a primitive operation,
15970 -- rewritten as F (X). This rewriting will be done again in an
15971 -- instance, so keep the original node. Global entities will be
15972 -- captured as for other constructs. Indicate that this must
15973 -- resolve as a call, to prevent accidental overloading in the
15974 -- instance, if both a component and a primitive operation appear
15978 Set_Is_Prefixed_Call (Parent (N));
15981 -- Entity is local. Reset in generic unit, so that node is resolved
15982 -- anew at the point of instantiation.
15985 Set_Associated_Node (N, Empty);
15986 Set_Etype (N, Empty);
15990 -----------------------------
15991 -- Save_Entity_Descendants --
15992 -----------------------------
15994 procedure Save_Entity_Descendants (N : Node_Id) is
15997 when N_Binary_Op =>
15998 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15999 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16002 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16004 when N_Expanded_Name
16005 | N_Selected_Component
16007 Save_Global_Descendant (Union_Id (Prefix (N)));
16008 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16010 when N_Character_Literal
16012 | N_Operator_Symbol
16017 raise Program_Error;
16019 end Save_Entity_Descendants;
16021 --------------------------
16022 -- Save_Global_Defaults --
16023 --------------------------
16025 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16026 Loc : constant Source_Ptr := Sloc (N1);
16027 Assoc2 : constant List_Id := Generic_Associations (N2);
16028 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16035 Actual : Entity_Id;
16038 Assoc1 := Generic_Associations (N1);
16040 if Present (Assoc1) then
16041 Act1 := First (Assoc1);
16044 Set_Generic_Associations (N1, New_List);
16045 Assoc1 := Generic_Associations (N1);
16048 if Present (Assoc2) then
16049 Act2 := First (Assoc2);
16054 while Present (Act1) and then Present (Act2) loop
16059 -- Find the associations added for default subprograms
16061 if Present (Act2) then
16062 while Nkind (Act2) /= N_Generic_Association
16063 or else No (Entity (Selector_Name (Act2)))
16064 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16069 -- Add a similar association if the default is global. The
16070 -- renaming declaration for the actual has been analyzed, and
16071 -- its alias is the program it renames. Link the actual in the
16072 -- original generic tree with the node in the analyzed tree.
16074 while Present (Act2) loop
16075 Subp := Entity (Selector_Name (Act2));
16076 Def := Explicit_Generic_Actual_Parameter (Act2);
16078 -- Following test is defence against rubbish errors
16080 if No (Alias (Subp)) then
16084 -- Retrieve the resolved actual from the renaming declaration
16085 -- created for the instantiated formal.
16087 Actual := Entity (Name (Parent (Parent (Subp))));
16088 Set_Entity (Def, Actual);
16089 Set_Etype (Def, Etype (Actual));
16091 if Is_Global (Actual) then
16093 Make_Generic_Association (Loc,
16095 New_Occurrence_Of (Subp, Loc),
16096 Explicit_Generic_Actual_Parameter =>
16097 New_Occurrence_Of (Actual, Loc));
16099 Set_Associated_Node
16100 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16102 Append (Ndec, Assoc1);
16104 -- If there are other defaults, add a dummy association in case
16105 -- there are other defaulted formals with the same name.
16107 elsif Present (Next (Act2)) then
16109 Make_Generic_Association (Loc,
16111 New_Occurrence_Of (Subp, Loc),
16112 Explicit_Generic_Actual_Parameter => Empty);
16114 Append (Ndec, Assoc1);
16121 if Nkind (Name (N1)) = N_Identifier
16122 and then Is_Child_Unit (Gen_Id)
16123 and then Is_Global (Gen_Id)
16124 and then Is_Generic_Unit (Scope (Gen_Id))
16125 and then In_Open_Scopes (Scope (Gen_Id))
16127 -- This is an instantiation of a child unit within a sibling, so
16128 -- that the generic parent is in scope. An eventual instance must
16129 -- occur within the scope of an instance of the parent. Make name
16130 -- in instance into an expanded name, to preserve the identifier
16131 -- of the parent, so it can be resolved subsequently.
16133 Rewrite (Name (N2),
16134 Make_Expanded_Name (Loc,
16135 Chars => Chars (Gen_Id),
16136 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16137 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16138 Set_Entity (Name (N2), Gen_Id);
16140 Rewrite (Name (N1),
16141 Make_Expanded_Name (Loc,
16142 Chars => Chars (Gen_Id),
16143 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16144 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16146 Set_Associated_Node (Name (N1), Name (N2));
16147 Set_Associated_Node (Prefix (Name (N1)), Empty);
16148 Set_Associated_Node
16149 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16150 Set_Etype (Name (N1), Etype (Gen_Id));
16152 end Save_Global_Defaults;
16154 ----------------------------
16155 -- Save_Global_Descendant --
16156 ----------------------------
16158 procedure Save_Global_Descendant (D : Union_Id) is
16162 if D in Node_Range then
16163 if D = Union_Id (Empty) then
16166 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16167 Save_References (Node_Id (D));
16170 elsif D in List_Range then
16171 pragma Assert (D /= Union_Id (No_List));
16172 -- Because No_List = Empty, which is in Node_Range above
16174 if Is_Empty_List (List_Id (D)) then
16178 N1 := First (List_Id (D));
16179 while Present (N1) loop
16180 Save_References (N1);
16185 -- Element list or other non-node field, nothing to do
16190 end Save_Global_Descendant;
16192 ---------------------
16193 -- Save_References --
16194 ---------------------
16196 -- This is the recursive procedure that does the work once the enclosing
16197 -- generic scope has been established. We have to treat specially a
16198 -- number of node rewritings that are required by semantic processing
16199 -- and which change the kind of nodes in the generic copy: typically
16200 -- constant-folding, replacing an operator node by a string literal, or
16201 -- a selected component by an expanded name. In each of those cases, the
16202 -- transformation is propagated to the generic unit.
16204 procedure Save_References (N : Node_Id) is
16205 Loc : constant Source_Ptr := Sloc (N);
16207 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16208 -- Determine whether arbitrary node Nod requires delayed capture of
16209 -- global references within its aspect specifications.
16211 procedure Save_References_In_Aggregate (N : Node_Id);
16212 -- Save all global references in [extension] aggregate node N
16214 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16215 -- Save all global references in a character literal or operator
16216 -- symbol denoted by N.
16218 procedure Save_References_In_Descendants (N : Node_Id);
16219 -- Save all global references in all descendants of node N
16221 procedure Save_References_In_Identifier (N : Node_Id);
16222 -- Save all global references in identifier node N
16224 procedure Save_References_In_Operator (N : Node_Id);
16225 -- Save all global references in operator node N
16227 procedure Save_References_In_Pragma (Prag : Node_Id);
16228 -- Save all global references found within the expression of pragma
16231 ---------------------------
16232 -- Requires_Delayed_Save --
16233 ---------------------------
16235 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16237 -- Generic packages and subprograms require delayed capture of
16238 -- global references within their aspects due to the timing of
16239 -- annotation analysis.
16241 if Nkind (Nod) in N_Generic_Package_Declaration
16242 | N_Generic_Subprogram_Declaration
16244 | N_Package_Body_Stub
16245 | N_Subprogram_Body
16246 | N_Subprogram_Body_Stub
16248 -- Since the capture of global references is done on the
16249 -- unanalyzed generic template, there is no information around
16250 -- to infer the context. Use the Associated_Entity linkages to
16251 -- peek into the analyzed generic copy and determine what the
16252 -- template corresponds to.
16254 if Nod = Templ then
16256 Is_Generic_Declaration_Or_Body
16257 (Unit_Declaration_Node
16258 (Associated_Entity (Defining_Entity (Nod))));
16260 -- Otherwise the generic unit being processed is not the top
16261 -- level template. It is safe to capture of global references
16262 -- within the generic unit because at this point the top level
16263 -- copy is fully analyzed.
16269 -- Otherwise capture the global references without interference
16274 end Requires_Delayed_Save;
16276 ----------------------------------
16277 -- Save_References_In_Aggregate --
16278 ----------------------------------
16280 procedure Save_References_In_Aggregate (N : Node_Id) is
16282 Qual : Node_Id := Empty;
16283 Typ : Entity_Id := Empty;
16285 use Atree.Unchecked_Access;
16286 -- This code section is part of implementing an untyped tree
16287 -- traversal, so it needs direct access to node fields.
16290 N2 := Get_Associated_Node (N);
16292 if Present (N2) then
16295 -- In an instance within a generic, use the name of the actual
16296 -- and not the original generic parameter. If the actual is
16297 -- global in the current generic it must be preserved for its
16300 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16301 and then Present (Generic_Parent_Type (Parent (Typ)))
16303 Typ := Base_Type (Typ);
16304 Set_Etype (N2, Typ);
16308 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16309 Set_Associated_Node (N, Empty);
16311 -- If the aggregate is an actual in a call, it has been
16312 -- resolved in the current context, to some local type. The
16313 -- enclosing call may have been disambiguated by the aggregate,
16314 -- and this disambiguation might fail at instantiation time
16315 -- because the type to which the aggregate did resolve is not
16316 -- preserved. In order to preserve some of this information,
16317 -- wrap the aggregate in a qualified expression, using the id
16318 -- of its type. For further disambiguation we qualify the type
16319 -- name with its scope (if visible and not hidden by a local
16320 -- homograph) because both id's will have corresponding
16321 -- entities in an instance. This resolves most of the problems
16322 -- with missing type information on aggregates in instances.
16325 and then Nkind (N2) = Nkind (N)
16326 and then Nkind (Parent (N2)) in N_Subprogram_Call
16327 and then Present (Typ)
16328 and then Comes_From_Source (Typ)
16330 Nam := Make_Identifier (Loc, Chars (Typ));
16332 if Is_Immediately_Visible (Scope (Typ))
16334 (not In_Open_Scopes (Scope (Typ))
16335 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16338 Make_Selected_Component (Loc,
16340 Make_Identifier (Loc, Chars (Scope (Typ))),
16341 Selector_Name => Nam);
16345 Make_Qualified_Expression (Loc,
16346 Subtype_Mark => Nam,
16347 Expression => Relocate_Node (N));
16351 Save_Global_Descendant (Field1 (N));
16352 Save_Global_Descendant (Field2 (N));
16353 Save_Global_Descendant (Field3 (N));
16354 Save_Global_Descendant (Field5 (N));
16356 if Present (Qual) then
16359 end Save_References_In_Aggregate;
16361 ----------------------------------------------
16362 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16363 ----------------------------------------------
16365 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16367 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16370 elsif Nkind (N) = N_Operator_Symbol
16371 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16373 Change_Operator_Symbol_To_String_Literal (N);
16375 end Save_References_In_Char_Lit_Or_Op_Symbol;
16377 ------------------------------------
16378 -- Save_References_In_Descendants --
16379 ------------------------------------
16381 procedure Save_References_In_Descendants (N : Node_Id) is
16382 use Atree.Unchecked_Access;
16383 -- This code section is part of implementing an untyped tree
16384 -- traversal, so it needs direct access to node fields.
16387 Save_Global_Descendant (Field1 (N));
16388 Save_Global_Descendant (Field2 (N));
16389 Save_Global_Descendant (Field3 (N));
16390 Save_Global_Descendant (Field4 (N));
16391 Save_Global_Descendant (Field5 (N));
16392 end Save_References_In_Descendants;
16394 -----------------------------------
16395 -- Save_References_In_Identifier --
16396 -----------------------------------
16398 procedure Save_References_In_Identifier (N : Node_Id) is
16400 -- The node did not undergo a transformation
16402 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16403 -- If this is a discriminant reference, always save it.
16404 -- It is used in the instance to find the corresponding
16405 -- discriminant positionally rather than by name.
16407 Set_Original_Discriminant
16408 (N, Original_Discriminant (Get_Associated_Node (N)));
16412 -- The analysis of the generic copy transformed the identifier
16413 -- into another construct. Propagate the changes to the template.
16416 N2 := Get_Associated_Node (N);
16418 -- The identifier denotes a call to a parameterless function.
16419 -- Mark the node as resolved when the function is external.
16421 if Nkind (N2) = N_Function_Call then
16422 E := Entity (Name (N2));
16424 if Present (E) and then Is_Global (E) then
16425 Set_Etype (N, Etype (N2));
16427 Set_Associated_Node (N, Empty);
16428 Set_Etype (N, Empty);
16431 -- The identifier denotes a named number that was constant
16432 -- folded. Preserve the original name for ASIS and undo the
16433 -- constant folding which will be repeated in the instance.
16434 -- Is this still needed???
16436 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16437 and then Is_Entity_Name (Original_Node (N2))
16439 Set_Associated_Node (N, Original_Node (N2));
16442 -- The identifier resolved to a string literal. Propagate this
16443 -- information to the generic template.
16445 elsif Nkind (N2) = N_String_Literal then
16446 Rewrite (N, New_Copy (N2));
16448 -- The identifier is rewritten as a dereference if it is the
16449 -- prefix of an implicit dereference. Preserve the original
16450 -- tree as the analysis of the instance will expand the node
16451 -- again, but preserve the resolved entity if it is global.
16453 elsif Nkind (N2) = N_Explicit_Dereference then
16454 if Is_Entity_Name (Prefix (N2))
16455 and then Present (Entity (Prefix (N2)))
16456 and then Is_Global (Entity (Prefix (N2)))
16458 Set_Associated_Node (N, Prefix (N2));
16460 elsif Nkind (Prefix (N2)) = N_Function_Call
16461 and then Present (Entity (Name (Prefix (N2))))
16462 and then Is_Global (Entity (Name (Prefix (N2))))
16465 Make_Explicit_Dereference (Loc,
16467 Make_Function_Call (Loc,
16470 (Entity (Name (Prefix (N2))), Loc))));
16473 Set_Associated_Node (N, Empty);
16474 Set_Etype (N, Empty);
16477 -- The subtype mark of a nominally unconstrained object is
16478 -- rewritten as a subtype indication using the bounds of the
16479 -- expression. Recover the original subtype mark.
16481 elsif Nkind (N2) = N_Subtype_Indication
16482 and then Is_Entity_Name (Original_Node (N2))
16484 Set_Associated_Node (N, Original_Node (N2));
16488 end Save_References_In_Identifier;
16490 ---------------------------------
16491 -- Save_References_In_Operator --
16492 ---------------------------------
16494 procedure Save_References_In_Operator (N : Node_Id) is
16496 -- The node did not undergo a transformation
16498 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16499 if Nkind (N) = N_Op_Concat then
16500 Set_Is_Component_Left_Opnd (N,
16501 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16503 Set_Is_Component_Right_Opnd (N,
16504 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16509 -- The analysis of the generic copy transformed the operator into
16510 -- some other construct. Propagate the changes to the template if
16514 N2 := Get_Associated_Node (N);
16516 -- The operator resoved to a function call
16518 if Nkind (N2) = N_Function_Call then
16520 -- Add explicit qualifications in the generic template for
16521 -- all operands of universal type. This aids resolution by
16522 -- preserving the actual type of a literal or an attribute
16523 -- that yields a universal result.
16525 Qualify_Universal_Operands (N, N2);
16527 E := Entity (Name (N2));
16529 if Present (E) and then Is_Global (E) then
16530 Set_Etype (N, Etype (N2));
16532 Set_Associated_Node (N, Empty);
16533 Set_Etype (N, Empty);
16536 -- The operator was folded into a literal
16538 elsif Nkind (N2) in N_Integer_Literal
16542 if Present (Original_Node (N2))
16543 and then Nkind (Original_Node (N2)) = Nkind (N)
16545 -- Operation was constant-folded. Whenever possible,
16546 -- recover semantic information from unfolded node.
16547 -- This was initially done for ASIS but is apparently
16548 -- needed also for e.g. compiling a-nbnbin.adb.
16550 Set_Associated_Node (N, Original_Node (N2));
16552 if Nkind (N) = N_Op_Concat then
16553 Set_Is_Component_Left_Opnd (N,
16554 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16555 Set_Is_Component_Right_Opnd (N,
16556 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16561 -- Propagate the constant folding back to the template
16564 Rewrite (N, New_Copy (N2));
16565 Set_Analyzed (N, False);
16568 -- The operator was folded into an enumeration literal. Retain
16569 -- the entity to avoid spurious ambiguities if it is overloaded
16570 -- at the point of instantiation or inlining.
16572 elsif Nkind (N2) = N_Identifier
16573 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16575 Rewrite (N, New_Copy (N2));
16576 Set_Analyzed (N, False);
16580 -- Complete the operands check if node has not been constant
16583 if Nkind (N) in N_Op then
16584 Save_Entity_Descendants (N);
16586 end Save_References_In_Operator;
16588 -------------------------------
16589 -- Save_References_In_Pragma --
16590 -------------------------------
16592 procedure Save_References_In_Pragma (Prag : Node_Id) is
16594 Do_Save : Boolean := True;
16596 use Atree.Unchecked_Access;
16597 -- This code section is part of implementing an untyped tree
16598 -- traversal, so it needs direct access to node fields.
16601 -- Do not save global references in pragmas generated from aspects
16602 -- because the pragmas will be regenerated at instantiation time.
16604 if From_Aspect_Specification (Prag) then
16607 -- The capture of global references within contract-related source
16608 -- pragmas associated with generic packages, subprograms or their
16609 -- respective bodies must be delayed due to timing of annotation
16610 -- analysis. Global references are still captured in routine
16611 -- Save_Global_References_In_Contract.
16613 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16614 if Is_Package_Contract_Annotation (Prag) then
16615 Context := Find_Related_Package_Or_Body (Prag);
16617 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16618 Context := Find_Related_Declaration_Or_Body (Prag);
16621 -- The use of Original_Node accounts for the case when the
16622 -- related context is generic template.
16624 if Requires_Delayed_Save (Original_Node (Context)) then
16629 -- For all other cases, save all global references within the
16630 -- descendants, but skip the following semantic fields:
16632 -- Field1 - Next_Pragma
16633 -- Field3 - Corresponding_Aspect
16634 -- Field5 - Next_Rep_Item
16637 Save_Global_Descendant (Field2 (Prag));
16638 Save_Global_Descendant (Field4 (Prag));
16640 end Save_References_In_Pragma;
16642 -- Start of processing for Save_References
16650 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16651 Save_References_In_Aggregate (N);
16653 -- Character literals, operator symbols
16655 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16656 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16658 -- Defining identifiers
16660 elsif Nkind (N) in N_Entity then
16665 elsif Nkind (N) = N_Identifier then
16666 Save_References_In_Identifier (N);
16670 elsif Nkind (N) in N_Op then
16671 Save_References_In_Operator (N);
16675 elsif Nkind (N) = N_Pragma then
16676 Save_References_In_Pragma (N);
16679 Save_References_In_Descendants (N);
16682 -- Save all global references found within the aspect specifications
16683 -- of the related node.
16685 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16687 -- The capture of global references within aspects associated with
16688 -- generic packages, subprograms or their bodies must be delayed
16689 -- due to timing of annotation analysis. Global references are
16690 -- still captured in routine Save_Global_References_In_Contract.
16692 if Requires_Delayed_Save (N) then
16695 -- Otherwise save all global references within the aspects
16698 Save_Global_References_In_Aspects (N);
16701 end Save_References;
16703 -- Start of processing for Save_Global_References
16706 Gen_Scope := Current_Scope;
16708 -- If the generic unit is a child unit, references to entities in the
16709 -- parent are treated as local, because they will be resolved anew in
16710 -- the context of the instance of the parent.
16712 while Is_Child_Unit (Gen_Scope)
16713 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16715 Gen_Scope := Scope (Gen_Scope);
16718 Save_References (Templ);
16719 end Save_Global_References;
16721 ---------------------------------------
16722 -- Save_Global_References_In_Aspects --
16723 ---------------------------------------
16725 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16730 Asp := First (Aspect_Specifications (N));
16731 while Present (Asp) loop
16732 Expr := Expression (Asp);
16734 if Present (Expr) then
16735 Save_Global_References (Expr);
16740 end Save_Global_References_In_Aspects;
16742 ------------------------------------------
16743 -- Set_Copied_Sloc_For_Inherited_Pragma --
16744 ------------------------------------------
16746 procedure Set_Copied_Sloc_For_Inherited_Pragma
16751 Create_Instantiation_Source (N, E,
16752 Inlined_Body => False,
16753 Inherited_Pragma => True,
16754 Factor => S_Adjustment);
16755 end Set_Copied_Sloc_For_Inherited_Pragma;
16757 --------------------------------------
16758 -- Set_Copied_Sloc_For_Inlined_Body --
16759 --------------------------------------
16761 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16763 Create_Instantiation_Source (N, E,
16764 Inlined_Body => True,
16765 Inherited_Pragma => False,
16766 Factor => S_Adjustment);
16767 end Set_Copied_Sloc_For_Inlined_Body;
16769 ---------------------
16770 -- Set_Instance_Of --
16771 ---------------------
16773 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16775 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16776 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16777 Generic_Renamings.Increment_Last;
16778 end Set_Instance_Of;
16780 --------------------
16781 -- Set_Next_Assoc --
16782 --------------------
16784 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16786 Generic_Renamings.Table (E).Next_In_HTable := Next;
16787 end Set_Next_Assoc;
16789 -------------------
16790 -- Start_Generic --
16791 -------------------
16793 procedure Start_Generic is
16795 -- ??? More things could be factored out in this routine.
16796 -- Should probably be done at a later stage.
16798 Generic_Flags.Append (Inside_A_Generic);
16799 Inside_A_Generic := True;
16801 Expander_Mode_Save_And_Set (False);
16804 ----------------------
16805 -- Set_Instance_Env --
16806 ----------------------
16808 -- WARNING: This routine manages SPARK regions
16810 procedure Set_Instance_Env
16811 (Gen_Unit : Entity_Id;
16812 Act_Unit : Entity_Id)
16814 Saved_AE : constant Boolean := Assertions_Enabled;
16815 Saved_CPL : constant Node_Id := Check_Policy_List;
16816 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16817 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16818 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16821 -- Regardless of the current mode, predefined units are analyzed in the
16822 -- most current Ada mode, and earlier version Ada checks do not apply
16823 -- to predefined units. Nothing needs to be done for non-internal units.
16824 -- These are always analyzed in the current mode.
16826 if In_Internal_Unit (Gen_Unit) then
16828 -- The following call resets all configuration attributes to default
16829 -- or the xxx_Config versions of the attributes when the current sem
16830 -- unit is the main unit. At the same time, internal units must also
16831 -- inherit certain configuration attributes from their context. It
16832 -- is unclear what these two sets are.
16834 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16836 -- Reinstall relevant configuration attributes of the context
16838 Assertions_Enabled := Saved_AE;
16839 Check_Policy_List := Saved_CPL;
16840 Dynamic_Elaboration_Checks := Saved_DEC;
16842 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16845 Current_Instantiated_Parent :=
16846 (Gen_Id => Gen_Unit,
16847 Act_Id => Act_Unit,
16848 Next_In_HTable => Assoc_Null);
16849 end Set_Instance_Env;
16855 procedure Switch_View (T : Entity_Id) is
16856 BT : constant Entity_Id := Base_Type (T);
16857 Priv_Elmt : Elmt_Id := No_Elmt;
16858 Priv_Sub : Entity_Id;
16861 -- T may be private but its base type may have been exchanged through
16862 -- some other occurrence, in which case there is nothing to switch
16863 -- besides T itself. Note that a private dependent subtype of a private
16864 -- type might not have been switched even if the base type has been,
16865 -- because of the last branch of Check_Private_View (see comment there).
16867 if not Is_Private_Type (BT) then
16868 Prepend_Elmt (Full_View (T), Exchanged_Views);
16869 Exchange_Declarations (T);
16873 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16875 if Present (Full_View (BT)) then
16876 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16877 Exchange_Declarations (BT);
16880 while Present (Priv_Elmt) loop
16881 Priv_Sub := Node (Priv_Elmt);
16883 if Present (Full_View (Priv_Sub)) then
16884 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16885 Exchange_Declarations (Priv_Sub);
16888 Next_Elmt (Priv_Elmt);
16896 function True_Parent (N : Node_Id) return Node_Id is
16898 if Nkind (Parent (N)) = N_Subunit then
16899 return Parent (Corresponding_Stub (Parent (N)));
16905 -----------------------------
16906 -- Valid_Default_Attribute --
16907 -----------------------------
16909 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16910 Attr_Id : constant Attribute_Id :=
16911 Get_Attribute_Id (Attribute_Name (Def));
16912 T : constant Entity_Id := Entity (Prefix (Def));
16913 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16919 if No (T) or else T = Any_Id then
16924 F := First_Formal (Nam);
16925 while Present (F) loop
16926 Num_F := Num_F + 1;
16931 when Attribute_Adjacent
16932 | Attribute_Ceiling
16933 | Attribute_Copy_Sign
16935 | Attribute_Fraction
16936 | Attribute_Machine
16938 | Attribute_Remainder
16939 | Attribute_Rounding
16940 | Attribute_Unbiased_Rounding
16944 and then Is_Floating_Point_Type (T);
16946 when Attribute_Image
16950 | Attribute_Wide_Image
16951 | Attribute_Wide_Value
16953 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16958 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16960 when Attribute_Input =>
16961 OK := (Is_Fun and then Num_F = 1);
16963 when Attribute_Output
16964 | Attribute_Put_Image
16968 OK := not Is_Fun and then Num_F = 2;
16976 ("attribute reference has wrong profile for subprogram", Def);
16978 end Valid_Default_Attribute;