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;
2105 P := Parent (I_Node);
2106 while Nkind (P) /= N_Compilation_Unit loop
2107 if Nkind (P) = N_Handled_Sequence_Of_Statements
2109 Needs_Freezing := False;
2116 if Needs_Freezing then
2117 Check_Generic_Parent;
2119 -- If the actual is a renaming of a proper
2120 -- instance of the formal package, indicate
2121 -- that it is the instance that must be frozen.
2123 if Nkind (Parent (Actual)) =
2124 N_Package_Renaming_Declaration
2126 Set_Has_Delayed_Freeze
2127 (Renamed_Entity (Actual));
2129 (Renamed_Entity (Actual),
2132 Set_Has_Delayed_Freeze (Actual);
2133 Append_Elmt (Actual, Actuals_To_Freeze);
2137 end Explicit_Freeze_Check;
2140 -- For use type and use package appearing in the generic part,
2141 -- we have already copied them, so we can just move them where
2142 -- they belong (we mustn't recopy them since this would mess up
2143 -- the Sloc values).
2145 when N_Use_Package_Clause
2148 if Nkind (Original_Node (I_Node)) =
2149 N_Formal_Package_Declaration
2151 Append (New_Copy_Tree (Formal), Assoc_List);
2154 Append (Formal, Assoc_List);
2158 raise Program_Error;
2161 Formal := Saved_Formal;
2162 Next_Non_Pragma (Analyzed_Formal);
2165 if Num_Actuals > Num_Matched then
2166 Error_Msg_Sloc := Sloc (Gen_Unit);
2168 if Present (Selector_Name (Actual)) then
2170 ("unmatched actual &", Actual, Selector_Name (Actual));
2172 ("\in instantiation of & declared#", Actual, Gen_Unit);
2175 ("unmatched actual in instantiation of & declared#",
2180 elsif Present (Actuals) then
2182 ("too many actuals in generic instantiation", Instantiation_Node);
2185 -- An instantiation freezes all generic actuals. The only exceptions
2186 -- to this are incomplete types and subprograms which are not fully
2187 -- defined at the point of instantiation.
2190 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2192 while Present (Elmt) loop
2193 Freeze_Before (I_Node, Node (Elmt));
2198 -- If there are default subprograms, normalize the tree by adding
2199 -- explicit associations for them. This is required if the instance
2200 -- appears within a generic.
2202 if not Is_Empty_List (Default_Actuals) then
2207 Default := First (Default_Actuals);
2208 while Present (Default) loop
2209 Mark_Rewrite_Insertion (Default);
2213 if No (Actuals) then
2214 Set_Generic_Associations (I_Node, Default_Actuals);
2216 Append_List_To (Actuals, Default_Actuals);
2221 -- If this is a formal package, normalize the parameter list by adding
2222 -- explicit box associations for the formals that are covered by an
2225 if not Is_Empty_List (Default_Formals) then
2226 Append_List (Default_Formals, Formals);
2230 end Analyze_Associations;
2232 -------------------------------
2233 -- Analyze_Formal_Array_Type --
2234 -------------------------------
2236 procedure Analyze_Formal_Array_Type
2237 (T : in out Entity_Id;
2243 -- Treated like a non-generic array declaration, with additional
2248 if Nkind (Def) = N_Constrained_Array_Definition then
2249 DSS := First (Discrete_Subtype_Definitions (Def));
2250 while Present (DSS) loop
2251 if Nkind (DSS) in N_Subtype_Indication
2253 | N_Attribute_Reference
2255 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2262 Array_Type_Declaration (T, Def);
2263 Set_Is_Generic_Type (Base_Type (T));
2265 if Ekind (Component_Type (T)) = E_Incomplete_Type
2266 and then No (Full_View (Component_Type (T)))
2268 Error_Msg_N ("premature usage of incomplete type", Def);
2270 -- Check that range constraint is not allowed on the component type
2271 -- of a generic formal array type (AARM 12.5.3(3))
2273 elsif Is_Internal (Component_Type (T))
2274 and then Present (Subtype_Indication (Component_Definition (Def)))
2275 and then Nkind (Original_Node
2276 (Subtype_Indication (Component_Definition (Def)))) =
2277 N_Subtype_Indication
2280 ("in a formal, a subtype indication can only be "
2281 & "a subtype mark (RM 12.5.3(3))",
2282 Subtype_Indication (Component_Definition (Def)));
2285 end Analyze_Formal_Array_Type;
2287 ---------------------------------------------
2288 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2289 ---------------------------------------------
2291 -- As for other generic types, we create a valid type representation with
2292 -- legal but arbitrary attributes, whose values are never considered
2293 -- static. For all scalar types we introduce an anonymous base type, with
2294 -- the same attributes. We choose the corresponding integer type to be
2295 -- Standard_Integer.
2296 -- Here and in other similar routines, the Sloc of the generated internal
2297 -- type must be the same as the sloc of the defining identifier of the
2298 -- formal type declaration, to provide proper source navigation.
2300 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2304 Loc : constant Source_Ptr := Sloc (Def);
2306 Base : constant Entity_Id :=
2308 (E_Decimal_Fixed_Point_Type,
2310 Sloc (Defining_Identifier (Parent (Def))), 'G');
2312 Int_Base : constant Entity_Id := Standard_Integer;
2313 Delta_Val : constant Ureal := Ureal_1;
2314 Digs_Val : constant Uint := Uint_6;
2316 function Make_Dummy_Bound return Node_Id;
2317 -- Return a properly typed universal real literal to use as a bound
2319 ----------------------
2320 -- Make_Dummy_Bound --
2321 ----------------------
2323 function Make_Dummy_Bound return Node_Id is
2324 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2326 Set_Etype (Bound, Universal_Real);
2328 end Make_Dummy_Bound;
2330 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2335 Set_Etype (Base, Base);
2336 Set_Size_Info (Base, Int_Base);
2337 Set_RM_Size (Base, RM_Size (Int_Base));
2338 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2339 Set_Digits_Value (Base, Digs_Val);
2340 Set_Delta_Value (Base, Delta_Val);
2341 Set_Small_Value (Base, Delta_Val);
2342 Set_Scalar_Range (Base,
2344 Low_Bound => Make_Dummy_Bound,
2345 High_Bound => Make_Dummy_Bound));
2347 Set_Is_Generic_Type (Base);
2348 Set_Parent (Base, Parent (Def));
2350 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2351 Set_Etype (T, Base);
2352 Set_Size_Info (T, Int_Base);
2353 Set_RM_Size (T, RM_Size (Int_Base));
2354 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2355 Set_Digits_Value (T, Digs_Val);
2356 Set_Delta_Value (T, Delta_Val);
2357 Set_Small_Value (T, Delta_Val);
2358 Set_Scalar_Range (T, Scalar_Range (Base));
2359 Set_Is_Constrained (T);
2361 Check_Restriction (No_Fixed_Point, Def);
2362 end Analyze_Formal_Decimal_Fixed_Point_Type;
2364 -------------------------------------------
2365 -- Analyze_Formal_Derived_Interface_Type --
2366 -------------------------------------------
2368 procedure Analyze_Formal_Derived_Interface_Type
2373 Loc : constant Source_Ptr := Sloc (Def);
2376 -- Rewrite as a type declaration of a derived type. This ensures that
2377 -- the interface list and primitive operations are properly captured.
2380 Make_Full_Type_Declaration (Loc,
2381 Defining_Identifier => T,
2382 Type_Definition => Def));
2384 Set_Is_Generic_Type (T);
2385 end Analyze_Formal_Derived_Interface_Type;
2387 ---------------------------------
2388 -- Analyze_Formal_Derived_Type --
2389 ---------------------------------
2391 procedure Analyze_Formal_Derived_Type
2396 Loc : constant Source_Ptr := Sloc (Def);
2397 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2401 Set_Is_Generic_Type (T);
2403 if Private_Present (Def) then
2405 Make_Private_Extension_Declaration (Loc,
2406 Defining_Identifier => T,
2407 Discriminant_Specifications => Discriminant_Specifications (N),
2408 Unknown_Discriminants_Present => Unk_Disc,
2409 Subtype_Indication => Subtype_Mark (Def),
2410 Interface_List => Interface_List (Def));
2412 Set_Abstract_Present (New_N, Abstract_Present (Def));
2413 Set_Limited_Present (New_N, Limited_Present (Def));
2414 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2418 Make_Full_Type_Declaration (Loc,
2419 Defining_Identifier => T,
2420 Discriminant_Specifications =>
2421 Discriminant_Specifications (Parent (T)),
2423 Make_Derived_Type_Definition (Loc,
2424 Subtype_Indication => Subtype_Mark (Def)));
2426 Set_Abstract_Present
2427 (Type_Definition (New_N), Abstract_Present (Def));
2429 (Type_Definition (New_N), Limited_Present (Def));
2436 if not Is_Composite_Type (T) then
2438 ("unknown discriminants not allowed for elementary types", N);
2440 Set_Has_Unknown_Discriminants (T);
2441 Set_Is_Constrained (T, False);
2445 -- If the parent type has a known size, so does the formal, which makes
2446 -- legal representation clauses that involve the formal.
2448 Set_Size_Known_At_Compile_Time
2449 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2450 end Analyze_Formal_Derived_Type;
2452 ----------------------------------
2453 -- Analyze_Formal_Discrete_Type --
2454 ----------------------------------
2456 -- The operations defined for a discrete types are those of an enumeration
2457 -- type. The size is set to an arbitrary value, for use in analyzing the
2460 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2461 Loc : constant Source_Ptr := Sloc (Def);
2465 Base : constant Entity_Id :=
2467 (E_Floating_Point_Type, Current_Scope,
2468 Sloc (Defining_Identifier (Parent (Def))), 'G');
2472 Set_Ekind (T, E_Enumeration_Subtype);
2473 Set_Etype (T, Base);
2476 Set_Is_Generic_Type (T);
2477 Set_Is_Constrained (T);
2479 -- For semantic analysis, the bounds of the type must be set to some
2480 -- non-static value. The simplest is to create attribute nodes for those
2481 -- bounds, that refer to the type itself. These bounds are never
2482 -- analyzed but serve as place-holders.
2485 Make_Attribute_Reference (Loc,
2486 Attribute_Name => Name_First,
2487 Prefix => New_Occurrence_Of (T, Loc));
2491 Make_Attribute_Reference (Loc,
2492 Attribute_Name => Name_Last,
2493 Prefix => New_Occurrence_Of (T, Loc));
2496 Set_Scalar_Range (T,
2501 Set_Ekind (Base, E_Enumeration_Type);
2502 Set_Etype (Base, Base);
2503 Init_Size (Base, 8);
2504 Init_Alignment (Base);
2505 Set_Is_Generic_Type (Base);
2506 Set_Scalar_Range (Base, Scalar_Range (T));
2507 Set_Parent (Base, Parent (Def));
2508 end Analyze_Formal_Discrete_Type;
2510 ----------------------------------
2511 -- Analyze_Formal_Floating_Type --
2512 ---------------------------------
2514 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2515 Base : constant Entity_Id :=
2517 (E_Floating_Point_Type, Current_Scope,
2518 Sloc (Defining_Identifier (Parent (Def))), 'G');
2521 -- The various semantic attributes are taken from the predefined type
2522 -- Float, just so that all of them are initialized. Their values are
2523 -- never used because no constant folding or expansion takes place in
2524 -- the generic itself.
2527 Set_Ekind (T, E_Floating_Point_Subtype);
2528 Set_Etype (T, Base);
2529 Set_Size_Info (T, (Standard_Float));
2530 Set_RM_Size (T, RM_Size (Standard_Float));
2531 Set_Digits_Value (T, Digits_Value (Standard_Float));
2532 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2533 Set_Is_Constrained (T);
2535 Set_Is_Generic_Type (Base);
2536 Set_Etype (Base, Base);
2537 Set_Size_Info (Base, (Standard_Float));
2538 Set_RM_Size (Base, RM_Size (Standard_Float));
2539 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2540 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2541 Set_Parent (Base, Parent (Def));
2543 Check_Restriction (No_Floating_Point, Def);
2544 end Analyze_Formal_Floating_Type;
2546 -----------------------------------
2547 -- Analyze_Formal_Interface_Type;--
2548 -----------------------------------
2550 procedure Analyze_Formal_Interface_Type
2555 Loc : constant Source_Ptr := Sloc (N);
2560 Make_Full_Type_Declaration (Loc,
2561 Defining_Identifier => T,
2562 Type_Definition => Def);
2566 Set_Is_Generic_Type (T);
2567 end Analyze_Formal_Interface_Type;
2569 ---------------------------------
2570 -- Analyze_Formal_Modular_Type --
2571 ---------------------------------
2573 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2575 -- Apart from their entity kind, generic modular types are treated like
2576 -- signed integer types, and have the same attributes.
2578 Analyze_Formal_Signed_Integer_Type (T, Def);
2579 Set_Ekind (T, E_Modular_Integer_Subtype);
2580 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2582 end Analyze_Formal_Modular_Type;
2584 ---------------------------------------
2585 -- Analyze_Formal_Object_Declaration --
2586 ---------------------------------------
2588 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2589 E : constant Node_Id := Default_Expression (N);
2590 Id : constant Node_Id := Defining_Identifier (N);
2597 -- Determine the mode of the formal object
2599 if Out_Present (N) then
2600 K := E_Generic_In_Out_Parameter;
2602 if not In_Present (N) then
2603 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2607 K := E_Generic_In_Parameter;
2610 if Present (Subtype_Mark (N)) then
2611 Find_Type (Subtype_Mark (N));
2612 T := Entity (Subtype_Mark (N));
2614 -- Verify that there is no redundant null exclusion
2616 if Null_Exclusion_Present (N) then
2617 if not Is_Access_Type (T) then
2619 ("null exclusion can only apply to an access type", N);
2621 elsif Can_Never_Be_Null (T) then
2623 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2627 -- Ada 2005 (AI-423): Formal object with an access definition
2630 Check_Access_Definition (N);
2631 T := Access_Definition
2633 N => Access_Definition (N));
2636 if Ekind (T) = E_Incomplete_Type then
2638 Error_Node : Node_Id;
2641 if Present (Subtype_Mark (N)) then
2642 Error_Node := Subtype_Mark (N);
2644 Check_Access_Definition (N);
2645 Error_Node := Access_Definition (N);
2648 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2652 if K = E_Generic_In_Parameter then
2654 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2656 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2658 ("generic formal of mode IN must not be of limited type", N);
2659 Explain_Limited_Type (T, N);
2662 if Is_Abstract_Type (T) then
2664 ("generic formal of mode IN must not be of abstract type", N);
2668 Preanalyze_Spec_Expression (E, T);
2670 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2672 ("initialization not allowed for limited types", E);
2673 Explain_Limited_Type (T, E);
2680 -- Case of generic IN OUT parameter
2683 -- If the formal has an unconstrained type, construct its actual
2684 -- subtype, as is done for subprogram formals. In this fashion, all
2685 -- its uses can refer to specific bounds.
2690 if (Is_Array_Type (T) and then not Is_Constrained (T))
2691 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2694 Non_Freezing_Ref : constant Node_Id :=
2695 New_Occurrence_Of (Id, Sloc (Id));
2699 -- Make sure the actual subtype doesn't generate bogus freezing
2701 Set_Must_Not_Freeze (Non_Freezing_Ref);
2702 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2703 Insert_Before_And_Analyze (N, Decl);
2704 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2707 Set_Actual_Subtype (Id, T);
2712 ("initialization not allowed for `IN OUT` formals", N);
2716 if Has_Aspects (N) then
2717 Analyze_Aspect_Specifications (N, Id);
2719 end Analyze_Formal_Object_Declaration;
2721 ----------------------------------------------
2722 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2723 ----------------------------------------------
2725 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2729 Loc : constant Source_Ptr := Sloc (Def);
2730 Base : constant Entity_Id :=
2732 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2733 Sloc (Defining_Identifier (Parent (Def))), 'G');
2736 -- The semantic attributes are set for completeness only, their values
2737 -- will never be used, since all properties of the type are non-static.
2740 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2741 Set_Etype (T, Base);
2742 Set_Size_Info (T, Standard_Integer);
2743 Set_RM_Size (T, RM_Size (Standard_Integer));
2744 Set_Small_Value (T, Ureal_1);
2745 Set_Delta_Value (T, Ureal_1);
2746 Set_Scalar_Range (T,
2748 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2749 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2750 Set_Is_Constrained (T);
2752 Set_Is_Generic_Type (Base);
2753 Set_Etype (Base, Base);
2754 Set_Size_Info (Base, Standard_Integer);
2755 Set_RM_Size (Base, RM_Size (Standard_Integer));
2756 Set_Small_Value (Base, Ureal_1);
2757 Set_Delta_Value (Base, Ureal_1);
2758 Set_Scalar_Range (Base, Scalar_Range (T));
2759 Set_Parent (Base, Parent (Def));
2761 Check_Restriction (No_Fixed_Point, Def);
2762 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2764 ----------------------------------------
2765 -- Analyze_Formal_Package_Declaration --
2766 ----------------------------------------
2768 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2769 Gen_Id : constant Node_Id := Name (N);
2770 Loc : constant Source_Ptr := Sloc (N);
2771 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2774 Gen_Unit : Entity_Id;
2777 Vis_Prims_List : Elist_Id := No_Elist;
2778 -- List of primitives made temporarily visible in the instantiation
2779 -- to match the visibility of the formal type.
2781 function Build_Local_Package return Node_Id;
2782 -- The formal package is rewritten so that its parameters are replaced
2783 -- with corresponding declarations. For parameters with bona fide
2784 -- associations these declarations are created by Analyze_Associations
2785 -- as for a regular instantiation. For boxed parameters, we preserve
2786 -- the formal declarations and analyze them, in order to introduce
2787 -- entities of the right kind in the environment of the formal.
2789 -------------------------
2790 -- Build_Local_Package --
2791 -------------------------
2793 function Build_Local_Package return Node_Id is
2795 Pack_Decl : Node_Id;
2798 -- Within the formal, the name of the generic package is a renaming
2799 -- of the formal (as for a regular instantiation).
2802 Make_Package_Declaration (Loc,
2805 (Specification (Original_Node (Gen_Decl)),
2806 Empty, Instantiating => True));
2809 Make_Package_Renaming_Declaration (Loc,
2810 Defining_Unit_Name =>
2811 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2812 Name => New_Occurrence_Of (Formal, Loc));
2814 if Nkind (Gen_Id) = N_Identifier
2815 and then Chars (Gen_Id) = Chars (Pack_Id)
2818 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2821 -- If the formal is declared with a box, or with an others choice,
2822 -- create corresponding declarations for all entities in the formal
2823 -- part, so that names with the proper types are available in the
2824 -- specification of the formal package.
2826 -- On the other hand, if there are no associations, then all the
2827 -- formals must have defaults, and this will be checked by the
2828 -- call to Analyze_Associations.
2831 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2834 Formal_Decl : Node_Id;
2837 -- TBA : for a formal package, need to recurse ???
2842 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2843 while Present (Formal_Decl) loop
2847 (Formal_Decl, Empty, Instantiating => True));
2852 -- If generic associations are present, use Analyze_Associations to
2853 -- create the proper renaming declarations.
2857 Act_Tree : constant Node_Id :=
2859 (Original_Node (Gen_Decl), Empty,
2860 Instantiating => True);
2863 Generic_Renamings.Set_Last (0);
2864 Generic_Renamings_HTable.Reset;
2865 Instantiation_Node := N;
2868 Analyze_Associations
2869 (I_Node => Original_Node (N),
2870 Formals => Generic_Formal_Declarations (Act_Tree),
2871 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2873 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2877 Append (Renaming, To => Decls);
2879 -- Add generated declarations ahead of local declarations in
2882 if No (Visible_Declarations (Specification (Pack_Decl))) then
2883 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2886 (First (Visible_Declarations (Specification (Pack_Decl))),
2891 end Build_Local_Package;
2895 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2896 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2898 Associations : Boolean := True;
2900 Parent_Installed : Boolean := False;
2901 Parent_Instance : Entity_Id;
2902 Renaming_In_Par : Entity_Id;
2904 -- Start of processing for Analyze_Formal_Package_Declaration
2907 Check_Text_IO_Special_Unit (Gen_Id);
2910 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2911 Gen_Unit := Entity (Gen_Id);
2913 -- Check for a formal package that is a package renaming
2915 if Present (Renamed_Object (Gen_Unit)) then
2917 -- Indicate that unit is used, before replacing it with renamed
2918 -- entity for use below.
2920 if In_Extended_Main_Source_Unit (N) then
2921 Set_Is_Instantiated (Gen_Unit);
2922 Generate_Reference (Gen_Unit, N);
2925 Gen_Unit := Renamed_Object (Gen_Unit);
2928 if Ekind (Gen_Unit) /= E_Generic_Package then
2929 Error_Msg_N ("expect generic package name", Gen_Id);
2933 elsif Gen_Unit = Current_Scope then
2935 ("generic package cannot be used as a formal package of itself",
2940 elsif In_Open_Scopes (Gen_Unit) then
2941 if Is_Compilation_Unit (Gen_Unit)
2942 and then Is_Child_Unit (Current_Scope)
2944 -- Special-case the error when the formal is a parent, and
2945 -- continue analysis to minimize cascaded errors.
2948 ("generic parent cannot be used as formal package of a child "
2953 ("generic package cannot be used as a formal package within "
2954 & "itself", Gen_Id);
2960 -- Check that name of formal package does not hide name of generic,
2961 -- or its leading prefix. This check must be done separately because
2962 -- the name of the generic has already been analyzed.
2965 Gen_Name : Entity_Id;
2969 while Nkind (Gen_Name) = N_Expanded_Name loop
2970 Gen_Name := Prefix (Gen_Name);
2973 if Chars (Gen_Name) = Chars (Pack_Id) then
2975 ("& is hidden within declaration of formal package",
2981 or else No (Generic_Associations (N))
2982 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2984 Associations := False;
2987 -- If there are no generic associations, the generic parameters appear
2988 -- as local entities and are instantiated like them. We copy the generic
2989 -- package declaration as if it were an instantiation, and analyze it
2990 -- like a regular package, except that we treat the formals as
2991 -- additional visible components.
2993 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2995 if In_Extended_Main_Source_Unit (N) then
2996 Set_Is_Instantiated (Gen_Unit);
2997 Generate_Reference (Gen_Unit, N);
3000 Formal := New_Copy (Pack_Id);
3001 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3003 -- Make local generic without formals. The formals will be replaced with
3004 -- internal declarations.
3007 New_N := Build_Local_Package;
3009 -- If there are errors in the parameter list, Analyze_Associations
3010 -- raises Instantiation_Error. Patch the declaration to prevent further
3011 -- exception propagation.
3014 when Instantiation_Error =>
3015 Enter_Name (Formal);
3016 Set_Ekind (Formal, E_Variable);
3017 Set_Etype (Formal, Any_Type);
3018 Restore_Hidden_Primitives (Vis_Prims_List);
3020 if Parent_Installed then
3028 Set_Defining_Unit_Name (Specification (New_N), Formal);
3029 Set_Generic_Parent (Specification (N), Gen_Unit);
3030 Set_Instance_Env (Gen_Unit, Formal);
3031 Set_Is_Generic_Instance (Formal);
3033 Enter_Name (Formal);
3034 Set_Ekind (Formal, E_Package);
3035 Set_Etype (Formal, Standard_Void_Type);
3036 Set_Inner_Instances (Formal, New_Elmt_List);
3038 -- It is unclear that any aspects can apply to a formal package
3039 -- declaration, given that they look like a hidden conformance
3040 -- requirement on the corresponding actual. However, Abstract_State
3041 -- must be treated specially because it generates declarations that
3042 -- must appear before other declarations in the specification and
3043 -- must be analyzed at once.
3045 if Present (Aspect_Specifications (Gen_Decl)) then
3046 if No (Aspect_Specifications (N)) then
3047 Set_Aspect_Specifications (N, New_List);
3048 Set_Has_Aspects (N);
3052 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3056 while Present (ASN) loop
3057 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3059 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3060 Set_Entity (New_A, Formal);
3061 Set_Analyzed (New_A, False);
3062 Append (New_A, Aspect_Specifications (N));
3063 Analyze_Aspect_Specifications (N, Formal);
3072 Push_Scope (Formal);
3074 -- Manually set the SPARK_Mode from the context because the package
3075 -- declaration is never analyzed.
3077 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3078 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3079 Set_SPARK_Pragma_Inherited (Formal);
3080 Set_SPARK_Aux_Pragma_Inherited (Formal);
3082 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3084 -- Similarly, we have to make the name of the formal visible in the
3085 -- parent instance, to resolve properly fully qualified names that
3086 -- may appear in the generic unit. The parent instance has been
3087 -- placed on the scope stack ahead of the current scope.
3089 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3092 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3093 Set_Ekind (Renaming_In_Par, E_Package);
3094 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3095 Set_Scope (Renaming_In_Par, Parent_Instance);
3096 Set_Parent (Renaming_In_Par, Parent (Formal));
3097 Set_Renamed_Object (Renaming_In_Par, Formal);
3098 Append_Entity (Renaming_In_Par, Parent_Instance);
3101 -- A formal package declaration behaves as a package instantiation with
3102 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3103 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3104 -- all SPARK_Mode pragmas within the generic_package_name.
3106 if SPARK_Mode /= On then
3107 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3109 -- Mark the formal spec in case the body is instantiated at a later
3110 -- pass. This preserves the original context in effect for the body.
3112 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3115 Analyze (Specification (N));
3117 -- The formals for which associations are provided are not visible
3118 -- outside of the formal package. The others are still declared by a
3119 -- formal parameter declaration.
3121 -- If there are no associations, the only local entity to hide is the
3122 -- generated package renaming itself.
3128 E := First_Entity (Formal);
3129 while Present (E) loop
3130 if Associations and then not Is_Generic_Formal (E) then
3134 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3143 End_Package_Scope (Formal);
3144 Restore_Hidden_Primitives (Vis_Prims_List);
3146 if Parent_Installed then
3152 -- Inside the generic unit, the formal package is a regular package, but
3153 -- no body is needed for it. Note that after instantiation, the defining
3154 -- unit name we need is in the new tree and not in the original (see
3155 -- Package_Instantiation). A generic formal package is an instance, and
3156 -- can be used as an actual for an inner instance.
3158 Set_Has_Completion (Formal, True);
3160 -- Add semantic information to the original defining identifier.
3162 Set_Ekind (Pack_Id, E_Package);
3163 Set_Etype (Pack_Id, Standard_Void_Type);
3164 Set_Scope (Pack_Id, Scope (Formal));
3165 Set_Has_Completion (Pack_Id, True);
3168 if Has_Aspects (N) then
3169 -- Unclear that any other aspects may appear here, snalyze them
3170 -- for completion, given that the grammar allows their appearance.
3172 Analyze_Aspect_Specifications (N, Pack_Id);
3175 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3176 end Analyze_Formal_Package_Declaration;
3178 ---------------------------------
3179 -- Analyze_Formal_Private_Type --
3180 ---------------------------------
3182 procedure Analyze_Formal_Private_Type
3188 New_Private_Type (N, T, Def);
3190 -- Set the size to an arbitrary but legal value
3192 Set_Size_Info (T, Standard_Integer);
3193 Set_RM_Size (T, RM_Size (Standard_Integer));
3194 end Analyze_Formal_Private_Type;
3196 ------------------------------------
3197 -- Analyze_Formal_Incomplete_Type --
3198 ------------------------------------
3200 procedure Analyze_Formal_Incomplete_Type
3206 Set_Ekind (T, E_Incomplete_Type);
3208 Set_Private_Dependents (T, New_Elmt_List);
3210 if Tagged_Present (Def) then
3211 Set_Is_Tagged_Type (T);
3212 Make_Class_Wide_Type (T);
3213 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3215 end Analyze_Formal_Incomplete_Type;
3217 ----------------------------------------
3218 -- Analyze_Formal_Signed_Integer_Type --
3219 ----------------------------------------
3221 procedure Analyze_Formal_Signed_Integer_Type
3225 Base : constant Entity_Id :=
3227 (E_Signed_Integer_Type,
3229 Sloc (Defining_Identifier (Parent (Def))), 'G');
3234 Set_Ekind (T, E_Signed_Integer_Subtype);
3235 Set_Etype (T, Base);
3236 Set_Size_Info (T, Standard_Integer);
3237 Set_RM_Size (T, RM_Size (Standard_Integer));
3238 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3239 Set_Is_Constrained (T);
3241 Set_Is_Generic_Type (Base);
3242 Set_Size_Info (Base, Standard_Integer);
3243 Set_RM_Size (Base, RM_Size (Standard_Integer));
3244 Set_Etype (Base, Base);
3245 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3246 Set_Parent (Base, Parent (Def));
3247 end Analyze_Formal_Signed_Integer_Type;
3249 -------------------------------------------
3250 -- Analyze_Formal_Subprogram_Declaration --
3251 -------------------------------------------
3253 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3254 Spec : constant Node_Id := Specification (N);
3255 Def : constant Node_Id := Default_Name (N);
3256 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3264 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3265 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3269 Analyze_Subprogram_Declaration (N);
3270 Set_Is_Formal_Subprogram (Nam);
3271 Set_Has_Completion (Nam);
3273 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3274 Set_Is_Abstract_Subprogram (Nam);
3276 Set_Is_Dispatching_Operation (Nam);
3278 -- A formal abstract procedure cannot have a null default
3279 -- (RM 12.6(4.1/2)).
3281 if Nkind (Spec) = N_Procedure_Specification
3282 and then Null_Present (Spec)
3285 ("a formal abstract subprogram cannot default to null", Spec);
3289 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3291 if No (Ctrl_Type) then
3293 ("abstract formal subprogram must have a controlling type",
3296 elsif Ada_Version >= Ada_2012
3297 and then Is_Incomplete_Type (Ctrl_Type)
3300 ("controlling type of abstract formal subprogram cannot "
3301 & "be incomplete type", N, Ctrl_Type);
3304 Check_Controlling_Formals (Ctrl_Type, Nam);
3309 -- Default name is resolved at the point of instantiation
3311 if Box_Present (N) then
3314 -- Else default is bound at the point of generic declaration
3316 elsif Present (Def) then
3317 if Nkind (Def) = N_Operator_Symbol then
3318 Find_Direct_Name (Def);
3320 elsif Nkind (Def) /= N_Attribute_Reference then
3324 -- For an attribute reference, analyze the prefix and verify
3325 -- that it has the proper profile for the subprogram.
3327 Analyze (Prefix (Def));
3328 Valid_Default_Attribute (Nam, Def);
3332 -- Default name may be overloaded, in which case the interpretation
3333 -- with the correct profile must be selected, as for a renaming.
3334 -- If the definition is an indexed component, it must denote a
3335 -- member of an entry family. If it is a selected component, it
3336 -- can be a protected operation.
3338 if Etype (Def) = Any_Type then
3341 elsif Nkind (Def) = N_Selected_Component then
3342 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3343 Error_Msg_N ("expect valid subprogram name as default", Def);
3346 elsif Nkind (Def) = N_Indexed_Component then
3347 if Is_Entity_Name (Prefix (Def)) then
3348 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3349 Error_Msg_N ("expect valid subprogram name as default", Def);
3352 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3353 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3356 Error_Msg_N ("expect valid subprogram name as default", Def);
3360 Error_Msg_N ("expect valid subprogram name as default", Def);
3364 elsif Nkind (Def) = N_Character_Literal then
3366 -- Needs some type checks: subprogram should be parameterless???
3368 Resolve (Def, (Etype (Nam)));
3370 elsif not Is_Entity_Name (Def)
3371 or else not Is_Overloadable (Entity (Def))
3373 Error_Msg_N ("expect valid subprogram name as default", Def);
3376 elsif not Is_Overloaded (Def) then
3377 Subp := Entity (Def);
3380 Error_Msg_N ("premature usage of formal subprogram", Def);
3382 elsif not Entity_Matches_Spec (Subp, Nam) then
3383 Error_Msg_N ("no visible entity matches specification", Def);
3386 -- More than one interpretation, so disambiguate as for a renaming
3391 I1 : Interp_Index := 0;
3397 Get_First_Interp (Def, I, It);
3398 while Present (It.Nam) loop
3399 if Entity_Matches_Spec (It.Nam, Nam) then
3400 if Subp /= Any_Id then
3401 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3403 if It1 = No_Interp then
3404 Error_Msg_N ("ambiguous default subprogram", Def);
3417 Get_Next_Interp (I, It);
3421 if Subp /= Any_Id then
3423 -- Subprogram found, generate reference to it
3425 Set_Entity (Def, Subp);
3426 Generate_Reference (Subp, Def);
3429 Error_Msg_N ("premature usage of formal subprogram", Def);
3431 elsif Ekind (Subp) /= E_Operator then
3432 Check_Mode_Conformant (Subp, Nam);
3436 Error_Msg_N ("no visible subprogram matches specification", N);
3442 if Has_Aspects (N) then
3443 Analyze_Aspect_Specifications (N, Nam);
3446 end Analyze_Formal_Subprogram_Declaration;
3448 -------------------------------------
3449 -- Analyze_Formal_Type_Declaration --
3450 -------------------------------------
3452 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3453 Def : constant Node_Id := Formal_Type_Definition (N);
3457 T := Defining_Identifier (N);
3459 if Present (Discriminant_Specifications (N))
3460 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3463 ("discriminants not allowed for this formal type", T);
3466 -- Enter the new name, and branch to specific routine
3469 when N_Formal_Private_Type_Definition =>
3470 Analyze_Formal_Private_Type (N, T, Def);
3472 when N_Formal_Derived_Type_Definition =>
3473 Analyze_Formal_Derived_Type (N, T, Def);
3475 when N_Formal_Incomplete_Type_Definition =>
3476 Analyze_Formal_Incomplete_Type (T, Def);
3478 when N_Formal_Discrete_Type_Definition =>
3479 Analyze_Formal_Discrete_Type (T, Def);
3481 when N_Formal_Signed_Integer_Type_Definition =>
3482 Analyze_Formal_Signed_Integer_Type (T, Def);
3484 when N_Formal_Modular_Type_Definition =>
3485 Analyze_Formal_Modular_Type (T, Def);
3487 when N_Formal_Floating_Point_Definition =>
3488 Analyze_Formal_Floating_Type (T, Def);
3490 when N_Formal_Ordinary_Fixed_Point_Definition =>
3491 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3493 when N_Formal_Decimal_Fixed_Point_Definition =>
3494 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3496 when N_Array_Type_Definition =>
3497 Analyze_Formal_Array_Type (T, Def);
3499 when N_Access_Function_Definition
3500 | N_Access_Procedure_Definition
3501 | N_Access_To_Object_Definition
3503 Analyze_Generic_Access_Type (T, Def);
3505 -- Ada 2005: a interface declaration is encoded as an abstract
3506 -- record declaration or a abstract type derivation.
3508 when N_Record_Definition =>
3509 Analyze_Formal_Interface_Type (N, T, Def);
3511 when N_Derived_Type_Definition =>
3512 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3518 raise Program_Error;
3521 -- A formal type declaration declares a type and its first
3524 Set_Is_Generic_Type (T);
3525 Set_Is_First_Subtype (T);
3527 if Has_Aspects (N) then
3528 Analyze_Aspect_Specifications (N, T);
3530 end Analyze_Formal_Type_Declaration;
3532 ------------------------------------
3533 -- Analyze_Function_Instantiation --
3534 ------------------------------------
3536 procedure Analyze_Function_Instantiation (N : Node_Id) is
3538 Analyze_Subprogram_Instantiation (N, E_Function);
3539 end Analyze_Function_Instantiation;
3541 ---------------------------------
3542 -- Analyze_Generic_Access_Type --
3543 ---------------------------------
3545 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3549 if Nkind (Def) = N_Access_To_Object_Definition then
3550 Access_Type_Declaration (T, Def);
3552 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3553 and then No (Full_View (Designated_Type (T)))
3554 and then not Is_Generic_Type (Designated_Type (T))
3556 Error_Msg_N ("premature usage of incomplete type", Def);
3558 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3560 ("only a subtype mark is allowed in a formal", Def);
3564 Access_Subprogram_Declaration (T, Def);
3566 end Analyze_Generic_Access_Type;
3568 ---------------------------------
3569 -- Analyze_Generic_Formal_Part --
3570 ---------------------------------
3572 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3573 Gen_Parm_Decl : Node_Id;
3576 -- The generic formals are processed in the scope of the generic unit,
3577 -- where they are immediately visible. The scope is installed by the
3580 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3581 while Present (Gen_Parm_Decl) loop
3582 Analyze (Gen_Parm_Decl);
3583 Next (Gen_Parm_Decl);
3586 Generate_Reference_To_Generic_Formals (Current_Scope);
3588 -- For Ada 2020, some formal parameters can carry aspects, which must
3589 -- be name-resolved at the end of the list of formal parameters (which
3590 -- has the semantics of a declaration list).
3592 Analyze_Contracts (Generic_Formal_Declarations (N));
3593 end Analyze_Generic_Formal_Part;
3595 ------------------------------------------
3596 -- Analyze_Generic_Package_Declaration --
3597 ------------------------------------------
3599 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3600 Decls : constant List_Id := Visible_Declarations (Specification (N));
3601 Loc : constant Source_Ptr := Sloc (N);
3607 Save_Parent : Node_Id;
3610 -- A generic may grant access to its private enclosing context depending
3611 -- on the placement of its corresponding body. From elaboration point of
3612 -- view, the flow of execution may enter this private context, and then
3613 -- reach an external unit, thus producing a dependency on that external
3614 -- unit. For such a path to be properly discovered and encoded in the
3615 -- ALI file of the main unit, let the ABE mechanism process the body of
3616 -- the main unit, and encode all relevant invocation constructs and the
3617 -- relations between them.
3619 Mark_Save_Invocation_Graph_Of_Body;
3621 -- We introduce a renaming of the enclosing package, to have a usable
3622 -- entity as the prefix of an expanded name for a local entity of the
3623 -- form Par.P.Q, where P is the generic package. This is because a local
3624 -- entity named P may hide it, so that the usual visibility rules in
3625 -- the instance will not resolve properly.
3628 Make_Package_Renaming_Declaration (Loc,
3629 Defining_Unit_Name =>
3630 Make_Defining_Identifier (Loc,
3631 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3633 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3635 -- The declaration is inserted before other declarations, but before
3636 -- pragmas that may be library-unit pragmas and must appear before other
3637 -- declarations. The pragma Compile_Time_Error is not in this class, and
3638 -- may contain an expression that includes such a qualified name, so the
3639 -- renaming declaration must appear before it.
3641 -- Are there other pragmas that require this special handling ???
3643 if Present (Decls) then
3644 Decl := First (Decls);
3645 while Present (Decl)
3646 and then Nkind (Decl) = N_Pragma
3647 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3652 if Present (Decl) then
3653 Insert_Before (Decl, Renaming);
3655 Append (Renaming, Visible_Declarations (Specification (N)));
3659 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3662 -- Create copy of generic unit, and save for instantiation. If the unit
3663 -- is a child unit, do not copy the specifications for the parent, which
3664 -- are not part of the generic tree.
3666 Save_Parent := Parent_Spec (N);
3667 Set_Parent_Spec (N, Empty);
3669 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3670 Set_Parent_Spec (New_N, Save_Parent);
3673 -- Once the contents of the generic copy and the template are swapped,
3674 -- do the same for their respective aspect specifications.
3676 Exchange_Aspects (N, New_N);
3678 -- Collect all contract-related source pragmas found within the template
3679 -- and attach them to the contract of the package spec. This contract is
3680 -- used in the capture of global references within annotations.
3682 Create_Generic_Contract (N);
3684 Id := Defining_Entity (N);
3685 Generate_Definition (Id);
3687 -- Expansion is not applied to generic units
3692 Set_Ekind (Id, E_Generic_Package);
3693 Set_Etype (Id, Standard_Void_Type);
3695 -- Set SPARK_Mode from context
3697 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3698 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3699 Set_SPARK_Pragma_Inherited (Id);
3700 Set_SPARK_Aux_Pragma_Inherited (Id);
3702 -- Preserve relevant elaboration-related attributes of the context which
3703 -- are no longer available or very expensive to recompute once analysis,
3704 -- resolution, and expansion are over.
3706 Mark_Elaboration_Attributes
3711 -- Analyze aspects now, so that generated pragmas appear in the
3712 -- declarations before building and analyzing the generic copy.
3714 if Has_Aspects (N) then
3715 Analyze_Aspect_Specifications (N, Id);
3719 Enter_Generic_Scope (Id);
3720 Set_Inner_Instances (Id, New_Elmt_List);
3722 Set_Categorization_From_Pragmas (N);
3723 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3725 -- Link the declaration of the generic homonym in the generic copy to
3726 -- the package it renames, so that it is always resolved properly.
3728 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3729 Set_Entity (Associated_Node (Name (Renaming)), Id);
3731 -- For a library unit, we have reconstructed the entity for the unit,
3732 -- and must reset it in the library tables.
3734 if Nkind (Parent (N)) = N_Compilation_Unit then
3735 Set_Cunit_Entity (Current_Sem_Unit, Id);
3738 Analyze_Generic_Formal_Part (N);
3740 -- After processing the generic formals, analysis proceeds as for a
3741 -- non-generic package.
3743 Analyze (Specification (N));
3745 Validate_Categorization_Dependency (N, Id);
3749 End_Package_Scope (Id);
3750 Exit_Generic_Scope (Id);
3752 -- If the generic appears within a package unit, the body of that unit
3753 -- has to be present for instantiation and inlining.
3755 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3756 Set_Body_Needed_For_Inlining
3757 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3760 if Nkind (Parent (N)) /= N_Compilation_Unit then
3761 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3762 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3763 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3766 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3767 Validate_RT_RAT_Component (N);
3769 -- If this is a spec without a body, check that generic parameters
3772 if not Body_Required (Parent (N)) then
3773 Check_References (Id);
3777 -- If there is a specified storage pool in the context, create an
3778 -- aspect on the package declaration, so that it is used in any
3779 -- instance that does not override it.
3781 if Present (Default_Pool) then
3787 Make_Aspect_Specification (Loc,
3788 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3789 Expression => New_Copy (Default_Pool));
3791 if No (Aspect_Specifications (Specification (N))) then
3792 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3794 Append (ASN, Aspect_Specifications (Specification (N)));
3798 end Analyze_Generic_Package_Declaration;
3800 --------------------------------------------
3801 -- Analyze_Generic_Subprogram_Declaration --
3802 --------------------------------------------
3804 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3808 Result_Type : Entity_Id;
3809 Save_Parent : Node_Id;
3814 -- A generic may grant access to its private enclosing context depending
3815 -- on the placement of its corresponding body. From elaboration point of
3816 -- view, the flow of execution may enter this private context, and then
3817 -- reach an external unit, thus producing a dependency on that external
3818 -- unit. For such a path to be properly discovered and encoded in the
3819 -- ALI file of the main unit, let the ABE mechanism process the body of
3820 -- the main unit, and encode all relevant invocation constructs and the
3821 -- relations between them.
3823 Mark_Save_Invocation_Graph_Of_Body;
3825 -- Create copy of generic unit, and save for instantiation. If the unit
3826 -- is a child unit, do not copy the specifications for the parent, which
3827 -- are not part of the generic tree.
3829 Save_Parent := Parent_Spec (N);
3830 Set_Parent_Spec (N, Empty);
3832 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3833 Set_Parent_Spec (New_N, Save_Parent);
3836 -- Once the contents of the generic copy and the template are swapped,
3837 -- do the same for their respective aspect specifications.
3839 Exchange_Aspects (N, New_N);
3841 -- Collect all contract-related source pragmas found within the template
3842 -- and attach them to the contract of the subprogram spec. This contract
3843 -- is used in the capture of global references within annotations.
3845 Create_Generic_Contract (N);
3847 Spec := Specification (N);
3848 Id := Defining_Entity (Spec);
3849 Generate_Definition (Id);
3851 if Nkind (Id) = N_Defining_Operator_Symbol then
3853 ("operator symbol not allowed for generic subprogram", Id);
3859 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3862 Enter_Generic_Scope (Id);
3863 Set_Inner_Instances (Id, New_Elmt_List);
3864 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3866 Analyze_Generic_Formal_Part (N);
3868 if Nkind (Spec) = N_Function_Specification then
3869 Set_Ekind (Id, E_Generic_Function);
3871 Set_Ekind (Id, E_Generic_Procedure);
3874 -- Set SPARK_Mode from context
3876 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3877 Set_SPARK_Pragma_Inherited (Id);
3879 -- Preserve relevant elaboration-related attributes of the context which
3880 -- are no longer available or very expensive to recompute once analysis,
3881 -- resolution, and expansion are over.
3883 Mark_Elaboration_Attributes
3888 Formals := Parameter_Specifications (Spec);
3890 if Present (Formals) then
3891 Process_Formals (Formals, Spec);
3894 if Nkind (Spec) = N_Function_Specification then
3895 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3896 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3897 Set_Etype (Id, Result_Type);
3899 -- Check restriction imposed by AI05-073: a generic function
3900 -- cannot return an abstract type or an access to such.
3902 -- This is a binding interpretation should it apply to earlier
3903 -- versions of Ada as well as Ada 2012???
3905 if Is_Abstract_Type (Designated_Type (Result_Type))
3906 and then Ada_Version >= Ada_2012
3909 ("generic function cannot have an access result "
3910 & "that designates an abstract type", Spec);
3914 Find_Type (Result_Definition (Spec));
3915 Typ := Entity (Result_Definition (Spec));
3917 if Is_Abstract_Type (Typ)
3918 and then Ada_Version >= Ada_2012
3921 ("generic function cannot have abstract result type", Spec);
3924 -- If a null exclusion is imposed on the result type, then create
3925 -- a null-excluding itype (an access subtype) and use it as the
3926 -- function's Etype.
3928 if Is_Access_Type (Typ)
3929 and then Null_Exclusion_Present (Spec)
3932 Create_Null_Excluding_Itype
3934 Related_Nod => Spec,
3935 Scope_Id => Defining_Unit_Name (Spec)));
3937 Set_Etype (Id, Typ);
3942 Set_Etype (Id, Standard_Void_Type);
3945 -- Analyze the aspects of the generic copy to ensure that all generated
3946 -- pragmas (if any) perform their semantic effects.
3948 if Has_Aspects (N) then
3949 Analyze_Aspect_Specifications (N, Id);
3952 -- For a library unit, we have reconstructed the entity for the unit,
3953 -- and must reset it in the library tables. We also make sure that
3954 -- Body_Required is set properly in the original compilation unit node.
3956 if Nkind (Parent (N)) = N_Compilation_Unit then
3957 Set_Cunit_Entity (Current_Sem_Unit, Id);
3958 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3961 -- If the generic appears within a package unit, the body of that unit
3962 -- has to be present for instantiation and inlining.
3964 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3965 and then Unit_Requires_Body (Id)
3967 Set_Body_Needed_For_Inlining
3968 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3971 Set_Categorization_From_Pragmas (N);
3972 Validate_Categorization_Dependency (N, Id);
3974 -- Capture all global references that occur within the profile of the
3975 -- generic subprogram. Aspects are not part of this processing because
3976 -- they must be delayed. If processed now, Save_Global_References will
3977 -- destroy the Associated_Node links and prevent the capture of global
3978 -- references when the contract of the generic subprogram is analyzed.
3980 Save_Global_References (Original_Node (N));
3984 Exit_Generic_Scope (Id);
3985 Generate_Reference_To_Formals (Id);
3987 List_Inherited_Pre_Post_Aspects (Id);
3988 end Analyze_Generic_Subprogram_Declaration;
3990 -----------------------------------
3991 -- Analyze_Package_Instantiation --
3992 -----------------------------------
3994 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3995 -- must be replaced by gotos which jump to the end of the routine in order
3996 -- to restore the Ghost and SPARK modes.
3998 procedure Analyze_Package_Instantiation (N : Node_Id) is
3999 Has_Inline_Always : Boolean := False;
4000 -- Set if the generic unit contains any subprograms with Inline_Always.
4001 -- Only relevant when back-end inlining is not enabled.
4003 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4004 -- Return True if inlining is active and Gen_Unit contains inlined
4005 -- subprograms. In this case, we may either instantiate the body when
4006 -- front-end inlining is enabled, or add a pending instantiation when
4007 -- back-end inlining is enabled. In the former case, this may cause
4008 -- superfluous instantiations, but in either case we need to perform
4009 -- the instantiation of the body in the context of the instance and
4010 -- not in that of the point of inlining.
4012 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4013 -- Return True if Gen_Unit needs to have its body instantiated in the
4014 -- context of N. This in particular excludes generic contexts.
4016 -----------------------
4017 -- Might_Inline_Subp --
4018 -----------------------
4020 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4024 if Inline_Processing_Required then
4025 -- No need to recompute the answer if we know it is positive
4026 -- and back-end inlining is enabled.
4028 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4032 E := First_Entity (Gen_Unit);
4033 while Present (E) loop
4034 if Is_Subprogram (E) and then Is_Inlined (E) then
4035 -- Remember if there are any subprograms with Inline_Always
4037 if Has_Pragma_Inline_Always (E) then
4038 Has_Inline_Always := True;
4041 Set_Is_Inlined (Gen_Unit);
4050 end Might_Inline_Subp;
4052 -------------------------------
4053 -- Needs_Body_Instantiated --
4054 -------------------------------
4056 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4058 -- No need to instantiate bodies in generic units
4060 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4064 -- If the instantiation is in the main unit, then the body is needed
4066 if Is_In_Main_Unit (N) then
4070 -- In GNATprove mode, never instantiate bodies outside of the main
4071 -- unit, as it does not use frontend/backend inlining in the way that
4072 -- GNAT does, so does not benefit from such instantiations. On the
4073 -- contrary, such instantiations may bring artificial constraints,
4074 -- as for example such bodies may require preprocessing.
4076 if GNATprove_Mode then
4080 -- If not, then again no need to instantiate bodies in generic units
4082 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4086 -- Here we have a special handling for back-end inlining: if inline
4087 -- processing is required, then we unconditionally want to have the
4088 -- body instantiated. The reason is that Might_Inline_Subp does not
4089 -- catch all the cases (as it does not recurse into nested packages)
4090 -- so this avoids the need to patch things up afterwards. Moreover,
4091 -- these instantiations are only performed on demand when back-end
4092 -- inlining is enabled, so this causes very little extra work.
4094 if Inline_Processing_Required and then Back_End_Inlining then
4098 -- We want to have the bodies instantiated in non-main units if
4099 -- they might contribute inlined subprograms.
4101 return Might_Inline_Subp (Gen_Unit);
4102 end Needs_Body_Instantiated;
4104 -- Local declarations
4106 Gen_Id : constant Node_Id := Name (N);
4107 Inst_Id : constant Entity_Id := Defining_Entity (N);
4108 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4109 Loc : constant Source_Ptr := Sloc (N);
4111 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4112 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4113 Saved_ISMP : constant Boolean :=
4114 Ignore_SPARK_Mode_Pragmas_In_Instance;
4115 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4116 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4117 -- Save the Ghost and SPARK mode-related data to restore on exit
4119 Saved_Style_Check : constant Boolean := Style_Check;
4120 -- Save style check mode for restore on exit
4123 Act_Decl_Name : Node_Id;
4124 Act_Decl_Id : Entity_Id;
4127 Env_Installed : Boolean := False;
4130 Gen_Unit : Entity_Id;
4131 Inline_Now : Boolean := False;
4132 Needs_Body : Boolean;
4133 Parent_Installed : Boolean := False;
4134 Renaming_List : List_Id;
4135 Unit_Renaming : Node_Id;
4137 Vis_Prims_List : Elist_Id := No_Elist;
4138 -- List of primitives made temporarily visible in the instantiation
4139 -- to match the visibility of the formal type
4141 -- Start of processing for Analyze_Package_Instantiation
4144 -- Preserve relevant elaboration-related attributes of the context which
4145 -- are no longer available or very expensive to recompute once analysis,
4146 -- resolution, and expansion are over.
4148 Mark_Elaboration_Attributes
4155 -- Very first thing: check for Text_IO special unit in case we are
4156 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4158 Check_Text_IO_Special_Unit (Name (N));
4160 -- Make node global for error reporting
4162 Instantiation_Node := N;
4164 -- Case of instantiation of a generic package
4166 if Nkind (N) = N_Package_Instantiation then
4167 Act_Decl_Id := New_Copy (Defining_Entity (N));
4168 Set_Comes_From_Source (Act_Decl_Id, True);
4170 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4172 Make_Defining_Program_Unit_Name (Loc,
4174 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4175 Defining_Identifier => Act_Decl_Id);
4177 Act_Decl_Name := Act_Decl_Id;
4180 -- Case of instantiation of a formal package
4183 Act_Decl_Id := Defining_Identifier (N);
4184 Act_Decl_Name := Act_Decl_Id;
4187 Generate_Definition (Act_Decl_Id);
4188 Set_Ekind (Act_Decl_Id, E_Package);
4190 -- Initialize list of incomplete actuals before analysis
4192 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4194 Preanalyze_Actuals (N, Act_Decl_Id);
4196 -- Turn off style checking in instances. If the check is enabled on the
4197 -- generic unit, a warning in an instance would just be noise. If not
4198 -- enabled on the generic, then a warning in an instance is just wrong.
4199 -- This must be done after analyzing the actuals, which do come from
4200 -- source and are subject to style checking.
4202 Style_Check := False;
4205 Env_Installed := True;
4207 -- Reset renaming map for formal types. The mapping is established
4208 -- when analyzing the generic associations, but some mappings are
4209 -- inherited from formal packages of parent units, and these are
4210 -- constructed when the parents are installed.
4212 Generic_Renamings.Set_Last (0);
4213 Generic_Renamings_HTable.Reset;
4215 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4216 Gen_Unit := Entity (Gen_Id);
4218 -- A package instantiation is Ghost when it is subject to pragma Ghost
4219 -- or the generic template is Ghost. Set the mode now to ensure that
4220 -- any nodes generated during analysis and expansion are marked as
4223 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4225 -- Verify that it is the name of a generic package
4227 -- A visibility glitch: if the instance is a child unit and the generic
4228 -- is the generic unit of a parent instance (i.e. both the parent and
4229 -- the child units are instances of the same package) the name now
4230 -- denotes the renaming within the parent, not the intended generic
4231 -- unit. See if there is a homonym that is the desired generic. The
4232 -- renaming declaration must be visible inside the instance of the
4233 -- child, but not when analyzing the name in the instantiation itself.
4235 if Ekind (Gen_Unit) = E_Package
4236 and then Present (Renamed_Entity (Gen_Unit))
4237 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4238 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4239 and then Present (Homonym (Gen_Unit))
4241 Gen_Unit := Homonym (Gen_Unit);
4244 if Etype (Gen_Unit) = Any_Type then
4248 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4250 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4252 if From_Limited_With (Gen_Unit) then
4254 ("cannot instantiate a limited withed package", Gen_Id);
4257 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4264 if In_Extended_Main_Source_Unit (N) then
4265 Set_Is_Instantiated (Gen_Unit);
4266 Generate_Reference (Gen_Unit, N);
4268 if Present (Renamed_Object (Gen_Unit)) then
4269 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4270 Generate_Reference (Renamed_Object (Gen_Unit), N);
4274 if Nkind (Gen_Id) = N_Identifier
4275 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4278 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4280 elsif Nkind (Gen_Id) = N_Expanded_Name
4281 and then Is_Child_Unit (Gen_Unit)
4282 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4283 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4286 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4289 Set_Entity (Gen_Id, Gen_Unit);
4291 -- If generic is a renaming, get original generic unit
4293 if Present (Renamed_Object (Gen_Unit))
4294 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4296 Gen_Unit := Renamed_Object (Gen_Unit);
4299 -- Verify that there are no circular instantiations
4301 if In_Open_Scopes (Gen_Unit) then
4302 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4306 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4307 Error_Msg_Node_2 := Current_Scope;
4309 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4310 Circularity_Detected := True;
4315 Set_Ekind (Inst_Id, E_Package);
4316 Set_Scope (Inst_Id, Current_Scope);
4318 -- If the context of the instance is subject to SPARK_Mode "off" or
4319 -- the annotation is altogether missing, set the global flag which
4320 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4323 if SPARK_Mode /= On then
4324 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4326 -- Mark the instance spec in case the body is instantiated at a
4327 -- later pass. This preserves the original context in effect for
4330 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4333 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4334 Gen_Spec := Specification (Gen_Decl);
4336 -- Initialize renamings map, for error checking, and the list that
4337 -- holds private entities whose views have changed between generic
4338 -- definition and instantiation. If this is the instance created to
4339 -- validate an actual package, the instantiation environment is that
4340 -- of the enclosing instance.
4342 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4344 -- Copy original generic tree, to produce text for instantiation
4348 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4350 Act_Spec := Specification (Act_Tree);
4352 -- If this is the instance created to validate an actual package,
4353 -- only the formals matter, do not examine the package spec itself.
4355 if Is_Actual_Pack then
4356 Set_Visible_Declarations (Act_Spec, New_List);
4357 Set_Private_Declarations (Act_Spec, New_List);
4361 Analyze_Associations
4363 Formals => Generic_Formal_Declarations (Act_Tree),
4364 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4366 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4368 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4369 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4370 Set_Is_Generic_Instance (Act_Decl_Id);
4371 Set_Generic_Parent (Act_Spec, Gen_Unit);
4373 -- References to the generic in its own declaration or its body are
4374 -- references to the instance. Add a renaming declaration for the
4375 -- generic unit itself. This declaration, as well as the renaming
4376 -- declarations for the generic formals, must remain private to the
4377 -- unit: the formals, because this is the language semantics, and
4378 -- the unit because its use is an artifact of the implementation.
4381 Make_Package_Renaming_Declaration (Loc,
4382 Defining_Unit_Name =>
4383 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4384 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4386 Append (Unit_Renaming, Renaming_List);
4388 -- The renaming declarations are the first local declarations of the
4391 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4393 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4395 Set_Visible_Declarations (Act_Spec, Renaming_List);
4398 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4400 -- Propagate the aspect specifications from the package declaration
4401 -- template to the instantiated version of the package declaration.
4403 if Has_Aspects (Act_Tree) then
4404 Set_Aspect_Specifications (Act_Decl,
4405 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4408 -- The generic may have a generated Default_Storage_Pool aspect,
4409 -- set at the point of generic declaration. If the instance has
4410 -- that aspect, it overrides the one inherited from the generic.
4412 if Has_Aspects (Gen_Spec) then
4413 if No (Aspect_Specifications (N)) then
4414 Set_Aspect_Specifications (N,
4416 (Aspect_Specifications (Gen_Spec))));
4420 Inherited_Aspects : constant List_Id :=
4422 (Aspect_Specifications (Gen_Spec));
4426 Pool_Present : Boolean := False;
4429 ASN1 := First (Aspect_Specifications (N));
4430 while Present (ASN1) loop
4431 if Chars (Identifier (ASN1)) =
4432 Name_Default_Storage_Pool
4434 Pool_Present := True;
4441 if Pool_Present then
4443 -- If generic carries a default storage pool, remove it
4444 -- in favor of the instance one.
4446 ASN2 := First (Inherited_Aspects);
4447 while Present (ASN2) loop
4448 if Chars (Identifier (ASN2)) =
4449 Name_Default_Storage_Pool
4460 (Aspect_Specifications (N), Inherited_Aspects);
4465 -- Save the instantiation node for a subsequent instantiation of the
4466 -- body if there is one and it needs to be instantiated here.
4468 -- We instantiate the body only if we are generating code, or if we
4469 -- are generating cross-reference information, or for GNATprove use.
4472 Enclosing_Body_Present : Boolean := False;
4473 -- If the generic unit is not a compilation unit, then a body may
4474 -- be present in its parent even if none is required. We create a
4475 -- tentative pending instantiation for the body, which will be
4476 -- discarded if none is actually present.
4481 if Scope (Gen_Unit) /= Standard_Standard
4482 and then not Is_Child_Unit (Gen_Unit)
4484 Scop := Scope (Gen_Unit);
4485 while Present (Scop) and then Scop /= Standard_Standard loop
4486 if Unit_Requires_Body (Scop) then
4487 Enclosing_Body_Present := True;
4490 elsif In_Open_Scopes (Scop)
4491 and then In_Package_Body (Scop)
4493 Enclosing_Body_Present := True;
4497 exit when Is_Compilation_Unit (Scop);
4498 Scop := Scope (Scop);
4502 -- If front-end inlining is enabled or there are any subprograms
4503 -- marked with Inline_Always, and this is a unit for which code
4504 -- will be generated, we instantiate the body at once.
4506 -- This is done if the instance is not the main unit, and if the
4507 -- generic is not a child unit of another generic, to avoid scope
4508 -- problems and the reinstallation of parent instances.
4511 and then (not Is_Child_Unit (Gen_Unit)
4512 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4513 and then Might_Inline_Subp (Gen_Unit)
4514 and then not Is_Actual_Pack
4516 if not Back_End_Inlining
4517 and then (Front_End_Inlining or else Has_Inline_Always)
4518 and then (Is_In_Main_Unit (N)
4519 or else In_Main_Context (Current_Scope))
4520 and then Nkind (Parent (N)) /= N_Compilation_Unit
4524 -- In configurable_run_time mode we force the inlining of
4525 -- predefined subprograms marked Inline_Always, to minimize
4526 -- the use of the run-time library.
4528 elsif In_Predefined_Unit (Gen_Decl)
4529 and then Configurable_Run_Time_Mode
4530 and then Nkind (Parent (N)) /= N_Compilation_Unit
4535 -- If the current scope is itself an instance within a child
4536 -- unit, there will be duplications in the scope stack, and the
4537 -- unstacking mechanism in Inline_Instance_Body will fail.
4538 -- This loses some rare cases of optimization, and might be
4539 -- improved some day, if we can find a proper abstraction for
4540 -- "the complete compilation context" that can be saved and
4543 if Is_Generic_Instance (Current_Scope) then
4545 Curr_Unit : constant Entity_Id :=
4546 Cunit_Entity (Current_Sem_Unit);
4548 if Curr_Unit /= Current_Scope
4549 and then Is_Child_Unit (Curr_Unit)
4551 Inline_Now := False;
4558 (Unit_Requires_Body (Gen_Unit)
4559 or else Enclosing_Body_Present
4560 or else Present (Corresponding_Body (Gen_Decl)))
4561 and then Needs_Body_Instantiated (Gen_Unit)
4562 and then not Is_Actual_Pack
4563 and then not Inline_Now
4564 and then (Operating_Mode = Generate_Code
4565 or else (Operating_Mode = Check_Semantics
4566 and then GNATprove_Mode));
4568 -- If front-end inlining is enabled or there are any subprograms
4569 -- marked with Inline_Always, do not instantiate body when within
4570 -- a generic context.
4572 if not Back_End_Inlining
4573 and then (Front_End_Inlining or else Has_Inline_Always)
4574 and then not Expander_Active
4576 Needs_Body := False;
4579 -- If the current context is generic, and the package being
4580 -- instantiated is declared within a formal package, there is no
4581 -- body to instantiate until the enclosing generic is instantiated
4582 -- and there is an actual for the formal package. If the formal
4583 -- package has parameters, we build a regular package instance for
4584 -- it, that precedes the original formal package declaration.
4586 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4588 Decl : constant Node_Id :=
4590 (Unit_Declaration_Node (Scope (Gen_Unit)));
4592 if Nkind (Decl) = N_Formal_Package_Declaration
4593 or else (Nkind (Decl) = N_Package_Declaration
4594 and then Is_List_Member (Decl)
4595 and then Present (Next (Decl))
4597 Nkind (Next (Decl)) =
4598 N_Formal_Package_Declaration)
4600 Needs_Body := False;
4606 -- For RCI unit calling stubs, we omit the instance body if the
4607 -- instance is the RCI library unit itself.
4609 -- However there is a special case for nested instances: in this case
4610 -- we do generate the instance body, as it might be required, e.g.
4611 -- because it provides stream attributes for some type used in the
4612 -- profile of a remote subprogram. This is consistent with 12.3(12),
4613 -- which indicates that the instance body occurs at the place of the
4614 -- instantiation, and thus is part of the RCI declaration, which is
4615 -- present on all client partitions (this is E.2.3(18)).
4617 -- Note that AI12-0002 may make it illegal at some point to have
4618 -- stream attributes defined in an RCI unit, in which case this
4619 -- special case will become unnecessary. In the meantime, there
4620 -- is known application code in production that depends on this
4621 -- being possible, so we definitely cannot eliminate the body in
4622 -- the case of nested instances for the time being.
4624 -- When we generate a nested instance body, calling stubs for any
4625 -- relevant subprogram will be inserted immediately after the
4626 -- subprogram declarations, and will take precedence over the
4627 -- subsequent (original) body. (The stub and original body will be
4628 -- complete homographs, but this is permitted in an instance).
4629 -- (Could we do better and remove the original body???)
4631 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4632 and then Comes_From_Source (N)
4633 and then Nkind (Parent (N)) = N_Compilation_Unit
4635 Needs_Body := False;
4639 -- Indicate that the enclosing scopes contain an instantiation,
4640 -- and that cleanup actions should be delayed until after the
4641 -- instance body is expanded.
4643 Check_Forward_Instantiation (Gen_Decl);
4644 if Nkind (N) = N_Package_Instantiation then
4646 Enclosing_Master : Entity_Id;
4649 -- Loop to search enclosing masters
4651 Enclosing_Master := Current_Scope;
4652 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4653 if Ekind (Enclosing_Master) = E_Package then
4654 if Is_Compilation_Unit (Enclosing_Master) then
4655 if In_Package_Body (Enclosing_Master) then
4656 Set_Delay_Subprogram_Descriptors
4657 (Body_Entity (Enclosing_Master));
4659 Set_Delay_Subprogram_Descriptors
4666 Enclosing_Master := Scope (Enclosing_Master);
4669 elsif Is_Generic_Unit (Enclosing_Master)
4670 or else Ekind (Enclosing_Master) = E_Void
4672 -- Cleanup actions will eventually be performed on the
4673 -- enclosing subprogram or package instance, if any.
4674 -- Enclosing scope is void in the formal part of a
4675 -- generic subprogram.
4680 if Ekind (Enclosing_Master) = E_Entry
4682 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4684 if not Expander_Active then
4688 Protected_Body_Subprogram (Enclosing_Master);
4692 Set_Delay_Cleanups (Enclosing_Master);
4694 while Ekind (Enclosing_Master) = E_Block loop
4695 Enclosing_Master := Scope (Enclosing_Master);
4698 if Is_Subprogram (Enclosing_Master) then
4699 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4701 elsif Is_Task_Type (Enclosing_Master) then
4703 TBP : constant Node_Id :=
4704 Get_Task_Body_Procedure
4707 if Present (TBP) then
4708 Set_Delay_Subprogram_Descriptors (TBP);
4709 Set_Delay_Cleanups (TBP);
4716 end loop Scope_Loop;
4719 -- Make entry in table
4721 Add_Pending_Instantiation (N, Act_Decl);
4725 Set_Categorization_From_Pragmas (Act_Decl);
4727 if Parent_Installed then
4731 Set_Instance_Spec (N, Act_Decl);
4733 -- If not a compilation unit, insert the package declaration before
4734 -- the original instantiation node.
4736 if Nkind (Parent (N)) /= N_Compilation_Unit then
4737 Mark_Rewrite_Insertion (Act_Decl);
4738 Insert_Before (N, Act_Decl);
4740 if Has_Aspects (N) then
4741 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4743 -- The pragma created for a Default_Storage_Pool aspect must
4744 -- appear ahead of the declarations in the instance spec.
4745 -- Analysis has placed it after the instance node, so remove
4746 -- it and reinsert it properly now.
4749 ASN : constant Node_Id := First (Aspect_Specifications (N));
4750 A_Name : constant Name_Id := Chars (Identifier (ASN));
4754 if A_Name = Name_Default_Storage_Pool then
4755 if No (Visible_Declarations (Act_Spec)) then
4756 Set_Visible_Declarations (Act_Spec, New_List);
4760 while Present (Decl) loop
4761 if Nkind (Decl) = N_Pragma then
4763 Prepend (Decl, Visible_Declarations (Act_Spec));
4775 -- For an instantiation that is a compilation unit, place
4776 -- declaration on current node so context is complete for analysis
4777 -- (including nested instantiations). If this is the main unit,
4778 -- the declaration eventually replaces the instantiation node.
4779 -- If the instance body is created later, it replaces the
4780 -- instance node, and the declaration is attached to it
4781 -- (see Build_Instance_Compilation_Unit_Nodes).
4784 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4786 -- The entity for the current unit is the newly created one,
4787 -- and all semantic information is attached to it.
4789 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4791 -- If this is the main unit, replace the main entity as well
4793 if Current_Sem_Unit = Main_Unit then
4794 Main_Unit_Entity := Act_Decl_Id;
4798 Set_Unit (Parent (N), Act_Decl);
4799 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4800 Set_Package_Instantiation (Act_Decl_Id, N);
4802 -- Process aspect specifications of the instance node, if any, to
4803 -- take into account categorization pragmas before analyzing the
4806 if Has_Aspects (N) then
4807 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4811 Set_Unit (Parent (N), N);
4812 Set_Body_Required (Parent (N), False);
4814 -- We never need elaboration checks on instantiations, since by
4815 -- definition, the body instantiation is elaborated at the same
4816 -- time as the spec instantiation.
4818 if Legacy_Elaboration_Checks then
4819 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4820 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4824 if Legacy_Elaboration_Checks then
4825 Check_Elab_Instantiation (N);
4828 -- Save the scenario for later examination by the ABE Processing
4831 Record_Elaboration_Scenario (N);
4833 -- The instantiation results in a guaranteed ABE
4835 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4836 -- Do not instantiate the corresponding body because gigi cannot
4837 -- handle certain types of premature instantiations.
4839 Remove_Dead_Instance (N);
4841 -- Create completing bodies for all subprogram declarations since
4842 -- their real bodies will not be instantiated.
4844 Provide_Completing_Bodies (Instance_Spec (N));
4847 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4849 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4850 First_Private_Entity (Act_Decl_Id));
4852 -- If the instantiation will receive a body, the unit will be
4853 -- transformed into a package body, and receive its own elaboration
4854 -- entity. Otherwise, the nature of the unit is now a package
4857 if Nkind (Parent (N)) = N_Compilation_Unit
4858 and then not Needs_Body
4860 Rewrite (N, Act_Decl);
4863 if Present (Corresponding_Body (Gen_Decl))
4864 or else Unit_Requires_Body (Gen_Unit)
4866 Set_Has_Completion (Act_Decl_Id);
4869 Check_Formal_Packages (Act_Decl_Id);
4871 Restore_Hidden_Primitives (Vis_Prims_List);
4872 Restore_Private_Views (Act_Decl_Id);
4874 Inherit_Context (Gen_Decl, N);
4876 if Parent_Installed then
4881 Env_Installed := False;
4884 Validate_Categorization_Dependency (N, Act_Decl_Id);
4886 -- There used to be a check here to prevent instantiations in local
4887 -- contexts if the No_Local_Allocators restriction was active. This
4888 -- check was removed by a binding interpretation in AI-95-00130/07,
4889 -- but we retain the code for documentation purposes.
4891 -- if Ekind (Act_Decl_Id) /= E_Void
4892 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4894 -- Check_Restriction (No_Local_Allocators, N);
4898 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4901 -- Check that if N is an instantiation of System.Dim_Float_IO or
4902 -- System.Dim_Integer_IO, the formal type has a dimension system.
4904 if Nkind (N) = N_Package_Instantiation
4905 and then Is_Dim_IO_Package_Instantiation (N)
4908 Assoc : constant Node_Id := First (Generic_Associations (N));
4910 if not Has_Dimension_System
4911 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4913 Error_Msg_N ("type with a dimension system expected", Assoc);
4919 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4920 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4923 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4924 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4925 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4926 Style_Check := Saved_Style_Check;
4929 when Instantiation_Error =>
4930 if Parent_Installed then
4934 if Env_Installed then
4938 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4939 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4940 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4941 Style_Check := Saved_Style_Check;
4942 end Analyze_Package_Instantiation;
4944 --------------------------
4945 -- Inline_Instance_Body --
4946 --------------------------
4948 -- WARNING: This routine manages SPARK regions. Return statements must be
4949 -- replaced by gotos which jump to the end of the routine and restore the
4952 procedure Inline_Instance_Body
4954 Gen_Unit : Entity_Id;
4957 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4959 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4960 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4961 Gen_Comp : constant Entity_Id :=
4962 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4964 Scope_Stack_Depth : constant Pos :=
4965 Scope_Stack.Last - Scope_Stack.First + 1;
4967 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4968 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4969 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4971 Curr_Scope : Entity_Id := Empty;
4972 List : Elist_Id := No_Elist; -- init to avoid warning
4973 N_Instances : Nat := 0;
4974 Num_Inner : Nat := 0;
4975 Num_Scopes : Nat := 0;
4976 Removed : Boolean := False;
4981 -- Case of generic unit defined in another unit. We must remove the
4982 -- complete context of the current unit to install that of the generic.
4984 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4986 -- Add some comments for the following two loops ???
4989 while Present (S) and then S /= Standard_Standard loop
4991 Num_Scopes := Num_Scopes + 1;
4993 Use_Clauses (Num_Scopes) :=
4995 (Scope_Stack.Last - Num_Scopes + 1).
4997 End_Use_Clauses (Use_Clauses (Num_Scopes));
4999 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5000 or else Scope_Stack.Table
5001 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5004 exit when Is_Generic_Instance (S)
5005 and then (In_Package_Body (S)
5006 or else Ekind (S) = E_Procedure
5007 or else Ekind (S) = E_Function);
5011 Vis := Is_Immediately_Visible (Gen_Comp);
5013 -- Find and save all enclosing instances
5018 and then S /= Standard_Standard
5020 if Is_Generic_Instance (S) then
5021 N_Instances := N_Instances + 1;
5022 Instances (N_Instances) := S;
5024 exit when In_Package_Body (S);
5030 -- Remove context of current compilation unit, unless we are within a
5031 -- nested package instantiation, in which case the context has been
5032 -- removed previously.
5034 -- If current scope is the body of a child unit, remove context of
5035 -- spec as well. If an enclosing scope is an instance body, the
5036 -- context has already been removed, but the entities in the body
5037 -- must be made invisible as well.
5040 while Present (S) and then S /= Standard_Standard loop
5041 if Is_Generic_Instance (S)
5042 and then (In_Package_Body (S)
5043 or else Ekind (S) in E_Procedure | E_Function)
5045 -- We still have to remove the entities of the enclosing
5046 -- instance from direct visibility.
5051 E := First_Entity (S);
5052 while Present (E) loop
5053 Set_Is_Immediately_Visible (E, False);
5062 or else (Ekind (Curr_Unit) = E_Package_Body
5063 and then S = Spec_Entity (Curr_Unit))
5064 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5065 and then S = Corresponding_Spec
5066 (Unit_Declaration_Node (Curr_Unit)))
5070 -- Remove entities in current scopes from visibility, so that
5071 -- instance body is compiled in a clean environment.
5073 List := Save_Scope_Stack (Handle_Use => False);
5075 if Is_Child_Unit (S) then
5077 -- Remove child unit from stack, as well as inner scopes.
5078 -- Removing the context of a child unit removes parent units
5081 while Current_Scope /= S loop
5082 Num_Inner := Num_Inner + 1;
5083 Inner_Scopes (Num_Inner) := Current_Scope;
5088 Remove_Context (Curr_Comp);
5092 Remove_Context (Curr_Comp);
5095 if Ekind (Curr_Unit) = E_Package_Body then
5096 Remove_Context (Library_Unit (Curr_Comp));
5103 pragma Assert (Num_Inner < Num_Scopes);
5105 Push_Scope (Standard_Standard);
5106 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5108 -- The inlined package body is analyzed with the configuration state
5109 -- of the context prior to the scope manipulations performed above.
5111 -- ??? shouldn't this also use the warning state of the context prior
5112 -- to the scope manipulations?
5114 Instantiate_Package_Body
5116 ((Act_Decl => Act_Decl,
5117 Config_Switches => Config_Attrs,
5118 Current_Sem_Unit => Current_Sem_Unit,
5119 Expander_Status => Expander_Active,
5121 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5122 Scope_Suppress => Scope_Suppress,
5123 Warnings => Save_Warnings)),
5124 Inlined_Body => True);
5130 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5132 -- Reset Generic_Instance flag so that use clauses can be installed
5133 -- in the proper order. (See Use_One_Package for effect of enclosing
5134 -- instances on processing of use clauses).
5136 for J in 1 .. N_Instances loop
5137 Set_Is_Generic_Instance (Instances (J), False);
5141 Install_Context (Curr_Comp, Chain => False);
5143 if Present (Curr_Scope)
5144 and then Is_Child_Unit (Curr_Scope)
5146 Push_Scope (Curr_Scope);
5147 Set_Is_Immediately_Visible (Curr_Scope);
5149 -- Finally, restore inner scopes as well
5151 for J in reverse 1 .. Num_Inner loop
5152 Push_Scope (Inner_Scopes (J));
5156 Restore_Scope_Stack (List, Handle_Use => False);
5158 if Present (Curr_Scope)
5160 (In_Private_Part (Curr_Scope)
5161 or else In_Package_Body (Curr_Scope))
5163 -- Install private declaration of ancestor units, which are
5164 -- currently available. Restore_Scope_Stack and Install_Context
5165 -- only install the visible part of parents.
5170 Par := Scope (Curr_Scope);
5171 while (Present (Par)) and then Par /= Standard_Standard loop
5172 Install_Private_Declarations (Par);
5179 -- Restore use clauses. For a child unit, use clauses in the parents
5180 -- are restored when installing the context, so only those in inner
5181 -- scopes (and those local to the child unit itself) need to be
5182 -- installed explicitly.
5184 if Is_Child_Unit (Curr_Unit) and then Removed then
5185 for J in reverse 1 .. Num_Inner + 1 loop
5186 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5188 Install_Use_Clauses (Use_Clauses (J));
5192 for J in reverse 1 .. Num_Scopes loop
5193 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5195 Install_Use_Clauses (Use_Clauses (J));
5199 -- Restore status of instances. If one of them is a body, make its
5200 -- local entities visible again.
5207 for J in 1 .. N_Instances loop
5208 Inst := Instances (J);
5209 Set_Is_Generic_Instance (Inst, True);
5211 if In_Package_Body (Inst)
5212 or else Ekind (S) in E_Procedure | E_Function
5214 E := First_Entity (Instances (J));
5215 while Present (E) loop
5216 Set_Is_Immediately_Visible (E);
5223 -- If generic unit is in current unit, current context is correct. Note
5224 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5225 -- enclosing scopes were removed.
5228 Instantiate_Package_Body
5230 ((Act_Decl => Act_Decl,
5231 Config_Switches => Save_Config_Switches,
5232 Current_Sem_Unit => Current_Sem_Unit,
5233 Expander_Status => Expander_Active,
5235 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5236 Scope_Suppress => Scope_Suppress,
5237 Warnings => Save_Warnings)),
5238 Inlined_Body => True);
5240 end Inline_Instance_Body;
5242 -------------------------------------
5243 -- Analyze_Procedure_Instantiation --
5244 -------------------------------------
5246 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5248 Analyze_Subprogram_Instantiation (N, E_Procedure);
5249 end Analyze_Procedure_Instantiation;
5251 -----------------------------------
5252 -- Need_Subprogram_Instance_Body --
5253 -----------------------------------
5255 function Need_Subprogram_Instance_Body
5257 Subp : Entity_Id) return Boolean
5259 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5260 -- Return True if E is an inlined subprogram, an inlined renaming or a
5261 -- subprogram nested in an inlined subprogram. The inlining machinery
5262 -- totally disregards nested subprograms since it considers that they
5263 -- will always be compiled if the parent is (see Inline.Is_Nested).
5265 ------------------------------------
5266 -- Is_Inlined_Or_Child_Of_Inlined --
5267 ------------------------------------
5269 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5273 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5278 while Scop /= Standard_Standard loop
5279 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5283 Scop := Scope (Scop);
5287 end Is_Inlined_Or_Child_Of_Inlined;
5290 -- Must be in the main unit or inlined (or child of inlined)
5292 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5294 -- Must be generating code or analyzing code in GNATprove mode
5296 and then (Operating_Mode = Generate_Code
5297 or else (Operating_Mode = Check_Semantics
5298 and then GNATprove_Mode))
5300 -- The body is needed when generating code (full expansion) and in
5301 -- in GNATprove mode (special expansion) for formal verification of
5304 and then (Expander_Active or GNATprove_Mode)
5306 -- No point in inlining if ABE is inevitable
5308 and then not Is_Known_Guaranteed_ABE (N)
5310 -- Or if subprogram is eliminated
5312 and then not Is_Eliminated (Subp)
5314 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5317 -- Here if not inlined, or we ignore the inlining
5322 end Need_Subprogram_Instance_Body;
5324 --------------------------------------
5325 -- Analyze_Subprogram_Instantiation --
5326 --------------------------------------
5328 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5329 -- must be replaced by gotos which jump to the end of the routine in order
5330 -- to restore the Ghost and SPARK modes.
5332 procedure Analyze_Subprogram_Instantiation
5336 Errs : constant Nat := Serious_Errors_Detected;
5337 Gen_Id : constant Node_Id := Name (N);
5338 Inst_Id : constant Entity_Id := Defining_Entity (N);
5339 Anon_Id : constant Entity_Id :=
5340 Make_Defining_Identifier (Sloc (Inst_Id),
5341 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5342 Loc : constant Source_Ptr := Sloc (N);
5344 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5349 Env_Installed : Boolean := False;
5350 Gen_Unit : Entity_Id;
5352 Pack_Id : Entity_Id;
5353 Parent_Installed : Boolean := False;
5355 Renaming_List : List_Id;
5356 -- The list of declarations that link formals and actuals of the
5357 -- instance. These are subtype declarations for formal types, and
5358 -- renaming declarations for other formals. The subprogram declaration
5359 -- for the instance is then appended to the list, and the last item on
5360 -- the list is the renaming declaration for the instance.
5362 procedure Analyze_Instance_And_Renamings;
5363 -- The instance must be analyzed in a context that includes the mappings
5364 -- of generic parameters into actuals. We create a package declaration
5365 -- for this purpose, and a subprogram with an internal name within the
5366 -- package. The subprogram instance is simply an alias for the internal
5367 -- subprogram, declared in the current scope.
5369 procedure Build_Subprogram_Renaming;
5370 -- If the subprogram is recursive, there are occurrences of the name of
5371 -- the generic within the body, which must resolve to the current
5372 -- instance. We add a renaming declaration after the declaration, which
5373 -- is available in the instance body, as well as in the analysis of
5374 -- aspects that appear in the generic. This renaming declaration is
5375 -- inserted after the instance declaration which it renames.
5377 ------------------------------------
5378 -- Analyze_Instance_And_Renamings --
5379 ------------------------------------
5381 procedure Analyze_Instance_And_Renamings is
5382 Def_Ent : constant Entity_Id := Defining_Entity (N);
5383 Pack_Decl : Node_Id;
5386 if Nkind (Parent (N)) = N_Compilation_Unit then
5388 -- For the case of a compilation unit, the container package has
5389 -- the same name as the instantiation, to insure that the binder
5390 -- calls the elaboration procedure with the right name. Copy the
5391 -- entity of the instance, which may have compilation level flags
5392 -- (e.g. Is_Child_Unit) set.
5394 Pack_Id := New_Copy (Def_Ent);
5397 -- Otherwise we use the name of the instantiation concatenated
5398 -- with its source position to ensure uniqueness if there are
5399 -- several instantiations with the same name.
5402 Make_Defining_Identifier (Loc,
5403 Chars => New_External_Name
5404 (Related_Id => Chars (Def_Ent),
5406 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5410 Make_Package_Declaration (Loc,
5411 Specification => Make_Package_Specification (Loc,
5412 Defining_Unit_Name => Pack_Id,
5413 Visible_Declarations => Renaming_List,
5414 End_Label => Empty));
5416 Set_Instance_Spec (N, Pack_Decl);
5417 Set_Is_Generic_Instance (Pack_Id);
5418 Set_Debug_Info_Needed (Pack_Id);
5420 -- Case of not a compilation unit
5422 if Nkind (Parent (N)) /= N_Compilation_Unit then
5423 Mark_Rewrite_Insertion (Pack_Decl);
5424 Insert_Before (N, Pack_Decl);
5425 Set_Has_Completion (Pack_Id);
5427 -- Case of an instantiation that is a compilation unit
5429 -- Place declaration on current node so context is complete for
5430 -- analysis (including nested instantiations), and for use in a
5431 -- context_clause (see Analyze_With_Clause).
5434 Set_Unit (Parent (N), Pack_Decl);
5435 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5438 Analyze (Pack_Decl);
5439 Check_Formal_Packages (Pack_Id);
5441 -- Body of the enclosing package is supplied when instantiating the
5442 -- subprogram body, after semantic analysis is completed.
5444 if Nkind (Parent (N)) = N_Compilation_Unit then
5446 -- Remove package itself from visibility, so it does not
5447 -- conflict with subprogram.
5449 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5451 -- Set name and scope of internal subprogram so that the proper
5452 -- external name will be generated. The proper scope is the scope
5453 -- of the wrapper package. We need to generate debugging info for
5454 -- the internal subprogram, so set flag accordingly.
5456 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5457 Set_Scope (Anon_Id, Scope (Pack_Id));
5459 -- Mark wrapper package as referenced, to avoid spurious warnings
5460 -- if the instantiation appears in various with_ clauses of
5461 -- subunits of the main unit.
5463 Set_Referenced (Pack_Id);
5466 Set_Is_Generic_Instance (Anon_Id);
5467 Set_Debug_Info_Needed (Anon_Id);
5468 Act_Decl_Id := New_Copy (Anon_Id);
5470 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5471 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5472 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5474 -- Subprogram instance comes from source only if generic does
5476 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5478 -- If the instance is a child unit, mark the Id accordingly. Mark
5479 -- the anonymous entity as well, which is the real subprogram and
5480 -- which is used when the instance appears in a context clause.
5481 -- Similarly, propagate the Is_Eliminated flag to handle properly
5482 -- nested eliminated subprograms.
5484 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5485 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5486 New_Overloaded_Entity (Act_Decl_Id);
5487 Check_Eliminated (Act_Decl_Id);
5488 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5490 if Nkind (Parent (N)) = N_Compilation_Unit then
5492 -- In compilation unit case, kill elaboration checks on the
5493 -- instantiation, since they are never needed - the body is
5494 -- instantiated at the same point as the spec.
5496 if Legacy_Elaboration_Checks then
5497 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5498 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5501 Set_Is_Compilation_Unit (Anon_Id);
5502 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5505 -- The instance is not a freezing point for the new subprogram.
5506 -- The anonymous subprogram may have a freeze node, created for
5507 -- some delayed aspects. This freeze node must not be inherited
5508 -- by the visible subprogram entity.
5510 Set_Is_Frozen (Act_Decl_Id, False);
5511 Set_Freeze_Node (Act_Decl_Id, Empty);
5513 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5514 Valid_Operator_Definition (Act_Decl_Id);
5517 Set_Alias (Act_Decl_Id, Anon_Id);
5518 Set_Has_Completion (Act_Decl_Id);
5519 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5521 if Nkind (Parent (N)) = N_Compilation_Unit then
5522 Set_Body_Required (Parent (N), False);
5524 end Analyze_Instance_And_Renamings;
5526 -------------------------------
5527 -- Build_Subprogram_Renaming --
5528 -------------------------------
5530 procedure Build_Subprogram_Renaming is
5531 Renaming_Decl : Node_Id;
5532 Unit_Renaming : Node_Id;
5536 Make_Subprogram_Renaming_Declaration (Loc,
5539 (Specification (Original_Node (Gen_Decl)),
5541 Instantiating => True),
5542 Name => New_Occurrence_Of (Anon_Id, Loc));
5544 -- The generic may be a child unit. The renaming needs an identifier
5545 -- with the proper name.
5547 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5548 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5550 -- If there is a formal subprogram with the same name as the unit
5551 -- itself, do not add this renaming declaration, to prevent
5552 -- ambiguities when there is a call with that name in the body.
5553 -- This is a partial and ugly fix for one ACATS test. ???
5555 Renaming_Decl := First (Renaming_List);
5556 while Present (Renaming_Decl) loop
5557 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5559 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5564 Next (Renaming_Decl);
5567 if No (Renaming_Decl) then
5568 Append (Unit_Renaming, Renaming_List);
5570 end Build_Subprogram_Renaming;
5574 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5575 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5576 Saved_ISMP : constant Boolean :=
5577 Ignore_SPARK_Mode_Pragmas_In_Instance;
5578 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5579 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5580 -- Save the Ghost and SPARK mode-related data to restore on exit
5582 Vis_Prims_List : Elist_Id := No_Elist;
5583 -- List of primitives made temporarily visible in the instantiation
5584 -- to match the visibility of the formal type
5586 -- Start of processing for Analyze_Subprogram_Instantiation
5589 -- Preserve relevant elaboration-related attributes of the context which
5590 -- are no longer available or very expensive to recompute once analysis,
5591 -- resolution, and expansion are over.
5593 Mark_Elaboration_Attributes
5600 -- Very first thing: check for special Text_IO unit in case we are
5601 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5602 -- such an instantiation is bogus (these are packages, not subprograms),
5603 -- but we get a better error message if we do this.
5605 Check_Text_IO_Special_Unit (Gen_Id);
5607 -- Make node global for error reporting
5609 Instantiation_Node := N;
5611 -- For package instantiations we turn off style checks, because they
5612 -- will have been emitted in the generic. For subprogram instantiations
5613 -- we want to apply at least the check on overriding indicators so we
5614 -- do not modify the style check status.
5616 -- The renaming declarations for the actuals do not come from source and
5617 -- will not generate spurious warnings.
5619 Preanalyze_Actuals (N);
5622 Env_Installed := True;
5623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5624 Gen_Unit := Entity (Gen_Id);
5626 -- A subprogram instantiation is Ghost when it is subject to pragma
5627 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5628 -- that any nodes generated during analysis and expansion are marked as
5631 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5633 Generate_Reference (Gen_Unit, Gen_Id);
5635 if Nkind (Gen_Id) = N_Identifier
5636 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5639 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5642 if Etype (Gen_Unit) = Any_Type then
5647 -- Verify that it is a generic subprogram of the right kind, and that
5648 -- it does not lead to a circular instantiation.
5650 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5652 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5654 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5656 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5658 elsif In_Open_Scopes (Gen_Unit) then
5659 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5662 Set_Ekind (Inst_Id, K);
5663 Set_Scope (Inst_Id, Current_Scope);
5665 Set_Entity (Gen_Id, Gen_Unit);
5666 Set_Is_Instantiated (Gen_Unit);
5668 if In_Extended_Main_Source_Unit (N) then
5669 Generate_Reference (Gen_Unit, N);
5672 -- If renaming, get original unit
5674 if Present (Renamed_Object (Gen_Unit))
5675 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5677 Gen_Unit := Renamed_Object (Gen_Unit);
5678 Set_Is_Instantiated (Gen_Unit);
5679 Generate_Reference (Gen_Unit, N);
5682 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5683 Error_Msg_Node_2 := Current_Scope;
5685 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5686 Circularity_Detected := True;
5687 Restore_Hidden_Primitives (Vis_Prims_List);
5691 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5693 -- Initialize renamings map, for error checking
5695 Generic_Renamings.Set_Last (0);
5696 Generic_Renamings_HTable.Reset;
5698 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5700 -- Copy original generic tree, to produce text for instantiation
5704 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5706 -- Inherit overriding indicator from instance node
5708 Act_Spec := Specification (Act_Tree);
5709 Set_Must_Override (Act_Spec, Must_Override (N));
5710 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5713 Analyze_Associations
5715 Formals => Generic_Formal_Declarations (Act_Tree),
5716 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5718 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5720 -- The subprogram itself cannot contain a nested instance, so the
5721 -- current parent is left empty.
5723 Set_Instance_Env (Gen_Unit, Empty);
5725 -- Build the subprogram declaration, which does not appear in the
5726 -- generic template, and give it a sloc consistent with that of the
5729 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5730 Set_Generic_Parent (Act_Spec, Gen_Unit);
5732 Make_Subprogram_Declaration (Sloc (Act_Spec),
5733 Specification => Act_Spec);
5735 -- The aspects have been copied previously, but they have to be
5736 -- linked explicitly to the new subprogram declaration. Explicit
5737 -- pre/postconditions on the instance are analyzed below, in a
5740 Move_Aspects (Act_Tree, To => Act_Decl);
5741 Set_Categorization_From_Pragmas (Act_Decl);
5743 if Parent_Installed then
5747 Append (Act_Decl, Renaming_List);
5749 -- Contract-related source pragmas that follow a generic subprogram
5750 -- must be instantiated explicitly because they are not part of the
5751 -- subprogram template.
5753 Instantiate_Subprogram_Contract
5754 (Original_Node (Gen_Decl), Renaming_List);
5756 Build_Subprogram_Renaming;
5758 -- If the context of the instance is subject to SPARK_Mode "off" or
5759 -- the annotation is altogether missing, set the global flag which
5760 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5761 -- the instance. This should be done prior to analyzing the instance.
5763 if SPARK_Mode /= On then
5764 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5767 -- If the context of an instance is not subject to SPARK_Mode "off",
5768 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5769 -- the latter should be the one applicable to the instance.
5771 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5772 and then Saved_SM /= Off
5773 and then Present (SPARK_Pragma (Gen_Unit))
5775 Set_SPARK_Mode (Gen_Unit);
5778 Analyze_Instance_And_Renamings;
5780 -- Restore SPARK_Mode from the context after analysis of the package
5781 -- declaration, so that the SPARK_Mode on the generic spec does not
5782 -- apply to the pending instance for the instance body.
5784 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5785 and then Saved_SM /= Off
5786 and then Present (SPARK_Pragma (Gen_Unit))
5788 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5791 -- If the generic is marked Import (Intrinsic), then so is the
5792 -- instance. This indicates that there is no body to instantiate. If
5793 -- generic is marked inline, so it the instance, and the anonymous
5794 -- subprogram it renames. If inlined, or else if inlining is enabled
5795 -- for the compilation, we generate the instance body even if it is
5796 -- not within the main unit.
5798 if Is_Intrinsic_Subprogram (Gen_Unit) then
5799 Set_Is_Intrinsic_Subprogram (Anon_Id);
5800 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5802 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5803 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5807 -- Inherit convention from generic unit. Intrinsic convention, as for
5808 -- an instance of unchecked conversion, is not inherited because an
5809 -- explicit Ada instance has been created.
5811 if Has_Convention_Pragma (Gen_Unit)
5812 and then Convention (Gen_Unit) /= Convention_Intrinsic
5814 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5815 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5818 Generate_Definition (Act_Decl_Id);
5820 -- Inherit all inlining-related flags which apply to the generic in
5821 -- the subprogram and its declaration.
5823 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5824 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5826 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5827 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5829 Set_Has_Pragma_Inline_Always
5830 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5831 Set_Has_Pragma_Inline_Always
5832 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5834 Set_Has_Pragma_No_Inline
5835 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5836 Set_Has_Pragma_No_Inline
5837 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5839 -- Propagate No_Return if pragma applied to generic unit. This must
5840 -- be done explicitly because pragma does not appear in generic
5841 -- declaration (unlike the aspect case).
5843 if No_Return (Gen_Unit) then
5844 Set_No_Return (Act_Decl_Id);
5845 Set_No_Return (Anon_Id);
5848 -- Mark both the instance spec and the anonymous package in case the
5849 -- body is instantiated at a later pass. This preserves the original
5850 -- context in effect for the body.
5852 if SPARK_Mode /= On then
5853 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5854 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5857 if Legacy_Elaboration_Checks
5858 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5860 Check_Elab_Instantiation (N);
5863 -- Save the scenario for later examination by the ABE Processing
5866 Record_Elaboration_Scenario (N);
5868 -- The instantiation results in a guaranteed ABE. Create a completing
5869 -- body for the subprogram declaration because the real body will not
5872 if Is_Known_Guaranteed_ABE (N) then
5873 Provide_Completing_Bodies (Instance_Spec (N));
5876 if Is_Dispatching_Operation (Act_Decl_Id)
5877 and then Ada_Version >= Ada_2005
5883 Formal := First_Formal (Act_Decl_Id);
5884 while Present (Formal) loop
5885 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5886 and then Is_Controlling_Formal (Formal)
5887 and then not Can_Never_Be_Null (Formal)
5890 ("access parameter& is controlling,", N, Formal);
5892 ("\corresponding parameter of & must be explicitly "
5893 & "null-excluding", N, Gen_Id);
5896 Next_Formal (Formal);
5901 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5903 Validate_Categorization_Dependency (N, Act_Decl_Id);
5905 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5906 Inherit_Context (Gen_Decl, N);
5908 Restore_Private_Views (Pack_Id, False);
5910 -- If the context requires a full instantiation, mark node for
5911 -- subsequent construction of the body.
5913 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5914 Check_Forward_Instantiation (Gen_Decl);
5916 -- The wrapper package is always delayed, because it does not
5917 -- constitute a freeze point, but to insure that the freeze node
5918 -- is placed properly, it is created directly when instantiating
5919 -- the body (otherwise the freeze node might appear to early for
5920 -- nested instantiations).
5922 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5923 Rewrite (N, Unit (Parent (N)));
5924 Set_Unit (Parent (N), N);
5927 -- Replace instance node for library-level instantiations of
5928 -- intrinsic subprograms.
5930 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5931 Rewrite (N, Unit (Parent (N)));
5932 Set_Unit (Parent (N), N);
5935 if Parent_Installed then
5939 Restore_Hidden_Primitives (Vis_Prims_List);
5941 Env_Installed := False;
5942 Generic_Renamings.Set_Last (0);
5943 Generic_Renamings_HTable.Reset;
5947 -- Analyze aspects in declaration if no errors appear in the instance.
5949 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5950 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5953 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5954 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5955 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5958 when Instantiation_Error =>
5959 if Parent_Installed then
5963 if Env_Installed then
5967 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5968 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5969 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5970 end Analyze_Subprogram_Instantiation;
5972 -------------------------
5973 -- Get_Associated_Node --
5974 -------------------------
5976 function Get_Associated_Node (N : Node_Id) return Node_Id is
5980 Assoc := Associated_Node (N);
5982 if Nkind (Assoc) /= Nkind (N) then
5985 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5989 -- If the node is part of an inner generic, it may itself have been
5990 -- remapped into a further generic copy. Associated_Node is otherwise
5991 -- used for the entity of the node, and will be of a different node
5992 -- kind, or else N has been rewritten as a literal or function call.
5994 while Present (Associated_Node (Assoc))
5995 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5997 Assoc := Associated_Node (Assoc);
6000 -- Follow an additional link in case the final node was rewritten.
6001 -- This can only happen with nested generic units.
6003 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6004 and then Present (Associated_Node (Assoc))
6005 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6006 | N_Explicit_Dereference
6011 Assoc := Associated_Node (Assoc);
6014 -- An additional special case: an unconstrained type in an object
6015 -- declaration may have been rewritten as a local subtype constrained
6016 -- by the expression in the declaration. We need to recover the
6017 -- original entity, which may be global.
6019 if Present (Original_Node (Assoc))
6020 and then Nkind (Parent (N)) = N_Object_Declaration
6022 Assoc := Original_Node (Assoc);
6027 end Get_Associated_Node;
6029 ----------------------------
6030 -- Build_Function_Wrapper --
6031 ----------------------------
6033 function Build_Function_Wrapper
6034 (Formal_Subp : Entity_Id;
6035 Actual_Subp : Entity_Id) return Node_Id
6037 Loc : constant Source_Ptr := Sloc (Current_Scope);
6038 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6041 Func_Name : Node_Id;
6043 Parm_Type : Node_Id;
6044 Profile : List_Id := New_List;
6051 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6053 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6054 Set_Ekind (Func, E_Function);
6055 Set_Is_Generic_Actual_Subprogram (Func);
6057 Actuals := New_List;
6058 Profile := New_List;
6060 Act_F := First_Formal (Actual_Subp);
6061 Form_F := First_Formal (Formal_Subp);
6062 while Present (Form_F) loop
6064 -- Create new formal for profile of wrapper, and add a reference
6065 -- to it in the list of actuals for the enclosing call. The name
6066 -- must be that of the formal in the formal subprogram, because
6067 -- calls to it in the generic body may use named associations.
6069 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6072 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6075 Make_Parameter_Specification (Loc,
6076 Defining_Identifier => New_F,
6077 Parameter_Type => Parm_Type));
6079 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6080 Next_Formal (Form_F);
6082 if Present (Act_F) then
6083 Next_Formal (Act_F);
6088 Make_Function_Specification (Loc,
6089 Defining_Unit_Name => Func,
6090 Parameter_Specifications => Profile,
6091 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6094 Make_Expression_Function (Loc,
6095 Specification => Spec,
6097 Make_Function_Call (Loc,
6099 Parameter_Associations => Actuals));
6102 end Build_Function_Wrapper;
6104 ----------------------------
6105 -- Build_Operator_Wrapper --
6106 ----------------------------
6108 function Build_Operator_Wrapper
6109 (Formal_Subp : Entity_Id;
6110 Actual_Subp : Entity_Id) return Node_Id
6112 Loc : constant Source_Ptr := Sloc (Current_Scope);
6113 Ret_Type : constant Entity_Id :=
6114 Get_Instance_Of (Etype (Formal_Subp));
6115 Op_Type : constant Entity_Id :=
6116 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6117 Is_Binary : constant Boolean :=
6118 Present (Next_Formal (First_Formal (Formal_Subp)));
6121 Expr : Node_Id := Empty;
6129 Op_Name := Chars (Actual_Subp);
6131 -- Create entities for wrapper function and its formals
6133 F1 := Make_Temporary (Loc, 'A');
6134 F2 := Make_Temporary (Loc, 'B');
6135 L := New_Occurrence_Of (F1, Loc);
6136 R := New_Occurrence_Of (F2, Loc);
6138 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6139 Set_Ekind (Func, E_Function);
6140 Set_Is_Generic_Actual_Subprogram (Func);
6143 Make_Function_Specification (Loc,
6144 Defining_Unit_Name => Func,
6145 Parameter_Specifications => New_List (
6146 Make_Parameter_Specification (Loc,
6147 Defining_Identifier => F1,
6148 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6149 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6152 Append_To (Parameter_Specifications (Spec),
6153 Make_Parameter_Specification (Loc,
6154 Defining_Identifier => F2,
6155 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6158 -- Build expression as a function call, or as an operator node
6159 -- that corresponds to the name of the actual, starting with
6160 -- binary operators.
6162 if Op_Name not in Any_Operator_Name then
6164 Make_Function_Call (Loc,
6166 New_Occurrence_Of (Actual_Subp, Loc),
6167 Parameter_Associations => New_List (L));
6170 Append_To (Parameter_Associations (Expr), R);
6175 elsif Is_Binary then
6176 if Op_Name = Name_Op_And then
6177 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6178 elsif Op_Name = Name_Op_Or then
6179 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6180 elsif Op_Name = Name_Op_Xor then
6181 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Eq then
6183 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Ne then
6185 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Le then
6187 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Gt then
6189 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Ge then
6191 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Lt then
6193 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6194 elsif Op_Name = Name_Op_Add then
6195 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6196 elsif Op_Name = Name_Op_Subtract then
6197 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6198 elsif Op_Name = Name_Op_Concat then
6199 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6200 elsif Op_Name = Name_Op_Multiply then
6201 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6202 elsif Op_Name = Name_Op_Divide then
6203 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6204 elsif Op_Name = Name_Op_Mod then
6205 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6206 elsif Op_Name = Name_Op_Rem then
6207 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6208 elsif Op_Name = Name_Op_Expon then
6209 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6215 if Op_Name = Name_Op_Add then
6216 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6217 elsif Op_Name = Name_Op_Subtract then
6218 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6219 elsif Op_Name = Name_Op_Abs then
6220 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6221 elsif Op_Name = Name_Op_Not then
6222 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6227 Make_Expression_Function (Loc,
6228 Specification => Spec,
6229 Expression => Expr);
6232 end Build_Operator_Wrapper;
6234 -----------------------------------
6235 -- Build_Subprogram_Decl_Wrapper --
6236 -----------------------------------
6238 function Build_Subprogram_Decl_Wrapper
6239 (Formal_Subp : Entity_Id) return Node_Id
6241 Loc : constant Source_Ptr := Sloc (Current_Scope);
6242 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6245 Parm_Spec : Node_Id;
6246 Profile : List_Id := New_List;
6253 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6254 Set_Ekind (Subp, Ekind (Formal_Subp));
6255 Set_Is_Generic_Actual_Subprogram (Subp);
6257 Profile := Parameter_Specifications (
6259 (Specification (Unit_Declaration_Node (Formal_Subp))));
6261 Form_F := First_Formal (Formal_Subp);
6262 Parm_Spec := First (Profile);
6264 -- Create new entities for the formals. Reset entities so that
6265 -- parameter types are properly resolved when wrapper declaration
6268 while Present (Parm_Spec) loop
6269 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6270 Set_Defining_Identifier (Parm_Spec, New_F);
6271 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6273 Next_Formal (Form_F);
6276 if Ret_Type = Standard_Void_Type then
6278 Make_Procedure_Specification (Loc,
6279 Defining_Unit_Name => Subp,
6280 Parameter_Specifications => Profile);
6283 Make_Function_Specification (Loc,
6284 Defining_Unit_Name => Subp,
6285 Parameter_Specifications => Profile,
6286 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6290 Make_Subprogram_Declaration (Loc, Specification => Spec);
6293 end Build_Subprogram_Decl_Wrapper;
6295 -----------------------------------
6296 -- Build_Subprogram_Body_Wrapper --
6297 -----------------------------------
6299 function Build_Subprogram_Body_Wrapper
6300 (Formal_Subp : Entity_Id;
6301 Actual_Name : Node_Id) return Node_Id
6303 Loc : constant Source_Ptr := Sloc (Current_Scope);
6304 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6305 Spec_Node : constant Node_Id :=
6307 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6310 Body_Node : Node_Id;
6313 Actuals := New_List;
6314 Act := First (Parameter_Specifications (Spec_Node));
6316 while Present (Act) loop
6318 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6322 if Ret_Type = Standard_Void_Type then
6323 Stmt := Make_Procedure_Call_Statement (Loc,
6324 Name => Actual_Name,
6325 Parameter_Associations => Actuals);
6328 Stmt := Make_Simple_Return_Statement (Loc,
6330 Make_Function_Call (Loc,
6331 Name => Actual_Name,
6332 Parameter_Associations => Actuals));
6335 Body_Node := Make_Subprogram_Body (Loc,
6336 Specification => Spec_Node,
6337 Declarations => New_List,
6338 Handled_Statement_Sequence =>
6339 Make_Handled_Sequence_Of_Statements (Loc,
6340 Statements => New_List (Stmt)));
6343 end Build_Subprogram_Body_Wrapper;
6345 -------------------------------------------
6346 -- Build_Instance_Compilation_Unit_Nodes --
6347 -------------------------------------------
6349 procedure Build_Instance_Compilation_Unit_Nodes
6354 Decl_Cunit : Node_Id;
6355 Body_Cunit : Node_Id;
6357 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6358 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6361 -- A new compilation unit node is built for the instance declaration
6364 Make_Compilation_Unit (Sloc (N),
6365 Context_Items => Empty_List,
6367 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6369 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6371 -- The new compilation unit is linked to its body, but both share the
6372 -- same file, so we do not set Body_Required on the new unit so as not
6373 -- to create a spurious dependency on a non-existent body in the ali.
6374 -- This simplifies CodePeer unit traversal.
6376 -- We use the original instantiation compilation unit as the resulting
6377 -- compilation unit of the instance, since this is the main unit.
6379 Rewrite (N, Act_Body);
6381 -- Propagate the aspect specifications from the package body template to
6382 -- the instantiated version of the package body.
6384 if Has_Aspects (Act_Body) then
6385 Set_Aspect_Specifications
6386 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6389 Body_Cunit := Parent (N);
6391 -- The two compilation unit nodes are linked by the Library_Unit field
6393 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6394 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6396 -- Preserve the private nature of the package if needed
6398 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6400 -- If the instance is not the main unit, its context, categorization
6401 -- and elaboration entity are not relevant to the compilation.
6403 if Body_Cunit /= Cunit (Main_Unit) then
6404 Make_Instance_Unit (Body_Cunit, In_Main => False);
6408 -- The context clause items on the instantiation, which are now attached
6409 -- to the body compilation unit (since the body overwrote the original
6410 -- instantiation node), semantically belong on the spec, so copy them
6411 -- there. It's harmless to leave them on the body as well. In fact one
6412 -- could argue that they belong in both places.
6414 Citem := First (Context_Items (Body_Cunit));
6415 while Present (Citem) loop
6416 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6420 -- Propagate categorization flags on packages, so that they appear in
6421 -- the ali file for the spec of the unit.
6423 if Ekind (New_Main) = E_Package then
6424 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6425 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6426 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6427 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6428 Set_Is_Remote_Call_Interface
6429 (Old_Main, Is_Remote_Call_Interface (New_Main));
6432 -- Make entry in Units table, so that binder can generate call to
6433 -- elaboration procedure for body, if any.
6435 Make_Instance_Unit (Body_Cunit, In_Main => True);
6436 Main_Unit_Entity := New_Main;
6437 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6439 -- Build elaboration entity, since the instance may certainly generate
6440 -- elaboration code requiring a flag for protection.
6442 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6443 end Build_Instance_Compilation_Unit_Nodes;
6445 -----------------------------
6446 -- Check_Access_Definition --
6447 -----------------------------
6449 procedure Check_Access_Definition (N : Node_Id) is
6452 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6454 end Check_Access_Definition;
6456 -----------------------------------
6457 -- Check_Formal_Package_Instance --
6458 -----------------------------------
6460 -- If the formal has specific parameters, they must match those of the
6461 -- actual. Both of them are instances, and the renaming declarations for
6462 -- their formal parameters appear in the same order in both. The analyzed
6463 -- formal has been analyzed in the context of the current instance.
6465 procedure Check_Formal_Package_Instance
6466 (Formal_Pack : Entity_Id;
6467 Actual_Pack : Entity_Id)
6469 E1 : Entity_Id := First_Entity (Actual_Pack);
6470 E2 : Entity_Id := First_Entity (Formal_Pack);
6471 Prev_E1 : Entity_Id;
6476 procedure Check_Mismatch (B : Boolean);
6477 -- Common error routine for mismatch between the parameters of the
6478 -- actual instance and those of the formal package.
6480 function Is_Defaulted (Param : Entity_Id) return Boolean;
6481 -- If the formal package has partly box-initialized formals, skip
6482 -- conformance check for these formals. Previously the code assumed
6483 -- that box initialization for a formal package applied to all its
6484 -- formal parameters.
6486 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6487 -- The formal may come from a nested formal package, and the actual may
6488 -- have been constant-folded. To determine whether the two denote the
6489 -- same entity we may have to traverse several definitions to recover
6490 -- the ultimate entity that they refer to.
6492 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6493 -- The formal and the actual must be identical, but if both are
6494 -- given by attributes they end up renaming different generated bodies,
6495 -- and we must verify that the attributes themselves match.
6497 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6498 -- Similarly, if the formal comes from a nested formal package, the
6499 -- actual may designate the formal through multiple renamings, which
6500 -- have to be followed to determine the original variable in question.
6502 --------------------
6503 -- Check_Mismatch --
6504 --------------------
6506 procedure Check_Mismatch (B : Boolean) is
6507 -- A Formal_Type_Declaration for a derived private type is rewritten
6508 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6509 -- which is why we examine the original node.
6511 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6514 if Kind = N_Formal_Type_Declaration then
6517 elsif Kind in N_Formal_Object_Declaration
6518 | N_Formal_Package_Declaration
6519 | N_Formal_Subprogram_Declaration
6523 -- Ada 2012: If both formal and actual are incomplete types they
6526 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6531 ("actual for & in actual instance does not match formal",
6532 Parent (Actual_Pack), E1);
6540 function Is_Defaulted (Param : Entity_Id) return Boolean is
6545 First (Generic_Associations (Parent
6546 (Associated_Formal_Package (Actual_Pack))));
6548 while Present (Assoc) loop
6549 if Nkind (Assoc) = N_Others_Choice then
6552 elsif Nkind (Assoc) = N_Generic_Association
6553 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6555 return Box_Present (Assoc);
6564 --------------------------------
6565 -- Same_Instantiated_Constant --
6566 --------------------------------
6568 function Same_Instantiated_Constant
6569 (E1, E2 : Entity_Id) return Boolean
6575 while Present (Ent) loop
6579 elsif Ekind (Ent) /= E_Constant then
6582 elsif Is_Entity_Name (Constant_Value (Ent)) then
6583 if Entity (Constant_Value (Ent)) = E1 then
6586 Ent := Entity (Constant_Value (Ent));
6589 -- The actual may be a constant that has been folded. Recover
6592 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6593 Ent := Entity (Original_Node (Constant_Value (Ent)));
6601 end Same_Instantiated_Constant;
6603 --------------------------------
6604 -- Same_Instantiated_Function --
6605 --------------------------------
6607 function Same_Instantiated_Function
6608 (E1, E2 : Entity_Id) return Boolean
6612 if Alias (E1) = Alias (E2) then
6615 elsif Present (Alias (E2)) then
6616 U1 := Original_Node (Unit_Declaration_Node (E1));
6617 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6619 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6620 and then Nkind (Name (U1)) = N_Attribute_Reference
6622 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6623 and then Nkind (Name (U2)) = N_Attribute_Reference
6626 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6630 end Same_Instantiated_Function;
6632 --------------------------------
6633 -- Same_Instantiated_Variable --
6634 --------------------------------
6636 function Same_Instantiated_Variable
6637 (E1, E2 : Entity_Id) return Boolean
6639 function Original_Entity (E : Entity_Id) return Entity_Id;
6640 -- Follow chain of renamings to the ultimate ancestor
6642 ---------------------
6643 -- Original_Entity --
6644 ---------------------
6646 function Original_Entity (E : Entity_Id) return Entity_Id is
6651 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6652 and then Present (Renamed_Object (Orig))
6653 and then Is_Entity_Name (Renamed_Object (Orig))
6655 Orig := Entity (Renamed_Object (Orig));
6659 end Original_Entity;
6661 -- Start of processing for Same_Instantiated_Variable
6664 return Ekind (E1) = Ekind (E2)
6665 and then Original_Entity (E1) = Original_Entity (E2);
6666 end Same_Instantiated_Variable;
6668 -- Start of processing for Check_Formal_Package_Instance
6672 while Present (E1) and then Present (E2) loop
6673 exit when Ekind (E1) = E_Package
6674 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6676 -- If the formal is the renaming of the formal package, this
6677 -- is the end of its formal part, which may occur before the
6678 -- end of the formal part in the actual in the presence of
6679 -- defaulted parameters in the formal package.
6681 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6682 and then Renamed_Entity (E2) = Scope (E2);
6684 -- The analysis of the actual may generate additional internal
6685 -- entities. If the formal is defaulted, there is no corresponding
6686 -- analysis and the internal entities must be skipped, until we
6687 -- find corresponding entities again.
6689 if Comes_From_Source (E2)
6690 and then not Comes_From_Source (E1)
6691 and then Chars (E1) /= Chars (E2)
6693 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6701 -- Entities may be declared without full declaration, such as
6702 -- itypes and predefined operators (concatenation for arrays, eg).
6703 -- Skip it and keep the formal entity to find a later match for it.
6705 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6709 -- If the formal entity comes from a formal declaration, it was
6710 -- defaulted in the formal package, and no check is needed on it.
6712 elsif Nkind (Original_Node (Parent (E2))) in
6713 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6715 -- If the formal is a tagged type the corresponding class-wide
6716 -- type has been generated as well, and it must be skipped.
6718 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6724 -- Ditto for defaulted formal subprograms.
6726 elsif Is_Overloadable (E1)
6727 and then Nkind (Unit_Declaration_Node (E2)) in
6728 N_Formal_Subprogram_Declaration
6732 elsif Is_Defaulted (E1) then
6735 elsif Is_Type (E1) then
6737 -- Subtypes must statically match. E1, E2 are the local entities
6738 -- that are subtypes of the actuals. Itypes generated for other
6739 -- parameters need not be checked, the check will be performed
6740 -- on the parameters themselves.
6742 -- If E2 is a formal type declaration, it is a defaulted parameter
6743 -- and needs no checking.
6745 if not Is_Itype (E1) and then not Is_Itype (E2) then
6748 or else Etype (E1) /= Etype (E2)
6749 or else not Subtypes_Statically_Match (E1, E2));
6752 elsif Ekind (E1) = E_Constant then
6754 -- IN parameters must denote the same static value, or the same
6755 -- constant, or the literal null.
6757 Expr1 := Expression (Parent (E1));
6759 if Ekind (E2) /= E_Constant then
6760 Check_Mismatch (True);
6763 Expr2 := Expression (Parent (E2));
6766 if Is_OK_Static_Expression (Expr1) then
6767 if not Is_OK_Static_Expression (Expr2) then
6768 Check_Mismatch (True);
6770 elsif Is_Discrete_Type (Etype (E1)) then
6772 V1 : constant Uint := Expr_Value (Expr1);
6773 V2 : constant Uint := Expr_Value (Expr2);
6775 Check_Mismatch (V1 /= V2);
6778 elsif Is_Real_Type (Etype (E1)) then
6780 V1 : constant Ureal := Expr_Value_R (Expr1);
6781 V2 : constant Ureal := Expr_Value_R (Expr2);
6783 Check_Mismatch (V1 /= V2);
6786 elsif Is_String_Type (Etype (E1))
6787 and then Nkind (Expr1) = N_String_Literal
6789 if Nkind (Expr2) /= N_String_Literal then
6790 Check_Mismatch (True);
6793 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6797 elsif Is_Entity_Name (Expr1) then
6798 if Is_Entity_Name (Expr2) then
6799 if Entity (Expr1) = Entity (Expr2) then
6803 (not Same_Instantiated_Constant
6804 (Entity (Expr1), Entity (Expr2)));
6808 Check_Mismatch (True);
6811 elsif Is_Entity_Name (Original_Node (Expr1))
6812 and then Is_Entity_Name (Expr2)
6813 and then Same_Instantiated_Constant
6814 (Entity (Original_Node (Expr1)), Entity (Expr2))
6818 elsif Nkind (Expr1) = N_Null then
6819 Check_Mismatch (Nkind (Expr1) /= N_Null);
6822 Check_Mismatch (True);
6825 elsif Ekind (E1) = E_Variable then
6826 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6828 elsif Ekind (E1) = E_Package then
6830 (Ekind (E1) /= Ekind (E2)
6831 or else (Present (Renamed_Object (E2))
6832 and then Renamed_Object (E1) /=
6833 Renamed_Object (E2)));
6835 elsif Is_Overloadable (E1) then
6836 -- Verify that the actual subprograms match. Note that actuals
6837 -- that are attributes are rewritten as subprograms. If the
6838 -- subprogram in the formal package is defaulted, no check is
6839 -- needed. Note that this can only happen in Ada 2005 when the
6840 -- formal package can be partially parameterized.
6842 if Nkind (Unit_Declaration_Node (E1)) =
6843 N_Subprogram_Renaming_Declaration
6844 and then From_Default (Unit_Declaration_Node (E1))
6848 -- If the formal package has an "others" box association that
6849 -- covers this formal, there is no need for a check either.
6851 elsif Nkind (Unit_Declaration_Node (E2)) in
6852 N_Formal_Subprogram_Declaration
6853 and then Box_Present (Unit_Declaration_Node (E2))
6857 -- No check needed if subprogram is a defaulted null procedure
6859 elsif No (Alias (E2))
6860 and then Ekind (E2) = E_Procedure
6862 Null_Present (Specification (Unit_Declaration_Node (E2)))
6866 -- Otherwise the actual in the formal and the actual in the
6867 -- instantiation of the formal must match, up to renamings.
6871 (Ekind (E2) /= Ekind (E1)
6872 or else not Same_Instantiated_Function (E1, E2));
6876 raise Program_Error;
6884 end Check_Formal_Package_Instance;
6886 ---------------------------
6887 -- Check_Formal_Packages --
6888 ---------------------------
6890 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6892 Formal_P : Entity_Id;
6893 Formal_Decl : Node_Id;
6895 -- Iterate through the declarations in the instance, looking for package
6896 -- renaming declarations that denote instances of formal packages. Stop
6897 -- when we find the renaming of the current package itself. The
6898 -- declaration for a formal package without a box is followed by an
6899 -- internal entity that repeats the instantiation.
6901 E := First_Entity (P_Id);
6902 while Present (E) loop
6903 if Ekind (E) = E_Package then
6904 if Renamed_Object (E) = P_Id then
6907 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6911 Formal_Decl := Parent (Associated_Formal_Package (E));
6913 -- Nothing to check if the formal has a box or an others_clause
6914 -- (necessarily with a box), or no associations altogether
6916 if Box_Present (Formal_Decl)
6917 or else No (Generic_Associations (Formal_Decl))
6921 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6924 -- The internal validating package was generated but formal
6925 -- and instance are known to be compatible.
6927 Formal_P := Next_Entity (E);
6928 Remove (Unit_Declaration_Node (Formal_P));
6931 Formal_P := Next_Entity (E);
6933 -- If the instance is within an enclosing instance body
6934 -- there is no need to verify the legality of current formal
6935 -- packages because they were legal in the generic body.
6936 -- This optimization may be applicable elsewhere, and it
6937 -- also removes spurious errors that may arise with
6938 -- on-the-fly inlining and confusion between private and
6941 if not In_Instance_Body then
6942 Check_Formal_Package_Instance (Formal_P, E);
6945 -- Restore the visibility of formals of the formal instance
6946 -- that are not defaulted, and are hidden within the current
6947 -- generic. These formals may be visible within an enclosing
6953 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6954 while Present (Elmt) loop
6955 Set_Is_Hidden (Node (Elmt), False);
6960 -- After checking, remove the internal validating package.
6961 -- It is only needed for semantic checks, and as it may
6962 -- contain generic formal declarations it should not reach
6965 Remove (Unit_Declaration_Node (Formal_P));
6972 end Check_Formal_Packages;
6974 ---------------------------------
6975 -- Check_Forward_Instantiation --
6976 ---------------------------------
6978 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6980 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6983 -- The instantiation appears before the generic body if we are in the
6984 -- scope of the unit containing the generic, either in its spec or in
6985 -- the package body, and before the generic body.
6987 if Ekind (Gen_Comp) = E_Package_Body then
6988 Gen_Comp := Spec_Entity (Gen_Comp);
6991 if In_Open_Scopes (Gen_Comp)
6992 and then No (Corresponding_Body (Decl))
6997 and then not Is_Compilation_Unit (S)
6998 and then not Is_Child_Unit (S)
7000 if Ekind (S) = E_Package then
7001 Set_Has_Forward_Instantiation (S);
7007 end Check_Forward_Instantiation;
7009 ---------------------------
7010 -- Check_Generic_Actuals --
7011 ---------------------------
7013 -- The visibility of the actuals may be different between the point of
7014 -- generic instantiation and the instantiation of the body.
7016 procedure Check_Generic_Actuals
7017 (Instance : Entity_Id;
7018 Is_Formal_Box : Boolean)
7024 E := First_Entity (Instance);
7025 while Present (E) loop
7027 and then Nkind (Parent (E)) = N_Subtype_Declaration
7028 and then Scope (Etype (E)) /= Instance
7029 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7031 -- Restore the proper view of the actual from the information
7032 -- saved earlier by Instantiate_Type.
7034 Check_Private_View (Subtype_Indication (Parent (E)));
7036 -- If the actual is itself the formal of a parent instance,
7037 -- then also restore the proper view of its actual and so on.
7038 -- That's necessary for nested instantiations of the form
7041 -- type Component is private;
7042 -- type Array_Type is array (Positive range <>) of Component;
7045 -- when the outermost actuals have inconsistent views, because
7046 -- the Component_Type of Array_Type of the inner instantiations
7047 -- is the actual of Component of the outermost one and not that
7048 -- of the corresponding inner instantiations.
7050 Astype := Ancestor_Subtype (E);
7051 while Present (Astype)
7052 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7053 and then Present (Generic_Parent_Type (Parent (Astype)))
7054 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7056 Check_Private_View (Subtype_Indication (Parent (Astype)));
7057 Astype := Ancestor_Subtype (Astype);
7060 Set_Is_Generic_Actual_Type (E);
7062 if Is_Private_Type (E) and then Present (Full_View (E)) then
7063 Set_Is_Generic_Actual_Type (Full_View (E));
7066 Set_Is_Hidden (E, False);
7067 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7069 -- We constructed the generic actual type as a subtype of the
7070 -- supplied type. This means that it normally would not inherit
7071 -- subtype specific attributes of the actual, which is wrong for
7072 -- the generic case.
7074 Astype := Ancestor_Subtype (E);
7078 -- This can happen when E is an itype that is the full view of
7079 -- a private type completed, e.g. with a constrained array. In
7080 -- that case, use the first subtype, which will carry size
7081 -- information. The base type itself is unconstrained and will
7084 Astype := First_Subtype (E);
7087 Set_Size_Info (E, (Astype));
7088 Set_RM_Size (E, RM_Size (Astype));
7089 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7091 if Is_Discrete_Or_Fixed_Point_Type (E) then
7092 Set_RM_Size (E, RM_Size (Astype));
7095 elsif Ekind (E) = E_Package then
7097 -- If this is the renaming for the current instance, we're done.
7098 -- Otherwise it is a formal package. If the corresponding formal
7099 -- was declared with a box, the (instantiations of the) generic
7100 -- formal part are also visible. Otherwise, ignore the entity
7101 -- created to validate the actuals.
7103 if Renamed_Object (E) = Instance then
7106 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7109 -- The visibility of a formal of an enclosing generic is already
7112 elsif Denotes_Formal_Package (E) then
7115 elsif Present (Associated_Formal_Package (E))
7116 and then not Is_Generic_Formal (E)
7118 if Box_Present (Parent (Associated_Formal_Package (E))) then
7119 Check_Generic_Actuals (Renamed_Object (E), True);
7122 Check_Generic_Actuals (Renamed_Object (E), False);
7125 Set_Is_Hidden (E, False);
7128 -- If this is a subprogram instance (in a wrapper package) the
7129 -- actual is fully visible.
7131 elsif Is_Wrapper_Package (Instance) then
7132 Set_Is_Hidden (E, False);
7134 -- If the formal package is declared with a box, or if the formal
7135 -- parameter is defaulted, it is visible in the body.
7137 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7138 Set_Is_Hidden (E, False);
7141 if Ekind (E) = E_Constant then
7143 -- If the type of the actual is a private type declared in the
7144 -- enclosing scope of the generic unit, the body of the generic
7145 -- sees the full view of the type (because it has to appear in
7146 -- the corresponding package body). If the type is private now,
7147 -- exchange views to restore the proper visiblity in the instance.
7150 Typ : constant Entity_Id := Base_Type (Etype (E));
7151 -- The type of the actual
7156 Parent_Scope : Entity_Id;
7157 -- The enclosing scope of the generic unit
7160 if Is_Wrapper_Package (Instance) then
7164 (Unit_Declaration_Node
7165 (Related_Instance (Instance))));
7168 Generic_Parent (Package_Specification (Instance));
7171 Parent_Scope := Scope (Gen_Id);
7173 -- The exchange is only needed if the generic is defined
7174 -- within a package which is not a common ancestor of the
7175 -- scope of the instance, and is not already in scope.
7177 if Is_Private_Type (Typ)
7178 and then Scope (Typ) = Parent_Scope
7179 and then Scope (Instance) /= Parent_Scope
7180 and then Ekind (Parent_Scope) = E_Package
7181 and then not Is_Child_Unit (Gen_Id)
7185 -- If the type of the entity is a subtype, it may also have
7186 -- to be made visible, together with the base type of its
7187 -- full view, after exchange.
7189 if Is_Private_Type (Etype (E)) then
7190 Switch_View (Etype (E));
7191 Switch_View (Base_Type (Etype (E)));
7199 end Check_Generic_Actuals;
7201 ------------------------------
7202 -- Check_Generic_Child_Unit --
7203 ------------------------------
7205 procedure Check_Generic_Child_Unit
7207 Parent_Installed : in out Boolean)
7209 Loc : constant Source_Ptr := Sloc (Gen_Id);
7210 Gen_Par : Entity_Id := Empty;
7212 Inst_Par : Entity_Id;
7215 function Find_Generic_Child
7217 Id : Node_Id) return Entity_Id;
7218 -- Search generic parent for possible child unit with the given name
7220 function In_Enclosing_Instance return Boolean;
7221 -- Within an instance of the parent, the child unit may be denoted by
7222 -- a simple name, or an abbreviated expanded name. Examine enclosing
7223 -- scopes to locate a possible parent instantiation.
7225 ------------------------
7226 -- Find_Generic_Child --
7227 ------------------------
7229 function Find_Generic_Child
7231 Id : Node_Id) return Entity_Id
7236 -- If entity of name is already set, instance has already been
7237 -- resolved, e.g. in an enclosing instantiation.
7239 if Present (Entity (Id)) then
7240 if Scope (Entity (Id)) = Scop then
7247 E := First_Entity (Scop);
7248 while Present (E) loop
7249 if Chars (E) = Chars (Id)
7250 and then Is_Child_Unit (E)
7252 if Is_Child_Unit (E)
7253 and then not Is_Visible_Lib_Unit (E)
7256 ("generic child unit& is not visible", Gen_Id, E);
7268 end Find_Generic_Child;
7270 ---------------------------
7271 -- In_Enclosing_Instance --
7272 ---------------------------
7274 function In_Enclosing_Instance return Boolean is
7275 Enclosing_Instance : Node_Id;
7276 Instance_Decl : Node_Id;
7279 -- We do not inline any call that contains instantiations, except
7280 -- for instantiations of Unchecked_Conversion, so if we are within
7281 -- an inlined body the current instance does not require parents.
7283 if In_Inlined_Body then
7284 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7288 -- Loop to check enclosing scopes
7290 Enclosing_Instance := Current_Scope;
7291 while Present (Enclosing_Instance) loop
7292 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7294 if Ekind (Enclosing_Instance) = E_Package
7295 and then Is_Generic_Instance (Enclosing_Instance)
7297 (Generic_Parent (Specification (Instance_Decl)))
7299 -- Check whether the generic we are looking for is a child of
7302 E := Find_Generic_Child
7303 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7304 exit when Present (E);
7310 Enclosing_Instance := Scope (Enclosing_Instance);
7322 Make_Expanded_Name (Loc,
7324 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7325 Selector_Name => New_Occurrence_Of (E, Loc)));
7327 Set_Entity (Gen_Id, E);
7328 Set_Etype (Gen_Id, Etype (E));
7329 Parent_Installed := False; -- Already in scope.
7332 end In_Enclosing_Instance;
7334 -- Start of processing for Check_Generic_Child_Unit
7337 -- If the name of the generic is given by a selected component, it may
7338 -- be the name of a generic child unit, and the prefix is the name of an
7339 -- instance of the parent, in which case the child unit must be visible.
7340 -- If this instance is not in scope, it must be placed there and removed
7341 -- after instantiation, because what is being instantiated is not the
7342 -- original child, but the corresponding child present in the instance
7345 -- If the child is instantiated within the parent, it can be given by
7346 -- a simple name. In this case the instance is already in scope, but
7347 -- the child generic must be recovered from the generic parent as well.
7349 if Nkind (Gen_Id) = N_Selected_Component then
7350 S := Selector_Name (Gen_Id);
7351 Analyze (Prefix (Gen_Id));
7352 Inst_Par := Entity (Prefix (Gen_Id));
7354 if Ekind (Inst_Par) = E_Package
7355 and then Present (Renamed_Object (Inst_Par))
7357 Inst_Par := Renamed_Object (Inst_Par);
7360 if Ekind (Inst_Par) = E_Package then
7361 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7362 Gen_Par := Generic_Parent (Parent (Inst_Par));
7364 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7366 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7368 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7371 elsif Ekind (Inst_Par) = E_Generic_Package
7372 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7374 -- A formal package may be a real child package, and not the
7375 -- implicit instance within a parent. In this case the child is
7376 -- not visible and has to be retrieved explicitly as well.
7378 Gen_Par := Inst_Par;
7381 if Present (Gen_Par) then
7383 -- The prefix denotes an instantiation. The entity itself may be a
7384 -- nested generic, or a child unit.
7386 E := Find_Generic_Child (Gen_Par, S);
7389 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7390 Set_Entity (Gen_Id, E);
7391 Set_Etype (Gen_Id, Etype (E));
7393 Set_Etype (S, Etype (E));
7395 -- Indicate that this is a reference to the parent
7397 if In_Extended_Main_Source_Unit (Gen_Id) then
7398 Set_Is_Instantiated (Inst_Par);
7401 -- A common mistake is to replicate the naming scheme of a
7402 -- hierarchy by instantiating a generic child directly, rather
7403 -- than the implicit child in a parent instance:
7405 -- generic .. package Gpar is ..
7406 -- generic .. package Gpar.Child is ..
7407 -- package Par is new Gpar ();
7410 -- package Par.Child is new Gpar.Child ();
7411 -- rather than Par.Child
7413 -- In this case the instantiation is within Par, which is an
7414 -- instance, but Gpar does not denote Par because we are not IN
7415 -- the instance of Gpar, so this is illegal. The test below
7416 -- recognizes this particular case.
7418 if Is_Child_Unit (E)
7419 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7420 and then (not In_Instance
7421 or else Nkind (Parent (Parent (Gen_Id))) =
7425 ("prefix of generic child unit must be instance of parent",
7429 if not In_Open_Scopes (Inst_Par)
7430 and then Nkind (Parent (Gen_Id)) not in
7431 N_Generic_Renaming_Declaration
7433 Install_Parent (Inst_Par);
7434 Parent_Installed := True;
7436 elsif In_Open_Scopes (Inst_Par) then
7438 -- If the parent is already installed, install the actuals
7439 -- for its formal packages. This is necessary when the child
7440 -- instance is a child of the parent instance: in this case,
7441 -- the parent is placed on the scope stack but the formal
7442 -- packages are not made visible.
7444 Install_Formal_Packages (Inst_Par);
7448 -- If the generic parent does not contain an entity that
7449 -- corresponds to the selector, the instance doesn't either.
7450 -- Analyzing the node will yield the appropriate error message.
7451 -- If the entity is not a child unit, then it is an inner
7452 -- generic in the parent.
7460 if Is_Child_Unit (Entity (Gen_Id))
7462 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7463 and then not In_Open_Scopes (Inst_Par)
7465 Install_Parent (Inst_Par);
7466 Parent_Installed := True;
7468 -- The generic unit may be the renaming of the implicit child
7469 -- present in an instance. In that case the parent instance is
7470 -- obtained from the name of the renamed entity.
7472 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7473 and then Present (Renamed_Entity (Entity (Gen_Id)))
7474 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7477 Renamed_Package : constant Node_Id :=
7478 Name (Parent (Entity (Gen_Id)));
7480 if Nkind (Renamed_Package) = N_Expanded_Name then
7481 Inst_Par := Entity (Prefix (Renamed_Package));
7482 Install_Parent (Inst_Par);
7483 Parent_Installed := True;
7489 elsif Nkind (Gen_Id) = N_Expanded_Name then
7491 -- Entity already present, analyze prefix, whose meaning may be an
7492 -- instance in the current context. If it is an instance of a
7493 -- relative within another, the proper parent may still have to be
7494 -- installed, if they are not of the same generation.
7496 Analyze (Prefix (Gen_Id));
7498 -- Prevent cascaded errors
7500 if Etype (Prefix (Gen_Id)) = Any_Type then
7504 -- In the unlikely case that a local declaration hides the name of
7505 -- the parent package, locate it on the homonym chain. If the context
7506 -- is an instance of the parent, the renaming entity is flagged as
7509 Inst_Par := Entity (Prefix (Gen_Id));
7510 while Present (Inst_Par)
7511 and then not Is_Package_Or_Generic_Package (Inst_Par)
7513 Inst_Par := Homonym (Inst_Par);
7516 pragma Assert (Present (Inst_Par));
7517 Set_Entity (Prefix (Gen_Id), Inst_Par);
7519 if In_Enclosing_Instance then
7522 elsif Present (Entity (Gen_Id))
7523 and then No (Renamed_Entity (Entity (Gen_Id)))
7524 and then Is_Child_Unit (Entity (Gen_Id))
7525 and then not In_Open_Scopes (Inst_Par)
7527 Install_Parent (Inst_Par);
7528 Parent_Installed := True;
7530 -- Handle renaming of generic child unit
7532 elsif Present (Entity (Gen_Id))
7533 and then Present (Renamed_Entity (Entity (Gen_Id)))
7534 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7541 -- The entity of the renamed generic child unit does not
7542 -- have any reference to the instantiated parent. In order to
7543 -- locate it we traverse the scope containing the renaming
7544 -- declaration; the instance of the parent is available in
7545 -- the prefix of the renaming declaration. For example:
7548 -- package Inst_Par is new ...
7549 -- generic package Ren_Child renames Ins_Par.Child;
7554 -- package Inst_Child is new A.Ren_Child;
7557 E := First_Entity (Entity (Prefix (Gen_Id)));
7558 while Present (E) loop
7559 if Present (Renamed_Entity (E))
7561 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7563 Ren_Decl := Parent (E);
7564 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7566 if not In_Open_Scopes (Inst_Par) then
7567 Install_Parent (Inst_Par);
7568 Parent_Installed := True;
7574 E := Next_Entity (E);
7579 elsif In_Enclosing_Instance then
7581 -- The child unit is found in some enclosing scope
7588 -- If this is the renaming of the implicit child in a parent
7589 -- instance, recover the parent name and install it.
7591 if Is_Entity_Name (Gen_Id) then
7592 E := Entity (Gen_Id);
7594 if Is_Generic_Unit (E)
7595 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7596 and then Is_Child_Unit (Renamed_Object (E))
7597 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7598 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7600 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7601 Inst_Par := Entity (Prefix (Gen_Id));
7603 if not In_Open_Scopes (Inst_Par) then
7604 Install_Parent (Inst_Par);
7605 Parent_Installed := True;
7608 -- If it is a child unit of a non-generic parent, it may be
7609 -- use-visible and given by a direct name. Install parent as
7612 elsif Is_Generic_Unit (E)
7613 and then Is_Child_Unit (E)
7615 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7616 and then not Is_Generic_Unit (Scope (E))
7618 if not In_Open_Scopes (Scope (E)) then
7619 Install_Parent (Scope (E));
7620 Parent_Installed := True;
7625 end Check_Generic_Child_Unit;
7627 -----------------------------
7628 -- Check_Hidden_Child_Unit --
7629 -----------------------------
7631 procedure Check_Hidden_Child_Unit
7633 Gen_Unit : Entity_Id;
7634 Act_Decl_Id : Entity_Id)
7636 Gen_Id : constant Node_Id := Name (N);
7639 if Is_Child_Unit (Gen_Unit)
7640 and then Is_Child_Unit (Act_Decl_Id)
7641 and then Nkind (Gen_Id) = N_Expanded_Name
7642 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7643 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7645 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7647 ("generic unit & is implicitly declared in &",
7648 Defining_Unit_Name (N), Gen_Unit);
7649 Error_Msg_N ("\instance must have different name",
7650 Defining_Unit_Name (N));
7652 end Check_Hidden_Child_Unit;
7654 ------------------------
7655 -- Check_Private_View --
7656 ------------------------
7658 procedure Check_Private_View (N : Node_Id) is
7659 T : constant Entity_Id := Etype (N);
7663 -- Exchange views if the type was not private in the generic but is
7664 -- private at the point of instantiation. Do not exchange views if
7665 -- the scope of the type is in scope. This can happen if both generic
7666 -- and instance are sibling units, or if type is defined in a parent.
7667 -- In this case the visibility of the type will be correct for all
7671 BT := Base_Type (T);
7673 if Is_Private_Type (T)
7674 and then not Has_Private_View (N)
7675 and then Present (Full_View (T))
7676 and then not In_Open_Scopes (Scope (T))
7678 -- In the generic, the full declaration was visible
7682 elsif Has_Private_View (N)
7683 and then not Is_Private_Type (T)
7684 and then not Has_Been_Exchanged (T)
7685 and then (not In_Open_Scopes (Scope (T))
7686 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7688 -- In the generic, only the private declaration was visible
7690 -- If the type appears in a subtype declaration, the subtype in
7691 -- instance must have a view compatible with that of its parent,
7692 -- which must be exchanged (see corresponding code in Restore_
7693 -- Private_Views) so we make an exception to the open scope rule.
7695 Prepend_Elmt (T, Exchanged_Views);
7696 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7698 -- Finally, a non-private subtype may have a private base type, which
7699 -- must be exchanged for consistency. This can happen when a package
7700 -- body is instantiated, when the scope stack is empty but in fact
7701 -- the subtype and the base type are declared in an enclosing scope.
7703 -- Note that in this case we introduce an inconsistency in the view
7704 -- set, because we switch the base type BT, but there could be some
7705 -- private dependent subtypes of BT which remain unswitched. Such
7706 -- subtypes might need to be switched at a later point (see specific
7707 -- provision for that case in Switch_View).
7709 elsif not Is_Private_Type (T)
7710 and then not Has_Private_View (N)
7711 and then Is_Private_Type (BT)
7712 and then Present (Full_View (BT))
7713 and then not Is_Generic_Type (BT)
7714 and then not In_Open_Scopes (BT)
7716 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7717 Exchange_Declarations (BT);
7720 end Check_Private_View;
7722 -----------------------------
7723 -- Check_Hidden_Primitives --
7724 -----------------------------
7726 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7729 Result : Elist_Id := No_Elist;
7732 if No (Assoc_List) then
7736 -- Traverse the list of associations between formals and actuals
7737 -- searching for renamings of tagged types
7739 Actual := First (Assoc_List);
7740 while Present (Actual) loop
7741 if Nkind (Actual) = N_Subtype_Declaration then
7742 Gen_T := Generic_Parent_Type (Actual);
7744 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7746 -- Traverse the list of primitives of the actual types
7747 -- searching for hidden primitives that are visible in the
7748 -- corresponding generic formal; leave them visible and
7749 -- append them to Result to restore their decoration later.
7751 Install_Hidden_Primitives
7752 (Prims_List => Result,
7754 Act_T => Entity (Subtype_Indication (Actual)));
7762 end Check_Hidden_Primitives;
7764 --------------------------
7765 -- Contains_Instance_Of --
7766 --------------------------
7768 function Contains_Instance_Of
7771 N : Node_Id) return Boolean
7779 -- Verify that there are no circular instantiations. We check whether
7780 -- the unit contains an instance of the current scope or some enclosing
7781 -- scope (in case one of the instances appears in a subunit). Longer
7782 -- circularities involving subunits might seem too pathological to
7783 -- consider, but they were not too pathological for the authors of
7784 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7785 -- enclosing generic scopes as containing an instance.
7788 -- Within a generic subprogram body, the scope is not generic, to
7789 -- allow for recursive subprograms. Use the declaration to determine
7790 -- whether this is a generic unit.
7792 if Ekind (Scop) = E_Generic_Package
7793 or else (Is_Subprogram (Scop)
7794 and then Nkind (Unit_Declaration_Node (Scop)) =
7795 N_Generic_Subprogram_Declaration)
7797 Elmt := First_Elmt (Inner_Instances (Inner));
7799 while Present (Elmt) loop
7800 if Node (Elmt) = Scop then
7801 Error_Msg_Node_2 := Inner;
7803 ("circular Instantiation: & instantiated within &!",
7807 elsif Node (Elmt) = Inner then
7810 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7811 Error_Msg_Node_2 := Inner;
7813 ("circular Instantiation: & instantiated within &!",
7821 -- Indicate that Inner is being instantiated within Scop
7823 Append_Elmt (Inner, Inner_Instances (Scop));
7826 if Scop = Standard_Standard then
7829 Scop := Scope (Scop);
7834 end Contains_Instance_Of;
7836 -----------------------
7837 -- Copy_Generic_Node --
7838 -----------------------
7840 function Copy_Generic_Node
7842 Parent_Id : Node_Id;
7843 Instantiating : Boolean) return Node_Id
7848 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7849 -- Check the given value of one of the Fields referenced by the current
7850 -- node to determine whether to copy it recursively. The field may hold
7851 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7852 -- Char) in which case it need not be copied.
7854 procedure Copy_Descendants;
7855 -- Common utility for various nodes
7857 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7858 -- Make copy of element list
7860 function Copy_Generic_List
7862 Parent_Id : Node_Id) return List_Id;
7863 -- Apply Copy_Node recursively to the members of a node list
7865 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7866 -- True if an identifier is part of the defining program unit name of
7868 -- Consider removing this subprogram now that ASIS no longer uses it.
7870 ----------------------
7871 -- Copy_Descendants --
7872 ----------------------
7874 procedure Copy_Descendants is
7875 use Atree.Unchecked_Access;
7876 -- This code section is part of the implementation of an untyped
7877 -- tree traversal, so it needs direct access to node fields.
7880 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7881 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7882 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7883 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7884 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7885 end Copy_Descendants;
7887 -----------------------------
7888 -- Copy_Generic_Descendant --
7889 -----------------------------
7891 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7893 if D = Union_Id (Empty) then
7896 elsif D in Node_Range then
7898 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7900 elsif D in List_Range then
7901 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7903 elsif D in Elist_Range then
7904 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7906 -- Nothing else is copyable (e.g. Uint values), return as is
7911 end Copy_Generic_Descendant;
7913 ------------------------
7914 -- Copy_Generic_Elist --
7915 ------------------------
7917 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7924 M := First_Elmt (E);
7925 while Present (M) loop
7927 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7936 end Copy_Generic_Elist;
7938 -----------------------
7939 -- Copy_Generic_List --
7940 -----------------------
7942 function Copy_Generic_List
7944 Parent_Id : Node_Id) return List_Id
7952 Set_Parent (New_L, Parent_Id);
7955 while Present (N) loop
7956 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7965 end Copy_Generic_List;
7967 ---------------------------
7968 -- In_Defining_Unit_Name --
7969 ---------------------------
7971 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7974 Present (Parent (Nam))
7975 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7977 (Nkind (Parent (Nam)) = N_Expanded_Name
7978 and then In_Defining_Unit_Name (Parent (Nam))));
7979 end In_Defining_Unit_Name;
7981 -- Start of processing for Copy_Generic_Node
7988 New_N := New_Copy (N);
7990 -- Copy aspects if present
7992 if Has_Aspects (N) then
7993 Set_Has_Aspects (New_N, False);
7994 Set_Aspect_Specifications
7995 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7998 -- If we are instantiating, we want to adjust the sloc based on the
7999 -- current S_Adjustment. However, if this is the root node of a subunit,
8000 -- we need to defer that adjustment to below (see "elsif Instantiating
8001 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8002 -- computed the adjustment.
8005 and then not (Nkind (N) in N_Proper_Body
8006 and then Was_Originally_Stub (N))
8008 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8011 if not Is_List_Member (N) then
8012 Set_Parent (New_N, Parent_Id);
8015 -- Special casing for identifiers and other entity names and operators
8017 if Nkind (New_N) in N_Character_Literal
8023 if not Instantiating then
8025 -- Link both nodes in order to assign subsequently the entity of
8026 -- the copy to the original node, in case this is a global
8029 Set_Associated_Node (N, New_N);
8031 -- If we are within an instantiation, this is a nested generic
8032 -- that has already been analyzed at the point of definition.
8033 -- We must preserve references that were global to the enclosing
8034 -- parent at that point. Other occurrences, whether global or
8035 -- local to the current generic, must be resolved anew, so we
8036 -- reset the entity in the generic copy. A global reference has a
8037 -- smaller depth than the parent, or else the same depth in case
8038 -- both are distinct compilation units.
8040 -- A child unit is implicitly declared within the enclosing parent
8041 -- but is in fact global to it, and must be preserved.
8043 -- It is also possible for Current_Instantiated_Parent to be
8044 -- defined, and for this not to be a nested generic, namely if
8045 -- the unit is loaded through Rtsfind. In that case, the entity of
8046 -- New_N is only a link to the associated node, and not a defining
8049 -- The entities for parent units in the defining_program_unit of a
8050 -- generic child unit are established when the context of the unit
8051 -- is first analyzed, before the generic copy is made. They are
8052 -- preserved in the copy for use in e.g. ASIS queries.
8054 Ent := Entity (New_N);
8056 if No (Current_Instantiated_Parent.Gen_Id) then
8058 or else Nkind (Ent) /= N_Defining_Identifier
8059 or else not In_Defining_Unit_Name (N)
8061 Set_Associated_Node (New_N, Empty);
8065 or else Nkind (Ent) not in N_Entity
8066 or else No (Scope (Ent))
8068 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8069 and then not Is_Child_Unit (Ent))
8071 (Scope_Depth (Scope (Ent)) >
8072 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8074 Get_Source_Unit (Ent) =
8075 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8077 Set_Associated_Node (New_N, Empty);
8080 -- Case of instantiating identifier or some other name or operator
8083 -- If the associated node is still defined, the entity in it
8084 -- is global, and must be copied to the instance. If this copy
8085 -- is being made for a body to inline, it is applied to an
8086 -- instantiated tree, and the entity is already present and
8087 -- must be also preserved.
8090 Assoc : constant Node_Id := Get_Associated_Node (N);
8093 if Present (Assoc) then
8094 if Nkind (Assoc) = Nkind (N) then
8095 Set_Entity (New_N, Entity (Assoc));
8096 Check_Private_View (N);
8098 -- Here we deal with a very peculiar case for which the
8099 -- Has_Private_View mechanism is not sufficient, because
8100 -- the reference to the type is implicit in the tree,
8101 -- that is to say, it's not referenced from a node but
8102 -- only from another type, namely through Component_Type.
8106 -- type Pt is private;
8109 -- type Ft is array (Positive range <>) of Pt;
8111 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8115 -- type Pt is new Boolean;
8118 -- package body P is
8119 -- package body G is
8120 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8122 -- if (F1 < F2) /= Lt then
8129 -- type Arr is array (Positive range <>) of P.Pt;
8131 -- package Inst is new P.G (Arr);
8133 -- Pt is a global type for the generic package G and it
8134 -- is not referenced in its body, but only as component
8135 -- type of Ft, which is a local type. This means that no
8136 -- references to Pt or Ft are seen during the copy of the
8137 -- body, the only reference to Pt being seen is when the
8138 -- actuals are checked by Check_Generic_Actuals, but Pt
8139 -- is still private at this point. In the end, the views
8140 -- of Pt are not switched in the body and, therefore, the
8141 -- array comparison is rejected because the component is
8144 -- Adding e.g. a dummy variable of type Pt in the body is
8145 -- sufficient to make everything work, so we generate an
8146 -- artificial reference to Pt on the fly and thus force
8147 -- the switching of views on the grounds that, if the
8148 -- comparison was accepted during the semantic analysis
8149 -- of the generic, this means that the component cannot
8150 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8152 if Nkind (Assoc) in N_Op_Compare
8153 and then Present (Etype (Left_Opnd (Assoc)))
8154 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8155 and then Present (Etype (Right_Opnd (Assoc)))
8156 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8159 Ltyp : constant Entity_Id :=
8160 Etype (Left_Opnd (Assoc));
8161 Rtyp : constant Entity_Id :=
8162 Etype (Right_Opnd (Assoc));
8164 if Is_Private_Type (Component_Type (Ltyp)) then
8166 (New_Occurrence_Of (Component_Type (Ltyp),
8169 if Is_Private_Type (Component_Type (Rtyp)) then
8171 (New_Occurrence_Of (Component_Type (Rtyp),
8176 -- Here is a similar case, for the Designated_Type of an
8177 -- access type that is present as target type in a type
8178 -- conversion from another access type. In this case, if
8179 -- the base types of the designated types are different
8180 -- and the conversion was accepted during the semantic
8181 -- analysis of the generic, this means that the target
8182 -- type cannot have been private (see Valid_Conversion).
8184 elsif Nkind (Assoc) = N_Identifier
8185 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8186 and then Subtype_Mark (Parent (Assoc)) = Assoc
8187 and then Present (Etype (Assoc))
8188 and then Is_Access_Type (Etype (Assoc))
8189 and then Present (Etype (Expression (Parent (Assoc))))
8191 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8194 Targ_Desig : constant Entity_Id :=
8195 Designated_Type (Etype (Assoc));
8196 Expr_Desig : constant Entity_Id :=
8198 (Etype (Expression (Parent (Assoc))));
8200 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8201 and then Is_Private_Type (Targ_Desig)
8204 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8209 -- The node is a reference to a global type and acts as the
8210 -- subtype mark of a qualified expression created in order
8211 -- to aid resolution of accidental overloading in instances.
8212 -- Since N is a reference to a type, the Associated_Node of
8213 -- N denotes an entity rather than another identifier. See
8214 -- Qualify_Universal_Operands for details.
8216 elsif Nkind (N) = N_Identifier
8217 and then Nkind (Parent (N)) = N_Qualified_Expression
8218 and then Subtype_Mark (Parent (N)) = N
8219 and then Is_Qualified_Universal_Literal (Parent (N))
8221 Set_Entity (New_N, Assoc);
8223 -- The name in the call may be a selected component if the
8224 -- call has not been analyzed yet, as may be the case for
8225 -- pre/post conditions in a generic unit.
8227 elsif Nkind (Assoc) = N_Function_Call
8228 and then Is_Entity_Name (Name (Assoc))
8230 Set_Entity (New_N, Entity (Name (Assoc)));
8232 elsif Nkind (Assoc) in N_Entity
8233 and then Expander_Active
8235 -- Inlining case: we are copying a tree that contains
8236 -- global entities, which are preserved in the copy to be
8237 -- used for subsequent inlining.
8242 Set_Entity (New_N, Empty);
8248 -- For expanded name, we must copy the Prefix and Selector_Name
8250 if Nkind (N) = N_Expanded_Name then
8252 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8254 Set_Selector_Name (New_N,
8255 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8257 -- For operators, copy the operands
8259 elsif Nkind (N) in N_Op then
8260 if Nkind (N) in N_Binary_Op then
8261 Set_Left_Opnd (New_N,
8262 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8265 Set_Right_Opnd (New_N,
8266 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8269 -- Establish a link between an entity from the generic template and the
8270 -- corresponding entity in the generic copy to be analyzed.
8272 elsif Nkind (N) in N_Entity then
8273 if not Instantiating then
8274 Set_Associated_Entity (N, New_N);
8277 -- Clear any existing link the copy may inherit from the replicated
8278 -- generic template entity.
8280 Set_Associated_Entity (New_N, Empty);
8282 -- Special casing for stubs
8284 elsif Nkind (N) in N_Body_Stub then
8286 -- In any case, we must copy the specification or defining
8287 -- identifier as appropriate.
8289 if Nkind (N) = N_Subprogram_Body_Stub then
8290 Set_Specification (New_N,
8291 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8294 Set_Defining_Identifier (New_N,
8296 (Defining_Identifier (N), New_N, Instantiating));
8299 -- If we are not instantiating, then this is where we load and
8300 -- analyze subunits, i.e. at the point where the stub occurs. A
8301 -- more permissive system might defer this analysis to the point
8302 -- of instantiation, but this seems too complicated for now.
8304 if not Instantiating then
8306 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8308 Unum : Unit_Number_Type;
8312 -- Make sure that, if it is a subunit of the main unit that is
8313 -- preprocessed and if -gnateG is specified, the preprocessed
8314 -- file will be written.
8316 Lib.Analysing_Subunit_Of_Main :=
8317 Lib.In_Extended_Main_Source_Unit (N);
8320 (Load_Name => Subunit_Name,
8324 Lib.Analysing_Subunit_Of_Main := False;
8326 -- If the proper body is not found, a warning message will be
8327 -- emitted when analyzing the stub, or later at the point of
8328 -- instantiation. Here we just leave the stub as is.
8330 if Unum = No_Unit then
8331 Subunits_Missing := True;
8332 goto Subunit_Not_Found;
8335 Subunit := Cunit (Unum);
8337 if Nkind (Unit (Subunit)) /= N_Subunit then
8339 ("found child unit instead of expected SEPARATE subunit",
8341 Error_Msg_Sloc := Sloc (N);
8342 Error_Msg_N ("\to complete stub #", Subunit);
8343 goto Subunit_Not_Found;
8346 -- We must create a generic copy of the subunit, in order to
8347 -- perform semantic analysis on it, and we must replace the
8348 -- stub in the original generic unit with the subunit, in order
8349 -- to preserve non-local references within.
8351 -- Only the proper body needs to be copied. Library_Unit and
8352 -- context clause are simply inherited by the generic copy.
8353 -- Note that the copy (which may be recursive if there are
8354 -- nested subunits) must be done first, before attaching it to
8355 -- the enclosing generic.
8359 (Proper_Body (Unit (Subunit)),
8360 Empty, Instantiating => False);
8362 -- Now place the original proper body in the original generic
8363 -- unit. This is a body, not a compilation unit.
8365 Rewrite (N, Proper_Body (Unit (Subunit)));
8366 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8367 Set_Was_Originally_Stub (N);
8369 -- Finally replace the body of the subunit with its copy, and
8370 -- make this new subunit into the library unit of the generic
8371 -- copy, which does not have stubs any longer.
8373 Set_Proper_Body (Unit (Subunit), New_Body);
8374 Set_Library_Unit (New_N, Subunit);
8375 Inherit_Context (Unit (Subunit), N);
8378 -- If we are instantiating, this must be an error case, since
8379 -- otherwise we would have replaced the stub node by the proper body
8380 -- that corresponds. So just ignore it in the copy (i.e. we have
8381 -- copied it, and that is good enough).
8387 <<Subunit_Not_Found>> null;
8389 -- If the node is a compilation unit, it is the subunit of a stub, which
8390 -- has been loaded already (see code below). In this case, the library
8391 -- unit field of N points to the parent unit (which is a compilation
8392 -- unit) and need not (and cannot) be copied.
8394 -- When the proper body of the stub is analyzed, the library_unit link
8395 -- is used to establish the proper context (see sem_ch10).
8397 -- The other fields of a compilation unit are copied as usual
8399 elsif Nkind (N) = N_Compilation_Unit then
8401 -- This code can only be executed when not instantiating, because in
8402 -- the copy made for an instantiation, the compilation unit node has
8403 -- disappeared at the point that a stub is replaced by its proper
8406 pragma Assert (not Instantiating);
8408 Set_Context_Items (New_N,
8409 Copy_Generic_List (Context_Items (N), New_N));
8412 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8414 Set_First_Inlined_Subprogram (New_N,
8416 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8421 (Aux_Decls_Node (N), New_N, Instantiating => False));
8423 -- For an assignment node, the assignment is known to be semantically
8424 -- legal if we are instantiating the template. This avoids incorrect
8425 -- diagnostics in generated code.
8427 elsif Nkind (N) = N_Assignment_Statement then
8429 -- Copy name and expression fields in usual manner
8432 Copy_Generic_Node (Name (N), New_N, Instantiating));
8434 Set_Expression (New_N,
8435 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8437 if Instantiating then
8438 Set_Assignment_OK (Name (New_N), True);
8441 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8442 if not Instantiating then
8443 Set_Associated_Node (N, New_N);
8446 if Present (Get_Associated_Node (N))
8447 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8449 -- In the generic the aggregate has some composite type. If at
8450 -- the point of instantiation the type has a private view,
8451 -- install the full view (and that of its ancestors, if any).
8454 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8458 if Present (T) and then Is_Private_Type (T) then
8463 and then Is_Tagged_Type (T)
8464 and then Is_Derived_Type (T)
8466 Rt := Root_Type (T);
8471 if Is_Private_Type (T) then
8482 -- Do not copy the associated node, which points to the generic copy
8483 -- of the aggregate.
8486 use Atree.Unchecked_Access;
8487 -- This code section is part of the implementation of an untyped
8488 -- tree traversal, so it needs direct access to node fields.
8491 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8492 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8493 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8494 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8497 -- Allocators do not have an identifier denoting the access type, so we
8498 -- must locate it through the expression to check whether the views are
8501 elsif Nkind (N) = N_Allocator
8502 and then Nkind (Expression (N)) = N_Qualified_Expression
8503 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8504 and then Instantiating
8507 T : constant Node_Id :=
8508 Get_Associated_Node (Subtype_Mark (Expression (N)));
8514 -- Retrieve the allocator node in the generic copy
8516 Acc_T := Etype (Parent (Parent (T)));
8518 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8519 Switch_View (Acc_T);
8526 -- For a proper body, we must catch the case of a proper body that
8527 -- replaces a stub. This represents the point at which a separate
8528 -- compilation unit, and hence template file, may be referenced, so we
8529 -- must make a new source instantiation entry for the template of the
8530 -- subunit, and ensure that all nodes in the subunit are adjusted using
8531 -- this new source instantiation entry.
8533 elsif Nkind (N) in N_Proper_Body then
8535 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8537 if Instantiating and then Was_Originally_Stub (N) then
8538 Create_Instantiation_Source
8539 (Instantiation_Node,
8540 Defining_Entity (N),
8543 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8546 -- Now copy the fields of the proper body, using the new
8547 -- adjustment factor if one was needed as per test above.
8551 -- Restore the original adjustment factor
8553 S_Adjustment := Save_Adjustment;
8556 elsif Nkind (N) = N_Pragma and then Instantiating then
8558 -- Do not copy Comment or Ident pragmas their content is relevant to
8559 -- the generic unit, not to the instantiating unit.
8561 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8562 New_N := Make_Null_Statement (Sloc (N));
8564 -- Do not copy pragmas generated from aspects because the pragmas do
8565 -- not carry any semantic information, plus they will be regenerated
8568 -- However, generating C we need to copy them since postconditions
8569 -- are inlined by the front end, and the front-end inlining machinery
8570 -- relies on this routine to perform inlining.
8572 elsif From_Aspect_Specification (N)
8573 and then not Modify_Tree_For_C
8575 New_N := Make_Null_Statement (Sloc (N));
8581 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8583 -- No descendant fields need traversing
8587 elsif Nkind (N) = N_String_Literal
8588 and then Present (Etype (N))
8589 and then Instantiating
8591 -- If the string is declared in an outer scope, the string_literal
8592 -- subtype created for it may have the wrong scope. Force reanalysis
8593 -- of the constant to generate a new itype in the proper context.
8595 Set_Etype (New_N, Empty);
8596 Set_Analyzed (New_N, False);
8598 -- For the remaining nodes, copy their descendants recursively
8603 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8604 Set_Generic_Parent (Specification (New_N), N);
8606 -- Should preserve Corresponding_Spec??? (12.3(14))
8610 -- Propagate dimensions if present, so that they are reflected in the
8613 if Nkind (N) in N_Has_Etype
8614 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8615 and then Present (Etype (N))
8616 and then Is_Floating_Point_Type (Etype (N))
8617 and then Has_Dimension_System (Etype (N))
8619 Copy_Dimensions (N, New_N);
8623 end Copy_Generic_Node;
8625 ----------------------------
8626 -- Denotes_Formal_Package --
8627 ----------------------------
8629 function Denotes_Formal_Package
8631 On_Exit : Boolean := False;
8632 Instance : Entity_Id := Empty) return Boolean
8635 Scop : constant Entity_Id := Scope (Pack);
8638 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8639 -- The package in question may be an actual for a previous formal
8640 -- package P of the current instance, so examine its actuals as well.
8641 -- This must be recursive over other formal packages.
8643 ----------------------------------
8644 -- Is_Actual_Of_Previous_Formal --
8645 ----------------------------------
8647 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8651 E1 := First_Entity (P);
8652 while Present (E1) and then E1 /= Instance loop
8653 if Ekind (E1) = E_Package
8654 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8656 if Renamed_Object (E1) = Pack then
8659 elsif E1 = P or else Renamed_Object (E1) = P then
8662 elsif Is_Actual_Of_Previous_Formal (E1) then
8671 end Is_Actual_Of_Previous_Formal;
8673 -- Start of processing for Denotes_Formal_Package
8679 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8681 Par := Current_Instantiated_Parent.Act_Id;
8684 if Ekind (Scop) = E_Generic_Package
8685 or else Nkind (Unit_Declaration_Node (Scop)) =
8686 N_Generic_Subprogram_Declaration
8690 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8691 N_Formal_Package_Declaration
8699 -- Check whether this package is associated with a formal package of
8700 -- the enclosing instantiation. Iterate over the list of renamings.
8702 E := First_Entity (Par);
8703 while Present (E) loop
8704 if Ekind (E) /= E_Package
8705 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8709 elsif Renamed_Object (E) = Par then
8712 elsif Renamed_Object (E) = Pack then
8715 elsif Is_Actual_Of_Previous_Formal (E) then
8725 end Denotes_Formal_Package;
8731 procedure End_Generic is
8733 -- ??? More things could be factored out in this routine. Should
8734 -- probably be done at a later stage.
8736 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8737 Generic_Flags.Decrement_Last;
8739 Expander_Mode_Restore;
8746 function Earlier (N1, N2 : Node_Id) return Boolean is
8747 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8748 -- Find distance from given node to enclosing compilation unit
8754 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8757 and then Nkind (P) /= N_Compilation_Unit
8759 P := True_Parent (P);
8764 -- Local declarations
8773 -- Start of processing for Earlier
8776 Find_Depth (P1, D1);
8777 Find_Depth (P2, D2);
8787 P1 := True_Parent (P1);
8792 P2 := True_Parent (P2);
8796 -- At this point P1 and P2 are at the same distance from the root.
8797 -- We examine their parents until we find a common declarative list.
8798 -- If we reach the root, N1 and N2 do not descend from the same
8799 -- declarative list (e.g. one is nested in the declarative part and
8800 -- the other is in a block in the statement part) and the earlier
8801 -- one is already frozen.
8803 while not Is_List_Member (P1)
8804 or else not Is_List_Member (P2)
8805 or else not In_Same_List (P1, P2)
8807 P1 := True_Parent (P1);
8808 P2 := True_Parent (P2);
8810 if Nkind (Parent (P1)) = N_Subunit then
8811 P1 := Corresponding_Stub (Parent (P1));
8814 if Nkind (Parent (P2)) = N_Subunit then
8815 P2 := Corresponding_Stub (Parent (P2));
8823 -- Expanded code usually shares the source location of the original
8824 -- construct it was generated for. This however may not necessarily
8825 -- reflect the true location of the code within the tree.
8827 -- Before comparing the slocs of the two nodes, make sure that we are
8828 -- working with correct source locations. Assume that P1 is to the left
8829 -- of P2. If either one does not come from source, traverse the common
8830 -- list heading towards the other node and locate the first source
8834 -- ----+===+===+--------------+===+===+----
8835 -- expanded code expanded code
8837 if not Comes_From_Source (P1) then
8838 while Present (P1) loop
8840 -- Neither P2 nor a source statement were located during the
8841 -- search. If we reach the end of the list, then P1 does not
8842 -- occur earlier than P2.
8845 -- start --- P2 ----- P1 --- end
8847 if No (Next (P1)) then
8850 -- We encounter P2 while going to the right of the list. This
8851 -- means that P1 does indeed appear earlier.
8854 -- start --- P1 ===== P2 --- end
8855 -- expanded code in between
8860 -- No need to look any further since we have located a source
8863 elsif Comes_From_Source (P1) then
8873 if not Comes_From_Source (P2) then
8874 while Present (P2) loop
8876 -- Neither P1 nor a source statement were located during the
8877 -- search. If we reach the start of the list, then P1 does not
8878 -- occur earlier than P2.
8881 -- start --- P2 --- P1 --- end
8883 if No (Prev (P2)) then
8886 -- We encounter P1 while going to the left of the list. This
8887 -- means that P1 does indeed appear earlier.
8890 -- start --- P1 ===== P2 --- end
8891 -- expanded code in between
8896 -- No need to look any further since we have located a source
8899 elsif Comes_From_Source (P2) then
8909 -- At this point either both nodes came from source or we approximated
8910 -- their source locations through neighboring source statements.
8912 T1 := Top_Level_Location (Sloc (P1));
8913 T2 := Top_Level_Location (Sloc (P2));
8915 -- When two nodes come from the same instance, they have identical top
8916 -- level locations. To determine proper relation within the tree, check
8917 -- their locations within the template.
8920 return Sloc (P1) < Sloc (P2);
8922 -- The two nodes either come from unrelated instances or do not come
8923 -- from instantiated code at all.
8930 ----------------------
8931 -- Find_Actual_Type --
8932 ----------------------
8934 function Find_Actual_Type
8936 Gen_Type : Entity_Id) return Entity_Id
8938 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8942 -- Special processing only applies to child units
8944 if not Is_Child_Unit (Gen_Scope) then
8945 return Get_Instance_Of (Typ);
8947 -- If designated or component type is itself a formal of the child unit,
8948 -- its instance is available.
8950 elsif Scope (Typ) = Gen_Scope then
8951 return Get_Instance_Of (Typ);
8953 -- If the array or access type is not declared in the parent unit,
8954 -- no special processing needed.
8956 elsif not Is_Generic_Type (Typ)
8957 and then Scope (Gen_Scope) /= Scope (Typ)
8959 return Get_Instance_Of (Typ);
8961 -- Otherwise, retrieve designated or component type by visibility
8964 T := Current_Entity (Typ);
8965 while Present (T) loop
8966 if In_Open_Scopes (Scope (T)) then
8968 elsif Is_Generic_Actual_Type (T) then
8977 end Find_Actual_Type;
8979 ----------------------------
8980 -- Freeze_Subprogram_Body --
8981 ----------------------------
8983 procedure Freeze_Subprogram_Body
8984 (Inst_Node : Node_Id;
8986 Pack_Id : Entity_Id)
8988 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8989 Par : constant Entity_Id := Scope (Gen_Unit);
8995 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8996 -- Find innermost package body that encloses the given node, and which
8997 -- is not a compilation unit. Freeze nodes for the instance, or for its
8998 -- enclosing body, may be inserted after the enclosing_body of the
8999 -- generic unit. Used to determine proper placement of freeze node for
9000 -- both package and subprogram instances.
9002 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9003 -- Find entity for given package body, and locate or create a freeze
9006 ----------------------------
9007 -- Enclosing_Package_Body --
9008 ----------------------------
9010 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9016 and then Nkind (Parent (P)) /= N_Compilation_Unit
9018 if Nkind (P) = N_Package_Body then
9019 if Nkind (Parent (P)) = N_Subunit then
9020 return Corresponding_Stub (Parent (P));
9026 P := True_Parent (P);
9030 end Enclosing_Package_Body;
9032 -------------------------
9033 -- Package_Freeze_Node --
9034 -------------------------
9036 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9040 if Nkind (B) = N_Package_Body then
9041 Id := Corresponding_Spec (B);
9042 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9043 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9046 Ensure_Freeze_Node (Id);
9047 return Freeze_Node (Id);
9048 end Package_Freeze_Node;
9050 -- Start of processing for Freeze_Subprogram_Body
9053 -- If the instance and the generic body appear within the same unit, and
9054 -- the instance precedes the generic, the freeze node for the instance
9055 -- must appear after that of the generic. If the generic is nested
9056 -- within another instance I2, then current instance must be frozen
9057 -- after I2. In both cases, the freeze nodes are those of enclosing
9058 -- packages. Otherwise, the freeze node is placed at the end of the
9059 -- current declarative part.
9061 Enc_G := Enclosing_Package_Body (Gen_Body);
9062 Enc_I := Enclosing_Package_Body (Inst_Node);
9063 Ensure_Freeze_Node (Pack_Id);
9064 F_Node := Freeze_Node (Pack_Id);
9066 if Is_Generic_Instance (Par)
9067 and then Present (Freeze_Node (Par))
9068 and then In_Same_Declarative_Part
9069 (Parent (Freeze_Node (Par)), Inst_Node)
9071 -- The parent was a premature instantiation. Insert freeze node at
9072 -- the end the current declarative part.
9074 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9075 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9077 -- Handle the following case:
9079 -- package Parent_Inst is new ...
9082 -- procedure P ... -- this body freezes Parent_Inst
9084 -- procedure Inst is new ...
9086 -- In this particular scenario, the freeze node for Inst must be
9087 -- inserted in the same manner as that of Parent_Inst - before the
9088 -- next source body or at the end of the declarative list (body not
9089 -- available). If body P did not exist and Parent_Inst was frozen
9090 -- after Inst, either by a body following Inst or at the end of the
9091 -- declarative region, the freeze node for Inst must be inserted
9092 -- after that of Parent_Inst. This relation is established by
9093 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9095 elsif In_Same_List (Get_Unit_Instantiation_Node (Par), Inst_Node)
9096 and then Sloc (Freeze_Node (Par)) <= Sloc (Inst_Node)
9098 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9101 Insert_After (Freeze_Node (Par), F_Node);
9104 -- The body enclosing the instance should be frozen after the body that
9105 -- includes the generic, because the body of the instance may make
9106 -- references to entities therein. If the two are not in the same
9107 -- declarative part, or if the one enclosing the instance is frozen
9108 -- already, freeze the instance at the end of the current declarative
9111 elsif Is_Generic_Instance (Par)
9112 and then Present (Freeze_Node (Par))
9113 and then Present (Enc_I)
9115 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9116 -- The enclosing package may contain several instances. Rather
9117 -- than computing the earliest point at which to insert its freeze
9118 -- node, we place it at the end of the declarative part of the
9119 -- parent of the generic.
9121 Insert_Freeze_Node_For_Instance
9122 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9125 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9127 elsif Present (Enc_G)
9128 and then Present (Enc_I)
9129 and then Enc_G /= Enc_I
9130 and then Earlier (Inst_Node, Gen_Body)
9132 -- Freeze package that encloses instance, and place node after the
9133 -- package that encloses generic. If enclosing package is already
9134 -- frozen we have to assume it is at the proper place. This may be a
9135 -- potential ABE that requires dynamic checking. Do not add a freeze
9136 -- node if the package that encloses the generic is inside the body
9137 -- that encloses the instance, because the freeze node would be in
9138 -- the wrong scope. Additional contortions needed if the bodies are
9139 -- within a subunit.
9142 Enclosing_Body : Node_Id;
9145 if Nkind (Enc_I) = N_Package_Body_Stub then
9146 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9148 Enclosing_Body := Enc_I;
9151 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9152 Insert_Freeze_Node_For_Instance
9153 (Enc_G, Package_Freeze_Node (Enc_I));
9157 -- Freeze enclosing subunit before instance
9159 Enc_G_F := Package_Freeze_Node (Enc_G);
9161 if not Is_List_Member (Enc_G_F) then
9162 Insert_After (Enc_G, Enc_G_F);
9165 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9168 -- If none of the above, insert freeze node at the end of the current
9169 -- declarative part.
9171 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9173 end Freeze_Subprogram_Body;
9179 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9181 return Generic_Renamings.Table (E).Gen_Id;
9184 ---------------------
9185 -- Get_Instance_Of --
9186 ---------------------
9188 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9189 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9192 if Res /= Assoc_Null then
9193 return Generic_Renamings.Table (Res).Act_Id;
9196 -- On exit, entity is not instantiated: not a generic parameter, or
9197 -- else parameter of an inner generic unit.
9201 end Get_Instance_Of;
9203 ---------------------------------
9204 -- Get_Unit_Instantiation_Node --
9205 ---------------------------------
9207 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9208 Decl : Node_Id := Unit_Declaration_Node (A);
9212 -- If the Package_Instantiation attribute has been set on the package
9213 -- entity, then use it directly when it (or its Original_Node) refers
9214 -- to an N_Package_Instantiation node. In principle it should be
9215 -- possible to have this field set in all cases, which should be
9216 -- investigated, and would allow this function to be significantly
9219 Inst := Package_Instantiation (A);
9221 if Present (Inst) then
9222 if Nkind (Inst) = N_Package_Instantiation then
9225 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9226 return Original_Node (Inst);
9230 -- If the instantiation is a compilation unit that does not need body
9231 -- then the instantiation node has been rewritten as a package
9232 -- declaration for the instance, and we return the original node.
9234 -- If it is a compilation unit and the instance node has not been
9235 -- rewritten, then it is still the unit of the compilation. Finally, if
9236 -- a body is present, this is a parent of the main unit whose body has
9237 -- been compiled for inlining purposes, and the instantiation node has
9238 -- been rewritten with the instance body.
9240 -- Otherwise the instantiation node appears after the declaration. If
9241 -- the entity is a formal package, the declaration may have been
9242 -- rewritten as a generic declaration (in the case of a formal with box)
9243 -- or left as a formal package declaration if it has actuals, and is
9244 -- found with a forward search.
9246 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9247 if Nkind (Decl) = N_Package_Declaration
9248 and then Present (Corresponding_Body (Decl))
9250 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9253 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9254 return Original_Node (Decl);
9256 return Unit (Parent (Decl));
9259 elsif Nkind (Decl) = N_Package_Declaration
9260 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9262 return Original_Node (Decl);
9265 Inst := Next (Decl);
9266 while Nkind (Inst) not in N_Formal_Package_Declaration
9267 | N_Function_Instantiation
9268 | N_Package_Instantiation
9269 | N_Procedure_Instantiation
9276 end Get_Unit_Instantiation_Node;
9278 ------------------------
9279 -- Has_Been_Exchanged --
9280 ------------------------
9282 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9286 Next := First_Elmt (Exchanged_Views);
9287 while Present (Next) loop
9288 if Full_View (Node (Next)) = E then
9296 end Has_Been_Exchanged;
9302 function Has_Contracts (Decl : Node_Id) return Boolean is
9303 A_List : constant List_Id := Aspect_Specifications (Decl);
9310 A_Spec := First (A_List);
9311 while Present (A_Spec) loop
9312 A_Id := Get_Aspect_Id (A_Spec);
9313 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9328 function Hash (F : Entity_Id) return HTable_Range is
9330 return HTable_Range (F mod HTable_Size);
9333 ------------------------
9334 -- Hide_Current_Scope --
9335 ------------------------
9337 procedure Hide_Current_Scope is
9338 C : constant Entity_Id := Current_Scope;
9342 Set_Is_Hidden_Open_Scope (C);
9344 E := First_Entity (C);
9345 while Present (E) loop
9346 if Is_Immediately_Visible (E) then
9347 Set_Is_Immediately_Visible (E, False);
9348 Append_Elmt (E, Hidden_Entities);
9354 -- Make the scope name invisible as well. This is necessary, but might
9355 -- conflict with calls to Rtsfind later on, in case the scope is a
9356 -- predefined one. There is no clean solution to this problem, so for
9357 -- now we depend on the user not redefining Standard itself in one of
9358 -- the parent units.
9360 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9361 Set_Is_Immediately_Visible (C, False);
9362 Append_Elmt (C, Hidden_Entities);
9365 end Hide_Current_Scope;
9371 procedure Init_Env is
9372 Saved : Instance_Env;
9375 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9376 Saved.Exchanged_Views := Exchanged_Views;
9377 Saved.Hidden_Entities := Hidden_Entities;
9378 Saved.Current_Sem_Unit := Current_Sem_Unit;
9379 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9380 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9382 -- Save configuration switches. These may be reset if the unit is a
9383 -- predefined unit, and the current mode is not Ada 2005.
9385 Saved.Switches := Save_Config_Switches;
9387 Instance_Envs.Append (Saved);
9389 Exchanged_Views := New_Elmt_List;
9390 Hidden_Entities := New_Elmt_List;
9392 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9393 -- this is set properly in Set_Instance_Env.
9395 Current_Instantiated_Parent :=
9396 (Current_Scope, Current_Scope, Assoc_Null);
9399 ---------------------
9400 -- In_Main_Context --
9401 ---------------------
9403 function In_Main_Context (E : Entity_Id) return Boolean is
9409 if not Is_Compilation_Unit (E)
9410 or else Ekind (E) /= E_Package
9411 or else In_Private_Part (E)
9416 Context := Context_Items (Cunit (Main_Unit));
9418 Clause := First (Context);
9419 while Present (Clause) loop
9420 if Nkind (Clause) = N_With_Clause then
9421 Nam := Name (Clause);
9423 -- If the current scope is part of the context of the main unit,
9424 -- analysis of the corresponding with_clause is not complete, and
9425 -- the entity is not set. We use the Chars field directly, which
9426 -- might produce false positives in rare cases, but guarantees
9427 -- that we produce all the instance bodies we will need.
9429 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9430 or else (Nkind (Nam) = N_Selected_Component
9431 and then Chars (Selector_Name (Nam)) = Chars (E))
9441 end In_Main_Context;
9443 ---------------------
9444 -- Inherit_Context --
9445 ---------------------
9447 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9448 Current_Context : List_Id;
9449 Current_Unit : Node_Id;
9458 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9460 -- The inherited context is attached to the enclosing compilation
9461 -- unit. This is either the main unit, or the declaration for the
9462 -- main unit (in case the instantiation appears within the package
9463 -- declaration and the main unit is its body).
9465 Current_Unit := Parent (Inst);
9466 while Present (Current_Unit)
9467 and then Nkind (Current_Unit) /= N_Compilation_Unit
9469 Current_Unit := Parent (Current_Unit);
9472 Current_Context := Context_Items (Current_Unit);
9474 Item := First (Context_Items (Parent (Gen_Decl)));
9475 while Present (Item) loop
9476 if Nkind (Item) = N_With_Clause then
9477 Lib_Unit := Library_Unit (Item);
9479 -- Take care to prevent direct cyclic with's
9481 if Lib_Unit /= Current_Unit then
9483 -- Do not add a unit if it is already in the context
9485 Clause := First (Current_Context);
9487 while Present (Clause) loop
9488 if Nkind (Clause) = N_With_Clause
9489 and then Library_Unit (Clause) = Lib_Unit
9499 New_I := New_Copy (Item);
9500 Set_Implicit_With (New_I);
9502 Append (New_I, Current_Context);
9510 end Inherit_Context;
9516 procedure Initialize is
9518 Generic_Renamings.Init;
9521 Generic_Renamings_HTable.Reset;
9522 Circularity_Detected := False;
9523 Exchanged_Views := No_Elist;
9524 Hidden_Entities := No_Elist;
9527 -------------------------------------
9528 -- Insert_Freeze_Node_For_Instance --
9529 -------------------------------------
9531 procedure Insert_Freeze_Node_For_Instance
9540 function Enclosing_Body (N : Node_Id) return Node_Id;
9541 -- Find enclosing package or subprogram body, if any. Freeze node may
9542 -- be placed at end of current declarative list if previous instance
9543 -- and current one have different enclosing bodies.
9545 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9546 -- Find the local instance, if any, that declares the generic that is
9547 -- being instantiated. If present, the freeze node for this instance
9548 -- must follow the freeze node for the previous instance.
9550 --------------------
9551 -- Enclosing_Body --
9552 --------------------
9554 function Enclosing_Body (N : Node_Id) return Node_Id is
9560 and then Nkind (Parent (P)) /= N_Compilation_Unit
9562 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9563 if Nkind (Parent (P)) = N_Subunit then
9564 return Corresponding_Stub (Parent (P));
9570 P := True_Parent (P);
9576 -----------------------
9577 -- Previous_Instance --
9578 -----------------------
9580 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9585 while Present (S) and then S /= Standard_Standard loop
9586 if Is_Generic_Instance (S)
9587 and then In_Same_Source_Unit (S, N)
9596 end Previous_Instance;
9598 -- Start of processing for Insert_Freeze_Node_For_Instance
9601 if not Is_List_Member (F_Node) then
9603 Decls := List_Containing (N);
9604 Inst := Entity (F_Node);
9605 Par_N := Parent (Decls);
9607 -- When processing a subprogram instantiation, utilize the actual
9608 -- subprogram instantiation rather than its package wrapper as it
9609 -- carries all the context information.
9611 if Is_Wrapper_Package (Inst) then
9612 Inst := Related_Instance (Inst);
9615 -- If this is a package instance, check whether the generic is
9616 -- declared in a previous instance and the current instance is
9617 -- not within the previous one.
9619 if Present (Generic_Parent (Parent (Inst)))
9620 and then Is_In_Main_Unit (N)
9623 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9624 Par_I : constant Entity_Id :=
9626 (Generic_Parent (Parent (Inst)));
9631 and then Earlier (N, Freeze_Node (Par_I))
9633 Scop := Scope (Inst);
9635 -- If the current instance is within the one that contains
9636 -- the generic, the freeze node for the current one must
9637 -- appear in the current declarative part. Ditto, if the
9638 -- current instance is within another package instance or
9639 -- within a body that does not enclose the current instance.
9640 -- In these three cases the freeze node of the previous
9641 -- instance is not relevant.
9643 while Present (Scop) and then Scop /= Standard_Standard loop
9644 exit when Scop = Par_I
9646 (Is_Generic_Instance (Scop)
9647 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9648 Scop := Scope (Scop);
9651 -- Previous instance encloses current instance
9653 if Scop = Par_I then
9656 -- If the next node is a source body we must freeze in
9657 -- the current scope as well.
9659 elsif Present (Next (N))
9660 and then Nkind (Next (N)) in N_Subprogram_Body
9662 and then Comes_From_Source (Next (N))
9666 -- Current instance is within an unrelated instance
9668 elsif Is_Generic_Instance (Scop) then
9671 -- Current instance is within an unrelated body
9673 elsif Present (Enclosing_N)
9674 and then Enclosing_N /= Enclosing_Body (Par_I)
9679 Insert_After (Freeze_Node (Par_I), F_Node);
9686 -- When the instantiation occurs in a package declaration, append the
9687 -- freeze node to the private declarations (if any).
9689 if Nkind (Par_N) = N_Package_Specification
9690 and then Decls = Visible_Declarations (Par_N)
9691 and then Present (Private_Declarations (Par_N))
9692 and then not Is_Empty_List (Private_Declarations (Par_N))
9694 Decls := Private_Declarations (Par_N);
9695 Decl := First (Decls);
9698 -- Determine the proper freeze point of a package instantiation. We
9699 -- adhere to the general rule of a package or subprogram body causing
9700 -- freezing of anything before it in the same declarative region. In
9701 -- this case, the proper freeze point of a package instantiation is
9702 -- before the first source body which follows, or before a stub. This
9703 -- ensures that entities coming from the instance are already frozen
9704 -- and usable in source bodies.
9706 if Nkind (Par_N) /= N_Package_Declaration
9707 and then Ekind (Inst) = E_Package
9708 and then Is_Generic_Instance (Inst)
9710 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9712 while Present (Decl) loop
9713 if (Nkind (Decl) in N_Unit_Body
9715 Nkind (Decl) in N_Body_Stub)
9716 and then Comes_From_Source (Decl)
9718 Insert_Before (Decl, F_Node);
9726 -- In a package declaration, or if no previous body, insert at end
9729 Set_Sloc (F_Node, Sloc (Last (Decls)));
9730 Insert_After (Last (Decls), F_Node);
9732 end Insert_Freeze_Node_For_Instance;
9738 procedure Install_Body
9739 (Act_Body : Node_Id;
9744 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9745 -- Check if the generic definition and the instantiation come from
9746 -- a common scope, in which case the instance must be frozen after
9747 -- the generic body.
9749 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9750 -- If the instance is nested inside a generic unit, the Sloc of the
9751 -- instance indicates the place of the original definition, not the
9752 -- point of the current enclosing instance. Pending a better usage of
9753 -- Slocs to indicate instantiation places, we determine the place of
9754 -- origin of a node by finding the maximum sloc of any ancestor node.
9755 -- Why is this not equivalent to Top_Level_Location ???
9761 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9762 Act_Scop : Entity_Id := Scope (Act_Id);
9763 Gen_Scop : Entity_Id := Scope (Gen_Id);
9766 while Act_Scop /= Standard_Standard
9767 and then Gen_Scop /= Standard_Standard
9769 if Act_Scop = Gen_Scop then
9773 Act_Scop := Scope (Act_Scop);
9774 Gen_Scop := Scope (Gen_Scop);
9784 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9791 while Present (N1) and then N1 /= Act_Unit loop
9792 if Sloc (N1) > Res then
9802 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9803 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9804 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9805 Par : constant Entity_Id := Scope (Gen_Id);
9806 Gen_Unit : constant Node_Id :=
9807 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9809 Body_Unit : Node_Id;
9811 Must_Delay : Boolean;
9812 Orig_Body : Node_Id := Gen_Body;
9814 -- Start of processing for Install_Body
9817 -- Handle first the case of an instance with incomplete actual types.
9818 -- The instance body cannot be placed after the declaration because
9819 -- full views have not been seen yet. Any use of the non-limited views
9820 -- in the instance body requires the presence of a regular with_clause
9821 -- in the enclosing unit, and will fail if this with_clause is missing.
9822 -- We place the instance body at the beginning of the enclosing body,
9823 -- which is the unit being compiled. The freeze node for the instance
9824 -- is then placed after the instance body.
9826 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9827 and then Expander_Active
9828 and then Ekind (Scope (Act_Id)) = E_Package
9831 Scop : constant Entity_Id := Scope (Act_Id);
9832 Body_Id : constant Node_Id :=
9833 Corresponding_Body (Unit_Declaration_Node (Scop));
9836 Ensure_Freeze_Node (Act_Id);
9837 F_Node := Freeze_Node (Act_Id);
9838 if Present (Body_Id) then
9839 Set_Is_Frozen (Act_Id, False);
9840 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9841 if Is_List_Member (F_Node) then
9845 Insert_After (Act_Body, F_Node);
9851 -- If the body is a subunit, the freeze point is the corresponding stub
9852 -- in the current compilation, not the subunit itself.
9854 if Nkind (Parent (Gen_Body)) = N_Subunit then
9855 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9857 Orig_Body := Gen_Body;
9860 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9862 -- If the instantiation and the generic definition appear in the same
9863 -- package declaration, this is an early instantiation. If they appear
9864 -- in the same declarative part, it is an early instantiation only if
9865 -- the generic body appears textually later, and the generic body is
9866 -- also in the main unit.
9868 -- If instance is nested within a subprogram, and the generic body
9869 -- is not, the instance is delayed because the enclosing body is. If
9870 -- instance and body are within the same scope, or the same subprogram
9871 -- body, indicate explicitly that the instance is delayed.
9874 (Gen_Unit = Act_Unit
9875 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9876 | N_Package_Declaration
9877 or else (Gen_Unit = Body_Unit
9878 and then True_Sloc (N, Act_Unit) <
9880 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9881 and then In_Same_Scope (Gen_Id, Act_Id));
9883 -- If this is an early instantiation, the freeze node is placed after
9884 -- the generic body. Otherwise, if the generic appears in an instance,
9885 -- we cannot freeze the current instance until the outer one is frozen.
9886 -- This is only relevant if the current instance is nested within some
9887 -- inner scope not itself within the outer instance. If this scope is
9888 -- a package body in the same declarative part as the outer instance,
9889 -- then that body needs to be frozen after the outer instance. Finally,
9890 -- if no delay is needed, we place the freeze node at the end of the
9891 -- current declarative part.
9894 and then (No (Freeze_Node (Act_Id))
9895 or else not Is_List_Member (Freeze_Node (Act_Id)))
9897 Ensure_Freeze_Node (Act_Id);
9898 F_Node := Freeze_Node (Act_Id);
9901 Insert_After (Orig_Body, F_Node);
9903 elsif Is_Generic_Instance (Par)
9904 and then Present (Freeze_Node (Par))
9905 and then Scope (Act_Id) /= Par
9907 -- Freeze instance of inner generic after instance of enclosing
9910 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9912 -- Handle the following case:
9914 -- package Parent_Inst is new ...
9917 -- procedure P ... -- this body freezes Parent_Inst
9919 -- package Inst is new ...
9921 -- In this particular scenario, the freeze node for Inst must
9922 -- be inserted in the same manner as that of Parent_Inst,
9923 -- before the next source body or at the end of the declarative
9924 -- list (body not available). If body P did not exist and
9925 -- Parent_Inst was frozen after Inst, either by a body
9926 -- following Inst or at the end of the declarative region,
9927 -- the freeze node for Inst must be inserted after that of
9928 -- Parent_Inst. This relation is established by comparing
9929 -- the Slocs of Parent_Inst freeze node and Inst.
9930 -- We examine the parents of the enclosing lists to handle
9931 -- the case where the parent instance is in the visible part
9932 -- of a package declaration, and the inner instance is in
9933 -- the corresponding private part.
9935 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9936 = Parent (List_Containing (N))
9937 and then Sloc (Freeze_Node (Par)) <= Sloc (N)
9939 Insert_Freeze_Node_For_Instance (N, F_Node);
9941 Insert_After (Freeze_Node (Par), F_Node);
9944 -- Freeze package enclosing instance of inner generic after
9945 -- instance of enclosing generic.
9947 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9948 and then In_Same_Declarative_Part
9949 (Parent (Freeze_Node (Par)), Parent (N))
9952 Enclosing : Entity_Id;
9955 Enclosing := Corresponding_Spec (Parent (N));
9957 if No (Enclosing) then
9958 Enclosing := Defining_Entity (Parent (N));
9961 Insert_Freeze_Node_For_Instance (N, F_Node);
9962 Ensure_Freeze_Node (Enclosing);
9964 if not Is_List_Member (Freeze_Node (Enclosing)) then
9966 -- The enclosing context is a subunit, insert the freeze
9967 -- node after the stub.
9969 if Nkind (Parent (Parent (N))) = N_Subunit then
9970 Insert_Freeze_Node_For_Instance
9971 (Corresponding_Stub (Parent (Parent (N))),
9972 Freeze_Node (Enclosing));
9974 -- The enclosing context is a package with a stub body
9975 -- which has already been replaced by the real body.
9976 -- Insert the freeze node after the actual body.
9978 elsif Ekind (Enclosing) = E_Package
9979 and then Present (Body_Entity (Enclosing))
9980 and then Was_Originally_Stub
9981 (Parent (Body_Entity (Enclosing)))
9983 Insert_Freeze_Node_For_Instance
9984 (Parent (Body_Entity (Enclosing)),
9985 Freeze_Node (Enclosing));
9987 -- The parent instance has been frozen before the body of
9988 -- the enclosing package, insert the freeze node after
9991 elsif In_Same_List (Freeze_Node (Par), Parent (N))
9992 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9994 Insert_Freeze_Node_For_Instance
9995 (Parent (N), Freeze_Node (Enclosing));
9999 (Freeze_Node (Par), Freeze_Node (Enclosing));
10005 Insert_Freeze_Node_For_Instance (N, F_Node);
10009 Insert_Freeze_Node_For_Instance (N, F_Node);
10013 Set_Is_Frozen (Act_Id);
10014 Insert_Before (N, Act_Body);
10015 Mark_Rewrite_Insertion (Act_Body);
10018 -----------------------------
10019 -- Install_Formal_Packages --
10020 -----------------------------
10022 procedure Install_Formal_Packages (Par : Entity_Id) is
10025 Gen_E : Entity_Id := Empty;
10028 E := First_Entity (Par);
10030 -- If we are installing an instance parent, locate the formal packages
10031 -- of its generic parent.
10033 if Is_Generic_Instance (Par) then
10034 Gen := Generic_Parent (Package_Specification (Par));
10035 Gen_E := First_Entity (Gen);
10038 while Present (E) loop
10039 if Ekind (E) = E_Package
10040 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10042 -- If this is the renaming for the parent instance, done
10044 if Renamed_Object (E) = Par then
10047 -- The visibility of a formal of an enclosing generic is already
10050 elsif Denotes_Formal_Package (E) then
10053 elsif Present (Associated_Formal_Package (E)) then
10054 Check_Generic_Actuals (Renamed_Object (E), True);
10055 Set_Is_Hidden (E, False);
10057 -- Find formal package in generic unit that corresponds to
10058 -- (instance of) formal package in instance.
10060 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10061 Next_Entity (Gen_E);
10064 if Present (Gen_E) then
10065 Map_Formal_Package_Entities (Gen_E, E);
10072 if Present (Gen_E) then
10073 Next_Entity (Gen_E);
10076 end Install_Formal_Packages;
10078 --------------------
10079 -- Install_Parent --
10080 --------------------
10082 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10083 Ancestors : constant Elist_Id := New_Elmt_List;
10084 S : constant Entity_Id := Current_Scope;
10085 Inst_Par : Entity_Id;
10086 First_Par : Entity_Id;
10087 Inst_Node : Node_Id;
10088 Gen_Par : Entity_Id;
10089 First_Gen : Entity_Id;
10092 procedure Install_Noninstance_Specs (Par : Entity_Id);
10093 -- Install the scopes of noninstance parent units ending with Par
10095 procedure Install_Spec (Par : Entity_Id);
10096 -- The child unit is within the declarative part of the parent, so the
10097 -- declarations within the parent are immediately visible.
10099 -------------------------------
10100 -- Install_Noninstance_Specs --
10101 -------------------------------
10103 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10106 and then Par /= Standard_Standard
10107 and then not In_Open_Scopes (Par)
10109 Install_Noninstance_Specs (Scope (Par));
10110 Install_Spec (Par);
10112 end Install_Noninstance_Specs;
10118 procedure Install_Spec (Par : Entity_Id) is
10119 Spec : constant Node_Id := Package_Specification (Par);
10122 -- If this parent of the child instance is a top-level unit,
10123 -- then record the unit and its visibility for later resetting in
10124 -- Remove_Parent. We exclude units that are generic instances, as we
10125 -- only want to record this information for the ultimate top-level
10126 -- noninstance parent (is that always correct???).
10128 if Scope (Par) = Standard_Standard
10129 and then not Is_Generic_Instance (Par)
10131 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10132 Instance_Parent_Unit := Par;
10135 -- Open the parent scope and make it and its declarations visible.
10136 -- If this point is not within a body, then only the visible
10137 -- declarations should be made visible, and installation of the
10138 -- private declarations is deferred until the appropriate point
10139 -- within analysis of the spec being instantiated (see the handling
10140 -- of parent visibility in Analyze_Package_Specification). This is
10141 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10142 -- private view problems that occur when compiling instantiations of
10143 -- a generic child of that package (Generic_Dispatching_Constructor).
10144 -- If the instance freezes a tagged type, inlinings of operations
10145 -- from Ada.Tags may need the full view of type Tag. If inlining took
10146 -- proper account of establishing visibility of inlined subprograms'
10147 -- parents then it should be possible to remove this
10148 -- special check. ???
10151 Set_Is_Immediately_Visible (Par);
10152 Install_Visible_Declarations (Par);
10153 Set_Use (Visible_Declarations (Spec));
10155 if In_Body or else Is_RTU (Par, Ada_Tags) then
10156 Install_Private_Declarations (Par);
10157 Set_Use (Private_Declarations (Spec));
10161 -- Start of processing for Install_Parent
10164 -- We need to install the parent instance to compile the instantiation
10165 -- of the child, but the child instance must appear in the current
10166 -- scope. Given that we cannot place the parent above the current scope
10167 -- in the scope stack, we duplicate the current scope and unstack both
10168 -- after the instantiation is complete.
10170 -- If the parent is itself the instantiation of a child unit, we must
10171 -- also stack the instantiation of its parent, and so on. Each such
10172 -- ancestor is the prefix of the name in a prior instantiation.
10174 -- If this is a nested instance, the parent unit itself resolves to
10175 -- a renaming of the parent instance, whose declaration we need.
10177 -- Finally, the parent may be a generic (not an instance) when the
10178 -- child unit appears as a formal package.
10182 if Present (Renamed_Entity (Inst_Par)) then
10183 Inst_Par := Renamed_Entity (Inst_Par);
10186 First_Par := Inst_Par;
10188 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10190 First_Gen := Gen_Par;
10192 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10194 -- Load grandparent instance as well
10196 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10198 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10199 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10201 if Present (Renamed_Entity (Inst_Par)) then
10202 Inst_Par := Renamed_Entity (Inst_Par);
10205 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10207 if Present (Gen_Par) then
10208 Prepend_Elmt (Inst_Par, Ancestors);
10211 -- Parent is not the name of an instantiation
10213 Install_Noninstance_Specs (Inst_Par);
10224 if Present (First_Gen) then
10225 Append_Elmt (First_Par, Ancestors);
10227 Install_Noninstance_Specs (First_Par);
10230 if not Is_Empty_Elmt_List (Ancestors) then
10231 Elmt := First_Elmt (Ancestors);
10232 while Present (Elmt) loop
10233 Install_Spec (Node (Elmt));
10234 Install_Formal_Packages (Node (Elmt));
10239 if not In_Body then
10242 end Install_Parent;
10244 -------------------------------
10245 -- Install_Hidden_Primitives --
10246 -------------------------------
10248 procedure Install_Hidden_Primitives
10249 (Prims_List : in out Elist_Id;
10254 List : Elist_Id := No_Elist;
10255 Prim_G_Elmt : Elmt_Id;
10256 Prim_A_Elmt : Elmt_Id;
10261 -- No action needed in case of serious errors because we cannot trust
10262 -- in the order of primitives
10264 if Serious_Errors_Detected > 0 then
10267 -- No action possible if we don't have available the list of primitive
10271 or else not Is_Record_Type (Gen_T)
10272 or else not Is_Tagged_Type (Gen_T)
10273 or else not Is_Record_Type (Act_T)
10274 or else not Is_Tagged_Type (Act_T)
10278 -- There is no need to handle interface types since their primitives
10279 -- cannot be hidden
10281 elsif Is_Interface (Gen_T) then
10285 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10287 if not Is_Class_Wide_Type (Act_T) then
10288 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10290 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10294 -- Skip predefined primitives in the generic formal
10296 while Present (Prim_G_Elmt)
10297 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10299 Next_Elmt (Prim_G_Elmt);
10302 -- Skip predefined primitives in the generic actual
10304 while Present (Prim_A_Elmt)
10305 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10307 Next_Elmt (Prim_A_Elmt);
10310 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10312 Prim_G := Node (Prim_G_Elmt);
10313 Prim_A := Node (Prim_A_Elmt);
10315 -- There is no need to handle interface primitives because their
10316 -- primitives are not hidden
10318 exit when Present (Interface_Alias (Prim_G));
10320 -- Here we install one hidden primitive
10322 if Chars (Prim_G) /= Chars (Prim_A)
10323 and then Has_Suffix (Prim_A, 'P')
10324 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10326 Set_Chars (Prim_A, Chars (Prim_G));
10327 Append_New_Elmt (Prim_A, To => List);
10330 Next_Elmt (Prim_A_Elmt);
10331 Next_Elmt (Prim_G_Elmt);
10334 -- Append the elements to the list of temporarily visible primitives
10335 -- avoiding duplicates.
10337 if Present (List) then
10338 if No (Prims_List) then
10339 Prims_List := New_Elmt_List;
10342 Elmt := First_Elmt (List);
10343 while Present (Elmt) loop
10344 Append_Unique_Elmt (Node (Elmt), Prims_List);
10348 end Install_Hidden_Primitives;
10350 -------------------------------
10351 -- Restore_Hidden_Primitives --
10352 -------------------------------
10354 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10355 Prim_Elmt : Elmt_Id;
10359 if Prims_List /= No_Elist then
10360 Prim_Elmt := First_Elmt (Prims_List);
10361 while Present (Prim_Elmt) loop
10362 Prim := Node (Prim_Elmt);
10363 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10364 Next_Elmt (Prim_Elmt);
10367 Prims_List := No_Elist;
10369 end Restore_Hidden_Primitives;
10371 --------------------------------
10372 -- Instantiate_Formal_Package --
10373 --------------------------------
10375 function Instantiate_Formal_Package
10378 Analyzed_Formal : Node_Id) return List_Id
10380 Loc : constant Source_Ptr := Sloc (Actual);
10381 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10382 Actual_Pack : Entity_Id;
10383 Formal_Pack : Entity_Id;
10384 Gen_Parent : Entity_Id;
10387 Parent_Spec : Node_Id;
10389 procedure Find_Matching_Actual
10391 Act : in out Entity_Id);
10392 -- We need to associate each formal entity in the formal package with
10393 -- the corresponding entity in the actual package. The actual package
10394 -- has been analyzed and possibly expanded, and as a result there is
10395 -- no one-to-one correspondence between the two lists (for example,
10396 -- the actual may include subtypes, itypes, and inherited primitive
10397 -- operations, interspersed among the renaming declarations for the
10398 -- actuals). We retrieve the corresponding actual by name because each
10399 -- actual has the same name as the formal, and they do appear in the
10402 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10403 -- Retrieve entity of defining entity of generic formal parameter.
10404 -- Only the declarations of formals need to be considered when
10405 -- linking them to actuals, but the declarative list may include
10406 -- internal entities generated during analysis, and those are ignored.
10408 procedure Match_Formal_Entity
10409 (Formal_Node : Node_Id;
10410 Formal_Ent : Entity_Id;
10411 Actual_Ent : Entity_Id);
10412 -- Associates the formal entity with the actual. In the case where
10413 -- Formal_Ent is a formal package, this procedure iterates through all
10414 -- of its formals and enters associations between the actuals occurring
10415 -- in the formal package's corresponding actual package (given by
10416 -- Actual_Ent) and the formal package's formal parameters. This
10417 -- procedure recurses if any of the parameters is itself a package.
10419 function Is_Instance_Of
10420 (Act_Spec : Entity_Id;
10421 Gen_Anc : Entity_Id) return Boolean;
10422 -- The actual can be an instantiation of a generic within another
10423 -- instance, in which case there is no direct link from it to the
10424 -- original generic ancestor. In that case, we recognize that the
10425 -- ultimate ancestor is the same by examining names and scopes.
10427 procedure Process_Nested_Formal (Formal : Entity_Id);
10428 -- If the current formal is declared with a box, its own formals are
10429 -- visible in the instance, as they were in the generic, and their
10430 -- Hidden flag must be reset. If some of these formals are themselves
10431 -- packages declared with a box, the processing must be recursive.
10433 --------------------------
10434 -- Find_Matching_Actual --
10435 --------------------------
10437 procedure Find_Matching_Actual
10439 Act : in out Entity_Id)
10441 Formal_Ent : Entity_Id;
10444 case Nkind (Original_Node (F)) is
10445 when N_Formal_Object_Declaration
10446 | N_Formal_Type_Declaration
10448 Formal_Ent := Defining_Identifier (F);
10450 while Present (Act)
10451 and then Chars (Act) /= Chars (Formal_Ent)
10456 when N_Formal_Package_Declaration
10457 | N_Formal_Subprogram_Declaration
10458 | N_Generic_Package_Declaration
10459 | N_Package_Declaration
10461 Formal_Ent := Defining_Entity (F);
10463 while Present (Act)
10464 and then Chars (Act) /= Chars (Formal_Ent)
10470 raise Program_Error;
10472 end Find_Matching_Actual;
10474 -------------------------
10475 -- Match_Formal_Entity --
10476 -------------------------
10478 procedure Match_Formal_Entity
10479 (Formal_Node : Node_Id;
10480 Formal_Ent : Entity_Id;
10481 Actual_Ent : Entity_Id)
10483 Act_Pkg : Entity_Id;
10486 Set_Instance_Of (Formal_Ent, Actual_Ent);
10488 if Ekind (Actual_Ent) = E_Package then
10490 -- Record associations for each parameter
10492 Act_Pkg := Actual_Ent;
10495 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10499 Gen_Decl : Node_Id;
10501 Actual : Entity_Id;
10504 -- Retrieve the actual given in the formal package declaration
10506 Actual := Entity (Name (Original_Node (Formal_Node)));
10508 -- The actual in the formal package declaration may be a
10509 -- renamed generic package, in which case we want to retrieve
10510 -- the original generic in order to traverse its formal part.
10512 if Present (Renamed_Entity (Actual)) then
10513 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10515 Gen_Decl := Unit_Declaration_Node (Actual);
10518 Formals := Generic_Formal_Declarations (Gen_Decl);
10520 if Present (Formals) then
10521 F_Node := First_Non_Pragma (Formals);
10526 while Present (A_Ent)
10527 and then Present (F_Node)
10528 and then A_Ent /= First_Private_Entity (Act_Pkg)
10530 F_Ent := Get_Formal_Entity (F_Node);
10532 if Present (F_Ent) then
10534 -- This is a formal of the original package. Record
10535 -- association and recurse.
10537 Find_Matching_Actual (F_Node, A_Ent);
10538 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10539 Next_Entity (A_Ent);
10542 Next_Non_Pragma (F_Node);
10546 end Match_Formal_Entity;
10548 -----------------------
10549 -- Get_Formal_Entity --
10550 -----------------------
10552 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10553 Kind : constant Node_Kind := Nkind (Original_Node (N));
10556 when N_Formal_Object_Declaration =>
10557 return Defining_Identifier (N);
10559 when N_Formal_Type_Declaration =>
10560 return Defining_Identifier (N);
10562 when N_Formal_Subprogram_Declaration =>
10563 return Defining_Unit_Name (Specification (N));
10565 when N_Formal_Package_Declaration =>
10566 return Defining_Identifier (Original_Node (N));
10568 when N_Generic_Package_Declaration =>
10569 return Defining_Identifier (Original_Node (N));
10571 -- All other declarations are introduced by semantic analysis and
10572 -- have no match in the actual.
10577 end Get_Formal_Entity;
10579 --------------------
10580 -- Is_Instance_Of --
10581 --------------------
10583 function Is_Instance_Of
10584 (Act_Spec : Entity_Id;
10585 Gen_Anc : Entity_Id) return Boolean
10587 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10590 if No (Gen_Par) then
10593 -- Simplest case: the generic parent of the actual is the formal
10595 elsif Gen_Par = Gen_Anc then
10598 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10601 -- The actual may be obtained through several instantiations. Its
10602 -- scope must itself be an instance of a generic declared in the
10603 -- same scope as the formal. Any other case is detected above.
10605 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10609 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10611 end Is_Instance_Of;
10613 ---------------------------
10614 -- Process_Nested_Formal --
10615 ---------------------------
10617 procedure Process_Nested_Formal (Formal : Entity_Id) is
10621 if Present (Associated_Formal_Package (Formal))
10622 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10624 Ent := First_Entity (Formal);
10625 while Present (Ent) loop
10626 Set_Is_Hidden (Ent, False);
10627 Set_Is_Visible_Formal (Ent);
10628 Set_Is_Potentially_Use_Visible
10629 (Ent, Is_Potentially_Use_Visible (Formal));
10631 if Ekind (Ent) = E_Package then
10632 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10633 Process_Nested_Formal (Ent);
10639 end Process_Nested_Formal;
10641 -- Start of processing for Instantiate_Formal_Package
10646 -- The actual must be a package instance, or else a current instance
10647 -- such as a parent generic within the body of a generic child.
10649 if not Is_Entity_Name (Actual)
10650 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10653 ("expect package instance to instantiate formal", Actual);
10654 Abandon_Instantiation (Actual);
10655 raise Program_Error;
10658 Actual_Pack := Entity (Actual);
10659 Set_Is_Instantiated (Actual_Pack);
10661 -- The actual may be a renamed package, or an outer generic formal
10662 -- package whose instantiation is converted into a renaming.
10664 if Present (Renamed_Object (Actual_Pack)) then
10665 Actual_Pack := Renamed_Object (Actual_Pack);
10668 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10669 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10670 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10673 Generic_Parent (Specification (Analyzed_Formal));
10675 Defining_Unit_Name (Specification (Analyzed_Formal));
10678 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10679 Parent_Spec := Package_Specification (Actual_Pack);
10681 Parent_Spec := Parent (Actual_Pack);
10684 if Gen_Parent = Any_Id then
10686 ("previous error in declaration of formal package", Actual);
10687 Abandon_Instantiation (Actual);
10689 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10692 -- If this is the current instance of an enclosing generic, that unit
10693 -- is the generic package we need.
10695 elsif In_Open_Scopes (Actual_Pack)
10696 and then Ekind (Actual_Pack) = E_Generic_Package
10702 ("actual parameter must be instance of&", Actual, Gen_Parent);
10703 Abandon_Instantiation (Actual);
10706 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10707 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10710 Make_Package_Renaming_Declaration (Loc,
10711 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10712 Name => New_Occurrence_Of (Actual_Pack, Loc));
10714 Set_Associated_Formal_Package
10715 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10716 Decls := New_List (Nod);
10718 -- If the formal F has a box, then the generic declarations are
10719 -- visible in the generic G. In an instance of G, the corresponding
10720 -- entities in the actual for F (which are the actuals for the
10721 -- instantiation of the generic that F denotes) must also be made
10722 -- visible for analysis of the current instance. On exit from the
10723 -- current instance, those entities are made private again. If the
10724 -- actual is currently in use, these entities are also use-visible.
10726 -- The loop through the actual entities also steps through the formal
10727 -- entities and enters associations from formals to actuals into the
10728 -- renaming map. This is necessary to properly handle checking of
10729 -- actual parameter associations for later formals that depend on
10730 -- actuals declared in the formal package.
10732 -- In Ada 2005, partial parameterization requires that we make
10733 -- visible the actuals corresponding to formals that were defaulted
10734 -- in the formal package. There formals are identified because they
10735 -- remain formal generics within the formal package, rather than
10736 -- being renamings of the actuals supplied.
10739 Gen_Decl : constant Node_Id :=
10740 Unit_Declaration_Node (Gen_Parent);
10741 Formals : constant List_Id :=
10742 Generic_Formal_Declarations (Gen_Decl);
10744 Actual_Ent : Entity_Id;
10745 Actual_Of_Formal : Node_Id;
10746 Formal_Node : Node_Id;
10747 Formal_Ent : Entity_Id;
10750 if Present (Formals) then
10751 Formal_Node := First_Non_Pragma (Formals);
10753 Formal_Node := Empty;
10756 Actual_Ent := First_Entity (Actual_Pack);
10757 Actual_Of_Formal :=
10758 First (Visible_Declarations (Specification (Analyzed_Formal)));
10759 while Present (Actual_Ent)
10760 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10762 if Present (Formal_Node) then
10763 Formal_Ent := Get_Formal_Entity (Formal_Node);
10765 if Present (Formal_Ent) then
10766 Find_Matching_Actual (Formal_Node, Actual_Ent);
10767 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10769 -- We iterate at the same time over the actuals of the
10770 -- local package created for the formal, to determine
10771 -- which one of the formals of the original generic were
10772 -- defaulted in the formal. The corresponding actual
10773 -- entities are visible in the enclosing instance.
10775 if Box_Present (Formal)
10777 (Present (Actual_Of_Formal)
10780 (Get_Formal_Entity (Actual_Of_Formal)))
10782 Set_Is_Hidden (Actual_Ent, False);
10783 Set_Is_Visible_Formal (Actual_Ent);
10784 Set_Is_Potentially_Use_Visible
10785 (Actual_Ent, In_Use (Actual_Pack));
10787 if Ekind (Actual_Ent) = E_Package then
10788 Process_Nested_Formal (Actual_Ent);
10792 if not Is_Hidden (Actual_Ent) then
10793 Append_Elmt (Actual_Ent, Hidden_Formals);
10796 Set_Is_Hidden (Actual_Ent);
10797 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10801 Next_Non_Pragma (Formal_Node);
10802 Next (Actual_Of_Formal);
10805 -- No further formals to match, but the generic part may
10806 -- contain inherited operation that are not hidden in the
10807 -- enclosing instance.
10809 Next_Entity (Actual_Ent);
10813 -- Inherited subprograms generated by formal derived types are
10814 -- also visible if the types are.
10816 Actual_Ent := First_Entity (Actual_Pack);
10817 while Present (Actual_Ent)
10818 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10820 if Is_Overloadable (Actual_Ent)
10822 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10824 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10826 Set_Is_Hidden (Actual_Ent, False);
10827 Set_Is_Potentially_Use_Visible
10828 (Actual_Ent, In_Use (Actual_Pack));
10831 Next_Entity (Actual_Ent);
10834 -- No conformance to check if the generic has no formal parameters
10835 -- and the formal package has no generic associations.
10837 if Is_Empty_List (Formals)
10839 (Box_Present (Formal)
10840 or else No (Generic_Associations (Formal)))
10846 -- If the formal is not declared with a box, reanalyze it as an
10847 -- abbreviated instantiation, to verify the matching rules of 12.7.
10848 -- The actual checks are performed after the generic associations
10849 -- have been analyzed, to guarantee the same visibility for this
10850 -- instantiation and for the actuals.
10852 -- In Ada 2005, the generic associations for the formal can include
10853 -- defaulted parameters. These are ignored during check. This
10854 -- internal instantiation is removed from the tree after conformance
10855 -- checking, because it contains formal declarations for those
10856 -- defaulted parameters, and those should not reach the back-end.
10858 if not Box_Present (Formal) then
10860 I_Pack : constant Entity_Id :=
10861 Make_Temporary (Sloc (Actual), 'P');
10864 Set_Is_Internal (I_Pack);
10865 Set_Ekind (I_Pack, E_Package);
10866 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10869 Make_Package_Instantiation (Sloc (Actual),
10870 Defining_Unit_Name => I_Pack,
10873 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10874 Generic_Associations => Generic_Associations (Formal)));
10880 end Instantiate_Formal_Package;
10882 -----------------------------------
10883 -- Instantiate_Formal_Subprogram --
10884 -----------------------------------
10886 function Instantiate_Formal_Subprogram
10889 Analyzed_Formal : Node_Id) return Node_Id
10891 Analyzed_S : constant Entity_Id :=
10892 Defining_Unit_Name (Specification (Analyzed_Formal));
10893 Formal_Sub : constant Entity_Id :=
10894 Defining_Unit_Name (Specification (Formal));
10896 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10897 -- If the generic is a child unit, the parent has been installed on the
10898 -- scope stack, but a default subprogram cannot resolve to something
10899 -- on the parent because that parent is not really part of the visible
10900 -- context (it is there to resolve explicit local entities). If the
10901 -- default has resolved in this way, we remove the entity from immediate
10902 -- visibility and analyze the node again to emit an error message or
10903 -- find another visible candidate.
10905 procedure Valid_Actual_Subprogram (Act : Node_Id);
10906 -- Perform legality check and raise exception on failure
10908 -----------------------
10909 -- From_Parent_Scope --
10910 -----------------------
10912 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10913 Gen_Scope : Node_Id;
10916 Gen_Scope := Scope (Analyzed_S);
10917 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10918 if Scope (Subp) = Scope (Gen_Scope) then
10922 Gen_Scope := Scope (Gen_Scope);
10926 end From_Parent_Scope;
10928 -----------------------------
10929 -- Valid_Actual_Subprogram --
10930 -----------------------------
10932 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10936 if Is_Entity_Name (Act) then
10937 Act_E := Entity (Act);
10939 elsif Nkind (Act) = N_Selected_Component
10940 and then Is_Entity_Name (Selector_Name (Act))
10942 Act_E := Entity (Selector_Name (Act));
10948 if (Present (Act_E) and then Is_Overloadable (Act_E))
10949 or else Nkind (Act) in N_Attribute_Reference
10950 | N_Indexed_Component
10951 | N_Character_Literal
10952 | N_Explicit_Dereference
10958 ("expect subprogram or entry name in instantiation of &",
10959 Instantiation_Node, Formal_Sub);
10960 Abandon_Instantiation (Instantiation_Node);
10961 end Valid_Actual_Subprogram;
10965 Decl_Node : Node_Id;
10968 New_Spec : Node_Id;
10969 New_Subp : Entity_Id;
10971 -- Start of processing for Instantiate_Formal_Subprogram
10974 New_Spec := New_Copy_Tree (Specification (Formal));
10976 -- The tree copy has created the proper instantiation sloc for the
10977 -- new specification. Use this location for all other constructed
10980 Loc := Sloc (Defining_Unit_Name (New_Spec));
10982 -- Create new entity for the actual (New_Copy_Tree does not), and
10983 -- indicate that it is an actual.
10985 -- If the actual is not an entity (i.e. an attribute reference)
10986 -- and the formal includes aspect specifications for contracts,
10987 -- we create an internal name for the renaming declaration. The
10988 -- constructed wrapper contains a call to the entity in the renaming.
10989 -- This is an expansion activity, as is the wrapper creation.
10991 if Ada_Version >= Ada_2020
10992 and then Has_Contracts (Analyzed_Formal)
10993 and then not Is_Entity_Name (Actual)
10994 and then Expander_Active
10996 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10997 Set_Defining_Unit_Name (New_Spec, New_Subp);
10999 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11002 Set_Ekind (New_Subp, Ekind (Analyzed_S));
11003 Set_Is_Generic_Actual_Subprogram (New_Subp);
11004 Set_Defining_Unit_Name (New_Spec, New_Subp);
11006 -- Create new entities for the each of the formals in the specification
11007 -- of the renaming declaration built for the actual.
11009 if Present (Parameter_Specifications (New_Spec)) then
11015 F := First (Parameter_Specifications (New_Spec));
11016 while Present (F) loop
11017 F_Id := Defining_Identifier (F);
11019 Set_Defining_Identifier (F,
11020 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11026 -- Find entity of actual. If the actual is an attribute reference, it
11027 -- cannot be resolved here (its formal is missing) but is handled
11028 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11029 -- fully resolved subsequently, when the renaming declaration for the
11030 -- formal is analyzed. If it is an explicit dereference, resolve the
11031 -- prefix but not the actual itself, to prevent interpretation as call.
11033 if Present (Actual) then
11034 Loc := Sloc (Actual);
11035 Set_Sloc (New_Spec, Loc);
11037 if Nkind (Actual) = N_Operator_Symbol then
11038 Find_Direct_Name (Actual);
11040 elsif Nkind (Actual) = N_Explicit_Dereference then
11041 Analyze (Prefix (Actual));
11043 elsif Nkind (Actual) /= N_Attribute_Reference then
11047 Valid_Actual_Subprogram (Actual);
11050 elsif Present (Default_Name (Formal)) then
11051 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11052 | N_Selected_Component
11053 | N_Indexed_Component
11054 | N_Character_Literal
11055 and then Present (Entity (Default_Name (Formal)))
11057 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11059 Nam := New_Copy (Default_Name (Formal));
11060 Set_Sloc (Nam, Loc);
11063 elsif Box_Present (Formal) then
11065 -- Actual is resolved at the point of instantiation. Create an
11066 -- identifier or operator with the same name as the formal.
11068 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11070 Make_Operator_Symbol (Loc,
11071 Chars => Chars (Formal_Sub),
11072 Strval => No_String);
11074 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11077 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11078 and then Null_Present (Specification (Formal))
11080 -- Generate null body for procedure, for use in the instance
11083 Make_Subprogram_Body (Loc,
11084 Specification => New_Spec,
11085 Declarations => New_List,
11086 Handled_Statement_Sequence =>
11087 Make_Handled_Sequence_Of_Statements (Loc,
11088 Statements => New_List (Make_Null_Statement (Loc))));
11090 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11092 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11094 -- Eliminate the calls to it when optimization is enabled
11096 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11100 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11102 ("missing actual&", Instantiation_Node, Formal_Sub);
11104 ("\in instantiation of & declared#",
11105 Instantiation_Node, Scope (Analyzed_S));
11106 Abandon_Instantiation (Instantiation_Node);
11110 Make_Subprogram_Renaming_Declaration (Loc,
11111 Specification => New_Spec,
11114 -- If we do not have an actual and the formal specified <> then set to
11115 -- get proper default.
11117 if No (Actual) and then Box_Present (Formal) then
11118 Set_From_Default (Decl_Node);
11121 -- Gather possible interpretations for the actual before analyzing the
11122 -- instance. If overloaded, it will be resolved when analyzing the
11123 -- renaming declaration.
11125 if Box_Present (Formal) and then No (Actual) then
11128 if Is_Child_Unit (Scope (Analyzed_S))
11129 and then Present (Entity (Nam))
11131 if not Is_Overloaded (Nam) then
11132 if From_Parent_Scope (Entity (Nam)) then
11133 Set_Is_Immediately_Visible (Entity (Nam), False);
11134 Set_Entity (Nam, Empty);
11135 Set_Etype (Nam, Empty);
11138 Set_Is_Immediately_Visible (Entity (Nam));
11147 Get_First_Interp (Nam, I, It);
11148 while Present (It.Nam) loop
11149 if From_Parent_Scope (It.Nam) then
11153 Get_Next_Interp (I, It);
11160 -- The generic instantiation freezes the actual. This can only be done
11161 -- once the actual is resolved, in the analysis of the renaming
11162 -- declaration. To make the formal subprogram entity available, we set
11163 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11164 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11165 -- of formal abstract subprograms.
11167 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11169 -- We cannot analyze the renaming declaration, and thus find the actual,
11170 -- until all the actuals are assembled in the instance. For subsequent
11171 -- checks of other actuals, indicate the node that will hold the
11172 -- instance of this formal.
11174 Set_Instance_Of (Analyzed_S, Nam);
11176 if Nkind (Actual) = N_Selected_Component
11177 and then Is_Task_Type (Etype (Prefix (Actual)))
11178 and then not Is_Frozen (Etype (Prefix (Actual)))
11180 -- The renaming declaration will create a body, which must appear
11181 -- outside of the instantiation, We move the renaming declaration
11182 -- out of the instance, and create an additional renaming inside,
11183 -- to prevent freezing anomalies.
11186 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11189 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11190 Insert_Before (Instantiation_Node, Decl_Node);
11191 Analyze (Decl_Node);
11193 -- Now create renaming within the instance
11196 Make_Subprogram_Renaming_Declaration (Loc,
11197 Specification => New_Copy_Tree (New_Spec),
11198 Name => New_Occurrence_Of (Anon_Id, Loc));
11200 Set_Defining_Unit_Name (Specification (Decl_Node),
11201 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11206 end Instantiate_Formal_Subprogram;
11208 ------------------------
11209 -- Instantiate_Object --
11210 ------------------------
11212 function Instantiate_Object
11215 Analyzed_Formal : Node_Id) return List_Id
11217 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11218 A_Gen_Obj : constant Entity_Id :=
11219 Defining_Identifier (Analyzed_Formal);
11220 Acc_Def : Node_Id := Empty;
11221 Act_Assoc : constant Node_Id := Parent (Actual);
11222 Actual_Decl : Node_Id := Empty;
11223 Decl_Node : Node_Id;
11226 List : constant List_Id := New_List;
11227 Loc : constant Source_Ptr := Sloc (Actual);
11228 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11229 Subt_Decl : Node_Id := Empty;
11230 Subt_Mark : Node_Id := Empty;
11232 -- Start of processing for Instantiate_Object
11235 -- Formal may be an anonymous access
11237 if Present (Subtype_Mark (Formal)) then
11238 Subt_Mark := Subtype_Mark (Formal);
11240 Check_Access_Definition (Formal);
11241 Acc_Def := Access_Definition (Formal);
11244 -- Sloc for error message on missing actual
11246 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11248 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11249 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11252 Set_Parent (List, Parent (Actual));
11256 if Out_Present (Formal) then
11258 -- An IN OUT generic actual must be a name. The instantiation is a
11259 -- renaming declaration. The actual is the name being renamed. We
11260 -- use the actual directly, rather than a copy, because it is not
11261 -- used further in the list of actuals, and because a copy or a use
11262 -- of relocate_node is incorrect if the instance is nested within a
11263 -- generic. In order to simplify e.g. ASIS queries, the
11264 -- Generic_Parent field links the declaration to the generic
11267 if No (Actual) then
11269 ("missing actual &",
11270 Instantiation_Node, Gen_Obj);
11272 ("\in instantiation of & declared#",
11273 Instantiation_Node, Scope (A_Gen_Obj));
11274 Abandon_Instantiation (Instantiation_Node);
11277 if Present (Subt_Mark) then
11279 Make_Object_Renaming_Declaration (Loc,
11280 Defining_Identifier => New_Copy (Gen_Obj),
11281 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11284 else pragma Assert (Present (Acc_Def));
11286 Make_Object_Renaming_Declaration (Loc,
11287 Defining_Identifier => New_Copy (Gen_Obj),
11288 Access_Definition => New_Copy_Tree (Acc_Def),
11292 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11294 -- The analysis of the actual may produce Insert_Action nodes, so
11295 -- the declaration must have a context in which to attach them.
11297 Append (Decl_Node, List);
11300 -- Return if the analysis of the actual reported some error
11302 if Etype (Actual) = Any_Type then
11306 -- This check is performed here because Analyze_Object_Renaming will
11307 -- not check it when Comes_From_Source is False. Note though that the
11308 -- check for the actual being the name of an object will be performed
11309 -- in Analyze_Object_Renaming.
11311 if Is_Object_Reference (Actual)
11312 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11315 ("illegal discriminant-dependent component for in out parameter",
11319 -- The actual has to be resolved in order to check that it is a
11320 -- variable (due to cases such as F (1), where F returns access to
11321 -- an array, and for overloaded prefixes).
11323 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11325 -- If the type of the formal is not itself a formal, and the current
11326 -- unit is a child unit, the formal type must be declared in a
11327 -- parent, and must be retrieved by visibility.
11329 if Ftyp = Orig_Ftyp
11330 and then Is_Generic_Unit (Scope (Ftyp))
11331 and then Is_Child_Unit (Scope (A_Gen_Obj))
11334 Temp : constant Node_Id :=
11335 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11337 Set_Entity (Temp, Empty);
11339 Ftyp := Entity (Temp);
11343 if Is_Private_Type (Ftyp)
11344 and then not Is_Private_Type (Etype (Actual))
11345 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11346 or else Base_Type (Etype (Actual)) = Ftyp)
11348 -- If the actual has the type of the full view of the formal, or
11349 -- else a non-private subtype of the formal, then the visibility
11350 -- of the formal type has changed. Add to the actuals a subtype
11351 -- declaration that will force the exchange of views in the body
11352 -- of the instance as well.
11355 Make_Subtype_Declaration (Loc,
11356 Defining_Identifier => Make_Temporary (Loc, 'P'),
11357 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11359 Prepend (Subt_Decl, List);
11361 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11362 Exchange_Declarations (Ftyp);
11365 Resolve (Actual, Ftyp);
11367 if not Denotes_Variable (Actual) then
11368 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11370 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11372 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11373 -- the type of the actual shall resolve to a specific anonymous
11376 if Ada_Version < Ada_2005
11377 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11378 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11381 ("type of actual does not match type of&", Actual, Gen_Obj);
11385 Note_Possible_Modification (Actual, Sure => True);
11387 -- Check for instantiation with atomic/volatile/VFA object actual for
11388 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11390 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11392 ("cannot instantiate nonatomic formal & of mode in out",
11394 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11396 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11399 ("cannot instantiate nonvolatile formal & of mode in out",
11401 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11403 elsif Is_Volatile_Full_Access_Object (Actual)
11404 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11407 ("cannot instantiate nonfull access formal & of mode in out",
11410 ("\with full access object actual (RM C.6(12))", Actual);
11413 -- Check for instantiation on nonatomic subcomponent of a full access
11414 -- object in Ada 2020 (RM C.6 (12)).
11416 if Ada_Version >= Ada_2020
11417 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11418 and then not Is_Atomic_Object (Actual)
11421 ("cannot instantiate formal & of mode in out with actual",
11424 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11428 -- Check actual/formal compatibility with respect to the four
11429 -- volatility refinement aspects.
11432 Actual_Obj : Entity_Id;
11433 N : Node_Id := Actual;
11435 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11436 -- pointer dereference like component selection.
11438 if Is_Entity_Name (N) then
11439 Actual_Obj := Entity (N);
11444 when N_Indexed_Component
11445 | N_Selected_Component
11447 | N_Explicit_Dereference
11451 when N_Type_Conversion =>
11452 N := Expression (N);
11455 Actual_Obj := Etype (N);
11460 Check_Volatility_Compatibility
11461 (Actual_Obj, A_Gen_Obj, "actual object",
11462 "its corresponding formal object of mode in out",
11463 Srcpos_Bearer => Actual);
11466 -- Formal in-parameter
11469 -- The instantiation of a generic formal in-parameter is constant
11470 -- declaration. The actual is the expression for that declaration.
11471 -- Its type is a full copy of the type of the formal. This may be
11472 -- an access to subprogram, for which we need to generate entities
11473 -- for the formals in the new signature.
11475 if Present (Actual) then
11476 if Present (Subt_Mark) then
11477 Def := New_Copy_Tree (Subt_Mark);
11479 pragma Assert (Present (Acc_Def));
11480 Def := New_Copy_Tree (Acc_Def);
11484 Make_Object_Declaration (Loc,
11485 Defining_Identifier => New_Copy (Gen_Obj),
11486 Constant_Present => True,
11487 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11488 Object_Definition => Def,
11489 Expression => Actual);
11491 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11493 -- A generic formal object of a tagged type is defined to be
11494 -- aliased so the new constant must also be treated as aliased.
11496 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11497 Set_Aliased_Present (Decl_Node);
11500 Append (Decl_Node, List);
11502 -- No need to repeat (pre-)analysis of some expression nodes
11503 -- already handled in Preanalyze_Actuals.
11505 if Nkind (Actual) /= N_Allocator then
11508 -- Return if the analysis of the actual reported some error
11510 if Etype (Actual) = Any_Type then
11516 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11520 Typ := Get_Instance_Of (Formal_Type);
11522 -- If the actual appears in the current or an enclosing scope,
11523 -- use its type directly. This is relevant if it has an actual
11524 -- subtype that is distinct from its nominal one. This cannot
11525 -- be done in general because the type of the actual may
11526 -- depend on other actuals, and only be fully determined when
11527 -- the enclosing instance is analyzed.
11529 if Present (Etype (Actual))
11530 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11532 Freeze_Before (Instantiation_Node, Etype (Actual));
11534 Freeze_Before (Instantiation_Node, Typ);
11537 -- If the actual is an aggregate, perform name resolution on
11538 -- its components (the analysis of an aggregate does not do it)
11539 -- to capture local names that may be hidden if the generic is
11542 if Nkind (Actual) = N_Aggregate then
11543 Preanalyze_And_Resolve (Actual, Typ);
11546 if Is_Limited_Type (Typ)
11547 and then not OK_For_Limited_Init (Typ, Actual)
11550 ("initialization not allowed for limited types", Actual);
11551 Explain_Limited_Type (Typ, Actual);
11555 elsif Present (Default_Expression (Formal)) then
11557 -- Use default to construct declaration
11559 if Present (Subt_Mark) then
11560 Def := New_Copy (Subt_Mark);
11562 pragma Assert (Present (Acc_Def));
11563 Def := New_Copy_Tree (Acc_Def);
11567 Make_Object_Declaration (Sloc (Formal),
11568 Defining_Identifier => New_Copy (Gen_Obj),
11569 Constant_Present => True,
11570 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11571 Object_Definition => Def,
11572 Expression => New_Copy_Tree
11573 (Default_Expression (Formal)));
11575 Set_Corresponding_Generic_Association
11576 (Decl_Node, Expression (Decl_Node));
11578 Append (Decl_Node, List);
11579 Set_Analyzed (Expression (Decl_Node), False);
11582 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11583 Error_Msg_NE ("\in instantiation of & declared#",
11584 Instantiation_Node, Scope (A_Gen_Obj));
11586 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11588 -- Create dummy constant declaration so that instance can be
11589 -- analyzed, to minimize cascaded visibility errors.
11591 if Present (Subt_Mark) then
11593 else pragma Assert (Present (Acc_Def));
11598 Make_Object_Declaration (Loc,
11599 Defining_Identifier => New_Copy (Gen_Obj),
11600 Constant_Present => True,
11601 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11602 Object_Definition => New_Copy (Def),
11604 Make_Attribute_Reference (Sloc (Gen_Obj),
11605 Attribute_Name => Name_First,
11606 Prefix => New_Copy (Def)));
11608 Append (Decl_Node, List);
11611 Abandon_Instantiation (Instantiation_Node);
11616 if Nkind (Actual) in N_Has_Entity then
11617 Actual_Decl := Parent (Entity (Actual));
11620 -- Ada 2005 (AI-423) refined by AI12-0287:
11621 -- For an object_renaming_declaration with a null_exclusion or an
11622 -- access_definition that has a null_exclusion, the subtype of the
11623 -- object_name shall exclude null. In addition, if the
11624 -- object_renaming_declaration occurs within the body of a generic unit
11625 -- G or within the body of a generic unit declared within the
11626 -- declarative region of generic unit G, then:
11627 -- * if the object_name statically denotes a generic formal object of
11628 -- mode in out of G, then the declaration of that object shall have a
11630 -- * if the object_name statically denotes a call of a generic formal
11631 -- function of G, then the declaration of the result of that function
11632 -- shall have a null_exclusion.
11634 if Ada_Version >= Ada_2005
11635 and then Present (Actual_Decl)
11636 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11637 | N_Object_Declaration
11638 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11639 and then not Has_Null_Exclusion (Actual_Decl)
11640 and then Has_Null_Exclusion (Analyzed_Formal)
11641 and then Ekind (Defining_Identifier (Analyzed_Formal))
11642 = E_Generic_In_Out_Parameter
11643 and then ((In_Generic_Scope (Entity (Actual))
11644 and then In_Package_Body (Scope (Entity (Actual))))
11645 or else not Can_Never_Be_Null (Etype (Actual)))
11647 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11649 ("actual must exclude null to match generic formal#", Actual);
11652 -- An effectively volatile object cannot be used as an actual in a
11653 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11654 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11655 -- legality rule, and also verifies that the actual is an object.
11658 and then Present (Actual)
11659 and then Is_Object_Reference (Actual)
11660 and then Is_Effectively_Volatile_Object (Actual)
11661 and then not Is_Effectively_Volatile (A_Gen_Obj)
11664 ("volatile object cannot act as actual in generic instantiation",
11669 end Instantiate_Object;
11671 ------------------------------
11672 -- Instantiate_Package_Body --
11673 ------------------------------
11675 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11676 -- must be replaced by gotos which jump to the end of the routine in order
11677 -- to restore the Ghost and SPARK modes.
11679 procedure Instantiate_Package_Body
11680 (Body_Info : Pending_Body_Info;
11681 Inlined_Body : Boolean := False;
11682 Body_Optional : Boolean := False)
11684 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11685 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11686 Act_Spec : constant Node_Id := Specification (Act_Decl);
11687 Ctx_Parents : Elist_Id := No_Elist;
11688 Ctx_Top : Int := 0;
11689 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11690 Gen_Id : constant Node_Id := Name (Inst_Node);
11691 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11692 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11693 Loc : constant Source_Ptr := Sloc (Inst_Node);
11695 procedure Check_Initialized_Types;
11696 -- In a generic package body, an entity of a generic private type may
11697 -- appear uninitialized. This is suspicious, unless the actual is a
11698 -- fully initialized type.
11700 procedure Install_Parents_Of_Generic_Context
11701 (Inst_Scope : Entity_Id;
11702 Ctx_Parents : out Elist_Id);
11703 -- Inst_Scope is the scope where the instance appears within; when it
11704 -- appears within a generic child package G, this routine collects and
11705 -- installs the enclosing packages of G in the scopes stack; installed
11706 -- packages are returned in Ctx_Parents.
11708 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11709 -- Reverse effect after instantiation is complete
11711 -----------------------------
11712 -- Check_Initialized_Types --
11713 -----------------------------
11715 procedure Check_Initialized_Types is
11717 Formal : Entity_Id;
11718 Actual : Entity_Id;
11719 Uninit_Var : Entity_Id;
11722 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11723 while Present (Decl) loop
11724 Uninit_Var := Empty;
11726 if Nkind (Decl) = N_Private_Extension_Declaration then
11727 Uninit_Var := Uninitialized_Variable (Decl);
11729 elsif Nkind (Decl) = N_Formal_Type_Declaration
11730 and then Nkind (Formal_Type_Definition (Decl)) =
11731 N_Formal_Private_Type_Definition
11734 Uninitialized_Variable (Formal_Type_Definition (Decl));
11737 if Present (Uninit_Var) then
11738 Formal := Defining_Identifier (Decl);
11739 Actual := First_Entity (Act_Decl_Id);
11741 -- For each formal there is a subtype declaration that renames
11742 -- the actual and has the same name as the formal. Locate the
11743 -- formal for warning message about uninitialized variables
11744 -- in the generic, for which the actual type should be a fully
11745 -- initialized type.
11747 while Present (Actual) loop
11748 exit when Ekind (Actual) = E_Package
11749 and then Present (Renamed_Object (Actual));
11751 if Chars (Actual) = Chars (Formal)
11752 and then not Is_Scalar_Type (Actual)
11753 and then not Is_Fully_Initialized_Type (Actual)
11754 and then Warn_On_No_Value_Assigned
11756 Error_Msg_Node_2 := Formal;
11758 ("generic unit has uninitialized variable& of "
11759 & "formal private type &?v?", Actual, Uninit_Var);
11761 ("actual type for& should be fully initialized type?v?",
11766 Next_Entity (Actual);
11772 end Check_Initialized_Types;
11774 ----------------------------------------
11775 -- Install_Parents_Of_Generic_Context --
11776 ----------------------------------------
11778 procedure Install_Parents_Of_Generic_Context
11779 (Inst_Scope : Entity_Id;
11780 Ctx_Parents : out Elist_Id)
11786 Ctx_Parents := New_Elmt_List;
11788 -- Collect context parents (ie. parents where the instantiation
11789 -- appears within).
11792 while S /= Standard_Standard loop
11793 Prepend_Elmt (S, Ctx_Parents);
11797 -- Install enclosing parents
11799 Elmt := First_Elmt (Ctx_Parents);
11800 while Present (Elmt) loop
11801 Push_Scope (Node (Elmt));
11802 Set_Is_Immediately_Visible (Node (Elmt));
11805 end Install_Parents_Of_Generic_Context;
11807 ---------------------------------------
11808 -- Remove_Parents_Of_Generic_Context --
11809 ---------------------------------------
11811 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11815 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11817 Elmt := Last_Elmt (Ctx_Parents);
11818 while Present (Elmt) loop
11819 pragma Assert (Current_Scope = Node (Elmt));
11820 Set_Is_Immediately_Visible (Current_Scope, False);
11823 Remove_Last_Elmt (Ctx_Parents);
11824 Elmt := Last_Elmt (Ctx_Parents);
11826 end Remove_Parents_Of_Generic_Context;
11830 -- The following constants capture the context prior to instantiating
11831 -- the package body.
11833 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11834 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11835 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11836 Saved_ISMP : constant Boolean :=
11837 Ignore_SPARK_Mode_Pragmas_In_Instance;
11838 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11839 Local_Suppress_Stack_Top;
11840 Saved_SC : constant Boolean := Style_Check;
11841 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11842 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11843 Saved_SS : constant Suppress_Record := Scope_Suppress;
11844 Saved_Warn : constant Warning_Record := Save_Warnings;
11846 Act_Body : Node_Id;
11847 Act_Body_Id : Entity_Id;
11848 Act_Body_Name : Node_Id;
11849 Gen_Body : Node_Id;
11850 Gen_Body_Id : Node_Id;
11851 Par_Ent : Entity_Id := Empty;
11852 Par_Installed : Boolean := False;
11853 Par_Vis : Boolean := False;
11855 Scope_Check_Id : Entity_Id;
11856 Scope_Check_Last : Nat;
11857 -- Value of Current_Scope before calls to Install_Parents; used to check
11858 -- that scopes are correctly removed after instantiation.
11860 Vis_Prims_List : Elist_Id := No_Elist;
11861 -- List of primitives made temporarily visible in the instantiation
11862 -- to match the visibility of the formal type.
11864 -- Start of processing for Instantiate_Package_Body
11867 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11869 -- The instance body may already have been processed, as the parent of
11870 -- another instance that is inlined (Load_Parent_Of_Generic).
11872 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11876 -- The package being instantiated may be subject to pragma Ghost. Set
11877 -- the mode now to ensure that any nodes generated during instantiation
11878 -- are properly marked as Ghost.
11880 Set_Ghost_Mode (Act_Decl_Id);
11882 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11884 -- Re-establish the state of information on which checks are suppressed.
11885 -- This information was set in Body_Info at the point of instantiation,
11886 -- and now we restore it so that the instance is compiled using the
11887 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11889 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11890 Scope_Suppress := Body_Info.Scope_Suppress;
11892 Restore_Config_Switches (Body_Info.Config_Switches);
11893 Restore_Warnings (Body_Info.Warnings);
11895 if No (Gen_Body_Id) then
11897 -- Do not look for parent of generic body if none is required.
11898 -- This may happen when the routine is called as part of the
11899 -- Pending_Instantiations processing, when nested instances
11900 -- may precede the one generated from the main unit.
11902 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11903 and then Body_Optional
11907 Load_Parent_Of_Generic
11908 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11910 -- Surprisingly enough, loading the body of the parent can cause
11911 -- the body to be instantiated and the double instantiation needs
11912 -- to be prevented in order to avoid giving bogus semantic errors.
11914 -- This case can occur because of the Collect_Previous_Instances
11915 -- machinery of Load_Parent_Of_Generic, which will instantiate
11916 -- bodies that are deemed to be ahead of the body of the parent
11917 -- in the compilation unit. But the relative position of these
11918 -- bodies is computed using the mere comparison of their Sloc.
11920 -- Now suppose that you have two generic packages G and H, with
11921 -- G containing a mere instantiation of H:
11927 -- package Nested_G is
11938 -- package My_H is new H;
11942 -- and a third package Q instantiating G and Nested_G:
11948 -- package My_G is new G;
11950 -- package My_Nested_G is new My_G.My_H.Nested_G;
11954 -- The body to be instantiated is that of My_Nested_G and its
11955 -- parent is the instance My_G.My_H. This latter instantiation
11956 -- is done when My_G is analyzed, i.e. after the declarations
11957 -- of My_G and My_Nested_G have been parsed; as a result, the
11958 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11960 -- Therefore loading the body of My_G.My_H will cause the body
11961 -- of My_Nested_G to be instantiated because it is deemed to be
11962 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11963 -- will again be invoked on My_G.My_H, but this time with the
11964 -- Collect_Previous_Instances machinery disabled, so there is
11965 -- no endless mutual recursion and things are done in order.
11967 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11971 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11975 -- Establish global variable for sloc adjustment and for error recovery
11976 -- In the case of an instance body for an instantiation with actuals
11977 -- from a limited view, the instance body is placed at the beginning
11978 -- of the enclosing package body: use the body entity as the source
11979 -- location for nodes of the instance body.
11981 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11983 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11984 Body_Id : constant Node_Id :=
11985 Corresponding_Body (Unit_Declaration_Node (Scop));
11988 Instantiation_Node := Body_Id;
11991 Instantiation_Node := Inst_Node;
11994 if Present (Gen_Body_Id) then
11995 Save_Env (Gen_Unit, Act_Decl_Id);
11996 Style_Check := False;
11998 -- If the context of the instance is subject to SPARK_Mode "off", the
11999 -- annotation is missing, or the body is instantiated at a later pass
12000 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12001 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12004 if SPARK_Mode /= On
12005 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12007 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12010 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12011 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12013 Create_Instantiation_Source
12014 (Inst_Node, Gen_Body_Id, S_Adjustment);
12018 (Original_Node (Gen_Body), Empty, Instantiating => True);
12020 -- Create proper (possibly qualified) defining name for the body, to
12021 -- correspond to the one in the spec.
12024 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12025 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12027 -- Some attributes of spec entity are not inherited by body entity
12029 Set_Handler_Records (Act_Body_Id, No_List);
12031 if Nkind (Defining_Unit_Name (Act_Spec)) =
12032 N_Defining_Program_Unit_Name
12035 Make_Defining_Program_Unit_Name (Loc,
12037 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12038 Defining_Identifier => Act_Body_Id);
12040 Act_Body_Name := Act_Body_Id;
12043 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12045 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12046 Check_Generic_Actuals (Act_Decl_Id, False);
12047 Check_Initialized_Types;
12049 -- Install primitives hidden at the point of the instantiation but
12050 -- visible when processing the generic formals
12056 E := First_Entity (Act_Decl_Id);
12057 while Present (E) loop
12059 and then not Is_Itype (E)
12060 and then Is_Generic_Actual_Type (E)
12061 and then Is_Tagged_Type (E)
12063 Install_Hidden_Primitives
12064 (Prims_List => Vis_Prims_List,
12065 Gen_T => Generic_Parent_Type (Parent (E)),
12073 Scope_Check_Id := Current_Scope;
12074 Scope_Check_Last := Scope_Stack.Last;
12076 -- If the instantiation appears within a generic child some actual
12077 -- parameter may be the current instance of the enclosing generic
12081 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12084 if Is_Child_Unit (Inst_Scope)
12085 and then Ekind (Inst_Scope) = E_Generic_Package
12086 and then Present (Generic_Associations (Inst_Node))
12088 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12090 -- Hide them from visibility; required to avoid conflicts
12091 -- installing the parent instance.
12093 if Present (Ctx_Parents) then
12094 Push_Scope (Standard_Standard);
12095 Ctx_Top := Scope_Stack.Last;
12096 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12101 -- If it is a child unit, make the parent instance (which is an
12102 -- instance of the parent of the generic) visible. The parent
12103 -- instance is the prefix of the name of the generic unit.
12105 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12106 and then Nkind (Gen_Id) = N_Expanded_Name
12108 Par_Ent := Entity (Prefix (Gen_Id));
12109 Par_Vis := Is_Immediately_Visible (Par_Ent);
12110 Install_Parent (Par_Ent, In_Body => True);
12111 Par_Installed := True;
12113 elsif Is_Child_Unit (Gen_Unit) then
12114 Par_Ent := Scope (Gen_Unit);
12115 Par_Vis := Is_Immediately_Visible (Par_Ent);
12116 Install_Parent (Par_Ent, In_Body => True);
12117 Par_Installed := True;
12120 -- If the instantiation is a library unit, and this is the main unit,
12121 -- then build the resulting compilation unit nodes for the instance.
12122 -- If this is a compilation unit but it is not the main unit, then it
12123 -- is the body of a unit in the context, that is being compiled
12124 -- because it is encloses some inlined unit or another generic unit
12125 -- being instantiated. In that case, this body is not part of the
12126 -- current compilation, and is not attached to the tree, but its
12127 -- parent must be set for analysis.
12129 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12131 -- Replace instance node with body of instance, and create new
12132 -- node for corresponding instance declaration.
12134 Build_Instance_Compilation_Unit_Nodes
12135 (Inst_Node, Act_Body, Act_Decl);
12137 -- If the instantiation appears within a generic child package
12138 -- enable visibility of current instance of enclosing generic
12141 if Present (Ctx_Parents) then
12142 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12143 Analyze (Inst_Node);
12144 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12146 Analyze (Inst_Node);
12149 if Parent (Inst_Node) = Cunit (Main_Unit) then
12151 -- If the instance is a child unit itself, then set the scope
12152 -- of the expanded body to be the parent of the instantiation
12153 -- (ensuring that the fully qualified name will be generated
12154 -- for the elaboration subprogram).
12156 if Nkind (Defining_Unit_Name (Act_Spec)) =
12157 N_Defining_Program_Unit_Name
12159 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12163 -- Case where instantiation is not a library unit
12166 -- If this is an early instantiation, i.e. appears textually
12167 -- before the corresponding body and must be elaborated first,
12168 -- indicate that the body instance is to be delayed.
12170 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12172 -- If the instantiation appears within a generic child package
12173 -- enable visibility of current instance of enclosing generic
12176 if Present (Ctx_Parents) then
12177 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12178 Analyze (Act_Body);
12179 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12181 Analyze (Act_Body);
12185 Inherit_Context (Gen_Body, Inst_Node);
12187 if Par_Installed then
12188 Remove_Parent (In_Body => True);
12190 -- Restore the previous visibility of the parent
12192 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12195 -- Remove the parent instances if they have been placed on the scope
12196 -- stack to compile the body.
12198 if Present (Ctx_Parents) then
12199 pragma Assert (Scope_Stack.Last = Ctx_Top
12200 and then Current_Scope = Standard_Standard);
12203 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12206 pragma Assert (Current_Scope = Scope_Check_Id);
12207 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12209 Restore_Hidden_Primitives (Vis_Prims_List);
12211 -- Restore the private views that were made visible when the body of
12212 -- the instantiation was created. Note that, in the case where one of
12213 -- these private views is declared in the parent, there is a nesting
12214 -- issue with the calls to Install_Parent and Remove_Parent made in
12215 -- between above with In_Body set to True, because these calls also
12216 -- want to swap and restore this private view respectively. In this
12217 -- case, the call to Install_Parent does nothing, but the call to
12218 -- Remove_Parent does restore the private view, thus undercutting the
12219 -- call to Restore_Private_Views. That's OK under the condition that
12220 -- the two mechanisms swap exactly the same entities, in particular
12221 -- the private entities dependent on the primary private entities.
12223 Restore_Private_Views (Act_Decl_Id);
12225 -- Remove the current unit from visibility if this is an instance
12226 -- that is not elaborated on the fly for inlining purposes.
12228 if not Inlined_Body then
12229 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12234 -- If we have no body, and the unit requires a body, then complain. This
12235 -- complaint is suppressed if we have detected other errors (since a
12236 -- common reason for missing the body is that it had errors).
12237 -- In CodePeer mode, a warning has been emitted already, no need for
12238 -- further messages.
12240 elsif Unit_Requires_Body (Gen_Unit)
12241 and then not Body_Optional
12243 if CodePeer_Mode then
12246 elsif Serious_Errors_Detected = 0 then
12248 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12250 -- Don't attempt to perform any cleanup actions if some other error
12251 -- was already detected, since this can cause blowups.
12257 -- Case of package that does not need a body
12260 -- If the instantiation of the declaration is a library unit, rewrite
12261 -- the original package instantiation as a package declaration in the
12262 -- compilation unit node.
12264 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12265 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12266 Rewrite (Inst_Node, Act_Decl);
12268 -- Generate elaboration entity, in case spec has elaboration code.
12269 -- This cannot be done when the instance is analyzed, because it
12270 -- is not known yet whether the body exists.
12272 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12273 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12275 -- If the instantiation is not a library unit, then append the
12276 -- declaration to the list of implicitly generated entities, unless
12277 -- it is already a list member which means that it was already
12280 elsif not Is_List_Member (Act_Decl) then
12281 Mark_Rewrite_Insertion (Act_Decl);
12282 Insert_Before (Inst_Node, Act_Decl);
12288 -- Restore the context that was in effect prior to instantiating the
12291 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12292 Local_Suppress_Stack_Top := Saved_LSST;
12293 Scope_Suppress := Saved_SS;
12294 Style_Check := Saved_SC;
12296 Expander_Mode_Restore;
12297 Restore_Config_Switches (Saved_CS);
12298 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12299 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12300 Restore_Warnings (Saved_Warn);
12301 end Instantiate_Package_Body;
12303 ---------------------------------
12304 -- Instantiate_Subprogram_Body --
12305 ---------------------------------
12307 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12308 -- must be replaced by gotos which jump to the end of the routine in order
12309 -- to restore the Ghost and SPARK modes.
12311 procedure Instantiate_Subprogram_Body
12312 (Body_Info : Pending_Body_Info;
12313 Body_Optional : Boolean := False)
12315 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12316 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12317 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12318 Gen_Id : constant Node_Id := Name (Inst_Node);
12319 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12320 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12321 Loc : constant Source_Ptr := Sloc (Inst_Node);
12322 Pack_Id : constant Entity_Id :=
12323 Defining_Unit_Name (Parent (Act_Decl));
12325 -- The following constants capture the context prior to instantiating
12326 -- the subprogram body.
12328 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12329 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12330 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12331 Saved_ISMP : constant Boolean :=
12332 Ignore_SPARK_Mode_Pragmas_In_Instance;
12333 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12334 Local_Suppress_Stack_Top;
12335 Saved_SC : constant Boolean := Style_Check;
12336 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12337 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12338 Saved_SS : constant Suppress_Record := Scope_Suppress;
12339 Saved_Warn : constant Warning_Record := Save_Warnings;
12341 Act_Body : Node_Id;
12342 Act_Body_Id : Entity_Id;
12343 Gen_Body : Node_Id;
12344 Gen_Body_Id : Node_Id;
12345 Pack_Body : Node_Id;
12346 Par_Ent : Entity_Id := Empty;
12347 Par_Installed : Boolean := False;
12348 Par_Vis : Boolean := False;
12349 Ret_Expr : Node_Id;
12352 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12354 -- Subprogram body may have been created already because of an inline
12355 -- pragma, or because of multiple elaborations of the enclosing package
12356 -- when several instances of the subprogram appear in the main unit.
12358 if Present (Corresponding_Body (Act_Decl)) then
12362 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12363 -- the mode now to ensure that any nodes generated during instantiation
12364 -- are properly marked as Ghost.
12366 Set_Ghost_Mode (Act_Decl_Id);
12368 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12370 -- Re-establish the state of information on which checks are suppressed.
12371 -- This information was set in Body_Info at the point of instantiation,
12372 -- and now we restore it so that the instance is compiled using the
12373 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12375 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12376 Scope_Suppress := Body_Info.Scope_Suppress;
12378 Restore_Config_Switches (Body_Info.Config_Switches);
12379 Restore_Warnings (Body_Info.Warnings);
12381 if No (Gen_Body_Id) then
12383 -- For imported generic subprogram, no body to compile, complete
12384 -- the spec entity appropriately.
12386 if Is_Imported (Gen_Unit) then
12387 Set_Is_Imported (Act_Decl_Id);
12388 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12389 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12390 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12391 Set_Has_Completion (Act_Decl_Id);
12394 -- For other cases, compile the body
12397 Load_Parent_Of_Generic
12398 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12399 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12403 Instantiation_Node := Inst_Node;
12405 if Present (Gen_Body_Id) then
12406 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12408 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12410 -- Either body is not present, or context is non-expanding, as
12411 -- when compiling a subunit. Mark the instance as completed, and
12412 -- diagnose a missing body when needed.
12415 and then Operating_Mode = Generate_Code
12417 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12420 Set_Has_Completion (Act_Decl_Id);
12424 Save_Env (Gen_Unit, Act_Decl_Id);
12425 Style_Check := False;
12427 -- If the context of the instance is subject to SPARK_Mode "off", the
12428 -- annotation is missing, or the body is instantiated at a later pass
12429 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12430 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12433 if SPARK_Mode /= On
12434 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12436 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12439 -- If the context of an instance is not subject to SPARK_Mode "off",
12440 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12441 -- the latter should be the one applicable to the instance.
12443 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12444 and then SPARK_Mode /= Off
12445 and then Present (SPARK_Pragma (Gen_Body_Id))
12447 Set_SPARK_Mode (Gen_Body_Id);
12450 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12451 Create_Instantiation_Source
12458 (Original_Node (Gen_Body), Empty, Instantiating => True);
12460 -- Create proper defining name for the body, to correspond to the one
12464 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12466 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12467 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12469 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12470 Set_Has_Completion (Act_Decl_Id);
12471 Check_Generic_Actuals (Pack_Id, False);
12473 -- Generate a reference to link the visible subprogram instance to
12474 -- the generic body, which for navigation purposes is the only
12475 -- available source for the instance.
12478 (Related_Instance (Pack_Id),
12479 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12481 -- If it is a child unit, make the parent instance (which is an
12482 -- instance of the parent of the generic) visible. The parent
12483 -- instance is the prefix of the name of the generic unit.
12485 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12486 and then Nkind (Gen_Id) = N_Expanded_Name
12488 Par_Ent := Entity (Prefix (Gen_Id));
12489 Par_Vis := Is_Immediately_Visible (Par_Ent);
12490 Install_Parent (Par_Ent, In_Body => True);
12491 Par_Installed := True;
12493 elsif Is_Child_Unit (Gen_Unit) then
12494 Par_Ent := Scope (Gen_Unit);
12495 Par_Vis := Is_Immediately_Visible (Par_Ent);
12496 Install_Parent (Par_Ent, In_Body => True);
12497 Par_Installed := True;
12500 -- Subprogram body is placed in the body of wrapper package,
12501 -- whose spec contains the subprogram declaration as well as
12502 -- the renaming declarations for the generic parameters.
12505 Make_Package_Body (Loc,
12506 Defining_Unit_Name => New_Copy (Pack_Id),
12507 Declarations => New_List (Act_Body));
12509 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12511 -- If the instantiation is a library unit, then build resulting
12512 -- compilation unit nodes for the instance. The declaration of
12513 -- the enclosing package is the grandparent of the subprogram
12514 -- declaration. First replace the instantiation node as the unit
12515 -- of the corresponding compilation.
12517 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12518 if Parent (Inst_Node) = Cunit (Main_Unit) then
12519 Set_Unit (Parent (Inst_Node), Inst_Node);
12520 Build_Instance_Compilation_Unit_Nodes
12521 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12522 Analyze (Inst_Node);
12524 Set_Parent (Pack_Body, Parent (Inst_Node));
12525 Analyze (Pack_Body);
12529 Insert_Before (Inst_Node, Pack_Body);
12530 Mark_Rewrite_Insertion (Pack_Body);
12531 Analyze (Pack_Body);
12533 if Expander_Active then
12534 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12538 Inherit_Context (Gen_Body, Inst_Node);
12540 Restore_Private_Views (Pack_Id, False);
12542 if Par_Installed then
12543 Remove_Parent (In_Body => True);
12545 -- Restore the previous visibility of the parent
12547 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12552 -- Body not found. Error was emitted already. If there were no previous
12553 -- errors, this may be an instance whose scope is a premature instance.
12554 -- In that case we must insure that the (legal) program does raise
12555 -- program error if executed. We generate a subprogram body for this
12556 -- purpose. See DEC ac30vso.
12558 -- Should not reference proprietary DEC tests in comments ???
12560 elsif Serious_Errors_Detected = 0
12561 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12563 if Body_Optional then
12566 elsif Ekind (Act_Decl_Id) = E_Procedure then
12568 Make_Subprogram_Body (Loc,
12570 Make_Procedure_Specification (Loc,
12571 Defining_Unit_Name =>
12572 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12573 Parameter_Specifications =>
12575 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12577 Declarations => Empty_List,
12578 Handled_Statement_Sequence =>
12579 Make_Handled_Sequence_Of_Statements (Loc,
12580 Statements => New_List (
12581 Make_Raise_Program_Error (Loc,
12582 Reason => PE_Access_Before_Elaboration))));
12586 Make_Raise_Program_Error (Loc,
12587 Reason => PE_Access_Before_Elaboration);
12589 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12590 Set_Analyzed (Ret_Expr);
12593 Make_Subprogram_Body (Loc,
12595 Make_Function_Specification (Loc,
12596 Defining_Unit_Name =>
12597 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12598 Parameter_Specifications =>
12600 (Parameter_Specifications (Parent (Act_Decl_Id))),
12601 Result_Definition =>
12602 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12604 Declarations => Empty_List,
12605 Handled_Statement_Sequence =>
12606 Make_Handled_Sequence_Of_Statements (Loc,
12607 Statements => New_List (
12608 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12612 Make_Package_Body (Loc,
12613 Defining_Unit_Name => New_Copy (Pack_Id),
12614 Declarations => New_List (Act_Body));
12616 Insert_After (Inst_Node, Pack_Body);
12617 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12618 Analyze (Pack_Body);
12623 -- Restore the context that was in effect prior to instantiating the
12624 -- subprogram body.
12626 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12627 Local_Suppress_Stack_Top := Saved_LSST;
12628 Scope_Suppress := Saved_SS;
12629 Style_Check := Saved_SC;
12631 Expander_Mode_Restore;
12632 Restore_Config_Switches (Saved_CS);
12633 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12634 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12635 Restore_Warnings (Saved_Warn);
12636 end Instantiate_Subprogram_Body;
12638 ----------------------
12639 -- Instantiate_Type --
12640 ----------------------
12642 function Instantiate_Type
12645 Analyzed_Formal : Node_Id;
12646 Actual_Decls : List_Id) return List_Id
12648 A_Gen_T : constant Entity_Id :=
12649 Defining_Identifier (Analyzed_Formal);
12650 Def : constant Node_Id := Formal_Type_Definition (Formal);
12651 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12653 Ancestor : Entity_Id := Empty;
12654 Decl_Node : Node_Id;
12655 Decl_Nodes : List_Id;
12659 procedure Check_Shared_Variable_Control_Aspects;
12660 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12661 -- that may be specified for a formal type are obeyed by the actual.
12663 procedure Diagnose_Predicated_Actual;
12664 -- There are a number of constructs in which a discrete type with
12665 -- predicates is illegal, e.g. as an index in an array type declaration.
12666 -- If a generic type is used is such a construct in a generic package
12667 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12668 -- of the generic contract that the actual cannot have predicates.
12670 procedure Validate_Array_Type_Instance;
12671 procedure Validate_Access_Subprogram_Instance;
12672 procedure Validate_Access_Type_Instance;
12673 procedure Validate_Derived_Type_Instance;
12674 procedure Validate_Derived_Interface_Type_Instance;
12675 procedure Validate_Discriminated_Formal_Type;
12676 procedure Validate_Interface_Type_Instance;
12677 procedure Validate_Private_Type_Instance;
12678 procedure Validate_Incomplete_Type_Instance;
12679 -- These procedures perform validation tests for the named case.
12680 -- Validate_Discriminated_Formal_Type is shared by formal private
12681 -- types and Ada 2012 formal incomplete types.
12683 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12684 -- Check that base types are the same and that the subtypes match
12685 -- statically. Used in several of the above.
12687 --------------------------------------------
12688 -- Check_Shared_Variable_Control_Aspects --
12689 --------------------------------------------
12691 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12692 -- that may be specified for the formal are obeyed by the actual.
12693 -- If the formal is a derived type the aspect specifications must match.
12694 -- NOTE: AI12-0282 implies that matching of aspects is required between
12695 -- formal and actual in all cases, but this is too restrictive.
12696 -- In particular it violates a language design rule: a limited private
12697 -- indefinite formal can be matched by any actual. The current code
12698 -- reflects an older and more permissive version of RM C.6 (12/5).
12700 procedure Check_Shared_Variable_Control_Aspects is
12702 if Ada_Version >= Ada_2020 then
12703 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12705 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12707 elsif Is_Derived_Type (A_Gen_T)
12708 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12711 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12714 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12716 ("actual for& must have Volatile aspect",
12719 elsif Is_Derived_Type (A_Gen_T)
12720 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12723 ("actual for& has different Volatile aspect",
12727 -- We assume that an array type whose atomic component type
12728 -- is Atomic is equivalent to an array type with the explicit
12729 -- aspect Has_Atomic_Components. This is a reasonable inference
12730 -- from the intent of AI12-0282, and makes it legal to use an
12731 -- actual that does not have the identical aspect as the formal.
12732 -- Ditto for volatile components.
12735 Actual_Atomic_Comp : constant Boolean :=
12736 Has_Atomic_Components (Act_T)
12737 or else (Is_Array_Type (Act_T)
12738 and then Is_Atomic (Component_Type (Act_T)));
12740 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12742 ("formal and actual for& must agree on atomic components",
12748 Actual_Volatile_Comp : constant Boolean :=
12749 Has_Volatile_Components (Act_T)
12750 or else (Is_Array_Type (Act_T)
12751 and then Is_Volatile (Component_Type (Act_T)));
12753 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12756 ("actual for& must have volatile components",
12761 -- The following two aspects do not require exact matching,
12762 -- but only one-way agreement. See RM C.6.
12764 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12767 ("actual for& must have Independent aspect specified",
12771 if Has_Independent_Components (A_Gen_T)
12772 and then not Has_Independent_Components (Act_T)
12775 ("actual for& must have Independent_Components specified",
12779 -- Check actual/formal compatibility with respect to the four
12780 -- volatility refinement aspects.
12782 Check_Volatility_Compatibility
12784 "actual type", "its corresponding formal type",
12785 Srcpos_Bearer => Act_T);
12787 end Check_Shared_Variable_Control_Aspects;
12789 ---------------------------------
12790 -- Diagnose_Predicated_Actual --
12791 ---------------------------------
12793 procedure Diagnose_Predicated_Actual is
12795 if No_Predicate_On_Actual (A_Gen_T)
12796 and then Has_Predicates (Act_T)
12799 ("actual for& cannot be a type with predicate",
12800 Instantiation_Node, A_Gen_T);
12802 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12803 and then Has_Predicates (Act_T)
12804 and then not Has_Static_Predicate_Aspect (Act_T)
12807 ("actual for& cannot be a type with a dynamic predicate",
12808 Instantiation_Node, A_Gen_T);
12810 end Diagnose_Predicated_Actual;
12812 --------------------
12813 -- Subtypes_Match --
12814 --------------------
12816 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12817 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12820 -- Some detailed comments would be useful here ???
12822 return ((Base_Type (T) = Act_T
12823 or else Base_Type (T) = Base_Type (Act_T))
12824 and then Subtypes_Statically_Match (T, Act_T))
12826 or else (Is_Class_Wide_Type (Gen_T)
12827 and then Is_Class_Wide_Type (Act_T)
12828 and then Subtypes_Match
12829 (Get_Instance_Of (Root_Type (Gen_T)),
12830 Root_Type (Act_T)))
12833 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12834 | E_Anonymous_Access_Type
12835 and then Ekind (Act_T) = Ekind (Gen_T)
12836 and then Subtypes_Statically_Match
12837 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12838 end Subtypes_Match;
12840 -----------------------------------------
12841 -- Validate_Access_Subprogram_Instance --
12842 -----------------------------------------
12844 procedure Validate_Access_Subprogram_Instance is
12846 if not Is_Access_Type (Act_T)
12847 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12850 ("expect access type in instantiation of &", Actual, Gen_T);
12851 Abandon_Instantiation (Actual);
12854 -- According to AI05-288, actuals for access_to_subprograms must be
12855 -- subtype conformant with the generic formal. Previous to AI05-288
12856 -- only mode conformance was required.
12858 -- This is a binding interpretation that applies to previous versions
12859 -- of the language, no need to maintain previous weaker checks.
12861 Check_Subtype_Conformant
12862 (Designated_Type (Act_T),
12863 Designated_Type (A_Gen_T),
12867 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12868 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12870 ("protected access type not allowed for formal &",
12874 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12876 ("expect protected access type for formal &",
12880 -- If the formal has a specified convention (which in most cases
12881 -- will be StdCall) verify that the actual has the same convention.
12883 if Has_Convention_Pragma (A_Gen_T)
12884 and then Convention (A_Gen_T) /= Convention (Act_T)
12886 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12888 ("actual for formal & must have convention %", Actual, Gen_T);
12891 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12893 ("non null exclusion of actual and formal & do not match",
12896 end Validate_Access_Subprogram_Instance;
12898 -----------------------------------
12899 -- Validate_Access_Type_Instance --
12900 -----------------------------------
12902 procedure Validate_Access_Type_Instance is
12903 Desig_Type : constant Entity_Id :=
12904 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12905 Desig_Act : Entity_Id;
12908 if not Is_Access_Type (Act_T) then
12910 ("expect access type in instantiation of &", Actual, Gen_T);
12911 Abandon_Instantiation (Actual);
12914 if Is_Access_Constant (A_Gen_T) then
12915 if not Is_Access_Constant (Act_T) then
12917 ("actual type must be access-to-constant type", Actual);
12918 Abandon_Instantiation (Actual);
12921 if Is_Access_Constant (Act_T) then
12923 ("actual type must be access-to-variable type", Actual);
12924 Abandon_Instantiation (Actual);
12926 elsif Ekind (A_Gen_T) = E_General_Access_Type
12927 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12929 Error_Msg_N -- CODEFIX
12930 ("actual must be general access type!", Actual);
12931 Error_Msg_NE -- CODEFIX
12932 ("add ALL to }!", Actual, Act_T);
12933 Abandon_Instantiation (Actual);
12937 -- The designated subtypes, that is to say the subtypes introduced
12938 -- by an access type declaration (and not by a subtype declaration)
12941 Desig_Act := Designated_Type (Base_Type (Act_T));
12943 -- The designated type may have been introduced through a limited_
12944 -- with clause, in which case retrieve the non-limited view. This
12945 -- applies to incomplete types as well as to class-wide types.
12947 if From_Limited_With (Desig_Act) then
12948 Desig_Act := Available_View (Desig_Act);
12951 if not Subtypes_Match (Desig_Type, Desig_Act) then
12953 ("designated type of actual does not match that of formal &",
12956 if not Predicates_Match (Desig_Type, Desig_Act) then
12957 Error_Msg_N ("\predicates do not match", Actual);
12960 Abandon_Instantiation (Actual);
12962 elsif Is_Access_Type (Designated_Type (Act_T))
12963 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12965 Is_Constrained (Designated_Type (Desig_Type))
12968 ("designated type of actual does not match that of formal &",
12971 if not Predicates_Match (Desig_Type, Desig_Act) then
12972 Error_Msg_N ("\predicates do not match", Actual);
12975 Abandon_Instantiation (Actual);
12978 -- Ada 2005: null-exclusion indicators of the two types must agree
12980 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12982 ("non null exclusion of actual and formal & do not match",
12985 end Validate_Access_Type_Instance;
12987 ----------------------------------
12988 -- Validate_Array_Type_Instance --
12989 ----------------------------------
12991 procedure Validate_Array_Type_Instance is
12996 function Formal_Dimensions return Nat;
12997 -- Count number of dimensions in array type formal
12999 -----------------------
13000 -- Formal_Dimensions --
13001 -----------------------
13003 function Formal_Dimensions return Nat is
13008 if Nkind (Def) = N_Constrained_Array_Definition then
13009 Index := First (Discrete_Subtype_Definitions (Def));
13011 Index := First (Subtype_Marks (Def));
13014 while Present (Index) loop
13016 Next_Index (Index);
13020 end Formal_Dimensions;
13022 -- Start of processing for Validate_Array_Type_Instance
13025 if not Is_Array_Type (Act_T) then
13027 ("expect array type in instantiation of &", Actual, Gen_T);
13028 Abandon_Instantiation (Actual);
13030 elsif Nkind (Def) = N_Constrained_Array_Definition then
13031 if not (Is_Constrained (Act_T)) then
13033 ("expect constrained array in instantiation of &",
13035 Abandon_Instantiation (Actual);
13039 if Is_Constrained (Act_T) then
13041 ("expect unconstrained array in instantiation of &",
13043 Abandon_Instantiation (Actual);
13047 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13049 ("dimensions of actual do not match formal &", Actual, Gen_T);
13050 Abandon_Instantiation (Actual);
13053 I1 := First_Index (A_Gen_T);
13054 I2 := First_Index (Act_T);
13055 for J in 1 .. Formal_Dimensions loop
13057 -- If the indexes of the actual were given by a subtype_mark,
13058 -- the index was transformed into a range attribute. Retrieve
13059 -- the original type mark for checking.
13061 if Is_Entity_Name (Original_Node (I2)) then
13062 T2 := Entity (Original_Node (I2));
13067 if not Subtypes_Match
13068 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13071 ("index types of actual do not match those of formal &",
13073 Abandon_Instantiation (Actual);
13080 -- Check matching subtypes. Note that there are complex visibility
13081 -- issues when the generic is a child unit and some aspect of the
13082 -- generic type is declared in a parent unit of the generic. We do
13083 -- the test to handle this special case only after a direct check
13084 -- for static matching has failed. The case where both the component
13085 -- type and the array type are separate formals, and the component
13086 -- type is a private view may also require special checking in
13087 -- Subtypes_Match. Finally, we assume that a child instance where
13088 -- the component type comes from a formal of a parent instance is
13089 -- correct because the generic was correct. A more precise check
13090 -- seems too complex to install???
13093 (Component_Type (A_Gen_T), Component_Type (Act_T))
13096 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13097 Component_Type (Act_T))
13099 (not Inside_A_Generic
13100 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13105 ("component subtype of actual does not match that of formal &",
13107 Abandon_Instantiation (Actual);
13110 if Has_Aliased_Components (A_Gen_T)
13111 and then not Has_Aliased_Components (Act_T)
13114 ("actual must have aliased components to match formal type &",
13117 end Validate_Array_Type_Instance;
13119 -----------------------------------------------
13120 -- Validate_Derived_Interface_Type_Instance --
13121 -----------------------------------------------
13123 procedure Validate_Derived_Interface_Type_Instance is
13124 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13128 -- First apply interface instance checks
13130 Validate_Interface_Type_Instance;
13132 -- Verify that immediate parent interface is an ancestor of
13136 and then not Interface_Present_In_Ancestor (Act_T, Par)
13139 ("interface actual must include progenitor&", Actual, Par);
13142 -- Now verify that the actual includes all other ancestors of
13145 Elmt := First_Elmt (Interfaces (A_Gen_T));
13146 while Present (Elmt) loop
13147 if not Interface_Present_In_Ancestor
13148 (Act_T, Get_Instance_Of (Node (Elmt)))
13151 ("interface actual must include progenitor&",
13152 Actual, Node (Elmt));
13157 end Validate_Derived_Interface_Type_Instance;
13159 ------------------------------------
13160 -- Validate_Derived_Type_Instance --
13161 ------------------------------------
13163 procedure Validate_Derived_Type_Instance is
13164 Actual_Discr : Entity_Id;
13165 Ancestor_Discr : Entity_Id;
13168 -- Verify that the actual includes the progenitors of the formal,
13169 -- if any. The formal may depend on previous formals and their
13170 -- instance, so we must examine instance of interfaces if present.
13171 -- The actual may be an extension of an interface, in which case
13172 -- it does not appear in the interface list, so this must be
13173 -- checked separately.
13175 if Present (Interface_List (Def)) then
13176 if not Has_Interfaces (Act_T) then
13178 ("actual must implement all interfaces of formal&",
13183 Act_Iface_List : Elist_Id;
13185 Iface_Ent : Entity_Id;
13187 function Instance_Exists (I : Entity_Id) return Boolean;
13188 -- If the interface entity is declared in a generic unit,
13189 -- this can only be legal if we are within an instantiation
13190 -- of a child of that generic. There is currently no
13191 -- mechanism to relate an interface declared within a
13192 -- generic to the corresponding interface in an instance,
13193 -- so we traverse the list of interfaces of the actual,
13194 -- looking for a name match.
13196 ---------------------
13197 -- Instance_Exists --
13198 ---------------------
13200 function Instance_Exists (I : Entity_Id) return Boolean is
13201 Iface_Elmt : Elmt_Id;
13204 Iface_Elmt := First_Elmt (Act_Iface_List);
13205 while Present (Iface_Elmt) loop
13206 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13207 and then Chars (Node (Iface_Elmt)) = Chars (I)
13212 Next_Elmt (Iface_Elmt);
13216 end Instance_Exists;
13219 Iface := First (Abstract_Interface_List (A_Gen_T));
13220 Collect_Interfaces (Act_T, Act_Iface_List);
13222 while Present (Iface) loop
13223 Iface_Ent := Get_Instance_Of (Entity (Iface));
13225 if Is_Ancestor (Iface_Ent, Act_T)
13226 or else Is_Progenitor (Iface_Ent, Act_T)
13230 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13231 and then Instance_Exists (Iface_Ent)
13236 Error_Msg_Name_1 := Chars (Act_T);
13238 ("Actual% must implement interface&",
13239 Actual, Etype (Iface));
13248 -- If the parent type in the generic declaration is itself a previous
13249 -- formal type, then it is local to the generic and absent from the
13250 -- analyzed generic definition. In that case the ancestor is the
13251 -- instance of the formal (which must have been instantiated
13252 -- previously), unless the ancestor is itself a formal derived type.
13253 -- In this latter case (which is the subject of Corrigendum 8652/0038
13254 -- (AI-202) the ancestor of the formals is the ancestor of its
13255 -- parent. Otherwise, the analyzed generic carries the parent type.
13256 -- If the parent type is defined in a previous formal package, then
13257 -- the scope of that formal package is that of the generic type
13258 -- itself, and it has already been mapped into the corresponding type
13259 -- in the actual package.
13261 -- Common case: parent type defined outside of the generic
13263 if Is_Entity_Name (Subtype_Mark (Def))
13264 and then Present (Entity (Subtype_Mark (Def)))
13266 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13268 -- Check whether parent is defined in a previous formal package
13271 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13274 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13276 -- The type may be a local derivation, or a type extension of a
13277 -- previous formal, or of a formal of a parent package.
13279 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13281 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13283 -- Check whether the parent is another derived formal type in the
13284 -- same generic unit.
13286 if Etype (A_Gen_T) /= A_Gen_T
13287 and then Is_Generic_Type (Etype (A_Gen_T))
13288 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13289 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13291 -- Locate ancestor of parent from the subtype declaration
13292 -- created for the actual.
13298 Decl := First (Actual_Decls);
13299 while Present (Decl) loop
13300 if Nkind (Decl) = N_Subtype_Declaration
13301 and then Chars (Defining_Identifier (Decl)) =
13302 Chars (Etype (A_Gen_T))
13304 Ancestor := Generic_Parent_Type (Decl);
13312 pragma Assert (Present (Ancestor));
13314 -- The ancestor itself may be a previous formal that has been
13317 Ancestor := Get_Instance_Of (Ancestor);
13321 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13324 -- Check whether parent is a previous formal of the current generic
13326 elsif Is_Derived_Type (A_Gen_T)
13327 and then Is_Generic_Type (Etype (A_Gen_T))
13328 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13330 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13332 -- An unusual case: the actual is a type declared in a parent unit,
13333 -- but is not a formal type so there is no instance_of for it.
13334 -- Retrieve it by analyzing the record extension.
13336 elsif Is_Child_Unit (Scope (A_Gen_T))
13337 and then In_Open_Scopes (Scope (Act_T))
13338 and then Is_Generic_Instance (Scope (Act_T))
13340 Analyze (Subtype_Mark (Def));
13341 Ancestor := Entity (Subtype_Mark (Def));
13344 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13347 -- If the formal derived type has pragma Preelaborable_Initialization
13348 -- then the actual type must have preelaborable initialization.
13350 if Known_To_Have_Preelab_Init (A_Gen_T)
13351 and then not Has_Preelaborable_Initialization (Act_T)
13354 ("actual for & must have preelaborable initialization",
13358 -- Ada 2005 (AI-251)
13360 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13361 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13363 ("(Ada 2005) expected type implementing & in instantiation",
13367 -- Finally verify that the (instance of) the ancestor is an ancestor
13370 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13372 ("expect type derived from & in instantiation",
13373 Actual, First_Subtype (Ancestor));
13374 Abandon_Instantiation (Actual);
13377 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13378 -- that the formal type declaration has been rewritten as a private
13381 if Ada_Version >= Ada_2005
13382 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13383 and then Synchronized_Present (Parent (A_Gen_T))
13385 -- The actual must be a synchronized tagged type
13387 if not Is_Tagged_Type (Act_T) then
13389 ("actual of synchronized type must be tagged", Actual);
13390 Abandon_Instantiation (Actual);
13392 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13393 and then Nkind (Type_Definition (Parent (Act_T))) =
13394 N_Derived_Type_Definition
13395 and then not Synchronized_Present
13396 (Type_Definition (Parent (Act_T)))
13399 ("actual of synchronized type must be synchronized", Actual);
13400 Abandon_Instantiation (Actual);
13404 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13405 -- removes the second instance of the phrase "or allow pass by copy".
13407 -- For Ada 2020, the aspect may be specified explicitly for the
13408 -- formal regardless of whether an ancestor obeys it.
13410 if Is_Atomic (Act_T)
13411 and then not Is_Atomic (Ancestor)
13412 and then not Is_Atomic (A_Gen_T)
13415 ("cannot have atomic actual type for non-atomic formal type",
13418 elsif Is_Volatile (Act_T)
13419 and then not Is_Volatile (Ancestor)
13420 and then not Is_Volatile (A_Gen_T)
13423 ("cannot have volatile actual type for non-volatile formal type",
13427 -- It should not be necessary to check for unknown discriminants on
13428 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13429 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13430 -- needs fixing. ???
13432 if Is_Definite_Subtype (A_Gen_T)
13433 and then not Unknown_Discriminants_Present (Formal)
13434 and then not Is_Definite_Subtype (Act_T)
13436 Error_Msg_N ("actual subtype must be constrained", Actual);
13437 Abandon_Instantiation (Actual);
13440 if not Unknown_Discriminants_Present (Formal) then
13441 if Is_Constrained (Ancestor) then
13442 if not Is_Constrained (Act_T) then
13443 Error_Msg_N ("actual subtype must be constrained", Actual);
13444 Abandon_Instantiation (Actual);
13447 -- Ancestor is unconstrained, Check if generic formal and actual
13448 -- agree on constrainedness. The check only applies to array types
13449 -- and discriminated types.
13451 elsif Is_Constrained (Act_T) then
13452 if Ekind (Ancestor) = E_Access_Type
13453 or else (not Is_Constrained (A_Gen_T)
13454 and then Is_Composite_Type (A_Gen_T))
13456 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13457 Abandon_Instantiation (Actual);
13460 -- A class-wide type is only allowed if the formal has unknown
13463 elsif Is_Class_Wide_Type (Act_T)
13464 and then not Has_Unknown_Discriminants (Ancestor)
13467 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13468 Abandon_Instantiation (Actual);
13470 -- Otherwise, the formal and actual must have the same number
13471 -- of discriminants and each discriminant of the actual must
13472 -- correspond to a discriminant of the formal.
13474 elsif Has_Discriminants (Act_T)
13475 and then not Has_Unknown_Discriminants (Act_T)
13476 and then Has_Discriminants (Ancestor)
13478 Actual_Discr := First_Discriminant (Act_T);
13479 Ancestor_Discr := First_Discriminant (Ancestor);
13480 while Present (Actual_Discr)
13481 and then Present (Ancestor_Discr)
13483 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13484 No (Corresponding_Discriminant (Actual_Discr))
13487 ("discriminant & does not correspond "
13488 & "to ancestor discriminant", Actual, Actual_Discr);
13489 Abandon_Instantiation (Actual);
13492 Next_Discriminant (Actual_Discr);
13493 Next_Discriminant (Ancestor_Discr);
13496 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13498 ("actual for & must have same number of discriminants",
13500 Abandon_Instantiation (Actual);
13503 -- This case should be caught by the earlier check for
13504 -- constrainedness, but the check here is added for completeness.
13506 elsif Has_Discriminants (Act_T)
13507 and then not Has_Unknown_Discriminants (Act_T)
13510 ("actual for & must not have discriminants", Actual, Gen_T);
13511 Abandon_Instantiation (Actual);
13513 elsif Has_Discriminants (Ancestor) then
13515 ("actual for & must have known discriminants", Actual, Gen_T);
13516 Abandon_Instantiation (Actual);
13519 if not Subtypes_Statically_Compatible
13520 (Act_T, Ancestor, Formal_Derived_Matching => True)
13523 ("actual for & must be statically compatible with ancestor",
13526 if not Predicates_Compatible (Act_T, Ancestor) then
13528 ("\predicate on actual is not compatible with ancestor",
13532 Abandon_Instantiation (Actual);
13536 -- If the formal and actual types are abstract, check that there
13537 -- are no abstract primitives of the actual type that correspond to
13538 -- nonabstract primitives of the formal type (second sentence of
13541 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13542 Check_Abstract_Primitives : declare
13543 Gen_Prims : constant Elist_Id :=
13544 Primitive_Operations (A_Gen_T);
13545 Gen_Elmt : Elmt_Id;
13546 Gen_Subp : Entity_Id;
13547 Anc_Subp : Entity_Id;
13548 Anc_Formal : Entity_Id;
13549 Anc_F_Type : Entity_Id;
13551 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13552 Act_Elmt : Elmt_Id;
13553 Act_Subp : Entity_Id;
13554 Act_Formal : Entity_Id;
13555 Act_F_Type : Entity_Id;
13557 Subprograms_Correspond : Boolean;
13559 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13560 -- Returns true if T2 is derived directly or indirectly from
13561 -- T1, including derivations from interfaces. T1 and T2 are
13562 -- required to be specific tagged base types.
13564 ------------------------
13565 -- Is_Tagged_Ancestor --
13566 ------------------------
13568 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13570 Intfc_Elmt : Elmt_Id;
13573 -- The predicate is satisfied if the types are the same
13578 -- If we've reached the top of the derivation chain then
13579 -- we know that T1 is not an ancestor of T2.
13581 elsif Etype (T2) = T2 then
13584 -- Proceed to check T2's immediate parent
13586 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13589 -- Finally, check to see if T1 is an ancestor of any of T2's
13593 Intfc_Elmt := First_Elmt (Interfaces (T2));
13594 while Present (Intfc_Elmt) loop
13595 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13599 Next_Elmt (Intfc_Elmt);
13604 end Is_Tagged_Ancestor;
13606 -- Start of processing for Check_Abstract_Primitives
13609 -- Loop over all of the formal derived type's primitives
13611 Gen_Elmt := First_Elmt (Gen_Prims);
13612 while Present (Gen_Elmt) loop
13613 Gen_Subp := Node (Gen_Elmt);
13615 -- If the primitive of the formal is not abstract, then
13616 -- determine whether there is a corresponding primitive of
13617 -- the actual type that's abstract.
13619 if not Is_Abstract_Subprogram (Gen_Subp) then
13620 Act_Elmt := First_Elmt (Act_Prims);
13621 while Present (Act_Elmt) loop
13622 Act_Subp := Node (Act_Elmt);
13624 -- If we find an abstract primitive of the actual,
13625 -- then we need to test whether it corresponds to the
13626 -- subprogram from which the generic formal primitive
13629 if Is_Abstract_Subprogram (Act_Subp) then
13630 Anc_Subp := Alias (Gen_Subp);
13632 -- Test whether we have a corresponding primitive
13633 -- by comparing names, kinds, formal types, and
13636 if Chars (Anc_Subp) = Chars (Act_Subp)
13637 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13639 Anc_Formal := First_Formal (Anc_Subp);
13640 Act_Formal := First_Formal (Act_Subp);
13641 while Present (Anc_Formal)
13642 and then Present (Act_Formal)
13644 Anc_F_Type := Etype (Anc_Formal);
13645 Act_F_Type := Etype (Act_Formal);
13647 if Ekind (Anc_F_Type) =
13648 E_Anonymous_Access_Type
13650 Anc_F_Type := Designated_Type (Anc_F_Type);
13652 if Ekind (Act_F_Type) =
13653 E_Anonymous_Access_Type
13656 Designated_Type (Act_F_Type);
13662 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13667 Anc_F_Type := Base_Type (Anc_F_Type);
13668 Act_F_Type := Base_Type (Act_F_Type);
13670 -- If the formal is controlling, then the
13671 -- the type of the actual primitive's formal
13672 -- must be derived directly or indirectly
13673 -- from the type of the ancestor primitive's
13676 if Is_Controlling_Formal (Anc_Formal) then
13677 if not Is_Tagged_Ancestor
13678 (Anc_F_Type, Act_F_Type)
13683 -- Otherwise the types of the formals must
13686 elsif Anc_F_Type /= Act_F_Type then
13690 Next_Entity (Anc_Formal);
13691 Next_Entity (Act_Formal);
13694 -- If we traversed through all of the formals
13695 -- then so far the subprograms correspond, so
13696 -- now check that any result types correspond.
13698 if No (Anc_Formal) and then No (Act_Formal) then
13699 Subprograms_Correspond := True;
13701 if Ekind (Act_Subp) = E_Function then
13702 Anc_F_Type := Etype (Anc_Subp);
13703 Act_F_Type := Etype (Act_Subp);
13705 if Ekind (Anc_F_Type) =
13706 E_Anonymous_Access_Type
13709 Designated_Type (Anc_F_Type);
13711 if Ekind (Act_F_Type) =
13712 E_Anonymous_Access_Type
13715 Designated_Type (Act_F_Type);
13717 Subprograms_Correspond := False;
13722 = E_Anonymous_Access_Type
13724 Subprograms_Correspond := False;
13727 Anc_F_Type := Base_Type (Anc_F_Type);
13728 Act_F_Type := Base_Type (Act_F_Type);
13730 -- Now either the result types must be
13731 -- the same or, if the result type is
13732 -- controlling, the result type of the
13733 -- actual primitive must descend from the
13734 -- result type of the ancestor primitive.
13736 if Subprograms_Correspond
13737 and then Anc_F_Type /= Act_F_Type
13739 Has_Controlling_Result (Anc_Subp)
13740 and then not Is_Tagged_Ancestor
13741 (Anc_F_Type, Act_F_Type)
13743 Subprograms_Correspond := False;
13747 -- Found a matching subprogram belonging to
13748 -- formal ancestor type, so actual subprogram
13749 -- corresponds and this violates 3.9.3(9).
13751 if Subprograms_Correspond then
13753 ("abstract subprogram & overrides "
13754 & "nonabstract subprogram of ancestor",
13761 Next_Elmt (Act_Elmt);
13765 Next_Elmt (Gen_Elmt);
13767 end Check_Abstract_Primitives;
13770 -- Verify that limitedness matches. If parent is a limited
13771 -- interface then the generic formal is not unless declared
13772 -- explicitly so. If not declared limited, the actual cannot be
13773 -- limited (see AI05-0087).
13775 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13776 if not In_Instance then
13778 ("actual for non-limited & cannot be a limited type",
13780 Explain_Limited_Type (Act_T, Actual);
13781 Abandon_Instantiation (Actual);
13785 -- Check for AI12-0036
13788 Formal_Is_Private_Extension : constant Boolean :=
13789 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13791 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13794 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13795 if not In_Instance then
13796 if Actual_Is_Tagged then
13798 ("actual for & cannot be a tagged type", Actual, Gen_T);
13801 ("actual for & must be a tagged type", Actual, Gen_T);
13804 Abandon_Instantiation (Actual);
13808 end Validate_Derived_Type_Instance;
13810 ----------------------------------------
13811 -- Validate_Discriminated_Formal_Type --
13812 ----------------------------------------
13814 procedure Validate_Discriminated_Formal_Type is
13815 Formal_Discr : Entity_Id;
13816 Actual_Discr : Entity_Id;
13817 Formal_Subt : Entity_Id;
13820 if Has_Discriminants (A_Gen_T) then
13821 if not Has_Discriminants (Act_T) then
13823 ("actual for & must have discriminants", Actual, Gen_T);
13824 Abandon_Instantiation (Actual);
13826 elsif Is_Constrained (Act_T) then
13828 ("actual for & must be unconstrained", Actual, Gen_T);
13829 Abandon_Instantiation (Actual);
13832 Formal_Discr := First_Discriminant (A_Gen_T);
13833 Actual_Discr := First_Discriminant (Act_T);
13834 while Formal_Discr /= Empty loop
13835 if Actual_Discr = Empty then
13837 ("discriminants on actual do not match formal",
13839 Abandon_Instantiation (Actual);
13842 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13844 -- Access discriminants match if designated types do
13846 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13847 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13848 E_Anonymous_Access_Type
13851 (Designated_Type (Base_Type (Formal_Subt))) =
13852 Designated_Type (Base_Type (Etype (Actual_Discr)))
13856 elsif Base_Type (Formal_Subt) /=
13857 Base_Type (Etype (Actual_Discr))
13860 ("types of actual discriminants must match formal",
13862 Abandon_Instantiation (Actual);
13864 elsif not Subtypes_Statically_Match
13865 (Formal_Subt, Etype (Actual_Discr))
13866 and then Ada_Version >= Ada_95
13869 ("subtypes of actual discriminants must match formal",
13871 Abandon_Instantiation (Actual);
13874 Next_Discriminant (Formal_Discr);
13875 Next_Discriminant (Actual_Discr);
13878 if Actual_Discr /= Empty then
13880 ("discriminants on actual do not match formal",
13882 Abandon_Instantiation (Actual);
13886 end Validate_Discriminated_Formal_Type;
13888 ---------------------------------------
13889 -- Validate_Incomplete_Type_Instance --
13890 ---------------------------------------
13892 procedure Validate_Incomplete_Type_Instance is
13894 if not Is_Tagged_Type (Act_T)
13895 and then Is_Tagged_Type (A_Gen_T)
13898 ("actual for & must be a tagged type", Actual, Gen_T);
13901 Validate_Discriminated_Formal_Type;
13902 end Validate_Incomplete_Type_Instance;
13904 --------------------------------------
13905 -- Validate_Interface_Type_Instance --
13906 --------------------------------------
13908 procedure Validate_Interface_Type_Instance is
13910 if not Is_Interface (Act_T) then
13912 ("actual for formal interface type must be an interface",
13915 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13916 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13917 or else Is_Protected_Interface (A_Gen_T) /=
13918 Is_Protected_Interface (Act_T)
13919 or else Is_Synchronized_Interface (A_Gen_T) /=
13920 Is_Synchronized_Interface (Act_T)
13923 ("actual for interface& does not match (RM 12.5.5(4))",
13926 end Validate_Interface_Type_Instance;
13928 ------------------------------------
13929 -- Validate_Private_Type_Instance --
13930 ------------------------------------
13932 procedure Validate_Private_Type_Instance is
13934 if Is_Limited_Type (Act_T)
13935 and then not Is_Limited_Type (A_Gen_T)
13937 if In_Instance then
13941 ("actual for non-limited & cannot be a limited type", Actual,
13943 Explain_Limited_Type (Act_T, Actual);
13944 Abandon_Instantiation (Actual);
13947 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13948 and then not Has_Preelaborable_Initialization (Act_T)
13951 ("actual for & must have preelaborable initialization", Actual,
13954 elsif not Is_Definite_Subtype (Act_T)
13955 and then Is_Definite_Subtype (A_Gen_T)
13956 and then Ada_Version >= Ada_95
13959 ("actual for & must be a definite subtype", Actual, Gen_T);
13961 elsif not Is_Tagged_Type (Act_T)
13962 and then Is_Tagged_Type (A_Gen_T)
13965 ("actual for & must be a tagged type", Actual, Gen_T);
13968 Validate_Discriminated_Formal_Type;
13970 end Validate_Private_Type_Instance;
13972 -- Start of processing for Instantiate_Type
13975 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13976 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13977 return New_List (Error);
13979 elsif not Is_Entity_Name (Actual)
13980 or else not Is_Type (Entity (Actual))
13983 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13984 Abandon_Instantiation (Actual);
13987 Act_T := Entity (Actual);
13989 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13990 -- as a generic actual parameter if the corresponding formal type
13991 -- does not have a known_discriminant_part, or is a formal derived
13992 -- type that is an Unchecked_Union type.
13994 if Is_Unchecked_Union (Base_Type (Act_T)) then
13995 if not Has_Discriminants (A_Gen_T)
13996 or else (Is_Derived_Type (A_Gen_T)
13997 and then Is_Unchecked_Union (A_Gen_T))
14001 Error_Msg_N ("unchecked union cannot be the actual for a "
14002 & "discriminated formal type", Act_T);
14007 -- Deal with fixed/floating restrictions
14009 if Is_Floating_Point_Type (Act_T) then
14010 Check_Restriction (No_Floating_Point, Actual);
14011 elsif Is_Fixed_Point_Type (Act_T) then
14012 Check_Restriction (No_Fixed_Point, Actual);
14015 -- Deal with error of using incomplete type as generic actual.
14016 -- This includes limited views of a type, even if the non-limited
14017 -- view may be available.
14019 if Ekind (Act_T) = E_Incomplete_Type
14020 or else (Is_Class_Wide_Type (Act_T)
14021 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14023 -- If the formal is an incomplete type, the actual can be
14024 -- incomplete as well.
14026 if Ekind (A_Gen_T) = E_Incomplete_Type then
14029 elsif Is_Class_Wide_Type (Act_T)
14030 or else No (Full_View (Act_T))
14032 Error_Msg_N ("premature use of incomplete type", Actual);
14033 Abandon_Instantiation (Actual);
14035 Act_T := Full_View (Act_T);
14036 Set_Entity (Actual, Act_T);
14038 if Has_Private_Component (Act_T) then
14040 ("premature use of type with private component", Actual);
14044 -- Deal with error of premature use of private type as generic actual
14046 elsif Is_Private_Type (Act_T)
14047 and then Is_Private_Type (Base_Type (Act_T))
14048 and then not Is_Generic_Type (Act_T)
14049 and then not Is_Derived_Type (Act_T)
14050 and then No (Full_View (Root_Type (Act_T)))
14052 -- If the formal is an incomplete type, the actual can be
14053 -- private or incomplete as well.
14055 if Ekind (A_Gen_T) = E_Incomplete_Type then
14058 Error_Msg_N ("premature use of private type", Actual);
14061 elsif Has_Private_Component (Act_T) then
14063 ("premature use of type with private component", Actual);
14066 Set_Instance_Of (A_Gen_T, Act_T);
14068 -- If the type is generic, the class-wide type may also be used
14070 if Is_Tagged_Type (A_Gen_T)
14071 and then Is_Tagged_Type (Act_T)
14072 and then not Is_Class_Wide_Type (A_Gen_T)
14074 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14075 Class_Wide_Type (Act_T));
14078 if not Is_Abstract_Type (A_Gen_T)
14079 and then Is_Abstract_Type (Act_T)
14082 ("actual of non-abstract formal cannot be abstract", Actual);
14085 -- A generic scalar type is a first subtype for which we generate
14086 -- an anonymous base type. Indicate that the instance of this base
14087 -- is the base type of the actual.
14089 if Is_Scalar_Type (A_Gen_T) then
14090 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14094 Check_Shared_Variable_Control_Aspects;
14096 if Error_Posted (Act_T) then
14099 case Nkind (Def) is
14100 when N_Formal_Private_Type_Definition =>
14101 Validate_Private_Type_Instance;
14103 when N_Formal_Incomplete_Type_Definition =>
14104 Validate_Incomplete_Type_Instance;
14106 when N_Formal_Derived_Type_Definition =>
14107 Validate_Derived_Type_Instance;
14109 when N_Formal_Discrete_Type_Definition =>
14110 if not Is_Discrete_Type (Act_T) then
14112 ("expect discrete type in instantiation of&",
14114 Abandon_Instantiation (Actual);
14117 Diagnose_Predicated_Actual;
14119 when N_Formal_Signed_Integer_Type_Definition =>
14120 if not Is_Signed_Integer_Type (Act_T) then
14122 ("expect signed integer type in instantiation of&",
14124 Abandon_Instantiation (Actual);
14127 Diagnose_Predicated_Actual;
14129 when N_Formal_Modular_Type_Definition =>
14130 if not Is_Modular_Integer_Type (Act_T) then
14132 ("expect modular type in instantiation of &",
14134 Abandon_Instantiation (Actual);
14137 Diagnose_Predicated_Actual;
14139 when N_Formal_Floating_Point_Definition =>
14140 if not Is_Floating_Point_Type (Act_T) then
14142 ("expect float type in instantiation of &", Actual, Gen_T);
14143 Abandon_Instantiation (Actual);
14146 when N_Formal_Ordinary_Fixed_Point_Definition =>
14147 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14149 ("expect ordinary fixed point type in instantiation of &",
14151 Abandon_Instantiation (Actual);
14154 when N_Formal_Decimal_Fixed_Point_Definition =>
14155 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14157 ("expect decimal type in instantiation of &",
14159 Abandon_Instantiation (Actual);
14162 when N_Array_Type_Definition =>
14163 Validate_Array_Type_Instance;
14165 when N_Access_To_Object_Definition =>
14166 Validate_Access_Type_Instance;
14168 when N_Access_Function_Definition
14169 | N_Access_Procedure_Definition
14171 Validate_Access_Subprogram_Instance;
14173 when N_Record_Definition =>
14174 Validate_Interface_Type_Instance;
14176 when N_Derived_Type_Definition =>
14177 Validate_Derived_Interface_Type_Instance;
14180 raise Program_Error;
14184 Subt := New_Copy (Gen_T);
14186 -- Use adjusted sloc of subtype name as the location for other nodes in
14187 -- the subtype declaration.
14189 Loc := Sloc (Subt);
14192 Make_Subtype_Declaration (Loc,
14193 Defining_Identifier => Subt,
14194 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14196 -- Record whether the actual is private at this point, so that
14197 -- Check_Generic_Actuals can restore its proper view before the
14198 -- semantic analysis of the instance.
14200 if Is_Private_Type (Act_T) then
14201 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14204 -- In Ada 2012 the actual may be a limited view. Indicate that
14205 -- the local subtype must be treated as such.
14207 if From_Limited_With (Act_T) then
14208 Set_Ekind (Subt, E_Incomplete_Subtype);
14209 Set_From_Limited_With (Subt);
14212 Decl_Nodes := New_List (Decl_Node);
14214 -- Flag actual derived types so their elaboration produces the
14215 -- appropriate renamings for the primitive operations of the ancestor.
14216 -- Flag actual for formal private types as well, to determine whether
14217 -- operations in the private part may override inherited operations.
14218 -- If the formal has an interface list, the ancestor is not the
14219 -- parent, but the analyzed formal that includes the interface
14220 -- operations of all its progenitors.
14222 -- Same treatment for formal private types, so we can check whether the
14223 -- type is tagged limited when validating derivations in the private
14224 -- part. (See AI05-096).
14226 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14227 if Present (Interface_List (Def)) then
14228 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14230 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14233 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14234 | N_Formal_Incomplete_Type_Definition
14236 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14239 -- If the actual is a synchronized type that implements an interface,
14240 -- the primitive operations are attached to the corresponding record,
14241 -- and we have to treat it as an additional generic actual, so that its
14242 -- primitive operations become visible in the instance. The task or
14243 -- protected type itself does not carry primitive operations.
14245 if Is_Concurrent_Type (Act_T)
14246 and then Is_Tagged_Type (Act_T)
14247 and then Present (Corresponding_Record_Type (Act_T))
14248 and then Present (Ancestor)
14249 and then Is_Interface (Ancestor)
14252 Corr_Rec : constant Entity_Id :=
14253 Corresponding_Record_Type (Act_T);
14254 New_Corr : Entity_Id;
14255 Corr_Decl : Node_Id;
14258 New_Corr := Make_Temporary (Loc, 'S');
14260 Make_Subtype_Declaration (Loc,
14261 Defining_Identifier => New_Corr,
14262 Subtype_Indication =>
14263 New_Occurrence_Of (Corr_Rec, Loc));
14264 Append_To (Decl_Nodes, Corr_Decl);
14266 if Ekind (Act_T) = E_Task_Type then
14267 Set_Ekind (Subt, E_Task_Subtype);
14269 Set_Ekind (Subt, E_Protected_Subtype);
14272 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14273 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14274 Set_Generic_Parent_Type (Decl_Node, Empty);
14278 -- For a floating-point type, capture dimension info if any, because
14279 -- the generated subtype declaration does not come from source and
14280 -- will not process dimensions.
14282 if Is_Floating_Point_Type (Act_T) then
14283 Copy_Dimensions (Act_T, Subt);
14287 end Instantiate_Type;
14289 ---------------------
14290 -- Is_In_Main_Unit --
14291 ---------------------
14293 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14294 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14295 Current_Unit : Node_Id;
14298 if Unum = Main_Unit then
14301 -- If the current unit is a subunit then it is either the main unit or
14302 -- is being compiled as part of the main unit.
14304 elsif Nkind (N) = N_Compilation_Unit then
14305 return Nkind (Unit (N)) = N_Subunit;
14308 Current_Unit := Parent (N);
14309 while Present (Current_Unit)
14310 and then Nkind (Current_Unit) /= N_Compilation_Unit
14312 Current_Unit := Parent (Current_Unit);
14315 -- The instantiation node is in the main unit, or else the current node
14316 -- (perhaps as the result of nested instantiations) is in the main unit,
14317 -- or in the declaration of the main unit, which in this last case must
14321 Current_Unit = Cunit (Main_Unit)
14322 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14323 or else (Present (Current_Unit)
14324 and then Present (Library_Unit (Current_Unit))
14325 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14326 end Is_In_Main_Unit;
14328 ----------------------------
14329 -- Load_Parent_Of_Generic --
14330 ----------------------------
14332 procedure Load_Parent_Of_Generic
14335 Body_Optional : Boolean := False)
14337 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14338 Saved_Style_Check : constant Boolean := Style_Check;
14339 Saved_Warnings : constant Warning_Record := Save_Warnings;
14340 True_Parent : Node_Id;
14341 Inst_Node : Node_Id;
14343 Previous_Instances : constant Elist_Id := New_Elmt_List;
14345 procedure Collect_Previous_Instances (Decls : List_Id);
14346 -- Collect all instantiations in the given list of declarations, that
14347 -- precede the generic that we need to load. If the bodies of these
14348 -- instantiations are available, we must analyze them, to ensure that
14349 -- the public symbols generated are the same when the unit is compiled
14350 -- to generate code, and when it is compiled in the context of a unit
14351 -- that needs a particular nested instance. This process is applied to
14352 -- both package and subprogram instances.
14354 --------------------------------
14355 -- Collect_Previous_Instances --
14356 --------------------------------
14358 procedure Collect_Previous_Instances (Decls : List_Id) is
14362 Decl := First (Decls);
14363 while Present (Decl) loop
14364 if Sloc (Decl) >= Sloc (Inst_Node) then
14367 -- If Decl is an instantiation, then record it as requiring
14368 -- instantiation of the corresponding body, except if it is an
14369 -- abbreviated instantiation generated internally for conformance
14370 -- checking purposes only for the case of a formal package
14371 -- declared without a box (see Instantiate_Formal_Package). Such
14372 -- an instantiation does not generate any code (the actual code
14373 -- comes from actual) and thus does not need to be analyzed here.
14374 -- If the instantiation appears with a generic package body it is
14375 -- not analyzed here either.
14377 elsif Nkind (Decl) = N_Package_Instantiation
14378 and then not Is_Internal (Defining_Entity (Decl))
14380 Append_Elmt (Decl, Previous_Instances);
14382 -- For a subprogram instantiation, omit instantiations intrinsic
14383 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14385 elsif Nkind (Decl) in N_Function_Instantiation
14386 | N_Procedure_Instantiation
14387 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14389 Append_Elmt (Decl, Previous_Instances);
14391 elsif Nkind (Decl) = N_Package_Declaration then
14392 Collect_Previous_Instances
14393 (Visible_Declarations (Specification (Decl)));
14394 Collect_Previous_Instances
14395 (Private_Declarations (Specification (Decl)));
14397 -- Previous non-generic bodies may contain instances as well
14399 elsif Nkind (Decl) = N_Package_Body
14400 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14402 Collect_Previous_Instances (Declarations (Decl));
14404 elsif Nkind (Decl) = N_Subprogram_Body
14405 and then not Acts_As_Spec (Decl)
14406 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14408 Collect_Previous_Instances (Declarations (Decl));
14413 end Collect_Previous_Instances;
14415 -- Start of processing for Load_Parent_Of_Generic
14418 if not In_Same_Source_Unit (N, Spec)
14419 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14420 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14421 and then not Is_In_Main_Unit (Spec))
14423 -- Find body of parent of spec, and analyze it. A special case arises
14424 -- when the parent is an instantiation, that is to say when we are
14425 -- currently instantiating a nested generic. In that case, there is
14426 -- no separate file for the body of the enclosing instance. Instead,
14427 -- the enclosing body must be instantiated as if it were a pending
14428 -- instantiation, in order to produce the body for the nested generic
14429 -- we require now. Note that in that case the generic may be defined
14430 -- in a package body, the instance defined in the same package body,
14431 -- and the original enclosing body may not be in the main unit.
14433 Inst_Node := Empty;
14435 True_Parent := Parent (Spec);
14436 while Present (True_Parent)
14437 and then Nkind (True_Parent) /= N_Compilation_Unit
14439 if Nkind (True_Parent) = N_Package_Declaration
14441 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14443 -- Parent is a compilation unit that is an instantiation, and
14444 -- instantiation node has been replaced with package decl.
14446 Inst_Node := Original_Node (True_Parent);
14449 elsif Nkind (True_Parent) = N_Package_Declaration
14450 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14452 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14454 -- Parent is a compilation unit that is an instantiation, but
14455 -- instantiation node has not been replaced with package decl.
14457 Inst_Node := Unit (Parent (True_Parent));
14460 elsif Nkind (True_Parent) = N_Package_Declaration
14461 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14462 and then Present (Generic_Parent (Specification (True_Parent)))
14464 -- Parent is an instantiation within another specification.
14465 -- Declaration for instance has been inserted before original
14466 -- instantiation node. A direct link would be preferable?
14468 Inst_Node := Next (True_Parent);
14469 while Present (Inst_Node)
14470 and then Nkind (Inst_Node) /= N_Package_Instantiation
14475 -- If the instance appears within a generic, and the generic
14476 -- unit is defined within a formal package of the enclosing
14477 -- generic, there is no generic body available, and none
14478 -- needed. A more precise test should be used ???
14480 if No (Inst_Node) then
14486 -- If an ancestor of the generic comes from a formal package
14487 -- there is no source for the ancestor body. This is detected
14488 -- by examining the scope of the ancestor and its declaration.
14489 -- The body, if any is needed, will be available when the
14490 -- current unit (containing a formal package) is instantiated.
14492 elsif Nkind (True_Parent) = N_Package_Specification
14493 and then Present (Generic_Parent (True_Parent))
14495 (Original_Node (Unit_Declaration_Node
14496 (Scope (Generic_Parent (True_Parent)))))
14497 = N_Formal_Package_Declaration
14502 True_Parent := Parent (True_Parent);
14506 -- Case where we are currently instantiating a nested generic
14508 if Present (Inst_Node) then
14509 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14511 -- Instantiation node and declaration of instantiated package
14512 -- were exchanged when only the declaration was needed.
14513 -- Restore instantiation node before proceeding with body.
14515 Set_Unit (Parent (True_Parent), Inst_Node);
14518 -- Now complete instantiation of enclosing body, if it appears in
14519 -- some other unit. If it appears in the current unit, the body
14520 -- will have been instantiated already.
14522 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14524 -- We need to determine the expander mode to instantiate the
14525 -- enclosing body. Because the generic body we need may use
14526 -- global entities declared in the enclosing package (including
14527 -- aggregates) it is in general necessary to compile this body
14528 -- with expansion enabled, except if we are within a generic
14529 -- package, in which case the usual generic rule applies.
14532 Exp_Status : Boolean := True;
14536 -- Loop through scopes looking for generic package
14538 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14539 while Present (Scop)
14540 and then Scop /= Standard_Standard
14542 if Ekind (Scop) = E_Generic_Package then
14543 Exp_Status := False;
14547 Scop := Scope (Scop);
14550 -- Collect previous instantiations in the unit that contains
14551 -- the desired generic.
14553 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14554 and then not Body_Optional
14558 Info : Pending_Body_Info;
14562 Par := Parent (Inst_Node);
14563 while Present (Par) loop
14564 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14565 Par := Parent (Par);
14568 pragma Assert (Present (Par));
14570 if Nkind (Par) = N_Package_Body then
14571 Collect_Previous_Instances (Declarations (Par));
14573 elsif Nkind (Par) = N_Package_Declaration then
14574 Collect_Previous_Instances
14575 (Visible_Declarations (Specification (Par)));
14576 Collect_Previous_Instances
14577 (Private_Declarations (Specification (Par)));
14580 -- Enclosing unit is a subprogram body. In this
14581 -- case all instance bodies are processed in order
14582 -- and there is no need to collect them separately.
14587 Decl := First_Elmt (Previous_Instances);
14588 while Present (Decl) loop
14591 Instance_Spec (Node (Decl)),
14592 Config_Switches => Save_Config_Switches,
14593 Current_Sem_Unit =>
14594 Get_Code_Unit (Sloc (Node (Decl))),
14595 Expander_Status => Exp_Status,
14596 Inst_Node => Node (Decl),
14597 Local_Suppress_Stack_Top =>
14598 Local_Suppress_Stack_Top,
14599 Scope_Suppress => Scope_Suppress,
14600 Warnings => Save_Warnings);
14602 -- Package instance
14604 if Nkind (Node (Decl)) = N_Package_Instantiation
14606 Instantiate_Package_Body
14607 (Info, Body_Optional => True);
14609 -- Subprogram instance
14612 -- The instance_spec is in the wrapper package,
14613 -- usually followed by its local renaming
14614 -- declaration. See Build_Subprogram_Renaming
14615 -- for details. If the instance carries aspects,
14616 -- these result in the corresponding pragmas,
14617 -- inserted after the subprogram declaration.
14618 -- They must be skipped as well when retrieving
14619 -- the desired spec. Some of them may have been
14620 -- rewritten as null statements.
14621 -- A direct link would be more robust ???
14625 (Last (Visible_Declarations
14626 (Specification (Info.Act_Decl))));
14628 while Nkind (Decl) in
14631 N_Subprogram_Renaming_Declaration
14633 Decl := Prev (Decl);
14636 Info.Act_Decl := Decl;
14639 Instantiate_Subprogram_Body
14640 (Info, Body_Optional => True);
14648 Instantiate_Package_Body
14650 ((Act_Decl => True_Parent,
14651 Config_Switches => Save_Config_Switches,
14652 Current_Sem_Unit =>
14653 Get_Code_Unit (Sloc (Inst_Node)),
14654 Expander_Status => Exp_Status,
14655 Inst_Node => Inst_Node,
14656 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14657 Scope_Suppress => Scope_Suppress,
14658 Warnings => Save_Warnings)),
14659 Body_Optional => Body_Optional);
14663 -- Case where we are not instantiating a nested generic
14666 Opt.Style_Check := False;
14667 Expander_Mode_Save_And_Set (True);
14668 Load_Needed_Body (Comp_Unit, OK);
14669 Opt.Style_Check := Saved_Style_Check;
14670 Restore_Warnings (Saved_Warnings);
14671 Expander_Mode_Restore;
14674 and then Unit_Requires_Body (Defining_Entity (Spec))
14675 and then not Body_Optional
14678 Bname : constant Unit_Name_Type :=
14679 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14682 -- In CodePeer mode, the missing body may make the analysis
14683 -- incomplete, but we do not treat it as fatal.
14685 if CodePeer_Mode then
14689 Error_Msg_Unit_1 := Bname;
14690 Error_Msg_N ("this instantiation requires$!", N);
14691 Error_Msg_File_1 :=
14692 Get_File_Name (Bname, Subunit => False);
14693 Error_Msg_N ("\but file{ was not found!", N);
14694 raise Unrecoverable_Error;
14701 -- If loading parent of the generic caused an instantiation circularity,
14702 -- we abandon compilation at this point, because otherwise in some cases
14703 -- we get into trouble with infinite recursions after this point.
14705 if Circularity_Detected then
14706 raise Unrecoverable_Error;
14708 end Load_Parent_Of_Generic;
14710 ---------------------------------
14711 -- Map_Formal_Package_Entities --
14712 ---------------------------------
14714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14719 Set_Instance_Of (Form, Act);
14721 -- Traverse formal and actual package to map the corresponding entities.
14722 -- We skip over internal entities that may be generated during semantic
14723 -- analysis, and find the matching entities by name, given that they
14724 -- must appear in the same order.
14726 E1 := First_Entity (Form);
14727 E2 := First_Entity (Act);
14728 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14729 -- Could this test be a single condition??? Seems like it could, and
14730 -- isn't FPE (Form) a constant anyway???
14732 if not Is_Internal (E1)
14733 and then Present (Parent (E1))
14734 and then not Is_Class_Wide_Type (E1)
14735 and then not Is_Internal_Name (Chars (E1))
14737 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14744 Set_Instance_Of (E1, E2);
14746 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14747 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14750 if Is_Constrained (E1) then
14751 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14754 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14755 Map_Formal_Package_Entities (E1, E2);
14762 end Map_Formal_Package_Entities;
14764 -----------------------
14765 -- Move_Freeze_Nodes --
14766 -----------------------
14768 procedure Move_Freeze_Nodes
14769 (Out_Of : Entity_Id;
14774 Next_Decl : Node_Id;
14775 Next_Node : Node_Id := After;
14778 function Is_Outer_Type (T : Entity_Id) return Boolean;
14779 -- Check whether entity is declared in a scope external to that of the
14782 -------------------
14783 -- Is_Outer_Type --
14784 -------------------
14786 function Is_Outer_Type (T : Entity_Id) return Boolean is
14787 Scop : Entity_Id := Scope (T);
14790 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14794 while Scop /= Standard_Standard loop
14795 if Scop = Out_Of then
14798 Scop := Scope (Scop);
14806 -- Start of processing for Move_Freeze_Nodes
14813 -- First remove the freeze nodes that may appear before all other
14817 while Present (Decl)
14818 and then Nkind (Decl) = N_Freeze_Entity
14819 and then Is_Outer_Type (Entity (Decl))
14821 Decl := Remove_Head (L);
14822 Insert_After (Next_Node, Decl);
14823 Set_Analyzed (Decl, False);
14828 -- Next scan the list of declarations and remove each freeze node that
14829 -- appears ahead of the current node.
14831 while Present (Decl) loop
14832 while Present (Next (Decl))
14833 and then Nkind (Next (Decl)) = N_Freeze_Entity
14834 and then Is_Outer_Type (Entity (Next (Decl)))
14836 Next_Decl := Remove_Next (Decl);
14837 Insert_After (Next_Node, Next_Decl);
14838 Set_Analyzed (Next_Decl, False);
14839 Next_Node := Next_Decl;
14842 -- If the declaration is a nested package or concurrent type, then
14843 -- recurse. Nested generic packages will have been processed from the
14846 case Nkind (Decl) is
14847 when N_Package_Declaration =>
14848 Spec := Specification (Decl);
14850 when N_Task_Type_Declaration =>
14851 Spec := Task_Definition (Decl);
14853 when N_Protected_Type_Declaration =>
14854 Spec := Protected_Definition (Decl);
14860 if Present (Spec) then
14861 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14862 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14867 end Move_Freeze_Nodes;
14873 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14875 return Generic_Renamings.Table (E).Next_In_HTable;
14878 ------------------------
14879 -- Preanalyze_Actuals --
14880 ------------------------
14882 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14883 procedure Perform_Appropriate_Analysis (N : Node_Id);
14884 -- Determine if the actuals we are analyzing come from a generic
14885 -- instantiation that is a library unit and dispatch accordingly.
14887 ----------------------------------
14888 -- Perform_Appropriate_Analysis --
14889 ----------------------------------
14891 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14893 -- When we have a library instantiation we cannot allow any expansion
14894 -- to occur, since there may be no place to put it. Instead, in that
14895 -- case we perform a preanalysis of the actual.
14897 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14902 end Perform_Appropriate_Analysis;
14906 Errs : constant Nat := Serious_Errors_Detected;
14911 Cur : Entity_Id := Empty;
14912 -- Current homograph of the instance name
14914 Vis : Boolean := False;
14915 -- Saved visibility status of the current homograph
14917 -- Start of processing for Preanalyze_Actuals
14920 Assoc := First (Generic_Associations (N));
14922 -- If the instance is a child unit, its name may hide an outer homonym,
14923 -- so make it invisible to perform name resolution on the actuals.
14925 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14927 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14929 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14931 if Is_Compilation_Unit (Cur) then
14932 Vis := Is_Immediately_Visible (Cur);
14933 Set_Is_Immediately_Visible (Cur, False);
14939 while Present (Assoc) loop
14940 if Nkind (Assoc) /= N_Others_Choice then
14941 Act := Explicit_Generic_Actual_Parameter (Assoc);
14943 -- Within a nested instantiation, a defaulted actual is an empty
14944 -- association, so nothing to analyze. If the subprogram actual
14945 -- is an attribute, analyze prefix only, because actual is not a
14946 -- complete attribute reference.
14948 -- If actual is an allocator, analyze expression only. The full
14949 -- analysis can generate code, and if instance is a compilation
14950 -- unit we have to wait until the package instance is installed
14951 -- to have a proper place to insert this code.
14953 -- String literals may be operators, but at this point we do not
14954 -- know whether the actual is a formal subprogram or a string.
14959 elsif Nkind (Act) = N_Attribute_Reference then
14960 Perform_Appropriate_Analysis (Prefix (Act));
14962 elsif Nkind (Act) = N_Explicit_Dereference then
14963 Perform_Appropriate_Analysis (Prefix (Act));
14965 elsif Nkind (Act) = N_Allocator then
14967 Expr : constant Node_Id := Expression (Act);
14970 if Nkind (Expr) = N_Subtype_Indication then
14971 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14973 -- Analyze separately each discriminant constraint, when
14974 -- given with a named association.
14980 Constr := First (Constraints (Constraint (Expr)));
14981 while Present (Constr) loop
14982 if Nkind (Constr) = N_Discriminant_Association then
14983 Perform_Appropriate_Analysis
14984 (Expression (Constr));
14986 Perform_Appropriate_Analysis (Constr);
14994 Perform_Appropriate_Analysis (Expr);
14998 elsif Nkind (Act) /= N_Operator_Symbol then
14999 Perform_Appropriate_Analysis (Act);
15001 -- Within a package instance, mark actuals that are limited
15002 -- views, so their use can be moved to the body of the
15005 if Is_Entity_Name (Act)
15006 and then Is_Type (Entity (Act))
15007 and then From_Limited_With (Entity (Act))
15008 and then Present (Inst)
15010 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15014 if Errs /= Serious_Errors_Detected then
15016 -- Do a minimal analysis of the generic, to prevent spurious
15017 -- warnings complaining about the generic being unreferenced,
15018 -- before abandoning the instantiation.
15020 Perform_Appropriate_Analysis (Name (N));
15022 if Is_Entity_Name (Name (N))
15023 and then Etype (Name (N)) /= Any_Type
15025 Generate_Reference (Entity (Name (N)), Name (N));
15026 Set_Is_Instantiated (Entity (Name (N)));
15029 if Present (Cur) then
15031 -- For the case of a child instance hiding an outer homonym,
15032 -- provide additional warning which might explain the error.
15034 Set_Is_Immediately_Visible (Cur, Vis);
15036 ("& hides outer unit with the same name??",
15037 N, Defining_Unit_Name (N));
15040 Abandon_Instantiation (Act);
15047 if Present (Cur) then
15048 Set_Is_Immediately_Visible (Cur, Vis);
15050 end Preanalyze_Actuals;
15052 -------------------------------
15053 -- Provide_Completing_Bodies --
15054 -------------------------------
15056 procedure Provide_Completing_Bodies (N : Node_Id) is
15057 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15058 -- Generate the completing body for subprogram declaration Subp_Decl
15060 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15061 -- Generating completing bodies for all subprograms found in declarative
15064 ---------------------------
15065 -- Build_Completing_Body --
15066 ---------------------------
15068 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15069 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15070 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15074 -- Nothing to do if the subprogram already has a completing body
15076 if Present (Corresponding_Body (Subp_Decl)) then
15079 -- Mark the function as having a valid return statement even though
15080 -- the body contains a single raise statement.
15082 elsif Ekind (Subp_Id) = E_Function then
15083 Set_Return_Present (Subp_Id);
15086 -- Clone the specification to obtain new entities and reset the only
15089 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15090 Set_Generic_Parent (Spec, Empty);
15093 -- function Func ... return ... is
15095 -- procedure Proc ... is
15097 -- raise Program_Error with "access before elaboration";
15100 Insert_After_And_Analyze (Subp_Decl,
15101 Make_Subprogram_Body (Loc,
15102 Specification => Spec,
15103 Declarations => New_List,
15104 Handled_Statement_Sequence =>
15105 Make_Handled_Sequence_Of_Statements (Loc,
15106 Statements => New_List (
15107 Make_Raise_Program_Error (Loc,
15108 Reason => PE_Access_Before_Elaboration)))));
15109 end Build_Completing_Body;
15111 ----------------------------------
15112 -- Provide_Completing_Bodies_In --
15113 ----------------------------------
15115 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15119 if Present (Decls) then
15120 Decl := First (Decls);
15121 while Present (Decl) loop
15122 Provide_Completing_Bodies (Decl);
15126 end Provide_Completing_Bodies_In;
15132 -- Start of processing for Provide_Completing_Bodies
15135 if Nkind (N) = N_Package_Declaration then
15136 Spec := Specification (N);
15138 Push_Scope (Defining_Entity (N));
15139 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15140 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15143 elsif Nkind (N) = N_Subprogram_Declaration then
15144 Build_Completing_Body (N);
15146 end Provide_Completing_Bodies;
15148 -------------------
15149 -- Remove_Parent --
15150 -------------------
15152 procedure Remove_Parent (In_Body : Boolean := False) is
15153 S : Entity_Id := Current_Scope;
15154 -- S is the scope containing the instantiation just completed. The scope
15155 -- stack contains the parent instances of the instantiation, followed by
15164 -- After child instantiation is complete, remove from scope stack the
15165 -- extra copy of the current scope, and then remove parent instances.
15167 if not In_Body then
15170 while Current_Scope /= S loop
15171 P := Current_Scope;
15172 End_Package_Scope (Current_Scope);
15174 if In_Open_Scopes (P) then
15175 E := First_Entity (P);
15176 while Present (E) loop
15177 Set_Is_Immediately_Visible (E, True);
15181 -- If instantiation is declared in a block, it is the enclosing
15182 -- scope that might be a parent instance. Note that only one
15183 -- block can be involved, because the parent instances have
15184 -- been installed within it.
15186 if Ekind (P) = E_Block then
15187 Cur_P := Scope (P);
15192 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15193 -- We are within an instance of some sibling. Retain
15194 -- visibility of parent, for proper subsequent cleanup, and
15195 -- reinstall private declarations as well.
15197 Set_In_Private_Part (P);
15198 Install_Private_Declarations (P);
15201 -- If the ultimate parent is a top-level unit recorded in
15202 -- Instance_Parent_Unit, then reset its visibility to what it was
15203 -- before instantiation. (It's not clear what the purpose is of
15204 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15205 -- present before the ultimate parent test was added.???)
15207 elsif not In_Open_Scopes (Scope (P))
15208 or else (P = Instance_Parent_Unit
15209 and then not Parent_Unit_Visible)
15211 Set_Is_Immediately_Visible (P, False);
15213 -- If the current scope is itself an instantiation of a generic
15214 -- nested within P, and we are in the private part of body of this
15215 -- instantiation, restore the full views of P, that were removed
15216 -- in End_Package_Scope above. This obscure case can occur when a
15217 -- subunit of a generic contains an instance of a child unit of
15218 -- its generic parent unit.
15220 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15222 Par : constant Entity_Id :=
15223 Generic_Parent (Package_Specification (S));
15226 and then P = Scope (Par)
15227 and then (In_Package_Body (S) or else In_Private_Part (S))
15229 Set_In_Private_Part (P);
15230 Install_Private_Declarations (P);
15236 -- Reset visibility of entities in the enclosing scope
15238 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15240 Hidden := First_Elmt (Hidden_Entities);
15241 while Present (Hidden) loop
15242 Set_Is_Immediately_Visible (Node (Hidden), True);
15243 Next_Elmt (Hidden);
15247 -- Each body is analyzed separately, and there is no context that
15248 -- needs preserving from one body instance to the next, so remove all
15249 -- parent scopes that have been installed.
15251 while Present (S) loop
15252 End_Package_Scope (S);
15253 Set_Is_Immediately_Visible (S, False);
15254 S := Current_Scope;
15255 exit when S = Standard_Standard;
15264 procedure Restore_Env is
15265 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15268 if No (Current_Instantiated_Parent.Act_Id) then
15269 -- Restore environment after subprogram inlining
15271 Restore_Private_Views (Empty);
15274 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15275 Exchanged_Views := Saved.Exchanged_Views;
15276 Hidden_Entities := Saved.Hidden_Entities;
15277 Current_Sem_Unit := Saved.Current_Sem_Unit;
15278 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15279 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15281 Restore_Config_Switches (Saved.Switches);
15283 Instance_Envs.Decrement_Last;
15286 ---------------------------
15287 -- Restore_Private_Views --
15288 ---------------------------
15290 procedure Restore_Private_Views
15291 (Pack_Id : Entity_Id;
15292 Is_Package : Boolean := True)
15297 Dep_Elmt : Elmt_Id;
15300 procedure Restore_Nested_Formal (Formal : Entity_Id);
15301 -- Hide the generic formals of formal packages declared with box which
15302 -- were reachable in the current instantiation.
15304 ---------------------------
15305 -- Restore_Nested_Formal --
15306 ---------------------------
15308 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15312 if Present (Renamed_Object (Formal))
15313 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15317 elsif Present (Associated_Formal_Package (Formal)) then
15318 Ent := First_Entity (Formal);
15319 while Present (Ent) loop
15320 exit when Ekind (Ent) = E_Package
15321 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15323 Set_Is_Hidden (Ent);
15324 Set_Is_Potentially_Use_Visible (Ent, False);
15326 -- If package, then recurse
15328 if Ekind (Ent) = E_Package then
15329 Restore_Nested_Formal (Ent);
15335 end Restore_Nested_Formal;
15337 -- Start of processing for Restore_Private_Views
15340 M := First_Elmt (Exchanged_Views);
15341 while Present (M) loop
15344 -- Subtypes of types whose views have been exchanged, and that are
15345 -- defined within the instance, were not on the Private_Dependents
15346 -- list on entry to the instance, so they have to be exchanged
15347 -- explicitly now, in order to remain consistent with the view of the
15350 if Ekind (Typ) in E_Private_Type
15351 | E_Limited_Private_Type
15352 | E_Record_Type_With_Private
15354 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15355 while Present (Dep_Elmt) loop
15356 Dep_Typ := Node (Dep_Elmt);
15358 if Scope (Dep_Typ) = Pack_Id
15359 and then Present (Full_View (Dep_Typ))
15361 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15362 Exchange_Declarations (Dep_Typ);
15365 Next_Elmt (Dep_Elmt);
15369 Exchange_Declarations (Node (M));
15373 if No (Pack_Id) then
15377 -- Make the generic formal parameters private, and make the formal types
15378 -- into subtypes of the actuals again.
15380 E := First_Entity (Pack_Id);
15381 while Present (E) loop
15382 Set_Is_Hidden (E, True);
15385 and then Nkind (Parent (E)) = N_Subtype_Declaration
15387 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15388 -- as it is needed to identify the subtype with the type it
15389 -- renames, when there are conversions between access types
15392 if GNATprove_Mode then
15395 -- If the actual for E is itself a generic actual type from
15396 -- an enclosing instance, E is still a generic actual type
15397 -- outside of the current instance. This matter when resolving
15398 -- an overloaded call that may be ambiguous in the enclosing
15399 -- instance, when two of its actuals coincide.
15401 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15402 and then Is_Generic_Actual_Type
15403 (Entity (Subtype_Indication (Parent (E))))
15407 Set_Is_Generic_Actual_Type (E, False);
15409 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15410 -- flag also on the Full_View if the type is private, since it
15411 -- was set also on this Full_View. However, this flag is relied
15412 -- upon by Covers to spot "types exported from instantiations"
15413 -- which are implicit Full_Views built for instantiations made
15414 -- on private types and we get type mismatches if we do it when
15415 -- the block exchanging the declarations below triggers ???
15417 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15418 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15422 -- An unusual case of aliasing: the actual may also be directly
15423 -- visible in the generic, and be private there, while it is fully
15424 -- visible in the context of the instance. The internal subtype
15425 -- is private in the instance but has full visibility like its
15426 -- parent in the enclosing scope. This enforces the invariant that
15427 -- the privacy status of all private dependents of a type coincide
15428 -- with that of the parent type. This can only happen when a
15429 -- generic child unit is instantiated within a sibling.
15431 if Is_Private_Type (E)
15432 and then not Is_Private_Type (Etype (E))
15434 Exchange_Declarations (E);
15437 elsif Ekind (E) = E_Package then
15439 -- The end of the renaming list is the renaming of the generic
15440 -- package itself. If the instance is a subprogram, all entities
15441 -- in the corresponding package are renamings. If this entity is
15442 -- a formal package, make its own formals private as well. The
15443 -- actual in this case is itself the renaming of an instantiation.
15444 -- If the entity is not a package renaming, it is the entity
15445 -- created to validate formal package actuals: ignore it.
15447 -- If the actual is itself a formal package for the enclosing
15448 -- generic, or the actual for such a formal package, it remains
15449 -- visible on exit from the instance, and therefore nothing needs
15450 -- to be done either, except to keep it accessible.
15452 if Is_Package and then Renamed_Object (E) = Pack_Id then
15455 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15459 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15461 Set_Is_Hidden (E, False);
15465 Act_P : constant Entity_Id := Renamed_Object (E);
15469 Id := First_Entity (Act_P);
15471 and then Id /= First_Private_Entity (Act_P)
15473 exit when Ekind (Id) = E_Package
15474 and then Renamed_Object (Id) = Act_P;
15476 Set_Is_Hidden (Id, True);
15477 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15479 if Ekind (Id) = E_Package then
15480 Restore_Nested_Formal (Id);
15491 end Restore_Private_Views;
15498 (Gen_Unit : Entity_Id;
15499 Act_Unit : Entity_Id)
15503 Set_Instance_Env (Gen_Unit, Act_Unit);
15506 ----------------------------
15507 -- Save_Global_References --
15508 ----------------------------
15510 procedure Save_Global_References (Templ : Node_Id) is
15512 -- ??? it is horrible to use global variables in highly recursive code
15515 -- The entity of the current associated node
15517 Gen_Scope : Entity_Id;
15518 -- The scope of the generic for which references are being saved
15521 -- The current associated node
15523 function Is_Global (E : Entity_Id) return Boolean;
15524 -- Check whether entity is defined outside of generic unit. Examine the
15525 -- scope of an entity, and the scope of the scope, etc, until we find
15526 -- either Standard, in which case the entity is global, or the generic
15527 -- unit itself, which indicates that the entity is local. If the entity
15528 -- is the generic unit itself, as in the case of a recursive call, or
15529 -- the enclosing generic unit, if different from the current scope, then
15530 -- it is local as well, because it will be replaced at the point of
15531 -- instantiation. On the other hand, if it is a reference to a child
15532 -- unit of a common ancestor, which appears in an instantiation, it is
15533 -- global because it is used to denote a specific compilation unit at
15534 -- the time the instantiations will be analyzed.
15536 procedure Qualify_Universal_Operands
15538 Func_Call : Node_Id);
15539 -- Op denotes a binary or unary operator in generic template Templ. Node
15540 -- Func_Call is the function call alternative of the operator within the
15541 -- the analyzed copy of the template. Change each operand which yields a
15542 -- universal type by wrapping it into a qualified expression
15544 -- Actual_Typ'(Operand)
15546 -- where Actual_Typ is the type of corresponding actual parameter of
15547 -- Operand in Func_Call.
15549 procedure Reset_Entity (N : Node_Id);
15550 -- Save semantic information on global entity so that it is not resolved
15551 -- again at instantiation time.
15553 procedure Save_Entity_Descendants (N : Node_Id);
15554 -- Apply Save_Global_References to the two syntactic descendants of
15555 -- non-terminal nodes that carry an Associated_Node and are processed
15556 -- through Reset_Entity. Once the global entity (if any) has been
15557 -- captured together with its type, only two syntactic descendants need
15558 -- to be traversed to complete the processing of the tree rooted at N.
15559 -- This applies to Selected_Components, Expanded_Names, and to Operator
15560 -- nodes. N can also be a character literal, identifier, or operator
15561 -- symbol node, but the call has no effect in these cases.
15563 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15564 -- Default actuals in nested instances must be handled specially
15565 -- because there is no link to them from the original tree. When an
15566 -- actual subprogram is given by a default, we add an explicit generic
15567 -- association for it in the instantiation node. When we save the
15568 -- global references on the name of the instance, we recover the list
15569 -- of generic associations, and add an explicit one to the original
15570 -- generic tree, through which a global actual can be preserved.
15571 -- Similarly, if a child unit is instantiated within a sibling, in the
15572 -- context of the parent, we must preserve the identifier of the parent
15573 -- so that it can be properly resolved in a subsequent instantiation.
15575 procedure Save_Global_Descendant (D : Union_Id);
15576 -- Apply Save_References recursively to the descendants of node D
15578 procedure Save_References (N : Node_Id);
15579 -- This is the recursive procedure that does the work, once the
15580 -- enclosing generic scope has been established.
15586 function Is_Global (E : Entity_Id) return Boolean is
15589 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15590 -- Determine whether the parent node of a reference to a child unit
15591 -- denotes an instantiation or a formal package, in which case the
15592 -- reference to the child unit is global, even if it appears within
15593 -- the current scope (e.g. when the instance appears within the body
15594 -- of an ancestor).
15596 ----------------------
15597 -- Is_Instance_Node --
15598 ----------------------
15600 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15602 return Nkind (Decl) in N_Generic_Instantiation
15604 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15605 end Is_Instance_Node;
15607 -- Start of processing for Is_Global
15610 if E = Gen_Scope then
15613 elsif E = Standard_Standard then
15616 elsif Is_Child_Unit (E)
15617 and then (Is_Instance_Node (Parent (N2))
15618 or else (Nkind (Parent (N2)) = N_Expanded_Name
15619 and then N2 = Selector_Name (Parent (N2))
15621 Is_Instance_Node (Parent (Parent (N2)))))
15627 while Se /= Gen_Scope loop
15628 if Se = Standard_Standard then
15639 --------------------------------
15640 -- Qualify_Universal_Operands --
15641 --------------------------------
15643 procedure Qualify_Universal_Operands
15645 Func_Call : Node_Id)
15647 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15648 -- Rewrite operand Opnd as a qualified expression of the form
15650 -- Actual_Typ'(Opnd)
15652 -- where Actual is the corresponding actual parameter of Opnd in
15653 -- function call Func_Call.
15655 function Qualify_Type
15657 Typ : Entity_Id) return Node_Id;
15658 -- Qualify type Typ by creating a selected component of the form
15660 -- Scope_Of_Typ.Typ
15662 ---------------------
15663 -- Qualify_Operand --
15664 ---------------------
15666 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15667 Loc : constant Source_Ptr := Sloc (Opnd);
15668 Typ : constant Entity_Id := Etype (Actual);
15673 -- Qualify the operand when it is of a universal type. Note that
15674 -- the template is unanalyzed and it is not possible to directly
15675 -- query the type. This transformation is not done when the type
15676 -- of the actual is internally generated because the type will be
15677 -- regenerated in the instance.
15679 if Yields_Universal_Type (Opnd)
15680 and then Comes_From_Source (Typ)
15681 and then not Is_Hidden (Typ)
15683 -- The type of the actual may be a global reference. Save this
15684 -- information by creating a reference to it.
15686 if Is_Global (Typ) then
15687 Mark := New_Occurrence_Of (Typ, Loc);
15689 -- Otherwise rely on resolution to find the proper type within
15693 Mark := Qualify_Type (Loc, Typ);
15697 Make_Qualified_Expression (Loc,
15698 Subtype_Mark => Mark,
15699 Expression => Relocate_Node (Opnd));
15701 -- Mark the qualification to distinguish it from other source
15702 -- constructs and signal the instantiation mechanism that this
15703 -- node requires special processing. See Copy_Generic_Node for
15706 Set_Is_Qualified_Universal_Literal (Qual);
15708 Rewrite (Opnd, Qual);
15710 end Qualify_Operand;
15716 function Qualify_Type
15718 Typ : Entity_Id) return Node_Id
15720 Scop : constant Entity_Id := Scope (Typ);
15724 Result := Make_Identifier (Loc, Chars (Typ));
15726 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15728 Make_Selected_Component (Loc,
15729 Prefix => Make_Identifier (Loc, Chars (Scop)),
15730 Selector_Name => Result);
15738 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15740 -- Start of processing for Qualify_Universal_Operands
15743 if Nkind (Op) in N_Binary_Op then
15744 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15745 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15747 elsif Nkind (Op) in N_Unary_Op then
15748 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15750 end Qualify_Universal_Operands;
15756 procedure Reset_Entity (N : Node_Id) is
15757 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15758 -- If the type of N2 is global to the generic unit, save the type in
15759 -- the generic node. Just as we perform name capture for explicit
15760 -- references within the generic, we must capture the global types
15761 -- of local entities because they may participate in resolution in
15764 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15765 -- Find the ultimate ancestor of the current unit. If it is not a
15766 -- generic unit, then the name of the current unit in the prefix of
15767 -- an expanded name must be replaced with its generic homonym to
15768 -- ensure that it will be properly resolved in an instance.
15770 ---------------------
15771 -- Set_Global_Type --
15772 ---------------------
15774 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15775 Typ : constant Entity_Id := Etype (N2);
15778 Set_Etype (N, Typ);
15780 -- If the entity of N is not the associated node, this is a
15781 -- nested generic and it has an associated node as well, whose
15782 -- type is already the full view (see below). Indicate that the
15783 -- original node has a private view.
15785 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15786 Set_Has_Private_View (N);
15789 -- If not a private type, nothing else to do
15791 if not Is_Private_Type (Typ) then
15794 -- If it is a derivation of a private type in a context where no
15795 -- full view is needed, nothing to do either.
15797 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15800 -- Otherwise mark the type for flipping and use the full view when
15804 Set_Has_Private_View (N);
15806 if Present (Full_View (Typ)) then
15807 Set_Etype (N2, Full_View (Typ));
15811 if Is_Floating_Point_Type (Typ)
15812 and then Has_Dimension_System (Typ)
15814 Copy_Dimensions (N2, N);
15816 end Set_Global_Type;
15822 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15827 while Is_Child_Unit (Par) loop
15828 Par := Scope (Par);
15834 -- Start of processing for Reset_Entity
15837 N2 := Get_Associated_Node (N);
15840 if Present (E) then
15842 -- If the node is an entry call to an entry in an enclosing task,
15843 -- it is rewritten as a selected component. No global entity to
15844 -- preserve in this case, since the expansion will be redone in
15847 if Nkind (E) not in N_Entity then
15848 Set_Associated_Node (N, Empty);
15849 Set_Etype (N, Empty);
15853 -- If the entity is an itype created as a subtype of an access
15854 -- type with a null exclusion restore source entity for proper
15855 -- visibility. The itype will be created anew in the instance.
15858 and then Ekind (E) = E_Access_Subtype
15859 and then Is_Entity_Name (N)
15860 and then Chars (Etype (E)) = Chars (N)
15863 Set_Entity (N2, E);
15867 if Is_Global (E) then
15868 Set_Global_Type (N, N2);
15870 elsif Nkind (N) = N_Op_Concat
15871 and then Is_Generic_Type (Etype (N2))
15872 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15874 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15875 and then Is_Intrinsic_Subprogram (E)
15879 -- Entity is local. Mark generic node as unresolved. Note that now
15880 -- it does not have an entity.
15883 Set_Associated_Node (N, Empty);
15884 Set_Etype (N, Empty);
15887 if Nkind (Parent (N)) in N_Generic_Instantiation
15888 and then N = Name (Parent (N))
15890 Save_Global_Defaults (Parent (N), Parent (N2));
15893 elsif Nkind (Parent (N)) = N_Selected_Component
15894 and then Nkind (Parent (N2)) = N_Expanded_Name
15896 -- In case of previous errors, the tree might be malformed
15898 if No (Entity (Parent (N2))) then
15901 elsif Is_Global (Entity (Parent (N2))) then
15902 Change_Selected_Component_To_Expanded_Name (Parent (N));
15903 Set_Associated_Node (Parent (N), Parent (N2));
15904 Set_Global_Type (Parent (N), Parent (N2));
15905 Save_Entity_Descendants (N);
15907 -- If this is a reference to the current generic entity, replace
15908 -- by the name of the generic homonym of the current package. This
15909 -- is because in an instantiation Par.P.Q will not resolve to the
15910 -- name of the instance, whose enclosing scope is not necessarily
15911 -- Par. We use the generic homonym rather that the name of the
15912 -- generic itself because it may be hidden by a local declaration.
15914 elsif In_Open_Scopes (Entity (Parent (N2)))
15916 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15918 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15919 Rewrite (Parent (N),
15920 Make_Identifier (Sloc (N),
15922 Chars (Generic_Homonym (Entity (Parent (N2))))));
15924 Rewrite (Parent (N),
15925 Make_Identifier (Sloc (N),
15926 Chars => Chars (Selector_Name (Parent (N2)))));
15930 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15931 and then Parent (N) = Name (Parent (Parent (N)))
15933 Save_Global_Defaults
15934 (Parent (Parent (N)), Parent (Parent (N2)));
15937 -- A selected component may denote a static constant that has been
15938 -- folded. If the static constant is global to the generic, capture
15939 -- its value. Otherwise the folding will happen in any instantiation.
15941 elsif Nkind (Parent (N)) = N_Selected_Component
15942 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15944 if Present (Entity (Original_Node (Parent (N2))))
15945 and then Is_Global (Entity (Original_Node (Parent (N2))))
15947 Rewrite (Parent (N), New_Copy (Parent (N2)));
15948 Set_Analyzed (Parent (N), False);
15951 -- A selected component may be transformed into a parameterless
15952 -- function call. If the called entity is global, rewrite the node
15953 -- appropriately, i.e. as an extended name for the global entity.
15955 elsif Nkind (Parent (N)) = N_Selected_Component
15956 and then Nkind (Parent (N2)) = N_Function_Call
15957 and then N = Selector_Name (Parent (N))
15959 if No (Parameter_Associations (Parent (N2))) then
15960 if Is_Global (Entity (Name (Parent (N2)))) then
15961 Change_Selected_Component_To_Expanded_Name (Parent (N));
15962 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15963 Set_Global_Type (Parent (N), Name (Parent (N2)));
15964 Save_Entity_Descendants (N);
15967 Set_Is_Prefixed_Call (Parent (N));
15968 Set_Associated_Node (N, Empty);
15969 Set_Etype (N, Empty);
15972 -- In Ada 2005, X.F may be a call to a primitive operation,
15973 -- rewritten as F (X). This rewriting will be done again in an
15974 -- instance, so keep the original node. Global entities will be
15975 -- captured as for other constructs. Indicate that this must
15976 -- resolve as a call, to prevent accidental overloading in the
15977 -- instance, if both a component and a primitive operation appear
15981 Set_Is_Prefixed_Call (Parent (N));
15984 -- Entity is local. Reset in generic unit, so that node is resolved
15985 -- anew at the point of instantiation.
15988 Set_Associated_Node (N, Empty);
15989 Set_Etype (N, Empty);
15993 -----------------------------
15994 -- Save_Entity_Descendants --
15995 -----------------------------
15997 procedure Save_Entity_Descendants (N : Node_Id) is
16000 when N_Binary_Op =>
16001 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16002 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16005 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16007 when N_Expanded_Name
16008 | N_Selected_Component
16010 Save_Global_Descendant (Union_Id (Prefix (N)));
16011 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16013 when N_Character_Literal
16015 | N_Operator_Symbol
16020 raise Program_Error;
16022 end Save_Entity_Descendants;
16024 --------------------------
16025 -- Save_Global_Defaults --
16026 --------------------------
16028 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16029 Loc : constant Source_Ptr := Sloc (N1);
16030 Assoc2 : constant List_Id := Generic_Associations (N2);
16031 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16038 Actual : Entity_Id;
16041 Assoc1 := Generic_Associations (N1);
16043 if Present (Assoc1) then
16044 Act1 := First (Assoc1);
16047 Set_Generic_Associations (N1, New_List);
16048 Assoc1 := Generic_Associations (N1);
16051 if Present (Assoc2) then
16052 Act2 := First (Assoc2);
16057 while Present (Act1) and then Present (Act2) loop
16062 -- Find the associations added for default subprograms
16064 if Present (Act2) then
16065 while Nkind (Act2) /= N_Generic_Association
16066 or else No (Entity (Selector_Name (Act2)))
16067 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16072 -- Add a similar association if the default is global. The
16073 -- renaming declaration for the actual has been analyzed, and
16074 -- its alias is the program it renames. Link the actual in the
16075 -- original generic tree with the node in the analyzed tree.
16077 while Present (Act2) loop
16078 Subp := Entity (Selector_Name (Act2));
16079 Def := Explicit_Generic_Actual_Parameter (Act2);
16081 -- Following test is defence against rubbish errors
16083 if No (Alias (Subp)) then
16087 -- Retrieve the resolved actual from the renaming declaration
16088 -- created for the instantiated formal.
16090 Actual := Entity (Name (Parent (Parent (Subp))));
16091 Set_Entity (Def, Actual);
16092 Set_Etype (Def, Etype (Actual));
16094 if Is_Global (Actual) then
16096 Make_Generic_Association (Loc,
16098 New_Occurrence_Of (Subp, Loc),
16099 Explicit_Generic_Actual_Parameter =>
16100 New_Occurrence_Of (Actual, Loc));
16102 Set_Associated_Node
16103 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16105 Append (Ndec, Assoc1);
16107 -- If there are other defaults, add a dummy association in case
16108 -- there are other defaulted formals with the same name.
16110 elsif Present (Next (Act2)) then
16112 Make_Generic_Association (Loc,
16114 New_Occurrence_Of (Subp, Loc),
16115 Explicit_Generic_Actual_Parameter => Empty);
16117 Append (Ndec, Assoc1);
16124 if Nkind (Name (N1)) = N_Identifier
16125 and then Is_Child_Unit (Gen_Id)
16126 and then Is_Global (Gen_Id)
16127 and then Is_Generic_Unit (Scope (Gen_Id))
16128 and then In_Open_Scopes (Scope (Gen_Id))
16130 -- This is an instantiation of a child unit within a sibling, so
16131 -- that the generic parent is in scope. An eventual instance must
16132 -- occur within the scope of an instance of the parent. Make name
16133 -- in instance into an expanded name, to preserve the identifier
16134 -- of the parent, so it can be resolved subsequently.
16136 Rewrite (Name (N2),
16137 Make_Expanded_Name (Loc,
16138 Chars => Chars (Gen_Id),
16139 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16140 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16141 Set_Entity (Name (N2), Gen_Id);
16143 Rewrite (Name (N1),
16144 Make_Expanded_Name (Loc,
16145 Chars => Chars (Gen_Id),
16146 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16147 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16149 Set_Associated_Node (Name (N1), Name (N2));
16150 Set_Associated_Node (Prefix (Name (N1)), Empty);
16151 Set_Associated_Node
16152 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16153 Set_Etype (Name (N1), Etype (Gen_Id));
16155 end Save_Global_Defaults;
16157 ----------------------------
16158 -- Save_Global_Descendant --
16159 ----------------------------
16161 procedure Save_Global_Descendant (D : Union_Id) is
16165 if D in Node_Range then
16166 if D = Union_Id (Empty) then
16169 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16170 Save_References (Node_Id (D));
16173 elsif D in List_Range then
16174 pragma Assert (D /= Union_Id (No_List));
16175 -- Because No_List = Empty, which is in Node_Range above
16177 if Is_Empty_List (List_Id (D)) then
16181 N1 := First (List_Id (D));
16182 while Present (N1) loop
16183 Save_References (N1);
16188 -- Element list or other non-node field, nothing to do
16193 end Save_Global_Descendant;
16195 ---------------------
16196 -- Save_References --
16197 ---------------------
16199 -- This is the recursive procedure that does the work once the enclosing
16200 -- generic scope has been established. We have to treat specially a
16201 -- number of node rewritings that are required by semantic processing
16202 -- and which change the kind of nodes in the generic copy: typically
16203 -- constant-folding, replacing an operator node by a string literal, or
16204 -- a selected component by an expanded name. In each of those cases, the
16205 -- transformation is propagated to the generic unit.
16207 procedure Save_References (N : Node_Id) is
16208 Loc : constant Source_Ptr := Sloc (N);
16210 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16211 -- Determine whether arbitrary node Nod requires delayed capture of
16212 -- global references within its aspect specifications.
16214 procedure Save_References_In_Aggregate (N : Node_Id);
16215 -- Save all global references in [extension] aggregate node N
16217 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16218 -- Save all global references in a character literal or operator
16219 -- symbol denoted by N.
16221 procedure Save_References_In_Descendants (N : Node_Id);
16222 -- Save all global references in all descendants of node N
16224 procedure Save_References_In_Identifier (N : Node_Id);
16225 -- Save all global references in identifier node N
16227 procedure Save_References_In_Operator (N : Node_Id);
16228 -- Save all global references in operator node N
16230 procedure Save_References_In_Pragma (Prag : Node_Id);
16231 -- Save all global references found within the expression of pragma
16234 ---------------------------
16235 -- Requires_Delayed_Save --
16236 ---------------------------
16238 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16240 -- Generic packages and subprograms require delayed capture of
16241 -- global references within their aspects due to the timing of
16242 -- annotation analysis.
16244 if Nkind (Nod) in N_Generic_Package_Declaration
16245 | N_Generic_Subprogram_Declaration
16247 | N_Package_Body_Stub
16248 | N_Subprogram_Body
16249 | N_Subprogram_Body_Stub
16251 -- Since the capture of global references is done on the
16252 -- unanalyzed generic template, there is no information around
16253 -- to infer the context. Use the Associated_Entity linkages to
16254 -- peek into the analyzed generic copy and determine what the
16255 -- template corresponds to.
16257 if Nod = Templ then
16259 Is_Generic_Declaration_Or_Body
16260 (Unit_Declaration_Node
16261 (Associated_Entity (Defining_Entity (Nod))));
16263 -- Otherwise the generic unit being processed is not the top
16264 -- level template. It is safe to capture of global references
16265 -- within the generic unit because at this point the top level
16266 -- copy is fully analyzed.
16272 -- Otherwise capture the global references without interference
16277 end Requires_Delayed_Save;
16279 ----------------------------------
16280 -- Save_References_In_Aggregate --
16281 ----------------------------------
16283 procedure Save_References_In_Aggregate (N : Node_Id) is
16285 Qual : Node_Id := Empty;
16286 Typ : Entity_Id := Empty;
16288 use Atree.Unchecked_Access;
16289 -- This code section is part of implementing an untyped tree
16290 -- traversal, so it needs direct access to node fields.
16293 N2 := Get_Associated_Node (N);
16295 if Present (N2) then
16298 -- In an instance within a generic, use the name of the actual
16299 -- and not the original generic parameter. If the actual is
16300 -- global in the current generic it must be preserved for its
16303 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16304 and then Present (Generic_Parent_Type (Parent (Typ)))
16306 Typ := Base_Type (Typ);
16307 Set_Etype (N2, Typ);
16311 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16312 Set_Associated_Node (N, Empty);
16314 -- If the aggregate is an actual in a call, it has been
16315 -- resolved in the current context, to some local type. The
16316 -- enclosing call may have been disambiguated by the aggregate,
16317 -- and this disambiguation might fail at instantiation time
16318 -- because the type to which the aggregate did resolve is not
16319 -- preserved. In order to preserve some of this information,
16320 -- wrap the aggregate in a qualified expression, using the id
16321 -- of its type. For further disambiguation we qualify the type
16322 -- name with its scope (if visible and not hidden by a local
16323 -- homograph) because both id's will have corresponding
16324 -- entities in an instance. This resolves most of the problems
16325 -- with missing type information on aggregates in instances.
16328 and then Nkind (N2) = Nkind (N)
16329 and then Nkind (Parent (N2)) in N_Subprogram_Call
16330 and then Present (Typ)
16331 and then Comes_From_Source (Typ)
16333 Nam := Make_Identifier (Loc, Chars (Typ));
16335 if Is_Immediately_Visible (Scope (Typ))
16337 (not In_Open_Scopes (Scope (Typ))
16338 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16341 Make_Selected_Component (Loc,
16343 Make_Identifier (Loc, Chars (Scope (Typ))),
16344 Selector_Name => Nam);
16348 Make_Qualified_Expression (Loc,
16349 Subtype_Mark => Nam,
16350 Expression => Relocate_Node (N));
16354 Save_Global_Descendant (Field1 (N));
16355 Save_Global_Descendant (Field2 (N));
16356 Save_Global_Descendant (Field3 (N));
16357 Save_Global_Descendant (Field5 (N));
16359 if Present (Qual) then
16362 end Save_References_In_Aggregate;
16364 ----------------------------------------------
16365 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16366 ----------------------------------------------
16368 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16370 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16373 elsif Nkind (N) = N_Operator_Symbol
16374 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16376 Change_Operator_Symbol_To_String_Literal (N);
16378 end Save_References_In_Char_Lit_Or_Op_Symbol;
16380 ------------------------------------
16381 -- Save_References_In_Descendants --
16382 ------------------------------------
16384 procedure Save_References_In_Descendants (N : Node_Id) is
16385 use Atree.Unchecked_Access;
16386 -- This code section is part of implementing an untyped tree
16387 -- traversal, so it needs direct access to node fields.
16390 Save_Global_Descendant (Field1 (N));
16391 Save_Global_Descendant (Field2 (N));
16392 Save_Global_Descendant (Field3 (N));
16393 Save_Global_Descendant (Field4 (N));
16394 Save_Global_Descendant (Field5 (N));
16395 end Save_References_In_Descendants;
16397 -----------------------------------
16398 -- Save_References_In_Identifier --
16399 -----------------------------------
16401 procedure Save_References_In_Identifier (N : Node_Id) is
16403 -- The node did not undergo a transformation
16405 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16406 -- If this is a discriminant reference, always save it.
16407 -- It is used in the instance to find the corresponding
16408 -- discriminant positionally rather than by name.
16410 Set_Original_Discriminant
16411 (N, Original_Discriminant (Get_Associated_Node (N)));
16415 -- The analysis of the generic copy transformed the identifier
16416 -- into another construct. Propagate the changes to the template.
16419 N2 := Get_Associated_Node (N);
16421 -- The identifier denotes a call to a parameterless function.
16422 -- Mark the node as resolved when the function is external.
16424 if Nkind (N2) = N_Function_Call then
16425 E := Entity (Name (N2));
16427 if Present (E) and then Is_Global (E) then
16428 Set_Etype (N, Etype (N2));
16430 Set_Associated_Node (N, Empty);
16431 Set_Etype (N, Empty);
16434 -- The identifier denotes a named number that was constant
16435 -- folded. Preserve the original name for ASIS and undo the
16436 -- constant folding which will be repeated in the instance.
16437 -- Is this still needed???
16439 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16440 and then Is_Entity_Name (Original_Node (N2))
16442 Set_Associated_Node (N, Original_Node (N2));
16445 -- The identifier resolved to a string literal. Propagate this
16446 -- information to the generic template.
16448 elsif Nkind (N2) = N_String_Literal then
16449 Rewrite (N, New_Copy (N2));
16451 -- The identifier is rewritten as a dereference if it is the
16452 -- prefix of an implicit dereference. Preserve the original
16453 -- tree as the analysis of the instance will expand the node
16454 -- again, but preserve the resolved entity if it is global.
16456 elsif Nkind (N2) = N_Explicit_Dereference then
16457 if Is_Entity_Name (Prefix (N2))
16458 and then Present (Entity (Prefix (N2)))
16459 and then Is_Global (Entity (Prefix (N2)))
16461 Set_Associated_Node (N, Prefix (N2));
16463 elsif Nkind (Prefix (N2)) = N_Function_Call
16464 and then Present (Entity (Name (Prefix (N2))))
16465 and then Is_Global (Entity (Name (Prefix (N2))))
16468 Make_Explicit_Dereference (Loc,
16470 Make_Function_Call (Loc,
16473 (Entity (Name (Prefix (N2))), Loc))));
16476 Set_Associated_Node (N, Empty);
16477 Set_Etype (N, Empty);
16480 -- The subtype mark of a nominally unconstrained object is
16481 -- rewritten as a subtype indication using the bounds of the
16482 -- expression. Recover the original subtype mark.
16484 elsif Nkind (N2) = N_Subtype_Indication
16485 and then Is_Entity_Name (Original_Node (N2))
16487 Set_Associated_Node (N, Original_Node (N2));
16491 end Save_References_In_Identifier;
16493 ---------------------------------
16494 -- Save_References_In_Operator --
16495 ---------------------------------
16497 procedure Save_References_In_Operator (N : Node_Id) is
16499 -- The node did not undergo a transformation
16501 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16502 if Nkind (N) = N_Op_Concat then
16503 Set_Is_Component_Left_Opnd (N,
16504 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16506 Set_Is_Component_Right_Opnd (N,
16507 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16512 -- The analysis of the generic copy transformed the operator into
16513 -- some other construct. Propagate the changes to the template if
16517 N2 := Get_Associated_Node (N);
16519 -- The operator resoved to a function call
16521 if Nkind (N2) = N_Function_Call then
16523 -- Add explicit qualifications in the generic template for
16524 -- all operands of universal type. This aids resolution by
16525 -- preserving the actual type of a literal or an attribute
16526 -- that yields a universal result.
16528 Qualify_Universal_Operands (N, N2);
16530 E := Entity (Name (N2));
16532 if Present (E) and then Is_Global (E) then
16533 Set_Etype (N, Etype (N2));
16535 Set_Associated_Node (N, Empty);
16536 Set_Etype (N, Empty);
16539 -- The operator was folded into a literal
16541 elsif Nkind (N2) in N_Integer_Literal
16545 if Present (Original_Node (N2))
16546 and then Nkind (Original_Node (N2)) = Nkind (N)
16548 -- Operation was constant-folded. Whenever possible,
16549 -- recover semantic information from unfolded node.
16550 -- This was initially done for ASIS but is apparently
16551 -- needed also for e.g. compiling a-nbnbin.adb.
16553 Set_Associated_Node (N, Original_Node (N2));
16555 if Nkind (N) = N_Op_Concat then
16556 Set_Is_Component_Left_Opnd (N,
16557 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16558 Set_Is_Component_Right_Opnd (N,
16559 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16564 -- Propagate the constant folding back to the template
16567 Rewrite (N, New_Copy (N2));
16568 Set_Analyzed (N, False);
16571 -- The operator was folded into an enumeration literal. Retain
16572 -- the entity to avoid spurious ambiguities if it is overloaded
16573 -- at the point of instantiation or inlining.
16575 elsif Nkind (N2) = N_Identifier
16576 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16578 Rewrite (N, New_Copy (N2));
16579 Set_Analyzed (N, False);
16583 -- Complete the operands check if node has not been constant
16586 if Nkind (N) in N_Op then
16587 Save_Entity_Descendants (N);
16589 end Save_References_In_Operator;
16591 -------------------------------
16592 -- Save_References_In_Pragma --
16593 -------------------------------
16595 procedure Save_References_In_Pragma (Prag : Node_Id) is
16597 Do_Save : Boolean := True;
16599 use Atree.Unchecked_Access;
16600 -- This code section is part of implementing an untyped tree
16601 -- traversal, so it needs direct access to node fields.
16604 -- Do not save global references in pragmas generated from aspects
16605 -- because the pragmas will be regenerated at instantiation time.
16607 if From_Aspect_Specification (Prag) then
16610 -- The capture of global references within contract-related source
16611 -- pragmas associated with generic packages, subprograms or their
16612 -- respective bodies must be delayed due to timing of annotation
16613 -- analysis. Global references are still captured in routine
16614 -- Save_Global_References_In_Contract.
16616 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16617 if Is_Package_Contract_Annotation (Prag) then
16618 Context := Find_Related_Package_Or_Body (Prag);
16620 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16621 Context := Find_Related_Declaration_Or_Body (Prag);
16624 -- The use of Original_Node accounts for the case when the
16625 -- related context is generic template.
16627 if Requires_Delayed_Save (Original_Node (Context)) then
16632 -- For all other cases, save all global references within the
16633 -- descendants, but skip the following semantic fields:
16635 -- Field1 - Next_Pragma
16636 -- Field3 - Corresponding_Aspect
16637 -- Field5 - Next_Rep_Item
16640 Save_Global_Descendant (Field2 (Prag));
16641 Save_Global_Descendant (Field4 (Prag));
16643 end Save_References_In_Pragma;
16645 -- Start of processing for Save_References
16653 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16654 Save_References_In_Aggregate (N);
16656 -- Character literals, operator symbols
16658 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16659 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16661 -- Defining identifiers
16663 elsif Nkind (N) in N_Entity then
16668 elsif Nkind (N) = N_Identifier then
16669 Save_References_In_Identifier (N);
16673 elsif Nkind (N) in N_Op then
16674 Save_References_In_Operator (N);
16678 elsif Nkind (N) = N_Pragma then
16679 Save_References_In_Pragma (N);
16682 Save_References_In_Descendants (N);
16685 -- Save all global references found within the aspect specifications
16686 -- of the related node.
16688 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16690 -- The capture of global references within aspects associated with
16691 -- generic packages, subprograms or their bodies must be delayed
16692 -- due to timing of annotation analysis. Global references are
16693 -- still captured in routine Save_Global_References_In_Contract.
16695 if Requires_Delayed_Save (N) then
16698 -- Otherwise save all global references within the aspects
16701 Save_Global_References_In_Aspects (N);
16704 end Save_References;
16706 -- Start of processing for Save_Global_References
16709 Gen_Scope := Current_Scope;
16711 -- If the generic unit is a child unit, references to entities in the
16712 -- parent are treated as local, because they will be resolved anew in
16713 -- the context of the instance of the parent.
16715 while Is_Child_Unit (Gen_Scope)
16716 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16718 Gen_Scope := Scope (Gen_Scope);
16721 Save_References (Templ);
16722 end Save_Global_References;
16724 ---------------------------------------
16725 -- Save_Global_References_In_Aspects --
16726 ---------------------------------------
16728 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16733 Asp := First (Aspect_Specifications (N));
16734 while Present (Asp) loop
16735 Expr := Expression (Asp);
16737 if Present (Expr) then
16738 Save_Global_References (Expr);
16743 end Save_Global_References_In_Aspects;
16745 ------------------------------------------
16746 -- Set_Copied_Sloc_For_Inherited_Pragma --
16747 ------------------------------------------
16749 procedure Set_Copied_Sloc_For_Inherited_Pragma
16754 Create_Instantiation_Source (N, E,
16755 Inlined_Body => False,
16756 Inherited_Pragma => True,
16757 Factor => S_Adjustment);
16758 end Set_Copied_Sloc_For_Inherited_Pragma;
16760 --------------------------------------
16761 -- Set_Copied_Sloc_For_Inlined_Body --
16762 --------------------------------------
16764 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16766 Create_Instantiation_Source (N, E,
16767 Inlined_Body => True,
16768 Inherited_Pragma => False,
16769 Factor => S_Adjustment);
16770 end Set_Copied_Sloc_For_Inlined_Body;
16772 ---------------------
16773 -- Set_Instance_Of --
16774 ---------------------
16776 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16778 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16779 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16780 Generic_Renamings.Increment_Last;
16781 end Set_Instance_Of;
16783 --------------------
16784 -- Set_Next_Assoc --
16785 --------------------
16787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16789 Generic_Renamings.Table (E).Next_In_HTable := Next;
16790 end Set_Next_Assoc;
16792 -------------------
16793 -- Start_Generic --
16794 -------------------
16796 procedure Start_Generic is
16798 -- ??? More things could be factored out in this routine.
16799 -- Should probably be done at a later stage.
16801 Generic_Flags.Append (Inside_A_Generic);
16802 Inside_A_Generic := True;
16804 Expander_Mode_Save_And_Set (False);
16807 ----------------------
16808 -- Set_Instance_Env --
16809 ----------------------
16811 -- WARNING: This routine manages SPARK regions
16813 procedure Set_Instance_Env
16814 (Gen_Unit : Entity_Id;
16815 Act_Unit : Entity_Id)
16817 Saved_AE : constant Boolean := Assertions_Enabled;
16818 Saved_CPL : constant Node_Id := Check_Policy_List;
16819 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16820 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16821 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16824 -- Regardless of the current mode, predefined units are analyzed in the
16825 -- most current Ada mode, and earlier version Ada checks do not apply
16826 -- to predefined units. Nothing needs to be done for non-internal units.
16827 -- These are always analyzed in the current mode.
16829 if In_Internal_Unit (Gen_Unit) then
16831 -- The following call resets all configuration attributes to default
16832 -- or the xxx_Config versions of the attributes when the current sem
16833 -- unit is the main unit. At the same time, internal units must also
16834 -- inherit certain configuration attributes from their context. It
16835 -- is unclear what these two sets are.
16837 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16839 -- Reinstall relevant configuration attributes of the context
16841 Assertions_Enabled := Saved_AE;
16842 Check_Policy_List := Saved_CPL;
16843 Dynamic_Elaboration_Checks := Saved_DEC;
16845 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16848 Current_Instantiated_Parent :=
16849 (Gen_Id => Gen_Unit,
16850 Act_Id => Act_Unit,
16851 Next_In_HTable => Assoc_Null);
16852 end Set_Instance_Env;
16858 procedure Switch_View (T : Entity_Id) is
16859 BT : constant Entity_Id := Base_Type (T);
16860 Priv_Elmt : Elmt_Id := No_Elmt;
16861 Priv_Sub : Entity_Id;
16864 -- T may be private but its base type may have been exchanged through
16865 -- some other occurrence, in which case there is nothing to switch
16866 -- besides T itself. Note that a private dependent subtype of a private
16867 -- type might not have been switched even if the base type has been,
16868 -- because of the last branch of Check_Private_View (see comment there).
16870 if not Is_Private_Type (BT) then
16871 Prepend_Elmt (Full_View (T), Exchanged_Views);
16872 Exchange_Declarations (T);
16876 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16878 if Present (Full_View (BT)) then
16879 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16880 Exchange_Declarations (BT);
16883 while Present (Priv_Elmt) loop
16884 Priv_Sub := Node (Priv_Elmt);
16886 if Present (Full_View (Priv_Sub)) then
16887 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16888 Exchange_Declarations (Priv_Sub);
16891 Next_Elmt (Priv_Elmt);
16899 function True_Parent (N : Node_Id) return Node_Id is
16901 if Nkind (Parent (N)) = N_Subunit then
16902 return Parent (Corresponding_Stub (Parent (N)));
16908 -----------------------------
16909 -- Valid_Default_Attribute --
16910 -----------------------------
16912 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16913 Attr_Id : constant Attribute_Id :=
16914 Get_Attribute_Id (Attribute_Name (Def));
16915 T : constant Entity_Id := Entity (Prefix (Def));
16916 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16922 if No (T) or else T = Any_Id then
16927 F := First_Formal (Nam);
16928 while Present (F) loop
16929 Num_F := Num_F + 1;
16934 when Attribute_Adjacent
16935 | Attribute_Ceiling
16936 | Attribute_Copy_Sign
16938 | Attribute_Fraction
16939 | Attribute_Machine
16941 | Attribute_Remainder
16942 | Attribute_Rounding
16943 | Attribute_Unbiased_Rounding
16947 and then Is_Floating_Point_Type (T);
16949 when Attribute_Image
16953 | Attribute_Wide_Image
16954 | Attribute_Wide_Value
16956 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16961 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16963 when Attribute_Input =>
16964 OK := (Is_Fun and then Num_F = 1);
16966 when Attribute_Output
16967 | Attribute_Put_Image
16971 OK := not Is_Fun and then Num_F = 2;
16979 ("attribute reference has wrong profile for subprogram", Def);
16981 end Valid_Default_Attribute;