1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
87 package body Sem_Ch12 is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
110 -- | |==============>| |
111 -- |___________| global |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
172 -- type Semi_Global is ... -- global to inner.
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
263 -- package subprogram [body]
264 -- Abstract_State Contract_Cases
265 -- Initial_Condition Depends
266 -- Initializes Extensions_Visible
269 -- Refined_State Post_Class
277 -- Subprogram_Variant
280 -- Most package contract annotations utilize forward references to classify
281 -- data declared within the package [body]. Subprogram annotations then use
282 -- the classifications to further refine them. These inter dependencies are
283 -- problematic with respect to the implementation of generics because their
284 -- analysis, capture of global references and instantiation does not mesh
285 -- well with the existing mechanism.
287 -- 1) Analysis of generic contracts is carried out the same way non-generic
288 -- contracts are analyzed:
290 -- 1.1) General rule - a contract is analyzed after all related aspects
291 -- and pragmas are analyzed. This is done by routines
293 -- Analyze_Package_Body_Contract
294 -- Analyze_Package_Contract
295 -- Analyze_Subprogram_Body_Contract
296 -- Analyze_Subprogram_Contract
298 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
301 -- 1.3) Compilation unit body - the contract is analyzed at the end of
302 -- the body declaration list.
304 -- 1.4) Package - the contract is analyzed at the end of the private or
305 -- visible declarations, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
308 -- 1.5) Package body - the contract is analyzed at the end of the body
309 -- declaration list, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
312 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
313 -- package or a subprogram, then its contract is analyzed at the end of
314 -- the enclosing declarations, otherwise the subprogram is a compilation
317 -- 1.7) Subprogram body - if the subprogram body is declared inside a
318 -- block, a package body or a subprogram body, then its contract is
319 -- analyzed at the end of the enclosing declarations, otherwise the
320 -- subprogram is a compilation unit 1.3).
322 -- 2) Capture of global references within contracts is done after capturing
323 -- global references within the generic template. There are two reasons for
324 -- this delay - pragma annotations are not part of the generic template in
325 -- the case of a generic subprogram declaration, and analysis of contracts
328 -- Contract-related source pragmas within generic templates are prepared
329 -- for delayed capture of global references by routine
331 -- Create_Generic_Contract
333 -- The routine associates these pragmas with the contract of the template.
334 -- In the case of a generic subprogram declaration, the routine creates
335 -- generic templates for the pragmas declared after the subprogram because
336 -- they are not part of the template.
338 -- generic -- template starts
339 -- procedure Gen_Proc (Input : Integer); -- template ends
340 -- pragma Precondition (Input > 0); -- requires own template
342 -- 2.1) The capture of global references with aspect specifications and
343 -- source pragmas that apply to a generic unit must be suppressed when
344 -- the generic template is being processed because the contracts have not
345 -- been analyzed yet. Any attempts to capture global references at that
346 -- point will destroy the Associated_Node linkages and leave the template
347 -- undecorated. This delay is controlled by routine
349 -- Requires_Delayed_Save
351 -- 2.2) The real capture of global references within a contract is done
352 -- after the contract has been analyzed, by routine
354 -- Save_Global_References_In_Contract
356 -- 3) The instantiation of a generic contract occurs as part of the
357 -- instantiation of the contract owner. Generic subprogram declarations
358 -- require additional processing when the contract is specified by pragmas
359 -- because the pragmas are not part of the generic template. This is done
362 -- Instantiate_Subprogram_Contract
364 --------------------------------------------------
365 -- Formal packages and partial parameterization --
366 --------------------------------------------------
368 -- When compiling a generic, a formal package is a local instantiation. If
369 -- declared with a box, its generic formals are visible in the enclosing
370 -- generic. If declared with a partial list of actuals, those actuals that
371 -- are defaulted (covered by an Others clause, or given an explicit box
372 -- initialization) are also visible in the enclosing generic, while those
373 -- that have a corresponding actual are not.
375 -- In our source model of instantiation, the same visibility must be
376 -- present in the spec and body of an instance: the names of the formals
377 -- that are defaulted must be made visible within the instance, and made
378 -- invisible (hidden) after the instantiation is complete, so that they
379 -- are not accessible outside of the instance.
381 -- In a generic, a formal package is treated like a special instantiation.
382 -- Our Ada 95 compiler handled formals with and without box in different
383 -- ways. With partial parameterization, we use a single model for both.
384 -- We create a package declaration that consists of the specification of
385 -- the generic package, and a set of declarations that map the actuals
386 -- into local renamings, just as we do for bona fide instantiations. For
387 -- defaulted parameters and formals with a box, we copy directly the
388 -- declarations of the formals into this local package. The result is a
389 -- package whose visible declarations may include generic formals. This
390 -- package is only used for type checking and visibility analysis, and
391 -- never reaches the back end, so it can freely violate the placement
392 -- rules for generic formal declarations.
394 -- The list of declarations (renamings and copies of formals) is built
395 -- by Analyze_Associations, just as for regular instantiations.
397 -- At the point of instantiation, conformance checking must be applied only
398 -- to those parameters that were specified in the formals. We perform this
399 -- checking by creating another internal instantiation, this one including
400 -- only the renamings and the formals (the rest of the package spec is not
401 -- relevant to conformance checking). We can then traverse two lists: the
402 -- list of actuals in the instance that corresponds to the formal package,
403 -- and the list of actuals produced for this bogus instantiation. We apply
404 -- the conformance rules to those actuals that are not defaulted, i.e.
405 -- which still appear as generic formals.
407 -- When we compile an instance body we must make the right parameters
408 -- visible again. The predicate Is_Generic_Formal indicates which of the
409 -- formals should have its Is_Hidden flag reset.
411 -----------------------
412 -- Local subprograms --
413 -----------------------
415 procedure Abandon_Instantiation (N : Node_Id);
416 pragma No_Return (Abandon_Instantiation);
417 -- Posts an error message "instantiation abandoned" at the indicated node
418 -- and then raises the exception Instantiation_Error to do it.
420 procedure Analyze_Formal_Array_Type
421 (T : in out Entity_Id;
423 -- A formal array type is treated like an array type declaration, and
424 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
425 -- in-out, because in the case of an anonymous type the entity is
426 -- actually created in the procedure.
428 -- The following procedures treat other kinds of formal parameters
430 procedure Analyze_Formal_Derived_Interface_Type
435 procedure Analyze_Formal_Derived_Type
440 procedure Analyze_Formal_Interface_Type
445 -- The following subprograms create abbreviated declarations for formal
446 -- scalar types. We introduce an anonymous base of the proper class for
447 -- each of them, and define the formals as constrained first subtypes of
448 -- their bases. The bounds are expressions that are non-static in the
451 procedure Analyze_Formal_Decimal_Fixed_Point_Type
452 (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
454 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
458 (T : Entity_Id; Def : Node_Id);
460 procedure Analyze_Formal_Private_Type
464 -- Creates a new private type, which does not require completion
466 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
467 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
469 procedure Analyze_Generic_Formal_Part (N : Node_Id);
470 -- Analyze generic formal part
472 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
473 -- Create a new access type with the given designated type
475 function Analyze_Associations
478 F_Copy : List_Id) return List_Id;
479 -- At instantiation time, build the list of associations between formals
480 -- and actuals. Each association becomes a renaming declaration for the
481 -- formal entity. F_Copy is the analyzed list of formals in the generic
482 -- copy. It is used to apply legality checks to the actuals. I_Node is the
483 -- instantiation node itself.
485 procedure Analyze_Subprogram_Instantiation
489 procedure Build_Instance_Compilation_Unit_Nodes
493 -- This procedure is used in the case where the generic instance of a
494 -- subprogram body or package body is a library unit. In this case, the
495 -- original library unit node for the generic instantiation must be
496 -- replaced by the resulting generic body, and a link made to a new
497 -- compilation unit node for the generic declaration. The argument N is
498 -- the original generic instantiation. Act_Body and Act_Decl are the body
499 -- and declaration of the instance (either package body and declaration
500 -- nodes or subprogram body and declaration nodes depending on the case).
501 -- On return, the node N has been rewritten with the actual body.
503 function Build_Subprogram_Decl_Wrapper
504 (Formal_Subp : Entity_Id) return Node_Id;
505 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
506 -- At the point of instantiation these contracts apply to uses of
507 -- the actual subprogram. This is implemented by creating wrapper
508 -- subprograms instead of the renamings previously used to link
509 -- formal subprograms and the corresponding actuals. If the actual
510 -- is not an entity (e.g. an attribute reference) a renaming is
511 -- created to handle the expansion of the attribute.
513 function Build_Subprogram_Body_Wrapper
514 (Formal_Subp : Entity_Id;
515 Actual_Name : Node_Id) return Node_Id;
516 -- The body of the wrapper is a call to the actual, with the generated
517 -- pre/postconditon checks added.
519 procedure Check_Access_Definition (N : Node_Id);
520 -- Subsidiary routine to null exclusion processing. Perform an assertion
521 -- check on Ada version and the presence of an access definition in N.
523 procedure Check_Formal_Packages (P_Id : Entity_Id);
524 -- Apply the following to all formal packages in generic associations.
525 -- Restore the visibility of the formals of the instance that are not
526 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
527 -- created for formal instances that are not defaulted.
529 procedure Check_Formal_Package_Instance
530 (Formal_Pack : Entity_Id;
531 Actual_Pack : Entity_Id);
532 -- Verify that the actuals of the actual instance match the actuals of
533 -- the template for a formal package that is not declared with a box.
535 procedure Check_Forward_Instantiation (Decl : Node_Id);
536 -- If the generic is a local entity and the corresponding body has not
537 -- been seen yet, flag enclosing packages to indicate that it will be
538 -- elaborated after the generic body. Subprograms declared in the same
539 -- package cannot be inlined by the front end because front-end inlining
540 -- requires a strict linear order of elaboration.
542 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
543 -- Check if some association between formals and actuals requires to make
544 -- visible primitives of a tagged type, and make those primitives visible.
545 -- Return the list of primitives whose visibility is modified (to restore
546 -- their visibility later through Restore_Hidden_Primitives). If no
547 -- candidate is found then return No_Elist.
549 procedure Check_Hidden_Child_Unit
551 Gen_Unit : Entity_Id;
552 Act_Decl_Id : Entity_Id);
553 -- If the generic unit is an implicit child instance within a parent
554 -- instance, we need to make an explicit test that it is not hidden by
555 -- a child instance of the same name and parent.
557 procedure Check_Generic_Actuals
558 (Instance : Entity_Id;
559 Is_Formal_Box : Boolean);
560 -- Similar to previous one. Check the actuals in the instantiation,
561 -- whose views can change between the point of instantiation and the point
562 -- of instantiation of the body. In addition, mark the generic renamings
563 -- as generic actuals, so that they are not compatible with other actuals.
564 -- Recurse on an actual that is a formal package whose declaration has
567 function Contains_Instance_Of
570 N : Node_Id) return Boolean;
571 -- Inner is instantiated within the generic Outer. Check whether Inner
572 -- directly or indirectly contains an instance of Outer or of one of its
573 -- parents, in the case of a subunit. Each generic unit holds a list of
574 -- the entities instantiated within (at any depth). This procedure
575 -- determines whether the set of such lists contains a cycle, i.e. an
576 -- illegal circular instantiation.
578 function Denotes_Formal_Package
580 On_Exit : Boolean := False;
581 Instance : Entity_Id := Empty) return Boolean;
582 -- Returns True if E is a formal package of an enclosing generic, or
583 -- the actual for such a formal in an enclosing instantiation. If such
584 -- a package is used as a formal in an nested generic, or as an actual
585 -- in a nested instantiation, the visibility of ITS formals should not
586 -- be modified. When called from within Restore_Private_Views, the flag
587 -- On_Exit is true, to indicate that the search for a possible enclosing
588 -- instance should ignore the current one. In that case Instance denotes
589 -- the declaration for which this is an actual. This declaration may be
590 -- an instantiation in the source, or the internal instantiation that
591 -- corresponds to the actual for a formal package.
593 function Earlier (N1, N2 : Node_Id) return Boolean;
594 -- Yields True if N1 and N2 appear in the same compilation unit,
595 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
596 -- traversal of the tree for the unit. Used to determine the placement
597 -- of freeze nodes for instance bodies that may depend on other instances.
599 function Find_Actual_Type
601 Gen_Type : Entity_Id) return Entity_Id;
602 -- When validating the actual types of a child instance, check whether
603 -- the formal is a formal type of the parent unit, and retrieve the current
604 -- actual for it. Typ is the entity in the analyzed formal type declaration
605 -- (component or index type of an array type, or designated type of an
606 -- access formal) and Gen_Type is the enclosing analyzed formal array
607 -- or access type. The desired actual may be a formal of a parent, or may
608 -- be declared in a formal package of a parent. In both cases it is a
609 -- generic actual type because it appears within a visible instance.
610 -- Finally, it may be declared in a parent unit without being a formal
611 -- of that unit, in which case it must be retrieved by visibility.
612 -- Ambiguities may still arise if two homonyms are declared in two formal
613 -- packages, and the prefix of the formal type may be needed to resolve
614 -- the ambiguity in the instance ???
616 procedure Freeze_Subprogram_Body
617 (Inst_Node : Node_Id;
619 Pack_Id : Entity_Id);
620 -- The generic body may appear textually after the instance, including
621 -- in the proper body of a stub, or within a different package instance.
622 -- Given that the instance can only be elaborated after the generic, we
623 -- place freeze_nodes for the instance and/or for packages that may enclose
624 -- the instance and the generic, so that the back-end can establish the
625 -- proper order of elaboration.
627 function Get_Associated_Node (N : Node_Id) return Node_Id;
628 -- In order to propagate semantic information back from the analyzed copy
629 -- to the original generic, we maintain links between selected nodes in the
630 -- generic and their corresponding copies. At the end of generic analysis,
631 -- the routine Save_Global_References traverses the generic tree, examines
632 -- the semantic information, and preserves the links to those nodes that
633 -- contain global information. At instantiation, the information from the
634 -- associated node is placed on the new copy, so that name resolution is
637 -- Three kinds of source nodes have associated nodes:
639 -- a) those that can reference (denote) entities, that is identifiers,
640 -- character literals, expanded_names, operator symbols, operators,
641 -- and attribute reference nodes. These nodes have an Entity field
642 -- and are the set of nodes that are in N_Has_Entity.
644 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
646 -- c) selected components (N_Selected_Component)
648 -- For the first class, the associated node preserves the entity if it is
649 -- global. If the generic contains nested instantiations, the associated
650 -- node itself has been recopied, and a chain of them must be followed.
652 -- For aggregates, the associated node allows retrieval of the type, which
653 -- may otherwise not appear in the generic. The view of this type may be
654 -- different between generic and instantiation, and the full view can be
655 -- installed before the instantiation is analyzed. For aggregates of type
656 -- extensions, the same view exchange may have to be performed for some of
657 -- the ancestor types, if their view is private at the point of
660 -- Nodes that are selected components in the parse tree may be rewritten
661 -- as expanded names after resolution, and must be treated as potential
662 -- entity holders, which is why they also have an Associated_Node.
664 -- Nodes that do not come from source, such as freeze nodes, do not appear
665 -- in the generic tree, and need not have an associated node.
667 -- The associated node is stored in the Associated_Node field. Note that
668 -- this field overlaps Entity, which is fine, because the whole point is
669 -- that we don't need or want the normal Entity field in this situation.
671 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
672 -- Traverse the Exchanged_Views list to see if a type was private
673 -- and has already been flipped during this phase of instantiation.
675 function Has_Contracts (Decl : Node_Id) return Boolean;
676 -- Determine whether a formal subprogram has a Pre- or Postcondition,
677 -- in which case a subprogram wrapper has to be built for the actual.
679 procedure Hide_Current_Scope;
680 -- When instantiating a generic child unit, the parent context must be
681 -- present, but the instance and all entities that may be generated
682 -- must be inserted in the current scope. We leave the current scope
683 -- on the stack, but make its entities invisible to avoid visibility
684 -- problems. This is reversed at the end of the instantiation. This is
685 -- not done for the instantiation of the bodies, which only require the
686 -- instances of the generic parents to be in scope.
688 function In_Main_Context (E : Entity_Id) return Boolean;
689 -- Check whether an instantiation is in the context of the main unit.
690 -- Used to determine whether its body should be elaborated to allow
691 -- front-end inlining.
693 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
694 -- Add the context clause of the unit containing a generic unit to a
695 -- compilation unit that is, or contains, an instantiation.
698 -- Establish environment for subsequent instantiation. Separated from
699 -- Save_Env because data-structures for visibility handling must be
700 -- initialized before call to Check_Generic_Child_Unit.
702 procedure Inline_Instance_Body
704 Gen_Unit : Entity_Id;
706 -- If front-end inlining is requested, instantiate the package body,
707 -- and preserve the visibility of its compilation unit, to insure
708 -- that successive instantiations succeed.
710 procedure Insert_Freeze_Node_For_Instance
713 -- N denotes a package or a subprogram instantiation and F_Node is the
714 -- associated freeze node. Insert the freeze node before the first source
715 -- body which follows immediately after N. If no such body is found, the
716 -- freeze node is inserted at the end of the declarative region which
719 procedure Install_Body
724 -- If the instantiation happens textually before the body of the generic,
725 -- the instantiation of the body must be analyzed after the generic body,
726 -- and not at the point of instantiation. Such early instantiations can
727 -- happen if the generic and the instance appear in a package declaration
728 -- because the generic body can only appear in the corresponding package
729 -- body. Early instantiations can also appear if generic, instance and
730 -- body are all in the declarative part of a subprogram or entry. Entities
731 -- of packages that are early instantiations are delayed, and their freeze
732 -- node appears after the generic body. This rather complex machinery is
733 -- needed when nested instantiations are present, because the source does
734 -- not carry any indication of where the corresponding instance bodies must
735 -- be installed and frozen.
737 procedure Install_Formal_Packages (Par : Entity_Id);
738 -- Install the visible part of any formal of the parent that is a formal
739 -- package. Note that for the case of a formal package with a box, this
740 -- includes the formal part of the formal package (12.7(10/2)).
742 procedure Install_Hidden_Primitives
743 (Prims_List : in out Elist_Id;
746 -- Remove suffix 'P' from hidden primitives of Act_T to match the
747 -- visibility of primitives of Gen_T. The list of primitives to which
748 -- the suffix is removed is added to Prims_List to restore them later.
750 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
751 -- When compiling an instance of a child unit the parent (which is
752 -- itself an instance) is an enclosing scope that must be made
753 -- immediately visible. This procedure is also used to install the non-
754 -- generic parent of a generic child unit when compiling its body, so
755 -- that full views of types in the parent are made visible.
757 -- The functions Instantiate_XXX perform various legality checks and build
758 -- the declarations for instantiated generic parameters. In all of these
759 -- Formal is the entity in the generic unit, Actual is the entity of
760 -- expression in the generic associations, and Analyzed_Formal is the
761 -- formal in the generic copy, which contains the semantic information to
762 -- be used to validate the actual.
764 function Instantiate_Object
767 Analyzed_Formal : Node_Id) return List_Id;
769 function Instantiate_Type
772 Analyzed_Formal : Node_Id;
773 Actual_Decls : List_Id) return List_Id;
775 function Instantiate_Formal_Subprogram
778 Analyzed_Formal : Node_Id) return Node_Id;
780 function Instantiate_Formal_Package
783 Analyzed_Formal : Node_Id) return List_Id;
784 -- If the formal package is declared with a box, special visibility rules
785 -- apply to its formals: they are in the visible part of the package. This
786 -- is true in the declarative region of the formal package, that is to say
787 -- in the enclosing generic or instantiation. For an instantiation, the
788 -- parameters of the formal package are made visible in an explicit step.
789 -- Furthermore, if the actual has a visible USE clause, these formals must
790 -- be made potentially use-visible as well. On exit from the enclosing
791 -- instantiation, the reverse must be done.
793 -- For a formal package declared without a box, there are conformance rules
794 -- that apply to the actuals in the generic declaration and the actuals of
795 -- the actual package in the enclosing instantiation. The simplest way to
796 -- apply these rules is to repeat the instantiation of the formal package
797 -- in the context of the enclosing instance, and compare the generic
798 -- associations of this instantiation with those of the actual package.
799 -- This internal instantiation only needs to contain the renamings of the
800 -- formals: the visible and private declarations themselves need not be
803 -- In Ada 2005, the formal package may be only partially parameterized.
804 -- In that case the visibility step must make visible those actuals whose
805 -- corresponding formals were given with a box. A final complication
806 -- involves inherited operations from formal derived types, which must
807 -- be visible if the type is.
809 function Is_In_Main_Unit (N : Node_Id) return Boolean;
810 -- Test if given node is in the main unit
812 procedure Load_Parent_Of_Generic
815 Body_Optional : Boolean := False);
816 -- If the generic appears in a separate non-generic library unit, load the
817 -- corresponding body to retrieve the body of the generic. N is the node
818 -- for the generic instantiation, Spec is the generic package declaration.
820 -- Body_Optional is a flag that indicates that the body is being loaded to
821 -- ensure that temporaries are generated consistently when there are other
822 -- instances in the current declarative part that precede the one being
823 -- loaded. In that case a missing body is acceptable.
825 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
826 -- Within the generic part, entities in the formal package are
827 -- visible. To validate subsequent type declarations, indicate
828 -- the correspondence between the entities in the analyzed formal,
829 -- and the entities in the actual package. There are three packages
830 -- involved in the instantiation of a formal package: the parent
831 -- generic P1 which appears in the generic declaration, the fake
832 -- instantiation P2 which appears in the analyzed generic, and whose
833 -- visible entities may be used in subsequent formals, and the actual
834 -- P3 in the instance. To validate subsequent formals, me indicate
835 -- that the entities in P2 are mapped into those of P3. The mapping of
836 -- entities has to be done recursively for nested packages.
838 procedure Move_Freeze_Nodes
842 -- Freeze nodes can be generated in the analysis of a generic unit, but
843 -- will not be seen by the back-end. It is necessary to move those nodes
844 -- to the enclosing scope if they freeze an outer entity. We place them
845 -- at the end of the enclosing generic package, which is semantically
848 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
849 -- Analyze actuals to perform name resolution. Full resolution is done
850 -- later, when the expected types are known, but names have to be captured
851 -- before installing parents of generics, that are not visible for the
852 -- actuals themselves.
854 -- If Inst is present, it is the entity of the package instance. This
855 -- entity is marked as having a limited_view actual when some actual is
856 -- a limited view. This is used to place the instance body properly.
858 procedure Provide_Completing_Bodies (N : Node_Id);
859 -- Generate completing bodies for all subprograms found within package or
860 -- subprogram declaration N.
862 procedure Remove_Parent (In_Body : Boolean := False);
863 -- Reverse effect after instantiation of child is complete
865 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
866 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
869 procedure Set_Instance_Env
870 (Gen_Unit : Entity_Id;
871 Act_Unit : Entity_Id);
872 -- Save current instance on saved environment, to be used to determine
873 -- the global status of entities in nested instances. Part of Save_Env.
874 -- called after verifying that the generic unit is legal for the instance,
875 -- The procedure also examines whether the generic unit is a predefined
876 -- unit, in order to set configuration switches accordingly. As a result
877 -- the procedure must be called after analyzing and freezing the actuals.
879 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
880 -- Associate analyzed generic parameter with corresponding instance. Used
881 -- for semantic checks at instantiation time.
883 function True_Parent (N : Node_Id) return Node_Id;
884 -- For a subunit, return parent of corresponding stub, else return
887 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
888 -- Verify that an attribute that appears as the default for a formal
889 -- subprogram is a function or procedure with the correct profile.
891 -------------------------------------------
892 -- Data Structures for Generic Renamings --
893 -------------------------------------------
895 -- The map Generic_Renamings associates generic entities with their
896 -- corresponding actuals. Currently used to validate type instances. It
897 -- will eventually be used for all generic parameters to eliminate the
898 -- need for overload resolution in the instance.
900 type Assoc_Ptr is new Int;
902 Assoc_Null : constant Assoc_Ptr := -1;
907 Next_In_HTable : Assoc_Ptr;
910 package Generic_Renamings is new Table.Table
911 (Table_Component_Type => Assoc,
912 Table_Index_Type => Assoc_Ptr,
913 Table_Low_Bound => 0,
915 Table_Increment => 100,
916 Table_Name => "Generic_Renamings");
918 -- Variable to hold enclosing instantiation. When the environment is
919 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
921 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
923 -- Hash table for associations
925 HTable_Size : constant := 37;
926 type HTable_Range is range 0 .. HTable_Size - 1;
928 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
929 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
930 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
931 function Hash (F : Entity_Id) return HTable_Range;
933 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
934 Header_Num => HTable_Range,
936 Elmt_Ptr => Assoc_Ptr,
937 Null_Ptr => Assoc_Null,
938 Set_Next => Set_Next_Assoc,
941 Get_Key => Get_Gen_Id,
945 Exchanged_Views : Elist_Id;
946 -- This list holds the private views that have been exchanged during
947 -- instantiation to restore the visibility of the generic declaration.
948 -- (see comments above). After instantiation, the current visibility is
949 -- reestablished by means of a traversal of this list.
951 Hidden_Entities : Elist_Id;
952 -- This list holds the entities of the current scope that are removed
953 -- from immediate visibility when instantiating a child unit. Their
954 -- visibility is restored in Remove_Parent.
956 -- Because instantiations can be recursive, the following must be saved
957 -- on entry and restored on exit from an instantiation (spec or body).
958 -- This is done by the two procedures Save_Env and Restore_Env. For
959 -- package and subprogram instantiations (but not for the body instances)
960 -- the action of Save_Env is done in two steps: Init_Env is called before
961 -- Check_Generic_Child_Unit, because setting the parent instances requires
962 -- that the visibility data structures be properly initialized. Once the
963 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
965 Parent_Unit_Visible : Boolean := False;
966 -- Parent_Unit_Visible is used when the generic is a child unit, and
967 -- indicates whether the ultimate parent of the generic is visible in the
968 -- instantiation environment. It is used to reset the visibility of the
969 -- parent at the end of the instantiation (see Remove_Parent).
971 Instance_Parent_Unit : Entity_Id := Empty;
972 -- This records the ultimate parent unit of an instance of a generic
973 -- child unit and is used in conjunction with Parent_Unit_Visible to
974 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
976 type Instance_Env is record
977 Instantiated_Parent : Assoc;
978 Exchanged_Views : Elist_Id;
979 Hidden_Entities : Elist_Id;
980 Current_Sem_Unit : Unit_Number_Type;
981 Parent_Unit_Visible : Boolean := False;
982 Instance_Parent_Unit : Entity_Id := Empty;
983 Switches : Config_Switches_Type;
986 package Instance_Envs is new Table.Table (
987 Table_Component_Type => Instance_Env,
988 Table_Index_Type => Int,
989 Table_Low_Bound => 0,
991 Table_Increment => 100,
992 Table_Name => "Instance_Envs");
994 procedure Restore_Private_Views
995 (Pack_Id : Entity_Id;
996 Is_Package : Boolean := True);
997 -- Restore the private views of external types, and unmark the generic
998 -- renamings of actuals, so that they become compatible subtypes again.
999 -- For subprograms, Pack_Id is the package constructed to hold the
1002 procedure Switch_View (T : Entity_Id);
1003 -- Switch the partial and full views of a type and its private
1004 -- dependents (i.e. its subtypes and derived types).
1006 ------------------------------------
1007 -- Structures for Error Reporting --
1008 ------------------------------------
1010 Instantiation_Node : Node_Id;
1011 -- Used by subprograms that validate instantiation of formal parameters
1012 -- where there might be no actual on which to place the error message.
1013 -- Also used to locate the instantiation node for generic subunits.
1015 Instantiation_Error : exception;
1016 -- When there is a semantic error in the generic parameter matching,
1017 -- there is no point in continuing the instantiation, because the
1018 -- number of cascaded errors is unpredictable. This exception aborts
1019 -- the instantiation process altogether.
1021 S_Adjustment : Sloc_Adjustment;
1022 -- Offset created for each node in an instantiation, in order to keep
1023 -- track of the source position of the instantiation in each of its nodes.
1024 -- A subsequent semantic error or warning on a construct of the instance
1025 -- points to both places: the original generic node, and the point of
1026 -- instantiation. See Sinput and Sinput.L for additional details.
1028 ------------------------------------------------------------
1029 -- Data structure for keeping track when inside a Generic --
1030 ------------------------------------------------------------
1032 -- The following table is used to save values of the Inside_A_Generic
1033 -- flag (see spec of Sem) when they are saved by Start_Generic.
1035 package Generic_Flags is new Table.Table (
1036 Table_Component_Type => Boolean,
1037 Table_Index_Type => Int,
1038 Table_Low_Bound => 0,
1039 Table_Initial => 32,
1040 Table_Increment => 200,
1041 Table_Name => "Generic_Flags");
1043 ---------------------------
1044 -- Abandon_Instantiation --
1045 ---------------------------
1047 procedure Abandon_Instantiation (N : Node_Id) is
1049 Error_Msg_N ("\instantiation abandoned!", N);
1050 raise Instantiation_Error;
1051 end Abandon_Instantiation;
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1066 function Analyze_Associations
1069 F_Copy : List_Id) return List_Id
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1084 Saved_Formal : Node_Id;
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1106 procedure Build_Subprogram_Wrappers;
1107 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1108 -- subprograms. The implementation of making the formal into a renaming
1109 -- of the actual does not work, given that subprogram renaming cannot
1110 -- carry aspect specifications. Instead we must create subprogram
1111 -- wrappers whose body is a call to the actual, and whose declaration
1112 -- carries the aspects of the formal.
1114 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1115 -- Warn if an actual fixed-point type has user-defined arithmetic
1116 -- operations, but there is no corresponding formal in the generic,
1117 -- in which case the predefined operations will be used. This merits
1118 -- a warning because of the special semantics of fixed point ops.
1120 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1121 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1122 -- cannot have a named association for it. AI05-0025 extends this rule
1123 -- to formals of formal packages by AI05-0025, and it also applies to
1124 -- box-initialized formals.
1126 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1127 -- Determine whether the parameter types and the return type of Subp
1128 -- are fully defined at the point of instantiation.
1130 function Matching_Actual
1132 A_F : Entity_Id) return Node_Id;
1133 -- Find actual that corresponds to a given a formal parameter. If the
1134 -- actuals are positional, return the next one, if any. If the actuals
1135 -- are named, scan the parameter associations to find the right one.
1136 -- A_F is the corresponding entity in the analyzed generic, which is
1137 -- placed on the selector name.
1139 -- In Ada 2005, a named association may be given with a box, in which
1140 -- case Matching_Actual sets Found_Assoc to the generic association,
1141 -- but return Empty for the actual itself. In this case the code below
1142 -- creates a corresponding declaration for the formal.
1144 function Partial_Parameterization return Boolean;
1145 -- Ada 2005: if no match is found for a given formal, check if the
1146 -- association for it includes a box, or whether the associations
1147 -- include an Others clause.
1149 procedure Process_Default (F : Entity_Id);
1150 -- Add a copy of the declaration of generic formal F to the list of
1151 -- associations, and add an explicit box association for F if there
1152 -- is none yet, and the default comes from an Others_Choice.
1154 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1155 -- Determine whether Subp renames one of the subprograms defined in the
1156 -- generated package Standard.
1158 procedure Set_Analyzed_Formal;
1159 -- Find the node in the generic copy that corresponds to a given formal.
1160 -- The semantic information on this node is used to perform legality
1161 -- checks on the actuals. Because semantic analysis can introduce some
1162 -- anonymous entities or modify the declaration node itself, the
1163 -- correspondence between the two lists is not one-one. In addition to
1164 -- anonymous types, the presence a formal equality will introduce an
1165 -- implicit declaration for the corresponding inequality.
1167 -----------------------------------------
1168 -- procedure Build_Subprogram_Wrappers --
1169 -----------------------------------------
1171 procedure Build_Subprogram_Wrappers is
1172 Formal : constant Entity_Id :=
1173 Defining_Unit_Name (Specification (Analyzed_Formal));
1174 Aspect_Spec : Node_Id;
1175 Decl_Node : Node_Id;
1176 Actual_Name : Node_Id;
1179 -- Create declaration for wrapper subprogram
1180 -- The actual can be overloaded, in which case it will be
1181 -- resolved when the call in the wrapper body is analyzed.
1182 -- We attach the possible interpretations of the actual to
1183 -- the name to be used in the call in the wrapper body.
1185 if Is_Entity_Name (Match) then
1186 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1188 if Is_Overloaded (Match) then
1189 Save_Interps (Match, Actual_Name);
1193 -- Use renaming declaration created when analyzing actual.
1194 -- This may be incomplete if there are several formal
1195 -- subprograms whose actual is an attribute ???
1198 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1201 Actual_Name := New_Occurrence_Of
1202 (Defining_Entity (Renaming_Decl), Sloc (Match));
1203 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1207 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1209 -- Transfer aspect specifications from formal subprogram to wrapper
1211 Set_Aspect_Specifications (Decl_Node,
1212 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1214 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1215 while Present (Aspect_Spec) loop
1216 Set_Analyzed (Aspect_Spec, False);
1220 Append_To (Assoc_List, Decl_Node);
1222 -- Create corresponding body, and append it to association list
1223 -- that appears at the head of the declarations in the instance.
1224 -- The subprogram may be called in the analysis of subsequent
1227 Append_To (Assoc_List,
1228 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1229 end Build_Subprogram_Wrappers;
1231 ----------------------------------------
1232 -- Check_Overloaded_Formal_Subprogram --
1233 ----------------------------------------
1235 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1236 Temp_Formal : Entity_Id;
1239 Temp_Formal := First (Formals);
1240 while Present (Temp_Formal) loop
1241 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1242 and then Temp_Formal /= Formal
1244 Chars (Defining_Unit_Name (Specification (Formal))) =
1245 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1247 if Present (Found_Assoc) then
1249 ("named association not allowed for overloaded formal",
1254 ("named association not allowed for overloaded formal",
1258 Abandon_Instantiation (Instantiation_Node);
1263 end Check_Overloaded_Formal_Subprogram;
1265 -------------------------------
1266 -- Check_Fixed_Point_Actual --
1267 -------------------------------
1269 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1270 Typ : constant Entity_Id := Entity (Actual);
1271 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1277 -- Locate primitive operations of the type that are arithmetic
1280 Elem := First_Elmt (Prims);
1281 while Present (Elem) loop
1282 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1284 -- Check whether the generic unit has a formal subprogram of
1285 -- the same name. This does not check types but is good enough
1286 -- to justify a warning.
1288 Formal := First_Non_Pragma (Formals);
1289 Op := Alias (Node (Elem));
1291 while Present (Formal) loop
1292 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1293 and then Chars (Defining_Entity (Formal)) =
1298 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1304 -- Locate corresponding actual, and check whether it
1305 -- includes a fixed-point type.
1307 Assoc := First (Assoc_List);
1308 while Present (Assoc) loop
1310 Nkind (Assoc) = N_Package_Renaming_Declaration
1311 and then Chars (Defining_Unit_Name (Assoc)) =
1312 Chars (Defining_Identifier (Formal));
1317 if Present (Assoc) then
1319 -- If formal package declares a fixed-point type,
1320 -- and the user-defined operator is derived from
1321 -- a generic instance package, the fixed-point type
1322 -- does not use the corresponding predefined op.
1324 Ent := First_Entity (Entity (Name (Assoc)));
1325 while Present (Ent) loop
1326 if Is_Fixed_Point_Type (Ent)
1327 and then Present (Op)
1328 and then Is_Generic_Instance (Scope (Op))
1343 Error_Msg_Sloc := Sloc (Node (Elem));
1345 ("?instance uses predefined operation, not primitive "
1346 & "operation&#", Actual, Node (Elem));
1352 end Check_Fixed_Point_Actual;
1354 -------------------------------
1355 -- Has_Fully_Defined_Profile --
1356 -------------------------------
1358 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1359 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1360 -- Determine whethet type Typ is fully defined
1362 ---------------------------
1363 -- Is_Fully_Defined_Type --
1364 ---------------------------
1366 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1368 -- A private type without a full view is not fully defined
1370 if Is_Private_Type (Typ)
1371 and then No (Full_View (Typ))
1375 -- An incomplete type is never fully defined
1377 elsif Is_Incomplete_Type (Typ) then
1380 -- All other types are fully defined
1385 end Is_Fully_Defined_Type;
1387 -- Local declarations
1391 -- Start of processing for Has_Fully_Defined_Profile
1394 -- Check the parameters
1396 Param := First_Formal (Subp);
1397 while Present (Param) loop
1398 if not Is_Fully_Defined_Type (Etype (Param)) then
1402 Next_Formal (Param);
1405 -- Check the return type
1407 return Is_Fully_Defined_Type (Etype (Subp));
1408 end Has_Fully_Defined_Profile;
1410 ---------------------
1411 -- Matching_Actual --
1412 ---------------------
1414 function Matching_Actual
1416 A_F : Entity_Id) return Node_Id
1422 Is_Named_Assoc := False;
1424 -- End of list of purely positional parameters
1426 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1427 Found_Assoc := Empty;
1430 -- Case of positional parameter corresponding to current formal
1432 elsif No (Selector_Name (Actual)) then
1433 Found_Assoc := Actual;
1434 Act := Explicit_Generic_Actual_Parameter (Actual);
1435 Num_Matched := Num_Matched + 1;
1438 -- Otherwise scan list of named actuals to find the one with the
1439 -- desired name. All remaining actuals have explicit names.
1442 Is_Named_Assoc := True;
1443 Found_Assoc := Empty;
1447 while Present (Actual) loop
1448 if Nkind (Actual) = N_Others_Choice then
1449 Found_Assoc := Empty;
1452 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1453 Set_Entity (Selector_Name (Actual), A_F);
1454 Set_Etype (Selector_Name (Actual), Etype (A_F));
1455 Generate_Reference (A_F, Selector_Name (Actual));
1457 Found_Assoc := Actual;
1458 Act := Explicit_Generic_Actual_Parameter (Actual);
1459 Num_Matched := Num_Matched + 1;
1467 -- Reset for subsequent searches. In most cases the named
1468 -- associations are in order. If they are not, we reorder them
1469 -- to avoid scanning twice the same actual. This is not just a
1470 -- question of efficiency: there may be multiple defaults with
1471 -- boxes that have the same name. In a nested instantiation we
1472 -- insert actuals for those defaults, and cannot rely on their
1473 -- names to disambiguate them.
1475 if Actual = First_Named then
1478 elsif Present (Actual) then
1479 Insert_Before (First_Named, Remove_Next (Prev));
1482 Actual := First_Named;
1485 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1486 Set_Used_As_Generic_Actual (Entity (Act));
1490 end Matching_Actual;
1492 ------------------------------
1493 -- Partial_Parameterization --
1494 ------------------------------
1496 function Partial_Parameterization return Boolean is
1498 return Others_Present
1499 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1500 end Partial_Parameterization;
1502 ---------------------
1503 -- Process_Default --
1504 ---------------------
1506 procedure Process_Default (F : Entity_Id) is
1507 Loc : constant Source_Ptr := Sloc (I_Node);
1508 F_Id : constant Entity_Id := Defining_Entity (F);
1514 -- Append copy of formal declaration to associations, and create new
1515 -- defining identifier for it.
1517 Decl := New_Copy_Tree (F);
1518 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1520 if Nkind (F) in N_Formal_Subprogram_Declaration then
1521 Set_Defining_Unit_Name (Specification (Decl), Id);
1524 Set_Defining_Identifier (Decl, Id);
1527 Append (Decl, Assoc_List);
1529 if No (Found_Assoc) then
1531 Make_Generic_Association (Loc,
1533 New_Occurrence_Of (Id, Loc),
1534 Explicit_Generic_Actual_Parameter => Empty);
1535 Set_Box_Present (Default);
1536 Append (Default, Default_Formals);
1538 end Process_Default;
1540 ---------------------------------
1541 -- Renames_Standard_Subprogram --
1542 ---------------------------------
1544 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1549 while Present (Id) loop
1550 if Scope (Id) = Standard_Standard then
1558 end Renames_Standard_Subprogram;
1560 -------------------------
1561 -- Set_Analyzed_Formal --
1562 -------------------------
1564 procedure Set_Analyzed_Formal is
1568 while Present (Analyzed_Formal) loop
1569 Kind := Nkind (Analyzed_Formal);
1571 case Nkind (Formal) is
1572 when N_Formal_Subprogram_Declaration =>
1573 exit when Kind in N_Formal_Subprogram_Declaration
1576 (Defining_Unit_Name (Specification (Formal))) =
1578 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1580 when N_Formal_Package_Declaration =>
1581 exit when Kind in N_Formal_Package_Declaration
1582 | N_Generic_Package_Declaration
1583 | N_Package_Declaration;
1585 when N_Use_Package_Clause
1592 -- Skip freeze nodes, and nodes inserted to replace
1593 -- unrecognized pragmas.
1596 Kind not in N_Formal_Subprogram_Declaration
1597 and then Kind not in N_Subprogram_Declaration
1601 and then Chars (Defining_Identifier (Formal)) =
1602 Chars (Defining_Identifier (Analyzed_Formal));
1605 Next (Analyzed_Formal);
1607 end Set_Analyzed_Formal;
1609 -- Start of processing for Analyze_Associations
1612 Actuals := Generic_Associations (I_Node);
1614 if Present (Actuals) then
1616 -- Check for an Others choice, indicating a partial parameterization
1617 -- for a formal package.
1619 Actual := First (Actuals);
1620 while Present (Actual) loop
1621 if Nkind (Actual) = N_Others_Choice then
1622 Others_Present := True;
1623 Others_Choice := Actual;
1625 if Present (Next (Actual)) then
1626 Error_Msg_N ("OTHERS must be last association", Actual);
1629 -- This subprogram is used both for formal packages and for
1630 -- instantiations. For the latter, associations must all be
1633 if Nkind (I_Node) /= N_Formal_Package_Declaration
1634 and then Comes_From_Source (I_Node)
1637 ("OTHERS association not allowed in an instance",
1641 -- In any case, nothing to do after the others association
1645 elsif Box_Present (Actual)
1646 and then Comes_From_Source (I_Node)
1647 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1650 ("box association not allowed in an instance", Actual);
1656 -- If named associations are present, save first named association
1657 -- (it may of course be Empty) to facilitate subsequent name search.
1659 First_Named := First (Actuals);
1660 while Present (First_Named)
1661 and then Nkind (First_Named) /= N_Others_Choice
1662 and then No (Selector_Name (First_Named))
1664 Num_Actuals := Num_Actuals + 1;
1669 Named := First_Named;
1670 while Present (Named) loop
1671 if Nkind (Named) /= N_Others_Choice
1672 and then No (Selector_Name (Named))
1674 Error_Msg_N ("invalid positional actual after named one", Named);
1675 Abandon_Instantiation (Named);
1678 -- A named association may lack an actual parameter, if it was
1679 -- introduced for a default subprogram that turns out to be local
1680 -- to the outer instantiation. If it has a box association it must
1681 -- correspond to some formal in the generic.
1683 if Nkind (Named) /= N_Others_Choice
1684 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1685 or else Box_Present (Named))
1687 Num_Actuals := Num_Actuals + 1;
1693 if Present (Formals) then
1694 Formal := First_Non_Pragma (Formals);
1695 Analyzed_Formal := First_Non_Pragma (F_Copy);
1697 if Present (Actuals) then
1698 Actual := First (Actuals);
1700 -- All formals should have default values
1706 while Present (Formal) loop
1707 Set_Analyzed_Formal;
1708 Saved_Formal := Next_Non_Pragma (Formal);
1710 case Nkind (Formal) is
1711 when N_Formal_Object_Declaration =>
1714 (Defining_Identifier (Formal),
1715 Defining_Identifier (Analyzed_Formal));
1717 if No (Match) and then Partial_Parameterization then
1718 Process_Default (Formal);
1722 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1725 -- For a defaulted in_parameter, create an entry in the
1726 -- the list of defaulted actuals, for GNATprove use. Do
1727 -- not included these defaults for an instance nested
1728 -- within a generic, because the defaults are also used
1729 -- in the analysis of the enclosing generic, and only
1730 -- defaulted subprograms are relevant there.
1732 if No (Match) and then not Inside_A_Generic then
1733 Append_To (Default_Actuals,
1734 Make_Generic_Association (Sloc (I_Node),
1737 (Defining_Identifier (Formal), Sloc (I_Node)),
1738 Explicit_Generic_Actual_Parameter =>
1739 New_Copy_Tree (Default_Expression (Formal))));
1743 -- If the object is a call to an expression function, this
1744 -- is a freezing point for it.
1746 if Is_Entity_Name (Match)
1747 and then Present (Entity (Match))
1749 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1750 = N_Expression_Function
1752 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1755 when N_Formal_Type_Declaration =>
1758 (Defining_Identifier (Formal),
1759 Defining_Identifier (Analyzed_Formal));
1762 if Partial_Parameterization then
1763 Process_Default (Formal);
1766 Error_Msg_Sloc := Sloc (Gen_Unit);
1769 Instantiation_Node, Defining_Identifier (Formal));
1771 ("\in instantiation of & declared#",
1772 Instantiation_Node, Gen_Unit);
1773 Abandon_Instantiation (Instantiation_Node);
1780 (Formal, Match, Analyzed_Formal, Assoc_List),
1783 -- Warn when an actual is a fixed-point with user-
1784 -- defined promitives. The warning is superfluous
1785 -- if the formal is private, because there can be
1786 -- no arithmetic operations in the generic so there
1787 -- no danger of confusion.
1789 if Is_Fixed_Point_Type (Entity (Match))
1790 and then not Is_Private_Type
1791 (Defining_Identifier (Analyzed_Formal))
1793 Check_Fixed_Point_Actual (Match);
1796 -- An instantiation is a freeze point for the actuals,
1797 -- unless this is a rewritten formal package, or the
1798 -- formal is an Ada 2012 formal incomplete type.
1800 if Nkind (I_Node) = N_Formal_Package_Declaration
1802 (Ada_Version >= Ada_2012
1804 Ekind (Defining_Identifier (Analyzed_Formal)) =
1810 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1814 -- A remote access-to-class-wide type is not a legal actual
1815 -- for a generic formal of an access type (E.2.2(17/2)).
1816 -- In GNAT an exception to this rule is introduced when
1817 -- the formal is marked as remote using implementation
1818 -- defined aspect/pragma Remote_Access_Type. In that case
1819 -- the actual must be remote as well.
1821 -- If the current instantiation is the construction of a
1822 -- local copy for a formal package the actuals may be
1823 -- defaulted, and there is no matching actual to check.
1825 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1827 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1828 N_Access_To_Object_Definition
1829 and then Present (Match)
1832 Formal_Ent : constant Entity_Id :=
1833 Defining_Identifier (Analyzed_Formal);
1835 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1836 = Is_Remote_Types (Formal_Ent)
1838 -- Remoteness of formal and actual match
1842 elsif Is_Remote_Types (Formal_Ent) then
1844 -- Remote formal, non-remote actual
1847 ("actual for& must be remote", Match, Formal_Ent);
1850 -- Non-remote formal, remote actual
1853 ("actual for& may not be remote",
1859 when N_Formal_Subprogram_Declaration =>
1862 (Defining_Unit_Name (Specification (Formal)),
1863 Defining_Unit_Name (Specification (Analyzed_Formal)));
1865 -- If the formal subprogram has the same name as another
1866 -- formal subprogram of the generic, then a named
1867 -- association is illegal (12.3(9)). Exclude named
1868 -- associations that are generated for a nested instance.
1871 and then Is_Named_Assoc
1872 and then Comes_From_Source (Found_Assoc)
1874 Check_Overloaded_Formal_Subprogram (Formal);
1877 -- If there is no corresponding actual, this may be case
1878 -- of partial parameterization, or else the formal has a
1879 -- default or a box.
1881 if No (Match) and then Partial_Parameterization then
1882 Process_Default (Formal);
1884 if Nkind (I_Node) = N_Formal_Package_Declaration then
1885 Check_Overloaded_Formal_Subprogram (Formal);
1889 Append_To (Assoc_List,
1890 Instantiate_Formal_Subprogram
1891 (Formal, Match, Analyzed_Formal));
1893 -- If formal subprogram has contracts, create wrappers
1894 -- for it. This is an expansion activity that cannot
1895 -- take place e.g. within an enclosing generic unit.
1897 if Has_Contracts (Analyzed_Formal)
1898 and then Expander_Active
1900 Build_Subprogram_Wrappers;
1903 -- An instantiation is a freeze point for the actuals,
1904 -- unless this is a rewritten formal package.
1906 if Nkind (I_Node) /= N_Formal_Package_Declaration
1907 and then Nkind (Match) = N_Identifier
1908 and then Is_Subprogram (Entity (Match))
1910 -- The actual subprogram may rename a routine defined
1911 -- in Standard. Avoid freezing such renamings because
1912 -- subprograms coming from Standard cannot be frozen.
1915 not Renames_Standard_Subprogram (Entity (Match))
1917 -- If the actual subprogram comes from a different
1918 -- unit, it is already frozen, either by a body in
1919 -- that unit or by the end of the declarative part
1920 -- of the unit. This check avoids the freezing of
1921 -- subprograms defined in Standard which are used
1922 -- as generic actuals.
1924 and then In_Same_Code_Unit (Entity (Match), I_Node)
1925 and then Has_Fully_Defined_Profile (Entity (Match))
1927 -- Mark the subprogram as having a delayed freeze
1928 -- since this may be an out-of-order action.
1930 Set_Has_Delayed_Freeze (Entity (Match));
1931 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1935 -- If this is a nested generic, preserve default for later
1936 -- instantiations. We do this as well for GNATprove use,
1937 -- so that the list of generic associations is complete.
1939 if No (Match) and then Box_Present (Formal) then
1941 Subp : constant Entity_Id :=
1943 (Specification (Last (Assoc_List)));
1946 Append_To (Default_Actuals,
1947 Make_Generic_Association (Sloc (I_Node),
1949 New_Occurrence_Of (Subp, Sloc (I_Node)),
1950 Explicit_Generic_Actual_Parameter =>
1951 New_Occurrence_Of (Subp, Sloc (I_Node))));
1955 when N_Formal_Package_Declaration =>
1956 -- The name of the formal package may be hidden by the
1957 -- formal parameter itself.
1959 if Error_Posted (Analyzed_Formal) then
1960 Abandon_Instantiation (Instantiation_Node);
1965 (Defining_Identifier (Formal),
1967 (Original_Node (Analyzed_Formal)));
1971 if Partial_Parameterization then
1972 Process_Default (Formal);
1975 Error_Msg_Sloc := Sloc (Gen_Unit);
1978 Instantiation_Node, Defining_Identifier (Formal));
1980 ("\in instantiation of & declared#",
1981 Instantiation_Node, Gen_Unit);
1983 Abandon_Instantiation (Instantiation_Node);
1989 (Instantiate_Formal_Package
1990 (Formal, Match, Analyzed_Formal),
1993 -- Determine whether the actual package needs an explicit
1994 -- freeze node. This is only the case if the actual is
1995 -- declared in the same unit and has a body. Normally
1996 -- packages do not have explicit freeze nodes, and gigi
1997 -- only uses them to elaborate entities in a package
2000 Explicit_Freeze_Check : declare
2001 Actual : constant Entity_Id := Entity (Match);
2002 Gen_Par : Entity_Id;
2004 Needs_Freezing : Boolean;
2007 procedure Check_Generic_Parent;
2008 -- The actual may be an instantiation of a unit
2009 -- declared in a previous instantiation. If that
2010 -- one is also in the current compilation, it must
2011 -- itself be frozen before the actual. The actual
2012 -- may be an instantiation of a generic child unit,
2013 -- in which case the same applies to the instance
2014 -- of the parent which must be frozen before the
2016 -- Should this itself be recursive ???
2018 --------------------------
2019 -- Check_Generic_Parent --
2020 --------------------------
2022 procedure Check_Generic_Parent is
2023 Inst : constant Node_Id :=
2024 Next (Unit_Declaration_Node (Actual));
2030 if Nkind (Parent (Actual)) = N_Package_Specification
2032 Par := Scope (Generic_Parent (Parent (Actual)));
2034 if Is_Generic_Instance (Par) then
2037 -- If the actual is a child generic unit, check
2038 -- whether the instantiation of the parent is
2039 -- also local and must also be frozen now. We
2040 -- must retrieve the instance node to locate the
2041 -- parent instance if any.
2043 elsif Ekind (Par) = E_Generic_Package
2044 and then Is_Child_Unit (Gen_Par)
2045 and then Ekind (Scope (Gen_Par)) =
2048 if Nkind (Inst) = N_Package_Instantiation
2049 and then Nkind (Name (Inst)) =
2052 -- Retrieve entity of parent instance
2054 Par := Entity (Prefix (Name (Inst)));
2063 and then Is_Generic_Instance (Par)
2064 and then Scope (Par) = Current_Scope
2066 (No (Freeze_Node (Par))
2068 not Is_List_Member (Freeze_Node (Par)))
2070 Set_Has_Delayed_Freeze (Par);
2071 Append_Elmt (Par, Actuals_To_Freeze);
2073 end Check_Generic_Parent;
2075 -- Start of processing for Explicit_Freeze_Check
2078 if Present (Renamed_Entity (Actual)) then
2080 Generic_Parent (Specification
2081 (Unit_Declaration_Node
2082 (Renamed_Entity (Actual))));
2085 Generic_Parent (Specification
2086 (Unit_Declaration_Node (Actual)));
2089 if not Expander_Active
2090 or else not Has_Completion (Actual)
2091 or else not In_Same_Source_Unit (I_Node, Actual)
2092 or else Is_Frozen (Actual)
2094 (Present (Renamed_Entity (Actual))
2096 not In_Same_Source_Unit
2097 (I_Node, (Renamed_Entity (Actual))))
2102 -- Finally we want to exclude such freeze nodes
2103 -- from statement sequences, which freeze
2104 -- everything before them.
2105 -- Is this strictly necessary ???
2107 Needs_Freezing := True;
2109 P := Parent (I_Node);
2110 while Nkind (P) /= N_Compilation_Unit loop
2111 if Nkind (P) = N_Handled_Sequence_Of_Statements
2113 Needs_Freezing := False;
2120 if Needs_Freezing then
2121 Check_Generic_Parent;
2123 -- If the actual is a renaming of a proper
2124 -- instance of the formal package, indicate
2125 -- that it is the instance that must be frozen.
2127 if Nkind (Parent (Actual)) =
2128 N_Package_Renaming_Declaration
2130 Set_Has_Delayed_Freeze
2131 (Renamed_Entity (Actual));
2133 (Renamed_Entity (Actual),
2136 Set_Has_Delayed_Freeze (Actual);
2137 Append_Elmt (Actual, Actuals_To_Freeze);
2141 end Explicit_Freeze_Check;
2144 -- For use type and use package appearing in the generic part,
2145 -- we have already copied them, so we can just move them where
2146 -- they belong (we mustn't recopy them since this would mess up
2147 -- the Sloc values).
2149 when N_Use_Package_Clause
2152 if Nkind (Original_Node (I_Node)) =
2153 N_Formal_Package_Declaration
2155 Append (New_Copy_Tree (Formal), Assoc_List);
2158 Append (Formal, Assoc_List);
2162 raise Program_Error;
2165 Formal := Saved_Formal;
2166 Next_Non_Pragma (Analyzed_Formal);
2169 if Num_Actuals > Num_Matched then
2170 Error_Msg_Sloc := Sloc (Gen_Unit);
2172 if Present (Selector_Name (Actual)) then
2174 ("unmatched actual &", Actual, Selector_Name (Actual));
2176 ("\in instantiation of & declared#", Actual, Gen_Unit);
2179 ("unmatched actual in instantiation of & declared#",
2184 elsif Present (Actuals) then
2186 ("too many actuals in generic instantiation", Instantiation_Node);
2189 -- An instantiation freezes all generic actuals. The only exceptions
2190 -- to this are incomplete types and subprograms which are not fully
2191 -- defined at the point of instantiation.
2194 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2196 while Present (Elmt) loop
2197 Freeze_Before (I_Node, Node (Elmt));
2202 -- If there are default subprograms, normalize the tree by adding
2203 -- explicit associations for them. This is required if the instance
2204 -- appears within a generic.
2206 if not Is_Empty_List (Default_Actuals) then
2211 Default := First (Default_Actuals);
2212 while Present (Default) loop
2213 Mark_Rewrite_Insertion (Default);
2217 if No (Actuals) then
2218 Set_Generic_Associations (I_Node, Default_Actuals);
2220 Append_List_To (Actuals, Default_Actuals);
2225 -- If this is a formal package, normalize the parameter list by adding
2226 -- explicit box associations for the formals that are covered by an
2229 if not Is_Empty_List (Default_Formals) then
2230 Append_List (Default_Formals, Formals);
2234 end Analyze_Associations;
2236 -------------------------------
2237 -- Analyze_Formal_Array_Type --
2238 -------------------------------
2240 procedure Analyze_Formal_Array_Type
2241 (T : in out Entity_Id;
2247 -- Treated like a non-generic array declaration, with additional
2252 if Nkind (Def) = N_Constrained_Array_Definition then
2253 DSS := First (Discrete_Subtype_Definitions (Def));
2254 while Present (DSS) loop
2255 if Nkind (DSS) in N_Subtype_Indication
2257 | N_Attribute_Reference
2259 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2266 Array_Type_Declaration (T, Def);
2267 Set_Is_Generic_Type (Base_Type (T));
2269 if Ekind (Component_Type (T)) = E_Incomplete_Type
2270 and then No (Full_View (Component_Type (T)))
2272 Error_Msg_N ("premature usage of incomplete type", Def);
2274 -- Check that range constraint is not allowed on the component type
2275 -- of a generic formal array type (AARM 12.5.3(3))
2277 elsif Is_Internal (Component_Type (T))
2278 and then Present (Subtype_Indication (Component_Definition (Def)))
2279 and then Nkind (Original_Node
2280 (Subtype_Indication (Component_Definition (Def)))) =
2281 N_Subtype_Indication
2284 ("in a formal, a subtype indication can only be "
2285 & "a subtype mark (RM 12.5.3(3))",
2286 Subtype_Indication (Component_Definition (Def)));
2289 end Analyze_Formal_Array_Type;
2291 ---------------------------------------------
2292 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2293 ---------------------------------------------
2295 -- As for other generic types, we create a valid type representation with
2296 -- legal but arbitrary attributes, whose values are never considered
2297 -- static. For all scalar types we introduce an anonymous base type, with
2298 -- the same attributes. We choose the corresponding integer type to be
2299 -- Standard_Integer.
2300 -- Here and in other similar routines, the Sloc of the generated internal
2301 -- type must be the same as the sloc of the defining identifier of the
2302 -- formal type declaration, to provide proper source navigation.
2304 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2308 Loc : constant Source_Ptr := Sloc (Def);
2310 Base : constant Entity_Id :=
2312 (E_Decimal_Fixed_Point_Type,
2314 Sloc (Defining_Identifier (Parent (Def))), 'G');
2316 Int_Base : constant Entity_Id := Standard_Integer;
2317 Delta_Val : constant Ureal := Ureal_1;
2318 Digs_Val : constant Uint := Uint_6;
2320 function Make_Dummy_Bound return Node_Id;
2321 -- Return a properly typed universal real literal to use as a bound
2323 ----------------------
2324 -- Make_Dummy_Bound --
2325 ----------------------
2327 function Make_Dummy_Bound return Node_Id is
2328 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2330 Set_Etype (Bound, Universal_Real);
2332 end Make_Dummy_Bound;
2334 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2339 Set_Etype (Base, Base);
2340 Set_Size_Info (Base, Int_Base);
2341 Set_RM_Size (Base, RM_Size (Int_Base));
2342 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2343 Set_Digits_Value (Base, Digs_Val);
2344 Set_Delta_Value (Base, Delta_Val);
2345 Set_Small_Value (Base, Delta_Val);
2346 Set_Scalar_Range (Base,
2348 Low_Bound => Make_Dummy_Bound,
2349 High_Bound => Make_Dummy_Bound));
2351 Set_Is_Generic_Type (Base);
2352 Set_Parent (Base, Parent (Def));
2354 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2355 Set_Etype (T, Base);
2356 Set_Size_Info (T, Int_Base);
2357 Set_RM_Size (T, RM_Size (Int_Base));
2358 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2359 Set_Digits_Value (T, Digs_Val);
2360 Set_Delta_Value (T, Delta_Val);
2361 Set_Small_Value (T, Delta_Val);
2362 Set_Scalar_Range (T, Scalar_Range (Base));
2363 Set_Is_Constrained (T);
2365 Check_Restriction (No_Fixed_Point, Def);
2366 end Analyze_Formal_Decimal_Fixed_Point_Type;
2368 -------------------------------------------
2369 -- Analyze_Formal_Derived_Interface_Type --
2370 -------------------------------------------
2372 procedure Analyze_Formal_Derived_Interface_Type
2377 Loc : constant Source_Ptr := Sloc (Def);
2380 -- Rewrite as a type declaration of a derived type. This ensures that
2381 -- the interface list and primitive operations are properly captured.
2384 Make_Full_Type_Declaration (Loc,
2385 Defining_Identifier => T,
2386 Type_Definition => Def));
2388 Set_Is_Generic_Type (T);
2389 end Analyze_Formal_Derived_Interface_Type;
2391 ---------------------------------
2392 -- Analyze_Formal_Derived_Type --
2393 ---------------------------------
2395 procedure Analyze_Formal_Derived_Type
2400 Loc : constant Source_Ptr := Sloc (Def);
2401 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2405 Set_Is_Generic_Type (T);
2407 if Private_Present (Def) then
2409 Make_Private_Extension_Declaration (Loc,
2410 Defining_Identifier => T,
2411 Discriminant_Specifications => Discriminant_Specifications (N),
2412 Unknown_Discriminants_Present => Unk_Disc,
2413 Subtype_Indication => Subtype_Mark (Def),
2414 Interface_List => Interface_List (Def));
2416 Set_Abstract_Present (New_N, Abstract_Present (Def));
2417 Set_Limited_Present (New_N, Limited_Present (Def));
2418 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2422 Make_Full_Type_Declaration (Loc,
2423 Defining_Identifier => T,
2424 Discriminant_Specifications =>
2425 Discriminant_Specifications (Parent (T)),
2427 Make_Derived_Type_Definition (Loc,
2428 Subtype_Indication => Subtype_Mark (Def)));
2430 Set_Abstract_Present
2431 (Type_Definition (New_N), Abstract_Present (Def));
2433 (Type_Definition (New_N), Limited_Present (Def));
2440 if not Is_Composite_Type (T) then
2442 ("unknown discriminants not allowed for elementary types", N);
2444 Set_Has_Unknown_Discriminants (T);
2445 Set_Is_Constrained (T, False);
2449 -- If the parent type has a known size, so does the formal, which makes
2450 -- legal representation clauses that involve the formal.
2452 Set_Size_Known_At_Compile_Time
2453 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2454 end Analyze_Formal_Derived_Type;
2456 ----------------------------------
2457 -- Analyze_Formal_Discrete_Type --
2458 ----------------------------------
2460 -- The operations defined for a discrete types are those of an enumeration
2461 -- type. The size is set to an arbitrary value, for use in analyzing the
2464 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2465 Loc : constant Source_Ptr := Sloc (Def);
2469 Base : constant Entity_Id :=
2471 (E_Floating_Point_Type, Current_Scope,
2472 Sloc (Defining_Identifier (Parent (Def))), 'G');
2476 Mutate_Ekind (T, E_Enumeration_Subtype);
2477 Set_Etype (T, Base);
2480 Set_Is_Generic_Type (T);
2481 Set_Is_Constrained (T);
2483 -- For semantic analysis, the bounds of the type must be set to some
2484 -- non-static value. The simplest is to create attribute nodes for those
2485 -- bounds, that refer to the type itself. These bounds are never
2486 -- analyzed but serve as place-holders.
2489 Make_Attribute_Reference (Loc,
2490 Attribute_Name => Name_First,
2491 Prefix => New_Occurrence_Of (T, Loc));
2495 Make_Attribute_Reference (Loc,
2496 Attribute_Name => Name_Last,
2497 Prefix => New_Occurrence_Of (T, Loc));
2500 Set_Scalar_Range (T,
2505 Mutate_Ekind (Base, E_Enumeration_Type);
2506 Set_Etype (Base, Base);
2507 Init_Size (Base, 8);
2508 Init_Alignment (Base);
2509 Set_Is_Generic_Type (Base);
2510 Set_Scalar_Range (Base, Scalar_Range (T));
2511 Set_Parent (Base, Parent (Def));
2512 end Analyze_Formal_Discrete_Type;
2514 ----------------------------------
2515 -- Analyze_Formal_Floating_Type --
2516 ---------------------------------
2518 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2519 Base : constant Entity_Id :=
2521 (E_Floating_Point_Type, Current_Scope,
2522 Sloc (Defining_Identifier (Parent (Def))), 'G');
2525 -- The various semantic attributes are taken from the predefined type
2526 -- Float, just so that all of them are initialized. Their values are
2527 -- never used because no constant folding or expansion takes place in
2528 -- the generic itself.
2531 Mutate_Ekind (T, E_Floating_Point_Subtype);
2532 Set_Etype (T, Base);
2533 Set_Size_Info (T, (Standard_Float));
2534 Set_RM_Size (T, RM_Size (Standard_Float));
2535 Set_Digits_Value (T, Digits_Value (Standard_Float));
2536 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2537 Set_Is_Constrained (T);
2539 Set_Is_Generic_Type (Base);
2540 Set_Etype (Base, Base);
2541 Set_Size_Info (Base, (Standard_Float));
2542 Set_RM_Size (Base, RM_Size (Standard_Float));
2543 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2544 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2545 Set_Parent (Base, Parent (Def));
2547 Check_Restriction (No_Floating_Point, Def);
2548 end Analyze_Formal_Floating_Type;
2550 -----------------------------------
2551 -- Analyze_Formal_Interface_Type;--
2552 -----------------------------------
2554 procedure Analyze_Formal_Interface_Type
2559 Loc : constant Source_Ptr := Sloc (N);
2564 Make_Full_Type_Declaration (Loc,
2565 Defining_Identifier => T,
2566 Type_Definition => Def);
2570 Set_Is_Generic_Type (T);
2571 end Analyze_Formal_Interface_Type;
2573 ---------------------------------
2574 -- Analyze_Formal_Modular_Type --
2575 ---------------------------------
2577 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2579 -- Apart from their entity kind, generic modular types are treated like
2580 -- signed integer types, and have the same attributes.
2582 Analyze_Formal_Signed_Integer_Type (T, Def);
2583 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2584 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2586 end Analyze_Formal_Modular_Type;
2588 ---------------------------------------
2589 -- Analyze_Formal_Object_Declaration --
2590 ---------------------------------------
2592 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2593 E : constant Node_Id := Default_Expression (N);
2594 Id : constant Node_Id := Defining_Identifier (N);
2601 -- Determine the mode of the formal object
2603 if Out_Present (N) then
2604 K := E_Generic_In_Out_Parameter;
2606 if not In_Present (N) then
2607 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2611 K := E_Generic_In_Parameter;
2614 if Present (Subtype_Mark (N)) then
2615 Find_Type (Subtype_Mark (N));
2616 T := Entity (Subtype_Mark (N));
2618 -- Verify that there is no redundant null exclusion
2620 if Null_Exclusion_Present (N) then
2621 if not Is_Access_Type (T) then
2623 ("null exclusion can only apply to an access type", N);
2625 elsif Can_Never_Be_Null (T) then
2627 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2631 -- Ada 2005 (AI-423): Formal object with an access definition
2634 Check_Access_Definition (N);
2635 T := Access_Definition
2637 N => Access_Definition (N));
2640 if Ekind (T) = E_Incomplete_Type then
2642 Error_Node : Node_Id;
2645 if Present (Subtype_Mark (N)) then
2646 Error_Node := Subtype_Mark (N);
2648 Check_Access_Definition (N);
2649 Error_Node := Access_Definition (N);
2652 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2656 if K = E_Generic_In_Parameter then
2658 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2660 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2662 ("generic formal of mode IN must not be of limited type", N);
2663 Explain_Limited_Type (T, N);
2666 if Is_Abstract_Type (T) then
2668 ("generic formal of mode IN must not be of abstract type", N);
2672 Preanalyze_Spec_Expression (E, T);
2674 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2676 ("initialization not allowed for limited types", E);
2677 Explain_Limited_Type (T, E);
2681 Mutate_Ekind (Id, K);
2684 -- Case of generic IN OUT parameter
2687 -- If the formal has an unconstrained type, construct its actual
2688 -- subtype, as is done for subprogram formals. In this fashion, all
2689 -- its uses can refer to specific bounds.
2691 Mutate_Ekind (Id, K);
2694 if (Is_Array_Type (T) and then not Is_Constrained (T))
2695 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2698 Non_Freezing_Ref : constant Node_Id :=
2699 New_Occurrence_Of (Id, Sloc (Id));
2703 -- Make sure the actual subtype doesn't generate bogus freezing
2705 Set_Must_Not_Freeze (Non_Freezing_Ref);
2706 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2707 Insert_Before_And_Analyze (N, Decl);
2708 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2711 Set_Actual_Subtype (Id, T);
2716 ("initialization not allowed for `IN OUT` formals", N);
2720 if Has_Aspects (N) then
2721 Analyze_Aspect_Specifications (N, Id);
2723 end Analyze_Formal_Object_Declaration;
2725 ----------------------------------------------
2726 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2727 ----------------------------------------------
2729 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2733 Loc : constant Source_Ptr := Sloc (Def);
2734 Base : constant Entity_Id :=
2736 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2737 Sloc (Defining_Identifier (Parent (Def))), 'G');
2740 -- The semantic attributes are set for completeness only, their values
2741 -- will never be used, since all properties of the type are non-static.
2744 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2745 Set_Etype (T, Base);
2746 Set_Size_Info (T, Standard_Integer);
2747 Set_RM_Size (T, RM_Size (Standard_Integer));
2748 Set_Small_Value (T, Ureal_1);
2749 Set_Delta_Value (T, Ureal_1);
2750 Set_Scalar_Range (T,
2752 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2753 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2754 Set_Is_Constrained (T);
2756 Set_Is_Generic_Type (Base);
2757 Set_Etype (Base, Base);
2758 Set_Size_Info (Base, Standard_Integer);
2759 Set_RM_Size (Base, RM_Size (Standard_Integer));
2760 Set_Small_Value (Base, Ureal_1);
2761 Set_Delta_Value (Base, Ureal_1);
2762 Set_Scalar_Range (Base, Scalar_Range (T));
2763 Set_Parent (Base, Parent (Def));
2765 Check_Restriction (No_Fixed_Point, Def);
2766 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2768 ----------------------------------------
2769 -- Analyze_Formal_Package_Declaration --
2770 ----------------------------------------
2772 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2773 Gen_Id : constant Node_Id := Name (N);
2774 Loc : constant Source_Ptr := Sloc (N);
2775 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2778 Gen_Unit : Entity_Id;
2781 Vis_Prims_List : Elist_Id := No_Elist;
2782 -- List of primitives made temporarily visible in the instantiation
2783 -- to match the visibility of the formal type.
2785 function Build_Local_Package return Node_Id;
2786 -- The formal package is rewritten so that its parameters are replaced
2787 -- with corresponding declarations. For parameters with bona fide
2788 -- associations these declarations are created by Analyze_Associations
2789 -- as for a regular instantiation. For boxed parameters, we preserve
2790 -- the formal declarations and analyze them, in order to introduce
2791 -- entities of the right kind in the environment of the formal.
2793 -------------------------
2794 -- Build_Local_Package --
2795 -------------------------
2797 function Build_Local_Package return Node_Id is
2799 Pack_Decl : Node_Id;
2802 -- Within the formal, the name of the generic package is a renaming
2803 -- of the formal (as for a regular instantiation).
2806 Make_Package_Declaration (Loc,
2809 (Specification (Original_Node (Gen_Decl)),
2810 Empty, Instantiating => True));
2813 Make_Package_Renaming_Declaration (Loc,
2814 Defining_Unit_Name =>
2815 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2816 Name => New_Occurrence_Of (Formal, Loc));
2818 if Nkind (Gen_Id) = N_Identifier
2819 and then Chars (Gen_Id) = Chars (Pack_Id)
2822 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2825 -- If the formal is declared with a box, or with an others choice,
2826 -- create corresponding declarations for all entities in the formal
2827 -- part, so that names with the proper types are available in the
2828 -- specification of the formal package.
2830 -- On the other hand, if there are no associations, then all the
2831 -- formals must have defaults, and this will be checked by the
2832 -- call to Analyze_Associations.
2835 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2838 Formal_Decl : Node_Id;
2841 -- TBA : for a formal package, need to recurse ???
2846 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2847 while Present (Formal_Decl) loop
2851 (Formal_Decl, Empty, Instantiating => True));
2856 -- If generic associations are present, use Analyze_Associations to
2857 -- create the proper renaming declarations.
2861 Act_Tree : constant Node_Id :=
2863 (Original_Node (Gen_Decl), Empty,
2864 Instantiating => True);
2867 Generic_Renamings.Set_Last (0);
2868 Generic_Renamings_HTable.Reset;
2869 Instantiation_Node := N;
2872 Analyze_Associations
2873 (I_Node => Original_Node (N),
2874 Formals => Generic_Formal_Declarations (Act_Tree),
2875 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2877 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2881 Append (Renaming, To => Decls);
2883 -- Add generated declarations ahead of local declarations in
2886 if No (Visible_Declarations (Specification (Pack_Decl))) then
2887 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2890 (First (Visible_Declarations (Specification (Pack_Decl))),
2895 end Build_Local_Package;
2899 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2900 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2902 Associations : Boolean := True;
2904 Parent_Installed : Boolean := False;
2905 Parent_Instance : Entity_Id;
2906 Renaming_In_Par : Entity_Id;
2908 -- Start of processing for Analyze_Formal_Package_Declaration
2911 Check_Text_IO_Special_Unit (Gen_Id);
2914 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2915 Gen_Unit := Entity (Gen_Id);
2917 -- Check for a formal package that is a package renaming
2919 if Present (Renamed_Object (Gen_Unit)) then
2921 -- Indicate that unit is used, before replacing it with renamed
2922 -- entity for use below.
2924 if In_Extended_Main_Source_Unit (N) then
2925 Set_Is_Instantiated (Gen_Unit);
2926 Generate_Reference (Gen_Unit, N);
2929 Gen_Unit := Renamed_Object (Gen_Unit);
2932 if Ekind (Gen_Unit) /= E_Generic_Package then
2933 Error_Msg_N ("expect generic package name", Gen_Id);
2937 elsif Gen_Unit = Current_Scope then
2939 ("generic package cannot be used as a formal package of itself",
2944 elsif In_Open_Scopes (Gen_Unit) then
2945 if Is_Compilation_Unit (Gen_Unit)
2946 and then Is_Child_Unit (Current_Scope)
2948 -- Special-case the error when the formal is a parent, and
2949 -- continue analysis to minimize cascaded errors.
2952 ("generic parent cannot be used as formal package of a child "
2957 ("generic package cannot be used as a formal package within "
2958 & "itself", Gen_Id);
2964 -- Check that name of formal package does not hide name of generic,
2965 -- or its leading prefix. This check must be done separately because
2966 -- the name of the generic has already been analyzed.
2969 Gen_Name : Entity_Id;
2973 while Nkind (Gen_Name) = N_Expanded_Name loop
2974 Gen_Name := Prefix (Gen_Name);
2977 if Chars (Gen_Name) = Chars (Pack_Id) then
2979 ("& is hidden within declaration of formal package",
2985 or else No (Generic_Associations (N))
2986 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2988 Associations := False;
2991 -- If there are no generic associations, the generic parameters appear
2992 -- as local entities and are instantiated like them. We copy the generic
2993 -- package declaration as if it were an instantiation, and analyze it
2994 -- like a regular package, except that we treat the formals as
2995 -- additional visible components.
2997 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2999 if In_Extended_Main_Source_Unit (N) then
3000 Set_Is_Instantiated (Gen_Unit);
3001 Generate_Reference (Gen_Unit, N);
3004 Formal := New_Copy (Pack_Id);
3005 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3007 -- Make local generic without formals. The formals will be replaced with
3008 -- internal declarations.
3011 New_N := Build_Local_Package;
3013 -- If there are errors in the parameter list, Analyze_Associations
3014 -- raises Instantiation_Error. Patch the declaration to prevent further
3015 -- exception propagation.
3018 when Instantiation_Error =>
3019 Enter_Name (Formal);
3020 Mutate_Ekind (Formal, E_Variable);
3021 Set_Etype (Formal, Any_Type);
3022 Restore_Hidden_Primitives (Vis_Prims_List);
3024 if Parent_Installed then
3032 Set_Defining_Unit_Name (Specification (New_N), Formal);
3033 Set_Generic_Parent (Specification (N), Gen_Unit);
3034 Set_Instance_Env (Gen_Unit, Formal);
3035 Set_Is_Generic_Instance (Formal);
3037 Enter_Name (Formal);
3038 Mutate_Ekind (Formal, E_Package);
3039 Set_Etype (Formal, Standard_Void_Type);
3040 Set_Inner_Instances (Formal, New_Elmt_List);
3042 -- It is unclear that any aspects can apply to a formal package
3043 -- declaration, given that they look like a hidden conformance
3044 -- requirement on the corresponding actual. However, Abstract_State
3045 -- must be treated specially because it generates declarations that
3046 -- must appear before other declarations in the specification and
3047 -- must be analyzed at once.
3049 if Present (Aspect_Specifications (Gen_Decl)) then
3050 if No (Aspect_Specifications (N)) then
3051 Set_Aspect_Specifications (N, New_List);
3052 Set_Has_Aspects (N);
3056 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3060 while Present (ASN) loop
3061 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3063 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3064 Set_Entity (New_A, Formal);
3065 Set_Analyzed (New_A, False);
3066 Append (New_A, Aspect_Specifications (N));
3067 Analyze_Aspect_Specifications (N, Formal);
3076 Push_Scope (Formal);
3078 -- Manually set the SPARK_Mode from the context because the package
3079 -- declaration is never analyzed.
3081 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3082 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3083 Set_SPARK_Pragma_Inherited (Formal);
3084 Set_SPARK_Aux_Pragma_Inherited (Formal);
3086 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3088 -- Similarly, we have to make the name of the formal visible in the
3089 -- parent instance, to resolve properly fully qualified names that
3090 -- may appear in the generic unit. The parent instance has been
3091 -- placed on the scope stack ahead of the current scope.
3093 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3096 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3097 Mutate_Ekind (Renaming_In_Par, E_Package);
3098 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3099 Set_Scope (Renaming_In_Par, Parent_Instance);
3100 Set_Parent (Renaming_In_Par, Parent (Formal));
3101 Set_Renamed_Object (Renaming_In_Par, Formal);
3102 Append_Entity (Renaming_In_Par, Parent_Instance);
3105 -- A formal package declaration behaves as a package instantiation with
3106 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3107 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3108 -- all SPARK_Mode pragmas within the generic_package_name.
3110 if SPARK_Mode /= On then
3111 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3113 -- Mark the formal spec in case the body is instantiated at a later
3114 -- pass. This preserves the original context in effect for the body.
3116 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3119 Analyze (Specification (N));
3121 -- The formals for which associations are provided are not visible
3122 -- outside of the formal package. The others are still declared by a
3123 -- formal parameter declaration.
3125 -- If there are no associations, the only local entity to hide is the
3126 -- generated package renaming itself.
3132 E := First_Entity (Formal);
3133 while Present (E) loop
3134 if Associations and then not Is_Generic_Formal (E) then
3138 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3147 End_Package_Scope (Formal);
3148 Restore_Hidden_Primitives (Vis_Prims_List);
3150 if Parent_Installed then
3156 -- Inside the generic unit, the formal package is a regular package, but
3157 -- no body is needed for it. Note that after instantiation, the defining
3158 -- unit name we need is in the new tree and not in the original (see
3159 -- Package_Instantiation). A generic formal package is an instance, and
3160 -- can be used as an actual for an inner instance.
3162 Set_Has_Completion (Formal, True);
3164 -- Add semantic information to the original defining identifier.
3166 Mutate_Ekind (Pack_Id, E_Package);
3167 Set_Etype (Pack_Id, Standard_Void_Type);
3168 Set_Scope (Pack_Id, Scope (Formal));
3169 Set_Has_Completion (Pack_Id, True);
3172 if Has_Aspects (N) then
3173 -- Unclear that any other aspects may appear here, snalyze them
3174 -- for completion, given that the grammar allows their appearance.
3176 Analyze_Aspect_Specifications (N, Pack_Id);
3179 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3180 end Analyze_Formal_Package_Declaration;
3182 ---------------------------------
3183 -- Analyze_Formal_Private_Type --
3184 ---------------------------------
3186 procedure Analyze_Formal_Private_Type
3192 New_Private_Type (N, T, Def);
3194 -- Set the size to an arbitrary but legal value
3196 Set_Size_Info (T, Standard_Integer);
3197 Set_RM_Size (T, RM_Size (Standard_Integer));
3198 end Analyze_Formal_Private_Type;
3200 ------------------------------------
3201 -- Analyze_Formal_Incomplete_Type --
3202 ------------------------------------
3204 procedure Analyze_Formal_Incomplete_Type
3210 Mutate_Ekind (T, E_Incomplete_Type);
3212 Set_Private_Dependents (T, New_Elmt_List);
3214 if Tagged_Present (Def) then
3215 Set_Is_Tagged_Type (T);
3216 Make_Class_Wide_Type (T);
3217 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3219 end Analyze_Formal_Incomplete_Type;
3221 ----------------------------------------
3222 -- Analyze_Formal_Signed_Integer_Type --
3223 ----------------------------------------
3225 procedure Analyze_Formal_Signed_Integer_Type
3229 Base : constant Entity_Id :=
3231 (E_Signed_Integer_Type,
3233 Sloc (Defining_Identifier (Parent (Def))), 'G');
3238 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3239 Set_Etype (T, Base);
3240 Set_Size_Info (T, Standard_Integer);
3241 Set_RM_Size (T, RM_Size (Standard_Integer));
3242 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3243 Set_Is_Constrained (T);
3245 Set_Is_Generic_Type (Base);
3246 Set_Size_Info (Base, Standard_Integer);
3247 Set_RM_Size (Base, RM_Size (Standard_Integer));
3248 Set_Etype (Base, Base);
3249 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3250 Set_Parent (Base, Parent (Def));
3251 end Analyze_Formal_Signed_Integer_Type;
3253 -------------------------------------------
3254 -- Analyze_Formal_Subprogram_Declaration --
3255 -------------------------------------------
3257 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3258 Spec : constant Node_Id := Specification (N);
3259 Def : constant Node_Id := Default_Name (N);
3260 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3268 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3269 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3273 Analyze_Subprogram_Declaration (N);
3274 Set_Is_Formal_Subprogram (Nam);
3275 Set_Has_Completion (Nam);
3277 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3278 Set_Is_Abstract_Subprogram (Nam);
3280 Set_Is_Dispatching_Operation (Nam);
3282 -- A formal abstract procedure cannot have a null default
3283 -- (RM 12.6(4.1/2)).
3285 if Nkind (Spec) = N_Procedure_Specification
3286 and then Null_Present (Spec)
3289 ("a formal abstract subprogram cannot default to null", Spec);
3293 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3295 if No (Ctrl_Type) then
3297 ("abstract formal subprogram must have a controlling type",
3300 elsif Ada_Version >= Ada_2012
3301 and then Is_Incomplete_Type (Ctrl_Type)
3304 ("controlling type of abstract formal subprogram cannot "
3305 & "be incomplete type", N, Ctrl_Type);
3308 Check_Controlling_Formals (Ctrl_Type, Nam);
3313 -- Default name is resolved at the point of instantiation
3315 if Box_Present (N) then
3318 -- Else default is bound at the point of generic declaration
3320 elsif Present (Def) then
3321 if Nkind (Def) = N_Operator_Symbol then
3322 Find_Direct_Name (Def);
3324 elsif Nkind (Def) /= N_Attribute_Reference then
3328 -- For an attribute reference, analyze the prefix and verify
3329 -- that it has the proper profile for the subprogram.
3331 Analyze (Prefix (Def));
3332 Valid_Default_Attribute (Nam, Def);
3336 -- Default name may be overloaded, in which case the interpretation
3337 -- with the correct profile must be selected, as for a renaming.
3338 -- If the definition is an indexed component, it must denote a
3339 -- member of an entry family. If it is a selected component, it
3340 -- can be a protected operation.
3342 if Etype (Def) = Any_Type then
3345 elsif Nkind (Def) = N_Selected_Component then
3346 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3347 Error_Msg_N ("expect valid subprogram name as default", Def);
3350 elsif Nkind (Def) = N_Indexed_Component then
3351 if Is_Entity_Name (Prefix (Def)) then
3352 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3353 Error_Msg_N ("expect valid subprogram name as default", Def);
3356 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3357 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3360 Error_Msg_N ("expect valid subprogram name as default", Def);
3364 Error_Msg_N ("expect valid subprogram name as default", Def);
3368 elsif Nkind (Def) = N_Character_Literal then
3370 -- Needs some type checks: subprogram should be parameterless???
3372 Resolve (Def, (Etype (Nam)));
3374 elsif not Is_Entity_Name (Def)
3375 or else not Is_Overloadable (Entity (Def))
3377 Error_Msg_N ("expect valid subprogram name as default", Def);
3380 elsif not Is_Overloaded (Def) then
3381 Subp := Entity (Def);
3384 Error_Msg_N ("premature usage of formal subprogram", Def);
3386 elsif not Entity_Matches_Spec (Subp, Nam) then
3387 Error_Msg_N ("no visible entity matches specification", Def);
3390 -- More than one interpretation, so disambiguate as for a renaming
3395 I1 : Interp_Index := 0;
3401 Get_First_Interp (Def, I, It);
3402 while Present (It.Nam) loop
3403 if Entity_Matches_Spec (It.Nam, Nam) then
3404 if Subp /= Any_Id then
3405 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3407 if It1 = No_Interp then
3408 Error_Msg_N ("ambiguous default subprogram", Def);
3421 Get_Next_Interp (I, It);
3425 if Subp /= Any_Id then
3427 -- Subprogram found, generate reference to it
3429 Set_Entity (Def, Subp);
3430 Generate_Reference (Subp, Def);
3433 Error_Msg_N ("premature usage of formal subprogram", Def);
3435 elsif Ekind (Subp) /= E_Operator then
3436 Check_Mode_Conformant (Subp, Nam);
3440 Error_Msg_N ("no visible subprogram matches specification", N);
3446 if Has_Aspects (N) then
3447 Analyze_Aspect_Specifications (N, Nam);
3450 end Analyze_Formal_Subprogram_Declaration;
3452 -------------------------------------
3453 -- Analyze_Formal_Type_Declaration --
3454 -------------------------------------
3456 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3457 Def : constant Node_Id := Formal_Type_Definition (N);
3461 T := Defining_Identifier (N);
3463 if Present (Discriminant_Specifications (N))
3464 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3467 ("discriminants not allowed for this formal type", T);
3470 -- Enter the new name, and branch to specific routine
3473 when N_Formal_Private_Type_Definition =>
3474 Analyze_Formal_Private_Type (N, T, Def);
3476 when N_Formal_Derived_Type_Definition =>
3477 Analyze_Formal_Derived_Type (N, T, Def);
3479 when N_Formal_Incomplete_Type_Definition =>
3480 Analyze_Formal_Incomplete_Type (T, Def);
3482 when N_Formal_Discrete_Type_Definition =>
3483 Analyze_Formal_Discrete_Type (T, Def);
3485 when N_Formal_Signed_Integer_Type_Definition =>
3486 Analyze_Formal_Signed_Integer_Type (T, Def);
3488 when N_Formal_Modular_Type_Definition =>
3489 Analyze_Formal_Modular_Type (T, Def);
3491 when N_Formal_Floating_Point_Definition =>
3492 Analyze_Formal_Floating_Type (T, Def);
3494 when N_Formal_Ordinary_Fixed_Point_Definition =>
3495 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3497 when N_Formal_Decimal_Fixed_Point_Definition =>
3498 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3500 when N_Array_Type_Definition =>
3501 Analyze_Formal_Array_Type (T, Def);
3503 when N_Access_Function_Definition
3504 | N_Access_Procedure_Definition
3505 | N_Access_To_Object_Definition
3507 Analyze_Generic_Access_Type (T, Def);
3509 -- Ada 2005: a interface declaration is encoded as an abstract
3510 -- record declaration or a abstract type derivation.
3512 when N_Record_Definition =>
3513 Analyze_Formal_Interface_Type (N, T, Def);
3515 when N_Derived_Type_Definition =>
3516 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3522 raise Program_Error;
3525 -- A formal type declaration declares a type and its first
3528 Set_Is_Generic_Type (T);
3529 Set_Is_First_Subtype (T);
3531 if Has_Aspects (N) then
3532 Analyze_Aspect_Specifications (N, T);
3534 end Analyze_Formal_Type_Declaration;
3536 ------------------------------------
3537 -- Analyze_Function_Instantiation --
3538 ------------------------------------
3540 procedure Analyze_Function_Instantiation (N : Node_Id) is
3542 Analyze_Subprogram_Instantiation (N, E_Function);
3543 end Analyze_Function_Instantiation;
3545 ---------------------------------
3546 -- Analyze_Generic_Access_Type --
3547 ---------------------------------
3549 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3553 if Nkind (Def) = N_Access_To_Object_Definition then
3554 Access_Type_Declaration (T, Def);
3556 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3557 and then No (Full_View (Designated_Type (T)))
3558 and then not Is_Generic_Type (Designated_Type (T))
3560 Error_Msg_N ("premature usage of incomplete type", Def);
3562 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3564 ("only a subtype mark is allowed in a formal", Def);
3568 Access_Subprogram_Declaration (T, Def);
3570 end Analyze_Generic_Access_Type;
3572 ---------------------------------
3573 -- Analyze_Generic_Formal_Part --
3574 ---------------------------------
3576 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3577 Gen_Parm_Decl : Node_Id;
3580 -- The generic formals are processed in the scope of the generic unit,
3581 -- where they are immediately visible. The scope is installed by the
3584 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3585 while Present (Gen_Parm_Decl) loop
3586 Analyze (Gen_Parm_Decl);
3587 Next (Gen_Parm_Decl);
3590 Generate_Reference_To_Generic_Formals (Current_Scope);
3592 -- For Ada 2020, some formal parameters can carry aspects, which must
3593 -- be name-resolved at the end of the list of formal parameters (which
3594 -- has the semantics of a declaration list).
3596 Analyze_Contracts (Generic_Formal_Declarations (N));
3597 end Analyze_Generic_Formal_Part;
3599 ------------------------------------------
3600 -- Analyze_Generic_Package_Declaration --
3601 ------------------------------------------
3603 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3604 Decls : constant List_Id := Visible_Declarations (Specification (N));
3605 Loc : constant Source_Ptr := Sloc (N);
3611 Save_Parent : Node_Id;
3614 -- A generic may grant access to its private enclosing context depending
3615 -- on the placement of its corresponding body. From elaboration point of
3616 -- view, the flow of execution may enter this private context, and then
3617 -- reach an external unit, thus producing a dependency on that external
3618 -- unit. For such a path to be properly discovered and encoded in the
3619 -- ALI file of the main unit, let the ABE mechanism process the body of
3620 -- the main unit, and encode all relevant invocation constructs and the
3621 -- relations between them.
3623 Mark_Save_Invocation_Graph_Of_Body;
3625 -- We introduce a renaming of the enclosing package, to have a usable
3626 -- entity as the prefix of an expanded name for a local entity of the
3627 -- form Par.P.Q, where P is the generic package. This is because a local
3628 -- entity named P may hide it, so that the usual visibility rules in
3629 -- the instance will not resolve properly.
3632 Make_Package_Renaming_Declaration (Loc,
3633 Defining_Unit_Name =>
3634 Make_Defining_Identifier (Loc,
3635 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3637 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3639 -- The declaration is inserted before other declarations, but before
3640 -- pragmas that may be library-unit pragmas and must appear before other
3641 -- declarations. The pragma Compile_Time_Error is not in this class, and
3642 -- may contain an expression that includes such a qualified name, so the
3643 -- renaming declaration must appear before it.
3645 -- Are there other pragmas that require this special handling ???
3647 if Present (Decls) then
3648 Decl := First (Decls);
3649 while Present (Decl)
3650 and then Nkind (Decl) = N_Pragma
3651 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3656 if Present (Decl) then
3657 Insert_Before (Decl, Renaming);
3659 Append (Renaming, Visible_Declarations (Specification (N)));
3663 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3666 -- Create copy of generic unit, and save for instantiation. If the unit
3667 -- is a child unit, do not copy the specifications for the parent, which
3668 -- are not part of the generic tree.
3670 Save_Parent := Parent_Spec (N);
3671 Set_Parent_Spec (N, Empty);
3673 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3674 Set_Parent_Spec (New_N, Save_Parent);
3677 -- Once the contents of the generic copy and the template are swapped,
3678 -- do the same for their respective aspect specifications.
3680 Exchange_Aspects (N, New_N);
3682 -- Collect all contract-related source pragmas found within the template
3683 -- and attach them to the contract of the package spec. This contract is
3684 -- used in the capture of global references within annotations.
3686 Create_Generic_Contract (N);
3688 Id := Defining_Entity (N);
3689 Generate_Definition (Id);
3691 -- Expansion is not applied to generic units
3696 Mutate_Ekind (Id, E_Generic_Package);
3697 Set_Etype (Id, Standard_Void_Type);
3699 -- Set SPARK_Mode from context
3701 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3702 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3703 Set_SPARK_Pragma_Inherited (Id);
3704 Set_SPARK_Aux_Pragma_Inherited (Id);
3706 -- Preserve relevant elaboration-related attributes of the context which
3707 -- are no longer available or very expensive to recompute once analysis,
3708 -- resolution, and expansion are over.
3710 Mark_Elaboration_Attributes
3715 -- Analyze aspects now, so that generated pragmas appear in the
3716 -- declarations before building and analyzing the generic copy.
3718 if Has_Aspects (N) then
3719 Analyze_Aspect_Specifications (N, Id);
3723 Enter_Generic_Scope (Id);
3724 Set_Inner_Instances (Id, New_Elmt_List);
3726 Set_Categorization_From_Pragmas (N);
3727 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3729 -- Link the declaration of the generic homonym in the generic copy to
3730 -- the package it renames, so that it is always resolved properly.
3732 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3733 Set_Entity (Associated_Node (Name (Renaming)), Id);
3735 -- For a library unit, we have reconstructed the entity for the unit,
3736 -- and must reset it in the library tables.
3738 if Nkind (Parent (N)) = N_Compilation_Unit then
3739 Set_Cunit_Entity (Current_Sem_Unit, Id);
3742 Analyze_Generic_Formal_Part (N);
3744 -- After processing the generic formals, analysis proceeds as for a
3745 -- non-generic package.
3747 Analyze (Specification (N));
3749 Validate_Categorization_Dependency (N, Id);
3753 End_Package_Scope (Id);
3754 Exit_Generic_Scope (Id);
3756 -- If the generic appears within a package unit, the body of that unit
3757 -- has to be present for instantiation and inlining.
3759 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3760 Set_Body_Needed_For_Inlining
3761 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3764 if Nkind (Parent (N)) /= N_Compilation_Unit then
3765 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3766 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3767 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3770 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3771 Validate_RT_RAT_Component (N);
3773 -- If this is a spec without a body, check that generic parameters
3776 if not Body_Required (Parent (N)) then
3777 Check_References (Id);
3781 -- If there is a specified storage pool in the context, create an
3782 -- aspect on the package declaration, so that it is used in any
3783 -- instance that does not override it.
3785 if Present (Default_Pool) then
3791 Make_Aspect_Specification (Loc,
3792 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3793 Expression => New_Copy (Default_Pool));
3795 if No (Aspect_Specifications (Specification (N))) then
3796 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3798 Append (ASN, Aspect_Specifications (Specification (N)));
3802 end Analyze_Generic_Package_Declaration;
3804 --------------------------------------------
3805 -- Analyze_Generic_Subprogram_Declaration --
3806 --------------------------------------------
3808 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3812 Result_Type : Entity_Id;
3813 Save_Parent : Node_Id;
3818 -- A generic may grant access to its private enclosing context depending
3819 -- on the placement of its corresponding body. From elaboration point of
3820 -- view, the flow of execution may enter this private context, and then
3821 -- reach an external unit, thus producing a dependency on that external
3822 -- unit. For such a path to be properly discovered and encoded in the
3823 -- ALI file of the main unit, let the ABE mechanism process the body of
3824 -- the main unit, and encode all relevant invocation constructs and the
3825 -- relations between them.
3827 Mark_Save_Invocation_Graph_Of_Body;
3829 -- Create copy of generic unit, and save for instantiation. If the unit
3830 -- is a child unit, do not copy the specifications for the parent, which
3831 -- are not part of the generic tree.
3833 Save_Parent := Parent_Spec (N);
3834 Set_Parent_Spec (N, Empty);
3836 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3837 Set_Parent_Spec (New_N, Save_Parent);
3840 -- Once the contents of the generic copy and the template are swapped,
3841 -- do the same for their respective aspect specifications.
3843 Exchange_Aspects (N, New_N);
3845 -- Collect all contract-related source pragmas found within the template
3846 -- and attach them to the contract of the subprogram spec. This contract
3847 -- is used in the capture of global references within annotations.
3849 Create_Generic_Contract (N);
3851 Spec := Specification (N);
3852 Id := Defining_Entity (Spec);
3853 Generate_Definition (Id);
3855 if Nkind (Id) = N_Defining_Operator_Symbol then
3857 ("operator symbol not allowed for generic subprogram", Id);
3863 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3866 Enter_Generic_Scope (Id);
3867 Set_Inner_Instances (Id, New_Elmt_List);
3868 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3870 Analyze_Generic_Formal_Part (N);
3872 if Nkind (Spec) = N_Function_Specification then
3873 Mutate_Ekind (Id, E_Generic_Function);
3875 Mutate_Ekind (Id, E_Generic_Procedure);
3878 -- Set SPARK_Mode from context
3880 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3881 Set_SPARK_Pragma_Inherited (Id);
3883 -- Preserve relevant elaboration-related attributes of the context which
3884 -- are no longer available or very expensive to recompute once analysis,
3885 -- resolution, and expansion are over.
3887 Mark_Elaboration_Attributes
3892 Formals := Parameter_Specifications (Spec);
3894 if Present (Formals) then
3895 Process_Formals (Formals, Spec);
3898 if Nkind (Spec) = N_Function_Specification then
3899 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3900 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3901 Set_Etype (Id, Result_Type);
3903 -- Check restriction imposed by AI05-073: a generic function
3904 -- cannot return an abstract type or an access to such.
3906 -- This is a binding interpretation should it apply to earlier
3907 -- versions of Ada as well as Ada 2012???
3909 if Is_Abstract_Type (Designated_Type (Result_Type))
3910 and then Ada_Version >= Ada_2012
3913 ("generic function cannot have an access result "
3914 & "that designates an abstract type", Spec);
3918 Find_Type (Result_Definition (Spec));
3919 Typ := Entity (Result_Definition (Spec));
3921 if Is_Abstract_Type (Typ)
3922 and then Ada_Version >= Ada_2012
3925 ("generic function cannot have abstract result type", Spec);
3928 -- If a null exclusion is imposed on the result type, then create
3929 -- a null-excluding itype (an access subtype) and use it as the
3930 -- function's Etype.
3932 if Is_Access_Type (Typ)
3933 and then Null_Exclusion_Present (Spec)
3936 Create_Null_Excluding_Itype
3938 Related_Nod => Spec,
3939 Scope_Id => Defining_Unit_Name (Spec)));
3941 Set_Etype (Id, Typ);
3946 Set_Etype (Id, Standard_Void_Type);
3949 -- Analyze the aspects of the generic copy to ensure that all generated
3950 -- pragmas (if any) perform their semantic effects.
3952 if Has_Aspects (N) then
3953 Analyze_Aspect_Specifications (N, Id);
3956 -- For a library unit, we have reconstructed the entity for the unit,
3957 -- and must reset it in the library tables. We also make sure that
3958 -- Body_Required is set properly in the original compilation unit node.
3960 if Nkind (Parent (N)) = N_Compilation_Unit then
3961 Set_Cunit_Entity (Current_Sem_Unit, Id);
3962 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3965 -- If the generic appears within a package unit, the body of that unit
3966 -- has to be present for instantiation and inlining.
3968 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3969 and then Unit_Requires_Body (Id)
3971 Set_Body_Needed_For_Inlining
3972 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3975 Set_Categorization_From_Pragmas (N);
3976 Validate_Categorization_Dependency (N, Id);
3978 -- Capture all global references that occur within the profile of the
3979 -- generic subprogram. Aspects are not part of this processing because
3980 -- they must be delayed. If processed now, Save_Global_References will
3981 -- destroy the Associated_Node links and prevent the capture of global
3982 -- references when the contract of the generic subprogram is analyzed.
3984 Save_Global_References (Original_Node (N));
3988 Exit_Generic_Scope (Id);
3989 Generate_Reference_To_Formals (Id);
3991 List_Inherited_Pre_Post_Aspects (Id);
3992 end Analyze_Generic_Subprogram_Declaration;
3994 -----------------------------------
3995 -- Analyze_Package_Instantiation --
3996 -----------------------------------
3998 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3999 -- must be replaced by gotos which jump to the end of the routine in order
4000 -- to restore the Ghost and SPARK modes.
4002 procedure Analyze_Package_Instantiation (N : Node_Id) is
4003 Has_Inline_Always : Boolean := False;
4004 -- Set if the generic unit contains any subprograms with Inline_Always.
4005 -- Only relevant when back-end inlining is not enabled.
4007 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4008 -- Return True if inlining is active and Gen_Unit contains inlined
4009 -- subprograms. In this case, we may either instantiate the body when
4010 -- front-end inlining is enabled, or add a pending instantiation when
4011 -- back-end inlining is enabled. In the former case, this may cause
4012 -- superfluous instantiations, but in either case we need to perform
4013 -- the instantiation of the body in the context of the instance and
4014 -- not in that of the point of inlining.
4016 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4017 -- Return True if Gen_Unit needs to have its body instantiated in the
4018 -- context of N. This in particular excludes generic contexts.
4020 -----------------------
4021 -- Might_Inline_Subp --
4022 -----------------------
4024 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4028 if Inline_Processing_Required then
4029 -- No need to recompute the answer if we know it is positive
4030 -- and back-end inlining is enabled.
4032 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4036 E := First_Entity (Gen_Unit);
4037 while Present (E) loop
4038 if Is_Subprogram (E) and then Is_Inlined (E) then
4039 -- Remember if there are any subprograms with Inline_Always
4041 if Has_Pragma_Inline_Always (E) then
4042 Has_Inline_Always := True;
4045 Set_Is_Inlined (Gen_Unit);
4054 end Might_Inline_Subp;
4056 -------------------------------
4057 -- Needs_Body_Instantiated --
4058 -------------------------------
4060 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4062 -- No need to instantiate bodies in generic units
4064 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4068 -- If the instantiation is in the main unit, then the body is needed
4070 if Is_In_Main_Unit (N) then
4074 -- In GNATprove mode, never instantiate bodies outside of the main
4075 -- unit, as it does not use frontend/backend inlining in the way that
4076 -- GNAT does, so does not benefit from such instantiations. On the
4077 -- contrary, such instantiations may bring artificial constraints,
4078 -- as for example such bodies may require preprocessing.
4080 if GNATprove_Mode then
4084 -- If not, then again no need to instantiate bodies in generic units
4086 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4090 -- Here we have a special handling for back-end inlining: if inline
4091 -- processing is required, then we unconditionally want to have the
4092 -- body instantiated. The reason is that Might_Inline_Subp does not
4093 -- catch all the cases (as it does not recurse into nested packages)
4094 -- so this avoids the need to patch things up afterwards. Moreover,
4095 -- these instantiations are only performed on demand when back-end
4096 -- inlining is enabled, so this causes very little extra work.
4098 if Inline_Processing_Required and then Back_End_Inlining then
4102 -- We want to have the bodies instantiated in non-main units if
4103 -- they might contribute inlined subprograms.
4105 return Might_Inline_Subp (Gen_Unit);
4106 end Needs_Body_Instantiated;
4108 -- Local declarations
4110 Gen_Id : constant Node_Id := Name (N);
4111 Inst_Id : constant Entity_Id := Defining_Entity (N);
4112 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4113 Loc : constant Source_Ptr := Sloc (N);
4115 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4116 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4117 Saved_ISMP : constant Boolean :=
4118 Ignore_SPARK_Mode_Pragmas_In_Instance;
4119 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4120 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4121 -- Save the Ghost and SPARK mode-related data to restore on exit
4123 Saved_Style_Check : constant Boolean := Style_Check;
4124 -- Save style check mode for restore on exit
4127 Act_Decl_Name : Node_Id;
4128 Act_Decl_Id : Entity_Id;
4131 Env_Installed : Boolean := False;
4134 Gen_Unit : Entity_Id;
4135 Inline_Now : Boolean := False;
4136 Needs_Body : Boolean;
4137 Parent_Installed : Boolean := False;
4138 Renaming_List : List_Id;
4139 Unit_Renaming : Node_Id;
4141 Vis_Prims_List : Elist_Id := No_Elist;
4142 -- List of primitives made temporarily visible in the instantiation
4143 -- to match the visibility of the formal type
4145 -- Start of processing for Analyze_Package_Instantiation
4148 -- Preserve relevant elaboration-related attributes of the context which
4149 -- are no longer available or very expensive to recompute once analysis,
4150 -- resolution, and expansion are over.
4152 Mark_Elaboration_Attributes
4159 -- Very first thing: check for Text_IO special unit in case we are
4160 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4162 Check_Text_IO_Special_Unit (Name (N));
4164 -- Make node global for error reporting
4166 Instantiation_Node := N;
4168 -- Case of instantiation of a generic package
4170 if Nkind (N) = N_Package_Instantiation then
4171 Act_Decl_Id := New_Copy (Defining_Entity (N));
4172 Set_Comes_From_Source (Act_Decl_Id, True);
4174 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4176 Make_Defining_Program_Unit_Name (Loc,
4178 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4179 Defining_Identifier => Act_Decl_Id);
4181 Act_Decl_Name := Act_Decl_Id;
4184 -- Case of instantiation of a formal package
4187 Act_Decl_Id := Defining_Identifier (N);
4188 Act_Decl_Name := Act_Decl_Id;
4191 Generate_Definition (Act_Decl_Id);
4192 Mutate_Ekind (Act_Decl_Id, E_Package);
4194 -- Initialize list of incomplete actuals before analysis
4196 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4198 Preanalyze_Actuals (N, Act_Decl_Id);
4200 -- Turn off style checking in instances. If the check is enabled on the
4201 -- generic unit, a warning in an instance would just be noise. If not
4202 -- enabled on the generic, then a warning in an instance is just wrong.
4203 -- This must be done after analyzing the actuals, which do come from
4204 -- source and are subject to style checking.
4206 Style_Check := False;
4209 Env_Installed := True;
4211 -- Reset renaming map for formal types. The mapping is established
4212 -- when analyzing the generic associations, but some mappings are
4213 -- inherited from formal packages of parent units, and these are
4214 -- constructed when the parents are installed.
4216 Generic_Renamings.Set_Last (0);
4217 Generic_Renamings_HTable.Reset;
4219 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4220 Gen_Unit := Entity (Gen_Id);
4222 -- A package instantiation is Ghost when it is subject to pragma Ghost
4223 -- or the generic template is Ghost. Set the mode now to ensure that
4224 -- any nodes generated during analysis and expansion are marked as
4227 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4229 -- Verify that it is the name of a generic package
4231 -- A visibility glitch: if the instance is a child unit and the generic
4232 -- is the generic unit of a parent instance (i.e. both the parent and
4233 -- the child units are instances of the same package) the name now
4234 -- denotes the renaming within the parent, not the intended generic
4235 -- unit. See if there is a homonym that is the desired generic. The
4236 -- renaming declaration must be visible inside the instance of the
4237 -- child, but not when analyzing the name in the instantiation itself.
4239 if Ekind (Gen_Unit) = E_Package
4240 and then Present (Renamed_Entity (Gen_Unit))
4241 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4242 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4243 and then Present (Homonym (Gen_Unit))
4245 Gen_Unit := Homonym (Gen_Unit);
4248 if Etype (Gen_Unit) = Any_Type then
4252 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4254 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4256 if From_Limited_With (Gen_Unit) then
4258 ("cannot instantiate a limited withed package", Gen_Id);
4261 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4268 if In_Extended_Main_Source_Unit (N) then
4269 Set_Is_Instantiated (Gen_Unit);
4270 Generate_Reference (Gen_Unit, N);
4272 if Present (Renamed_Object (Gen_Unit)) then
4273 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4274 Generate_Reference (Renamed_Object (Gen_Unit), N);
4278 if Nkind (Gen_Id) = N_Identifier
4279 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4282 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4284 elsif Nkind (Gen_Id) = N_Expanded_Name
4285 and then Is_Child_Unit (Gen_Unit)
4286 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4287 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4290 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4293 Set_Entity (Gen_Id, Gen_Unit);
4295 -- If generic is a renaming, get original generic unit
4297 if Present (Renamed_Object (Gen_Unit))
4298 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4300 Gen_Unit := Renamed_Object (Gen_Unit);
4303 -- Verify that there are no circular instantiations
4305 if In_Open_Scopes (Gen_Unit) then
4306 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4310 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4311 Error_Msg_Node_2 := Current_Scope;
4313 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4314 Circularity_Detected := True;
4319 Mutate_Ekind (Inst_Id, E_Package);
4320 Set_Scope (Inst_Id, Current_Scope);
4322 -- If the context of the instance is subject to SPARK_Mode "off" or
4323 -- the annotation is altogether missing, set the global flag which
4324 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4327 if SPARK_Mode /= On then
4328 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4330 -- Mark the instance spec in case the body is instantiated at a
4331 -- later pass. This preserves the original context in effect for
4334 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4337 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4338 Gen_Spec := Specification (Gen_Decl);
4340 -- Initialize renamings map, for error checking, and the list that
4341 -- holds private entities whose views have changed between generic
4342 -- definition and instantiation. If this is the instance created to
4343 -- validate an actual package, the instantiation environment is that
4344 -- of the enclosing instance.
4346 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4348 -- Copy original generic tree, to produce text for instantiation
4352 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4354 Act_Spec := Specification (Act_Tree);
4356 -- If this is the instance created to validate an actual package,
4357 -- only the formals matter, do not examine the package spec itself.
4359 if Is_Actual_Pack then
4360 Set_Visible_Declarations (Act_Spec, New_List);
4361 Set_Private_Declarations (Act_Spec, New_List);
4365 Analyze_Associations
4367 Formals => Generic_Formal_Declarations (Act_Tree),
4368 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4370 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4372 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4373 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4374 Set_Is_Generic_Instance (Act_Decl_Id);
4375 Set_Generic_Parent (Act_Spec, Gen_Unit);
4377 -- References to the generic in its own declaration or its body are
4378 -- references to the instance. Add a renaming declaration for the
4379 -- generic unit itself. This declaration, as well as the renaming
4380 -- declarations for the generic formals, must remain private to the
4381 -- unit: the formals, because this is the language semantics, and
4382 -- the unit because its use is an artifact of the implementation.
4385 Make_Package_Renaming_Declaration (Loc,
4386 Defining_Unit_Name =>
4387 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4388 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4390 Append (Unit_Renaming, Renaming_List);
4392 -- The renaming declarations are the first local declarations of the
4395 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4397 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4399 Set_Visible_Declarations (Act_Spec, Renaming_List);
4402 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4404 -- Propagate the aspect specifications from the package declaration
4405 -- template to the instantiated version of the package declaration.
4407 if Has_Aspects (Act_Tree) then
4408 Set_Aspect_Specifications (Act_Decl,
4409 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4412 -- The generic may have a generated Default_Storage_Pool aspect,
4413 -- set at the point of generic declaration. If the instance has
4414 -- that aspect, it overrides the one inherited from the generic.
4416 if Has_Aspects (Gen_Spec) then
4417 if No (Aspect_Specifications (N)) then
4418 Set_Aspect_Specifications (N,
4420 (Aspect_Specifications (Gen_Spec))));
4424 Inherited_Aspects : constant List_Id :=
4426 (Aspect_Specifications (Gen_Spec));
4430 Pool_Present : Boolean := False;
4433 ASN1 := First (Aspect_Specifications (N));
4434 while Present (ASN1) loop
4435 if Chars (Identifier (ASN1)) =
4436 Name_Default_Storage_Pool
4438 Pool_Present := True;
4445 if Pool_Present then
4447 -- If generic carries a default storage pool, remove it
4448 -- in favor of the instance one.
4450 ASN2 := First (Inherited_Aspects);
4451 while Present (ASN2) loop
4452 if Chars (Identifier (ASN2)) =
4453 Name_Default_Storage_Pool
4464 (Aspect_Specifications (N), Inherited_Aspects);
4469 -- Save the instantiation node for a subsequent instantiation of the
4470 -- body if there is one and it needs to be instantiated here.
4472 -- We instantiate the body only if we are generating code, or if we
4473 -- are generating cross-reference information, or for GNATprove use.
4476 Enclosing_Body_Present : Boolean := False;
4477 -- If the generic unit is not a compilation unit, then a body may
4478 -- be present in its parent even if none is required. We create a
4479 -- tentative pending instantiation for the body, which will be
4480 -- discarded if none is actually present.
4485 if Scope (Gen_Unit) /= Standard_Standard
4486 and then not Is_Child_Unit (Gen_Unit)
4488 Scop := Scope (Gen_Unit);
4489 while Present (Scop) and then Scop /= Standard_Standard loop
4490 if Unit_Requires_Body (Scop) then
4491 Enclosing_Body_Present := True;
4494 elsif In_Open_Scopes (Scop)
4495 and then In_Package_Body (Scop)
4497 Enclosing_Body_Present := True;
4501 exit when Is_Compilation_Unit (Scop);
4502 Scop := Scope (Scop);
4506 -- If front-end inlining is enabled or there are any subprograms
4507 -- marked with Inline_Always, and this is a unit for which code
4508 -- will be generated, we instantiate the body at once.
4510 -- This is done if the instance is not the main unit, and if the
4511 -- generic is not a child unit of another generic, to avoid scope
4512 -- problems and the reinstallation of parent instances.
4515 and then (not Is_Child_Unit (Gen_Unit)
4516 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4517 and then Might_Inline_Subp (Gen_Unit)
4518 and then not Is_Actual_Pack
4520 if not Back_End_Inlining
4521 and then (Front_End_Inlining or else Has_Inline_Always)
4522 and then (Is_In_Main_Unit (N)
4523 or else In_Main_Context (Current_Scope))
4524 and then Nkind (Parent (N)) /= N_Compilation_Unit
4528 -- In configurable_run_time mode we force the inlining of
4529 -- predefined subprograms marked Inline_Always, to minimize
4530 -- the use of the run-time library.
4532 elsif In_Predefined_Unit (Gen_Decl)
4533 and then Configurable_Run_Time_Mode
4534 and then Nkind (Parent (N)) /= N_Compilation_Unit
4539 -- If the current scope is itself an instance within a child
4540 -- unit, there will be duplications in the scope stack, and the
4541 -- unstacking mechanism in Inline_Instance_Body will fail.
4542 -- This loses some rare cases of optimization, and might be
4543 -- improved some day, if we can find a proper abstraction for
4544 -- "the complete compilation context" that can be saved and
4547 if Is_Generic_Instance (Current_Scope) then
4549 Curr_Unit : constant Entity_Id :=
4550 Cunit_Entity (Current_Sem_Unit);
4552 if Curr_Unit /= Current_Scope
4553 and then Is_Child_Unit (Curr_Unit)
4555 Inline_Now := False;
4562 (Unit_Requires_Body (Gen_Unit)
4563 or else Enclosing_Body_Present
4564 or else Present (Corresponding_Body (Gen_Decl)))
4565 and then Needs_Body_Instantiated (Gen_Unit)
4566 and then not Is_Actual_Pack
4567 and then not Inline_Now
4568 and then (Operating_Mode = Generate_Code
4569 or else (Operating_Mode = Check_Semantics
4570 and then GNATprove_Mode));
4572 -- If front-end inlining is enabled or there are any subprograms
4573 -- marked with Inline_Always, do not instantiate body when within
4574 -- a generic context.
4576 if not Back_End_Inlining
4577 and then (Front_End_Inlining or else Has_Inline_Always)
4578 and then not Expander_Active
4580 Needs_Body := False;
4583 -- If the current context is generic, and the package being
4584 -- instantiated is declared within a formal package, there is no
4585 -- body to instantiate until the enclosing generic is instantiated
4586 -- and there is an actual for the formal package. If the formal
4587 -- package has parameters, we build a regular package instance for
4588 -- it, that precedes the original formal package declaration.
4590 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4592 Decl : constant Node_Id :=
4594 (Unit_Declaration_Node (Scope (Gen_Unit)));
4596 if Nkind (Decl) = N_Formal_Package_Declaration
4597 or else (Nkind (Decl) = N_Package_Declaration
4598 and then Is_List_Member (Decl)
4599 and then Present (Next (Decl))
4601 Nkind (Next (Decl)) =
4602 N_Formal_Package_Declaration)
4604 Needs_Body := False;
4610 -- For RCI unit calling stubs, we omit the instance body if the
4611 -- instance is the RCI library unit itself.
4613 -- However there is a special case for nested instances: in this case
4614 -- we do generate the instance body, as it might be required, e.g.
4615 -- because it provides stream attributes for some type used in the
4616 -- profile of a remote subprogram. This is consistent with 12.3(12),
4617 -- which indicates that the instance body occurs at the place of the
4618 -- instantiation, and thus is part of the RCI declaration, which is
4619 -- present on all client partitions (this is E.2.3(18)).
4621 -- Note that AI12-0002 may make it illegal at some point to have
4622 -- stream attributes defined in an RCI unit, in which case this
4623 -- special case will become unnecessary. In the meantime, there
4624 -- is known application code in production that depends on this
4625 -- being possible, so we definitely cannot eliminate the body in
4626 -- the case of nested instances for the time being.
4628 -- When we generate a nested instance body, calling stubs for any
4629 -- relevant subprogram will be inserted immediately after the
4630 -- subprogram declarations, and will take precedence over the
4631 -- subsequent (original) body. (The stub and original body will be
4632 -- complete homographs, but this is permitted in an instance).
4633 -- (Could we do better and remove the original body???)
4635 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4636 and then Comes_From_Source (N)
4637 and then Nkind (Parent (N)) = N_Compilation_Unit
4639 Needs_Body := False;
4643 -- Indicate that the enclosing scopes contain an instantiation,
4644 -- and that cleanup actions should be delayed until after the
4645 -- instance body is expanded.
4647 Check_Forward_Instantiation (Gen_Decl);
4648 if Nkind (N) = N_Package_Instantiation then
4650 Enclosing_Master : Entity_Id;
4653 -- Loop to search enclosing masters
4655 Enclosing_Master := Current_Scope;
4656 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4657 if Ekind (Enclosing_Master) = E_Package then
4658 if Is_Compilation_Unit (Enclosing_Master) then
4659 if In_Package_Body (Enclosing_Master) then
4660 Set_Delay_Subprogram_Descriptors
4661 (Body_Entity (Enclosing_Master));
4663 Set_Delay_Subprogram_Descriptors
4670 Enclosing_Master := Scope (Enclosing_Master);
4673 elsif Is_Generic_Unit (Enclosing_Master)
4674 or else Ekind (Enclosing_Master) = E_Void
4676 -- Cleanup actions will eventually be performed on the
4677 -- enclosing subprogram or package instance, if any.
4678 -- Enclosing scope is void in the formal part of a
4679 -- generic subprogram.
4684 if Ekind (Enclosing_Master) = E_Entry
4686 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4688 if not Expander_Active then
4692 Protected_Body_Subprogram (Enclosing_Master);
4696 Set_Delay_Cleanups (Enclosing_Master);
4698 while Ekind (Enclosing_Master) = E_Block loop
4699 Enclosing_Master := Scope (Enclosing_Master);
4702 if Is_Subprogram (Enclosing_Master) then
4703 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4705 elsif Is_Task_Type (Enclosing_Master) then
4707 TBP : constant Node_Id :=
4708 Get_Task_Body_Procedure
4711 if Present (TBP) then
4712 Set_Delay_Subprogram_Descriptors (TBP);
4713 Set_Delay_Cleanups (TBP);
4720 end loop Scope_Loop;
4723 -- Make entry in table
4725 Add_Pending_Instantiation (N, Act_Decl);
4729 Set_Categorization_From_Pragmas (Act_Decl);
4731 if Parent_Installed then
4735 Set_Instance_Spec (N, Act_Decl);
4737 -- If not a compilation unit, insert the package declaration before
4738 -- the original instantiation node.
4740 if Nkind (Parent (N)) /= N_Compilation_Unit then
4741 Mark_Rewrite_Insertion (Act_Decl);
4742 Insert_Before (N, Act_Decl);
4744 if Has_Aspects (N) then
4745 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4747 -- The pragma created for a Default_Storage_Pool aspect must
4748 -- appear ahead of the declarations in the instance spec.
4749 -- Analysis has placed it after the instance node, so remove
4750 -- it and reinsert it properly now.
4753 ASN : constant Node_Id := First (Aspect_Specifications (N));
4754 A_Name : constant Name_Id := Chars (Identifier (ASN));
4758 if A_Name = Name_Default_Storage_Pool then
4759 if No (Visible_Declarations (Act_Spec)) then
4760 Set_Visible_Declarations (Act_Spec, New_List);
4764 while Present (Decl) loop
4765 if Nkind (Decl) = N_Pragma then
4767 Prepend (Decl, Visible_Declarations (Act_Spec));
4779 -- For an instantiation that is a compilation unit, place
4780 -- declaration on current node so context is complete for analysis
4781 -- (including nested instantiations). If this is the main unit,
4782 -- the declaration eventually replaces the instantiation node.
4783 -- If the instance body is created later, it replaces the
4784 -- instance node, and the declaration is attached to it
4785 -- (see Build_Instance_Compilation_Unit_Nodes).
4788 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4790 -- The entity for the current unit is the newly created one,
4791 -- and all semantic information is attached to it.
4793 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4795 -- If this is the main unit, replace the main entity as well
4797 if Current_Sem_Unit = Main_Unit then
4798 Main_Unit_Entity := Act_Decl_Id;
4802 Set_Unit (Parent (N), Act_Decl);
4803 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4804 Set_Package_Instantiation (Act_Decl_Id, N);
4806 -- Process aspect specifications of the instance node, if any, to
4807 -- take into account categorization pragmas before analyzing the
4810 if Has_Aspects (N) then
4811 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4815 Set_Unit (Parent (N), N);
4816 Set_Body_Required (Parent (N), False);
4818 -- We never need elaboration checks on instantiations, since by
4819 -- definition, the body instantiation is elaborated at the same
4820 -- time as the spec instantiation.
4822 if Legacy_Elaboration_Checks then
4823 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4824 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4828 if Legacy_Elaboration_Checks then
4829 Check_Elab_Instantiation (N);
4832 -- Save the scenario for later examination by the ABE Processing
4835 Record_Elaboration_Scenario (N);
4837 -- The instantiation results in a guaranteed ABE
4839 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4840 -- Do not instantiate the corresponding body because gigi cannot
4841 -- handle certain types of premature instantiations.
4843 Remove_Dead_Instance (N);
4845 -- Create completing bodies for all subprogram declarations since
4846 -- their real bodies will not be instantiated.
4848 Provide_Completing_Bodies (Instance_Spec (N));
4851 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4853 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4854 First_Private_Entity (Act_Decl_Id));
4856 -- If the instantiation will receive a body, the unit will be
4857 -- transformed into a package body, and receive its own elaboration
4858 -- entity. Otherwise, the nature of the unit is now a package
4861 if Nkind (Parent (N)) = N_Compilation_Unit
4862 and then not Needs_Body
4864 Rewrite (N, Act_Decl);
4867 if Present (Corresponding_Body (Gen_Decl))
4868 or else Unit_Requires_Body (Gen_Unit)
4870 Set_Has_Completion (Act_Decl_Id);
4873 Check_Formal_Packages (Act_Decl_Id);
4875 Restore_Hidden_Primitives (Vis_Prims_List);
4876 Restore_Private_Views (Act_Decl_Id);
4878 Inherit_Context (Gen_Decl, N);
4880 if Parent_Installed then
4885 Env_Installed := False;
4888 Validate_Categorization_Dependency (N, Act_Decl_Id);
4890 -- There used to be a check here to prevent instantiations in local
4891 -- contexts if the No_Local_Allocators restriction was active. This
4892 -- check was removed by a binding interpretation in AI-95-00130/07,
4893 -- but we retain the code for documentation purposes.
4895 -- if Ekind (Act_Decl_Id) /= E_Void
4896 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4898 -- Check_Restriction (No_Local_Allocators, N);
4902 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4905 -- Check that if N is an instantiation of System.Dim_Float_IO or
4906 -- System.Dim_Integer_IO, the formal type has a dimension system.
4908 if Nkind (N) = N_Package_Instantiation
4909 and then Is_Dim_IO_Package_Instantiation (N)
4912 Assoc : constant Node_Id := First (Generic_Associations (N));
4914 if not Has_Dimension_System
4915 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4917 Error_Msg_N ("type with a dimension system expected", Assoc);
4923 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4924 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4927 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4928 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4929 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4930 Style_Check := Saved_Style_Check;
4933 when Instantiation_Error =>
4934 if Parent_Installed then
4938 if Env_Installed then
4942 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4943 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4944 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4945 Style_Check := Saved_Style_Check;
4946 end Analyze_Package_Instantiation;
4948 --------------------------
4949 -- Inline_Instance_Body --
4950 --------------------------
4952 -- WARNING: This routine manages SPARK regions. Return statements must be
4953 -- replaced by gotos which jump to the end of the routine and restore the
4956 procedure Inline_Instance_Body
4958 Gen_Unit : Entity_Id;
4961 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4963 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4964 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4965 Gen_Comp : constant Entity_Id :=
4966 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4968 Scope_Stack_Depth : constant Pos :=
4969 Scope_Stack.Last - Scope_Stack.First + 1;
4971 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4972 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4973 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4975 Curr_Scope : Entity_Id := Empty;
4976 List : Elist_Id := No_Elist; -- init to avoid warning
4977 N_Instances : Nat := 0;
4978 Num_Inner : Nat := 0;
4979 Num_Scopes : Nat := 0;
4980 Removed : Boolean := False;
4985 -- Case of generic unit defined in another unit. We must remove the
4986 -- complete context of the current unit to install that of the generic.
4988 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4990 -- Add some comments for the following two loops ???
4993 while Present (S) and then S /= Standard_Standard loop
4995 Num_Scopes := Num_Scopes + 1;
4997 Use_Clauses (Num_Scopes) :=
4999 (Scope_Stack.Last - Num_Scopes + 1).
5001 End_Use_Clauses (Use_Clauses (Num_Scopes));
5003 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5004 or else Scope_Stack.Table
5005 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5008 exit when Is_Generic_Instance (S)
5009 and then (In_Package_Body (S)
5010 or else Ekind (S) = E_Procedure
5011 or else Ekind (S) = E_Function);
5015 Vis := Is_Immediately_Visible (Gen_Comp);
5017 -- Find and save all enclosing instances
5022 and then S /= Standard_Standard
5024 if Is_Generic_Instance (S) then
5025 N_Instances := N_Instances + 1;
5026 Instances (N_Instances) := S;
5028 exit when In_Package_Body (S);
5034 -- Remove context of current compilation unit, unless we are within a
5035 -- nested package instantiation, in which case the context has been
5036 -- removed previously.
5038 -- If current scope is the body of a child unit, remove context of
5039 -- spec as well. If an enclosing scope is an instance body, the
5040 -- context has already been removed, but the entities in the body
5041 -- must be made invisible as well.
5044 while Present (S) and then S /= Standard_Standard loop
5045 if Is_Generic_Instance (S)
5046 and then (In_Package_Body (S)
5047 or else Ekind (S) in E_Procedure | E_Function)
5049 -- We still have to remove the entities of the enclosing
5050 -- instance from direct visibility.
5055 E := First_Entity (S);
5056 while Present (E) loop
5057 Set_Is_Immediately_Visible (E, False);
5066 or else (Ekind (Curr_Unit) = E_Package_Body
5067 and then S = Spec_Entity (Curr_Unit))
5068 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5069 and then S = Corresponding_Spec
5070 (Unit_Declaration_Node (Curr_Unit)))
5074 -- Remove entities in current scopes from visibility, so that
5075 -- instance body is compiled in a clean environment.
5077 List := Save_Scope_Stack (Handle_Use => False);
5079 if Is_Child_Unit (S) then
5081 -- Remove child unit from stack, as well as inner scopes.
5082 -- Removing the context of a child unit removes parent units
5085 while Current_Scope /= S loop
5086 Num_Inner := Num_Inner + 1;
5087 Inner_Scopes (Num_Inner) := Current_Scope;
5092 Remove_Context (Curr_Comp);
5096 Remove_Context (Curr_Comp);
5099 if Ekind (Curr_Unit) = E_Package_Body then
5100 Remove_Context (Library_Unit (Curr_Comp));
5107 pragma Assert (Num_Inner < Num_Scopes);
5109 Push_Scope (Standard_Standard);
5110 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5112 -- The inlined package body is analyzed with the configuration state
5113 -- of the context prior to the scope manipulations performed above.
5115 -- ??? shouldn't this also use the warning state of the context prior
5116 -- to the scope manipulations?
5118 Instantiate_Package_Body
5120 ((Act_Decl => Act_Decl,
5121 Config_Switches => Config_Attrs,
5122 Current_Sem_Unit => Current_Sem_Unit,
5123 Expander_Status => Expander_Active,
5125 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5126 Scope_Suppress => Scope_Suppress,
5127 Warnings => Save_Warnings)),
5128 Inlined_Body => True);
5134 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5136 -- Reset Generic_Instance flag so that use clauses can be installed
5137 -- in the proper order. (See Use_One_Package for effect of enclosing
5138 -- instances on processing of use clauses).
5140 for J in 1 .. N_Instances loop
5141 Set_Is_Generic_Instance (Instances (J), False);
5145 Install_Context (Curr_Comp, Chain => False);
5147 if Present (Curr_Scope)
5148 and then Is_Child_Unit (Curr_Scope)
5150 Push_Scope (Curr_Scope);
5151 Set_Is_Immediately_Visible (Curr_Scope);
5153 -- Finally, restore inner scopes as well
5155 for J in reverse 1 .. Num_Inner loop
5156 Push_Scope (Inner_Scopes (J));
5160 Restore_Scope_Stack (List, Handle_Use => False);
5162 if Present (Curr_Scope)
5164 (In_Private_Part (Curr_Scope)
5165 or else In_Package_Body (Curr_Scope))
5167 -- Install private declaration of ancestor units, which are
5168 -- currently available. Restore_Scope_Stack and Install_Context
5169 -- only install the visible part of parents.
5174 Par := Scope (Curr_Scope);
5175 while (Present (Par)) and then Par /= Standard_Standard loop
5176 Install_Private_Declarations (Par);
5183 -- Restore use clauses. For a child unit, use clauses in the parents
5184 -- are restored when installing the context, so only those in inner
5185 -- scopes (and those local to the child unit itself) need to be
5186 -- installed explicitly.
5188 if Is_Child_Unit (Curr_Unit) and then Removed then
5189 for J in reverse 1 .. Num_Inner + 1 loop
5190 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5192 Install_Use_Clauses (Use_Clauses (J));
5196 for J in reverse 1 .. Num_Scopes loop
5197 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5199 Install_Use_Clauses (Use_Clauses (J));
5203 -- Restore status of instances. If one of them is a body, make its
5204 -- local entities visible again.
5211 for J in 1 .. N_Instances loop
5212 Inst := Instances (J);
5213 Set_Is_Generic_Instance (Inst, True);
5215 if In_Package_Body (Inst)
5216 or else Ekind (S) in E_Procedure | E_Function
5218 E := First_Entity (Instances (J));
5219 while Present (E) loop
5220 Set_Is_Immediately_Visible (E);
5227 -- If generic unit is in current unit, current context is correct. Note
5228 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5229 -- enclosing scopes were removed.
5232 Instantiate_Package_Body
5234 ((Act_Decl => Act_Decl,
5235 Config_Switches => Save_Config_Switches,
5236 Current_Sem_Unit => Current_Sem_Unit,
5237 Expander_Status => Expander_Active,
5239 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5240 Scope_Suppress => Scope_Suppress,
5241 Warnings => Save_Warnings)),
5242 Inlined_Body => True);
5244 end Inline_Instance_Body;
5246 -------------------------------------
5247 -- Analyze_Procedure_Instantiation --
5248 -------------------------------------
5250 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5252 Analyze_Subprogram_Instantiation (N, E_Procedure);
5253 end Analyze_Procedure_Instantiation;
5255 -----------------------------------
5256 -- Need_Subprogram_Instance_Body --
5257 -----------------------------------
5259 function Need_Subprogram_Instance_Body
5261 Subp : Entity_Id) return Boolean
5263 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5264 -- Return True if E is an inlined subprogram, an inlined renaming or a
5265 -- subprogram nested in an inlined subprogram. The inlining machinery
5266 -- totally disregards nested subprograms since it considers that they
5267 -- will always be compiled if the parent is (see Inline.Is_Nested).
5269 ------------------------------------
5270 -- Is_Inlined_Or_Child_Of_Inlined --
5271 ------------------------------------
5273 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5277 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5282 while Scop /= Standard_Standard loop
5283 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5287 Scop := Scope (Scop);
5291 end Is_Inlined_Or_Child_Of_Inlined;
5294 -- Must be in the main unit or inlined (or child of inlined)
5296 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5298 -- Must be generating code or analyzing code in GNATprove mode
5300 and then (Operating_Mode = Generate_Code
5301 or else (Operating_Mode = Check_Semantics
5302 and then GNATprove_Mode))
5304 -- The body is needed when generating code (full expansion) and in
5305 -- in GNATprove mode (special expansion) for formal verification of
5308 and then (Expander_Active or GNATprove_Mode)
5310 -- No point in inlining if ABE is inevitable
5312 and then not Is_Known_Guaranteed_ABE (N)
5314 -- Or if subprogram is eliminated
5316 and then not Is_Eliminated (Subp)
5318 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5321 -- Here if not inlined, or we ignore the inlining
5326 end Need_Subprogram_Instance_Body;
5328 --------------------------------------
5329 -- Analyze_Subprogram_Instantiation --
5330 --------------------------------------
5332 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5333 -- must be replaced by gotos which jump to the end of the routine in order
5334 -- to restore the Ghost and SPARK modes.
5336 procedure Analyze_Subprogram_Instantiation
5340 Errs : constant Nat := Serious_Errors_Detected;
5341 Gen_Id : constant Node_Id := Name (N);
5342 Inst_Id : constant Entity_Id := Defining_Entity (N);
5343 Anon_Id : constant Entity_Id :=
5344 Make_Defining_Identifier (Sloc (Inst_Id),
5345 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5346 Loc : constant Source_Ptr := Sloc (N);
5348 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5353 Env_Installed : Boolean := False;
5354 Gen_Unit : Entity_Id;
5356 Pack_Id : Entity_Id;
5357 Parent_Installed : Boolean := False;
5359 Renaming_List : List_Id;
5360 -- The list of declarations that link formals and actuals of the
5361 -- instance. These are subtype declarations for formal types, and
5362 -- renaming declarations for other formals. The subprogram declaration
5363 -- for the instance is then appended to the list, and the last item on
5364 -- the list is the renaming declaration for the instance.
5366 procedure Analyze_Instance_And_Renamings;
5367 -- The instance must be analyzed in a context that includes the mappings
5368 -- of generic parameters into actuals. We create a package declaration
5369 -- for this purpose, and a subprogram with an internal name within the
5370 -- package. The subprogram instance is simply an alias for the internal
5371 -- subprogram, declared in the current scope.
5373 procedure Build_Subprogram_Renaming;
5374 -- If the subprogram is recursive, there are occurrences of the name of
5375 -- the generic within the body, which must resolve to the current
5376 -- instance. We add a renaming declaration after the declaration, which
5377 -- is available in the instance body, as well as in the analysis of
5378 -- aspects that appear in the generic. This renaming declaration is
5379 -- inserted after the instance declaration which it renames.
5381 ------------------------------------
5382 -- Analyze_Instance_And_Renamings --
5383 ------------------------------------
5385 procedure Analyze_Instance_And_Renamings is
5386 Def_Ent : constant Entity_Id := Defining_Entity (N);
5387 Pack_Decl : Node_Id;
5390 if Nkind (Parent (N)) = N_Compilation_Unit then
5392 -- For the case of a compilation unit, the container package has
5393 -- the same name as the instantiation, to insure that the binder
5394 -- calls the elaboration procedure with the right name. Copy the
5395 -- entity of the instance, which may have compilation level flags
5396 -- (e.g. Is_Child_Unit) set.
5398 Pack_Id := New_Copy (Def_Ent);
5401 -- Otherwise we use the name of the instantiation concatenated
5402 -- with its source position to ensure uniqueness if there are
5403 -- several instantiations with the same name.
5406 Make_Defining_Identifier (Loc,
5407 Chars => New_External_Name
5408 (Related_Id => Chars (Def_Ent),
5410 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5414 Make_Package_Declaration (Loc,
5415 Specification => Make_Package_Specification (Loc,
5416 Defining_Unit_Name => Pack_Id,
5417 Visible_Declarations => Renaming_List,
5418 End_Label => Empty));
5420 Set_Instance_Spec (N, Pack_Decl);
5421 Set_Is_Generic_Instance (Pack_Id);
5422 Set_Debug_Info_Needed (Pack_Id);
5424 -- Case of not a compilation unit
5426 if Nkind (Parent (N)) /= N_Compilation_Unit then
5427 Mark_Rewrite_Insertion (Pack_Decl);
5428 Insert_Before (N, Pack_Decl);
5429 Set_Has_Completion (Pack_Id);
5431 -- Case of an instantiation that is a compilation unit
5433 -- Place declaration on current node so context is complete for
5434 -- analysis (including nested instantiations), and for use in a
5435 -- context_clause (see Analyze_With_Clause).
5438 Set_Unit (Parent (N), Pack_Decl);
5439 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5442 Analyze (Pack_Decl);
5443 Check_Formal_Packages (Pack_Id);
5445 -- Body of the enclosing package is supplied when instantiating the
5446 -- subprogram body, after semantic analysis is completed.
5448 if Nkind (Parent (N)) = N_Compilation_Unit then
5450 -- Remove package itself from visibility, so it does not
5451 -- conflict with subprogram.
5453 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5455 -- Set name and scope of internal subprogram so that the proper
5456 -- external name will be generated. The proper scope is the scope
5457 -- of the wrapper package. We need to generate debugging info for
5458 -- the internal subprogram, so set flag accordingly.
5460 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5461 Set_Scope (Anon_Id, Scope (Pack_Id));
5463 -- Mark wrapper package as referenced, to avoid spurious warnings
5464 -- if the instantiation appears in various with_ clauses of
5465 -- subunits of the main unit.
5467 Set_Referenced (Pack_Id);
5470 Set_Is_Generic_Instance (Anon_Id);
5471 Set_Debug_Info_Needed (Anon_Id);
5472 Act_Decl_Id := New_Copy (Anon_Id);
5474 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5475 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5476 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5478 -- Subprogram instance comes from source only if generic does
5480 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5482 -- If the instance is a child unit, mark the Id accordingly. Mark
5483 -- the anonymous entity as well, which is the real subprogram and
5484 -- which is used when the instance appears in a context clause.
5485 -- Similarly, propagate the Is_Eliminated flag to handle properly
5486 -- nested eliminated subprograms.
5488 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5489 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5490 New_Overloaded_Entity (Act_Decl_Id);
5491 Check_Eliminated (Act_Decl_Id);
5492 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5494 if Nkind (Parent (N)) = N_Compilation_Unit then
5496 -- In compilation unit case, kill elaboration checks on the
5497 -- instantiation, since they are never needed - the body is
5498 -- instantiated at the same point as the spec.
5500 if Legacy_Elaboration_Checks then
5501 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5502 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5505 Set_Is_Compilation_Unit (Anon_Id);
5506 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5509 -- The instance is not a freezing point for the new subprogram.
5510 -- The anonymous subprogram may have a freeze node, created for
5511 -- some delayed aspects. This freeze node must not be inherited
5512 -- by the visible subprogram entity.
5514 Set_Is_Frozen (Act_Decl_Id, False);
5515 Set_Freeze_Node (Act_Decl_Id, Empty);
5517 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5518 Valid_Operator_Definition (Act_Decl_Id);
5521 Set_Alias (Act_Decl_Id, Anon_Id);
5522 Set_Has_Completion (Act_Decl_Id);
5523 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5525 if Nkind (Parent (N)) = N_Compilation_Unit then
5526 Set_Body_Required (Parent (N), False);
5528 end Analyze_Instance_And_Renamings;
5530 -------------------------------
5531 -- Build_Subprogram_Renaming --
5532 -------------------------------
5534 procedure Build_Subprogram_Renaming is
5535 Renaming_Decl : Node_Id;
5536 Unit_Renaming : Node_Id;
5540 Make_Subprogram_Renaming_Declaration (Loc,
5543 (Specification (Original_Node (Gen_Decl)),
5545 Instantiating => True),
5546 Name => New_Occurrence_Of (Anon_Id, Loc));
5548 -- The generic may be a child unit. The renaming needs an identifier
5549 -- with the proper name.
5551 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5552 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5554 -- If there is a formal subprogram with the same name as the unit
5555 -- itself, do not add this renaming declaration, to prevent
5556 -- ambiguities when there is a call with that name in the body.
5557 -- This is a partial and ugly fix for one ACATS test. ???
5559 Renaming_Decl := First (Renaming_List);
5560 while Present (Renaming_Decl) loop
5561 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5563 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5568 Next (Renaming_Decl);
5571 if No (Renaming_Decl) then
5572 Append (Unit_Renaming, Renaming_List);
5574 end Build_Subprogram_Renaming;
5578 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5579 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5580 Saved_ISMP : constant Boolean :=
5581 Ignore_SPARK_Mode_Pragmas_In_Instance;
5582 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5583 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5584 -- Save the Ghost and SPARK mode-related data to restore on exit
5586 Vis_Prims_List : Elist_Id := No_Elist;
5587 -- List of primitives made temporarily visible in the instantiation
5588 -- to match the visibility of the formal type
5590 -- Start of processing for Analyze_Subprogram_Instantiation
5593 -- Preserve relevant elaboration-related attributes of the context which
5594 -- are no longer available or very expensive to recompute once analysis,
5595 -- resolution, and expansion are over.
5597 Mark_Elaboration_Attributes
5604 -- Very first thing: check for special Text_IO unit in case we are
5605 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5606 -- such an instantiation is bogus (these are packages, not subprograms),
5607 -- but we get a better error message if we do this.
5609 Check_Text_IO_Special_Unit (Gen_Id);
5611 -- Make node global for error reporting
5613 Instantiation_Node := N;
5615 -- For package instantiations we turn off style checks, because they
5616 -- will have been emitted in the generic. For subprogram instantiations
5617 -- we want to apply at least the check on overriding indicators so we
5618 -- do not modify the style check status.
5620 -- The renaming declarations for the actuals do not come from source and
5621 -- will not generate spurious warnings.
5623 Preanalyze_Actuals (N);
5626 Env_Installed := True;
5627 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5628 Gen_Unit := Entity (Gen_Id);
5630 -- A subprogram instantiation is Ghost when it is subject to pragma
5631 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5632 -- that any nodes generated during analysis and expansion are marked as
5635 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5637 Generate_Reference (Gen_Unit, Gen_Id);
5639 if Nkind (Gen_Id) = N_Identifier
5640 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5643 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5646 if Etype (Gen_Unit) = Any_Type then
5651 -- Verify that it is a generic subprogram of the right kind, and that
5652 -- it does not lead to a circular instantiation.
5654 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5656 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5658 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5660 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5662 elsif In_Open_Scopes (Gen_Unit) then
5663 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5666 Mutate_Ekind (Inst_Id, K);
5667 Set_Scope (Inst_Id, Current_Scope);
5669 Set_Entity (Gen_Id, Gen_Unit);
5670 Set_Is_Instantiated (Gen_Unit);
5672 if In_Extended_Main_Source_Unit (N) then
5673 Generate_Reference (Gen_Unit, N);
5676 -- If renaming, get original unit
5678 if Present (Renamed_Object (Gen_Unit))
5679 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5681 Gen_Unit := Renamed_Object (Gen_Unit);
5682 Set_Is_Instantiated (Gen_Unit);
5683 Generate_Reference (Gen_Unit, N);
5686 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5687 Error_Msg_Node_2 := Current_Scope;
5689 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5690 Circularity_Detected := True;
5691 Restore_Hidden_Primitives (Vis_Prims_List);
5695 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5697 -- Initialize renamings map, for error checking
5699 Generic_Renamings.Set_Last (0);
5700 Generic_Renamings_HTable.Reset;
5702 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5704 -- Copy original generic tree, to produce text for instantiation
5708 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5710 -- Inherit overriding indicator from instance node
5712 Act_Spec := Specification (Act_Tree);
5713 Set_Must_Override (Act_Spec, Must_Override (N));
5714 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5717 Analyze_Associations
5719 Formals => Generic_Formal_Declarations (Act_Tree),
5720 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5722 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5724 -- The subprogram itself cannot contain a nested instance, so the
5725 -- current parent is left empty.
5727 Set_Instance_Env (Gen_Unit, Empty);
5729 -- Build the subprogram declaration, which does not appear in the
5730 -- generic template, and give it a sloc consistent with that of the
5733 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5734 Set_Generic_Parent (Act_Spec, Gen_Unit);
5736 Make_Subprogram_Declaration (Sloc (Act_Spec),
5737 Specification => Act_Spec);
5739 -- The aspects have been copied previously, but they have to be
5740 -- linked explicitly to the new subprogram declaration. Explicit
5741 -- pre/postconditions on the instance are analyzed below, in a
5744 Move_Aspects (Act_Tree, To => Act_Decl);
5745 Set_Categorization_From_Pragmas (Act_Decl);
5747 if Parent_Installed then
5751 Append (Act_Decl, Renaming_List);
5753 -- Contract-related source pragmas that follow a generic subprogram
5754 -- must be instantiated explicitly because they are not part of the
5755 -- subprogram template.
5757 Instantiate_Subprogram_Contract
5758 (Original_Node (Gen_Decl), Renaming_List);
5760 Build_Subprogram_Renaming;
5762 -- If the context of the instance is subject to SPARK_Mode "off" or
5763 -- the annotation is altogether missing, set the global flag which
5764 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5765 -- the instance. This should be done prior to analyzing the instance.
5767 if SPARK_Mode /= On then
5768 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5771 -- If the context of an instance is not subject to SPARK_Mode "off",
5772 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5773 -- the latter should be the one applicable to the instance.
5775 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5776 and then Saved_SM /= Off
5777 and then Present (SPARK_Pragma (Gen_Unit))
5779 Set_SPARK_Mode (Gen_Unit);
5782 Analyze_Instance_And_Renamings;
5784 -- Restore SPARK_Mode from the context after analysis of the package
5785 -- declaration, so that the SPARK_Mode on the generic spec does not
5786 -- apply to the pending instance for the instance body.
5788 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5789 and then Saved_SM /= Off
5790 and then Present (SPARK_Pragma (Gen_Unit))
5792 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5795 -- If the generic is marked Import (Intrinsic), then so is the
5796 -- instance. This indicates that there is no body to instantiate. If
5797 -- generic is marked inline, so it the instance, and the anonymous
5798 -- subprogram it renames. If inlined, or else if inlining is enabled
5799 -- for the compilation, we generate the instance body even if it is
5800 -- not within the main unit.
5802 if Is_Intrinsic_Subprogram (Gen_Unit) then
5803 Set_Is_Intrinsic_Subprogram (Anon_Id);
5804 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5806 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5807 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5811 -- Inherit convention from generic unit. Intrinsic convention, as for
5812 -- an instance of unchecked conversion, is not inherited because an
5813 -- explicit Ada instance has been created.
5815 if Has_Convention_Pragma (Gen_Unit)
5816 and then Convention (Gen_Unit) /= Convention_Intrinsic
5818 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5819 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5822 Generate_Definition (Act_Decl_Id);
5824 -- Inherit all inlining-related flags which apply to the generic in
5825 -- the subprogram and its declaration.
5827 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5828 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5830 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5831 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5833 Set_Has_Pragma_Inline_Always
5834 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5835 Set_Has_Pragma_Inline_Always
5836 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5838 Set_Has_Pragma_No_Inline
5839 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5840 Set_Has_Pragma_No_Inline
5841 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5843 -- Propagate No_Return if pragma applied to generic unit. This must
5844 -- be done explicitly because pragma does not appear in generic
5845 -- declaration (unlike the aspect case).
5847 if No_Return (Gen_Unit) then
5848 Set_No_Return (Act_Decl_Id);
5849 Set_No_Return (Anon_Id);
5852 -- Mark both the instance spec and the anonymous package in case the
5853 -- body is instantiated at a later pass. This preserves the original
5854 -- context in effect for the body.
5856 if SPARK_Mode /= On then
5857 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5858 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5861 if Legacy_Elaboration_Checks
5862 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5864 Check_Elab_Instantiation (N);
5867 -- Save the scenario for later examination by the ABE Processing
5870 Record_Elaboration_Scenario (N);
5872 -- The instantiation results in a guaranteed ABE. Create a completing
5873 -- body for the subprogram declaration because the real body will not
5876 if Is_Known_Guaranteed_ABE (N) then
5877 Provide_Completing_Bodies (Instance_Spec (N));
5880 if Is_Dispatching_Operation (Act_Decl_Id)
5881 and then Ada_Version >= Ada_2005
5887 Formal := First_Formal (Act_Decl_Id);
5888 while Present (Formal) loop
5889 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5890 and then Is_Controlling_Formal (Formal)
5891 and then not Can_Never_Be_Null (Formal)
5894 ("access parameter& is controlling,", N, Formal);
5896 ("\corresponding parameter of & must be explicitly "
5897 & "null-excluding", N, Gen_Id);
5900 Next_Formal (Formal);
5905 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5907 Validate_Categorization_Dependency (N, Act_Decl_Id);
5909 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5910 Inherit_Context (Gen_Decl, N);
5912 Restore_Private_Views (Pack_Id, False);
5914 -- If the context requires a full instantiation, mark node for
5915 -- subsequent construction of the body.
5917 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5918 Check_Forward_Instantiation (Gen_Decl);
5920 -- The wrapper package is always delayed, because it does not
5921 -- constitute a freeze point, but to insure that the freeze node
5922 -- is placed properly, it is created directly when instantiating
5923 -- the body (otherwise the freeze node might appear to early for
5924 -- nested instantiations).
5926 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5927 Rewrite (N, Unit (Parent (N)));
5928 Set_Unit (Parent (N), N);
5931 -- Replace instance node for library-level instantiations of
5932 -- intrinsic subprograms.
5934 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5935 Rewrite (N, Unit (Parent (N)));
5936 Set_Unit (Parent (N), N);
5939 if Parent_Installed then
5943 Restore_Hidden_Primitives (Vis_Prims_List);
5945 Env_Installed := False;
5946 Generic_Renamings.Set_Last (0);
5947 Generic_Renamings_HTable.Reset;
5951 -- Analyze aspects in declaration if no errors appear in the instance.
5953 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5954 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5957 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5958 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5959 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5962 when Instantiation_Error =>
5963 if Parent_Installed then
5967 if Env_Installed then
5971 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5972 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5973 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5974 end Analyze_Subprogram_Instantiation;
5976 -------------------------
5977 -- Get_Associated_Node --
5978 -------------------------
5980 function Get_Associated_Node (N : Node_Id) return Node_Id is
5984 Assoc := Associated_Node (N);
5986 if Nkind (Assoc) /= Nkind (N) then
5989 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5993 -- If the node is part of an inner generic, it may itself have been
5994 -- remapped into a further generic copy. Associated_Node is otherwise
5995 -- used for the entity of the node, and will be of a different node
5996 -- kind, or else N has been rewritten as a literal or function call.
5998 while Present (Associated_Node (Assoc))
5999 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6001 Assoc := Associated_Node (Assoc);
6004 -- Follow an additional link in case the final node was rewritten.
6005 -- This can only happen with nested generic units.
6007 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6008 and then Present (Associated_Node (Assoc))
6009 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6010 | N_Explicit_Dereference
6015 Assoc := Associated_Node (Assoc);
6018 -- An additional special case: an unconstrained type in an object
6019 -- declaration may have been rewritten as a local subtype constrained
6020 -- by the expression in the declaration. We need to recover the
6021 -- original entity, which may be global.
6023 if Present (Original_Node (Assoc))
6024 and then Nkind (Parent (N)) = N_Object_Declaration
6026 Assoc := Original_Node (Assoc);
6031 end Get_Associated_Node;
6033 ----------------------------
6034 -- Build_Function_Wrapper --
6035 ----------------------------
6037 function Build_Function_Wrapper
6038 (Formal_Subp : Entity_Id;
6039 Actual_Subp : Entity_Id) return Node_Id
6041 Loc : constant Source_Ptr := Sloc (Current_Scope);
6042 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6045 Func_Name : Node_Id;
6047 Parm_Type : Node_Id;
6048 Profile : List_Id := New_List;
6055 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6057 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6058 Mutate_Ekind (Func, E_Function);
6059 Set_Is_Generic_Actual_Subprogram (Func);
6061 Actuals := New_List;
6062 Profile := New_List;
6064 Act_F := First_Formal (Actual_Subp);
6065 Form_F := First_Formal (Formal_Subp);
6066 while Present (Form_F) loop
6068 -- Create new formal for profile of wrapper, and add a reference
6069 -- to it in the list of actuals for the enclosing call. The name
6070 -- must be that of the formal in the formal subprogram, because
6071 -- calls to it in the generic body may use named associations.
6073 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6076 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6079 Make_Parameter_Specification (Loc,
6080 Defining_Identifier => New_F,
6081 Parameter_Type => Parm_Type));
6083 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6084 Next_Formal (Form_F);
6086 if Present (Act_F) then
6087 Next_Formal (Act_F);
6092 Make_Function_Specification (Loc,
6093 Defining_Unit_Name => Func,
6094 Parameter_Specifications => Profile,
6095 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6098 Make_Expression_Function (Loc,
6099 Specification => Spec,
6101 Make_Function_Call (Loc,
6103 Parameter_Associations => Actuals));
6106 end Build_Function_Wrapper;
6108 ----------------------------
6109 -- Build_Operator_Wrapper --
6110 ----------------------------
6112 function Build_Operator_Wrapper
6113 (Formal_Subp : Entity_Id;
6114 Actual_Subp : Entity_Id) return Node_Id
6116 Loc : constant Source_Ptr := Sloc (Current_Scope);
6117 Ret_Type : constant Entity_Id :=
6118 Get_Instance_Of (Etype (Formal_Subp));
6119 Op_Type : constant Entity_Id :=
6120 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6121 Is_Binary : constant Boolean :=
6122 Present (Next_Formal (First_Formal (Formal_Subp)));
6125 Expr : Node_Id := Empty;
6133 Op_Name := Chars (Actual_Subp);
6135 -- Create entities for wrapper function and its formals
6137 F1 := Make_Temporary (Loc, 'A');
6138 F2 := Make_Temporary (Loc, 'B');
6139 L := New_Occurrence_Of (F1, Loc);
6140 R := New_Occurrence_Of (F2, Loc);
6142 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6143 Mutate_Ekind (Func, E_Function);
6144 Set_Is_Generic_Actual_Subprogram (Func);
6147 Make_Function_Specification (Loc,
6148 Defining_Unit_Name => Func,
6149 Parameter_Specifications => New_List (
6150 Make_Parameter_Specification (Loc,
6151 Defining_Identifier => F1,
6152 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6153 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6156 Append_To (Parameter_Specifications (Spec),
6157 Make_Parameter_Specification (Loc,
6158 Defining_Identifier => F2,
6159 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6162 -- Build expression as a function call, or as an operator node
6163 -- that corresponds to the name of the actual, starting with
6164 -- binary operators.
6166 if Op_Name not in Any_Operator_Name then
6168 Make_Function_Call (Loc,
6170 New_Occurrence_Of (Actual_Subp, Loc),
6171 Parameter_Associations => New_List (L));
6174 Append_To (Parameter_Associations (Expr), R);
6179 elsif Is_Binary then
6180 if Op_Name = Name_Op_And then
6181 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Or then
6183 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Xor then
6185 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Eq then
6187 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Ne then
6189 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Le then
6191 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Gt then
6193 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6194 elsif Op_Name = Name_Op_Ge then
6195 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6196 elsif Op_Name = Name_Op_Lt then
6197 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6198 elsif Op_Name = Name_Op_Add then
6199 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6200 elsif Op_Name = Name_Op_Subtract then
6201 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6202 elsif Op_Name = Name_Op_Concat then
6203 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6204 elsif Op_Name = Name_Op_Multiply then
6205 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6206 elsif Op_Name = Name_Op_Divide then
6207 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6208 elsif Op_Name = Name_Op_Mod then
6209 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6210 elsif Op_Name = Name_Op_Rem then
6211 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6212 elsif Op_Name = Name_Op_Expon then
6213 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6219 if Op_Name = Name_Op_Add then
6220 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6221 elsif Op_Name = Name_Op_Subtract then
6222 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6223 elsif Op_Name = Name_Op_Abs then
6224 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6225 elsif Op_Name = Name_Op_Not then
6226 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6231 Make_Expression_Function (Loc,
6232 Specification => Spec,
6233 Expression => Expr);
6236 end Build_Operator_Wrapper;
6238 -----------------------------------
6239 -- Build_Subprogram_Decl_Wrapper --
6240 -----------------------------------
6242 function Build_Subprogram_Decl_Wrapper
6243 (Formal_Subp : Entity_Id) return Node_Id
6245 Loc : constant Source_Ptr := Sloc (Current_Scope);
6246 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6249 Parm_Spec : Node_Id;
6250 Profile : List_Id := New_List;
6257 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6258 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6259 Set_Is_Generic_Actual_Subprogram (Subp);
6261 Profile := Parameter_Specifications (
6263 (Specification (Unit_Declaration_Node (Formal_Subp))));
6265 Form_F := First_Formal (Formal_Subp);
6266 Parm_Spec := First (Profile);
6268 -- Create new entities for the formals. Reset entities so that
6269 -- parameter types are properly resolved when wrapper declaration
6272 while Present (Parm_Spec) loop
6273 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6274 Set_Defining_Identifier (Parm_Spec, New_F);
6275 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6277 Next_Formal (Form_F);
6280 if Ret_Type = Standard_Void_Type then
6282 Make_Procedure_Specification (Loc,
6283 Defining_Unit_Name => Subp,
6284 Parameter_Specifications => Profile);
6287 Make_Function_Specification (Loc,
6288 Defining_Unit_Name => Subp,
6289 Parameter_Specifications => Profile,
6290 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6294 Make_Subprogram_Declaration (Loc, Specification => Spec);
6297 end Build_Subprogram_Decl_Wrapper;
6299 -----------------------------------
6300 -- Build_Subprogram_Body_Wrapper --
6301 -----------------------------------
6303 function Build_Subprogram_Body_Wrapper
6304 (Formal_Subp : Entity_Id;
6305 Actual_Name : Node_Id) return Node_Id
6307 Loc : constant Source_Ptr := Sloc (Current_Scope);
6308 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6309 Spec_Node : constant Node_Id :=
6311 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6314 Body_Node : Node_Id;
6317 Actuals := New_List;
6318 Act := First (Parameter_Specifications (Spec_Node));
6320 while Present (Act) loop
6322 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6326 if Ret_Type = Standard_Void_Type then
6327 Stmt := Make_Procedure_Call_Statement (Loc,
6328 Name => Actual_Name,
6329 Parameter_Associations => Actuals);
6332 Stmt := Make_Simple_Return_Statement (Loc,
6334 Make_Function_Call (Loc,
6335 Name => Actual_Name,
6336 Parameter_Associations => Actuals));
6339 Body_Node := Make_Subprogram_Body (Loc,
6340 Specification => Spec_Node,
6341 Declarations => New_List,
6342 Handled_Statement_Sequence =>
6343 Make_Handled_Sequence_Of_Statements (Loc,
6344 Statements => New_List (Stmt)));
6347 end Build_Subprogram_Body_Wrapper;
6349 -------------------------------------------
6350 -- Build_Instance_Compilation_Unit_Nodes --
6351 -------------------------------------------
6353 procedure Build_Instance_Compilation_Unit_Nodes
6358 Decl_Cunit : Node_Id;
6359 Body_Cunit : Node_Id;
6361 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6362 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6365 -- A new compilation unit node is built for the instance declaration
6368 Make_Compilation_Unit (Sloc (N),
6369 Context_Items => Empty_List,
6371 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6373 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6375 -- The new compilation unit is linked to its body, but both share the
6376 -- same file, so we do not set Body_Required on the new unit so as not
6377 -- to create a spurious dependency on a non-existent body in the ali.
6378 -- This simplifies CodePeer unit traversal.
6380 -- We use the original instantiation compilation unit as the resulting
6381 -- compilation unit of the instance, since this is the main unit.
6383 Rewrite (N, Act_Body);
6385 -- Propagate the aspect specifications from the package body template to
6386 -- the instantiated version of the package body.
6388 if Has_Aspects (Act_Body) then
6389 Set_Aspect_Specifications
6390 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6393 Body_Cunit := Parent (N);
6395 -- The two compilation unit nodes are linked by the Library_Unit field
6397 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6398 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6400 -- Preserve the private nature of the package if needed
6402 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6404 -- If the instance is not the main unit, its context, categorization
6405 -- and elaboration entity are not relevant to the compilation.
6407 if Body_Cunit /= Cunit (Main_Unit) then
6408 Make_Instance_Unit (Body_Cunit, In_Main => False);
6412 -- The context clause items on the instantiation, which are now attached
6413 -- to the body compilation unit (since the body overwrote the original
6414 -- instantiation node), semantically belong on the spec, so copy them
6415 -- there. It's harmless to leave them on the body as well. In fact one
6416 -- could argue that they belong in both places.
6418 Citem := First (Context_Items (Body_Cunit));
6419 while Present (Citem) loop
6420 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6424 -- Propagate categorization flags on packages, so that they appear in
6425 -- the ali file for the spec of the unit.
6427 if Ekind (New_Main) = E_Package then
6428 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6429 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6430 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6431 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6432 Set_Is_Remote_Call_Interface
6433 (Old_Main, Is_Remote_Call_Interface (New_Main));
6436 -- Make entry in Units table, so that binder can generate call to
6437 -- elaboration procedure for body, if any.
6439 Make_Instance_Unit (Body_Cunit, In_Main => True);
6440 Main_Unit_Entity := New_Main;
6441 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6443 -- Build elaboration entity, since the instance may certainly generate
6444 -- elaboration code requiring a flag for protection.
6446 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6447 end Build_Instance_Compilation_Unit_Nodes;
6449 -----------------------------
6450 -- Check_Access_Definition --
6451 -----------------------------
6453 procedure Check_Access_Definition (N : Node_Id) is
6456 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6458 end Check_Access_Definition;
6460 -----------------------------------
6461 -- Check_Formal_Package_Instance --
6462 -----------------------------------
6464 -- If the formal has specific parameters, they must match those of the
6465 -- actual. Both of them are instances, and the renaming declarations for
6466 -- their formal parameters appear in the same order in both. The analyzed
6467 -- formal has been analyzed in the context of the current instance.
6469 procedure Check_Formal_Package_Instance
6470 (Formal_Pack : Entity_Id;
6471 Actual_Pack : Entity_Id)
6473 E1 : Entity_Id := First_Entity (Actual_Pack);
6474 E2 : Entity_Id := First_Entity (Formal_Pack);
6475 Prev_E1 : Entity_Id;
6480 procedure Check_Mismatch (B : Boolean);
6481 -- Common error routine for mismatch between the parameters of the
6482 -- actual instance and those of the formal package.
6484 function Is_Defaulted (Param : Entity_Id) return Boolean;
6485 -- If the formal package has partly box-initialized formals, skip
6486 -- conformance check for these formals. Previously the code assumed
6487 -- that box initialization for a formal package applied to all its
6488 -- formal parameters.
6490 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6491 -- The formal may come from a nested formal package, and the actual may
6492 -- have been constant-folded. To determine whether the two denote the
6493 -- same entity we may have to traverse several definitions to recover
6494 -- the ultimate entity that they refer to.
6496 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6497 -- The formal and the actual must be identical, but if both are
6498 -- given by attributes they end up renaming different generated bodies,
6499 -- and we must verify that the attributes themselves match.
6501 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6502 -- Similarly, if the formal comes from a nested formal package, the
6503 -- actual may designate the formal through multiple renamings, which
6504 -- have to be followed to determine the original variable in question.
6506 --------------------
6507 -- Check_Mismatch --
6508 --------------------
6510 procedure Check_Mismatch (B : Boolean) is
6511 -- A Formal_Type_Declaration for a derived private type is rewritten
6512 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6513 -- which is why we examine the original node.
6515 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6518 if Kind = N_Formal_Type_Declaration then
6521 elsif Kind in N_Formal_Object_Declaration
6522 | N_Formal_Package_Declaration
6523 | N_Formal_Subprogram_Declaration
6527 -- Ada 2012: If both formal and actual are incomplete types they
6530 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6535 ("actual for & in actual instance does not match formal",
6536 Parent (Actual_Pack), E1);
6544 function Is_Defaulted (Param : Entity_Id) return Boolean is
6549 First (Generic_Associations (Parent
6550 (Associated_Formal_Package (Actual_Pack))));
6552 while Present (Assoc) loop
6553 if Nkind (Assoc) = N_Others_Choice then
6556 elsif Nkind (Assoc) = N_Generic_Association
6557 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6559 return Box_Present (Assoc);
6568 --------------------------------
6569 -- Same_Instantiated_Constant --
6570 --------------------------------
6572 function Same_Instantiated_Constant
6573 (E1, E2 : Entity_Id) return Boolean
6579 while Present (Ent) loop
6583 elsif Ekind (Ent) /= E_Constant then
6586 elsif Is_Entity_Name (Constant_Value (Ent)) then
6587 if Entity (Constant_Value (Ent)) = E1 then
6590 Ent := Entity (Constant_Value (Ent));
6593 -- The actual may be a constant that has been folded. Recover
6596 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6597 Ent := Entity (Original_Node (Constant_Value (Ent)));
6605 end Same_Instantiated_Constant;
6607 --------------------------------
6608 -- Same_Instantiated_Function --
6609 --------------------------------
6611 function Same_Instantiated_Function
6612 (E1, E2 : Entity_Id) return Boolean
6616 if Alias (E1) = Alias (E2) then
6619 elsif Present (Alias (E2)) then
6620 U1 := Original_Node (Unit_Declaration_Node (E1));
6621 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6623 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6624 and then Nkind (Name (U1)) = N_Attribute_Reference
6626 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6627 and then Nkind (Name (U2)) = N_Attribute_Reference
6630 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6634 end Same_Instantiated_Function;
6636 --------------------------------
6637 -- Same_Instantiated_Variable --
6638 --------------------------------
6640 function Same_Instantiated_Variable
6641 (E1, E2 : Entity_Id) return Boolean
6643 function Original_Entity (E : Entity_Id) return Entity_Id;
6644 -- Follow chain of renamings to the ultimate ancestor
6646 ---------------------
6647 -- Original_Entity --
6648 ---------------------
6650 function Original_Entity (E : Entity_Id) return Entity_Id is
6655 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6656 and then Present (Renamed_Object (Orig))
6657 and then Is_Entity_Name (Renamed_Object (Orig))
6659 Orig := Entity (Renamed_Object (Orig));
6663 end Original_Entity;
6665 -- Start of processing for Same_Instantiated_Variable
6668 return Ekind (E1) = Ekind (E2)
6669 and then Original_Entity (E1) = Original_Entity (E2);
6670 end Same_Instantiated_Variable;
6672 -- Start of processing for Check_Formal_Package_Instance
6676 while Present (E1) and then Present (E2) loop
6677 exit when Ekind (E1) = E_Package
6678 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6680 -- If the formal is the renaming of the formal package, this
6681 -- is the end of its formal part, which may occur before the
6682 -- end of the formal part in the actual in the presence of
6683 -- defaulted parameters in the formal package.
6685 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6686 and then Renamed_Entity (E2) = Scope (E2);
6688 -- The analysis of the actual may generate additional internal
6689 -- entities. If the formal is defaulted, there is no corresponding
6690 -- analysis and the internal entities must be skipped, until we
6691 -- find corresponding entities again.
6693 if Comes_From_Source (E2)
6694 and then not Comes_From_Source (E1)
6695 and then Chars (E1) /= Chars (E2)
6697 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6705 -- Entities may be declared without full declaration, such as
6706 -- itypes and predefined operators (concatenation for arrays, eg).
6707 -- Skip it and keep the formal entity to find a later match for it.
6709 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6713 -- If the formal entity comes from a formal declaration, it was
6714 -- defaulted in the formal package, and no check is needed on it.
6716 elsif Nkind (Original_Node (Parent (E2))) in
6717 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6719 -- If the formal is a tagged type the corresponding class-wide
6720 -- type has been generated as well, and it must be skipped.
6722 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6728 -- Ditto for defaulted formal subprograms.
6730 elsif Is_Overloadable (E1)
6731 and then Nkind (Unit_Declaration_Node (E2)) in
6732 N_Formal_Subprogram_Declaration
6736 elsif Is_Defaulted (E1) then
6739 elsif Is_Type (E1) then
6741 -- Subtypes must statically match. E1, E2 are the local entities
6742 -- that are subtypes of the actuals. Itypes generated for other
6743 -- parameters need not be checked, the check will be performed
6744 -- on the parameters themselves.
6746 -- If E2 is a formal type declaration, it is a defaulted parameter
6747 -- and needs no checking.
6749 if not Is_Itype (E1) and then not Is_Itype (E2) then
6752 or else Etype (E1) /= Etype (E2)
6753 or else not Subtypes_Statically_Match (E1, E2));
6756 elsif Ekind (E1) = E_Constant then
6758 -- IN parameters must denote the same static value, or the same
6759 -- constant, or the literal null.
6761 Expr1 := Expression (Parent (E1));
6763 if Ekind (E2) /= E_Constant then
6764 Check_Mismatch (True);
6767 Expr2 := Expression (Parent (E2));
6770 if Is_OK_Static_Expression (Expr1) then
6771 if not Is_OK_Static_Expression (Expr2) then
6772 Check_Mismatch (True);
6774 elsif Is_Discrete_Type (Etype (E1)) then
6776 V1 : constant Uint := Expr_Value (Expr1);
6777 V2 : constant Uint := Expr_Value (Expr2);
6779 Check_Mismatch (V1 /= V2);
6782 elsif Is_Real_Type (Etype (E1)) then
6784 V1 : constant Ureal := Expr_Value_R (Expr1);
6785 V2 : constant Ureal := Expr_Value_R (Expr2);
6787 Check_Mismatch (V1 /= V2);
6790 elsif Is_String_Type (Etype (E1))
6791 and then Nkind (Expr1) = N_String_Literal
6793 if Nkind (Expr2) /= N_String_Literal then
6794 Check_Mismatch (True);
6797 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6801 elsif Is_Entity_Name (Expr1) then
6802 if Is_Entity_Name (Expr2) then
6803 if Entity (Expr1) = Entity (Expr2) then
6807 (not Same_Instantiated_Constant
6808 (Entity (Expr1), Entity (Expr2)));
6812 Check_Mismatch (True);
6815 elsif Is_Entity_Name (Original_Node (Expr1))
6816 and then Is_Entity_Name (Expr2)
6817 and then Same_Instantiated_Constant
6818 (Entity (Original_Node (Expr1)), Entity (Expr2))
6822 elsif Nkind (Expr1) = N_Null then
6823 Check_Mismatch (Nkind (Expr1) /= N_Null);
6826 Check_Mismatch (True);
6829 elsif Ekind (E1) = E_Variable then
6830 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6832 elsif Ekind (E1) = E_Package then
6834 (Ekind (E1) /= Ekind (E2)
6835 or else (Present (Renamed_Object (E2))
6836 and then Renamed_Object (E1) /=
6837 Renamed_Object (E2)));
6839 elsif Is_Overloadable (E1) then
6840 -- Verify that the actual subprograms match. Note that actuals
6841 -- that are attributes are rewritten as subprograms. If the
6842 -- subprogram in the formal package is defaulted, no check is
6843 -- needed. Note that this can only happen in Ada 2005 when the
6844 -- formal package can be partially parameterized.
6846 if Nkind (Unit_Declaration_Node (E1)) =
6847 N_Subprogram_Renaming_Declaration
6848 and then From_Default (Unit_Declaration_Node (E1))
6852 -- If the formal package has an "others" box association that
6853 -- covers this formal, there is no need for a check either.
6855 elsif Nkind (Unit_Declaration_Node (E2)) in
6856 N_Formal_Subprogram_Declaration
6857 and then Box_Present (Unit_Declaration_Node (E2))
6861 -- No check needed if subprogram is a defaulted null procedure
6863 elsif No (Alias (E2))
6864 and then Ekind (E2) = E_Procedure
6866 Null_Present (Specification (Unit_Declaration_Node (E2)))
6870 -- Otherwise the actual in the formal and the actual in the
6871 -- instantiation of the formal must match, up to renamings.
6875 (Ekind (E2) /= Ekind (E1)
6876 or else not Same_Instantiated_Function (E1, E2));
6880 raise Program_Error;
6888 end Check_Formal_Package_Instance;
6890 ---------------------------
6891 -- Check_Formal_Packages --
6892 ---------------------------
6894 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6896 Formal_P : Entity_Id;
6897 Formal_Decl : Node_Id;
6899 -- Iterate through the declarations in the instance, looking for package
6900 -- renaming declarations that denote instances of formal packages. Stop
6901 -- when we find the renaming of the current package itself. The
6902 -- declaration for a formal package without a box is followed by an
6903 -- internal entity that repeats the instantiation.
6905 E := First_Entity (P_Id);
6906 while Present (E) loop
6907 if Ekind (E) = E_Package then
6908 if Renamed_Object (E) = P_Id then
6911 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6915 Formal_Decl := Parent (Associated_Formal_Package (E));
6917 -- Nothing to check if the formal has a box or an others_clause
6918 -- (necessarily with a box), or no associations altogether
6920 if Box_Present (Formal_Decl)
6921 or else No (Generic_Associations (Formal_Decl))
6925 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6928 -- The internal validating package was generated but formal
6929 -- and instance are known to be compatible.
6931 Formal_P := Next_Entity (E);
6932 Remove (Unit_Declaration_Node (Formal_P));
6935 Formal_P := Next_Entity (E);
6937 -- If the instance is within an enclosing instance body
6938 -- there is no need to verify the legality of current formal
6939 -- packages because they were legal in the generic body.
6940 -- This optimization may be applicable elsewhere, and it
6941 -- also removes spurious errors that may arise with
6942 -- on-the-fly inlining and confusion between private and
6945 if not In_Instance_Body then
6946 Check_Formal_Package_Instance (Formal_P, E);
6949 -- Restore the visibility of formals of the formal instance
6950 -- that are not defaulted, and are hidden within the current
6951 -- generic. These formals may be visible within an enclosing
6957 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6958 while Present (Elmt) loop
6959 Set_Is_Hidden (Node (Elmt), False);
6964 -- After checking, remove the internal validating package.
6965 -- It is only needed for semantic checks, and as it may
6966 -- contain generic formal declarations it should not reach
6969 Remove (Unit_Declaration_Node (Formal_P));
6976 end Check_Formal_Packages;
6978 ---------------------------------
6979 -- Check_Forward_Instantiation --
6980 ---------------------------------
6982 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6984 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6987 -- The instantiation appears before the generic body if we are in the
6988 -- scope of the unit containing the generic, either in its spec or in
6989 -- the package body, and before the generic body.
6991 if Ekind (Gen_Comp) = E_Package_Body then
6992 Gen_Comp := Spec_Entity (Gen_Comp);
6995 if In_Open_Scopes (Gen_Comp)
6996 and then No (Corresponding_Body (Decl))
7001 and then not Is_Compilation_Unit (S)
7002 and then not Is_Child_Unit (S)
7004 if Ekind (S) = E_Package then
7005 Set_Has_Forward_Instantiation (S);
7011 end Check_Forward_Instantiation;
7013 ---------------------------
7014 -- Check_Generic_Actuals --
7015 ---------------------------
7017 -- The visibility of the actuals may be different between the point of
7018 -- generic instantiation and the instantiation of the body.
7020 procedure Check_Generic_Actuals
7021 (Instance : Entity_Id;
7022 Is_Formal_Box : Boolean)
7028 E := First_Entity (Instance);
7029 while Present (E) loop
7031 and then Nkind (Parent (E)) = N_Subtype_Declaration
7032 and then Scope (Etype (E)) /= Instance
7033 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7035 -- Restore the proper view of the actual from the information
7036 -- saved earlier by Instantiate_Type.
7038 Check_Private_View (Subtype_Indication (Parent (E)));
7040 -- If the actual is itself the formal of a parent instance,
7041 -- then also restore the proper view of its actual and so on.
7042 -- That's necessary for nested instantiations of the form
7045 -- type Component is private;
7046 -- type Array_Type is array (Positive range <>) of Component;
7049 -- when the outermost actuals have inconsistent views, because
7050 -- the Component_Type of Array_Type of the inner instantiations
7051 -- is the actual of Component of the outermost one and not that
7052 -- of the corresponding inner instantiations.
7054 Astype := Ancestor_Subtype (E);
7055 while Present (Astype)
7056 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7057 and then Present (Generic_Parent_Type (Parent (Astype)))
7058 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7060 Check_Private_View (Subtype_Indication (Parent (Astype)));
7061 Astype := Ancestor_Subtype (Astype);
7064 Set_Is_Generic_Actual_Type (E);
7066 if Is_Private_Type (E) and then Present (Full_View (E)) then
7067 Set_Is_Generic_Actual_Type (Full_View (E));
7070 Set_Is_Hidden (E, False);
7071 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7073 -- We constructed the generic actual type as a subtype of the
7074 -- supplied type. This means that it normally would not inherit
7075 -- subtype specific attributes of the actual, which is wrong for
7076 -- the generic case.
7078 Astype := Ancestor_Subtype (E);
7082 -- This can happen when E is an itype that is the full view of
7083 -- a private type completed, e.g. with a constrained array. In
7084 -- that case, use the first subtype, which will carry size
7085 -- information. The base type itself is unconstrained and will
7088 Astype := First_Subtype (E);
7091 Set_Size_Info (E, (Astype));
7092 Set_RM_Size (E, RM_Size (Astype));
7093 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7095 if Is_Discrete_Or_Fixed_Point_Type (E) then
7096 Set_RM_Size (E, RM_Size (Astype));
7099 elsif Ekind (E) = E_Package then
7101 -- If this is the renaming for the current instance, we're done.
7102 -- Otherwise it is a formal package. If the corresponding formal
7103 -- was declared with a box, the (instantiations of the) generic
7104 -- formal part are also visible. Otherwise, ignore the entity
7105 -- created to validate the actuals.
7107 if Renamed_Object (E) = Instance then
7110 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7113 -- The visibility of a formal of an enclosing generic is already
7116 elsif Denotes_Formal_Package (E) then
7119 elsif Present (Associated_Formal_Package (E))
7120 and then not Is_Generic_Formal (E)
7122 if Box_Present (Parent (Associated_Formal_Package (E))) then
7123 Check_Generic_Actuals (Renamed_Object (E), True);
7126 Check_Generic_Actuals (Renamed_Object (E), False);
7129 Set_Is_Hidden (E, False);
7132 -- If this is a subprogram instance (in a wrapper package) the
7133 -- actual is fully visible.
7135 elsif Is_Wrapper_Package (Instance) then
7136 Set_Is_Hidden (E, False);
7138 -- If the formal package is declared with a box, or if the formal
7139 -- parameter is defaulted, it is visible in the body.
7141 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7142 Set_Is_Hidden (E, False);
7145 if Ekind (E) = E_Constant then
7147 -- If the type of the actual is a private type declared in the
7148 -- enclosing scope of the generic unit, the body of the generic
7149 -- sees the full view of the type (because it has to appear in
7150 -- the corresponding package body). If the type is private now,
7151 -- exchange views to restore the proper visiblity in the instance.
7154 Typ : constant Entity_Id := Base_Type (Etype (E));
7155 -- The type of the actual
7160 Parent_Scope : Entity_Id;
7161 -- The enclosing scope of the generic unit
7164 if Is_Wrapper_Package (Instance) then
7168 (Unit_Declaration_Node
7169 (Related_Instance (Instance))));
7172 Generic_Parent (Package_Specification (Instance));
7175 Parent_Scope := Scope (Gen_Id);
7177 -- The exchange is only needed if the generic is defined
7178 -- within a package which is not a common ancestor of the
7179 -- scope of the instance, and is not already in scope.
7181 if Is_Private_Type (Typ)
7182 and then Scope (Typ) = Parent_Scope
7183 and then Scope (Instance) /= Parent_Scope
7184 and then Ekind (Parent_Scope) = E_Package
7185 and then not Is_Child_Unit (Gen_Id)
7189 -- If the type of the entity is a subtype, it may also have
7190 -- to be made visible, together with the base type of its
7191 -- full view, after exchange.
7193 if Is_Private_Type (Etype (E)) then
7194 Switch_View (Etype (E));
7195 Switch_View (Base_Type (Etype (E)));
7203 end Check_Generic_Actuals;
7205 ------------------------------
7206 -- Check_Generic_Child_Unit --
7207 ------------------------------
7209 procedure Check_Generic_Child_Unit
7211 Parent_Installed : in out Boolean)
7213 Loc : constant Source_Ptr := Sloc (Gen_Id);
7214 Gen_Par : Entity_Id := Empty;
7216 Inst_Par : Entity_Id;
7219 function Find_Generic_Child
7221 Id : Node_Id) return Entity_Id;
7222 -- Search generic parent for possible child unit with the given name
7224 function In_Enclosing_Instance return Boolean;
7225 -- Within an instance of the parent, the child unit may be denoted by
7226 -- a simple name, or an abbreviated expanded name. Examine enclosing
7227 -- scopes to locate a possible parent instantiation.
7229 ------------------------
7230 -- Find_Generic_Child --
7231 ------------------------
7233 function Find_Generic_Child
7235 Id : Node_Id) return Entity_Id
7240 -- If entity of name is already set, instance has already been
7241 -- resolved, e.g. in an enclosing instantiation.
7243 if Present (Entity (Id)) then
7244 if Scope (Entity (Id)) = Scop then
7251 E := First_Entity (Scop);
7252 while Present (E) loop
7253 if Chars (E) = Chars (Id)
7254 and then Is_Child_Unit (E)
7256 if Is_Child_Unit (E)
7257 and then not Is_Visible_Lib_Unit (E)
7260 ("generic child unit& is not visible", Gen_Id, E);
7272 end Find_Generic_Child;
7274 ---------------------------
7275 -- In_Enclosing_Instance --
7276 ---------------------------
7278 function In_Enclosing_Instance return Boolean is
7279 Enclosing_Instance : Node_Id;
7280 Instance_Decl : Node_Id;
7283 -- We do not inline any call that contains instantiations, except
7284 -- for instantiations of Unchecked_Conversion, so if we are within
7285 -- an inlined body the current instance does not require parents.
7287 if In_Inlined_Body then
7288 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7292 -- Loop to check enclosing scopes
7294 Enclosing_Instance := Current_Scope;
7295 while Present (Enclosing_Instance) loop
7296 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7298 if Ekind (Enclosing_Instance) = E_Package
7299 and then Is_Generic_Instance (Enclosing_Instance)
7301 (Generic_Parent (Specification (Instance_Decl)))
7303 -- Check whether the generic we are looking for is a child of
7306 E := Find_Generic_Child
7307 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7308 exit when Present (E);
7314 Enclosing_Instance := Scope (Enclosing_Instance);
7326 Make_Expanded_Name (Loc,
7328 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7329 Selector_Name => New_Occurrence_Of (E, Loc)));
7331 Set_Entity (Gen_Id, E);
7332 Set_Etype (Gen_Id, Etype (E));
7333 Parent_Installed := False; -- Already in scope.
7336 end In_Enclosing_Instance;
7338 -- Start of processing for Check_Generic_Child_Unit
7341 -- If the name of the generic is given by a selected component, it may
7342 -- be the name of a generic child unit, and the prefix is the name of an
7343 -- instance of the parent, in which case the child unit must be visible.
7344 -- If this instance is not in scope, it must be placed there and removed
7345 -- after instantiation, because what is being instantiated is not the
7346 -- original child, but the corresponding child present in the instance
7349 -- If the child is instantiated within the parent, it can be given by
7350 -- a simple name. In this case the instance is already in scope, but
7351 -- the child generic must be recovered from the generic parent as well.
7353 if Nkind (Gen_Id) = N_Selected_Component then
7354 S := Selector_Name (Gen_Id);
7355 Analyze (Prefix (Gen_Id));
7356 Inst_Par := Entity (Prefix (Gen_Id));
7358 if Ekind (Inst_Par) = E_Package
7359 and then Present (Renamed_Object (Inst_Par))
7361 Inst_Par := Renamed_Object (Inst_Par);
7364 if Ekind (Inst_Par) = E_Package then
7365 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7366 Gen_Par := Generic_Parent (Parent (Inst_Par));
7368 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7370 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7372 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7375 elsif Ekind (Inst_Par) = E_Generic_Package
7376 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7378 -- A formal package may be a real child package, and not the
7379 -- implicit instance within a parent. In this case the child is
7380 -- not visible and has to be retrieved explicitly as well.
7382 Gen_Par := Inst_Par;
7385 if Present (Gen_Par) then
7387 -- The prefix denotes an instantiation. The entity itself may be a
7388 -- nested generic, or a child unit.
7390 E := Find_Generic_Child (Gen_Par, S);
7393 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7394 Set_Entity (Gen_Id, E);
7395 Set_Etype (Gen_Id, Etype (E));
7397 Set_Etype (S, Etype (E));
7399 -- Indicate that this is a reference to the parent
7401 if In_Extended_Main_Source_Unit (Gen_Id) then
7402 Set_Is_Instantiated (Inst_Par);
7405 -- A common mistake is to replicate the naming scheme of a
7406 -- hierarchy by instantiating a generic child directly, rather
7407 -- than the implicit child in a parent instance:
7409 -- generic .. package Gpar is ..
7410 -- generic .. package Gpar.Child is ..
7411 -- package Par is new Gpar ();
7414 -- package Par.Child is new Gpar.Child ();
7415 -- rather than Par.Child
7417 -- In this case the instantiation is within Par, which is an
7418 -- instance, but Gpar does not denote Par because we are not IN
7419 -- the instance of Gpar, so this is illegal. The test below
7420 -- recognizes this particular case.
7422 if Is_Child_Unit (E)
7423 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7424 and then (not In_Instance
7425 or else Nkind (Parent (Parent (Gen_Id))) =
7429 ("prefix of generic child unit must be instance of parent",
7433 if not In_Open_Scopes (Inst_Par)
7434 and then Nkind (Parent (Gen_Id)) not in
7435 N_Generic_Renaming_Declaration
7437 Install_Parent (Inst_Par);
7438 Parent_Installed := True;
7440 elsif In_Open_Scopes (Inst_Par) then
7442 -- If the parent is already installed, install the actuals
7443 -- for its formal packages. This is necessary when the child
7444 -- instance is a child of the parent instance: in this case,
7445 -- the parent is placed on the scope stack but the formal
7446 -- packages are not made visible.
7448 Install_Formal_Packages (Inst_Par);
7452 -- If the generic parent does not contain an entity that
7453 -- corresponds to the selector, the instance doesn't either.
7454 -- Analyzing the node will yield the appropriate error message.
7455 -- If the entity is not a child unit, then it is an inner
7456 -- generic in the parent.
7464 if Is_Child_Unit (Entity (Gen_Id))
7466 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7467 and then not In_Open_Scopes (Inst_Par)
7469 Install_Parent (Inst_Par);
7470 Parent_Installed := True;
7472 -- The generic unit may be the renaming of the implicit child
7473 -- present in an instance. In that case the parent instance is
7474 -- obtained from the name of the renamed entity.
7476 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7477 and then Present (Renamed_Entity (Entity (Gen_Id)))
7478 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7481 Renamed_Package : constant Node_Id :=
7482 Name (Parent (Entity (Gen_Id)));
7484 if Nkind (Renamed_Package) = N_Expanded_Name then
7485 Inst_Par := Entity (Prefix (Renamed_Package));
7486 Install_Parent (Inst_Par);
7487 Parent_Installed := True;
7493 elsif Nkind (Gen_Id) = N_Expanded_Name then
7495 -- Entity already present, analyze prefix, whose meaning may be an
7496 -- instance in the current context. If it is an instance of a
7497 -- relative within another, the proper parent may still have to be
7498 -- installed, if they are not of the same generation.
7500 Analyze (Prefix (Gen_Id));
7502 -- Prevent cascaded errors
7504 if Etype (Prefix (Gen_Id)) = Any_Type then
7508 -- In the unlikely case that a local declaration hides the name of
7509 -- the parent package, locate it on the homonym chain. If the context
7510 -- is an instance of the parent, the renaming entity is flagged as
7513 Inst_Par := Entity (Prefix (Gen_Id));
7514 while Present (Inst_Par)
7515 and then not Is_Package_Or_Generic_Package (Inst_Par)
7517 Inst_Par := Homonym (Inst_Par);
7520 pragma Assert (Present (Inst_Par));
7521 Set_Entity (Prefix (Gen_Id), Inst_Par);
7523 if In_Enclosing_Instance then
7526 elsif Present (Entity (Gen_Id))
7527 and then No (Renamed_Entity (Entity (Gen_Id)))
7528 and then Is_Child_Unit (Entity (Gen_Id))
7529 and then not In_Open_Scopes (Inst_Par)
7531 Install_Parent (Inst_Par);
7532 Parent_Installed := True;
7534 -- Handle renaming of generic child unit
7536 elsif Present (Entity (Gen_Id))
7537 and then Present (Renamed_Entity (Entity (Gen_Id)))
7538 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7545 -- The entity of the renamed generic child unit does not
7546 -- have any reference to the instantiated parent. In order to
7547 -- locate it we traverse the scope containing the renaming
7548 -- declaration; the instance of the parent is available in
7549 -- the prefix of the renaming declaration. For example:
7552 -- package Inst_Par is new ...
7553 -- generic package Ren_Child renames Ins_Par.Child;
7558 -- package Inst_Child is new A.Ren_Child;
7561 E := First_Entity (Entity (Prefix (Gen_Id)));
7562 while Present (E) loop
7563 if Present (Renamed_Entity (E))
7565 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7567 Ren_Decl := Parent (E);
7568 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7570 if not In_Open_Scopes (Inst_Par) then
7571 Install_Parent (Inst_Par);
7572 Parent_Installed := True;
7578 E := Next_Entity (E);
7583 elsif In_Enclosing_Instance then
7585 -- The child unit is found in some enclosing scope
7592 -- If this is the renaming of the implicit child in a parent
7593 -- instance, recover the parent name and install it.
7595 if Is_Entity_Name (Gen_Id) then
7596 E := Entity (Gen_Id);
7598 if Is_Generic_Unit (E)
7599 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7600 and then Is_Child_Unit (Renamed_Object (E))
7601 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7602 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7604 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7605 Inst_Par := Entity (Prefix (Gen_Id));
7607 if not In_Open_Scopes (Inst_Par) then
7608 Install_Parent (Inst_Par);
7609 Parent_Installed := True;
7612 -- If it is a child unit of a non-generic parent, it may be
7613 -- use-visible and given by a direct name. Install parent as
7616 elsif Is_Generic_Unit (E)
7617 and then Is_Child_Unit (E)
7619 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7620 and then not Is_Generic_Unit (Scope (E))
7622 if not In_Open_Scopes (Scope (E)) then
7623 Install_Parent (Scope (E));
7624 Parent_Installed := True;
7629 end Check_Generic_Child_Unit;
7631 -----------------------------
7632 -- Check_Hidden_Child_Unit --
7633 -----------------------------
7635 procedure Check_Hidden_Child_Unit
7637 Gen_Unit : Entity_Id;
7638 Act_Decl_Id : Entity_Id)
7640 Gen_Id : constant Node_Id := Name (N);
7643 if Is_Child_Unit (Gen_Unit)
7644 and then Is_Child_Unit (Act_Decl_Id)
7645 and then Nkind (Gen_Id) = N_Expanded_Name
7646 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7647 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7649 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7651 ("generic unit & is implicitly declared in &",
7652 Defining_Unit_Name (N), Gen_Unit);
7653 Error_Msg_N ("\instance must have different name",
7654 Defining_Unit_Name (N));
7656 end Check_Hidden_Child_Unit;
7658 ------------------------
7659 -- Check_Private_View --
7660 ------------------------
7662 procedure Check_Private_View (N : Node_Id) is
7663 T : constant Entity_Id := Etype (N);
7667 -- Exchange views if the type was not private in the generic but is
7668 -- private at the point of instantiation. Do not exchange views if
7669 -- the scope of the type is in scope. This can happen if both generic
7670 -- and instance are sibling units, or if type is defined in a parent.
7671 -- In this case the visibility of the type will be correct for all
7675 BT := Base_Type (T);
7677 if Is_Private_Type (T)
7678 and then not Has_Private_View (N)
7679 and then Present (Full_View (T))
7680 and then not In_Open_Scopes (Scope (T))
7682 -- In the generic, the full declaration was visible
7686 elsif Has_Private_View (N)
7687 and then not Is_Private_Type (T)
7688 and then not Has_Been_Exchanged (T)
7689 and then (not In_Open_Scopes (Scope (T))
7690 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7692 -- In the generic, only the private declaration was visible
7694 -- If the type appears in a subtype declaration, the subtype in
7695 -- instance must have a view compatible with that of its parent,
7696 -- which must be exchanged (see corresponding code in Restore_
7697 -- Private_Views) so we make an exception to the open scope rule.
7699 Prepend_Elmt (T, Exchanged_Views);
7700 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7702 -- Finally, a non-private subtype may have a private base type, which
7703 -- must be exchanged for consistency. This can happen when a package
7704 -- body is instantiated, when the scope stack is empty but in fact
7705 -- the subtype and the base type are declared in an enclosing scope.
7707 -- Note that in this case we introduce an inconsistency in the view
7708 -- set, because we switch the base type BT, but there could be some
7709 -- private dependent subtypes of BT which remain unswitched. Such
7710 -- subtypes might need to be switched at a later point (see specific
7711 -- provision for that case in Switch_View).
7713 elsif not Is_Private_Type (T)
7714 and then not Has_Private_View (N)
7715 and then Is_Private_Type (BT)
7716 and then Present (Full_View (BT))
7717 and then not Is_Generic_Type (BT)
7718 and then not In_Open_Scopes (BT)
7720 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7721 Exchange_Declarations (BT);
7724 end Check_Private_View;
7726 -----------------------------
7727 -- Check_Hidden_Primitives --
7728 -----------------------------
7730 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7733 Result : Elist_Id := No_Elist;
7736 if No (Assoc_List) then
7740 -- Traverse the list of associations between formals and actuals
7741 -- searching for renamings of tagged types
7743 Actual := First (Assoc_List);
7744 while Present (Actual) loop
7745 if Nkind (Actual) = N_Subtype_Declaration then
7746 Gen_T := Generic_Parent_Type (Actual);
7748 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7750 -- Traverse the list of primitives of the actual types
7751 -- searching for hidden primitives that are visible in the
7752 -- corresponding generic formal; leave them visible and
7753 -- append them to Result to restore their decoration later.
7755 Install_Hidden_Primitives
7756 (Prims_List => Result,
7758 Act_T => Entity (Subtype_Indication (Actual)));
7766 end Check_Hidden_Primitives;
7768 --------------------------
7769 -- Contains_Instance_Of --
7770 --------------------------
7772 function Contains_Instance_Of
7775 N : Node_Id) return Boolean
7783 -- Verify that there are no circular instantiations. We check whether
7784 -- the unit contains an instance of the current scope or some enclosing
7785 -- scope (in case one of the instances appears in a subunit). Longer
7786 -- circularities involving subunits might seem too pathological to
7787 -- consider, but they were not too pathological for the authors of
7788 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7789 -- enclosing generic scopes as containing an instance.
7792 -- Within a generic subprogram body, the scope is not generic, to
7793 -- allow for recursive subprograms. Use the declaration to determine
7794 -- whether this is a generic unit.
7796 if Ekind (Scop) = E_Generic_Package
7797 or else (Is_Subprogram (Scop)
7798 and then Nkind (Unit_Declaration_Node (Scop)) =
7799 N_Generic_Subprogram_Declaration)
7801 Elmt := First_Elmt (Inner_Instances (Inner));
7803 while Present (Elmt) loop
7804 if Node (Elmt) = Scop then
7805 Error_Msg_Node_2 := Inner;
7807 ("circular instantiation: & instantiated within &!",
7811 elsif Node (Elmt) = Inner then
7814 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7815 Error_Msg_Node_2 := Inner;
7817 ("circular instantiation: & instantiated within &!",
7825 -- Indicate that Inner is being instantiated within Scop
7827 Append_Elmt (Inner, Inner_Instances (Scop));
7830 if Scop = Standard_Standard then
7833 Scop := Scope (Scop);
7838 end Contains_Instance_Of;
7840 -----------------------
7841 -- Copy_Generic_Node --
7842 -----------------------
7844 function Copy_Generic_Node
7846 Parent_Id : Node_Id;
7847 Instantiating : Boolean) return Node_Id
7852 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7853 -- Check the given value of one of the Fields referenced by the current
7854 -- node to determine whether to copy it recursively. The field may hold
7855 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7856 -- Char) in which case it need not be copied.
7858 procedure Copy_Descendants;
7859 -- Common utility for various nodes
7861 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7862 -- Make copy of element list
7864 function Copy_Generic_List
7866 Parent_Id : Node_Id) return List_Id;
7867 -- Apply Copy_Node recursively to the members of a node list
7869 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7870 -- True if an identifier is part of the defining program unit name of
7872 -- Consider removing this subprogram now that ASIS no longer uses it.
7874 ----------------------
7875 -- Copy_Descendants --
7876 ----------------------
7878 procedure Copy_Descendants is
7879 procedure Walk is new
7880 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7883 end Copy_Descendants;
7885 -----------------------------
7886 -- Copy_Generic_Descendant --
7887 -----------------------------
7889 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7891 if D = Union_Id (Empty) then
7894 elsif D in Node_Range then
7896 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7898 elsif D in List_Range then
7899 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7901 elsif D in Elist_Range then
7902 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7904 -- Nothing else is copyable (e.g. Uint values), return as is
7909 end Copy_Generic_Descendant;
7911 ------------------------
7912 -- Copy_Generic_Elist --
7913 ------------------------
7915 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7922 M := First_Elmt (E);
7923 while Present (M) loop
7925 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7934 end Copy_Generic_Elist;
7936 -----------------------
7937 -- Copy_Generic_List --
7938 -----------------------
7940 function Copy_Generic_List
7942 Parent_Id : Node_Id) return List_Id
7950 Set_Parent (New_L, Parent_Id);
7953 while Present (N) loop
7954 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7963 end Copy_Generic_List;
7965 ---------------------------
7966 -- In_Defining_Unit_Name --
7967 ---------------------------
7969 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7972 Present (Parent (Nam))
7973 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7975 (Nkind (Parent (Nam)) = N_Expanded_Name
7976 and then In_Defining_Unit_Name (Parent (Nam))));
7977 end In_Defining_Unit_Name;
7979 -- Start of processing for Copy_Generic_Node
7986 New_N := New_Copy (N);
7988 -- Copy aspects if present
7990 if Has_Aspects (N) then
7991 Set_Has_Aspects (New_N, False);
7992 Set_Aspect_Specifications
7993 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7996 -- If we are instantiating, we want to adjust the sloc based on the
7997 -- current S_Adjustment. However, if this is the root node of a subunit,
7998 -- we need to defer that adjustment to below (see "elsif Instantiating
7999 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8000 -- computed the adjustment.
8003 and then not (Nkind (N) in N_Proper_Body
8004 and then Was_Originally_Stub (N))
8006 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8009 if not Is_List_Member (N) then
8010 Set_Parent (New_N, Parent_Id);
8013 -- Special casing for identifiers and other entity names and operators
8015 if Nkind (New_N) in N_Character_Literal
8021 if not Instantiating then
8023 -- Link both nodes in order to assign subsequently the entity of
8024 -- the copy to the original node, in case this is a global
8027 Set_Associated_Node (N, New_N);
8029 -- If we are within an instantiation, this is a nested generic
8030 -- that has already been analyzed at the point of definition.
8031 -- We must preserve references that were global to the enclosing
8032 -- parent at that point. Other occurrences, whether global or
8033 -- local to the current generic, must be resolved anew, so we
8034 -- reset the entity in the generic copy. A global reference has a
8035 -- smaller depth than the parent, or else the same depth in case
8036 -- both are distinct compilation units.
8038 -- A child unit is implicitly declared within the enclosing parent
8039 -- but is in fact global to it, and must be preserved.
8041 -- It is also possible for Current_Instantiated_Parent to be
8042 -- defined, and for this not to be a nested generic, namely if
8043 -- the unit is loaded through Rtsfind. In that case, the entity of
8044 -- New_N is only a link to the associated node, and not a defining
8047 -- The entities for parent units in the defining_program_unit of a
8048 -- generic child unit are established when the context of the unit
8049 -- is first analyzed, before the generic copy is made. They are
8050 -- preserved in the copy for use in e.g. ASIS queries.
8052 Ent := Entity (New_N);
8054 if No (Current_Instantiated_Parent.Gen_Id) then
8056 or else Nkind (Ent) /= N_Defining_Identifier
8057 or else not In_Defining_Unit_Name (N)
8059 Set_Associated_Node (New_N, Empty);
8063 or else Nkind (Ent) not in N_Entity
8064 or else No (Scope (Ent))
8066 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8067 and then not Is_Child_Unit (Ent))
8069 (Scope_Depth (Scope (Ent)) >
8070 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8072 Get_Source_Unit (Ent) =
8073 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8075 Set_Associated_Node (New_N, Empty);
8078 -- Case of instantiating identifier or some other name or operator
8081 -- If the associated node is still defined, the entity in it
8082 -- is global, and must be copied to the instance. If this copy
8083 -- is being made for a body to inline, it is applied to an
8084 -- instantiated tree, and the entity is already present and
8085 -- must be also preserved.
8088 Assoc : constant Node_Id := Get_Associated_Node (N);
8091 if Present (Assoc) then
8092 if Nkind (Assoc) = Nkind (N) then
8093 Set_Entity (New_N, Entity (Assoc));
8094 Check_Private_View (N);
8096 -- Here we deal with a very peculiar case for which the
8097 -- Has_Private_View mechanism is not sufficient, because
8098 -- the reference to the type is implicit in the tree,
8099 -- that is to say, it's not referenced from a node but
8100 -- only from another type, namely through Component_Type.
8104 -- type Pt is private;
8107 -- type Ft is array (Positive range <>) of Pt;
8109 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8113 -- type Pt is new Boolean;
8116 -- package body P is
8117 -- package body G is
8118 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8120 -- if (F1 < F2) /= Lt then
8127 -- type Arr is array (Positive range <>) of P.Pt;
8129 -- package Inst is new P.G (Arr);
8131 -- Pt is a global type for the generic package G and it
8132 -- is not referenced in its body, but only as component
8133 -- type of Ft, which is a local type. This means that no
8134 -- references to Pt or Ft are seen during the copy of the
8135 -- body, the only reference to Pt being seen is when the
8136 -- actuals are checked by Check_Generic_Actuals, but Pt
8137 -- is still private at this point. In the end, the views
8138 -- of Pt are not switched in the body and, therefore, the
8139 -- array comparison is rejected because the component is
8142 -- Adding e.g. a dummy variable of type Pt in the body is
8143 -- sufficient to make everything work, so we generate an
8144 -- artificial reference to Pt on the fly and thus force
8145 -- the switching of views on the grounds that, if the
8146 -- comparison was accepted during the semantic analysis
8147 -- of the generic, this means that the component cannot
8148 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8150 if Nkind (Assoc) in N_Op_Compare
8151 and then Present (Etype (Left_Opnd (Assoc)))
8152 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8153 and then Present (Etype (Right_Opnd (Assoc)))
8154 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8157 Ltyp : constant Entity_Id :=
8158 Etype (Left_Opnd (Assoc));
8159 Rtyp : constant Entity_Id :=
8160 Etype (Right_Opnd (Assoc));
8162 if Is_Private_Type (Component_Type (Ltyp)) then
8164 (New_Occurrence_Of (Component_Type (Ltyp),
8167 if Is_Private_Type (Component_Type (Rtyp)) then
8169 (New_Occurrence_Of (Component_Type (Rtyp),
8174 -- Here is a similar case, for the Designated_Type of an
8175 -- access type that is present as target type in a type
8176 -- conversion from another access type. In this case, if
8177 -- the base types of the designated types are different
8178 -- and the conversion was accepted during the semantic
8179 -- analysis of the generic, this means that the target
8180 -- type cannot have been private (see Valid_Conversion).
8182 elsif Nkind (Assoc) = N_Identifier
8183 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8184 and then Subtype_Mark (Parent (Assoc)) = Assoc
8185 and then Present (Etype (Assoc))
8186 and then Is_Access_Type (Etype (Assoc))
8187 and then Present (Etype (Expression (Parent (Assoc))))
8189 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8192 Targ_Desig : constant Entity_Id :=
8193 Designated_Type (Etype (Assoc));
8194 Expr_Desig : constant Entity_Id :=
8196 (Etype (Expression (Parent (Assoc))));
8198 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8199 and then Is_Private_Type (Targ_Desig)
8202 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8207 -- The node is a reference to a global type and acts as the
8208 -- subtype mark of a qualified expression created in order
8209 -- to aid resolution of accidental overloading in instances.
8210 -- Since N is a reference to a type, the Associated_Node of
8211 -- N denotes an entity rather than another identifier. See
8212 -- Qualify_Universal_Operands for details.
8214 elsif Nkind (N) = N_Identifier
8215 and then Nkind (Parent (N)) = N_Qualified_Expression
8216 and then Subtype_Mark (Parent (N)) = N
8217 and then Is_Qualified_Universal_Literal (Parent (N))
8219 Set_Entity (New_N, Assoc);
8221 -- The name in the call may be a selected component if the
8222 -- call has not been analyzed yet, as may be the case for
8223 -- pre/post conditions in a generic unit.
8225 elsif Nkind (Assoc) = N_Function_Call
8226 and then Is_Entity_Name (Name (Assoc))
8228 Set_Entity (New_N, Entity (Name (Assoc)));
8230 elsif Nkind (Assoc) in N_Entity
8231 and then Expander_Active
8233 -- Inlining case: we are copying a tree that contains
8234 -- global entities, which are preserved in the copy to be
8235 -- used for subsequent inlining.
8240 Set_Entity (New_N, Empty);
8246 -- For expanded name, we must copy the Prefix and Selector_Name
8248 if Nkind (N) = N_Expanded_Name then
8250 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8252 Set_Selector_Name (New_N,
8253 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8255 -- For operators, copy the operands
8257 elsif Nkind (N) in N_Op then
8258 if Nkind (N) in N_Binary_Op then
8259 Set_Left_Opnd (New_N,
8260 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8263 Set_Right_Opnd (New_N,
8264 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8267 -- Establish a link between an entity from the generic template and the
8268 -- corresponding entity in the generic copy to be analyzed.
8270 elsif Nkind (N) in N_Entity then
8271 if not Instantiating then
8272 Set_Associated_Entity (N, New_N);
8275 -- Clear any existing link the copy may inherit from the replicated
8276 -- generic template entity.
8278 Set_Associated_Entity (New_N, Empty);
8280 -- Special casing for stubs
8282 elsif Nkind (N) in N_Body_Stub then
8284 -- In any case, we must copy the specification or defining
8285 -- identifier as appropriate.
8287 if Nkind (N) = N_Subprogram_Body_Stub then
8288 Set_Specification (New_N,
8289 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8292 Set_Defining_Identifier (New_N,
8294 (Defining_Identifier (N), New_N, Instantiating));
8297 -- If we are not instantiating, then this is where we load and
8298 -- analyze subunits, i.e. at the point where the stub occurs. A
8299 -- more permissive system might defer this analysis to the point
8300 -- of instantiation, but this seems too complicated for now.
8302 if not Instantiating then
8304 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8306 Unum : Unit_Number_Type;
8310 -- Make sure that, if it is a subunit of the main unit that is
8311 -- preprocessed and if -gnateG is specified, the preprocessed
8312 -- file will be written.
8314 Lib.Analysing_Subunit_Of_Main :=
8315 Lib.In_Extended_Main_Source_Unit (N);
8318 (Load_Name => Subunit_Name,
8322 Lib.Analysing_Subunit_Of_Main := False;
8324 -- If the proper body is not found, a warning message will be
8325 -- emitted when analyzing the stub, or later at the point of
8326 -- instantiation. Here we just leave the stub as is.
8328 if Unum = No_Unit then
8329 Subunits_Missing := True;
8330 goto Subunit_Not_Found;
8333 Subunit := Cunit (Unum);
8335 if Nkind (Unit (Subunit)) /= N_Subunit then
8337 ("found child unit instead of expected SEPARATE subunit",
8339 Error_Msg_Sloc := Sloc (N);
8340 Error_Msg_N ("\to complete stub #", Subunit);
8341 goto Subunit_Not_Found;
8344 -- We must create a generic copy of the subunit, in order to
8345 -- perform semantic analysis on it, and we must replace the
8346 -- stub in the original generic unit with the subunit, in order
8347 -- to preserve non-local references within.
8349 -- Only the proper body needs to be copied. Library_Unit and
8350 -- context clause are simply inherited by the generic copy.
8351 -- Note that the copy (which may be recursive if there are
8352 -- nested subunits) must be done first, before attaching it to
8353 -- the enclosing generic.
8357 (Proper_Body (Unit (Subunit)),
8358 Empty, Instantiating => False);
8360 -- Now place the original proper body in the original generic
8361 -- unit. This is a body, not a compilation unit.
8363 Rewrite (N, Proper_Body (Unit (Subunit)));
8364 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8365 Set_Was_Originally_Stub (N);
8367 -- Finally replace the body of the subunit with its copy, and
8368 -- make this new subunit into the library unit of the generic
8369 -- copy, which does not have stubs any longer.
8371 Set_Proper_Body (Unit (Subunit), New_Body);
8372 Set_Library_Unit (New_N, Subunit);
8373 Inherit_Context (Unit (Subunit), N);
8376 -- If we are instantiating, this must be an error case, since
8377 -- otherwise we would have replaced the stub node by the proper body
8378 -- that corresponds. So just ignore it in the copy (i.e. we have
8379 -- copied it, and that is good enough).
8385 <<Subunit_Not_Found>> null;
8387 -- If the node is a compilation unit, it is the subunit of a stub, which
8388 -- has been loaded already (see code below). In this case, the library
8389 -- unit field of N points to the parent unit (which is a compilation
8390 -- unit) and need not (and cannot) be copied.
8392 -- When the proper body of the stub is analyzed, the library_unit link
8393 -- is used to establish the proper context (see sem_ch10).
8395 -- The other fields of a compilation unit are copied as usual
8397 elsif Nkind (N) = N_Compilation_Unit then
8399 -- This code can only be executed when not instantiating, because in
8400 -- the copy made for an instantiation, the compilation unit node has
8401 -- disappeared at the point that a stub is replaced by its proper
8404 pragma Assert (not Instantiating);
8406 Set_Context_Items (New_N,
8407 Copy_Generic_List (Context_Items (N), New_N));
8410 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8412 Set_First_Inlined_Subprogram (New_N,
8414 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8419 (Aux_Decls_Node (N), New_N, Instantiating => False));
8421 -- For an assignment node, the assignment is known to be semantically
8422 -- legal if we are instantiating the template. This avoids incorrect
8423 -- diagnostics in generated code.
8425 elsif Nkind (N) = N_Assignment_Statement then
8427 -- Copy name and expression fields in usual manner
8430 Copy_Generic_Node (Name (N), New_N, Instantiating));
8432 Set_Expression (New_N,
8433 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8435 if Instantiating then
8436 Set_Assignment_OK (Name (New_N), True);
8439 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8440 if not Instantiating then
8441 Set_Associated_Node (N, New_N);
8444 if Present (Get_Associated_Node (N))
8445 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8447 -- In the generic the aggregate has some composite type. If at
8448 -- the point of instantiation the type has a private view,
8449 -- install the full view (and that of its ancestors, if any).
8452 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8456 if Present (T) and then Is_Private_Type (T) then
8461 and then Is_Tagged_Type (T)
8462 and then Is_Derived_Type (T)
8464 Rt := Root_Type (T);
8469 if Is_Private_Type (T) then
8480 -- Do not copy the associated node, which points to the generic copy
8481 -- of the aggregate.
8482 -- ????We ought to be able to get rid of all the Union_Id conversions
8484 if Nkind (N) = N_Aggregate then
8485 Set_Aggregate_Bounds
8487 Node_Id (Copy_Generic_Descendant
8488 (Union_Id (Aggregate_Bounds (N)))));
8490 elsif Nkind (N) = N_Extension_Aggregate then
8493 Node_Id (Copy_Generic_Descendant
8494 (Union_Id (Ancestor_Part (N)))));
8497 pragma Assert (False);
8502 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8503 Set_Component_Associations
8505 List_Id (Copy_Generic_Descendant
8506 (Union_Id (Component_Associations (N)))));
8508 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8510 -- Allocators do not have an identifier denoting the access type, so we
8511 -- must locate it through the expression to check whether the views are
8514 elsif Nkind (N) = N_Allocator
8515 and then Nkind (Expression (N)) = N_Qualified_Expression
8516 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8517 and then Instantiating
8520 T : constant Node_Id :=
8521 Get_Associated_Node (Subtype_Mark (Expression (N)));
8527 -- Retrieve the allocator node in the generic copy
8529 Acc_T := Etype (Parent (Parent (T)));
8531 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8532 Switch_View (Acc_T);
8539 -- For a proper body, we must catch the case of a proper body that
8540 -- replaces a stub. This represents the point at which a separate
8541 -- compilation unit, and hence template file, may be referenced, so we
8542 -- must make a new source instantiation entry for the template of the
8543 -- subunit, and ensure that all nodes in the subunit are adjusted using
8544 -- this new source instantiation entry.
8546 elsif Nkind (N) in N_Proper_Body then
8548 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8550 if Instantiating and then Was_Originally_Stub (N) then
8551 Create_Instantiation_Source
8552 (Instantiation_Node,
8553 Defining_Entity (N),
8556 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8559 -- Now copy the fields of the proper body, using the new
8560 -- adjustment factor if one was needed as per test above.
8564 -- Restore the original adjustment factor
8566 S_Adjustment := Save_Adjustment;
8569 elsif Nkind (N) = N_Pragma and then Instantiating then
8571 -- Do not copy Comment or Ident pragmas their content is relevant to
8572 -- the generic unit, not to the instantiating unit.
8574 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8575 New_N := Make_Null_Statement (Sloc (N));
8577 -- Do not copy pragmas generated from aspects because the pragmas do
8578 -- not carry any semantic information, plus they will be regenerated
8581 -- However, generating C we need to copy them since postconditions
8582 -- are inlined by the front end, and the front-end inlining machinery
8583 -- relies on this routine to perform inlining.
8585 elsif From_Aspect_Specification (N)
8586 and then not Modify_Tree_For_C
8588 New_N := Make_Null_Statement (Sloc (N));
8594 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8596 -- No descendant fields need traversing
8600 elsif Nkind (N) = N_String_Literal
8601 and then Present (Etype (N))
8602 and then Instantiating
8604 -- If the string is declared in an outer scope, the string_literal
8605 -- subtype created for it may have the wrong scope. Force reanalysis
8606 -- of the constant to generate a new itype in the proper context.
8608 Set_Etype (New_N, Empty);
8609 Set_Analyzed (New_N, False);
8611 -- For the remaining nodes, copy their descendants recursively
8616 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8617 Set_Generic_Parent (Specification (New_N), N);
8619 -- Should preserve Corresponding_Spec??? (12.3(14))
8623 -- Propagate dimensions if present, so that they are reflected in the
8626 if Nkind (N) in N_Has_Etype
8627 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8628 and then Present (Etype (N))
8629 and then Is_Floating_Point_Type (Etype (N))
8630 and then Has_Dimension_System (Etype (N))
8632 Copy_Dimensions (N, New_N);
8636 end Copy_Generic_Node;
8638 ----------------------------
8639 -- Denotes_Formal_Package --
8640 ----------------------------
8642 function Denotes_Formal_Package
8644 On_Exit : Boolean := False;
8645 Instance : Entity_Id := Empty) return Boolean
8648 Scop : constant Entity_Id := Scope (Pack);
8651 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8652 -- The package in question may be an actual for a previous formal
8653 -- package P of the current instance, so examine its actuals as well.
8654 -- This must be recursive over other formal packages.
8656 ----------------------------------
8657 -- Is_Actual_Of_Previous_Formal --
8658 ----------------------------------
8660 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8664 E1 := First_Entity (P);
8665 while Present (E1) and then E1 /= Instance loop
8666 if Ekind (E1) = E_Package
8667 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8669 if Renamed_Object (E1) = Pack then
8672 elsif E1 = P or else Renamed_Object (E1) = P then
8675 elsif Is_Actual_Of_Previous_Formal (E1) then
8684 end Is_Actual_Of_Previous_Formal;
8686 -- Start of processing for Denotes_Formal_Package
8692 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8694 Par := Current_Instantiated_Parent.Act_Id;
8697 if Ekind (Scop) = E_Generic_Package
8698 or else Nkind (Unit_Declaration_Node (Scop)) =
8699 N_Generic_Subprogram_Declaration
8703 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8704 N_Formal_Package_Declaration
8712 -- Check whether this package is associated with a formal package of
8713 -- the enclosing instantiation. Iterate over the list of renamings.
8715 E := First_Entity (Par);
8716 while Present (E) loop
8717 if Ekind (E) /= E_Package
8718 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8722 elsif Renamed_Object (E) = Par then
8725 elsif Renamed_Object (E) = Pack then
8728 elsif Is_Actual_Of_Previous_Formal (E) then
8738 end Denotes_Formal_Package;
8744 procedure End_Generic is
8746 -- ??? More things could be factored out in this routine. Should
8747 -- probably be done at a later stage.
8749 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8750 Generic_Flags.Decrement_Last;
8752 Expander_Mode_Restore;
8759 function Earlier (N1, N2 : Node_Id) return Boolean is
8760 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8761 -- Find distance from given node to enclosing compilation unit
8767 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8770 and then Nkind (P) /= N_Compilation_Unit
8772 P := True_Parent (P);
8777 -- Local declarations
8786 -- Start of processing for Earlier
8789 Find_Depth (P1, D1);
8790 Find_Depth (P2, D2);
8800 P1 := True_Parent (P1);
8805 P2 := True_Parent (P2);
8809 -- At this point P1 and P2 are at the same distance from the root.
8810 -- We examine their parents until we find a common declarative list.
8811 -- If we reach the root, N1 and N2 do not descend from the same
8812 -- declarative list (e.g. one is nested in the declarative part and
8813 -- the other is in a block in the statement part) and the earlier
8814 -- one is already frozen.
8816 while not Is_List_Member (P1)
8817 or else not Is_List_Member (P2)
8818 or else not In_Same_List (P1, P2)
8820 P1 := True_Parent (P1);
8821 P2 := True_Parent (P2);
8823 if Nkind (Parent (P1)) = N_Subunit then
8824 P1 := Corresponding_Stub (Parent (P1));
8827 if Nkind (Parent (P2)) = N_Subunit then
8828 P2 := Corresponding_Stub (Parent (P2));
8836 -- Expanded code usually shares the source location of the original
8837 -- construct it was generated for. This however may not necessarily
8838 -- reflect the true location of the code within the tree.
8840 -- Before comparing the slocs of the two nodes, make sure that we are
8841 -- working with correct source locations. Assume that P1 is to the left
8842 -- of P2. If either one does not come from source, traverse the common
8843 -- list heading towards the other node and locate the first source
8847 -- ----+===+===+--------------+===+===+----
8848 -- expanded code expanded code
8850 if not Comes_From_Source (P1) then
8851 while Present (P1) loop
8853 -- Neither P2 nor a source statement were located during the
8854 -- search. If we reach the end of the list, then P1 does not
8855 -- occur earlier than P2.
8858 -- start --- P2 ----- P1 --- end
8860 if No (Next (P1)) then
8863 -- We encounter P2 while going to the right of the list. This
8864 -- means that P1 does indeed appear earlier.
8867 -- start --- P1 ===== P2 --- end
8868 -- expanded code in between
8873 -- No need to look any further since we have located a source
8876 elsif Comes_From_Source (P1) then
8886 if not Comes_From_Source (P2) then
8887 while Present (P2) loop
8889 -- Neither P1 nor a source statement were located during the
8890 -- search. If we reach the start of the list, then P1 does not
8891 -- occur earlier than P2.
8894 -- start --- P2 --- P1 --- end
8896 if No (Prev (P2)) then
8899 -- We encounter P1 while going to the left of the list. This
8900 -- means that P1 does indeed appear earlier.
8903 -- start --- P1 ===== P2 --- end
8904 -- expanded code in between
8909 -- No need to look any further since we have located a source
8912 elsif Comes_From_Source (P2) then
8922 -- At this point either both nodes came from source or we approximated
8923 -- their source locations through neighboring source statements.
8925 T1 := Top_Level_Location (Sloc (P1));
8926 T2 := Top_Level_Location (Sloc (P2));
8928 -- When two nodes come from the same instance, they have identical top
8929 -- level locations. To determine proper relation within the tree, check
8930 -- their locations within the template.
8933 return Sloc (P1) < Sloc (P2);
8935 -- The two nodes either come from unrelated instances or do not come
8936 -- from instantiated code at all.
8943 ----------------------
8944 -- Find_Actual_Type --
8945 ----------------------
8947 function Find_Actual_Type
8949 Gen_Type : Entity_Id) return Entity_Id
8951 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8955 -- Special processing only applies to child units
8957 if not Is_Child_Unit (Gen_Scope) then
8958 return Get_Instance_Of (Typ);
8960 -- If designated or component type is itself a formal of the child unit,
8961 -- its instance is available.
8963 elsif Scope (Typ) = Gen_Scope then
8964 return Get_Instance_Of (Typ);
8966 -- If the array or access type is not declared in the parent unit,
8967 -- no special processing needed.
8969 elsif not Is_Generic_Type (Typ)
8970 and then Scope (Gen_Scope) /= Scope (Typ)
8972 return Get_Instance_Of (Typ);
8974 -- Otherwise, retrieve designated or component type by visibility
8977 T := Current_Entity (Typ);
8978 while Present (T) loop
8979 if In_Open_Scopes (Scope (T)) then
8981 elsif Is_Generic_Actual_Type (T) then
8990 end Find_Actual_Type;
8992 ----------------------------
8993 -- Freeze_Subprogram_Body --
8994 ----------------------------
8996 procedure Freeze_Subprogram_Body
8997 (Inst_Node : Node_Id;
8999 Pack_Id : Entity_Id)
9001 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
9002 Par : constant Entity_Id := Scope (Gen_Unit);
9008 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9009 -- Find innermost package body that encloses the given node, and which
9010 -- is not a compilation unit. Freeze nodes for the instance, or for its
9011 -- enclosing body, may be inserted after the enclosing_body of the
9012 -- generic unit. Used to determine proper placement of freeze node for
9013 -- both package and subprogram instances.
9015 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9016 -- Find entity for given package body, and locate or create a freeze
9019 ----------------------------
9020 -- Enclosing_Package_Body --
9021 ----------------------------
9023 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9029 and then Nkind (Parent (P)) /= N_Compilation_Unit
9031 if Nkind (P) = N_Package_Body then
9032 if Nkind (Parent (P)) = N_Subunit then
9033 return Corresponding_Stub (Parent (P));
9039 P := True_Parent (P);
9043 end Enclosing_Package_Body;
9045 -------------------------
9046 -- Package_Freeze_Node --
9047 -------------------------
9049 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9053 if Nkind (B) = N_Package_Body then
9054 Id := Corresponding_Spec (B);
9055 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9056 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9059 Ensure_Freeze_Node (Id);
9060 return Freeze_Node (Id);
9061 end Package_Freeze_Node;
9063 -- Start of processing for Freeze_Subprogram_Body
9066 -- If the instance and the generic body appear within the same unit, and
9067 -- the instance precedes the generic, the freeze node for the instance
9068 -- must appear after that of the generic. If the generic is nested
9069 -- within another instance I2, then current instance must be frozen
9070 -- after I2. In both cases, the freeze nodes are those of enclosing
9071 -- packages. Otherwise, the freeze node is placed at the end of the
9072 -- current declarative part.
9074 Enc_G := Enclosing_Package_Body (Gen_Body);
9075 Enc_I := Enclosing_Package_Body (Inst_Node);
9076 Ensure_Freeze_Node (Pack_Id);
9077 F_Node := Freeze_Node (Pack_Id);
9079 if Is_Generic_Instance (Par)
9080 and then Present (Freeze_Node (Par))
9081 and then In_Same_Declarative_Part
9082 (Parent (Freeze_Node (Par)), Inst_Node)
9084 -- The parent was a premature instantiation. Insert freeze node at
9085 -- the end the current declarative part.
9087 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9088 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9090 -- Handle the following case:
9092 -- package Parent_Inst is new ...
9095 -- procedure P ... -- this body freezes Parent_Inst
9097 -- procedure Inst is new ...
9099 -- In this particular scenario, the freeze node for Inst must be
9100 -- inserted in the same manner as that of Parent_Inst - before the
9101 -- next source body or at the end of the declarative list (body not
9102 -- available). If body P did not exist and Parent_Inst was frozen
9103 -- after Inst, either by a body following Inst or at the end of the
9104 -- declarative region, the freeze node for Inst must be inserted
9105 -- after that of Parent_Inst. This relation is established by
9106 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9108 elsif In_Same_List (Get_Unit_Instantiation_Node (Par), Inst_Node)
9109 and then Sloc (Freeze_Node (Par)) <= Sloc (Inst_Node)
9111 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9114 Insert_After (Freeze_Node (Par), F_Node);
9117 -- The body enclosing the instance should be frozen after the body that
9118 -- includes the generic, because the body of the instance may make
9119 -- references to entities therein. If the two are not in the same
9120 -- declarative part, or if the one enclosing the instance is frozen
9121 -- already, freeze the instance at the end of the current declarative
9124 elsif Is_Generic_Instance (Par)
9125 and then Present (Freeze_Node (Par))
9126 and then Present (Enc_I)
9128 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9129 -- The enclosing package may contain several instances. Rather
9130 -- than computing the earliest point at which to insert its freeze
9131 -- node, we place it at the end of the declarative part of the
9132 -- parent of the generic.
9134 Insert_Freeze_Node_For_Instance
9135 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9138 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9140 elsif Present (Enc_G)
9141 and then Present (Enc_I)
9142 and then Enc_G /= Enc_I
9143 and then Earlier (Inst_Node, Gen_Body)
9145 -- Freeze package that encloses instance, and place node after the
9146 -- package that encloses generic. If enclosing package is already
9147 -- frozen we have to assume it is at the proper place. This may be a
9148 -- potential ABE that requires dynamic checking. Do not add a freeze
9149 -- node if the package that encloses the generic is inside the body
9150 -- that encloses the instance, because the freeze node would be in
9151 -- the wrong scope. Additional contortions needed if the bodies are
9152 -- within a subunit.
9155 Enclosing_Body : Node_Id;
9158 if Nkind (Enc_I) = N_Package_Body_Stub then
9159 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9161 Enclosing_Body := Enc_I;
9164 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9165 Insert_Freeze_Node_For_Instance
9166 (Enc_G, Package_Freeze_Node (Enc_I));
9170 -- Freeze enclosing subunit before instance
9172 Enc_G_F := Package_Freeze_Node (Enc_G);
9174 if not Is_List_Member (Enc_G_F) then
9175 Insert_After (Enc_G, Enc_G_F);
9178 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9181 -- If none of the above, insert freeze node at the end of the current
9182 -- declarative part.
9184 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9186 end Freeze_Subprogram_Body;
9192 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9194 return Generic_Renamings.Table (E).Gen_Id;
9197 ---------------------
9198 -- Get_Instance_Of --
9199 ---------------------
9201 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9202 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9205 if Res /= Assoc_Null then
9206 return Generic_Renamings.Table (Res).Act_Id;
9209 -- On exit, entity is not instantiated: not a generic parameter, or
9210 -- else parameter of an inner generic unit.
9214 end Get_Instance_Of;
9216 ---------------------------------
9217 -- Get_Unit_Instantiation_Node --
9218 ---------------------------------
9220 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9221 Decl : Node_Id := Unit_Declaration_Node (A);
9225 -- If the Package_Instantiation attribute has been set on the package
9226 -- entity, then use it directly when it (or its Original_Node) refers
9227 -- to an N_Package_Instantiation node. In principle it should be
9228 -- possible to have this field set in all cases, which should be
9229 -- investigated, and would allow this function to be significantly
9232 Inst := Package_Instantiation (A);
9234 if Present (Inst) then
9235 if Nkind (Inst) = N_Package_Instantiation then
9238 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9239 return Original_Node (Inst);
9243 -- If the instantiation is a compilation unit that does not need body
9244 -- then the instantiation node has been rewritten as a package
9245 -- declaration for the instance, and we return the original node.
9247 -- If it is a compilation unit and the instance node has not been
9248 -- rewritten, then it is still the unit of the compilation. Finally, if
9249 -- a body is present, this is a parent of the main unit whose body has
9250 -- been compiled for inlining purposes, and the instantiation node has
9251 -- been rewritten with the instance body.
9253 -- Otherwise the instantiation node appears after the declaration. If
9254 -- the entity is a formal package, the declaration may have been
9255 -- rewritten as a generic declaration (in the case of a formal with box)
9256 -- or left as a formal package declaration if it has actuals, and is
9257 -- found with a forward search.
9259 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9260 if Nkind (Decl) = N_Package_Declaration
9261 and then Present (Corresponding_Body (Decl))
9263 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9266 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9267 return Original_Node (Decl);
9269 return Unit (Parent (Decl));
9272 elsif Nkind (Decl) = N_Package_Declaration
9273 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9275 return Original_Node (Decl);
9278 Inst := Next (Decl);
9279 while Nkind (Inst) not in N_Formal_Package_Declaration
9280 | N_Function_Instantiation
9281 | N_Package_Instantiation
9282 | N_Procedure_Instantiation
9289 end Get_Unit_Instantiation_Node;
9291 ------------------------
9292 -- Has_Been_Exchanged --
9293 ------------------------
9295 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9299 Next := First_Elmt (Exchanged_Views);
9300 while Present (Next) loop
9301 if Full_View (Node (Next)) = E then
9309 end Has_Been_Exchanged;
9315 function Has_Contracts (Decl : Node_Id) return Boolean is
9316 A_List : constant List_Id := Aspect_Specifications (Decl);
9323 A_Spec := First (A_List);
9324 while Present (A_Spec) loop
9325 A_Id := Get_Aspect_Id (A_Spec);
9326 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9341 function Hash (F : Entity_Id) return HTable_Range is
9343 return HTable_Range (F mod HTable_Size);
9346 ------------------------
9347 -- Hide_Current_Scope --
9348 ------------------------
9350 procedure Hide_Current_Scope is
9351 C : constant Entity_Id := Current_Scope;
9355 Set_Is_Hidden_Open_Scope (C);
9357 E := First_Entity (C);
9358 while Present (E) loop
9359 if Is_Immediately_Visible (E) then
9360 Set_Is_Immediately_Visible (E, False);
9361 Append_Elmt (E, Hidden_Entities);
9367 -- Make the scope name invisible as well. This is necessary, but might
9368 -- conflict with calls to Rtsfind later on, in case the scope is a
9369 -- predefined one. There is no clean solution to this problem, so for
9370 -- now we depend on the user not redefining Standard itself in one of
9371 -- the parent units.
9373 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9374 Set_Is_Immediately_Visible (C, False);
9375 Append_Elmt (C, Hidden_Entities);
9378 end Hide_Current_Scope;
9384 procedure Init_Env is
9385 Saved : Instance_Env;
9388 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9389 Saved.Exchanged_Views := Exchanged_Views;
9390 Saved.Hidden_Entities := Hidden_Entities;
9391 Saved.Current_Sem_Unit := Current_Sem_Unit;
9392 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9393 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9395 -- Save configuration switches. These may be reset if the unit is a
9396 -- predefined unit, and the current mode is not Ada 2005.
9398 Saved.Switches := Save_Config_Switches;
9400 Instance_Envs.Append (Saved);
9402 Exchanged_Views := New_Elmt_List;
9403 Hidden_Entities := New_Elmt_List;
9405 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9406 -- this is set properly in Set_Instance_Env.
9408 Current_Instantiated_Parent :=
9409 (Current_Scope, Current_Scope, Assoc_Null);
9412 ---------------------
9413 -- In_Main_Context --
9414 ---------------------
9416 function In_Main_Context (E : Entity_Id) return Boolean is
9422 if not Is_Compilation_Unit (E)
9423 or else Ekind (E) /= E_Package
9424 or else In_Private_Part (E)
9429 Context := Context_Items (Cunit (Main_Unit));
9431 Clause := First (Context);
9432 while Present (Clause) loop
9433 if Nkind (Clause) = N_With_Clause then
9434 Nam := Name (Clause);
9436 -- If the current scope is part of the context of the main unit,
9437 -- analysis of the corresponding with_clause is not complete, and
9438 -- the entity is not set. We use the Chars field directly, which
9439 -- might produce false positives in rare cases, but guarantees
9440 -- that we produce all the instance bodies we will need.
9442 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9443 or else (Nkind (Nam) = N_Selected_Component
9444 and then Chars (Selector_Name (Nam)) = Chars (E))
9454 end In_Main_Context;
9456 ---------------------
9457 -- Inherit_Context --
9458 ---------------------
9460 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9461 Current_Context : List_Id;
9462 Current_Unit : Node_Id;
9471 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9473 -- The inherited context is attached to the enclosing compilation
9474 -- unit. This is either the main unit, or the declaration for the
9475 -- main unit (in case the instantiation appears within the package
9476 -- declaration and the main unit is its body).
9478 Current_Unit := Parent (Inst);
9479 while Present (Current_Unit)
9480 and then Nkind (Current_Unit) /= N_Compilation_Unit
9482 Current_Unit := Parent (Current_Unit);
9485 Current_Context := Context_Items (Current_Unit);
9487 Item := First (Context_Items (Parent (Gen_Decl)));
9488 while Present (Item) loop
9489 if Nkind (Item) = N_With_Clause then
9490 Lib_Unit := Library_Unit (Item);
9492 -- Take care to prevent direct cyclic with's
9494 if Lib_Unit /= Current_Unit then
9496 -- Do not add a unit if it is already in the context
9498 Clause := First (Current_Context);
9500 while Present (Clause) loop
9501 if Nkind (Clause) = N_With_Clause
9502 and then Library_Unit (Clause) = Lib_Unit
9512 New_I := New_Copy (Item);
9513 Set_Implicit_With (New_I);
9515 Append (New_I, Current_Context);
9523 end Inherit_Context;
9529 procedure Initialize is
9531 Generic_Renamings.Init;
9534 Generic_Renamings_HTable.Reset;
9535 Circularity_Detected := False;
9536 Exchanged_Views := No_Elist;
9537 Hidden_Entities := No_Elist;
9540 -------------------------------------
9541 -- Insert_Freeze_Node_For_Instance --
9542 -------------------------------------
9544 procedure Insert_Freeze_Node_For_Instance
9553 function Enclosing_Body (N : Node_Id) return Node_Id;
9554 -- Find enclosing package or subprogram body, if any. Freeze node may
9555 -- be placed at end of current declarative list if previous instance
9556 -- and current one have different enclosing bodies.
9558 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9559 -- Find the local instance, if any, that declares the generic that is
9560 -- being instantiated. If present, the freeze node for this instance
9561 -- must follow the freeze node for the previous instance.
9563 --------------------
9564 -- Enclosing_Body --
9565 --------------------
9567 function Enclosing_Body (N : Node_Id) return Node_Id is
9573 and then Nkind (Parent (P)) /= N_Compilation_Unit
9575 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9576 if Nkind (Parent (P)) = N_Subunit then
9577 return Corresponding_Stub (Parent (P));
9583 P := True_Parent (P);
9589 -----------------------
9590 -- Previous_Instance --
9591 -----------------------
9593 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9598 while Present (S) and then S /= Standard_Standard loop
9599 if Is_Generic_Instance (S)
9600 and then In_Same_Source_Unit (S, N)
9609 end Previous_Instance;
9611 -- Start of processing for Insert_Freeze_Node_For_Instance
9614 if not Is_List_Member (F_Node) then
9616 Decls := List_Containing (N);
9617 Inst := Entity (F_Node);
9618 Par_N := Parent (Decls);
9620 -- When processing a subprogram instantiation, utilize the actual
9621 -- subprogram instantiation rather than its package wrapper as it
9622 -- carries all the context information.
9624 if Is_Wrapper_Package (Inst) then
9625 Inst := Related_Instance (Inst);
9628 -- If this is a package instance, check whether the generic is
9629 -- declared in a previous instance and the current instance is
9630 -- not within the previous one.
9632 if Present (Generic_Parent (Parent (Inst)))
9633 and then Is_In_Main_Unit (N)
9636 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9637 Par_I : constant Entity_Id :=
9639 (Generic_Parent (Parent (Inst)));
9644 and then Earlier (N, Freeze_Node (Par_I))
9646 Scop := Scope (Inst);
9648 -- If the current instance is within the one that contains
9649 -- the generic, the freeze node for the current one must
9650 -- appear in the current declarative part. Ditto, if the
9651 -- current instance is within another package instance or
9652 -- within a body that does not enclose the current instance.
9653 -- In these three cases the freeze node of the previous
9654 -- instance is not relevant.
9656 while Present (Scop) and then Scop /= Standard_Standard loop
9657 exit when Scop = Par_I
9659 (Is_Generic_Instance (Scop)
9660 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9661 Scop := Scope (Scop);
9664 -- Previous instance encloses current instance
9666 if Scop = Par_I then
9669 -- If the next node is a source body we must freeze in
9670 -- the current scope as well.
9672 elsif Present (Next (N))
9673 and then Nkind (Next (N)) in N_Subprogram_Body
9675 and then Comes_From_Source (Next (N))
9679 -- Current instance is within an unrelated instance
9681 elsif Is_Generic_Instance (Scop) then
9684 -- Current instance is within an unrelated body
9686 elsif Present (Enclosing_N)
9687 and then Enclosing_N /= Enclosing_Body (Par_I)
9692 Insert_After (Freeze_Node (Par_I), F_Node);
9699 -- When the instantiation occurs in a package declaration, append the
9700 -- freeze node to the private declarations (if any).
9702 if Nkind (Par_N) = N_Package_Specification
9703 and then Decls = Visible_Declarations (Par_N)
9704 and then Present (Private_Declarations (Par_N))
9705 and then not Is_Empty_List (Private_Declarations (Par_N))
9707 Decls := Private_Declarations (Par_N);
9708 Decl := First (Decls);
9711 -- Determine the proper freeze point of a package instantiation. We
9712 -- adhere to the general rule of a package or subprogram body causing
9713 -- freezing of anything before it in the same declarative region. In
9714 -- this case, the proper freeze point of a package instantiation is
9715 -- before the first source body which follows, or before a stub. This
9716 -- ensures that entities coming from the instance are already frozen
9717 -- and usable in source bodies.
9719 if Nkind (Par_N) /= N_Package_Declaration
9720 and then Ekind (Inst) = E_Package
9721 and then Is_Generic_Instance (Inst)
9723 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9725 while Present (Decl) loop
9726 if (Nkind (Decl) in N_Unit_Body
9728 Nkind (Decl) in N_Body_Stub)
9729 and then Comes_From_Source (Decl)
9731 Insert_Before (Decl, F_Node);
9739 -- In a package declaration, or if no previous body, insert at end
9742 Set_Sloc (F_Node, Sloc (Last (Decls)));
9743 Insert_After (Last (Decls), F_Node);
9745 end Insert_Freeze_Node_For_Instance;
9751 procedure Install_Body
9752 (Act_Body : Node_Id;
9757 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9758 -- Check if the generic definition and the instantiation come from
9759 -- a common scope, in which case the instance must be frozen after
9760 -- the generic body.
9762 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9763 -- If the instance is nested inside a generic unit, the Sloc of the
9764 -- instance indicates the place of the original definition, not the
9765 -- point of the current enclosing instance. Pending a better usage of
9766 -- Slocs to indicate instantiation places, we determine the place of
9767 -- origin of a node by finding the maximum sloc of any ancestor node.
9768 -- Why is this not equivalent to Top_Level_Location ???
9774 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9775 Act_Scop : Entity_Id := Scope (Act_Id);
9776 Gen_Scop : Entity_Id := Scope (Gen_Id);
9779 while Act_Scop /= Standard_Standard
9780 and then Gen_Scop /= Standard_Standard
9782 if Act_Scop = Gen_Scop then
9786 Act_Scop := Scope (Act_Scop);
9787 Gen_Scop := Scope (Gen_Scop);
9797 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9804 while Present (N1) and then N1 /= Act_Unit loop
9805 if Sloc (N1) > Res then
9815 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9816 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9817 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9818 Par : constant Entity_Id := Scope (Gen_Id);
9819 Gen_Unit : constant Node_Id :=
9820 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9822 Body_Unit : Node_Id;
9824 Must_Delay : Boolean;
9825 Orig_Body : Node_Id := Gen_Body;
9827 -- Start of processing for Install_Body
9830 -- Handle first the case of an instance with incomplete actual types.
9831 -- The instance body cannot be placed after the declaration because
9832 -- full views have not been seen yet. Any use of the non-limited views
9833 -- in the instance body requires the presence of a regular with_clause
9834 -- in the enclosing unit, and will fail if this with_clause is missing.
9835 -- We place the instance body at the beginning of the enclosing body,
9836 -- which is the unit being compiled. The freeze node for the instance
9837 -- is then placed after the instance body.
9839 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9840 and then Expander_Active
9841 and then Ekind (Scope (Act_Id)) = E_Package
9844 Scop : constant Entity_Id := Scope (Act_Id);
9845 Body_Id : constant Node_Id :=
9846 Corresponding_Body (Unit_Declaration_Node (Scop));
9849 Ensure_Freeze_Node (Act_Id);
9850 F_Node := Freeze_Node (Act_Id);
9851 if Present (Body_Id) then
9852 Set_Is_Frozen (Act_Id, False);
9853 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9854 if Is_List_Member (F_Node) then
9858 Insert_After (Act_Body, F_Node);
9864 -- If the body is a subunit, the freeze point is the corresponding stub
9865 -- in the current compilation, not the subunit itself.
9867 if Nkind (Parent (Gen_Body)) = N_Subunit then
9868 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9870 Orig_Body := Gen_Body;
9873 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9875 -- If the instantiation and the generic definition appear in the same
9876 -- package declaration, this is an early instantiation. If they appear
9877 -- in the same declarative part, it is an early instantiation only if
9878 -- the generic body appears textually later, and the generic body is
9879 -- also in the main unit.
9881 -- If instance is nested within a subprogram, and the generic body
9882 -- is not, the instance is delayed because the enclosing body is. If
9883 -- instance and body are within the same scope, or the same subprogram
9884 -- body, indicate explicitly that the instance is delayed.
9887 (Gen_Unit = Act_Unit
9888 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9889 | N_Package_Declaration
9890 or else (Gen_Unit = Body_Unit
9891 and then True_Sloc (N, Act_Unit) <
9893 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9894 and then In_Same_Scope (Gen_Id, Act_Id));
9896 -- If this is an early instantiation, the freeze node is placed after
9897 -- the generic body. Otherwise, if the generic appears in an instance,
9898 -- we cannot freeze the current instance until the outer one is frozen.
9899 -- This is only relevant if the current instance is nested within some
9900 -- inner scope not itself within the outer instance. If this scope is
9901 -- a package body in the same declarative part as the outer instance,
9902 -- then that body needs to be frozen after the outer instance. Finally,
9903 -- if no delay is needed, we place the freeze node at the end of the
9904 -- current declarative part.
9907 and then (No (Freeze_Node (Act_Id))
9908 or else not Is_List_Member (Freeze_Node (Act_Id)))
9910 Ensure_Freeze_Node (Act_Id);
9911 F_Node := Freeze_Node (Act_Id);
9914 Insert_After (Orig_Body, F_Node);
9916 elsif Is_Generic_Instance (Par)
9917 and then Present (Freeze_Node (Par))
9918 and then Scope (Act_Id) /= Par
9920 -- Freeze instance of inner generic after instance of enclosing
9923 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9925 -- Handle the following case:
9927 -- package Parent_Inst is new ...
9930 -- procedure P ... -- this body freezes Parent_Inst
9932 -- package Inst is new ...
9934 -- In this particular scenario, the freeze node for Inst must
9935 -- be inserted in the same manner as that of Parent_Inst,
9936 -- before the next source body or at the end of the declarative
9937 -- list (body not available). If body P did not exist and
9938 -- Parent_Inst was frozen after Inst, either by a body
9939 -- following Inst or at the end of the declarative region,
9940 -- the freeze node for Inst must be inserted after that of
9941 -- Parent_Inst. This relation is established by comparing
9942 -- the Slocs of Parent_Inst freeze node and Inst.
9943 -- We examine the parents of the enclosing lists to handle
9944 -- the case where the parent instance is in the visible part
9945 -- of a package declaration, and the inner instance is in
9946 -- the corresponding private part.
9948 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9949 = Parent (List_Containing (N))
9950 and then Sloc (Freeze_Node (Par)) <= Sloc (N)
9952 Insert_Freeze_Node_For_Instance (N, F_Node);
9954 Insert_After (Freeze_Node (Par), F_Node);
9957 -- Freeze package enclosing instance of inner generic after
9958 -- instance of enclosing generic.
9960 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9961 and then In_Same_Declarative_Part
9962 (Parent (Freeze_Node (Par)), Parent (N))
9965 Enclosing : Entity_Id;
9968 Enclosing := Corresponding_Spec (Parent (N));
9970 if No (Enclosing) then
9971 Enclosing := Defining_Entity (Parent (N));
9974 Insert_Freeze_Node_For_Instance (N, F_Node);
9975 Ensure_Freeze_Node (Enclosing);
9977 if not Is_List_Member (Freeze_Node (Enclosing)) then
9979 -- The enclosing context is a subunit, insert the freeze
9980 -- node after the stub.
9982 if Nkind (Parent (Parent (N))) = N_Subunit then
9983 Insert_Freeze_Node_For_Instance
9984 (Corresponding_Stub (Parent (Parent (N))),
9985 Freeze_Node (Enclosing));
9987 -- The enclosing context is a package with a stub body
9988 -- which has already been replaced by the real body.
9989 -- Insert the freeze node after the actual body.
9991 elsif Ekind (Enclosing) = E_Package
9992 and then Present (Body_Entity (Enclosing))
9993 and then Was_Originally_Stub
9994 (Parent (Body_Entity (Enclosing)))
9996 Insert_Freeze_Node_For_Instance
9997 (Parent (Body_Entity (Enclosing)),
9998 Freeze_Node (Enclosing));
10000 -- The parent instance has been frozen before the body of
10001 -- the enclosing package, insert the freeze node after
10004 elsif In_Same_List (Freeze_Node (Par), Parent (N))
10005 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
10007 Insert_Freeze_Node_For_Instance
10008 (Parent (N), Freeze_Node (Enclosing));
10012 (Freeze_Node (Par), Freeze_Node (Enclosing));
10018 Insert_Freeze_Node_For_Instance (N, F_Node);
10022 Insert_Freeze_Node_For_Instance (N, F_Node);
10026 Set_Is_Frozen (Act_Id);
10027 Insert_Before (N, Act_Body);
10028 Mark_Rewrite_Insertion (Act_Body);
10031 -----------------------------
10032 -- Install_Formal_Packages --
10033 -----------------------------
10035 procedure Install_Formal_Packages (Par : Entity_Id) is
10038 Gen_E : Entity_Id := Empty;
10041 E := First_Entity (Par);
10043 -- If we are installing an instance parent, locate the formal packages
10044 -- of its generic parent.
10046 if Is_Generic_Instance (Par) then
10047 Gen := Generic_Parent (Package_Specification (Par));
10048 Gen_E := First_Entity (Gen);
10051 while Present (E) loop
10052 if Ekind (E) = E_Package
10053 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10055 -- If this is the renaming for the parent instance, done
10057 if Renamed_Object (E) = Par then
10060 -- The visibility of a formal of an enclosing generic is already
10063 elsif Denotes_Formal_Package (E) then
10066 elsif Present (Associated_Formal_Package (E)) then
10067 Check_Generic_Actuals (Renamed_Object (E), True);
10068 Set_Is_Hidden (E, False);
10070 -- Find formal package in generic unit that corresponds to
10071 -- (instance of) formal package in instance.
10073 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10074 Next_Entity (Gen_E);
10077 if Present (Gen_E) then
10078 Map_Formal_Package_Entities (Gen_E, E);
10085 if Present (Gen_E) then
10086 Next_Entity (Gen_E);
10089 end Install_Formal_Packages;
10091 --------------------
10092 -- Install_Parent --
10093 --------------------
10095 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10096 Ancestors : constant Elist_Id := New_Elmt_List;
10097 S : constant Entity_Id := Current_Scope;
10098 Inst_Par : Entity_Id;
10099 First_Par : Entity_Id;
10100 Inst_Node : Node_Id;
10101 Gen_Par : Entity_Id;
10102 First_Gen : Entity_Id;
10105 procedure Install_Noninstance_Specs (Par : Entity_Id);
10106 -- Install the scopes of noninstance parent units ending with Par
10108 procedure Install_Spec (Par : Entity_Id);
10109 -- The child unit is within the declarative part of the parent, so the
10110 -- declarations within the parent are immediately visible.
10112 -------------------------------
10113 -- Install_Noninstance_Specs --
10114 -------------------------------
10116 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10119 and then Par /= Standard_Standard
10120 and then not In_Open_Scopes (Par)
10122 Install_Noninstance_Specs (Scope (Par));
10123 Install_Spec (Par);
10125 end Install_Noninstance_Specs;
10131 procedure Install_Spec (Par : Entity_Id) is
10132 Spec : constant Node_Id := Package_Specification (Par);
10135 -- If this parent of the child instance is a top-level unit,
10136 -- then record the unit and its visibility for later resetting in
10137 -- Remove_Parent. We exclude units that are generic instances, as we
10138 -- only want to record this information for the ultimate top-level
10139 -- noninstance parent (is that always correct???).
10141 if Scope (Par) = Standard_Standard
10142 and then not Is_Generic_Instance (Par)
10144 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10145 Instance_Parent_Unit := Par;
10148 -- Open the parent scope and make it and its declarations visible.
10149 -- If this point is not within a body, then only the visible
10150 -- declarations should be made visible, and installation of the
10151 -- private declarations is deferred until the appropriate point
10152 -- within analysis of the spec being instantiated (see the handling
10153 -- of parent visibility in Analyze_Package_Specification). This is
10154 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10155 -- private view problems that occur when compiling instantiations of
10156 -- a generic child of that package (Generic_Dispatching_Constructor).
10157 -- If the instance freezes a tagged type, inlinings of operations
10158 -- from Ada.Tags may need the full view of type Tag. If inlining took
10159 -- proper account of establishing visibility of inlined subprograms'
10160 -- parents then it should be possible to remove this
10161 -- special check. ???
10164 Set_Is_Immediately_Visible (Par);
10165 Install_Visible_Declarations (Par);
10166 Set_Use (Visible_Declarations (Spec));
10168 if In_Body or else Is_RTU (Par, Ada_Tags) then
10169 Install_Private_Declarations (Par);
10170 Set_Use (Private_Declarations (Spec));
10174 -- Start of processing for Install_Parent
10177 -- We need to install the parent instance to compile the instantiation
10178 -- of the child, but the child instance must appear in the current
10179 -- scope. Given that we cannot place the parent above the current scope
10180 -- in the scope stack, we duplicate the current scope and unstack both
10181 -- after the instantiation is complete.
10183 -- If the parent is itself the instantiation of a child unit, we must
10184 -- also stack the instantiation of its parent, and so on. Each such
10185 -- ancestor is the prefix of the name in a prior instantiation.
10187 -- If this is a nested instance, the parent unit itself resolves to
10188 -- a renaming of the parent instance, whose declaration we need.
10190 -- Finally, the parent may be a generic (not an instance) when the
10191 -- child unit appears as a formal package.
10195 if Present (Renamed_Entity (Inst_Par)) then
10196 Inst_Par := Renamed_Entity (Inst_Par);
10199 First_Par := Inst_Par;
10201 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10203 First_Gen := Gen_Par;
10205 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10207 -- Load grandparent instance as well
10209 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10211 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10212 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10214 if Present (Renamed_Entity (Inst_Par)) then
10215 Inst_Par := Renamed_Entity (Inst_Par);
10218 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10220 if Present (Gen_Par) then
10221 Prepend_Elmt (Inst_Par, Ancestors);
10224 -- Parent is not the name of an instantiation
10226 Install_Noninstance_Specs (Inst_Par);
10237 if Present (First_Gen) then
10238 Append_Elmt (First_Par, Ancestors);
10240 Install_Noninstance_Specs (First_Par);
10243 if not Is_Empty_Elmt_List (Ancestors) then
10244 Elmt := First_Elmt (Ancestors);
10245 while Present (Elmt) loop
10246 Install_Spec (Node (Elmt));
10247 Install_Formal_Packages (Node (Elmt));
10252 if not In_Body then
10255 end Install_Parent;
10257 -------------------------------
10258 -- Install_Hidden_Primitives --
10259 -------------------------------
10261 procedure Install_Hidden_Primitives
10262 (Prims_List : in out Elist_Id;
10267 List : Elist_Id := No_Elist;
10268 Prim_G_Elmt : Elmt_Id;
10269 Prim_A_Elmt : Elmt_Id;
10274 -- No action needed in case of serious errors because we cannot trust
10275 -- in the order of primitives
10277 if Serious_Errors_Detected > 0 then
10280 -- No action possible if we don't have available the list of primitive
10284 or else not Is_Record_Type (Gen_T)
10285 or else not Is_Tagged_Type (Gen_T)
10286 or else not Is_Record_Type (Act_T)
10287 or else not Is_Tagged_Type (Act_T)
10291 -- There is no need to handle interface types since their primitives
10292 -- cannot be hidden
10294 elsif Is_Interface (Gen_T) then
10298 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10300 if not Is_Class_Wide_Type (Act_T) then
10301 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10303 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10307 -- Skip predefined primitives in the generic formal
10309 while Present (Prim_G_Elmt)
10310 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10312 Next_Elmt (Prim_G_Elmt);
10315 -- Skip predefined primitives in the generic actual
10317 while Present (Prim_A_Elmt)
10318 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10320 Next_Elmt (Prim_A_Elmt);
10323 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10325 Prim_G := Node (Prim_G_Elmt);
10326 Prim_A := Node (Prim_A_Elmt);
10328 -- There is no need to handle interface primitives because their
10329 -- primitives are not hidden
10331 exit when Present (Interface_Alias (Prim_G));
10333 -- Here we install one hidden primitive
10335 if Chars (Prim_G) /= Chars (Prim_A)
10336 and then Has_Suffix (Prim_A, 'P')
10337 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10339 Set_Chars (Prim_A, Chars (Prim_G));
10340 Append_New_Elmt (Prim_A, To => List);
10343 Next_Elmt (Prim_A_Elmt);
10344 Next_Elmt (Prim_G_Elmt);
10347 -- Append the elements to the list of temporarily visible primitives
10348 -- avoiding duplicates.
10350 if Present (List) then
10351 if No (Prims_List) then
10352 Prims_List := New_Elmt_List;
10355 Elmt := First_Elmt (List);
10356 while Present (Elmt) loop
10357 Append_Unique_Elmt (Node (Elmt), Prims_List);
10361 end Install_Hidden_Primitives;
10363 -------------------------------
10364 -- Restore_Hidden_Primitives --
10365 -------------------------------
10367 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10368 Prim_Elmt : Elmt_Id;
10372 if Prims_List /= No_Elist then
10373 Prim_Elmt := First_Elmt (Prims_List);
10374 while Present (Prim_Elmt) loop
10375 Prim := Node (Prim_Elmt);
10376 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10377 Next_Elmt (Prim_Elmt);
10380 Prims_List := No_Elist;
10382 end Restore_Hidden_Primitives;
10384 --------------------------------
10385 -- Instantiate_Formal_Package --
10386 --------------------------------
10388 function Instantiate_Formal_Package
10391 Analyzed_Formal : Node_Id) return List_Id
10393 Loc : constant Source_Ptr := Sloc (Actual);
10394 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10395 Actual_Pack : Entity_Id;
10396 Formal_Pack : Entity_Id;
10397 Gen_Parent : Entity_Id;
10400 Parent_Spec : Node_Id;
10402 procedure Find_Matching_Actual
10404 Act : in out Entity_Id);
10405 -- We need to associate each formal entity in the formal package with
10406 -- the corresponding entity in the actual package. The actual package
10407 -- has been analyzed and possibly expanded, and as a result there is
10408 -- no one-to-one correspondence between the two lists (for example,
10409 -- the actual may include subtypes, itypes, and inherited primitive
10410 -- operations, interspersed among the renaming declarations for the
10411 -- actuals). We retrieve the corresponding actual by name because each
10412 -- actual has the same name as the formal, and they do appear in the
10415 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10416 -- Retrieve entity of defining entity of generic formal parameter.
10417 -- Only the declarations of formals need to be considered when
10418 -- linking them to actuals, but the declarative list may include
10419 -- internal entities generated during analysis, and those are ignored.
10421 procedure Match_Formal_Entity
10422 (Formal_Node : Node_Id;
10423 Formal_Ent : Entity_Id;
10424 Actual_Ent : Entity_Id);
10425 -- Associates the formal entity with the actual. In the case where
10426 -- Formal_Ent is a formal package, this procedure iterates through all
10427 -- of its formals and enters associations between the actuals occurring
10428 -- in the formal package's corresponding actual package (given by
10429 -- Actual_Ent) and the formal package's formal parameters. This
10430 -- procedure recurses if any of the parameters is itself a package.
10432 function Is_Instance_Of
10433 (Act_Spec : Entity_Id;
10434 Gen_Anc : Entity_Id) return Boolean;
10435 -- The actual can be an instantiation of a generic within another
10436 -- instance, in which case there is no direct link from it to the
10437 -- original generic ancestor. In that case, we recognize that the
10438 -- ultimate ancestor is the same by examining names and scopes.
10440 procedure Process_Nested_Formal (Formal : Entity_Id);
10441 -- If the current formal is declared with a box, its own formals are
10442 -- visible in the instance, as they were in the generic, and their
10443 -- Hidden flag must be reset. If some of these formals are themselves
10444 -- packages declared with a box, the processing must be recursive.
10446 --------------------------
10447 -- Find_Matching_Actual --
10448 --------------------------
10450 procedure Find_Matching_Actual
10452 Act : in out Entity_Id)
10454 Formal_Ent : Entity_Id;
10457 case Nkind (Original_Node (F)) is
10458 when N_Formal_Object_Declaration
10459 | N_Formal_Type_Declaration
10461 Formal_Ent := Defining_Identifier (F);
10463 while Present (Act)
10464 and then Chars (Act) /= Chars (Formal_Ent)
10469 when N_Formal_Package_Declaration
10470 | N_Formal_Subprogram_Declaration
10471 | N_Generic_Package_Declaration
10472 | N_Package_Declaration
10474 Formal_Ent := Defining_Entity (F);
10476 while Present (Act)
10477 and then Chars (Act) /= Chars (Formal_Ent)
10483 raise Program_Error;
10485 end Find_Matching_Actual;
10487 -------------------------
10488 -- Match_Formal_Entity --
10489 -------------------------
10491 procedure Match_Formal_Entity
10492 (Formal_Node : Node_Id;
10493 Formal_Ent : Entity_Id;
10494 Actual_Ent : Entity_Id)
10496 Act_Pkg : Entity_Id;
10499 Set_Instance_Of (Formal_Ent, Actual_Ent);
10501 if Ekind (Actual_Ent) = E_Package then
10503 -- Record associations for each parameter
10505 Act_Pkg := Actual_Ent;
10508 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10512 Gen_Decl : Node_Id;
10514 Actual : Entity_Id;
10517 -- Retrieve the actual given in the formal package declaration
10519 Actual := Entity (Name (Original_Node (Formal_Node)));
10521 -- The actual in the formal package declaration may be a
10522 -- renamed generic package, in which case we want to retrieve
10523 -- the original generic in order to traverse its formal part.
10525 if Present (Renamed_Entity (Actual)) then
10526 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10528 Gen_Decl := Unit_Declaration_Node (Actual);
10531 Formals := Generic_Formal_Declarations (Gen_Decl);
10533 if Present (Formals) then
10534 F_Node := First_Non_Pragma (Formals);
10539 while Present (A_Ent)
10540 and then Present (F_Node)
10541 and then A_Ent /= First_Private_Entity (Act_Pkg)
10543 F_Ent := Get_Formal_Entity (F_Node);
10545 if Present (F_Ent) then
10547 -- This is a formal of the original package. Record
10548 -- association and recurse.
10550 Find_Matching_Actual (F_Node, A_Ent);
10551 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10552 Next_Entity (A_Ent);
10555 Next_Non_Pragma (F_Node);
10559 end Match_Formal_Entity;
10561 -----------------------
10562 -- Get_Formal_Entity --
10563 -----------------------
10565 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10566 Kind : constant Node_Kind := Nkind (Original_Node (N));
10569 when N_Formal_Object_Declaration =>
10570 return Defining_Identifier (N);
10572 when N_Formal_Type_Declaration =>
10573 return Defining_Identifier (N);
10575 when N_Formal_Subprogram_Declaration =>
10576 return Defining_Unit_Name (Specification (N));
10578 when N_Formal_Package_Declaration =>
10579 return Defining_Identifier (Original_Node (N));
10581 when N_Generic_Package_Declaration =>
10582 return Defining_Identifier (Original_Node (N));
10584 -- All other declarations are introduced by semantic analysis and
10585 -- have no match in the actual.
10590 end Get_Formal_Entity;
10592 --------------------
10593 -- Is_Instance_Of --
10594 --------------------
10596 function Is_Instance_Of
10597 (Act_Spec : Entity_Id;
10598 Gen_Anc : Entity_Id) return Boolean
10600 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10603 if No (Gen_Par) then
10606 -- Simplest case: the generic parent of the actual is the formal
10608 elsif Gen_Par = Gen_Anc then
10611 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10614 -- The actual may be obtained through several instantiations. Its
10615 -- scope must itself be an instance of a generic declared in the
10616 -- same scope as the formal. Any other case is detected above.
10618 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10622 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10624 end Is_Instance_Of;
10626 ---------------------------
10627 -- Process_Nested_Formal --
10628 ---------------------------
10630 procedure Process_Nested_Formal (Formal : Entity_Id) is
10634 if Present (Associated_Formal_Package (Formal))
10635 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10637 Ent := First_Entity (Formal);
10638 while Present (Ent) loop
10639 Set_Is_Hidden (Ent, False);
10640 Set_Is_Visible_Formal (Ent);
10641 Set_Is_Potentially_Use_Visible
10642 (Ent, Is_Potentially_Use_Visible (Formal));
10644 if Ekind (Ent) = E_Package then
10645 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10646 Process_Nested_Formal (Ent);
10652 end Process_Nested_Formal;
10654 -- Start of processing for Instantiate_Formal_Package
10659 -- The actual must be a package instance, or else a current instance
10660 -- such as a parent generic within the body of a generic child.
10662 if not Is_Entity_Name (Actual)
10663 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10666 ("expect package instance to instantiate formal", Actual);
10667 Abandon_Instantiation (Actual);
10668 raise Program_Error;
10671 Actual_Pack := Entity (Actual);
10672 Set_Is_Instantiated (Actual_Pack);
10674 -- The actual may be a renamed package, or an outer generic formal
10675 -- package whose instantiation is converted into a renaming.
10677 if Present (Renamed_Object (Actual_Pack)) then
10678 Actual_Pack := Renamed_Object (Actual_Pack);
10681 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10682 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10683 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10686 Generic_Parent (Specification (Analyzed_Formal));
10688 Defining_Unit_Name (Specification (Analyzed_Formal));
10691 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10692 Parent_Spec := Package_Specification (Actual_Pack);
10694 Parent_Spec := Parent (Actual_Pack);
10697 if Gen_Parent = Any_Id then
10699 ("previous error in declaration of formal package", Actual);
10700 Abandon_Instantiation (Actual);
10702 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10705 -- If this is the current instance of an enclosing generic, that unit
10706 -- is the generic package we need.
10708 elsif In_Open_Scopes (Actual_Pack)
10709 and then Ekind (Actual_Pack) = E_Generic_Package
10715 ("actual parameter must be instance of&", Actual, Gen_Parent);
10716 Abandon_Instantiation (Actual);
10719 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10720 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10723 Make_Package_Renaming_Declaration (Loc,
10724 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10725 Name => New_Occurrence_Of (Actual_Pack, Loc));
10727 Set_Associated_Formal_Package
10728 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10729 Decls := New_List (Nod);
10731 -- If the formal F has a box, then the generic declarations are
10732 -- visible in the generic G. In an instance of G, the corresponding
10733 -- entities in the actual for F (which are the actuals for the
10734 -- instantiation of the generic that F denotes) must also be made
10735 -- visible for analysis of the current instance. On exit from the
10736 -- current instance, those entities are made private again. If the
10737 -- actual is currently in use, these entities are also use-visible.
10739 -- The loop through the actual entities also steps through the formal
10740 -- entities and enters associations from formals to actuals into the
10741 -- renaming map. This is necessary to properly handle checking of
10742 -- actual parameter associations for later formals that depend on
10743 -- actuals declared in the formal package.
10745 -- In Ada 2005, partial parameterization requires that we make
10746 -- visible the actuals corresponding to formals that were defaulted
10747 -- in the formal package. There formals are identified because they
10748 -- remain formal generics within the formal package, rather than
10749 -- being renamings of the actuals supplied.
10752 Gen_Decl : constant Node_Id :=
10753 Unit_Declaration_Node (Gen_Parent);
10754 Formals : constant List_Id :=
10755 Generic_Formal_Declarations (Gen_Decl);
10757 Actual_Ent : Entity_Id;
10758 Actual_Of_Formal : Node_Id;
10759 Formal_Node : Node_Id;
10760 Formal_Ent : Entity_Id;
10763 if Present (Formals) then
10764 Formal_Node := First_Non_Pragma (Formals);
10766 Formal_Node := Empty;
10769 Actual_Ent := First_Entity (Actual_Pack);
10770 Actual_Of_Formal :=
10771 First (Visible_Declarations (Specification (Analyzed_Formal)));
10772 while Present (Actual_Ent)
10773 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10775 if Present (Formal_Node) then
10776 Formal_Ent := Get_Formal_Entity (Formal_Node);
10778 if Present (Formal_Ent) then
10779 Find_Matching_Actual (Formal_Node, Actual_Ent);
10780 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10782 -- We iterate at the same time over the actuals of the
10783 -- local package created for the formal, to determine
10784 -- which one of the formals of the original generic were
10785 -- defaulted in the formal. The corresponding actual
10786 -- entities are visible in the enclosing instance.
10788 if Box_Present (Formal)
10790 (Present (Actual_Of_Formal)
10793 (Get_Formal_Entity (Actual_Of_Formal)))
10795 Set_Is_Hidden (Actual_Ent, False);
10796 Set_Is_Visible_Formal (Actual_Ent);
10797 Set_Is_Potentially_Use_Visible
10798 (Actual_Ent, In_Use (Actual_Pack));
10800 if Ekind (Actual_Ent) = E_Package then
10801 Process_Nested_Formal (Actual_Ent);
10805 if not Is_Hidden (Actual_Ent) then
10806 Append_Elmt (Actual_Ent, Hidden_Formals);
10809 Set_Is_Hidden (Actual_Ent);
10810 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10814 Next_Non_Pragma (Formal_Node);
10815 Next (Actual_Of_Formal);
10817 -- A formal subprogram may be overloaded, so advance in
10818 -- the list of actuals to make sure we do not match two
10819 -- successive formals to the same actual. This is only
10820 -- relevant for overloadable entities, others have
10823 if Is_Overloadable (Actual_Ent) then
10824 Next_Entity (Actual_Ent);
10828 -- No further formals to match, but the generic part may
10829 -- contain inherited operation that are not hidden in the
10830 -- enclosing instance.
10832 Next_Entity (Actual_Ent);
10836 -- Inherited subprograms generated by formal derived types are
10837 -- also visible if the types are.
10839 Actual_Ent := First_Entity (Actual_Pack);
10840 while Present (Actual_Ent)
10841 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10843 if Is_Overloadable (Actual_Ent)
10845 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10847 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10849 Set_Is_Hidden (Actual_Ent, False);
10850 Set_Is_Potentially_Use_Visible
10851 (Actual_Ent, In_Use (Actual_Pack));
10854 Next_Entity (Actual_Ent);
10857 -- No conformance to check if the generic has no formal parameters
10858 -- and the formal package has no generic associations.
10860 if Is_Empty_List (Formals)
10862 (Box_Present (Formal)
10863 or else No (Generic_Associations (Formal)))
10869 -- If the formal is not declared with a box, reanalyze it as an
10870 -- abbreviated instantiation, to verify the matching rules of 12.7.
10871 -- The actual checks are performed after the generic associations
10872 -- have been analyzed, to guarantee the same visibility for this
10873 -- instantiation and for the actuals.
10875 -- In Ada 2005, the generic associations for the formal can include
10876 -- defaulted parameters. These are ignored during check. This
10877 -- internal instantiation is removed from the tree after conformance
10878 -- checking, because it contains formal declarations for those
10879 -- defaulted parameters, and those should not reach the back-end.
10881 if not Box_Present (Formal) then
10883 I_Pack : constant Entity_Id :=
10884 Make_Temporary (Sloc (Actual), 'P');
10887 Set_Is_Internal (I_Pack);
10888 Mutate_Ekind (I_Pack, E_Package);
10889 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10892 Make_Package_Instantiation (Sloc (Actual),
10893 Defining_Unit_Name => I_Pack,
10896 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10897 Generic_Associations => Generic_Associations (Formal)));
10903 end Instantiate_Formal_Package;
10905 -----------------------------------
10906 -- Instantiate_Formal_Subprogram --
10907 -----------------------------------
10909 function Instantiate_Formal_Subprogram
10912 Analyzed_Formal : Node_Id) return Node_Id
10914 Analyzed_S : constant Entity_Id :=
10915 Defining_Unit_Name (Specification (Analyzed_Formal));
10916 Formal_Sub : constant Entity_Id :=
10917 Defining_Unit_Name (Specification (Formal));
10919 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10920 -- If the generic is a child unit, the parent has been installed on the
10921 -- scope stack, but a default subprogram cannot resolve to something
10922 -- on the parent because that parent is not really part of the visible
10923 -- context (it is there to resolve explicit local entities). If the
10924 -- default has resolved in this way, we remove the entity from immediate
10925 -- visibility and analyze the node again to emit an error message or
10926 -- find another visible candidate.
10928 procedure Valid_Actual_Subprogram (Act : Node_Id);
10929 -- Perform legality check and raise exception on failure
10931 -----------------------
10932 -- From_Parent_Scope --
10933 -----------------------
10935 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10936 Gen_Scope : Node_Id;
10939 Gen_Scope := Scope (Analyzed_S);
10940 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10941 if Scope (Subp) = Scope (Gen_Scope) then
10945 Gen_Scope := Scope (Gen_Scope);
10949 end From_Parent_Scope;
10951 -----------------------------
10952 -- Valid_Actual_Subprogram --
10953 -----------------------------
10955 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10959 if Is_Entity_Name (Act) then
10960 Act_E := Entity (Act);
10962 elsif Nkind (Act) = N_Selected_Component
10963 and then Is_Entity_Name (Selector_Name (Act))
10965 Act_E := Entity (Selector_Name (Act));
10971 if (Present (Act_E) and then Is_Overloadable (Act_E))
10972 or else Nkind (Act) in N_Attribute_Reference
10973 | N_Indexed_Component
10974 | N_Character_Literal
10975 | N_Explicit_Dereference
10981 ("expect subprogram or entry name in instantiation of &",
10982 Instantiation_Node, Formal_Sub);
10983 Abandon_Instantiation (Instantiation_Node);
10984 end Valid_Actual_Subprogram;
10988 Decl_Node : Node_Id;
10991 New_Spec : Node_Id;
10992 New_Subp : Entity_Id;
10994 -- Start of processing for Instantiate_Formal_Subprogram
10997 New_Spec := New_Copy_Tree (Specification (Formal));
10999 -- The tree copy has created the proper instantiation sloc for the
11000 -- new specification. Use this location for all other constructed
11003 Loc := Sloc (Defining_Unit_Name (New_Spec));
11005 -- Create new entity for the actual (New_Copy_Tree does not), and
11006 -- indicate that it is an actual.
11008 -- If the actual is not an entity (i.e. an attribute reference)
11009 -- and the formal includes aspect specifications for contracts,
11010 -- we create an internal name for the renaming declaration. The
11011 -- constructed wrapper contains a call to the entity in the renaming.
11012 -- This is an expansion activity, as is the wrapper creation.
11014 if Ada_Version >= Ada_2020
11015 and then Has_Contracts (Analyzed_Formal)
11016 and then not Is_Entity_Name (Actual)
11017 and then Expander_Active
11019 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11020 Set_Defining_Unit_Name (New_Spec, New_Subp);
11022 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11025 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11026 Set_Is_Generic_Actual_Subprogram (New_Subp);
11027 Set_Defining_Unit_Name (New_Spec, New_Subp);
11029 -- Create new entities for the each of the formals in the specification
11030 -- of the renaming declaration built for the actual.
11032 if Present (Parameter_Specifications (New_Spec)) then
11038 F := First (Parameter_Specifications (New_Spec));
11039 while Present (F) loop
11040 F_Id := Defining_Identifier (F);
11042 Set_Defining_Identifier (F,
11043 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11049 -- Find entity of actual. If the actual is an attribute reference, it
11050 -- cannot be resolved here (its formal is missing) but is handled
11051 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11052 -- fully resolved subsequently, when the renaming declaration for the
11053 -- formal is analyzed. If it is an explicit dereference, resolve the
11054 -- prefix but not the actual itself, to prevent interpretation as call.
11056 if Present (Actual) then
11057 Loc := Sloc (Actual);
11058 Set_Sloc (New_Spec, Loc);
11060 if Nkind (Actual) = N_Operator_Symbol then
11061 Find_Direct_Name (Actual);
11063 elsif Nkind (Actual) = N_Explicit_Dereference then
11064 Analyze (Prefix (Actual));
11066 elsif Nkind (Actual) /= N_Attribute_Reference then
11070 Valid_Actual_Subprogram (Actual);
11073 elsif Present (Default_Name (Formal)) then
11074 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11075 | N_Selected_Component
11076 | N_Indexed_Component
11077 | N_Character_Literal
11078 and then Present (Entity (Default_Name (Formal)))
11080 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11082 Nam := New_Copy (Default_Name (Formal));
11083 Set_Sloc (Nam, Loc);
11086 elsif Box_Present (Formal) then
11088 -- Actual is resolved at the point of instantiation. Create an
11089 -- identifier or operator with the same name as the formal.
11091 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11093 Make_Operator_Symbol (Loc,
11094 Chars => Chars (Formal_Sub),
11095 Strval => No_String);
11097 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11100 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11101 and then Null_Present (Specification (Formal))
11103 -- Generate null body for procedure, for use in the instance
11106 Make_Subprogram_Body (Loc,
11107 Specification => New_Spec,
11108 Declarations => New_List,
11109 Handled_Statement_Sequence =>
11110 Make_Handled_Sequence_Of_Statements (Loc,
11111 Statements => New_List (Make_Null_Statement (Loc))));
11113 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11115 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11117 -- Eliminate the calls to it when optimization is enabled
11119 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11123 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11125 ("missing actual&", Instantiation_Node, Formal_Sub);
11127 ("\in instantiation of & declared#",
11128 Instantiation_Node, Scope (Analyzed_S));
11129 Abandon_Instantiation (Instantiation_Node);
11133 Make_Subprogram_Renaming_Declaration (Loc,
11134 Specification => New_Spec,
11137 -- If we do not have an actual and the formal specified <> then set to
11138 -- get proper default.
11140 if No (Actual) and then Box_Present (Formal) then
11141 Set_From_Default (Decl_Node);
11144 -- Gather possible interpretations for the actual before analyzing the
11145 -- instance. If overloaded, it will be resolved when analyzing the
11146 -- renaming declaration.
11148 if Box_Present (Formal) and then No (Actual) then
11151 if Is_Child_Unit (Scope (Analyzed_S))
11152 and then Present (Entity (Nam))
11154 if not Is_Overloaded (Nam) then
11155 if From_Parent_Scope (Entity (Nam)) then
11156 Set_Is_Immediately_Visible (Entity (Nam), False);
11157 Set_Entity (Nam, Empty);
11158 Set_Etype (Nam, Empty);
11161 Set_Is_Immediately_Visible (Entity (Nam));
11170 Get_First_Interp (Nam, I, It);
11171 while Present (It.Nam) loop
11172 if From_Parent_Scope (It.Nam) then
11176 Get_Next_Interp (I, It);
11183 -- The generic instantiation freezes the actual. This can only be done
11184 -- once the actual is resolved, in the analysis of the renaming
11185 -- declaration. To make the formal subprogram entity available, we set
11186 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11187 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11188 -- of formal abstract subprograms.
11190 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11192 -- We cannot analyze the renaming declaration, and thus find the actual,
11193 -- until all the actuals are assembled in the instance. For subsequent
11194 -- checks of other actuals, indicate the node that will hold the
11195 -- instance of this formal.
11197 Set_Instance_Of (Analyzed_S, Nam);
11199 if Nkind (Actual) = N_Selected_Component
11200 and then Is_Task_Type (Etype (Prefix (Actual)))
11201 and then not Is_Frozen (Etype (Prefix (Actual)))
11203 -- The renaming declaration will create a body, which must appear
11204 -- outside of the instantiation, We move the renaming declaration
11205 -- out of the instance, and create an additional renaming inside,
11206 -- to prevent freezing anomalies.
11209 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11212 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11213 Insert_Before (Instantiation_Node, Decl_Node);
11214 Analyze (Decl_Node);
11216 -- Now create renaming within the instance
11219 Make_Subprogram_Renaming_Declaration (Loc,
11220 Specification => New_Copy_Tree (New_Spec),
11221 Name => New_Occurrence_Of (Anon_Id, Loc));
11223 Set_Defining_Unit_Name (Specification (Decl_Node),
11224 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11229 end Instantiate_Formal_Subprogram;
11231 ------------------------
11232 -- Instantiate_Object --
11233 ------------------------
11235 function Instantiate_Object
11238 Analyzed_Formal : Node_Id) return List_Id
11240 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11241 A_Gen_Obj : constant Entity_Id :=
11242 Defining_Identifier (Analyzed_Formal);
11243 Acc_Def : Node_Id := Empty;
11244 Act_Assoc : constant Node_Id := Parent (Actual);
11245 Actual_Decl : Node_Id := Empty;
11246 Decl_Node : Node_Id;
11249 List : constant List_Id := New_List;
11250 Loc : constant Source_Ptr := Sloc (Actual);
11251 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11252 Subt_Decl : Node_Id := Empty;
11253 Subt_Mark : Node_Id := Empty;
11255 -- Start of processing for Instantiate_Object
11258 -- Formal may be an anonymous access
11260 if Present (Subtype_Mark (Formal)) then
11261 Subt_Mark := Subtype_Mark (Formal);
11263 Check_Access_Definition (Formal);
11264 Acc_Def := Access_Definition (Formal);
11267 -- Sloc for error message on missing actual
11269 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11271 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11272 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11275 Set_Parent (List, Parent (Actual));
11279 if Out_Present (Formal) then
11281 -- An IN OUT generic actual must be a name. The instantiation is a
11282 -- renaming declaration. The actual is the name being renamed. We
11283 -- use the actual directly, rather than a copy, because it is not
11284 -- used further in the list of actuals, and because a copy or a use
11285 -- of relocate_node is incorrect if the instance is nested within a
11286 -- generic. In order to simplify e.g. ASIS queries, the
11287 -- Generic_Parent field links the declaration to the generic
11290 if No (Actual) then
11292 ("missing actual &",
11293 Instantiation_Node, Gen_Obj);
11295 ("\in instantiation of & declared#",
11296 Instantiation_Node, Scope (A_Gen_Obj));
11297 Abandon_Instantiation (Instantiation_Node);
11300 if Present (Subt_Mark) then
11302 Make_Object_Renaming_Declaration (Loc,
11303 Defining_Identifier => New_Copy (Gen_Obj),
11304 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11307 else pragma Assert (Present (Acc_Def));
11309 Make_Object_Renaming_Declaration (Loc,
11310 Defining_Identifier => New_Copy (Gen_Obj),
11311 Access_Definition => New_Copy_Tree (Acc_Def),
11315 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11317 -- The analysis of the actual may produce Insert_Action nodes, so
11318 -- the declaration must have a context in which to attach them.
11320 Append (Decl_Node, List);
11323 -- Return if the analysis of the actual reported some error
11325 if Etype (Actual) = Any_Type then
11329 -- This check is performed here because Analyze_Object_Renaming will
11330 -- not check it when Comes_From_Source is False. Note though that the
11331 -- check for the actual being the name of an object will be performed
11332 -- in Analyze_Object_Renaming.
11334 if Is_Object_Reference (Actual)
11335 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11338 ("illegal discriminant-dependent component for in out parameter",
11342 -- The actual has to be resolved in order to check that it is a
11343 -- variable (due to cases such as F (1), where F returns access to
11344 -- an array, and for overloaded prefixes).
11346 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11348 -- If the type of the formal is not itself a formal, and the current
11349 -- unit is a child unit, the formal type must be declared in a
11350 -- parent, and must be retrieved by visibility.
11352 if Ftyp = Orig_Ftyp
11353 and then Is_Generic_Unit (Scope (Ftyp))
11354 and then Is_Child_Unit (Scope (A_Gen_Obj))
11357 Temp : constant Node_Id :=
11358 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11360 Set_Entity (Temp, Empty);
11362 Ftyp := Entity (Temp);
11366 if Is_Private_Type (Ftyp)
11367 and then not Is_Private_Type (Etype (Actual))
11368 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11369 or else Base_Type (Etype (Actual)) = Ftyp)
11371 -- If the actual has the type of the full view of the formal, or
11372 -- else a non-private subtype of the formal, then the visibility
11373 -- of the formal type has changed. Add to the actuals a subtype
11374 -- declaration that will force the exchange of views in the body
11375 -- of the instance as well.
11378 Make_Subtype_Declaration (Loc,
11379 Defining_Identifier => Make_Temporary (Loc, 'P'),
11380 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11382 Prepend (Subt_Decl, List);
11384 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11385 Exchange_Declarations (Ftyp);
11388 Resolve (Actual, Ftyp);
11390 if not Denotes_Variable (Actual) then
11391 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11393 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11395 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11396 -- the type of the actual shall resolve to a specific anonymous
11399 if Ada_Version < Ada_2005
11400 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11401 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11404 ("type of actual does not match type of&", Actual, Gen_Obj);
11408 Note_Possible_Modification (Actual, Sure => True);
11410 -- Check for instantiation with atomic/volatile/VFA object actual for
11411 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11413 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11415 ("cannot instantiate nonatomic formal & of mode in out",
11417 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11419 elsif Is_Volatile_Object_Ref (Actual)
11420 and then not Is_Volatile (Orig_Ftyp)
11423 ("cannot instantiate nonvolatile formal & of mode in out",
11425 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11427 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11428 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11431 ("cannot instantiate nonfull access formal & of mode in out",
11434 ("\with full access object actual (RM C.6(12))", Actual);
11437 -- Check for instantiation on nonatomic subcomponent of a full access
11438 -- object in Ada 2020 (RM C.6 (12)).
11440 if Ada_Version >= Ada_2020
11441 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11442 and then not Is_Atomic_Object (Actual)
11445 ("cannot instantiate formal & of mode in out with actual",
11448 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11452 -- Check actual/formal compatibility with respect to the four
11453 -- volatility refinement aspects.
11456 Actual_Obj : Entity_Id;
11457 N : Node_Id := Actual;
11459 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11460 -- pointer dereference like component selection.
11462 if Is_Entity_Name (N) then
11463 Actual_Obj := Entity (N);
11468 when N_Indexed_Component
11469 | N_Selected_Component
11471 | N_Explicit_Dereference
11475 when N_Type_Conversion =>
11476 N := Expression (N);
11479 Actual_Obj := Etype (N);
11484 Check_Volatility_Compatibility
11485 (Actual_Obj, A_Gen_Obj, "actual object",
11486 "its corresponding formal object of mode in out",
11487 Srcpos_Bearer => Actual);
11490 -- Formal in-parameter
11493 -- The instantiation of a generic formal in-parameter is constant
11494 -- declaration. The actual is the expression for that declaration.
11495 -- Its type is a full copy of the type of the formal. This may be
11496 -- an access to subprogram, for which we need to generate entities
11497 -- for the formals in the new signature.
11499 if Present (Actual) then
11500 if Present (Subt_Mark) then
11501 Def := New_Copy_Tree (Subt_Mark);
11503 pragma Assert (Present (Acc_Def));
11504 Def := New_Copy_Tree (Acc_Def);
11508 Make_Object_Declaration (Loc,
11509 Defining_Identifier => New_Copy (Gen_Obj),
11510 Constant_Present => True,
11511 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11512 Object_Definition => Def,
11513 Expression => Actual);
11515 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11517 -- A generic formal object of a tagged type is defined to be
11518 -- aliased so the new constant must also be treated as aliased.
11520 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11521 Set_Aliased_Present (Decl_Node);
11524 Append (Decl_Node, List);
11526 -- No need to repeat (pre-)analysis of some expression nodes
11527 -- already handled in Preanalyze_Actuals.
11529 if Nkind (Actual) /= N_Allocator then
11532 -- Return if the analysis of the actual reported some error
11534 if Etype (Actual) = Any_Type then
11540 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11544 Typ := Get_Instance_Of (Formal_Type);
11546 -- If the actual appears in the current or an enclosing scope,
11547 -- use its type directly. This is relevant if it has an actual
11548 -- subtype that is distinct from its nominal one. This cannot
11549 -- be done in general because the type of the actual may
11550 -- depend on other actuals, and only be fully determined when
11551 -- the enclosing instance is analyzed.
11553 if Present (Etype (Actual))
11554 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11556 Freeze_Before (Instantiation_Node, Etype (Actual));
11558 Freeze_Before (Instantiation_Node, Typ);
11561 -- If the actual is an aggregate, perform name resolution on
11562 -- its components (the analysis of an aggregate does not do it)
11563 -- to capture local names that may be hidden if the generic is
11566 if Nkind (Actual) = N_Aggregate then
11567 Preanalyze_And_Resolve (Actual, Typ);
11570 if Is_Limited_Type (Typ)
11571 and then not OK_For_Limited_Init (Typ, Actual)
11574 ("initialization not allowed for limited types", Actual);
11575 Explain_Limited_Type (Typ, Actual);
11579 elsif Present (Default_Expression (Formal)) then
11581 -- Use default to construct declaration
11583 if Present (Subt_Mark) then
11584 Def := New_Copy_Tree (Subt_Mark);
11586 pragma Assert (Present (Acc_Def));
11587 Def := New_Copy_Tree (Acc_Def);
11591 Make_Object_Declaration (Sloc (Formal),
11592 Defining_Identifier => New_Copy (Gen_Obj),
11593 Constant_Present => True,
11594 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11595 Object_Definition => Def,
11596 Expression => New_Copy_Tree
11597 (Default_Expression (Formal)));
11599 Set_Corresponding_Generic_Association
11600 (Decl_Node, Expression (Decl_Node));
11602 Append (Decl_Node, List);
11603 Set_Analyzed (Expression (Decl_Node), False);
11606 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11607 Error_Msg_NE ("\in instantiation of & declared#",
11608 Instantiation_Node, Scope (A_Gen_Obj));
11610 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11612 -- Create dummy constant declaration so that instance can be
11613 -- analyzed, to minimize cascaded visibility errors.
11615 if Present (Subt_Mark) then
11617 else pragma Assert (Present (Acc_Def));
11622 Make_Object_Declaration (Loc,
11623 Defining_Identifier => New_Copy (Gen_Obj),
11624 Constant_Present => True,
11625 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11626 Object_Definition => New_Copy (Def),
11628 Make_Attribute_Reference (Sloc (Gen_Obj),
11629 Attribute_Name => Name_First,
11630 Prefix => New_Copy (Def)));
11632 Append (Decl_Node, List);
11635 Abandon_Instantiation (Instantiation_Node);
11640 if Nkind (Actual) in N_Has_Entity then
11641 Actual_Decl := Parent (Entity (Actual));
11644 -- Ada 2005 (AI-423) refined by AI12-0287:
11645 -- For an object_renaming_declaration with a null_exclusion or an
11646 -- access_definition that has a null_exclusion, the subtype of the
11647 -- object_name shall exclude null. In addition, if the
11648 -- object_renaming_declaration occurs within the body of a generic unit
11649 -- G or within the body of a generic unit declared within the
11650 -- declarative region of generic unit G, then:
11651 -- * if the object_name statically denotes a generic formal object of
11652 -- mode in out of G, then the declaration of that object shall have a
11654 -- * if the object_name statically denotes a call of a generic formal
11655 -- function of G, then the declaration of the result of that function
11656 -- shall have a null_exclusion.
11658 if Ada_Version >= Ada_2005
11659 and then Present (Actual_Decl)
11660 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11661 | N_Object_Declaration
11662 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11663 and then not Has_Null_Exclusion (Actual_Decl)
11664 and then Has_Null_Exclusion (Analyzed_Formal)
11665 and then Ekind (Defining_Identifier (Analyzed_Formal))
11666 = E_Generic_In_Out_Parameter
11667 and then ((In_Generic_Scope (Entity (Actual))
11668 and then In_Package_Body (Scope (Entity (Actual))))
11669 or else not Can_Never_Be_Null (Etype (Actual)))
11671 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11673 ("actual must exclude null to match generic formal#", Actual);
11676 -- An effectively volatile object cannot be used as an actual in a
11677 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11678 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11679 -- legality rule, and also verifies that the actual is an object.
11682 and then Present (Actual)
11683 and then Is_Object_Reference (Actual)
11684 and then Is_Effectively_Volatile_Object (Actual)
11685 and then not Is_Effectively_Volatile (A_Gen_Obj)
11688 ("volatile object cannot act as actual in generic instantiation",
11693 end Instantiate_Object;
11695 ------------------------------
11696 -- Instantiate_Package_Body --
11697 ------------------------------
11699 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11700 -- must be replaced by gotos which jump to the end of the routine in order
11701 -- to restore the Ghost and SPARK modes.
11703 procedure Instantiate_Package_Body
11704 (Body_Info : Pending_Body_Info;
11705 Inlined_Body : Boolean := False;
11706 Body_Optional : Boolean := False)
11708 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11709 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11710 Act_Spec : constant Node_Id := Specification (Act_Decl);
11711 Ctx_Parents : Elist_Id := No_Elist;
11712 Ctx_Top : Int := 0;
11713 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11714 Gen_Id : constant Node_Id := Name (Inst_Node);
11715 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11716 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11717 Loc : constant Source_Ptr := Sloc (Inst_Node);
11719 procedure Check_Initialized_Types;
11720 -- In a generic package body, an entity of a generic private type may
11721 -- appear uninitialized. This is suspicious, unless the actual is a
11722 -- fully initialized type.
11724 procedure Install_Parents_Of_Generic_Context
11725 (Inst_Scope : Entity_Id;
11726 Ctx_Parents : out Elist_Id);
11727 -- Inst_Scope is the scope where the instance appears within; when it
11728 -- appears within a generic child package G, this routine collects and
11729 -- installs the enclosing packages of G in the scopes stack; installed
11730 -- packages are returned in Ctx_Parents.
11732 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11733 -- Reverse effect after instantiation is complete
11735 -----------------------------
11736 -- Check_Initialized_Types --
11737 -----------------------------
11739 procedure Check_Initialized_Types is
11741 Formal : Entity_Id;
11742 Actual : Entity_Id;
11743 Uninit_Var : Entity_Id;
11746 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11747 while Present (Decl) loop
11748 Uninit_Var := Empty;
11750 if Nkind (Decl) = N_Private_Extension_Declaration then
11751 Uninit_Var := Uninitialized_Variable (Decl);
11753 elsif Nkind (Decl) = N_Formal_Type_Declaration
11754 and then Nkind (Formal_Type_Definition (Decl)) =
11755 N_Formal_Private_Type_Definition
11758 Uninitialized_Variable (Formal_Type_Definition (Decl));
11761 if Present (Uninit_Var) then
11762 Formal := Defining_Identifier (Decl);
11763 Actual := First_Entity (Act_Decl_Id);
11765 -- For each formal there is a subtype declaration that renames
11766 -- the actual and has the same name as the formal. Locate the
11767 -- formal for warning message about uninitialized variables
11768 -- in the generic, for which the actual type should be a fully
11769 -- initialized type.
11771 while Present (Actual) loop
11772 exit when Ekind (Actual) = E_Package
11773 and then Present (Renamed_Object (Actual));
11775 if Chars (Actual) = Chars (Formal)
11776 and then not Is_Scalar_Type (Actual)
11777 and then not Is_Fully_Initialized_Type (Actual)
11778 and then Warn_On_No_Value_Assigned
11780 Error_Msg_Node_2 := Formal;
11782 ("generic unit has uninitialized variable& of "
11783 & "formal private type &?v?", Actual, Uninit_Var);
11785 ("actual type for& should be fully initialized type?v?",
11790 Next_Entity (Actual);
11796 end Check_Initialized_Types;
11798 ----------------------------------------
11799 -- Install_Parents_Of_Generic_Context --
11800 ----------------------------------------
11802 procedure Install_Parents_Of_Generic_Context
11803 (Inst_Scope : Entity_Id;
11804 Ctx_Parents : out Elist_Id)
11810 Ctx_Parents := New_Elmt_List;
11812 -- Collect context parents (ie. parents where the instantiation
11813 -- appears within).
11816 while S /= Standard_Standard loop
11817 Prepend_Elmt (S, Ctx_Parents);
11821 -- Install enclosing parents
11823 Elmt := First_Elmt (Ctx_Parents);
11824 while Present (Elmt) loop
11825 Push_Scope (Node (Elmt));
11826 Set_Is_Immediately_Visible (Node (Elmt));
11829 end Install_Parents_Of_Generic_Context;
11831 ---------------------------------------
11832 -- Remove_Parents_Of_Generic_Context --
11833 ---------------------------------------
11835 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11839 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11841 Elmt := Last_Elmt (Ctx_Parents);
11842 while Present (Elmt) loop
11843 pragma Assert (Current_Scope = Node (Elmt));
11844 Set_Is_Immediately_Visible (Current_Scope, False);
11847 Remove_Last_Elmt (Ctx_Parents);
11848 Elmt := Last_Elmt (Ctx_Parents);
11850 end Remove_Parents_Of_Generic_Context;
11854 -- The following constants capture the context prior to instantiating
11855 -- the package body.
11857 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11858 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11859 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11860 Saved_ISMP : constant Boolean :=
11861 Ignore_SPARK_Mode_Pragmas_In_Instance;
11862 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11863 Local_Suppress_Stack_Top;
11864 Saved_SC : constant Boolean := Style_Check;
11865 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11866 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11867 Saved_SS : constant Suppress_Record := Scope_Suppress;
11868 Saved_Warn : constant Warning_Record := Save_Warnings;
11870 Act_Body : Node_Id;
11871 Act_Body_Id : Entity_Id;
11872 Act_Body_Name : Node_Id;
11873 Gen_Body : Node_Id;
11874 Gen_Body_Id : Node_Id;
11875 Par_Ent : Entity_Id := Empty;
11876 Par_Installed : Boolean := False;
11877 Par_Vis : Boolean := False;
11879 Scope_Check_Id : Entity_Id;
11880 Scope_Check_Last : Nat;
11881 -- Value of Current_Scope before calls to Install_Parents; used to check
11882 -- that scopes are correctly removed after instantiation.
11884 Vis_Prims_List : Elist_Id := No_Elist;
11885 -- List of primitives made temporarily visible in the instantiation
11886 -- to match the visibility of the formal type.
11888 -- Start of processing for Instantiate_Package_Body
11891 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11893 -- The instance body may already have been processed, as the parent of
11894 -- another instance that is inlined (Load_Parent_Of_Generic).
11896 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11900 -- The package being instantiated may be subject to pragma Ghost. Set
11901 -- the mode now to ensure that any nodes generated during instantiation
11902 -- are properly marked as Ghost.
11904 Set_Ghost_Mode (Act_Decl_Id);
11906 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11908 -- Re-establish the state of information on which checks are suppressed.
11909 -- This information was set in Body_Info at the point of instantiation,
11910 -- and now we restore it so that the instance is compiled using the
11911 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11913 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11914 Scope_Suppress := Body_Info.Scope_Suppress;
11916 Restore_Config_Switches (Body_Info.Config_Switches);
11917 Restore_Warnings (Body_Info.Warnings);
11919 if No (Gen_Body_Id) then
11921 -- Do not look for parent of generic body if none is required.
11922 -- This may happen when the routine is called as part of the
11923 -- Pending_Instantiations processing, when nested instances
11924 -- may precede the one generated from the main unit.
11926 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11927 and then Body_Optional
11931 Load_Parent_Of_Generic
11932 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11934 -- Surprisingly enough, loading the body of the parent can cause
11935 -- the body to be instantiated and the double instantiation needs
11936 -- to be prevented in order to avoid giving bogus semantic errors.
11938 -- This case can occur because of the Collect_Previous_Instances
11939 -- machinery of Load_Parent_Of_Generic, which will instantiate
11940 -- bodies that are deemed to be ahead of the body of the parent
11941 -- in the compilation unit. But the relative position of these
11942 -- bodies is computed using the mere comparison of their Sloc.
11944 -- Now suppose that you have two generic packages G and H, with
11945 -- G containing a mere instantiation of H:
11951 -- package Nested_G is
11962 -- package My_H is new H;
11966 -- and a third package Q instantiating G and Nested_G:
11972 -- package My_G is new G;
11974 -- package My_Nested_G is new My_G.My_H.Nested_G;
11978 -- The body to be instantiated is that of My_Nested_G and its
11979 -- parent is the instance My_G.My_H. This latter instantiation
11980 -- is done when My_G is analyzed, i.e. after the declarations
11981 -- of My_G and My_Nested_G have been parsed; as a result, the
11982 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11984 -- Therefore loading the body of My_G.My_H will cause the body
11985 -- of My_Nested_G to be instantiated because it is deemed to be
11986 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11987 -- will again be invoked on My_G.My_H, but this time with the
11988 -- Collect_Previous_Instances machinery disabled, so there is
11989 -- no endless mutual recursion and things are done in order.
11991 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11995 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11999 -- Establish global variable for sloc adjustment and for error recovery
12000 -- In the case of an instance body for an instantiation with actuals
12001 -- from a limited view, the instance body is placed at the beginning
12002 -- of the enclosing package body: use the body entity as the source
12003 -- location for nodes of the instance body.
12005 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12007 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12008 Body_Id : constant Node_Id :=
12009 Corresponding_Body (Unit_Declaration_Node (Scop));
12012 Instantiation_Node := Body_Id;
12015 Instantiation_Node := Inst_Node;
12018 if Present (Gen_Body_Id) then
12019 Save_Env (Gen_Unit, Act_Decl_Id);
12020 Style_Check := False;
12022 -- If the context of the instance is subject to SPARK_Mode "off", the
12023 -- annotation is missing, or the body is instantiated at a later pass
12024 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12025 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12028 if SPARK_Mode /= On
12029 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12031 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12034 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12035 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12037 Create_Instantiation_Source
12038 (Inst_Node, Gen_Body_Id, S_Adjustment);
12042 (Original_Node (Gen_Body), Empty, Instantiating => True);
12044 -- Create proper (possibly qualified) defining name for the body, to
12045 -- correspond to the one in the spec.
12048 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12049 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12051 -- Some attributes of spec entity are not inherited by body entity
12053 Set_Handler_Records (Act_Body_Id, No_List);
12055 if Nkind (Defining_Unit_Name (Act_Spec)) =
12056 N_Defining_Program_Unit_Name
12059 Make_Defining_Program_Unit_Name (Loc,
12061 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12062 Defining_Identifier => Act_Body_Id);
12064 Act_Body_Name := Act_Body_Id;
12067 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12069 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12070 Check_Generic_Actuals (Act_Decl_Id, False);
12071 Check_Initialized_Types;
12073 -- Install primitives hidden at the point of the instantiation but
12074 -- visible when processing the generic formals
12080 E := First_Entity (Act_Decl_Id);
12081 while Present (E) loop
12083 and then not Is_Itype (E)
12084 and then Is_Generic_Actual_Type (E)
12085 and then Is_Tagged_Type (E)
12087 Install_Hidden_Primitives
12088 (Prims_List => Vis_Prims_List,
12089 Gen_T => Generic_Parent_Type (Parent (E)),
12097 Scope_Check_Id := Current_Scope;
12098 Scope_Check_Last := Scope_Stack.Last;
12100 -- If the instantiation appears within a generic child some actual
12101 -- parameter may be the current instance of the enclosing generic
12105 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12108 if Is_Child_Unit (Inst_Scope)
12109 and then Ekind (Inst_Scope) = E_Generic_Package
12110 and then Present (Generic_Associations (Inst_Node))
12112 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12114 -- Hide them from visibility; required to avoid conflicts
12115 -- installing the parent instance.
12117 if Present (Ctx_Parents) then
12118 Push_Scope (Standard_Standard);
12119 Ctx_Top := Scope_Stack.Last;
12120 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12125 -- If it is a child unit, make the parent instance (which is an
12126 -- instance of the parent of the generic) visible. The parent
12127 -- instance is the prefix of the name of the generic unit.
12129 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12130 and then Nkind (Gen_Id) = N_Expanded_Name
12132 Par_Ent := Entity (Prefix (Gen_Id));
12133 Par_Vis := Is_Immediately_Visible (Par_Ent);
12134 Install_Parent (Par_Ent, In_Body => True);
12135 Par_Installed := True;
12137 elsif Is_Child_Unit (Gen_Unit) then
12138 Par_Ent := Scope (Gen_Unit);
12139 Par_Vis := Is_Immediately_Visible (Par_Ent);
12140 Install_Parent (Par_Ent, In_Body => True);
12141 Par_Installed := True;
12144 -- If the instantiation is a library unit, and this is the main unit,
12145 -- then build the resulting compilation unit nodes for the instance.
12146 -- If this is a compilation unit but it is not the main unit, then it
12147 -- is the body of a unit in the context, that is being compiled
12148 -- because it is encloses some inlined unit or another generic unit
12149 -- being instantiated. In that case, this body is not part of the
12150 -- current compilation, and is not attached to the tree, but its
12151 -- parent must be set for analysis.
12153 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12155 -- Replace instance node with body of instance, and create new
12156 -- node for corresponding instance declaration.
12158 Build_Instance_Compilation_Unit_Nodes
12159 (Inst_Node, Act_Body, Act_Decl);
12161 -- If the instantiation appears within a generic child package
12162 -- enable visibility of current instance of enclosing generic
12165 if Present (Ctx_Parents) then
12166 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12167 Analyze (Inst_Node);
12168 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12170 Analyze (Inst_Node);
12173 if Parent (Inst_Node) = Cunit (Main_Unit) then
12175 -- If the instance is a child unit itself, then set the scope
12176 -- of the expanded body to be the parent of the instantiation
12177 -- (ensuring that the fully qualified name will be generated
12178 -- for the elaboration subprogram).
12180 if Nkind (Defining_Unit_Name (Act_Spec)) =
12181 N_Defining_Program_Unit_Name
12183 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12187 -- Case where instantiation is not a library unit
12190 -- If this is an early instantiation, i.e. appears textually
12191 -- before the corresponding body and must be elaborated first,
12192 -- indicate that the body instance is to be delayed.
12194 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12196 -- If the instantiation appears within a generic child package
12197 -- enable visibility of current instance of enclosing generic
12200 if Present (Ctx_Parents) then
12201 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12202 Analyze (Act_Body);
12203 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12205 Analyze (Act_Body);
12209 Inherit_Context (Gen_Body, Inst_Node);
12211 if Par_Installed then
12212 Remove_Parent (In_Body => True);
12214 -- Restore the previous visibility of the parent
12216 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12219 -- Remove the parent instances if they have been placed on the scope
12220 -- stack to compile the body.
12222 if Present (Ctx_Parents) then
12223 pragma Assert (Scope_Stack.Last = Ctx_Top
12224 and then Current_Scope = Standard_Standard);
12227 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12230 pragma Assert (Current_Scope = Scope_Check_Id);
12231 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12233 Restore_Hidden_Primitives (Vis_Prims_List);
12235 -- Restore the private views that were made visible when the body of
12236 -- the instantiation was created. Note that, in the case where one of
12237 -- these private views is declared in the parent, there is a nesting
12238 -- issue with the calls to Install_Parent and Remove_Parent made in
12239 -- between above with In_Body set to True, because these calls also
12240 -- want to swap and restore this private view respectively. In this
12241 -- case, the call to Install_Parent does nothing, but the call to
12242 -- Remove_Parent does restore the private view, thus undercutting the
12243 -- call to Restore_Private_Views. That's OK under the condition that
12244 -- the two mechanisms swap exactly the same entities, in particular
12245 -- the private entities dependent on the primary private entities.
12247 Restore_Private_Views (Act_Decl_Id);
12249 -- Remove the current unit from visibility if this is an instance
12250 -- that is not elaborated on the fly for inlining purposes.
12252 if not Inlined_Body then
12253 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12258 -- If we have no body, and the unit requires a body, then complain. This
12259 -- complaint is suppressed if we have detected other errors (since a
12260 -- common reason for missing the body is that it had errors).
12261 -- In CodePeer mode, a warning has been emitted already, no need for
12262 -- further messages.
12264 elsif Unit_Requires_Body (Gen_Unit)
12265 and then not Body_Optional
12267 if CodePeer_Mode then
12270 elsif Serious_Errors_Detected = 0 then
12272 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12274 -- Don't attempt to perform any cleanup actions if some other error
12275 -- was already detected, since this can cause blowups.
12281 -- Case of package that does not need a body
12284 -- If the instantiation of the declaration is a library unit, rewrite
12285 -- the original package instantiation as a package declaration in the
12286 -- compilation unit node.
12288 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12289 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12290 Rewrite (Inst_Node, Act_Decl);
12292 -- Generate elaboration entity, in case spec has elaboration code.
12293 -- This cannot be done when the instance is analyzed, because it
12294 -- is not known yet whether the body exists.
12296 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12297 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12299 -- If the instantiation is not a library unit, then append the
12300 -- declaration to the list of implicitly generated entities, unless
12301 -- it is already a list member which means that it was already
12304 elsif not Is_List_Member (Act_Decl) then
12305 Mark_Rewrite_Insertion (Act_Decl);
12306 Insert_Before (Inst_Node, Act_Decl);
12312 -- Restore the context that was in effect prior to instantiating the
12315 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12316 Local_Suppress_Stack_Top := Saved_LSST;
12317 Scope_Suppress := Saved_SS;
12318 Style_Check := Saved_SC;
12320 Expander_Mode_Restore;
12321 Restore_Config_Switches (Saved_CS);
12322 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12323 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12324 Restore_Warnings (Saved_Warn);
12325 end Instantiate_Package_Body;
12327 ---------------------------------
12328 -- Instantiate_Subprogram_Body --
12329 ---------------------------------
12331 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12332 -- must be replaced by gotos which jump to the end of the routine in order
12333 -- to restore the Ghost and SPARK modes.
12335 procedure Instantiate_Subprogram_Body
12336 (Body_Info : Pending_Body_Info;
12337 Body_Optional : Boolean := False)
12339 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12340 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12341 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12342 Gen_Id : constant Node_Id := Name (Inst_Node);
12343 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12344 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12345 Loc : constant Source_Ptr := Sloc (Inst_Node);
12346 Pack_Id : constant Entity_Id :=
12347 Defining_Unit_Name (Parent (Act_Decl));
12349 -- The following constants capture the context prior to instantiating
12350 -- the subprogram body.
12352 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12353 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12354 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12355 Saved_ISMP : constant Boolean :=
12356 Ignore_SPARK_Mode_Pragmas_In_Instance;
12357 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12358 Local_Suppress_Stack_Top;
12359 Saved_SC : constant Boolean := Style_Check;
12360 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12361 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12362 Saved_SS : constant Suppress_Record := Scope_Suppress;
12363 Saved_Warn : constant Warning_Record := Save_Warnings;
12365 Act_Body : Node_Id;
12366 Act_Body_Id : Entity_Id;
12367 Gen_Body : Node_Id;
12368 Gen_Body_Id : Node_Id;
12369 Pack_Body : Node_Id;
12370 Par_Ent : Entity_Id := Empty;
12371 Par_Installed : Boolean := False;
12372 Par_Vis : Boolean := False;
12373 Ret_Expr : Node_Id;
12376 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12378 -- Subprogram body may have been created already because of an inline
12379 -- pragma, or because of multiple elaborations of the enclosing package
12380 -- when several instances of the subprogram appear in the main unit.
12382 if Present (Corresponding_Body (Act_Decl)) then
12386 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12387 -- the mode now to ensure that any nodes generated during instantiation
12388 -- are properly marked as Ghost.
12390 Set_Ghost_Mode (Act_Decl_Id);
12392 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12394 -- Re-establish the state of information on which checks are suppressed.
12395 -- This information was set in Body_Info at the point of instantiation,
12396 -- and now we restore it so that the instance is compiled using the
12397 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12399 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12400 Scope_Suppress := Body_Info.Scope_Suppress;
12402 Restore_Config_Switches (Body_Info.Config_Switches);
12403 Restore_Warnings (Body_Info.Warnings);
12405 if No (Gen_Body_Id) then
12407 -- For imported generic subprogram, no body to compile, complete
12408 -- the spec entity appropriately.
12410 if Is_Imported (Gen_Unit) then
12411 Set_Is_Imported (Act_Decl_Id);
12412 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12413 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12414 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12415 Set_Has_Completion (Act_Decl_Id);
12418 -- For other cases, compile the body
12421 Load_Parent_Of_Generic
12422 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12423 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12427 Instantiation_Node := Inst_Node;
12429 if Present (Gen_Body_Id) then
12430 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12432 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12434 -- Either body is not present, or context is non-expanding, as
12435 -- when compiling a subunit. Mark the instance as completed, and
12436 -- diagnose a missing body when needed.
12439 and then Operating_Mode = Generate_Code
12441 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12444 Set_Has_Completion (Act_Decl_Id);
12448 Save_Env (Gen_Unit, Act_Decl_Id);
12449 Style_Check := False;
12451 -- If the context of the instance is subject to SPARK_Mode "off", the
12452 -- annotation is missing, or the body is instantiated at a later pass
12453 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12454 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12457 if SPARK_Mode /= On
12458 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12460 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12463 -- If the context of an instance is not subject to SPARK_Mode "off",
12464 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12465 -- the latter should be the one applicable to the instance.
12467 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12468 and then SPARK_Mode /= Off
12469 and then Present (SPARK_Pragma (Gen_Body_Id))
12471 Set_SPARK_Mode (Gen_Body_Id);
12474 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12475 Create_Instantiation_Source
12482 (Original_Node (Gen_Body), Empty, Instantiating => True);
12484 -- Create proper defining name for the body, to correspond to the one
12488 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12490 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12491 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12493 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12494 Set_Has_Completion (Act_Decl_Id);
12495 Check_Generic_Actuals (Pack_Id, False);
12497 -- Generate a reference to link the visible subprogram instance to
12498 -- the generic body, which for navigation purposes is the only
12499 -- available source for the instance.
12502 (Related_Instance (Pack_Id),
12503 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12505 -- If it is a child unit, make the parent instance (which is an
12506 -- instance of the parent of the generic) visible. The parent
12507 -- instance is the prefix of the name of the generic unit.
12509 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12510 and then Nkind (Gen_Id) = N_Expanded_Name
12512 Par_Ent := Entity (Prefix (Gen_Id));
12513 Par_Vis := Is_Immediately_Visible (Par_Ent);
12514 Install_Parent (Par_Ent, In_Body => True);
12515 Par_Installed := True;
12517 elsif Is_Child_Unit (Gen_Unit) then
12518 Par_Ent := Scope (Gen_Unit);
12519 Par_Vis := Is_Immediately_Visible (Par_Ent);
12520 Install_Parent (Par_Ent, In_Body => True);
12521 Par_Installed := True;
12524 -- Subprogram body is placed in the body of wrapper package,
12525 -- whose spec contains the subprogram declaration as well as
12526 -- the renaming declarations for the generic parameters.
12529 Make_Package_Body (Loc,
12530 Defining_Unit_Name => New_Copy (Pack_Id),
12531 Declarations => New_List (Act_Body));
12533 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12535 -- If the instantiation is a library unit, then build resulting
12536 -- compilation unit nodes for the instance. The declaration of
12537 -- the enclosing package is the grandparent of the subprogram
12538 -- declaration. First replace the instantiation node as the unit
12539 -- of the corresponding compilation.
12541 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12542 if Parent (Inst_Node) = Cunit (Main_Unit) then
12543 Set_Unit (Parent (Inst_Node), Inst_Node);
12544 Build_Instance_Compilation_Unit_Nodes
12545 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12546 Analyze (Inst_Node);
12548 Set_Parent (Pack_Body, Parent (Inst_Node));
12549 Analyze (Pack_Body);
12553 Insert_Before (Inst_Node, Pack_Body);
12554 Mark_Rewrite_Insertion (Pack_Body);
12555 Analyze (Pack_Body);
12557 if Expander_Active then
12558 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12562 Inherit_Context (Gen_Body, Inst_Node);
12564 Restore_Private_Views (Pack_Id, False);
12566 if Par_Installed then
12567 Remove_Parent (In_Body => True);
12569 -- Restore the previous visibility of the parent
12571 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12576 -- Body not found. Error was emitted already. If there were no previous
12577 -- errors, this may be an instance whose scope is a premature instance.
12578 -- In that case we must insure that the (legal) program does raise
12579 -- program error if executed. We generate a subprogram body for this
12580 -- purpose. See DEC ac30vso.
12582 -- Should not reference proprietary DEC tests in comments ???
12584 elsif Serious_Errors_Detected = 0
12585 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12587 if Body_Optional then
12590 elsif Ekind (Act_Decl_Id) = E_Procedure then
12592 Make_Subprogram_Body (Loc,
12594 Make_Procedure_Specification (Loc,
12595 Defining_Unit_Name =>
12596 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12597 Parameter_Specifications =>
12599 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12601 Declarations => Empty_List,
12602 Handled_Statement_Sequence =>
12603 Make_Handled_Sequence_Of_Statements (Loc,
12604 Statements => New_List (
12605 Make_Raise_Program_Error (Loc,
12606 Reason => PE_Access_Before_Elaboration))));
12610 Make_Raise_Program_Error (Loc,
12611 Reason => PE_Access_Before_Elaboration);
12613 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12614 Set_Analyzed (Ret_Expr);
12617 Make_Subprogram_Body (Loc,
12619 Make_Function_Specification (Loc,
12620 Defining_Unit_Name =>
12621 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12622 Parameter_Specifications =>
12624 (Parameter_Specifications (Parent (Act_Decl_Id))),
12625 Result_Definition =>
12626 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12628 Declarations => Empty_List,
12629 Handled_Statement_Sequence =>
12630 Make_Handled_Sequence_Of_Statements (Loc,
12631 Statements => New_List (
12632 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12636 Make_Package_Body (Loc,
12637 Defining_Unit_Name => New_Copy (Pack_Id),
12638 Declarations => New_List (Act_Body));
12640 Insert_After (Inst_Node, Pack_Body);
12641 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12642 Analyze (Pack_Body);
12647 -- Restore the context that was in effect prior to instantiating the
12648 -- subprogram body.
12650 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12651 Local_Suppress_Stack_Top := Saved_LSST;
12652 Scope_Suppress := Saved_SS;
12653 Style_Check := Saved_SC;
12655 Expander_Mode_Restore;
12656 Restore_Config_Switches (Saved_CS);
12657 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12658 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12659 Restore_Warnings (Saved_Warn);
12660 end Instantiate_Subprogram_Body;
12662 ----------------------
12663 -- Instantiate_Type --
12664 ----------------------
12666 function Instantiate_Type
12669 Analyzed_Formal : Node_Id;
12670 Actual_Decls : List_Id) return List_Id
12672 A_Gen_T : constant Entity_Id :=
12673 Defining_Identifier (Analyzed_Formal);
12674 Def : constant Node_Id := Formal_Type_Definition (Formal);
12675 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12677 Ancestor : Entity_Id := Empty;
12678 Decl_Node : Node_Id;
12679 Decl_Nodes : List_Id;
12683 procedure Check_Shared_Variable_Control_Aspects;
12684 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12685 -- that may be specified for a formal type are obeyed by the actual.
12687 procedure Diagnose_Predicated_Actual;
12688 -- There are a number of constructs in which a discrete type with
12689 -- predicates is illegal, e.g. as an index in an array type declaration.
12690 -- If a generic type is used is such a construct in a generic package
12691 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12692 -- of the generic contract that the actual cannot have predicates.
12694 procedure Validate_Array_Type_Instance;
12695 procedure Validate_Access_Subprogram_Instance;
12696 procedure Validate_Access_Type_Instance;
12697 procedure Validate_Derived_Type_Instance;
12698 procedure Validate_Derived_Interface_Type_Instance;
12699 procedure Validate_Discriminated_Formal_Type;
12700 procedure Validate_Interface_Type_Instance;
12701 procedure Validate_Private_Type_Instance;
12702 procedure Validate_Incomplete_Type_Instance;
12703 -- These procedures perform validation tests for the named case.
12704 -- Validate_Discriminated_Formal_Type is shared by formal private
12705 -- types and Ada 2012 formal incomplete types.
12707 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12708 -- Check that base types are the same and that the subtypes match
12709 -- statically. Used in several of the above.
12711 --------------------------------------------
12712 -- Check_Shared_Variable_Control_Aspects --
12713 --------------------------------------------
12715 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12716 -- that may be specified for the formal are obeyed by the actual.
12717 -- If the formal is a derived type the aspect specifications must match.
12718 -- NOTE: AI12-0282 implies that matching of aspects is required between
12719 -- formal and actual in all cases, but this is too restrictive.
12720 -- In particular it violates a language design rule: a limited private
12721 -- indefinite formal can be matched by any actual. The current code
12722 -- reflects an older and more permissive version of RM C.6 (12/5).
12724 procedure Check_Shared_Variable_Control_Aspects is
12726 if Ada_Version >= Ada_2020 then
12727 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12729 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12731 elsif Is_Derived_Type (A_Gen_T)
12732 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12735 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12738 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12740 ("actual for& must have Volatile aspect",
12743 elsif Is_Derived_Type (A_Gen_T)
12744 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12747 ("actual for& has different Volatile aspect",
12751 -- We assume that an array type whose atomic component type
12752 -- is Atomic is equivalent to an array type with the explicit
12753 -- aspect Has_Atomic_Components. This is a reasonable inference
12754 -- from the intent of AI12-0282, and makes it legal to use an
12755 -- actual that does not have the identical aspect as the formal.
12756 -- Ditto for volatile components.
12759 Actual_Atomic_Comp : constant Boolean :=
12760 Has_Atomic_Components (Act_T)
12761 or else (Is_Array_Type (Act_T)
12762 and then Is_Atomic (Component_Type (Act_T)));
12764 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12766 ("formal and actual for& must agree on atomic components",
12772 Actual_Volatile_Comp : constant Boolean :=
12773 Has_Volatile_Components (Act_T)
12774 or else (Is_Array_Type (Act_T)
12775 and then Is_Volatile (Component_Type (Act_T)));
12777 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12780 ("actual for& must have volatile components",
12785 -- The following two aspects do not require exact matching,
12786 -- but only one-way agreement. See RM C.6.
12788 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12791 ("actual for& must have Independent aspect specified",
12795 if Has_Independent_Components (A_Gen_T)
12796 and then not Has_Independent_Components (Act_T)
12799 ("actual for& must have Independent_Components specified",
12803 -- Check actual/formal compatibility with respect to the four
12804 -- volatility refinement aspects.
12806 Check_Volatility_Compatibility
12808 "actual type", "its corresponding formal type",
12809 Srcpos_Bearer => Act_T);
12811 end Check_Shared_Variable_Control_Aspects;
12813 ---------------------------------
12814 -- Diagnose_Predicated_Actual --
12815 ---------------------------------
12817 procedure Diagnose_Predicated_Actual is
12819 if No_Predicate_On_Actual (A_Gen_T)
12820 and then Has_Predicates (Act_T)
12823 ("actual for& cannot be a type with predicate",
12824 Instantiation_Node, A_Gen_T);
12826 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12827 and then Has_Predicates (Act_T)
12828 and then not Has_Static_Predicate_Aspect (Act_T)
12831 ("actual for& cannot be a type with a dynamic predicate",
12832 Instantiation_Node, A_Gen_T);
12834 end Diagnose_Predicated_Actual;
12836 --------------------
12837 -- Subtypes_Match --
12838 --------------------
12840 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12841 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12844 -- Some detailed comments would be useful here ???
12846 return ((Base_Type (T) = Act_T
12847 or else Base_Type (T) = Base_Type (Act_T))
12848 and then Subtypes_Statically_Match (T, Act_T))
12850 or else (Is_Class_Wide_Type (Gen_T)
12851 and then Is_Class_Wide_Type (Act_T)
12852 and then Subtypes_Match
12853 (Get_Instance_Of (Root_Type (Gen_T)),
12854 Root_Type (Act_T)))
12857 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12858 | E_Anonymous_Access_Type
12859 and then Ekind (Act_T) = Ekind (Gen_T)
12860 and then Subtypes_Statically_Match
12861 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12862 end Subtypes_Match;
12864 -----------------------------------------
12865 -- Validate_Access_Subprogram_Instance --
12866 -----------------------------------------
12868 procedure Validate_Access_Subprogram_Instance is
12870 if not Is_Access_Type (Act_T)
12871 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12874 ("expect access type in instantiation of &", Actual, Gen_T);
12875 Abandon_Instantiation (Actual);
12878 -- According to AI05-288, actuals for access_to_subprograms must be
12879 -- subtype conformant with the generic formal. Previous to AI05-288
12880 -- only mode conformance was required.
12882 -- This is a binding interpretation that applies to previous versions
12883 -- of the language, no need to maintain previous weaker checks.
12885 Check_Subtype_Conformant
12886 (Designated_Type (Act_T),
12887 Designated_Type (A_Gen_T),
12891 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12892 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12894 ("protected access type not allowed for formal &",
12898 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12900 ("expect protected access type for formal &",
12904 -- If the formal has a specified convention (which in most cases
12905 -- will be StdCall) verify that the actual has the same convention.
12907 if Has_Convention_Pragma (A_Gen_T)
12908 and then Convention (A_Gen_T) /= Convention (Act_T)
12910 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12912 ("actual for formal & must have convention %", Actual, Gen_T);
12915 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12917 ("non null exclusion of actual and formal & do not match",
12920 end Validate_Access_Subprogram_Instance;
12922 -----------------------------------
12923 -- Validate_Access_Type_Instance --
12924 -----------------------------------
12926 procedure Validate_Access_Type_Instance is
12927 Desig_Type : constant Entity_Id :=
12928 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12929 Desig_Act : Entity_Id;
12932 if not Is_Access_Type (Act_T) then
12934 ("expect access type in instantiation of &", Actual, Gen_T);
12935 Abandon_Instantiation (Actual);
12938 if Is_Access_Constant (A_Gen_T) then
12939 if not Is_Access_Constant (Act_T) then
12941 ("actual type must be access-to-constant type", Actual);
12942 Abandon_Instantiation (Actual);
12945 if Is_Access_Constant (Act_T) then
12947 ("actual type must be access-to-variable type", Actual);
12948 Abandon_Instantiation (Actual);
12950 elsif Ekind (A_Gen_T) = E_General_Access_Type
12951 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12954 ("actual must be general access type!", Actual);
12955 Error_Msg_NE -- CODEFIX
12956 ("\add ALL to }!", Actual, Act_T);
12957 Abandon_Instantiation (Actual);
12961 -- The designated subtypes, that is to say the subtypes introduced
12962 -- by an access type declaration (and not by a subtype declaration)
12965 Desig_Act := Designated_Type (Base_Type (Act_T));
12967 -- The designated type may have been introduced through a limited_
12968 -- with clause, in which case retrieve the non-limited view. This
12969 -- applies to incomplete types as well as to class-wide types.
12971 if From_Limited_With (Desig_Act) then
12972 Desig_Act := Available_View (Desig_Act);
12975 if not Subtypes_Match (Desig_Type, Desig_Act) then
12977 ("designated type of actual does not match that of formal &",
12980 if not Predicates_Match (Desig_Type, Desig_Act) then
12981 Error_Msg_N ("\predicates do not match", Actual);
12984 Abandon_Instantiation (Actual);
12987 -- Ada 2005: null-exclusion indicators of the two types must agree
12989 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12991 ("non null exclusion of actual and formal & do not match",
12994 end Validate_Access_Type_Instance;
12996 ----------------------------------
12997 -- Validate_Array_Type_Instance --
12998 ----------------------------------
13000 procedure Validate_Array_Type_Instance is
13005 function Formal_Dimensions return Nat;
13006 -- Count number of dimensions in array type formal
13008 -----------------------
13009 -- Formal_Dimensions --
13010 -----------------------
13012 function Formal_Dimensions return Nat is
13017 if Nkind (Def) = N_Constrained_Array_Definition then
13018 Index := First (Discrete_Subtype_Definitions (Def));
13020 Index := First (Subtype_Marks (Def));
13023 while Present (Index) loop
13025 Next_Index (Index);
13029 end Formal_Dimensions;
13031 -- Start of processing for Validate_Array_Type_Instance
13034 if not Is_Array_Type (Act_T) then
13036 ("expect array type in instantiation of &", Actual, Gen_T);
13037 Abandon_Instantiation (Actual);
13039 elsif Nkind (Def) = N_Constrained_Array_Definition then
13040 if not (Is_Constrained (Act_T)) then
13042 ("expect constrained array in instantiation of &",
13044 Abandon_Instantiation (Actual);
13048 if Is_Constrained (Act_T) then
13050 ("expect unconstrained array in instantiation of &",
13052 Abandon_Instantiation (Actual);
13056 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13058 ("dimensions of actual do not match formal &", Actual, Gen_T);
13059 Abandon_Instantiation (Actual);
13062 I1 := First_Index (A_Gen_T);
13063 I2 := First_Index (Act_T);
13064 for J in 1 .. Formal_Dimensions loop
13066 -- If the indexes of the actual were given by a subtype_mark,
13067 -- the index was transformed into a range attribute. Retrieve
13068 -- the original type mark for checking.
13070 if Is_Entity_Name (Original_Node (I2)) then
13071 T2 := Entity (Original_Node (I2));
13076 if not Subtypes_Match
13077 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13080 ("index types of actual do not match those of formal &",
13082 Abandon_Instantiation (Actual);
13089 -- Check matching subtypes. Note that there are complex visibility
13090 -- issues when the generic is a child unit and some aspect of the
13091 -- generic type is declared in a parent unit of the generic. We do
13092 -- the test to handle this special case only after a direct check
13093 -- for static matching has failed. The case where both the component
13094 -- type and the array type are separate formals, and the component
13095 -- type is a private view may also require special checking in
13096 -- Subtypes_Match. Finally, we assume that a child instance where
13097 -- the component type comes from a formal of a parent instance is
13098 -- correct because the generic was correct. A more precise check
13099 -- seems too complex to install???
13102 (Component_Type (A_Gen_T), Component_Type (Act_T))
13105 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13106 Component_Type (Act_T))
13108 (not Inside_A_Generic
13109 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13114 ("component subtype of actual does not match that of formal &",
13116 Abandon_Instantiation (Actual);
13119 if Has_Aliased_Components (A_Gen_T)
13120 and then not Has_Aliased_Components (Act_T)
13123 ("actual must have aliased components to match formal type &",
13126 end Validate_Array_Type_Instance;
13128 -----------------------------------------------
13129 -- Validate_Derived_Interface_Type_Instance --
13130 -----------------------------------------------
13132 procedure Validate_Derived_Interface_Type_Instance is
13133 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13137 -- First apply interface instance checks
13139 Validate_Interface_Type_Instance;
13141 -- Verify that immediate parent interface is an ancestor of
13145 and then not Interface_Present_In_Ancestor (Act_T, Par)
13148 ("interface actual must include progenitor&", Actual, Par);
13151 -- Now verify that the actual includes all other ancestors of
13154 Elmt := First_Elmt (Interfaces (A_Gen_T));
13155 while Present (Elmt) loop
13156 if not Interface_Present_In_Ancestor
13157 (Act_T, Get_Instance_Of (Node (Elmt)))
13160 ("interface actual must include progenitor&",
13161 Actual, Node (Elmt));
13166 end Validate_Derived_Interface_Type_Instance;
13168 ------------------------------------
13169 -- Validate_Derived_Type_Instance --
13170 ------------------------------------
13172 procedure Validate_Derived_Type_Instance is
13173 Actual_Discr : Entity_Id;
13174 Ancestor_Discr : Entity_Id;
13177 -- Verify that the actual includes the progenitors of the formal,
13178 -- if any. The formal may depend on previous formals and their
13179 -- instance, so we must examine instance of interfaces if present.
13180 -- The actual may be an extension of an interface, in which case
13181 -- it does not appear in the interface list, so this must be
13182 -- checked separately.
13184 if Present (Interface_List (Def)) then
13185 if not Has_Interfaces (Act_T) then
13187 ("actual must implement all interfaces of formal&",
13192 Act_Iface_List : Elist_Id;
13194 Iface_Ent : Entity_Id;
13196 function Instance_Exists (I : Entity_Id) return Boolean;
13197 -- If the interface entity is declared in a generic unit,
13198 -- this can only be legal if we are within an instantiation
13199 -- of a child of that generic. There is currently no
13200 -- mechanism to relate an interface declared within a
13201 -- generic to the corresponding interface in an instance,
13202 -- so we traverse the list of interfaces of the actual,
13203 -- looking for a name match.
13205 ---------------------
13206 -- Instance_Exists --
13207 ---------------------
13209 function Instance_Exists (I : Entity_Id) return Boolean is
13210 Iface_Elmt : Elmt_Id;
13213 Iface_Elmt := First_Elmt (Act_Iface_List);
13214 while Present (Iface_Elmt) loop
13215 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13216 and then Chars (Node (Iface_Elmt)) = Chars (I)
13221 Next_Elmt (Iface_Elmt);
13225 end Instance_Exists;
13228 Iface := First (Abstract_Interface_List (A_Gen_T));
13229 Collect_Interfaces (Act_T, Act_Iface_List);
13231 while Present (Iface) loop
13232 Iface_Ent := Get_Instance_Of (Entity (Iface));
13234 if Is_Ancestor (Iface_Ent, Act_T)
13235 or else Is_Progenitor (Iface_Ent, Act_T)
13239 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13240 and then Instance_Exists (Iface_Ent)
13245 Error_Msg_Name_1 := Chars (Act_T);
13247 ("actual% must implement interface&",
13248 Actual, Etype (Iface));
13257 -- If the parent type in the generic declaration is itself a previous
13258 -- formal type, then it is local to the generic and absent from the
13259 -- analyzed generic definition. In that case the ancestor is the
13260 -- instance of the formal (which must have been instantiated
13261 -- previously), unless the ancestor is itself a formal derived type.
13262 -- In this latter case (which is the subject of Corrigendum 8652/0038
13263 -- (AI-202) the ancestor of the formals is the ancestor of its
13264 -- parent. Otherwise, the analyzed generic carries the parent type.
13265 -- If the parent type is defined in a previous formal package, then
13266 -- the scope of that formal package is that of the generic type
13267 -- itself, and it has already been mapped into the corresponding type
13268 -- in the actual package.
13270 -- Common case: parent type defined outside of the generic
13272 if Is_Entity_Name (Subtype_Mark (Def))
13273 and then Present (Entity (Subtype_Mark (Def)))
13275 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13277 -- Check whether parent is defined in a previous formal package
13280 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13283 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13285 -- The type may be a local derivation, or a type extension of a
13286 -- previous formal, or of a formal of a parent package.
13288 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13290 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13292 -- Check whether the parent is another derived formal type in the
13293 -- same generic unit.
13295 if Etype (A_Gen_T) /= A_Gen_T
13296 and then Is_Generic_Type (Etype (A_Gen_T))
13297 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13298 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13300 -- Locate ancestor of parent from the subtype declaration
13301 -- created for the actual.
13307 Decl := First (Actual_Decls);
13308 while Present (Decl) loop
13309 if Nkind (Decl) = N_Subtype_Declaration
13310 and then Chars (Defining_Identifier (Decl)) =
13311 Chars (Etype (A_Gen_T))
13313 Ancestor := Generic_Parent_Type (Decl);
13321 pragma Assert (Present (Ancestor));
13323 -- The ancestor itself may be a previous formal that has been
13326 Ancestor := Get_Instance_Of (Ancestor);
13330 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13333 -- Check whether parent is a previous formal of the current generic
13335 elsif Is_Derived_Type (A_Gen_T)
13336 and then Is_Generic_Type (Etype (A_Gen_T))
13337 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13339 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13341 -- An unusual case: the actual is a type declared in a parent unit,
13342 -- but is not a formal type so there is no instance_of for it.
13343 -- Retrieve it by analyzing the record extension.
13345 elsif Is_Child_Unit (Scope (A_Gen_T))
13346 and then In_Open_Scopes (Scope (Act_T))
13347 and then Is_Generic_Instance (Scope (Act_T))
13349 Analyze (Subtype_Mark (Def));
13350 Ancestor := Entity (Subtype_Mark (Def));
13353 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13356 -- If the formal derived type has pragma Preelaborable_Initialization
13357 -- then the actual type must have preelaborable initialization.
13359 if Known_To_Have_Preelab_Init (A_Gen_T)
13360 and then not Has_Preelaborable_Initialization (Act_T)
13363 ("actual for & must have preelaborable initialization",
13367 -- Ada 2005 (AI-251)
13369 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13370 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13372 ("(Ada 2005) expected type implementing & in instantiation",
13376 -- Finally verify that the (instance of) the ancestor is an ancestor
13379 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13381 ("expect type derived from & in instantiation",
13382 Actual, First_Subtype (Ancestor));
13383 Abandon_Instantiation (Actual);
13386 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13387 -- that the formal type declaration has been rewritten as a private
13390 if Ada_Version >= Ada_2005
13391 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13392 and then Synchronized_Present (Parent (A_Gen_T))
13394 -- The actual must be a synchronized tagged type
13396 if not Is_Tagged_Type (Act_T) then
13398 ("actual of synchronized type must be tagged", Actual);
13399 Abandon_Instantiation (Actual);
13401 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13402 and then Nkind (Type_Definition (Parent (Act_T))) =
13403 N_Derived_Type_Definition
13404 and then not Synchronized_Present
13405 (Type_Definition (Parent (Act_T)))
13408 ("actual of synchronized type must be synchronized", Actual);
13409 Abandon_Instantiation (Actual);
13413 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13414 -- removes the second instance of the phrase "or allow pass by copy".
13416 -- For Ada 2020, the aspect may be specified explicitly for the
13417 -- formal regardless of whether an ancestor obeys it.
13419 if Is_Atomic (Act_T)
13420 and then not Is_Atomic (Ancestor)
13421 and then not Is_Atomic (A_Gen_T)
13424 ("cannot have atomic actual type for non-atomic formal type",
13427 elsif Is_Volatile (Act_T)
13428 and then not Is_Volatile (Ancestor)
13429 and then not Is_Volatile (A_Gen_T)
13432 ("cannot have volatile actual type for non-volatile formal type",
13436 -- It should not be necessary to check for unknown discriminants on
13437 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13438 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13439 -- needs fixing. ???
13441 if Is_Definite_Subtype (A_Gen_T)
13442 and then not Unknown_Discriminants_Present (Formal)
13443 and then not Is_Definite_Subtype (Act_T)
13445 Error_Msg_N ("actual subtype must be constrained", Actual);
13446 Abandon_Instantiation (Actual);
13449 if not Unknown_Discriminants_Present (Formal) then
13450 if Is_Constrained (Ancestor) then
13451 if not Is_Constrained (Act_T) then
13452 Error_Msg_N ("actual subtype must be constrained", Actual);
13453 Abandon_Instantiation (Actual);
13456 -- Ancestor is unconstrained, Check if generic formal and actual
13457 -- agree on constrainedness. The check only applies to array types
13458 -- and discriminated types.
13460 elsif Is_Constrained (Act_T) then
13461 if Ekind (Ancestor) = E_Access_Type
13462 or else (not Is_Constrained (A_Gen_T)
13463 and then Is_Composite_Type (A_Gen_T))
13465 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13466 Abandon_Instantiation (Actual);
13469 -- A class-wide type is only allowed if the formal has unknown
13472 elsif Is_Class_Wide_Type (Act_T)
13473 and then not Has_Unknown_Discriminants (Ancestor)
13476 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13477 Abandon_Instantiation (Actual);
13479 -- Otherwise, the formal and actual must have the same number
13480 -- of discriminants and each discriminant of the actual must
13481 -- correspond to a discriminant of the formal.
13483 elsif Has_Discriminants (Act_T)
13484 and then not Has_Unknown_Discriminants (Act_T)
13485 and then Has_Discriminants (Ancestor)
13487 Actual_Discr := First_Discriminant (Act_T);
13488 Ancestor_Discr := First_Discriminant (Ancestor);
13489 while Present (Actual_Discr)
13490 and then Present (Ancestor_Discr)
13492 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13493 No (Corresponding_Discriminant (Actual_Discr))
13496 ("discriminant & does not correspond "
13497 & "to ancestor discriminant", Actual, Actual_Discr);
13498 Abandon_Instantiation (Actual);
13501 Next_Discriminant (Actual_Discr);
13502 Next_Discriminant (Ancestor_Discr);
13505 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13507 ("actual for & must have same number of discriminants",
13509 Abandon_Instantiation (Actual);
13512 -- This case should be caught by the earlier check for
13513 -- constrainedness, but the check here is added for completeness.
13515 elsif Has_Discriminants (Act_T)
13516 and then not Has_Unknown_Discriminants (Act_T)
13519 ("actual for & must not have discriminants", Actual, Gen_T);
13520 Abandon_Instantiation (Actual);
13522 elsif Has_Discriminants (Ancestor) then
13524 ("actual for & must have known discriminants", Actual, Gen_T);
13525 Abandon_Instantiation (Actual);
13528 if not Subtypes_Statically_Compatible
13529 (Act_T, Ancestor, Formal_Derived_Matching => True)
13532 ("actual for & must be statically compatible with ancestor",
13535 if not Predicates_Compatible (Act_T, Ancestor) then
13537 ("\predicate on actual is not compatible with ancestor",
13541 Abandon_Instantiation (Actual);
13545 -- If the formal and actual types are abstract, check that there
13546 -- are no abstract primitives of the actual type that correspond to
13547 -- nonabstract primitives of the formal type (second sentence of
13550 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13551 Check_Abstract_Primitives : declare
13552 Gen_Prims : constant Elist_Id :=
13553 Primitive_Operations (A_Gen_T);
13554 Gen_Elmt : Elmt_Id;
13555 Gen_Subp : Entity_Id;
13556 Anc_Subp : Entity_Id;
13557 Anc_Formal : Entity_Id;
13558 Anc_F_Type : Entity_Id;
13560 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13561 Act_Elmt : Elmt_Id;
13562 Act_Subp : Entity_Id;
13563 Act_Formal : Entity_Id;
13564 Act_F_Type : Entity_Id;
13566 Subprograms_Correspond : Boolean;
13568 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13569 -- Returns true if T2 is derived directly or indirectly from
13570 -- T1, including derivations from interfaces. T1 and T2 are
13571 -- required to be specific tagged base types.
13573 ------------------------
13574 -- Is_Tagged_Ancestor --
13575 ------------------------
13577 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13579 Intfc_Elmt : Elmt_Id;
13582 -- The predicate is satisfied if the types are the same
13587 -- If we've reached the top of the derivation chain then
13588 -- we know that T1 is not an ancestor of T2.
13590 elsif Etype (T2) = T2 then
13593 -- Proceed to check T2's immediate parent
13595 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13598 -- Finally, check to see if T1 is an ancestor of any of T2's
13602 Intfc_Elmt := First_Elmt (Interfaces (T2));
13603 while Present (Intfc_Elmt) loop
13604 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13608 Next_Elmt (Intfc_Elmt);
13613 end Is_Tagged_Ancestor;
13615 -- Start of processing for Check_Abstract_Primitives
13618 -- Loop over all of the formal derived type's primitives
13620 Gen_Elmt := First_Elmt (Gen_Prims);
13621 while Present (Gen_Elmt) loop
13622 Gen_Subp := Node (Gen_Elmt);
13624 -- If the primitive of the formal is not abstract, then
13625 -- determine whether there is a corresponding primitive of
13626 -- the actual type that's abstract.
13628 if not Is_Abstract_Subprogram (Gen_Subp) then
13629 Act_Elmt := First_Elmt (Act_Prims);
13630 while Present (Act_Elmt) loop
13631 Act_Subp := Node (Act_Elmt);
13633 -- If we find an abstract primitive of the actual,
13634 -- then we need to test whether it corresponds to the
13635 -- subprogram from which the generic formal primitive
13638 if Is_Abstract_Subprogram (Act_Subp) then
13639 Anc_Subp := Alias (Gen_Subp);
13641 -- Test whether we have a corresponding primitive
13642 -- by comparing names, kinds, formal types, and
13645 if Chars (Anc_Subp) = Chars (Act_Subp)
13646 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13648 Anc_Formal := First_Formal (Anc_Subp);
13649 Act_Formal := First_Formal (Act_Subp);
13650 while Present (Anc_Formal)
13651 and then Present (Act_Formal)
13653 Anc_F_Type := Etype (Anc_Formal);
13654 Act_F_Type := Etype (Act_Formal);
13656 if Ekind (Anc_F_Type) =
13657 E_Anonymous_Access_Type
13659 Anc_F_Type := Designated_Type (Anc_F_Type);
13661 if Ekind (Act_F_Type) =
13662 E_Anonymous_Access_Type
13665 Designated_Type (Act_F_Type);
13671 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13676 Anc_F_Type := Base_Type (Anc_F_Type);
13677 Act_F_Type := Base_Type (Act_F_Type);
13679 -- If the formal is controlling, then the
13680 -- the type of the actual primitive's formal
13681 -- must be derived directly or indirectly
13682 -- from the type of the ancestor primitive's
13685 if Is_Controlling_Formal (Anc_Formal) then
13686 if not Is_Tagged_Ancestor
13687 (Anc_F_Type, Act_F_Type)
13692 -- Otherwise the types of the formals must
13695 elsif Anc_F_Type /= Act_F_Type then
13699 Next_Formal (Anc_Formal);
13700 Next_Formal (Act_Formal);
13703 -- If we traversed through all of the formals
13704 -- then so far the subprograms correspond, so
13705 -- now check that any result types correspond.
13707 if No (Anc_Formal) and then No (Act_Formal) then
13708 Subprograms_Correspond := True;
13710 if Ekind (Act_Subp) = E_Function then
13711 Anc_F_Type := Etype (Anc_Subp);
13712 Act_F_Type := Etype (Act_Subp);
13714 if Ekind (Anc_F_Type) =
13715 E_Anonymous_Access_Type
13718 Designated_Type (Anc_F_Type);
13720 if Ekind (Act_F_Type) =
13721 E_Anonymous_Access_Type
13724 Designated_Type (Act_F_Type);
13726 Subprograms_Correspond := False;
13731 = E_Anonymous_Access_Type
13733 Subprograms_Correspond := False;
13736 Anc_F_Type := Base_Type (Anc_F_Type);
13737 Act_F_Type := Base_Type (Act_F_Type);
13739 -- Now either the result types must be
13740 -- the same or, if the result type is
13741 -- controlling, the result type of the
13742 -- actual primitive must descend from the
13743 -- result type of the ancestor primitive.
13745 if Subprograms_Correspond
13746 and then Anc_F_Type /= Act_F_Type
13748 Has_Controlling_Result (Anc_Subp)
13749 and then not Is_Tagged_Ancestor
13750 (Anc_F_Type, Act_F_Type)
13752 Subprograms_Correspond := False;
13756 -- Found a matching subprogram belonging to
13757 -- formal ancestor type, so actual subprogram
13758 -- corresponds and this violates 3.9.3(9).
13760 if Subprograms_Correspond then
13762 ("abstract subprogram & overrides "
13763 & "nonabstract subprogram of ancestor",
13770 Next_Elmt (Act_Elmt);
13774 Next_Elmt (Gen_Elmt);
13776 end Check_Abstract_Primitives;
13779 -- Verify that limitedness matches. If parent is a limited
13780 -- interface then the generic formal is not unless declared
13781 -- explicitly so. If not declared limited, the actual cannot be
13782 -- limited (see AI05-0087).
13784 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13785 if not In_Instance then
13787 ("actual for non-limited & cannot be a limited type",
13789 Explain_Limited_Type (Act_T, Actual);
13790 Abandon_Instantiation (Actual);
13794 -- Check for AI12-0036
13797 Formal_Is_Private_Extension : constant Boolean :=
13798 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13800 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13803 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13804 if not In_Instance then
13805 if Actual_Is_Tagged then
13807 ("actual for & cannot be a tagged type", Actual, Gen_T);
13810 ("actual for & must be a tagged type", Actual, Gen_T);
13813 Abandon_Instantiation (Actual);
13817 end Validate_Derived_Type_Instance;
13819 ----------------------------------------
13820 -- Validate_Discriminated_Formal_Type --
13821 ----------------------------------------
13823 procedure Validate_Discriminated_Formal_Type is
13824 Formal_Discr : Entity_Id;
13825 Actual_Discr : Entity_Id;
13826 Formal_Subt : Entity_Id;
13829 if Has_Discriminants (A_Gen_T) then
13830 if not Has_Discriminants (Act_T) then
13832 ("actual for & must have discriminants", Actual, Gen_T);
13833 Abandon_Instantiation (Actual);
13835 elsif Is_Constrained (Act_T) then
13837 ("actual for & must be unconstrained", Actual, Gen_T);
13838 Abandon_Instantiation (Actual);
13841 Formal_Discr := First_Discriminant (A_Gen_T);
13842 Actual_Discr := First_Discriminant (Act_T);
13843 while Formal_Discr /= Empty loop
13844 if Actual_Discr = Empty then
13846 ("discriminants on actual do not match formal",
13848 Abandon_Instantiation (Actual);
13851 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13853 -- Access discriminants match if designated types do
13855 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13856 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13857 E_Anonymous_Access_Type
13860 (Designated_Type (Base_Type (Formal_Subt))) =
13861 Designated_Type (Base_Type (Etype (Actual_Discr)))
13865 elsif Base_Type (Formal_Subt) /=
13866 Base_Type (Etype (Actual_Discr))
13869 ("types of actual discriminants must match formal",
13871 Abandon_Instantiation (Actual);
13873 elsif not Subtypes_Statically_Match
13874 (Formal_Subt, Etype (Actual_Discr))
13875 and then Ada_Version >= Ada_95
13878 ("subtypes of actual discriminants must match formal",
13880 Abandon_Instantiation (Actual);
13883 Next_Discriminant (Formal_Discr);
13884 Next_Discriminant (Actual_Discr);
13887 if Actual_Discr /= Empty then
13889 ("discriminants on actual do not match formal",
13891 Abandon_Instantiation (Actual);
13895 end Validate_Discriminated_Formal_Type;
13897 ---------------------------------------
13898 -- Validate_Incomplete_Type_Instance --
13899 ---------------------------------------
13901 procedure Validate_Incomplete_Type_Instance is
13903 if not Is_Tagged_Type (Act_T)
13904 and then Is_Tagged_Type (A_Gen_T)
13907 ("actual for & must be a tagged type", Actual, Gen_T);
13910 Validate_Discriminated_Formal_Type;
13911 end Validate_Incomplete_Type_Instance;
13913 --------------------------------------
13914 -- Validate_Interface_Type_Instance --
13915 --------------------------------------
13917 procedure Validate_Interface_Type_Instance is
13919 if not Is_Interface (Act_T) then
13921 ("actual for formal interface type must be an interface",
13924 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13925 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13926 or else Is_Protected_Interface (A_Gen_T) /=
13927 Is_Protected_Interface (Act_T)
13928 or else Is_Synchronized_Interface (A_Gen_T) /=
13929 Is_Synchronized_Interface (Act_T)
13932 ("actual for interface& does not match (RM 12.5.5(4))",
13935 end Validate_Interface_Type_Instance;
13937 ------------------------------------
13938 -- Validate_Private_Type_Instance --
13939 ------------------------------------
13941 procedure Validate_Private_Type_Instance is
13943 if Is_Limited_Type (Act_T)
13944 and then not Is_Limited_Type (A_Gen_T)
13946 if In_Instance then
13950 ("actual for non-limited & cannot be a limited type", Actual,
13952 Explain_Limited_Type (Act_T, Actual);
13953 Abandon_Instantiation (Actual);
13956 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13957 and then not Has_Preelaborable_Initialization (Act_T)
13960 ("actual for & must have preelaborable initialization", Actual,
13963 elsif not Is_Definite_Subtype (Act_T)
13964 and then Is_Definite_Subtype (A_Gen_T)
13965 and then Ada_Version >= Ada_95
13968 ("actual for & must be a definite subtype", Actual, Gen_T);
13970 elsif not Is_Tagged_Type (Act_T)
13971 and then Is_Tagged_Type (A_Gen_T)
13974 ("actual for & must be a tagged type", Actual, Gen_T);
13977 Validate_Discriminated_Formal_Type;
13979 end Validate_Private_Type_Instance;
13981 -- Start of processing for Instantiate_Type
13984 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13985 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13986 return New_List (Error);
13988 elsif not Is_Entity_Name (Actual)
13989 or else not Is_Type (Entity (Actual))
13992 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13993 Abandon_Instantiation (Actual);
13996 Act_T := Entity (Actual);
13998 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13999 -- as a generic actual parameter if the corresponding formal type
14000 -- does not have a known_discriminant_part, or is a formal derived
14001 -- type that is an Unchecked_Union type.
14003 if Is_Unchecked_Union (Base_Type (Act_T)) then
14004 if not Has_Discriminants (A_Gen_T)
14005 or else (Is_Derived_Type (A_Gen_T)
14006 and then Is_Unchecked_Union (A_Gen_T))
14010 Error_Msg_N ("unchecked union cannot be the actual for a "
14011 & "discriminated formal type", Act_T);
14016 -- Deal with fixed/floating restrictions
14018 if Is_Floating_Point_Type (Act_T) then
14019 Check_Restriction (No_Floating_Point, Actual);
14020 elsif Is_Fixed_Point_Type (Act_T) then
14021 Check_Restriction (No_Fixed_Point, Actual);
14024 -- Deal with error of using incomplete type as generic actual.
14025 -- This includes limited views of a type, even if the non-limited
14026 -- view may be available.
14028 if Ekind (Act_T) = E_Incomplete_Type
14029 or else (Is_Class_Wide_Type (Act_T)
14030 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14032 -- If the formal is an incomplete type, the actual can be
14033 -- incomplete as well.
14035 if Ekind (A_Gen_T) = E_Incomplete_Type then
14038 elsif Is_Class_Wide_Type (Act_T)
14039 or else No (Full_View (Act_T))
14041 Error_Msg_N ("premature use of incomplete type", Actual);
14042 Abandon_Instantiation (Actual);
14044 Act_T := Full_View (Act_T);
14045 Set_Entity (Actual, Act_T);
14047 if Has_Private_Component (Act_T) then
14049 ("premature use of type with private component", Actual);
14053 -- Deal with error of premature use of private type as generic actual
14055 elsif Is_Private_Type (Act_T)
14056 and then Is_Private_Type (Base_Type (Act_T))
14057 and then not Is_Generic_Type (Act_T)
14058 and then not Is_Derived_Type (Act_T)
14059 and then No (Full_View (Root_Type (Act_T)))
14061 -- If the formal is an incomplete type, the actual can be
14062 -- private or incomplete as well.
14064 if Ekind (A_Gen_T) = E_Incomplete_Type then
14067 Error_Msg_N ("premature use of private type", Actual);
14070 elsif Has_Private_Component (Act_T) then
14072 ("premature use of type with private component", Actual);
14075 Set_Instance_Of (A_Gen_T, Act_T);
14077 -- If the type is generic, the class-wide type may also be used
14079 if Is_Tagged_Type (A_Gen_T)
14080 and then Is_Tagged_Type (Act_T)
14081 and then not Is_Class_Wide_Type (A_Gen_T)
14083 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14084 Class_Wide_Type (Act_T));
14087 if not Is_Abstract_Type (A_Gen_T)
14088 and then Is_Abstract_Type (Act_T)
14091 ("actual of non-abstract formal cannot be abstract", Actual);
14094 -- A generic scalar type is a first subtype for which we generate
14095 -- an anonymous base type. Indicate that the instance of this base
14096 -- is the base type of the actual.
14098 if Is_Scalar_Type (A_Gen_T) then
14099 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14103 Check_Shared_Variable_Control_Aspects;
14105 if Error_Posted (Act_T) then
14108 case Nkind (Def) is
14109 when N_Formal_Private_Type_Definition =>
14110 Validate_Private_Type_Instance;
14112 when N_Formal_Incomplete_Type_Definition =>
14113 Validate_Incomplete_Type_Instance;
14115 when N_Formal_Derived_Type_Definition =>
14116 Validate_Derived_Type_Instance;
14118 when N_Formal_Discrete_Type_Definition =>
14119 if not Is_Discrete_Type (Act_T) then
14121 ("expect discrete type in instantiation of&",
14123 Abandon_Instantiation (Actual);
14126 Diagnose_Predicated_Actual;
14128 when N_Formal_Signed_Integer_Type_Definition =>
14129 if not Is_Signed_Integer_Type (Act_T) then
14131 ("expect signed integer type in instantiation of&",
14133 Abandon_Instantiation (Actual);
14136 Diagnose_Predicated_Actual;
14138 when N_Formal_Modular_Type_Definition =>
14139 if not Is_Modular_Integer_Type (Act_T) then
14141 ("expect modular type in instantiation of &",
14143 Abandon_Instantiation (Actual);
14146 Diagnose_Predicated_Actual;
14148 when N_Formal_Floating_Point_Definition =>
14149 if not Is_Floating_Point_Type (Act_T) then
14151 ("expect float type in instantiation of &", Actual, Gen_T);
14152 Abandon_Instantiation (Actual);
14155 when N_Formal_Ordinary_Fixed_Point_Definition =>
14156 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14158 ("expect ordinary fixed point type in instantiation of &",
14160 Abandon_Instantiation (Actual);
14163 when N_Formal_Decimal_Fixed_Point_Definition =>
14164 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14166 ("expect decimal type in instantiation of &",
14168 Abandon_Instantiation (Actual);
14171 when N_Array_Type_Definition =>
14172 Validate_Array_Type_Instance;
14174 when N_Access_To_Object_Definition =>
14175 Validate_Access_Type_Instance;
14177 when N_Access_Function_Definition
14178 | N_Access_Procedure_Definition
14180 Validate_Access_Subprogram_Instance;
14182 when N_Record_Definition =>
14183 Validate_Interface_Type_Instance;
14185 when N_Derived_Type_Definition =>
14186 Validate_Derived_Interface_Type_Instance;
14189 raise Program_Error;
14193 Subt := New_Copy (Gen_T);
14195 -- Use adjusted sloc of subtype name as the location for other nodes in
14196 -- the subtype declaration.
14198 Loc := Sloc (Subt);
14201 Make_Subtype_Declaration (Loc,
14202 Defining_Identifier => Subt,
14203 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14205 -- Record whether the actual is private at this point, so that
14206 -- Check_Generic_Actuals can restore its proper view before the
14207 -- semantic analysis of the instance.
14209 if Is_Private_Type (Act_T) then
14210 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14213 -- In Ada 2012 the actual may be a limited view. Indicate that
14214 -- the local subtype must be treated as such.
14216 if From_Limited_With (Act_T) then
14217 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14218 Set_From_Limited_With (Subt);
14221 Decl_Nodes := New_List (Decl_Node);
14223 -- Flag actual derived types so their elaboration produces the
14224 -- appropriate renamings for the primitive operations of the ancestor.
14225 -- Flag actual for formal private types as well, to determine whether
14226 -- operations in the private part may override inherited operations.
14227 -- If the formal has an interface list, the ancestor is not the
14228 -- parent, but the analyzed formal that includes the interface
14229 -- operations of all its progenitors.
14231 -- Same treatment for formal private types, so we can check whether the
14232 -- type is tagged limited when validating derivations in the private
14233 -- part. (See AI05-096).
14235 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14236 if Present (Interface_List (Def)) then
14237 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14239 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14242 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14243 | N_Formal_Incomplete_Type_Definition
14245 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14248 -- If the actual is a synchronized type that implements an interface,
14249 -- the primitive operations are attached to the corresponding record,
14250 -- and we have to treat it as an additional generic actual, so that its
14251 -- primitive operations become visible in the instance. The task or
14252 -- protected type itself does not carry primitive operations.
14254 if Is_Concurrent_Type (Act_T)
14255 and then Is_Tagged_Type (Act_T)
14256 and then Present (Corresponding_Record_Type (Act_T))
14257 and then Present (Ancestor)
14258 and then Is_Interface (Ancestor)
14261 Corr_Rec : constant Entity_Id :=
14262 Corresponding_Record_Type (Act_T);
14263 New_Corr : Entity_Id;
14264 Corr_Decl : Node_Id;
14267 New_Corr := Make_Temporary (Loc, 'S');
14269 Make_Subtype_Declaration (Loc,
14270 Defining_Identifier => New_Corr,
14271 Subtype_Indication =>
14272 New_Occurrence_Of (Corr_Rec, Loc));
14273 Append_To (Decl_Nodes, Corr_Decl);
14275 if Ekind (Act_T) = E_Task_Type then
14276 Mutate_Ekind (Subt, E_Task_Subtype);
14278 Mutate_Ekind (Subt, E_Protected_Subtype);
14281 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14282 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14283 Set_Generic_Parent_Type (Decl_Node, Empty);
14287 -- For a floating-point type, capture dimension info if any, because
14288 -- the generated subtype declaration does not come from source and
14289 -- will not process dimensions.
14291 if Is_Floating_Point_Type (Act_T) then
14292 Copy_Dimensions (Act_T, Subt);
14296 end Instantiate_Type;
14298 ---------------------
14299 -- Is_In_Main_Unit --
14300 ---------------------
14302 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14303 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14304 Current_Unit : Node_Id;
14307 if Unum = Main_Unit then
14310 -- If the current unit is a subunit then it is either the main unit or
14311 -- is being compiled as part of the main unit.
14313 elsif Nkind (N) = N_Compilation_Unit then
14314 return Nkind (Unit (N)) = N_Subunit;
14317 Current_Unit := Parent (N);
14318 while Present (Current_Unit)
14319 and then Nkind (Current_Unit) /= N_Compilation_Unit
14321 Current_Unit := Parent (Current_Unit);
14324 -- The instantiation node is in the main unit, or else the current node
14325 -- (perhaps as the result of nested instantiations) is in the main unit,
14326 -- or in the declaration of the main unit, which in this last case must
14330 Current_Unit = Cunit (Main_Unit)
14331 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14332 or else (Present (Current_Unit)
14333 and then Present (Library_Unit (Current_Unit))
14334 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14335 end Is_In_Main_Unit;
14337 ----------------------------
14338 -- Load_Parent_Of_Generic --
14339 ----------------------------
14341 procedure Load_Parent_Of_Generic
14344 Body_Optional : Boolean := False)
14346 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14347 Saved_Style_Check : constant Boolean := Style_Check;
14348 Saved_Warnings : constant Warning_Record := Save_Warnings;
14349 True_Parent : Node_Id;
14350 Inst_Node : Node_Id;
14352 Previous_Instances : constant Elist_Id := New_Elmt_List;
14354 procedure Collect_Previous_Instances (Decls : List_Id);
14355 -- Collect all instantiations in the given list of declarations, that
14356 -- precede the generic that we need to load. If the bodies of these
14357 -- instantiations are available, we must analyze them, to ensure that
14358 -- the public symbols generated are the same when the unit is compiled
14359 -- to generate code, and when it is compiled in the context of a unit
14360 -- that needs a particular nested instance. This process is applied to
14361 -- both package and subprogram instances.
14363 --------------------------------
14364 -- Collect_Previous_Instances --
14365 --------------------------------
14367 procedure Collect_Previous_Instances (Decls : List_Id) is
14371 Decl := First (Decls);
14372 while Present (Decl) loop
14373 if Sloc (Decl) >= Sloc (Inst_Node) then
14376 -- If Decl is an instantiation, then record it as requiring
14377 -- instantiation of the corresponding body, except if it is an
14378 -- abbreviated instantiation generated internally for conformance
14379 -- checking purposes only for the case of a formal package
14380 -- declared without a box (see Instantiate_Formal_Package). Such
14381 -- an instantiation does not generate any code (the actual code
14382 -- comes from actual) and thus does not need to be analyzed here.
14383 -- If the instantiation appears with a generic package body it is
14384 -- not analyzed here either.
14386 elsif Nkind (Decl) = N_Package_Instantiation
14387 and then not Is_Internal (Defining_Entity (Decl))
14389 Append_Elmt (Decl, Previous_Instances);
14391 -- For a subprogram instantiation, omit instantiations intrinsic
14392 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14394 elsif Nkind (Decl) in N_Function_Instantiation
14395 | N_Procedure_Instantiation
14396 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14398 Append_Elmt (Decl, Previous_Instances);
14400 elsif Nkind (Decl) = N_Package_Declaration then
14401 Collect_Previous_Instances
14402 (Visible_Declarations (Specification (Decl)));
14403 Collect_Previous_Instances
14404 (Private_Declarations (Specification (Decl)));
14406 -- Previous non-generic bodies may contain instances as well
14408 elsif Nkind (Decl) = N_Package_Body
14409 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14411 Collect_Previous_Instances (Declarations (Decl));
14413 elsif Nkind (Decl) = N_Subprogram_Body
14414 and then not Acts_As_Spec (Decl)
14415 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14417 Collect_Previous_Instances (Declarations (Decl));
14422 end Collect_Previous_Instances;
14424 -- Start of processing for Load_Parent_Of_Generic
14427 if not In_Same_Source_Unit (N, Spec)
14428 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14429 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14430 and then not Is_In_Main_Unit (Spec))
14432 -- Find body of parent of spec, and analyze it. A special case arises
14433 -- when the parent is an instantiation, that is to say when we are
14434 -- currently instantiating a nested generic. In that case, there is
14435 -- no separate file for the body of the enclosing instance. Instead,
14436 -- the enclosing body must be instantiated as if it were a pending
14437 -- instantiation, in order to produce the body for the nested generic
14438 -- we require now. Note that in that case the generic may be defined
14439 -- in a package body, the instance defined in the same package body,
14440 -- and the original enclosing body may not be in the main unit.
14442 Inst_Node := Empty;
14444 True_Parent := Parent (Spec);
14445 while Present (True_Parent)
14446 and then Nkind (True_Parent) /= N_Compilation_Unit
14448 if Nkind (True_Parent) = N_Package_Declaration
14450 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14452 -- Parent is a compilation unit that is an instantiation, and
14453 -- instantiation node has been replaced with package decl.
14455 Inst_Node := Original_Node (True_Parent);
14458 elsif Nkind (True_Parent) = N_Package_Declaration
14459 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14461 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14463 -- Parent is a compilation unit that is an instantiation, but
14464 -- instantiation node has not been replaced with package decl.
14466 Inst_Node := Unit (Parent (True_Parent));
14469 elsif Nkind (True_Parent) = N_Package_Declaration
14470 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14471 and then Present (Generic_Parent (Specification (True_Parent)))
14473 -- Parent is an instantiation within another specification.
14474 -- Declaration for instance has been inserted before original
14475 -- instantiation node. A direct link would be preferable?
14477 Inst_Node := Next (True_Parent);
14478 while Present (Inst_Node)
14479 and then Nkind (Inst_Node) /= N_Package_Instantiation
14484 -- If the instance appears within a generic, and the generic
14485 -- unit is defined within a formal package of the enclosing
14486 -- generic, there is no generic body available, and none
14487 -- needed. A more precise test should be used ???
14489 if No (Inst_Node) then
14495 -- If an ancestor of the generic comes from a formal package
14496 -- there is no source for the ancestor body. This is detected
14497 -- by examining the scope of the ancestor and its declaration.
14498 -- The body, if any is needed, will be available when the
14499 -- current unit (containing a formal package) is instantiated.
14501 elsif Nkind (True_Parent) = N_Package_Specification
14502 and then Present (Generic_Parent (True_Parent))
14504 (Original_Node (Unit_Declaration_Node
14505 (Scope (Generic_Parent (True_Parent)))))
14506 = N_Formal_Package_Declaration
14511 True_Parent := Parent (True_Parent);
14515 -- Case where we are currently instantiating a nested generic
14517 if Present (Inst_Node) then
14518 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14520 -- Instantiation node and declaration of instantiated package
14521 -- were exchanged when only the declaration was needed.
14522 -- Restore instantiation node before proceeding with body.
14524 Set_Unit (Parent (True_Parent), Inst_Node);
14527 -- Now complete instantiation of enclosing body, if it appears in
14528 -- some other unit. If it appears in the current unit, the body
14529 -- will have been instantiated already.
14531 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14533 -- We need to determine the expander mode to instantiate the
14534 -- enclosing body. Because the generic body we need may use
14535 -- global entities declared in the enclosing package (including
14536 -- aggregates) it is in general necessary to compile this body
14537 -- with expansion enabled, except if we are within a generic
14538 -- package, in which case the usual generic rule applies.
14541 Exp_Status : Boolean := True;
14545 -- Loop through scopes looking for generic package
14547 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14548 while Present (Scop)
14549 and then Scop /= Standard_Standard
14551 if Ekind (Scop) = E_Generic_Package then
14552 Exp_Status := False;
14556 Scop := Scope (Scop);
14559 -- Collect previous instantiations in the unit that contains
14560 -- the desired generic.
14562 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14563 and then not Body_Optional
14567 Info : Pending_Body_Info;
14571 Par := Parent (Inst_Node);
14572 while Present (Par) loop
14573 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14574 Par := Parent (Par);
14577 pragma Assert (Present (Par));
14579 if Nkind (Par) = N_Package_Body then
14580 Collect_Previous_Instances (Declarations (Par));
14582 elsif Nkind (Par) = N_Package_Declaration then
14583 Collect_Previous_Instances
14584 (Visible_Declarations (Specification (Par)));
14585 Collect_Previous_Instances
14586 (Private_Declarations (Specification (Par)));
14589 -- Enclosing unit is a subprogram body. In this
14590 -- case all instance bodies are processed in order
14591 -- and there is no need to collect them separately.
14596 Decl := First_Elmt (Previous_Instances);
14597 while Present (Decl) loop
14600 Instance_Spec (Node (Decl)),
14601 Config_Switches => Save_Config_Switches,
14602 Current_Sem_Unit =>
14603 Get_Code_Unit (Sloc (Node (Decl))),
14604 Expander_Status => Exp_Status,
14605 Inst_Node => Node (Decl),
14606 Local_Suppress_Stack_Top =>
14607 Local_Suppress_Stack_Top,
14608 Scope_Suppress => Scope_Suppress,
14609 Warnings => Save_Warnings);
14611 -- Package instance
14613 if Nkind (Node (Decl)) = N_Package_Instantiation
14615 Instantiate_Package_Body
14616 (Info, Body_Optional => True);
14618 -- Subprogram instance
14621 -- The instance_spec is in the wrapper package,
14622 -- usually followed by its local renaming
14623 -- declaration. See Build_Subprogram_Renaming
14624 -- for details. If the instance carries aspects,
14625 -- these result in the corresponding pragmas,
14626 -- inserted after the subprogram declaration.
14627 -- They must be skipped as well when retrieving
14628 -- the desired spec. Some of them may have been
14629 -- rewritten as null statements.
14630 -- A direct link would be more robust ???
14634 (Last (Visible_Declarations
14635 (Specification (Info.Act_Decl))));
14637 while Nkind (Decl) in
14640 N_Subprogram_Renaming_Declaration
14642 Decl := Prev (Decl);
14645 Info.Act_Decl := Decl;
14648 Instantiate_Subprogram_Body
14649 (Info, Body_Optional => True);
14657 Instantiate_Package_Body
14659 ((Act_Decl => True_Parent,
14660 Config_Switches => Save_Config_Switches,
14661 Current_Sem_Unit =>
14662 Get_Code_Unit (Sloc (Inst_Node)),
14663 Expander_Status => Exp_Status,
14664 Inst_Node => Inst_Node,
14665 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14666 Scope_Suppress => Scope_Suppress,
14667 Warnings => Save_Warnings)),
14668 Body_Optional => Body_Optional);
14672 -- Case where we are not instantiating a nested generic
14675 Opt.Style_Check := False;
14676 Expander_Mode_Save_And_Set (True);
14677 Load_Needed_Body (Comp_Unit, OK);
14678 Opt.Style_Check := Saved_Style_Check;
14679 Restore_Warnings (Saved_Warnings);
14680 Expander_Mode_Restore;
14683 and then Unit_Requires_Body (Defining_Entity (Spec))
14684 and then not Body_Optional
14687 Bname : constant Unit_Name_Type :=
14688 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14691 -- In CodePeer mode, the missing body may make the analysis
14692 -- incomplete, but we do not treat it as fatal.
14694 if CodePeer_Mode then
14698 Error_Msg_Unit_1 := Bname;
14699 Error_Msg_N ("this instantiation requires$!", N);
14700 Error_Msg_File_1 :=
14701 Get_File_Name (Bname, Subunit => False);
14702 Error_Msg_N ("\but file{ was not found!", N);
14703 raise Unrecoverable_Error;
14710 -- If loading parent of the generic caused an instantiation circularity,
14711 -- we abandon compilation at this point, because otherwise in some cases
14712 -- we get into trouble with infinite recursions after this point.
14714 if Circularity_Detected then
14715 raise Unrecoverable_Error;
14717 end Load_Parent_Of_Generic;
14719 ---------------------------------
14720 -- Map_Formal_Package_Entities --
14721 ---------------------------------
14723 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14728 Set_Instance_Of (Form, Act);
14730 -- Traverse formal and actual package to map the corresponding entities.
14731 -- We skip over internal entities that may be generated during semantic
14732 -- analysis, and find the matching entities by name, given that they
14733 -- must appear in the same order.
14735 E1 := First_Entity (Form);
14736 E2 := First_Entity (Act);
14737 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14738 -- Could this test be a single condition??? Seems like it could, and
14739 -- isn't FPE (Form) a constant anyway???
14741 if not Is_Internal (E1)
14742 and then Present (Parent (E1))
14743 and then not Is_Class_Wide_Type (E1)
14744 and then not Is_Internal_Name (Chars (E1))
14746 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14753 Set_Instance_Of (E1, E2);
14755 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14756 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14759 if Is_Constrained (E1) then
14760 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14763 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14764 Map_Formal_Package_Entities (E1, E2);
14771 end Map_Formal_Package_Entities;
14773 -----------------------
14774 -- Move_Freeze_Nodes --
14775 -----------------------
14777 procedure Move_Freeze_Nodes
14778 (Out_Of : Entity_Id;
14783 Next_Decl : Node_Id;
14784 Next_Node : Node_Id := After;
14787 function Is_Outer_Type (T : Entity_Id) return Boolean;
14788 -- Check whether entity is declared in a scope external to that of the
14791 -------------------
14792 -- Is_Outer_Type --
14793 -------------------
14795 function Is_Outer_Type (T : Entity_Id) return Boolean is
14796 Scop : Entity_Id := Scope (T);
14799 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14803 while Scop /= Standard_Standard loop
14804 if Scop = Out_Of then
14807 Scop := Scope (Scop);
14815 -- Start of processing for Move_Freeze_Nodes
14822 -- First remove the freeze nodes that may appear before all other
14826 while Present (Decl)
14827 and then Nkind (Decl) = N_Freeze_Entity
14828 and then Is_Outer_Type (Entity (Decl))
14830 Decl := Remove_Head (L);
14831 Insert_After (Next_Node, Decl);
14832 Set_Analyzed (Decl, False);
14837 -- Next scan the list of declarations and remove each freeze node that
14838 -- appears ahead of the current node.
14840 while Present (Decl) loop
14841 while Present (Next (Decl))
14842 and then Nkind (Next (Decl)) = N_Freeze_Entity
14843 and then Is_Outer_Type (Entity (Next (Decl)))
14845 Next_Decl := Remove_Next (Decl);
14846 Insert_After (Next_Node, Next_Decl);
14847 Set_Analyzed (Next_Decl, False);
14848 Next_Node := Next_Decl;
14851 -- If the declaration is a nested package or concurrent type, then
14852 -- recurse. Nested generic packages will have been processed from the
14855 case Nkind (Decl) is
14856 when N_Package_Declaration =>
14857 Spec := Specification (Decl);
14859 when N_Task_Type_Declaration =>
14860 Spec := Task_Definition (Decl);
14862 when N_Protected_Type_Declaration =>
14863 Spec := Protected_Definition (Decl);
14869 if Present (Spec) then
14870 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14871 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14876 end Move_Freeze_Nodes;
14882 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14884 return Generic_Renamings.Table (E).Next_In_HTable;
14887 ------------------------
14888 -- Preanalyze_Actuals --
14889 ------------------------
14891 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14892 procedure Perform_Appropriate_Analysis (N : Node_Id);
14893 -- Determine if the actuals we are analyzing come from a generic
14894 -- instantiation that is a library unit and dispatch accordingly.
14896 ----------------------------------
14897 -- Perform_Appropriate_Analysis --
14898 ----------------------------------
14900 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14902 -- When we have a library instantiation we cannot allow any expansion
14903 -- to occur, since there may be no place to put it. Instead, in that
14904 -- case we perform a preanalysis of the actual.
14906 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14911 end Perform_Appropriate_Analysis;
14915 Errs : constant Nat := Serious_Errors_Detected;
14920 Cur : Entity_Id := Empty;
14921 -- Current homograph of the instance name
14923 Vis : Boolean := False;
14924 -- Saved visibility status of the current homograph
14926 -- Start of processing for Preanalyze_Actuals
14929 Assoc := First (Generic_Associations (N));
14931 -- If the instance is a child unit, its name may hide an outer homonym,
14932 -- so make it invisible to perform name resolution on the actuals.
14934 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14936 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14938 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14940 if Is_Compilation_Unit (Cur) then
14941 Vis := Is_Immediately_Visible (Cur);
14942 Set_Is_Immediately_Visible (Cur, False);
14948 while Present (Assoc) loop
14949 if Nkind (Assoc) /= N_Others_Choice then
14950 Act := Explicit_Generic_Actual_Parameter (Assoc);
14952 -- Within a nested instantiation, a defaulted actual is an empty
14953 -- association, so nothing to analyze. If the subprogram actual
14954 -- is an attribute, analyze prefix only, because actual is not a
14955 -- complete attribute reference.
14957 -- If actual is an allocator, analyze expression only. The full
14958 -- analysis can generate code, and if instance is a compilation
14959 -- unit we have to wait until the package instance is installed
14960 -- to have a proper place to insert this code.
14962 -- String literals may be operators, but at this point we do not
14963 -- know whether the actual is a formal subprogram or a string.
14968 elsif Nkind (Act) = N_Attribute_Reference then
14969 Perform_Appropriate_Analysis (Prefix (Act));
14971 elsif Nkind (Act) = N_Explicit_Dereference then
14972 Perform_Appropriate_Analysis (Prefix (Act));
14974 elsif Nkind (Act) = N_Allocator then
14976 Expr : constant Node_Id := Expression (Act);
14979 if Nkind (Expr) = N_Subtype_Indication then
14980 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14982 -- Analyze separately each discriminant constraint, when
14983 -- given with a named association.
14989 Constr := First (Constraints (Constraint (Expr)));
14990 while Present (Constr) loop
14991 if Nkind (Constr) = N_Discriminant_Association then
14992 Perform_Appropriate_Analysis
14993 (Expression (Constr));
14995 Perform_Appropriate_Analysis (Constr);
15003 Perform_Appropriate_Analysis (Expr);
15007 elsif Nkind (Act) /= N_Operator_Symbol then
15008 Perform_Appropriate_Analysis (Act);
15010 -- Within a package instance, mark actuals that are limited
15011 -- views, so their use can be moved to the body of the
15014 if Is_Entity_Name (Act)
15015 and then Is_Type (Entity (Act))
15016 and then From_Limited_With (Entity (Act))
15017 and then Present (Inst)
15019 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15023 if Errs /= Serious_Errors_Detected then
15025 -- Do a minimal analysis of the generic, to prevent spurious
15026 -- warnings complaining about the generic being unreferenced,
15027 -- before abandoning the instantiation.
15029 Perform_Appropriate_Analysis (Name (N));
15031 if Is_Entity_Name (Name (N))
15032 and then Etype (Name (N)) /= Any_Type
15034 Generate_Reference (Entity (Name (N)), Name (N));
15035 Set_Is_Instantiated (Entity (Name (N)));
15038 if Present (Cur) then
15040 -- For the case of a child instance hiding an outer homonym,
15041 -- provide additional warning which might explain the error.
15043 Set_Is_Immediately_Visible (Cur, Vis);
15045 ("& hides outer unit with the same name??",
15046 N, Defining_Unit_Name (N));
15049 Abandon_Instantiation (Act);
15056 if Present (Cur) then
15057 Set_Is_Immediately_Visible (Cur, Vis);
15059 end Preanalyze_Actuals;
15061 -------------------------------
15062 -- Provide_Completing_Bodies --
15063 -------------------------------
15065 procedure Provide_Completing_Bodies (N : Node_Id) is
15066 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15067 -- Generate the completing body for subprogram declaration Subp_Decl
15069 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15070 -- Generating completing bodies for all subprograms found in declarative
15073 ---------------------------
15074 -- Build_Completing_Body --
15075 ---------------------------
15077 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15078 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15079 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15083 -- Nothing to do if the subprogram already has a completing body
15085 if Present (Corresponding_Body (Subp_Decl)) then
15088 -- Mark the function as having a valid return statement even though
15089 -- the body contains a single raise statement.
15091 elsif Ekind (Subp_Id) = E_Function then
15092 Set_Return_Present (Subp_Id);
15095 -- Clone the specification to obtain new entities and reset the only
15098 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15099 Set_Generic_Parent (Spec, Empty);
15102 -- function Func ... return ... is
15104 -- procedure Proc ... is
15106 -- raise Program_Error with "access before elaboration";
15109 Insert_After_And_Analyze (Subp_Decl,
15110 Make_Subprogram_Body (Loc,
15111 Specification => Spec,
15112 Declarations => New_List,
15113 Handled_Statement_Sequence =>
15114 Make_Handled_Sequence_Of_Statements (Loc,
15115 Statements => New_List (
15116 Make_Raise_Program_Error (Loc,
15117 Reason => PE_Access_Before_Elaboration)))));
15118 end Build_Completing_Body;
15120 ----------------------------------
15121 -- Provide_Completing_Bodies_In --
15122 ----------------------------------
15124 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15128 if Present (Decls) then
15129 Decl := First (Decls);
15130 while Present (Decl) loop
15131 Provide_Completing_Bodies (Decl);
15135 end Provide_Completing_Bodies_In;
15141 -- Start of processing for Provide_Completing_Bodies
15144 if Nkind (N) = N_Package_Declaration then
15145 Spec := Specification (N);
15147 Push_Scope (Defining_Entity (N));
15148 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15149 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15152 elsif Nkind (N) = N_Subprogram_Declaration then
15153 Build_Completing_Body (N);
15155 end Provide_Completing_Bodies;
15157 -------------------
15158 -- Remove_Parent --
15159 -------------------
15161 procedure Remove_Parent (In_Body : Boolean := False) is
15162 S : Entity_Id := Current_Scope;
15163 -- S is the scope containing the instantiation just completed. The scope
15164 -- stack contains the parent instances of the instantiation, followed by
15173 -- After child instantiation is complete, remove from scope stack the
15174 -- extra copy of the current scope, and then remove parent instances.
15176 if not In_Body then
15179 while Current_Scope /= S loop
15180 P := Current_Scope;
15181 End_Package_Scope (Current_Scope);
15183 if In_Open_Scopes (P) then
15184 E := First_Entity (P);
15185 while Present (E) loop
15186 Set_Is_Immediately_Visible (E, True);
15190 -- If instantiation is declared in a block, it is the enclosing
15191 -- scope that might be a parent instance. Note that only one
15192 -- block can be involved, because the parent instances have
15193 -- been installed within it.
15195 if Ekind (P) = E_Block then
15196 Cur_P := Scope (P);
15201 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15202 -- We are within an instance of some sibling. Retain
15203 -- visibility of parent, for proper subsequent cleanup, and
15204 -- reinstall private declarations as well.
15206 Set_In_Private_Part (P);
15207 Install_Private_Declarations (P);
15210 -- If the ultimate parent is a top-level unit recorded in
15211 -- Instance_Parent_Unit, then reset its visibility to what it was
15212 -- before instantiation. (It's not clear what the purpose is of
15213 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15214 -- present before the ultimate parent test was added.???)
15216 elsif not In_Open_Scopes (Scope (P))
15217 or else (P = Instance_Parent_Unit
15218 and then not Parent_Unit_Visible)
15220 Set_Is_Immediately_Visible (P, False);
15222 -- If the current scope is itself an instantiation of a generic
15223 -- nested within P, and we are in the private part of body of this
15224 -- instantiation, restore the full views of P, that were removed
15225 -- in End_Package_Scope above. This obscure case can occur when a
15226 -- subunit of a generic contains an instance of a child unit of
15227 -- its generic parent unit.
15229 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15231 Par : constant Entity_Id :=
15232 Generic_Parent (Package_Specification (S));
15235 and then P = Scope (Par)
15236 and then (In_Package_Body (S) or else In_Private_Part (S))
15238 Set_In_Private_Part (P);
15239 Install_Private_Declarations (P);
15245 -- Reset visibility of entities in the enclosing scope
15247 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15249 Hidden := First_Elmt (Hidden_Entities);
15250 while Present (Hidden) loop
15251 Set_Is_Immediately_Visible (Node (Hidden), True);
15252 Next_Elmt (Hidden);
15256 -- Each body is analyzed separately, and there is no context that
15257 -- needs preserving from one body instance to the next, so remove all
15258 -- parent scopes that have been installed.
15260 while Present (S) loop
15261 End_Package_Scope (S);
15262 Set_Is_Immediately_Visible (S, False);
15263 S := Current_Scope;
15264 exit when S = Standard_Standard;
15273 procedure Restore_Env is
15274 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15277 if No (Current_Instantiated_Parent.Act_Id) then
15278 -- Restore environment after subprogram inlining
15280 Restore_Private_Views (Empty);
15283 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15284 Exchanged_Views := Saved.Exchanged_Views;
15285 Hidden_Entities := Saved.Hidden_Entities;
15286 Current_Sem_Unit := Saved.Current_Sem_Unit;
15287 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15288 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15290 Restore_Config_Switches (Saved.Switches);
15292 Instance_Envs.Decrement_Last;
15295 ---------------------------
15296 -- Restore_Private_Views --
15297 ---------------------------
15299 procedure Restore_Private_Views
15300 (Pack_Id : Entity_Id;
15301 Is_Package : Boolean := True)
15306 Dep_Elmt : Elmt_Id;
15309 procedure Restore_Nested_Formal (Formal : Entity_Id);
15310 -- Hide the generic formals of formal packages declared with box which
15311 -- were reachable in the current instantiation.
15313 ---------------------------
15314 -- Restore_Nested_Formal --
15315 ---------------------------
15317 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15321 if Present (Renamed_Object (Formal))
15322 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15326 elsif Present (Associated_Formal_Package (Formal)) then
15327 Ent := First_Entity (Formal);
15328 while Present (Ent) loop
15329 exit when Ekind (Ent) = E_Package
15330 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15332 Set_Is_Hidden (Ent);
15333 Set_Is_Potentially_Use_Visible (Ent, False);
15335 -- If package, then recurse
15337 if Ekind (Ent) = E_Package then
15338 Restore_Nested_Formal (Ent);
15344 end Restore_Nested_Formal;
15346 -- Start of processing for Restore_Private_Views
15349 M := First_Elmt (Exchanged_Views);
15350 while Present (M) loop
15353 -- Subtypes of types whose views have been exchanged, and that are
15354 -- defined within the instance, were not on the Private_Dependents
15355 -- list on entry to the instance, so they have to be exchanged
15356 -- explicitly now, in order to remain consistent with the view of the
15359 if Ekind (Typ) in E_Private_Type
15360 | E_Limited_Private_Type
15361 | E_Record_Type_With_Private
15363 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15364 while Present (Dep_Elmt) loop
15365 Dep_Typ := Node (Dep_Elmt);
15367 if Scope (Dep_Typ) = Pack_Id
15368 and then Present (Full_View (Dep_Typ))
15370 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15371 Exchange_Declarations (Dep_Typ);
15374 Next_Elmt (Dep_Elmt);
15378 Exchange_Declarations (Node (M));
15382 if No (Pack_Id) then
15386 -- Make the generic formal parameters private, and make the formal types
15387 -- into subtypes of the actuals again.
15389 E := First_Entity (Pack_Id);
15390 while Present (E) loop
15391 Set_Is_Hidden (E, True);
15394 and then Nkind (Parent (E)) = N_Subtype_Declaration
15396 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15397 -- as it is needed to identify the subtype with the type it
15398 -- renames, when there are conversions between access types
15401 if GNATprove_Mode then
15404 -- If the actual for E is itself a generic actual type from
15405 -- an enclosing instance, E is still a generic actual type
15406 -- outside of the current instance. This matter when resolving
15407 -- an overloaded call that may be ambiguous in the enclosing
15408 -- instance, when two of its actuals coincide.
15410 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15411 and then Is_Generic_Actual_Type
15412 (Entity (Subtype_Indication (Parent (E))))
15416 Set_Is_Generic_Actual_Type (E, False);
15418 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15419 -- flag also on the Full_View if the type is private, since it
15420 -- was set also on this Full_View. However, this flag is relied
15421 -- upon by Covers to spot "types exported from instantiations"
15422 -- which are implicit Full_Views built for instantiations made
15423 -- on private types and we get type mismatches if we do it when
15424 -- the block exchanging the declarations below triggers ???
15426 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15427 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15431 -- An unusual case of aliasing: the actual may also be directly
15432 -- visible in the generic, and be private there, while it is fully
15433 -- visible in the context of the instance. The internal subtype
15434 -- is private in the instance but has full visibility like its
15435 -- parent in the enclosing scope. This enforces the invariant that
15436 -- the privacy status of all private dependents of a type coincide
15437 -- with that of the parent type. This can only happen when a
15438 -- generic child unit is instantiated within a sibling.
15440 if Is_Private_Type (E)
15441 and then not Is_Private_Type (Etype (E))
15443 Exchange_Declarations (E);
15446 elsif Ekind (E) = E_Package then
15448 -- The end of the renaming list is the renaming of the generic
15449 -- package itself. If the instance is a subprogram, all entities
15450 -- in the corresponding package are renamings. If this entity is
15451 -- a formal package, make its own formals private as well. The
15452 -- actual in this case is itself the renaming of an instantiation.
15453 -- If the entity is not a package renaming, it is the entity
15454 -- created to validate formal package actuals: ignore it.
15456 -- If the actual is itself a formal package for the enclosing
15457 -- generic, or the actual for such a formal package, it remains
15458 -- visible on exit from the instance, and therefore nothing needs
15459 -- to be done either, except to keep it accessible.
15461 if Is_Package and then Renamed_Object (E) = Pack_Id then
15464 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15468 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15470 Set_Is_Hidden (E, False);
15474 Act_P : constant Entity_Id := Renamed_Object (E);
15478 Id := First_Entity (Act_P);
15480 and then Id /= First_Private_Entity (Act_P)
15482 exit when Ekind (Id) = E_Package
15483 and then Renamed_Object (Id) = Act_P;
15485 Set_Is_Hidden (Id, True);
15486 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15488 if Ekind (Id) = E_Package then
15489 Restore_Nested_Formal (Id);
15500 end Restore_Private_Views;
15507 (Gen_Unit : Entity_Id;
15508 Act_Unit : Entity_Id)
15512 Set_Instance_Env (Gen_Unit, Act_Unit);
15515 ----------------------------
15516 -- Save_Global_References --
15517 ----------------------------
15519 procedure Save_Global_References (Templ : Node_Id) is
15521 -- ??? it is horrible to use global variables in highly recursive code
15524 -- The entity of the current associated node
15526 Gen_Scope : Entity_Id;
15527 -- The scope of the generic for which references are being saved
15530 -- The current associated node
15532 function Is_Global (E : Entity_Id) return Boolean;
15533 -- Check whether entity is defined outside of generic unit. Examine the
15534 -- scope of an entity, and the scope of the scope, etc, until we find
15535 -- either Standard, in which case the entity is global, or the generic
15536 -- unit itself, which indicates that the entity is local. If the entity
15537 -- is the generic unit itself, as in the case of a recursive call, or
15538 -- the enclosing generic unit, if different from the current scope, then
15539 -- it is local as well, because it will be replaced at the point of
15540 -- instantiation. On the other hand, if it is a reference to a child
15541 -- unit of a common ancestor, which appears in an instantiation, it is
15542 -- global because it is used to denote a specific compilation unit at
15543 -- the time the instantiations will be analyzed.
15545 procedure Qualify_Universal_Operands
15547 Func_Call : Node_Id);
15548 -- Op denotes a binary or unary operator in generic template Templ. Node
15549 -- Func_Call is the function call alternative of the operator within the
15550 -- the analyzed copy of the template. Change each operand which yields a
15551 -- universal type by wrapping it into a qualified expression
15553 -- Actual_Typ'(Operand)
15555 -- where Actual_Typ is the type of corresponding actual parameter of
15556 -- Operand in Func_Call.
15558 procedure Reset_Entity (N : Node_Id);
15559 -- Save semantic information on global entity so that it is not resolved
15560 -- again at instantiation time.
15562 procedure Save_Entity_Descendants (N : Node_Id);
15563 -- Apply Save_Global_References to the two syntactic descendants of
15564 -- non-terminal nodes that carry an Associated_Node and are processed
15565 -- through Reset_Entity. Once the global entity (if any) has been
15566 -- captured together with its type, only two syntactic descendants need
15567 -- to be traversed to complete the processing of the tree rooted at N.
15568 -- This applies to Selected_Components, Expanded_Names, and to Operator
15569 -- nodes. N can also be a character literal, identifier, or operator
15570 -- symbol node, but the call has no effect in these cases.
15572 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15573 -- Default actuals in nested instances must be handled specially
15574 -- because there is no link to them from the original tree. When an
15575 -- actual subprogram is given by a default, we add an explicit generic
15576 -- association for it in the instantiation node. When we save the
15577 -- global references on the name of the instance, we recover the list
15578 -- of generic associations, and add an explicit one to the original
15579 -- generic tree, through which a global actual can be preserved.
15580 -- Similarly, if a child unit is instantiated within a sibling, in the
15581 -- context of the parent, we must preserve the identifier of the parent
15582 -- so that it can be properly resolved in a subsequent instantiation.
15584 procedure Save_Global_Descendant (D : Union_Id);
15585 -- Apply Save_References recursively to the descendants of node D
15587 procedure Save_References (N : Node_Id);
15588 -- This is the recursive procedure that does the work, once the
15589 -- enclosing generic scope has been established.
15595 function Is_Global (E : Entity_Id) return Boolean is
15598 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15599 -- Determine whether the parent node of a reference to a child unit
15600 -- denotes an instantiation or a formal package, in which case the
15601 -- reference to the child unit is global, even if it appears within
15602 -- the current scope (e.g. when the instance appears within the body
15603 -- of an ancestor).
15605 ----------------------
15606 -- Is_Instance_Node --
15607 ----------------------
15609 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15611 return Nkind (Decl) in N_Generic_Instantiation
15613 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15614 end Is_Instance_Node;
15616 -- Start of processing for Is_Global
15619 if E = Gen_Scope then
15622 elsif E = Standard_Standard then
15625 -- E should be an entity, but it is not always
15627 elsif Nkind (E) not in N_Entity then -- ????
15630 elsif Is_Child_Unit (E)
15631 and then (Is_Instance_Node (Parent (N2))
15632 or else (Nkind (Parent (N2)) = N_Expanded_Name
15633 and then N2 = Selector_Name (Parent (N2))
15635 Is_Instance_Node (Parent (Parent (N2)))))
15641 while Se /= Gen_Scope loop
15642 if Se = Standard_Standard then
15653 --------------------------------
15654 -- Qualify_Universal_Operands --
15655 --------------------------------
15657 procedure Qualify_Universal_Operands
15659 Func_Call : Node_Id)
15661 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15662 -- Rewrite operand Opnd as a qualified expression of the form
15664 -- Actual_Typ'(Opnd)
15666 -- where Actual is the corresponding actual parameter of Opnd in
15667 -- function call Func_Call.
15669 function Qualify_Type
15671 Typ : Entity_Id) return Node_Id;
15672 -- Qualify type Typ by creating a selected component of the form
15674 -- Scope_Of_Typ.Typ
15676 ---------------------
15677 -- Qualify_Operand --
15678 ---------------------
15680 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15681 Loc : constant Source_Ptr := Sloc (Opnd);
15682 Typ : constant Entity_Id := Etype (Actual);
15687 -- Qualify the operand when it is of a universal type. Note that
15688 -- the template is unanalyzed and it is not possible to directly
15689 -- query the type. This transformation is not done when the type
15690 -- of the actual is internally generated because the type will be
15691 -- regenerated in the instance.
15693 if Yields_Universal_Type (Opnd)
15694 and then Comes_From_Source (Typ)
15695 and then not Is_Hidden (Typ)
15697 -- The type of the actual may be a global reference. Save this
15698 -- information by creating a reference to it.
15700 if Is_Global (Typ) then
15701 Mark := New_Occurrence_Of (Typ, Loc);
15703 -- Otherwise rely on resolution to find the proper type within
15707 Mark := Qualify_Type (Loc, Typ);
15711 Make_Qualified_Expression (Loc,
15712 Subtype_Mark => Mark,
15713 Expression => Relocate_Node (Opnd));
15715 -- Mark the qualification to distinguish it from other source
15716 -- constructs and signal the instantiation mechanism that this
15717 -- node requires special processing. See Copy_Generic_Node for
15720 Set_Is_Qualified_Universal_Literal (Qual);
15722 Rewrite (Opnd, Qual);
15724 end Qualify_Operand;
15730 function Qualify_Type
15732 Typ : Entity_Id) return Node_Id
15734 Scop : constant Entity_Id := Scope (Typ);
15738 Result := Make_Identifier (Loc, Chars (Typ));
15740 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15742 Make_Selected_Component (Loc,
15743 Prefix => Make_Identifier (Loc, Chars (Scop)),
15744 Selector_Name => Result);
15752 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15754 -- Start of processing for Qualify_Universal_Operands
15757 if Nkind (Op) in N_Binary_Op then
15758 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15759 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15761 elsif Nkind (Op) in N_Unary_Op then
15762 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15764 end Qualify_Universal_Operands;
15770 procedure Reset_Entity (N : Node_Id) is
15771 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15772 -- If the type of N2 is global to the generic unit, save the type in
15773 -- the generic node. Just as we perform name capture for explicit
15774 -- references within the generic, we must capture the global types
15775 -- of local entities because they may participate in resolution in
15778 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15779 -- Find the ultimate ancestor of the current unit. If it is not a
15780 -- generic unit, then the name of the current unit in the prefix of
15781 -- an expanded name must be replaced with its generic homonym to
15782 -- ensure that it will be properly resolved in an instance.
15784 ---------------------
15785 -- Set_Global_Type --
15786 ---------------------
15788 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15789 Typ : constant Entity_Id := Etype (N2);
15792 Set_Etype (N, Typ);
15794 -- If the entity of N is not the associated node, this is a
15795 -- nested generic and it has an associated node as well, whose
15796 -- type is already the full view (see below). Indicate that the
15797 -- original node has a private view.
15799 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15800 Set_Has_Private_View (N);
15803 -- If not a private type, nothing else to do
15805 if not Is_Private_Type (Typ) then
15808 -- If it is a derivation of a private type in a context where no
15809 -- full view is needed, nothing to do either.
15811 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15814 -- Otherwise mark the type for flipping and use the full view when
15818 Set_Has_Private_View (N);
15820 if Present (Full_View (Typ)) then
15821 Set_Etype (N2, Full_View (Typ));
15825 if Is_Floating_Point_Type (Typ)
15826 and then Has_Dimension_System (Typ)
15828 Copy_Dimensions (N2, N);
15830 end Set_Global_Type;
15836 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15841 while Is_Child_Unit (Par) loop
15842 Par := Scope (Par);
15848 -- Start of processing for Reset_Entity
15851 N2 := Get_Associated_Node (N);
15854 if Present (E) then
15856 -- If the node is an entry call to an entry in an enclosing task,
15857 -- it is rewritten as a selected component. No global entity to
15858 -- preserve in this case, since the expansion will be redone in
15861 if Nkind (E) not in N_Entity then
15862 Set_Associated_Node (N, Empty);
15863 Set_Etype (N, Empty);
15867 -- If the entity is an itype created as a subtype of an access
15868 -- type with a null exclusion restore source entity for proper
15869 -- visibility. The itype will be created anew in the instance.
15872 and then Ekind (E) = E_Access_Subtype
15873 and then Is_Entity_Name (N)
15874 and then Chars (Etype (E)) = Chars (N)
15877 Set_Entity (N2, E);
15881 if Is_Global (E) then
15882 Set_Global_Type (N, N2);
15884 elsif Nkind (N) = N_Op_Concat
15885 and then Is_Generic_Type (Etype (N2))
15886 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15888 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15889 and then Is_Intrinsic_Subprogram (E)
15893 -- Entity is local. Mark generic node as unresolved. Note that now
15894 -- it does not have an entity.
15897 Set_Associated_Node (N, Empty);
15898 Set_Etype (N, Empty);
15901 if Nkind (Parent (N)) in N_Generic_Instantiation
15902 and then N = Name (Parent (N))
15904 Save_Global_Defaults (Parent (N), Parent (N2));
15907 elsif Nkind (Parent (N)) = N_Selected_Component
15908 and then Nkind (Parent (N2)) = N_Expanded_Name
15910 -- In case of previous errors, the tree might be malformed
15912 if No (Entity (Parent (N2))) then
15915 elsif Is_Global (Entity (Parent (N2))) then
15916 Change_Selected_Component_To_Expanded_Name (Parent (N));
15917 Set_Associated_Node (Parent (N), Parent (N2));
15918 Set_Global_Type (Parent (N), Parent (N2));
15919 Save_Entity_Descendants (N);
15921 -- If this is a reference to the current generic entity, replace
15922 -- by the name of the generic homonym of the current package. This
15923 -- is because in an instantiation Par.P.Q will not resolve to the
15924 -- name of the instance, whose enclosing scope is not necessarily
15925 -- Par. We use the generic homonym rather that the name of the
15926 -- generic itself because it may be hidden by a local declaration.
15928 elsif In_Open_Scopes (Entity (Parent (N2)))
15930 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15932 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15933 Rewrite (Parent (N),
15934 Make_Identifier (Sloc (N),
15936 Chars (Generic_Homonym (Entity (Parent (N2))))));
15938 Rewrite (Parent (N),
15939 Make_Identifier (Sloc (N),
15940 Chars => Chars (Selector_Name (Parent (N2)))));
15944 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15945 and then Parent (N) = Name (Parent (Parent (N)))
15947 Save_Global_Defaults
15948 (Parent (Parent (N)), Parent (Parent (N2)));
15951 -- A selected component may denote a static constant that has been
15952 -- folded. If the static constant is global to the generic, capture
15953 -- its value. Otherwise the folding will happen in any instantiation.
15955 elsif Nkind (Parent (N)) = N_Selected_Component
15956 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15958 if Present (Entity (Original_Node (Parent (N2))))
15959 and then Is_Global (Entity (Original_Node (Parent (N2))))
15961 Rewrite (Parent (N), New_Copy (Parent (N2)));
15962 Set_Analyzed (Parent (N), False);
15965 -- A selected component may be transformed into a parameterless
15966 -- function call. If the called entity is global, rewrite the node
15967 -- appropriately, i.e. as an extended name for the global entity.
15969 elsif Nkind (Parent (N)) = N_Selected_Component
15970 and then Nkind (Parent (N2)) = N_Function_Call
15971 and then N = Selector_Name (Parent (N))
15973 if No (Parameter_Associations (Parent (N2))) then
15974 if Is_Global (Entity (Name (Parent (N2)))) then
15975 Change_Selected_Component_To_Expanded_Name (Parent (N));
15976 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15977 Set_Global_Type (Parent (N), Name (Parent (N2)));
15978 Save_Entity_Descendants (N);
15981 Set_Is_Prefixed_Call (Parent (N));
15982 Set_Associated_Node (N, Empty);
15983 Set_Etype (N, Empty);
15986 -- In Ada 2005, X.F may be a call to a primitive operation,
15987 -- rewritten as F (X). This rewriting will be done again in an
15988 -- instance, so keep the original node. Global entities will be
15989 -- captured as for other constructs. Indicate that this must
15990 -- resolve as a call, to prevent accidental overloading in the
15991 -- instance, if both a component and a primitive operation appear
15995 Set_Is_Prefixed_Call (Parent (N));
15998 -- Entity is local. Reset in generic unit, so that node is resolved
15999 -- anew at the point of instantiation.
16002 Set_Associated_Node (N, Empty);
16003 Set_Etype (N, Empty);
16007 -----------------------------
16008 -- Save_Entity_Descendants --
16009 -----------------------------
16011 procedure Save_Entity_Descendants (N : Node_Id) is
16014 when N_Binary_Op =>
16015 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16016 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16019 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16021 when N_Expanded_Name
16022 | N_Selected_Component
16024 Save_Global_Descendant (Union_Id (Prefix (N)));
16025 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16027 when N_Character_Literal
16029 | N_Operator_Symbol
16034 raise Program_Error;
16036 end Save_Entity_Descendants;
16038 --------------------------
16039 -- Save_Global_Defaults --
16040 --------------------------
16042 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16043 Loc : constant Source_Ptr := Sloc (N1);
16044 Assoc2 : constant List_Id := Generic_Associations (N2);
16045 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16052 Actual : Entity_Id;
16055 Assoc1 := Generic_Associations (N1);
16057 if Present (Assoc1) then
16058 Act1 := First (Assoc1);
16061 Set_Generic_Associations (N1, New_List);
16062 Assoc1 := Generic_Associations (N1);
16065 if Present (Assoc2) then
16066 Act2 := First (Assoc2);
16071 while Present (Act1) and then Present (Act2) loop
16076 -- Find the associations added for default subprograms
16078 if Present (Act2) then
16079 while Nkind (Act2) /= N_Generic_Association
16080 or else No (Entity (Selector_Name (Act2)))
16081 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16086 -- Add a similar association if the default is global. The
16087 -- renaming declaration for the actual has been analyzed, and
16088 -- its alias is the program it renames. Link the actual in the
16089 -- original generic tree with the node in the analyzed tree.
16091 while Present (Act2) loop
16092 Subp := Entity (Selector_Name (Act2));
16093 Def := Explicit_Generic_Actual_Parameter (Act2);
16095 -- Following test is defence against rubbish errors
16097 if No (Alias (Subp)) then
16101 -- Retrieve the resolved actual from the renaming declaration
16102 -- created for the instantiated formal.
16104 Actual := Entity (Name (Parent (Parent (Subp))));
16105 Set_Entity (Def, Actual);
16106 Set_Etype (Def, Etype (Actual));
16108 if Is_Global (Actual) then
16110 Make_Generic_Association (Loc,
16112 New_Occurrence_Of (Subp, Loc),
16113 Explicit_Generic_Actual_Parameter =>
16114 New_Occurrence_Of (Actual, Loc));
16116 Set_Associated_Node
16117 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16119 Append (Ndec, Assoc1);
16121 -- If there are other defaults, add a dummy association in case
16122 -- there are other defaulted formals with the same name.
16124 elsif Present (Next (Act2)) then
16126 Make_Generic_Association (Loc,
16128 New_Occurrence_Of (Subp, Loc),
16129 Explicit_Generic_Actual_Parameter => Empty);
16131 Append (Ndec, Assoc1);
16138 if Nkind (Name (N1)) = N_Identifier
16139 and then Is_Child_Unit (Gen_Id)
16140 and then Is_Global (Gen_Id)
16141 and then Is_Generic_Unit (Scope (Gen_Id))
16142 and then In_Open_Scopes (Scope (Gen_Id))
16144 -- This is an instantiation of a child unit within a sibling, so
16145 -- that the generic parent is in scope. An eventual instance must
16146 -- occur within the scope of an instance of the parent. Make name
16147 -- in instance into an expanded name, to preserve the identifier
16148 -- of the parent, so it can be resolved subsequently.
16150 Rewrite (Name (N2),
16151 Make_Expanded_Name (Loc,
16152 Chars => Chars (Gen_Id),
16153 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16154 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16155 Set_Entity (Name (N2), Gen_Id);
16157 Rewrite (Name (N1),
16158 Make_Expanded_Name (Loc,
16159 Chars => Chars (Gen_Id),
16160 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16161 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16163 Set_Associated_Node (Name (N1), Name (N2));
16164 Set_Associated_Node (Prefix (Name (N1)), Empty);
16165 Set_Associated_Node
16166 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16167 Set_Etype (Name (N1), Etype (Gen_Id));
16169 end Save_Global_Defaults;
16171 ----------------------------
16172 -- Save_Global_Descendant --
16173 ----------------------------
16175 procedure Save_Global_Descendant (D : Union_Id) is
16179 if D in Node_Range then
16180 if D = Union_Id (Empty) then
16183 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16184 Save_References (Node_Id (D));
16187 elsif D in List_Range then
16188 pragma Assert (D /= Union_Id (No_List));
16189 -- Because No_List = Empty, which is in Node_Range above
16191 N1 := First (List_Id (D));
16192 while Present (N1) loop
16193 Save_References (N1);
16197 -- Element list or other non-node field, nothing to do
16202 end Save_Global_Descendant;
16204 ---------------------
16205 -- Save_References --
16206 ---------------------
16208 -- This is the recursive procedure that does the work once the enclosing
16209 -- generic scope has been established. We have to treat specially a
16210 -- number of node rewritings that are required by semantic processing
16211 -- and which change the kind of nodes in the generic copy: typically
16212 -- constant-folding, replacing an operator node by a string literal, or
16213 -- a selected component by an expanded name. In each of those cases, the
16214 -- transformation is propagated to the generic unit.
16216 procedure Save_References (N : Node_Id) is
16217 Loc : constant Source_Ptr := Sloc (N);
16219 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16220 -- Determine whether arbitrary node Nod requires delayed capture of
16221 -- global references within its aspect specifications.
16223 procedure Save_References_In_Aggregate (N : Node_Id);
16224 -- Save all global references in [extension] aggregate node N
16226 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16227 -- Save all global references in a character literal or operator
16228 -- symbol denoted by N.
16230 procedure Save_References_In_Descendants (N : Node_Id);
16231 -- Save all global references in all descendants of node N
16233 procedure Save_References_In_Identifier (N : Node_Id);
16234 -- Save all global references in identifier node N
16236 procedure Save_References_In_Operator (N : Node_Id);
16237 -- Save all global references in operator node N
16239 procedure Save_References_In_Pragma (Prag : Node_Id);
16240 -- Save all global references found within the expression of pragma
16243 ---------------------------
16244 -- Requires_Delayed_Save --
16245 ---------------------------
16247 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16249 -- Generic packages and subprograms require delayed capture of
16250 -- global references within their aspects due to the timing of
16251 -- annotation analysis.
16253 if Nkind (Nod) in N_Generic_Package_Declaration
16254 | N_Generic_Subprogram_Declaration
16256 | N_Package_Body_Stub
16257 | N_Subprogram_Body
16258 | N_Subprogram_Body_Stub
16260 -- Since the capture of global references is done on the
16261 -- unanalyzed generic template, there is no information around
16262 -- to infer the context. Use the Associated_Entity linkages to
16263 -- peek into the analyzed generic copy and determine what the
16264 -- template corresponds to.
16266 if Nod = Templ then
16268 Is_Generic_Declaration_Or_Body
16269 (Unit_Declaration_Node
16270 (Associated_Entity (Defining_Entity (Nod))));
16272 -- Otherwise the generic unit being processed is not the top
16273 -- level template. It is safe to capture of global references
16274 -- within the generic unit because at this point the top level
16275 -- copy is fully analyzed.
16281 -- Otherwise capture the global references without interference
16286 end Requires_Delayed_Save;
16288 ----------------------------------
16289 -- Save_References_In_Aggregate --
16290 ----------------------------------
16292 procedure Save_References_In_Aggregate (N : Node_Id) is
16294 Qual : Node_Id := Empty;
16295 Typ : Entity_Id := Empty;
16298 N2 := Get_Associated_Node (N);
16300 if Present (N2) then
16303 -- In an instance within a generic, use the name of the actual
16304 -- and not the original generic parameter. If the actual is
16305 -- global in the current generic it must be preserved for its
16308 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16309 and then Present (Generic_Parent_Type (Parent (Typ)))
16311 Typ := Base_Type (Typ);
16312 Set_Etype (N2, Typ);
16316 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16317 Set_Associated_Node (N, Empty);
16319 -- If the aggregate is an actual in a call, it has been
16320 -- resolved in the current context, to some local type. The
16321 -- enclosing call may have been disambiguated by the aggregate,
16322 -- and this disambiguation might fail at instantiation time
16323 -- because the type to which the aggregate did resolve is not
16324 -- preserved. In order to preserve some of this information,
16325 -- wrap the aggregate in a qualified expression, using the id
16326 -- of its type. For further disambiguation we qualify the type
16327 -- name with its scope (if visible and not hidden by a local
16328 -- homograph) because both id's will have corresponding
16329 -- entities in an instance. This resolves most of the problems
16330 -- with missing type information on aggregates in instances.
16333 and then Nkind (N2) = Nkind (N)
16334 and then Nkind (Parent (N2)) in N_Subprogram_Call
16335 and then Present (Typ)
16336 and then Comes_From_Source (Typ)
16338 Nam := Make_Identifier (Loc, Chars (Typ));
16340 if Is_Immediately_Visible (Scope (Typ))
16342 (not In_Open_Scopes (Scope (Typ))
16343 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16346 Make_Selected_Component (Loc,
16348 Make_Identifier (Loc, Chars (Scope (Typ))),
16349 Selector_Name => Nam);
16353 Make_Qualified_Expression (Loc,
16354 Subtype_Mark => Nam,
16355 Expression => Relocate_Node (N));
16359 if Nkind (N) = N_Aggregate then
16360 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16362 elsif Nkind (N) = N_Extension_Aggregate then
16363 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16366 pragma Assert (False);
16369 Save_Global_Descendant (Union_Id (Expressions (N)));
16370 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16371 Save_Global_Descendant (Union_Id (Etype (N)));
16373 if Present (Qual) then
16376 end Save_References_In_Aggregate;
16378 ----------------------------------------------
16379 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16380 ----------------------------------------------
16382 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16384 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16387 elsif Nkind (N) = N_Operator_Symbol
16388 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16390 Change_Operator_Symbol_To_String_Literal (N);
16392 end Save_References_In_Char_Lit_Or_Op_Symbol;
16394 ------------------------------------
16395 -- Save_References_In_Descendants --
16396 ------------------------------------
16398 procedure Save_References_In_Descendants (N : Node_Id) is
16399 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16402 end Save_References_In_Descendants;
16404 -----------------------------------
16405 -- Save_References_In_Identifier --
16406 -----------------------------------
16408 procedure Save_References_In_Identifier (N : Node_Id) is
16410 -- The node did not undergo a transformation
16412 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16413 -- If this is a discriminant reference, always save it.
16414 -- It is used in the instance to find the corresponding
16415 -- discriminant positionally rather than by name.
16417 Set_Original_Discriminant
16418 (N, Original_Discriminant (Get_Associated_Node (N)));
16422 -- The analysis of the generic copy transformed the identifier
16423 -- into another construct. Propagate the changes to the template.
16426 N2 := Get_Associated_Node (N);
16428 -- The identifier denotes a call to a parameterless function.
16429 -- Mark the node as resolved when the function is external.
16431 if Nkind (N2) = N_Function_Call then
16432 E := Entity (Name (N2));
16434 if Present (E) and then Is_Global (E) then
16435 Set_Etype (N, Etype (N2));
16437 Set_Associated_Node (N, Empty);
16438 Set_Etype (N, Empty);
16441 -- The identifier denotes a named number that was constant
16442 -- folded. Preserve the original name for ASIS and undo the
16443 -- constant folding which will be repeated in the instance.
16444 -- Is this still needed???
16446 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16447 and then Is_Entity_Name (Original_Node (N2))
16449 Set_Associated_Node (N, Original_Node (N2));
16452 -- The identifier resolved to a string literal. Propagate this
16453 -- information to the generic template.
16455 elsif Nkind (N2) = N_String_Literal then
16456 Rewrite (N, New_Copy (N2));
16458 -- The identifier is rewritten as a dereference if it is the
16459 -- prefix of an implicit dereference. Preserve the original
16460 -- tree as the analysis of the instance will expand the node
16461 -- again, but preserve the resolved entity if it is global.
16463 elsif Nkind (N2) = N_Explicit_Dereference then
16464 if Is_Entity_Name (Prefix (N2))
16465 and then Present (Entity (Prefix (N2)))
16466 and then Is_Global (Entity (Prefix (N2)))
16468 Set_Associated_Node (N, Prefix (N2));
16470 elsif Nkind (Prefix (N2)) = N_Function_Call
16471 and then Present (Entity (Name (Prefix (N2))))
16472 and then Is_Global (Entity (Name (Prefix (N2))))
16475 Make_Explicit_Dereference (Loc,
16477 Make_Function_Call (Loc,
16480 (Entity (Name (Prefix (N2))), Loc))));
16483 Set_Associated_Node (N, Empty);
16484 Set_Etype (N, Empty);
16487 -- The subtype mark of a nominally unconstrained object is
16488 -- rewritten as a subtype indication using the bounds of the
16489 -- expression. Recover the original subtype mark.
16491 elsif Nkind (N2) = N_Subtype_Indication
16492 and then Is_Entity_Name (Original_Node (N2))
16494 Set_Associated_Node (N, Original_Node (N2));
16498 end Save_References_In_Identifier;
16500 ---------------------------------
16501 -- Save_References_In_Operator --
16502 ---------------------------------
16504 procedure Save_References_In_Operator (N : Node_Id) is
16506 -- The node did not undergo a transformation
16508 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16509 if Nkind (N) = N_Op_Concat then
16510 Set_Is_Component_Left_Opnd (N,
16511 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16513 Set_Is_Component_Right_Opnd (N,
16514 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16519 -- The analysis of the generic copy transformed the operator into
16520 -- some other construct. Propagate the changes to the template if
16524 N2 := Get_Associated_Node (N);
16526 -- The operator resoved to a function call
16528 if Nkind (N2) = N_Function_Call then
16530 -- Add explicit qualifications in the generic template for
16531 -- all operands of universal type. This aids resolution by
16532 -- preserving the actual type of a literal or an attribute
16533 -- that yields a universal result.
16535 Qualify_Universal_Operands (N, N2);
16537 E := Entity (Name (N2));
16539 if Present (E) and then Is_Global (E) then
16540 Set_Etype (N, Etype (N2));
16542 Set_Associated_Node (N, Empty);
16543 Set_Etype (N, Empty);
16546 -- The operator was folded into a literal
16548 elsif Nkind (N2) in N_Integer_Literal
16552 if Present (Original_Node (N2))
16553 and then Nkind (Original_Node (N2)) = Nkind (N)
16555 -- Operation was constant-folded. Whenever possible,
16556 -- recover semantic information from unfolded node.
16557 -- This was initially done for ASIS but is apparently
16558 -- needed also for e.g. compiling a-nbnbin.adb.
16560 Set_Associated_Node (N, Original_Node (N2));
16562 if Nkind (N) = N_Op_Concat then
16563 Set_Is_Component_Left_Opnd (N,
16564 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16565 Set_Is_Component_Right_Opnd (N,
16566 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16571 -- Propagate the constant folding back to the template
16574 Rewrite (N, New_Copy (N2));
16575 Set_Analyzed (N, False);
16578 -- The operator was folded into an enumeration literal. Retain
16579 -- the entity to avoid spurious ambiguities if it is overloaded
16580 -- at the point of instantiation or inlining.
16582 elsif Nkind (N2) = N_Identifier
16583 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16585 Rewrite (N, New_Copy (N2));
16586 Set_Analyzed (N, False);
16590 -- Complete the operands check if node has not been constant
16593 if Nkind (N) in N_Op then
16594 Save_Entity_Descendants (N);
16596 end Save_References_In_Operator;
16598 -------------------------------
16599 -- Save_References_In_Pragma --
16600 -------------------------------
16602 procedure Save_References_In_Pragma (Prag : Node_Id) is
16604 Do_Save : Boolean := True;
16607 -- Do not save global references in pragmas generated from aspects
16608 -- because the pragmas will be regenerated at instantiation time.
16610 if From_Aspect_Specification (Prag) then
16613 -- The capture of global references within contract-related source
16614 -- pragmas associated with generic packages, subprograms or their
16615 -- respective bodies must be delayed due to timing of annotation
16616 -- analysis. Global references are still captured in routine
16617 -- Save_Global_References_In_Contract.
16619 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16620 if Is_Package_Contract_Annotation (Prag) then
16621 Context := Find_Related_Package_Or_Body (Prag);
16623 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16624 Context := Find_Related_Declaration_Or_Body (Prag);
16627 -- The use of Original_Node accounts for the case when the
16628 -- related context is generic template.
16630 if Requires_Delayed_Save (Original_Node (Context)) then
16635 -- For all other cases, save all global references within the
16636 -- descendants, but skip the following semantic fields:
16637 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16640 Save_Global_Descendant
16641 (Union_Id (Pragma_Argument_Associations (N)));
16642 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16644 end Save_References_In_Pragma;
16646 -- Start of processing for Save_References
16654 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16655 Save_References_In_Aggregate (N);
16657 -- Character literals, operator symbols
16659 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16660 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16662 -- Defining identifiers
16664 elsif Nkind (N) in N_Entity then
16669 elsif Nkind (N) = N_Identifier then
16670 Save_References_In_Identifier (N);
16674 elsif Nkind (N) in N_Op then
16675 Save_References_In_Operator (N);
16679 elsif Nkind (N) = N_Pragma then
16680 Save_References_In_Pragma (N);
16683 Save_References_In_Descendants (N);
16686 -- Save all global references found within the aspect specifications
16687 -- of the related node.
16689 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16691 -- The capture of global references within aspects associated with
16692 -- generic packages, subprograms or their bodies must be delayed
16693 -- due to timing of annotation analysis. Global references are
16694 -- still captured in routine Save_Global_References_In_Contract.
16696 if Requires_Delayed_Save (N) then
16699 -- Otherwise save all global references within the aspects
16702 Save_Global_References_In_Aspects (N);
16705 end Save_References;
16707 -- Start of processing for Save_Global_References
16710 Gen_Scope := Current_Scope;
16712 -- If the generic unit is a child unit, references to entities in the
16713 -- parent are treated as local, because they will be resolved anew in
16714 -- the context of the instance of the parent.
16716 while Is_Child_Unit (Gen_Scope)
16717 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16719 Gen_Scope := Scope (Gen_Scope);
16722 Save_References (Templ);
16723 end Save_Global_References;
16725 ---------------------------------------
16726 -- Save_Global_References_In_Aspects --
16727 ---------------------------------------
16729 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16734 Asp := First (Aspect_Specifications (N));
16735 while Present (Asp) loop
16736 Expr := Expression (Asp);
16738 if Present (Expr) then
16739 Save_Global_References (Expr);
16744 end Save_Global_References_In_Aspects;
16746 ------------------------------------------
16747 -- Set_Copied_Sloc_For_Inherited_Pragma --
16748 ------------------------------------------
16750 procedure Set_Copied_Sloc_For_Inherited_Pragma
16755 Create_Instantiation_Source (N, E,
16756 Inlined_Body => False,
16757 Inherited_Pragma => True,
16758 Factor => S_Adjustment);
16759 end Set_Copied_Sloc_For_Inherited_Pragma;
16761 --------------------------------------
16762 -- Set_Copied_Sloc_For_Inlined_Body --
16763 --------------------------------------
16765 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16767 Create_Instantiation_Source (N, E,
16768 Inlined_Body => True,
16769 Inherited_Pragma => False,
16770 Factor => S_Adjustment);
16771 end Set_Copied_Sloc_For_Inlined_Body;
16773 ---------------------
16774 -- Set_Instance_Of --
16775 ---------------------
16777 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16779 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16780 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16781 Generic_Renamings.Increment_Last;
16782 end Set_Instance_Of;
16784 --------------------
16785 -- Set_Next_Assoc --
16786 --------------------
16788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16790 Generic_Renamings.Table (E).Next_In_HTable := Next;
16791 end Set_Next_Assoc;
16793 -------------------
16794 -- Start_Generic --
16795 -------------------
16797 procedure Start_Generic is
16799 -- ??? More things could be factored out in this routine.
16800 -- Should probably be done at a later stage.
16802 Generic_Flags.Append (Inside_A_Generic);
16803 Inside_A_Generic := True;
16805 Expander_Mode_Save_And_Set (False);
16808 ----------------------
16809 -- Set_Instance_Env --
16810 ----------------------
16812 -- WARNING: This routine manages SPARK regions
16814 procedure Set_Instance_Env
16815 (Gen_Unit : Entity_Id;
16816 Act_Unit : Entity_Id)
16818 Saved_AE : constant Boolean := Assertions_Enabled;
16819 Saved_CPL : constant Node_Id := Check_Policy_List;
16820 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16821 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16822 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16825 -- Regardless of the current mode, predefined units are analyzed in the
16826 -- most current Ada mode, and earlier version Ada checks do not apply
16827 -- to predefined units. Nothing needs to be done for non-internal units.
16828 -- These are always analyzed in the current mode.
16830 if In_Internal_Unit (Gen_Unit) then
16832 -- The following call resets all configuration attributes to default
16833 -- or the xxx_Config versions of the attributes when the current sem
16834 -- unit is the main unit. At the same time, internal units must also
16835 -- inherit certain configuration attributes from their context. It
16836 -- is unclear what these two sets are.
16838 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16840 -- Reinstall relevant configuration attributes of the context
16842 Assertions_Enabled := Saved_AE;
16843 Check_Policy_List := Saved_CPL;
16844 Dynamic_Elaboration_Checks := Saved_DEC;
16846 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16849 Current_Instantiated_Parent :=
16850 (Gen_Id => Gen_Unit,
16851 Act_Id => Act_Unit,
16852 Next_In_HTable => Assoc_Null);
16853 end Set_Instance_Env;
16859 procedure Switch_View (T : Entity_Id) is
16860 BT : constant Entity_Id := Base_Type (T);
16861 Priv_Elmt : Elmt_Id := No_Elmt;
16862 Priv_Sub : Entity_Id;
16865 -- T may be private but its base type may have been exchanged through
16866 -- some other occurrence, in which case there is nothing to switch
16867 -- besides T itself. Note that a private dependent subtype of a private
16868 -- type might not have been switched even if the base type has been,
16869 -- because of the last branch of Check_Private_View (see comment there).
16871 if not Is_Private_Type (BT) then
16872 Prepend_Elmt (Full_View (T), Exchanged_Views);
16873 Exchange_Declarations (T);
16877 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16879 if Present (Full_View (BT)) then
16880 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16881 Exchange_Declarations (BT);
16884 while Present (Priv_Elmt) loop
16885 Priv_Sub := Node (Priv_Elmt);
16887 if Present (Full_View (Priv_Sub)) then
16888 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16889 Exchange_Declarations (Priv_Sub);
16892 Next_Elmt (Priv_Elmt);
16900 function True_Parent (N : Node_Id) return Node_Id is
16902 if Nkind (Parent (N)) = N_Subunit then
16903 return Parent (Corresponding_Stub (Parent (N)));
16909 -----------------------------
16910 -- Valid_Default_Attribute --
16911 -----------------------------
16913 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16914 Attr_Id : constant Attribute_Id :=
16915 Get_Attribute_Id (Attribute_Name (Def));
16916 T : constant Entity_Id := Entity (Prefix (Def));
16917 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16923 if No (T) or else T = Any_Id then
16928 F := First_Formal (Nam);
16929 while Present (F) loop
16930 Num_F := Num_F + 1;
16935 when Attribute_Adjacent
16936 | Attribute_Ceiling
16937 | Attribute_Copy_Sign
16939 | Attribute_Fraction
16940 | Attribute_Machine
16942 | Attribute_Remainder
16943 | Attribute_Rounding
16944 | Attribute_Unbiased_Rounding
16948 and then Is_Floating_Point_Type (T);
16950 when Attribute_Image
16954 | Attribute_Wide_Image
16955 | Attribute_Wide_Value
16957 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16962 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16964 when Attribute_Input =>
16965 OK := (Is_Fun and then Num_F = 1);
16967 when Attribute_Output
16968 | Attribute_Put_Image
16972 OK := not Is_Fun and then Num_F = 2;
16980 ("attribute reference has wrong profile for subprogram", Def);
16982 end Valid_Default_Attribute;