1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
84 package body Sem_Ch12 is
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
107 -- | |==============>| |
108 -- |___________| global |__________|
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
165 -- type Global is ... -- outside of generic unit.
169 -- type Semi_Global is ... -- global to inner.
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
174 -- procedure in2 is new inner (...); -- 4
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
262 -- Refined_State Post_Class
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
323 -- Create_Generic_Contract
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
341 -- Requires_Delayed_Save
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
346 -- Save_Global_References_In_Contract
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
354 -- Instantiate_Subprogram_Contract
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
407 -----------------------
408 -- Local subprograms --
409 -----------------------
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
424 -- The following procedures treat other kinds of formal parameters
426 procedure Analyze_Formal_Derived_Interface_Type
431 procedure Analyze_Formal_Derived_Type
436 procedure Analyze_Formal_Interface_Type
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Private_Type
460 -- Creates a new private type, which does not require completion
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
471 function Analyze_Associations
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
481 procedure Analyze_Subprogram_Instantiation
485 procedure Build_Instance_Compilation_Unit_Nodes
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
526 procedure Check_Hidden_Child_Unit
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
544 function Contains_Instance_Of
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
555 function Denotes_Formal_Package
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
576 function Find_Actual_Type
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
614 -- Three kinds of source nodes have associated nodes:
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
623 -- c) selected components (N_Selected_Component)
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
661 function In_Same_Declarative_Part
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
686 procedure Inline_Instance_Body
688 Gen_Unit : Entity_Id;
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
694 procedure Insert_Freeze_Node_For_Instance
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
703 procedure Install_Body
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
748 function Instantiate_Object
751 Analyzed_Formal : Node_Id) return List_Id;
753 function Instantiate_Type
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
759 function Instantiate_Formal_Subprogram
762 Analyzed_Formal : Node_Id) return Node_Id;
764 function Instantiate_Formal_Package
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
796 procedure Load_Parent_Of_Generic
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
822 procedure Move_Freeze_Nodes
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
880 type Assoc_Ptr is new Int;
882 Assoc_Null : constant Assoc_Ptr := -1;
887 Next_In_HTable : Assoc_Ptr;
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
903 -- Hash table for associations
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
921 Get_Key => Get_Gen_Id,
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1027 procedure Abandon_Instantiation (N : Node_Id) is
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1071 function Analyze_Associations
1074 F_Copy : List_Id) return List_Id
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1089 Saved_Formal : Node_Id;
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1127 function Matching_Actual
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1180 if Present (Found_Assoc) then
1182 ("named association not allowed for overloaded formal",
1187 ("named association not allowed for overloaded formal",
1191 Abandon_Instantiation (Instantiation_Node);
1196 end Check_Overloaded_Formal_Subprogram;
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1209 -- Locate primitive operations of the type that are arithmetic
1212 Elem := First_Elmt (Prims);
1213 while Present (Elem) loop
1214 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1220 Formal := First_Non_Pragma (Formals);
1221 while Present (Formal) loop
1222 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars (Defining_Entity (Formal)) =
1233 Error_Msg_Sloc := Sloc (Node (Elem));
1235 ("?instance does not use primitive operation&#",
1236 Actual, Node (Elem));
1242 end Check_Fixed_Point_Actual;
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1248 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1249 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1256 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1258 -- A private type without a full view is not fully defined
1260 if Is_Private_Type (Typ)
1261 and then No (Full_View (Typ))
1265 -- An incomplete type is never fully defined
1267 elsif Is_Incomplete_Type (Typ) then
1270 -- All other types are fully defined
1275 end Is_Fully_Defined_Type;
1277 -- Local declarations
1281 -- Start of processing for Has_Fully_Defined_Profile
1284 -- Check the parameters
1286 Param := First_Formal (Subp);
1287 while Present (Param) loop
1288 if not Is_Fully_Defined_Type (Etype (Param)) then
1292 Next_Formal (Param);
1295 -- Check the return type
1297 return Is_Fully_Defined_Type (Etype (Subp));
1298 end Has_Fully_Defined_Profile;
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1304 function Matching_Actual
1306 A_F : Entity_Id) return Node_Id
1312 Is_Named_Assoc := False;
1314 -- End of list of purely positional parameters
1316 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1317 Found_Assoc := Empty;
1320 -- Case of positional parameter corresponding to current formal
1322 elsif No (Selector_Name (Actual)) then
1323 Found_Assoc := Actual;
1324 Act := Explicit_Generic_Actual_Parameter (Actual);
1325 Num_Matched := Num_Matched + 1;
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1332 Is_Named_Assoc := True;
1333 Found_Assoc := Empty;
1337 while Present (Actual) loop
1338 if Nkind (Actual) = N_Others_Choice then
1339 Found_Assoc := Empty;
1342 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1343 Set_Entity (Selector_Name (Actual), A_F);
1344 Set_Etype (Selector_Name (Actual), Etype (A_F));
1345 Generate_Reference (A_F, Selector_Name (Actual));
1347 Found_Assoc := Actual;
1348 Act := Explicit_Generic_Actual_Parameter (Actual);
1349 Num_Matched := Num_Matched + 1;
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1365 if Actual = First_Named then
1368 elsif Present (Actual) then
1369 Insert_Before (First_Named, Remove_Next (Prev));
1372 Actual := First_Named;
1375 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1376 Set_Used_As_Generic_Actual (Entity (Act));
1380 end Matching_Actual;
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1386 function Partial_Parameterization return Boolean is
1388 return Others_Present
1389 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1390 end Partial_Parameterization;
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1396 procedure Process_Default (F : Entity_Id) is
1397 Loc : constant Source_Ptr := Sloc (I_Node);
1398 F_Id : constant Entity_Id := Defining_Entity (F);
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1407 Decl := New_Copy_Tree (F);
1408 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1410 if Nkind (F) in N_Formal_Subprogram_Declaration then
1411 Set_Defining_Unit_Name (Specification (Decl), Id);
1414 Set_Defining_Identifier (Decl, Id);
1417 Append (Decl, Assoc);
1419 if No (Found_Assoc) then
1421 Make_Generic_Association (Loc,
1423 New_Occurrence_Of (Id, Loc),
1424 Explicit_Generic_Actual_Parameter => Empty);
1425 Set_Box_Present (Default);
1426 Append (Default, Default_Formals);
1428 end Process_Default;
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1434 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1439 while Present (Id) loop
1440 if Scope (Id) = Standard_Standard then
1448 end Renames_Standard_Subprogram;
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1454 procedure Set_Analyzed_Formal is
1458 while Present (Analyzed_Formal) loop
1459 Kind := Nkind (Analyzed_Formal);
1461 case Nkind (Formal) is
1462 when N_Formal_Subprogram_Declaration =>
1463 exit when Kind in N_Formal_Subprogram_Declaration
1466 (Defining_Unit_Name (Specification (Formal))) =
1468 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1470 when N_Formal_Package_Declaration =>
1471 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1472 N_Generic_Package_Declaration,
1473 N_Package_Declaration);
1475 when N_Use_Package_Clause
1482 -- Skip freeze nodes, and nodes inserted to replace
1483 -- unrecognized pragmas.
1486 Kind not in N_Formal_Subprogram_Declaration
1487 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1491 and then Chars (Defining_Identifier (Formal)) =
1492 Chars (Defining_Identifier (Analyzed_Formal));
1495 Next (Analyzed_Formal);
1497 end Set_Analyzed_Formal;
1499 -- Start of processing for Analyze_Associations
1502 Actuals := Generic_Associations (I_Node);
1504 if Present (Actuals) then
1506 -- Check for an Others choice, indicating a partial parameterization
1507 -- for a formal package.
1509 Actual := First (Actuals);
1510 while Present (Actual) loop
1511 if Nkind (Actual) = N_Others_Choice then
1512 Others_Present := True;
1513 Others_Choice := Actual;
1515 if Present (Next (Actual)) then
1516 Error_Msg_N ("others must be last association", Actual);
1519 -- This subprogram is used both for formal packages and for
1520 -- instantiations. For the latter, associations must all be
1523 if Nkind (I_Node) /= N_Formal_Package_Declaration
1524 and then Comes_From_Source (I_Node)
1527 ("others association not allowed in an instance",
1531 -- In any case, nothing to do after the others association
1535 elsif Box_Present (Actual)
1536 and then Comes_From_Source (I_Node)
1537 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1540 ("box association not allowed in an instance", Actual);
1546 -- If named associations are present, save first named association
1547 -- (it may of course be Empty) to facilitate subsequent name search.
1549 First_Named := First (Actuals);
1550 while Present (First_Named)
1551 and then Nkind (First_Named) /= N_Others_Choice
1552 and then No (Selector_Name (First_Named))
1554 Num_Actuals := Num_Actuals + 1;
1559 Named := First_Named;
1560 while Present (Named) loop
1561 if Nkind (Named) /= N_Others_Choice
1562 and then No (Selector_Name (Named))
1564 Error_Msg_N ("invalid positional actual after named one", Named);
1565 Abandon_Instantiation (Named);
1568 -- A named association may lack an actual parameter, if it was
1569 -- introduced for a default subprogram that turns out to be local
1570 -- to the outer instantiation. If it has a box association it must
1571 -- correspond to some formal in the generic.
1573 if Nkind (Named) /= N_Others_Choice
1574 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1575 or else Box_Present (Named))
1577 Num_Actuals := Num_Actuals + 1;
1583 if Present (Formals) then
1584 Formal := First_Non_Pragma (Formals);
1585 Analyzed_Formal := First_Non_Pragma (F_Copy);
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1590 -- All formals should have default values
1596 while Present (Formal) loop
1597 Set_Analyzed_Formal;
1598 Saved_Formal := Next_Non_Pragma (Formal);
1600 case Nkind (Formal) is
1601 when N_Formal_Object_Declaration =>
1604 (Defining_Identifier (Formal),
1605 Defining_Identifier (Analyzed_Formal));
1607 if No (Match) and then Partial_Parameterization then
1608 Process_Default (Formal);
1612 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1615 -- For a defaulted in_parameter, create an entry in the
1616 -- the list of defaulted actuals, for GNATProve use. Do
1617 -- not included these defaults for an instance nested
1618 -- within a generic, because the defaults are also used
1619 -- in the analysis of the enclosing generic, and only
1620 -- defaulted subprograms are relevant there.
1622 if No (Match) and then not Inside_A_Generic then
1623 Append_To (Default_Actuals,
1624 Make_Generic_Association (Sloc (I_Node),
1627 (Defining_Identifier (Formal), Sloc (I_Node)),
1628 Explicit_Generic_Actual_Parameter =>
1629 New_Copy_Tree (Default_Expression (Formal))));
1633 -- If the object is a call to an expression function, this
1634 -- is a freezing point for it.
1636 if Is_Entity_Name (Match)
1637 and then Present (Entity (Match))
1639 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1640 = N_Expression_Function
1642 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1645 when N_Formal_Type_Declaration =>
1648 (Defining_Identifier (Formal),
1649 Defining_Identifier (Analyzed_Formal));
1652 if Partial_Parameterization then
1653 Process_Default (Formal);
1656 Error_Msg_Sloc := Sloc (Gen_Unit);
1659 Instantiation_Node, Defining_Identifier (Formal));
1661 ("\in instantiation of & declared#",
1662 Instantiation_Node, Gen_Unit);
1663 Abandon_Instantiation (Instantiation_Node);
1670 (Formal, Match, Analyzed_Formal, Assoc),
1673 if Is_Fixed_Point_Type (Entity (Match)) then
1674 Check_Fixed_Point_Actual (Match);
1677 -- An instantiation is a freeze point for the actuals,
1678 -- unless this is a rewritten formal package, or the
1679 -- formal is an Ada 2012 formal incomplete type.
1681 if Nkind (I_Node) = N_Formal_Package_Declaration
1683 (Ada_Version >= Ada_2012
1685 Ekind (Defining_Identifier (Analyzed_Formal)) =
1691 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1695 -- A remote access-to-class-wide type is not a legal actual
1696 -- for a generic formal of an access type (E.2.2(17/2)).
1697 -- In GNAT an exception to this rule is introduced when
1698 -- the formal is marked as remote using implementation
1699 -- defined aspect/pragma Remote_Access_Type. In that case
1700 -- the actual must be remote as well.
1702 -- If the current instantiation is the construction of a
1703 -- local copy for a formal package the actuals may be
1704 -- defaulted, and there is no matching actual to check.
1706 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1708 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1709 N_Access_To_Object_Definition
1710 and then Present (Match)
1713 Formal_Ent : constant Entity_Id :=
1714 Defining_Identifier (Analyzed_Formal);
1716 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1717 = Is_Remote_Types (Formal_Ent)
1719 -- Remoteness of formal and actual match
1723 elsif Is_Remote_Types (Formal_Ent) then
1725 -- Remote formal, non-remote actual
1728 ("actual for& must be remote", Match, Formal_Ent);
1731 -- Non-remote formal, remote actual
1734 ("actual for& may not be remote",
1740 when N_Formal_Subprogram_Declaration =>
1743 (Defining_Unit_Name (Specification (Formal)),
1744 Defining_Unit_Name (Specification (Analyzed_Formal)));
1746 -- If the formal subprogram has the same name as another
1747 -- formal subprogram of the generic, then a named
1748 -- association is illegal (12.3(9)). Exclude named
1749 -- associations that are generated for a nested instance.
1752 and then Is_Named_Assoc
1753 and then Comes_From_Source (Found_Assoc)
1755 Check_Overloaded_Formal_Subprogram (Formal);
1758 -- If there is no corresponding actual, this may be case
1759 -- of partial parameterization, or else the formal has a
1760 -- default or a box.
1762 if No (Match) and then Partial_Parameterization then
1763 Process_Default (Formal);
1765 if Nkind (I_Node) = N_Formal_Package_Declaration then
1766 Check_Overloaded_Formal_Subprogram (Formal);
1771 Instantiate_Formal_Subprogram
1772 (Formal, Match, Analyzed_Formal));
1774 -- An instantiation is a freeze point for the actuals,
1775 -- unless this is a rewritten formal package.
1777 if Nkind (I_Node) /= N_Formal_Package_Declaration
1778 and then Nkind (Match) = N_Identifier
1779 and then Is_Subprogram (Entity (Match))
1781 -- The actual subprogram may rename a routine defined
1782 -- in Standard. Avoid freezing such renamings because
1783 -- subprograms coming from Standard cannot be frozen.
1786 not Renames_Standard_Subprogram (Entity (Match))
1788 -- If the actual subprogram comes from a different
1789 -- unit, it is already frozen, either by a body in
1790 -- that unit or by the end of the declarative part
1791 -- of the unit. This check avoids the freezing of
1792 -- subprograms defined in Standard which are used
1793 -- as generic actuals.
1795 and then In_Same_Code_Unit (Entity (Match), I_Node)
1796 and then Has_Fully_Defined_Profile (Entity (Match))
1798 -- Mark the subprogram as having a delayed freeze
1799 -- since this may be an out-of-order action.
1801 Set_Has_Delayed_Freeze (Entity (Match));
1802 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1806 -- If this is a nested generic, preserve default for later
1807 -- instantiations. We do this as well for GNATProve use,
1808 -- so that the list of generic associations is complete.
1810 if No (Match) and then Box_Present (Formal) then
1812 Subp : constant Entity_Id :=
1813 Defining_Unit_Name (Specification (Last (Assoc)));
1816 Append_To (Default_Actuals,
1817 Make_Generic_Association (Sloc (I_Node),
1819 New_Occurrence_Of (Subp, Sloc (I_Node)),
1820 Explicit_Generic_Actual_Parameter =>
1821 New_Occurrence_Of (Subp, Sloc (I_Node))));
1825 when N_Formal_Package_Declaration =>
1828 (Defining_Identifier (Formal),
1829 Defining_Identifier (Original_Node (Analyzed_Formal)));
1832 if Partial_Parameterization then
1833 Process_Default (Formal);
1836 Error_Msg_Sloc := Sloc (Gen_Unit);
1839 Instantiation_Node, Defining_Identifier (Formal));
1841 ("\in instantiation of & declared#",
1842 Instantiation_Node, Gen_Unit);
1844 Abandon_Instantiation (Instantiation_Node);
1850 (Instantiate_Formal_Package
1851 (Formal, Match, Analyzed_Formal),
1855 -- For use type and use package appearing in the generic part,
1856 -- we have already copied them, so we can just move them where
1857 -- they belong (we mustn't recopy them since this would mess up
1858 -- the Sloc values).
1860 when N_Use_Package_Clause
1863 if Nkind (Original_Node (I_Node)) =
1864 N_Formal_Package_Declaration
1866 Append (New_Copy_Tree (Formal), Assoc);
1869 Append (Formal, Assoc);
1873 raise Program_Error;
1876 Formal := Saved_Formal;
1877 Next_Non_Pragma (Analyzed_Formal);
1880 if Num_Actuals > Num_Matched then
1881 Error_Msg_Sloc := Sloc (Gen_Unit);
1883 if Present (Selector_Name (Actual)) then
1885 ("unmatched actual &", Actual, Selector_Name (Actual));
1887 ("\in instantiation of & declared#", Actual, Gen_Unit);
1890 ("unmatched actual in instantiation of & declared#",
1895 elsif Present (Actuals) then
1897 ("too many actuals in generic instantiation", Instantiation_Node);
1900 -- An instantiation freezes all generic actuals. The only exceptions
1901 -- to this are incomplete types and subprograms which are not fully
1902 -- defined at the point of instantiation.
1905 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1907 while Present (Elmt) loop
1908 Freeze_Before (I_Node, Node (Elmt));
1913 -- If there are default subprograms, normalize the tree by adding
1914 -- explicit associations for them. This is required if the instance
1915 -- appears within a generic.
1917 if not Is_Empty_List (Default_Actuals) then
1922 Default := First (Default_Actuals);
1923 while Present (Default) loop
1924 Mark_Rewrite_Insertion (Default);
1928 if No (Actuals) then
1929 Set_Generic_Associations (I_Node, Default_Actuals);
1931 Append_List_To (Actuals, Default_Actuals);
1936 -- If this is a formal package, normalize the parameter list by adding
1937 -- explicit box associations for the formals that are covered by an
1940 if not Is_Empty_List (Default_Formals) then
1941 Append_List (Default_Formals, Formals);
1945 end Analyze_Associations;
1947 -------------------------------
1948 -- Analyze_Formal_Array_Type --
1949 -------------------------------
1951 procedure Analyze_Formal_Array_Type
1952 (T : in out Entity_Id;
1958 -- Treated like a non-generic array declaration, with additional
1963 if Nkind (Def) = N_Constrained_Array_Definition then
1964 DSS := First (Discrete_Subtype_Definitions (Def));
1965 while Present (DSS) loop
1966 if Nkind_In (DSS, N_Subtype_Indication,
1968 N_Attribute_Reference)
1970 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1977 Array_Type_Declaration (T, Def);
1978 Set_Is_Generic_Type (Base_Type (T));
1980 if Ekind (Component_Type (T)) = E_Incomplete_Type
1981 and then No (Full_View (Component_Type (T)))
1983 Error_Msg_N ("premature usage of incomplete type", Def);
1985 -- Check that range constraint is not allowed on the component type
1986 -- of a generic formal array type (AARM 12.5.3(3))
1988 elsif Is_Internal (Component_Type (T))
1989 and then Present (Subtype_Indication (Component_Definition (Def)))
1990 and then Nkind (Original_Node
1991 (Subtype_Indication (Component_Definition (Def)))) =
1992 N_Subtype_Indication
1995 ("in a formal, a subtype indication can only be "
1996 & "a subtype mark (RM 12.5.3(3))",
1997 Subtype_Indication (Component_Definition (Def)));
2000 end Analyze_Formal_Array_Type;
2002 ---------------------------------------------
2003 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2004 ---------------------------------------------
2006 -- As for other generic types, we create a valid type representation with
2007 -- legal but arbitrary attributes, whose values are never considered
2008 -- static. For all scalar types we introduce an anonymous base type, with
2009 -- the same attributes. We choose the corresponding integer type to be
2010 -- Standard_Integer.
2011 -- Here and in other similar routines, the Sloc of the generated internal
2012 -- type must be the same as the sloc of the defining identifier of the
2013 -- formal type declaration, to provide proper source navigation.
2015 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2019 Loc : constant Source_Ptr := Sloc (Def);
2021 Base : constant Entity_Id :=
2023 (E_Decimal_Fixed_Point_Type,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2027 Int_Base : constant Entity_Id := Standard_Integer;
2028 Delta_Val : constant Ureal := Ureal_1;
2029 Digs_Val : constant Uint := Uint_6;
2031 function Make_Dummy_Bound return Node_Id;
2032 -- Return a properly typed universal real literal to use as a bound
2034 ----------------------
2035 -- Make_Dummy_Bound --
2036 ----------------------
2038 function Make_Dummy_Bound return Node_Id is
2039 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2041 Set_Etype (Bound, Universal_Real);
2043 end Make_Dummy_Bound;
2045 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2050 Set_Etype (Base, Base);
2051 Set_Size_Info (Base, Int_Base);
2052 Set_RM_Size (Base, RM_Size (Int_Base));
2053 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2054 Set_Digits_Value (Base, Digs_Val);
2055 Set_Delta_Value (Base, Delta_Val);
2056 Set_Small_Value (Base, Delta_Val);
2057 Set_Scalar_Range (Base,
2059 Low_Bound => Make_Dummy_Bound,
2060 High_Bound => Make_Dummy_Bound));
2062 Set_Is_Generic_Type (Base);
2063 Set_Parent (Base, Parent (Def));
2065 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2066 Set_Etype (T, Base);
2067 Set_Size_Info (T, Int_Base);
2068 Set_RM_Size (T, RM_Size (Int_Base));
2069 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2070 Set_Digits_Value (T, Digs_Val);
2071 Set_Delta_Value (T, Delta_Val);
2072 Set_Small_Value (T, Delta_Val);
2073 Set_Scalar_Range (T, Scalar_Range (Base));
2074 Set_Is_Constrained (T);
2076 Check_Restriction (No_Fixed_Point, Def);
2077 end Analyze_Formal_Decimal_Fixed_Point_Type;
2079 -------------------------------------------
2080 -- Analyze_Formal_Derived_Interface_Type --
2081 -------------------------------------------
2083 procedure Analyze_Formal_Derived_Interface_Type
2088 Loc : constant Source_Ptr := Sloc (Def);
2091 -- Rewrite as a type declaration of a derived type. This ensures that
2092 -- the interface list and primitive operations are properly captured.
2095 Make_Full_Type_Declaration (Loc,
2096 Defining_Identifier => T,
2097 Type_Definition => Def));
2099 Set_Is_Generic_Type (T);
2100 end Analyze_Formal_Derived_Interface_Type;
2102 ---------------------------------
2103 -- Analyze_Formal_Derived_Type --
2104 ---------------------------------
2106 procedure Analyze_Formal_Derived_Type
2111 Loc : constant Source_Ptr := Sloc (Def);
2112 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2116 Set_Is_Generic_Type (T);
2118 if Private_Present (Def) then
2120 Make_Private_Extension_Declaration (Loc,
2121 Defining_Identifier => T,
2122 Discriminant_Specifications => Discriminant_Specifications (N),
2123 Unknown_Discriminants_Present => Unk_Disc,
2124 Subtype_Indication => Subtype_Mark (Def),
2125 Interface_List => Interface_List (Def));
2127 Set_Abstract_Present (New_N, Abstract_Present (Def));
2128 Set_Limited_Present (New_N, Limited_Present (Def));
2129 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2133 Make_Full_Type_Declaration (Loc,
2134 Defining_Identifier => T,
2135 Discriminant_Specifications =>
2136 Discriminant_Specifications (Parent (T)),
2138 Make_Derived_Type_Definition (Loc,
2139 Subtype_Indication => Subtype_Mark (Def)));
2141 Set_Abstract_Present
2142 (Type_Definition (New_N), Abstract_Present (Def));
2144 (Type_Definition (New_N), Limited_Present (Def));
2151 if not Is_Composite_Type (T) then
2153 ("unknown discriminants not allowed for elementary types", N);
2155 Set_Has_Unknown_Discriminants (T);
2156 Set_Is_Constrained (T, False);
2160 -- If the parent type has a known size, so does the formal, which makes
2161 -- legal representation clauses that involve the formal.
2163 Set_Size_Known_At_Compile_Time
2164 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2165 end Analyze_Formal_Derived_Type;
2167 ----------------------------------
2168 -- Analyze_Formal_Discrete_Type --
2169 ----------------------------------
2171 -- The operations defined for a discrete types are those of an enumeration
2172 -- type. The size is set to an arbitrary value, for use in analyzing the
2175 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2176 Loc : constant Source_Ptr := Sloc (Def);
2180 Base : constant Entity_Id :=
2182 (E_Floating_Point_Type, Current_Scope,
2183 Sloc (Defining_Identifier (Parent (Def))), 'G');
2187 Set_Ekind (T, E_Enumeration_Subtype);
2188 Set_Etype (T, Base);
2191 Set_Is_Generic_Type (T);
2192 Set_Is_Constrained (T);
2194 -- For semantic analysis, the bounds of the type must be set to some
2195 -- non-static value. The simplest is to create attribute nodes for those
2196 -- bounds, that refer to the type itself. These bounds are never
2197 -- analyzed but serve as place-holders.
2200 Make_Attribute_Reference (Loc,
2201 Attribute_Name => Name_First,
2202 Prefix => New_Occurrence_Of (T, Loc));
2206 Make_Attribute_Reference (Loc,
2207 Attribute_Name => Name_Last,
2208 Prefix => New_Occurrence_Of (T, Loc));
2211 Set_Scalar_Range (T,
2216 Set_Ekind (Base, E_Enumeration_Type);
2217 Set_Etype (Base, Base);
2218 Init_Size (Base, 8);
2219 Init_Alignment (Base);
2220 Set_Is_Generic_Type (Base);
2221 Set_Scalar_Range (Base, Scalar_Range (T));
2222 Set_Parent (Base, Parent (Def));
2223 end Analyze_Formal_Discrete_Type;
2225 ----------------------------------
2226 -- Analyze_Formal_Floating_Type --
2227 ---------------------------------
2229 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2230 Base : constant Entity_Id :=
2232 (E_Floating_Point_Type, Current_Scope,
2233 Sloc (Defining_Identifier (Parent (Def))), 'G');
2236 -- The various semantic attributes are taken from the predefined type
2237 -- Float, just so that all of them are initialized. Their values are
2238 -- never used because no constant folding or expansion takes place in
2239 -- the generic itself.
2242 Set_Ekind (T, E_Floating_Point_Subtype);
2243 Set_Etype (T, Base);
2244 Set_Size_Info (T, (Standard_Float));
2245 Set_RM_Size (T, RM_Size (Standard_Float));
2246 Set_Digits_Value (T, Digits_Value (Standard_Float));
2247 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2248 Set_Is_Constrained (T);
2250 Set_Is_Generic_Type (Base);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, (Standard_Float));
2253 Set_RM_Size (Base, RM_Size (Standard_Float));
2254 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2255 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2256 Set_Parent (Base, Parent (Def));
2258 Check_Restriction (No_Floating_Point, Def);
2259 end Analyze_Formal_Floating_Type;
2261 -----------------------------------
2262 -- Analyze_Formal_Interface_Type;--
2263 -----------------------------------
2265 procedure Analyze_Formal_Interface_Type
2270 Loc : constant Source_Ptr := Sloc (N);
2275 Make_Full_Type_Declaration (Loc,
2276 Defining_Identifier => T,
2277 Type_Definition => Def);
2281 Set_Is_Generic_Type (T);
2282 end Analyze_Formal_Interface_Type;
2284 ---------------------------------
2285 -- Analyze_Formal_Modular_Type --
2286 ---------------------------------
2288 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2290 -- Apart from their entity kind, generic modular types are treated like
2291 -- signed integer types, and have the same attributes.
2293 Analyze_Formal_Signed_Integer_Type (T, Def);
2294 Set_Ekind (T, E_Modular_Integer_Subtype);
2295 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2297 end Analyze_Formal_Modular_Type;
2299 ---------------------------------------
2300 -- Analyze_Formal_Object_Declaration --
2301 ---------------------------------------
2303 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2304 E : constant Node_Id := Default_Expression (N);
2305 Id : constant Node_Id := Defining_Identifier (N);
2312 -- Determine the mode of the formal object
2314 if Out_Present (N) then
2315 K := E_Generic_In_Out_Parameter;
2317 if not In_Present (N) then
2318 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2322 K := E_Generic_In_Parameter;
2325 if Present (Subtype_Mark (N)) then
2326 Find_Type (Subtype_Mark (N));
2327 T := Entity (Subtype_Mark (N));
2329 -- Verify that there is no redundant null exclusion
2331 if Null_Exclusion_Present (N) then
2332 if not Is_Access_Type (T) then
2334 ("null exclusion can only apply to an access type", N);
2336 elsif Can_Never_Be_Null (T) then
2338 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2342 -- Ada 2005 (AI-423): Formal object with an access definition
2345 Check_Access_Definition (N);
2346 T := Access_Definition
2348 N => Access_Definition (N));
2351 if Ekind (T) = E_Incomplete_Type then
2353 Error_Node : Node_Id;
2356 if Present (Subtype_Mark (N)) then
2357 Error_Node := Subtype_Mark (N);
2359 Check_Access_Definition (N);
2360 Error_Node := Access_Definition (N);
2363 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2367 if K = E_Generic_In_Parameter then
2369 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2371 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2373 ("generic formal of mode IN must not be of limited type", N);
2374 Explain_Limited_Type (T, N);
2377 if Is_Abstract_Type (T) then
2379 ("generic formal of mode IN must not be of abstract type", N);
2383 Preanalyze_Spec_Expression (E, T);
2385 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2387 ("initialization not allowed for limited types", E);
2388 Explain_Limited_Type (T, E);
2395 -- Case of generic IN OUT parameter
2398 -- If the formal has an unconstrained type, construct its actual
2399 -- subtype, as is done for subprogram formals. In this fashion, all
2400 -- its uses can refer to specific bounds.
2405 if (Is_Array_Type (T) and then not Is_Constrained (T))
2406 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2409 Non_Freezing_Ref : constant Node_Id :=
2410 New_Occurrence_Of (Id, Sloc (Id));
2414 -- Make sure the actual subtype doesn't generate bogus freezing
2416 Set_Must_Not_Freeze (Non_Freezing_Ref);
2417 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2418 Insert_Before_And_Analyze (N, Decl);
2419 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2422 Set_Actual_Subtype (Id, T);
2427 ("initialization not allowed for `IN OUT` formals", N);
2431 if Has_Aspects (N) then
2432 Analyze_Aspect_Specifications (N, Id);
2434 end Analyze_Formal_Object_Declaration;
2436 ----------------------------------------------
2437 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2438 ----------------------------------------------
2440 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2444 Loc : constant Source_Ptr := Sloc (Def);
2445 Base : constant Entity_Id :=
2447 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2448 Sloc (Defining_Identifier (Parent (Def))), 'G');
2451 -- The semantic attributes are set for completeness only, their values
2452 -- will never be used, since all properties of the type are non-static.
2455 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2456 Set_Etype (T, Base);
2457 Set_Size_Info (T, Standard_Integer);
2458 Set_RM_Size (T, RM_Size (Standard_Integer));
2459 Set_Small_Value (T, Ureal_1);
2460 Set_Delta_Value (T, Ureal_1);
2461 Set_Scalar_Range (T,
2463 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2464 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2465 Set_Is_Constrained (T);
2467 Set_Is_Generic_Type (Base);
2468 Set_Etype (Base, Base);
2469 Set_Size_Info (Base, Standard_Integer);
2470 Set_RM_Size (Base, RM_Size (Standard_Integer));
2471 Set_Small_Value (Base, Ureal_1);
2472 Set_Delta_Value (Base, Ureal_1);
2473 Set_Scalar_Range (Base, Scalar_Range (T));
2474 Set_Parent (Base, Parent (Def));
2476 Check_Restriction (No_Fixed_Point, Def);
2477 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2479 ----------------------------------------
2480 -- Analyze_Formal_Package_Declaration --
2481 ----------------------------------------
2483 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2484 Gen_Id : constant Node_Id := Name (N);
2485 Loc : constant Source_Ptr := Sloc (N);
2486 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2489 Gen_Unit : Entity_Id;
2492 Vis_Prims_List : Elist_Id := No_Elist;
2493 -- List of primitives made temporarily visible in the instantiation
2494 -- to match the visibility of the formal type.
2496 function Build_Local_Package return Node_Id;
2497 -- The formal package is rewritten so that its parameters are replaced
2498 -- with corresponding declarations. For parameters with bona fide
2499 -- associations these declarations are created by Analyze_Associations
2500 -- as for a regular instantiation. For boxed parameters, we preserve
2501 -- the formal declarations and analyze them, in order to introduce
2502 -- entities of the right kind in the environment of the formal.
2504 -------------------------
2505 -- Build_Local_Package --
2506 -------------------------
2508 function Build_Local_Package return Node_Id is
2510 Pack_Decl : Node_Id;
2513 -- Within the formal, the name of the generic package is a renaming
2514 -- of the formal (as for a regular instantiation).
2517 Make_Package_Declaration (Loc,
2520 (Specification (Original_Node (Gen_Decl)),
2521 Empty, Instantiating => True));
2524 Make_Package_Renaming_Declaration (Loc,
2525 Defining_Unit_Name =>
2526 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2527 Name => New_Occurrence_Of (Formal, Loc));
2529 if Nkind (Gen_Id) = N_Identifier
2530 and then Chars (Gen_Id) = Chars (Pack_Id)
2533 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2536 -- If the formal is declared with a box, or with an others choice,
2537 -- create corresponding declarations for all entities in the formal
2538 -- part, so that names with the proper types are available in the
2539 -- specification of the formal package.
2541 -- On the other hand, if there are no associations, then all the
2542 -- formals must have defaults, and this will be checked by the
2543 -- call to Analyze_Associations.
2546 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2549 Formal_Decl : Node_Id;
2552 -- TBA : for a formal package, need to recurse ???
2557 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2558 while Present (Formal_Decl) loop
2560 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2565 -- If generic associations are present, use Analyze_Associations to
2566 -- create the proper renaming declarations.
2570 Act_Tree : constant Node_Id :=
2572 (Original_Node (Gen_Decl), Empty,
2573 Instantiating => True);
2576 Generic_Renamings.Set_Last (0);
2577 Generic_Renamings_HTable.Reset;
2578 Instantiation_Node := N;
2581 Analyze_Associations
2582 (I_Node => Original_Node (N),
2583 Formals => Generic_Formal_Declarations (Act_Tree),
2584 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2586 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2590 Append (Renaming, To => Decls);
2592 -- Add generated declarations ahead of local declarations in
2595 if No (Visible_Declarations (Specification (Pack_Decl))) then
2596 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2599 (First (Visible_Declarations (Specification (Pack_Decl))),
2604 end Build_Local_Package;
2608 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2609 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2611 Associations : Boolean := True;
2613 Parent_Installed : Boolean := False;
2614 Parent_Instance : Entity_Id;
2615 Renaming_In_Par : Entity_Id;
2617 -- Start of processing for Analyze_Formal_Package_Declaration
2620 Check_Text_IO_Special_Unit (Gen_Id);
2623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2624 Gen_Unit := Entity (Gen_Id);
2626 -- Check for a formal package that is a package renaming
2628 if Present (Renamed_Object (Gen_Unit)) then
2630 -- Indicate that unit is used, before replacing it with renamed
2631 -- entity for use below.
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2638 Gen_Unit := Renamed_Object (Gen_Unit);
2641 if Ekind (Gen_Unit) /= E_Generic_Package then
2642 Error_Msg_N ("expect generic package name", Gen_Id);
2646 elsif Gen_Unit = Current_Scope then
2648 ("generic package cannot be used as a formal package of itself",
2653 elsif In_Open_Scopes (Gen_Unit) then
2654 if Is_Compilation_Unit (Gen_Unit)
2655 and then Is_Child_Unit (Current_Scope)
2657 -- Special-case the error when the formal is a parent, and
2658 -- continue analysis to minimize cascaded errors.
2661 ("generic parent cannot be used as formal package of a child "
2666 ("generic package cannot be used as a formal package within "
2667 & "itself", Gen_Id);
2673 -- Check that name of formal package does not hide name of generic,
2674 -- or its leading prefix. This check must be done separately because
2675 -- the name of the generic has already been analyzed.
2678 Gen_Name : Entity_Id;
2682 while Nkind (Gen_Name) = N_Expanded_Name loop
2683 Gen_Name := Prefix (Gen_Name);
2686 if Chars (Gen_Name) = Chars (Pack_Id) then
2688 ("& is hidden within declaration of formal package",
2694 or else No (Generic_Associations (N))
2695 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2697 Associations := False;
2700 -- If there are no generic associations, the generic parameters appear
2701 -- as local entities and are instantiated like them. We copy the generic
2702 -- package declaration as if it were an instantiation, and analyze it
2703 -- like a regular package, except that we treat the formals as
2704 -- additional visible components.
2706 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2708 if In_Extended_Main_Source_Unit (N) then
2709 Set_Is_Instantiated (Gen_Unit);
2710 Generate_Reference (Gen_Unit, N);
2713 Formal := New_Copy (Pack_Id);
2714 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2716 -- Make local generic without formals. The formals will be replaced with
2717 -- internal declarations.
2720 New_N := Build_Local_Package;
2722 -- If there are errors in the parameter list, Analyze_Associations
2723 -- raises Instantiation_Error. Patch the declaration to prevent further
2724 -- exception propagation.
2727 when Instantiation_Error =>
2728 Enter_Name (Formal);
2729 Set_Ekind (Formal, E_Variable);
2730 Set_Etype (Formal, Any_Type);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2733 if Parent_Installed then
2741 Set_Defining_Unit_Name (Specification (New_N), Formal);
2742 Set_Generic_Parent (Specification (N), Gen_Unit);
2743 Set_Instance_Env (Gen_Unit, Formal);
2744 Set_Is_Generic_Instance (Formal);
2746 Enter_Name (Formal);
2747 Set_Ekind (Formal, E_Package);
2748 Set_Etype (Formal, Standard_Void_Type);
2749 Set_Inner_Instances (Formal, New_Elmt_List);
2750 Push_Scope (Formal);
2752 -- Manually set the SPARK_Mode from the context because the package
2753 -- declaration is never analyzed.
2755 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2756 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2757 Set_SPARK_Pragma_Inherited (Formal);
2758 Set_SPARK_Aux_Pragma_Inherited (Formal);
2760 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2762 -- Similarly, we have to make the name of the formal visible in the
2763 -- parent instance, to resolve properly fully qualified names that
2764 -- may appear in the generic unit. The parent instance has been
2765 -- placed on the scope stack ahead of the current scope.
2767 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2770 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2771 Set_Ekind (Renaming_In_Par, E_Package);
2772 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2773 Set_Scope (Renaming_In_Par, Parent_Instance);
2774 Set_Parent (Renaming_In_Par, Parent (Formal));
2775 Set_Renamed_Object (Renaming_In_Par, Formal);
2776 Append_Entity (Renaming_In_Par, Parent_Instance);
2779 -- A formal package declaration behaves as a package instantiation with
2780 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2781 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2782 -- all SPARK_Mode pragmas within the generic_package_name.
2784 if SPARK_Mode /= On then
2785 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2787 -- Mark the formal spec in case the body is instantiated at a later
2788 -- pass. This preserves the original context in effect for the body.
2790 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2793 Analyze (Specification (N));
2795 -- The formals for which associations are provided are not visible
2796 -- outside of the formal package. The others are still declared by a
2797 -- formal parameter declaration.
2799 -- If there are no associations, the only local entity to hide is the
2800 -- generated package renaming itself.
2806 E := First_Entity (Formal);
2807 while Present (E) loop
2808 if Associations and then not Is_Generic_Formal (E) then
2812 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2821 End_Package_Scope (Formal);
2822 Restore_Hidden_Primitives (Vis_Prims_List);
2824 if Parent_Installed then
2830 -- Inside the generic unit, the formal package is a regular package, but
2831 -- no body is needed for it. Note that after instantiation, the defining
2832 -- unit name we need is in the new tree and not in the original (see
2833 -- Package_Instantiation). A generic formal package is an instance, and
2834 -- can be used as an actual for an inner instance.
2836 Set_Has_Completion (Formal, True);
2838 -- Add semantic information to the original defining identifier for ASIS
2841 Set_Ekind (Pack_Id, E_Package);
2842 Set_Etype (Pack_Id, Standard_Void_Type);
2843 Set_Scope (Pack_Id, Scope (Formal));
2844 Set_Has_Completion (Pack_Id, True);
2847 if Has_Aspects (N) then
2848 Analyze_Aspect_Specifications (N, Pack_Id);
2851 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
2852 end Analyze_Formal_Package_Declaration;
2854 ---------------------------------
2855 -- Analyze_Formal_Private_Type --
2856 ---------------------------------
2858 procedure Analyze_Formal_Private_Type
2864 New_Private_Type (N, T, Def);
2866 -- Set the size to an arbitrary but legal value
2868 Set_Size_Info (T, Standard_Integer);
2869 Set_RM_Size (T, RM_Size (Standard_Integer));
2870 end Analyze_Formal_Private_Type;
2872 ------------------------------------
2873 -- Analyze_Formal_Incomplete_Type --
2874 ------------------------------------
2876 procedure Analyze_Formal_Incomplete_Type
2882 Set_Ekind (T, E_Incomplete_Type);
2884 Set_Private_Dependents (T, New_Elmt_List);
2886 if Tagged_Present (Def) then
2887 Set_Is_Tagged_Type (T);
2888 Make_Class_Wide_Type (T);
2889 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2891 end Analyze_Formal_Incomplete_Type;
2893 ----------------------------------------
2894 -- Analyze_Formal_Signed_Integer_Type --
2895 ----------------------------------------
2897 procedure Analyze_Formal_Signed_Integer_Type
2901 Base : constant Entity_Id :=
2903 (E_Signed_Integer_Type,
2905 Sloc (Defining_Identifier (Parent (Def))), 'G');
2910 Set_Ekind (T, E_Signed_Integer_Subtype);
2911 Set_Etype (T, Base);
2912 Set_Size_Info (T, Standard_Integer);
2913 Set_RM_Size (T, RM_Size (Standard_Integer));
2914 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2915 Set_Is_Constrained (T);
2917 Set_Is_Generic_Type (Base);
2918 Set_Size_Info (Base, Standard_Integer);
2919 Set_RM_Size (Base, RM_Size (Standard_Integer));
2920 Set_Etype (Base, Base);
2921 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2922 Set_Parent (Base, Parent (Def));
2923 end Analyze_Formal_Signed_Integer_Type;
2925 -------------------------------------------
2926 -- Analyze_Formal_Subprogram_Declaration --
2927 -------------------------------------------
2929 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2930 Spec : constant Node_Id := Specification (N);
2931 Def : constant Node_Id := Default_Name (N);
2932 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2940 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2941 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2945 Analyze_Subprogram_Declaration (N);
2946 Set_Is_Formal_Subprogram (Nam);
2947 Set_Has_Completion (Nam);
2949 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2950 Set_Is_Abstract_Subprogram (Nam);
2952 Set_Is_Dispatching_Operation (Nam);
2954 -- A formal abstract procedure cannot have a null default
2955 -- (RM 12.6(4.1/2)).
2957 if Nkind (Spec) = N_Procedure_Specification
2958 and then Null_Present (Spec)
2961 ("a formal abstract subprogram cannot default to null", Spec);
2965 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2967 if No (Ctrl_Type) then
2969 ("abstract formal subprogram must have a controlling type",
2972 elsif Ada_Version >= Ada_2012
2973 and then Is_Incomplete_Type (Ctrl_Type)
2976 ("controlling type of abstract formal subprogram cannot "
2977 & "be incomplete type", N, Ctrl_Type);
2980 Check_Controlling_Formals (Ctrl_Type, Nam);
2985 -- Default name is resolved at the point of instantiation
2987 if Box_Present (N) then
2990 -- Else default is bound at the point of generic declaration
2992 elsif Present (Def) then
2993 if Nkind (Def) = N_Operator_Symbol then
2994 Find_Direct_Name (Def);
2996 elsif Nkind (Def) /= N_Attribute_Reference then
3000 -- For an attribute reference, analyze the prefix and verify
3001 -- that it has the proper profile for the subprogram.
3003 Analyze (Prefix (Def));
3004 Valid_Default_Attribute (Nam, Def);
3008 -- Default name may be overloaded, in which case the interpretation
3009 -- with the correct profile must be selected, as for a renaming.
3010 -- If the definition is an indexed component, it must denote a
3011 -- member of an entry family. If it is a selected component, it
3012 -- can be a protected operation.
3014 if Etype (Def) = Any_Type then
3017 elsif Nkind (Def) = N_Selected_Component then
3018 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3019 Error_Msg_N ("expect valid subprogram name as default", Def);
3022 elsif Nkind (Def) = N_Indexed_Component then
3023 if Is_Entity_Name (Prefix (Def)) then
3024 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3025 Error_Msg_N ("expect valid subprogram name as default", Def);
3028 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3029 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3032 Error_Msg_N ("expect valid subprogram name as default", Def);
3036 Error_Msg_N ("expect valid subprogram name as default", Def);
3040 elsif Nkind (Def) = N_Character_Literal then
3042 -- Needs some type checks: subprogram should be parameterless???
3044 Resolve (Def, (Etype (Nam)));
3046 elsif not Is_Entity_Name (Def)
3047 or else not Is_Overloadable (Entity (Def))
3049 Error_Msg_N ("expect valid subprogram name as default", Def);
3052 elsif not Is_Overloaded (Def) then
3053 Subp := Entity (Def);
3056 Error_Msg_N ("premature usage of formal subprogram", Def);
3058 elsif not Entity_Matches_Spec (Subp, Nam) then
3059 Error_Msg_N ("no visible entity matches specification", Def);
3062 -- More than one interpretation, so disambiguate as for a renaming
3067 I1 : Interp_Index := 0;
3073 Get_First_Interp (Def, I, It);
3074 while Present (It.Nam) loop
3075 if Entity_Matches_Spec (It.Nam, Nam) then
3076 if Subp /= Any_Id then
3077 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3079 if It1 = No_Interp then
3080 Error_Msg_N ("ambiguous default subprogram", Def);
3093 Get_Next_Interp (I, It);
3097 if Subp /= Any_Id then
3099 -- Subprogram found, generate reference to it
3101 Set_Entity (Def, Subp);
3102 Generate_Reference (Subp, Def);
3105 Error_Msg_N ("premature usage of formal subprogram", Def);
3107 elsif Ekind (Subp) /= E_Operator then
3108 Check_Mode_Conformant (Subp, Nam);
3112 Error_Msg_N ("no visible subprogram matches specification", N);
3118 if Has_Aspects (N) then
3119 Analyze_Aspect_Specifications (N, Nam);
3122 end Analyze_Formal_Subprogram_Declaration;
3124 -------------------------------------
3125 -- Analyze_Formal_Type_Declaration --
3126 -------------------------------------
3128 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3129 Def : constant Node_Id := Formal_Type_Definition (N);
3133 T := Defining_Identifier (N);
3135 if Present (Discriminant_Specifications (N))
3136 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3139 ("discriminants not allowed for this formal type", T);
3142 -- Enter the new name, and branch to specific routine
3145 when N_Formal_Private_Type_Definition =>
3146 Analyze_Formal_Private_Type (N, T, Def);
3148 when N_Formal_Derived_Type_Definition =>
3149 Analyze_Formal_Derived_Type (N, T, Def);
3151 when N_Formal_Incomplete_Type_Definition =>
3152 Analyze_Formal_Incomplete_Type (T, Def);
3154 when N_Formal_Discrete_Type_Definition =>
3155 Analyze_Formal_Discrete_Type (T, Def);
3157 when N_Formal_Signed_Integer_Type_Definition =>
3158 Analyze_Formal_Signed_Integer_Type (T, Def);
3160 when N_Formal_Modular_Type_Definition =>
3161 Analyze_Formal_Modular_Type (T, Def);
3163 when N_Formal_Floating_Point_Definition =>
3164 Analyze_Formal_Floating_Type (T, Def);
3166 when N_Formal_Ordinary_Fixed_Point_Definition =>
3167 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3169 when N_Formal_Decimal_Fixed_Point_Definition =>
3170 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3172 when N_Array_Type_Definition =>
3173 Analyze_Formal_Array_Type (T, Def);
3175 when N_Access_Function_Definition
3176 | N_Access_Procedure_Definition
3177 | N_Access_To_Object_Definition
3179 Analyze_Generic_Access_Type (T, Def);
3181 -- Ada 2005: a interface declaration is encoded as an abstract
3182 -- record declaration or a abstract type derivation.
3184 when N_Record_Definition =>
3185 Analyze_Formal_Interface_Type (N, T, Def);
3187 when N_Derived_Type_Definition =>
3188 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3194 raise Program_Error;
3197 Set_Is_Generic_Type (T);
3199 if Has_Aspects (N) then
3200 Analyze_Aspect_Specifications (N, T);
3202 end Analyze_Formal_Type_Declaration;
3204 ------------------------------------
3205 -- Analyze_Function_Instantiation --
3206 ------------------------------------
3208 procedure Analyze_Function_Instantiation (N : Node_Id) is
3210 Analyze_Subprogram_Instantiation (N, E_Function);
3211 end Analyze_Function_Instantiation;
3213 ---------------------------------
3214 -- Analyze_Generic_Access_Type --
3215 ---------------------------------
3217 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3221 if Nkind (Def) = N_Access_To_Object_Definition then
3222 Access_Type_Declaration (T, Def);
3224 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3225 and then No (Full_View (Designated_Type (T)))
3226 and then not Is_Generic_Type (Designated_Type (T))
3228 Error_Msg_N ("premature usage of incomplete type", Def);
3230 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3232 ("only a subtype mark is allowed in a formal", Def);
3236 Access_Subprogram_Declaration (T, Def);
3238 end Analyze_Generic_Access_Type;
3240 ---------------------------------
3241 -- Analyze_Generic_Formal_Part --
3242 ---------------------------------
3244 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3245 Gen_Parm_Decl : Node_Id;
3248 -- The generic formals are processed in the scope of the generic unit,
3249 -- where they are immediately visible. The scope is installed by the
3252 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3253 while Present (Gen_Parm_Decl) loop
3254 Analyze (Gen_Parm_Decl);
3255 Next (Gen_Parm_Decl);
3258 Generate_Reference_To_Generic_Formals (Current_Scope);
3259 end Analyze_Generic_Formal_Part;
3261 ------------------------------------------
3262 -- Analyze_Generic_Package_Declaration --
3263 ------------------------------------------
3265 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3266 Loc : constant Source_Ptr := Sloc (N);
3267 Decls : constant List_Id :=
3268 Visible_Declarations (Specification (N));
3273 Save_Parent : Node_Id;
3276 Check_SPARK_05_Restriction ("generic is not allowed", N);
3278 -- We introduce a renaming of the enclosing package, to have a usable
3279 -- entity as the prefix of an expanded name for a local entity of the
3280 -- form Par.P.Q, where P is the generic package. This is because a local
3281 -- entity named P may hide it, so that the usual visibility rules in
3282 -- the instance will not resolve properly.
3285 Make_Package_Renaming_Declaration (Loc,
3286 Defining_Unit_Name =>
3287 Make_Defining_Identifier (Loc,
3288 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3290 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3292 if Present (Decls) then
3293 Decl := First (Decls);
3294 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3298 if Present (Decl) then
3299 Insert_Before (Decl, Renaming);
3301 Append (Renaming, Visible_Declarations (Specification (N)));
3305 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3308 -- Create copy of generic unit, and save for instantiation. If the unit
3309 -- is a child unit, do not copy the specifications for the parent, which
3310 -- are not part of the generic tree.
3312 Save_Parent := Parent_Spec (N);
3313 Set_Parent_Spec (N, Empty);
3315 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3316 Set_Parent_Spec (New_N, Save_Parent);
3319 -- Once the contents of the generic copy and the template are swapped,
3320 -- do the same for their respective aspect specifications.
3322 Exchange_Aspects (N, New_N);
3324 -- Collect all contract-related source pragmas found within the template
3325 -- and attach them to the contract of the package spec. This contract is
3326 -- used in the capture of global references within annotations.
3328 Create_Generic_Contract (N);
3330 Id := Defining_Entity (N);
3331 Generate_Definition (Id);
3333 -- Expansion is not applied to generic units
3338 Set_Ekind (Id, E_Generic_Package);
3339 Set_Etype (Id, Standard_Void_Type);
3341 -- Analyze aspects now, so that generated pragmas appear in the
3342 -- declarations before building and analyzing the generic copy.
3344 if Has_Aspects (N) then
3345 Analyze_Aspect_Specifications (N, Id);
3349 Enter_Generic_Scope (Id);
3350 Set_Inner_Instances (Id, New_Elmt_List);
3352 Set_Categorization_From_Pragmas (N);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3355 -- Link the declaration of the generic homonym in the generic copy to
3356 -- the package it renames, so that it is always resolved properly.
3358 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3359 Set_Entity (Associated_Node (Name (Renaming)), Id);
3361 -- For a library unit, we have reconstructed the entity for the unit,
3362 -- and must reset it in the library tables.
3364 if Nkind (Parent (N)) = N_Compilation_Unit then
3365 Set_Cunit_Entity (Current_Sem_Unit, Id);
3368 Analyze_Generic_Formal_Part (N);
3370 -- After processing the generic formals, analysis proceeds as for a
3371 -- non-generic package.
3373 Analyze (Specification (N));
3375 Validate_Categorization_Dependency (N, Id);
3379 End_Package_Scope (Id);
3380 Exit_Generic_Scope (Id);
3382 -- If the generic appears within a package unit, the body of that unit
3383 -- has to be present for instantiation and inlining.
3385 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3386 Set_Body_Needed_For_Inlining
3387 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3390 if Nkind (Parent (N)) /= N_Compilation_Unit then
3391 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3392 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3393 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3396 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3397 Validate_RT_RAT_Component (N);
3399 -- If this is a spec without a body, check that generic parameters
3402 if not Body_Required (Parent (N)) then
3403 Check_References (Id);
3407 -- If there is a specified storage pool in the context, create an
3408 -- aspect on the package declaration, so that it is used in any
3409 -- instance that does not override it.
3411 if Present (Default_Pool) then
3417 Make_Aspect_Specification (Loc,
3418 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3419 Expression => New_Copy (Default_Pool));
3421 if No (Aspect_Specifications (Specification (N))) then
3422 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3424 Append (ASN, Aspect_Specifications (Specification (N)));
3428 end Analyze_Generic_Package_Declaration;
3430 --------------------------------------------
3431 -- Analyze_Generic_Subprogram_Declaration --
3432 --------------------------------------------
3434 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3438 Result_Type : Entity_Id;
3439 Save_Parent : Node_Id;
3444 Check_SPARK_05_Restriction ("generic is not allowed", N);
3446 -- Create copy of generic unit, and save for instantiation. If the unit
3447 -- is a child unit, do not copy the specifications for the parent, which
3448 -- are not part of the generic tree.
3450 Save_Parent := Parent_Spec (N);
3451 Set_Parent_Spec (N, Empty);
3453 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3454 Set_Parent_Spec (New_N, Save_Parent);
3457 -- Once the contents of the generic copy and the template are swapped,
3458 -- do the same for their respective aspect specifications.
3460 Exchange_Aspects (N, New_N);
3462 -- Collect all contract-related source pragmas found within the template
3463 -- and attach them to the contract of the subprogram spec. This contract
3464 -- is used in the capture of global references within annotations.
3466 Create_Generic_Contract (N);
3468 Spec := Specification (N);
3469 Id := Defining_Entity (Spec);
3470 Generate_Definition (Id);
3472 if Nkind (Id) = N_Defining_Operator_Symbol then
3474 ("operator symbol not allowed for generic subprogram", Id);
3480 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3482 -- Analyze the aspects of the generic copy to ensure that all generated
3483 -- pragmas (if any) perform their semantic effects.
3485 if Has_Aspects (N) then
3486 Analyze_Aspect_Specifications (N, Id);
3490 Enter_Generic_Scope (Id);
3491 Set_Inner_Instances (Id, New_Elmt_List);
3492 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3494 Analyze_Generic_Formal_Part (N);
3496 Formals := Parameter_Specifications (Spec);
3498 if Nkind (Spec) = N_Function_Specification then
3499 Set_Ekind (Id, E_Generic_Function);
3501 Set_Ekind (Id, E_Generic_Procedure);
3504 if Present (Formals) then
3505 Process_Formals (Formals, Spec);
3508 if Nkind (Spec) = N_Function_Specification then
3509 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3510 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3511 Set_Etype (Id, Result_Type);
3513 -- Check restriction imposed by AI05-073: a generic function
3514 -- cannot return an abstract type or an access to such.
3516 -- This is a binding interpretation should it apply to earlier
3517 -- versions of Ada as well as Ada 2012???
3519 if Is_Abstract_Type (Designated_Type (Result_Type))
3520 and then Ada_Version >= Ada_2012
3523 ("generic function cannot have an access result "
3524 & "that designates an abstract type", Spec);
3528 Find_Type (Result_Definition (Spec));
3529 Typ := Entity (Result_Definition (Spec));
3531 if Is_Abstract_Type (Typ)
3532 and then Ada_Version >= Ada_2012
3535 ("generic function cannot have abstract result type", Spec);
3538 -- If a null exclusion is imposed on the result type, then create
3539 -- a null-excluding itype (an access subtype) and use it as the
3540 -- function's Etype.
3542 if Is_Access_Type (Typ)
3543 and then Null_Exclusion_Present (Spec)
3546 Create_Null_Excluding_Itype
3548 Related_Nod => Spec,
3549 Scope_Id => Defining_Unit_Name (Spec)));
3551 Set_Etype (Id, Typ);
3556 Set_Etype (Id, Standard_Void_Type);
3559 -- For a library unit, we have reconstructed the entity for the unit,
3560 -- and must reset it in the library tables. We also make sure that
3561 -- Body_Required is set properly in the original compilation unit node.
3563 if Nkind (Parent (N)) = N_Compilation_Unit then
3564 Set_Cunit_Entity (Current_Sem_Unit, Id);
3565 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3568 -- If the generic appears within a package unit, the body of that unit
3569 -- has to be present for instantiation and inlining.
3571 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3572 and then Unit_Requires_Body (Id)
3574 Set_Body_Needed_For_Inlining
3575 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3578 Set_Categorization_From_Pragmas (N);
3579 Validate_Categorization_Dependency (N, Id);
3581 -- Capture all global references that occur within the profile of the
3582 -- generic subprogram. Aspects are not part of this processing because
3583 -- they must be delayed. If processed now, Save_Global_References will
3584 -- destroy the Associated_Node links and prevent the capture of global
3585 -- references when the contract of the generic subprogram is analyzed.
3587 Save_Global_References (Original_Node (N));
3591 Exit_Generic_Scope (Id);
3592 Generate_Reference_To_Formals (Id);
3594 List_Inherited_Pre_Post_Aspects (Id);
3595 end Analyze_Generic_Subprogram_Declaration;
3597 -----------------------------------
3598 -- Analyze_Package_Instantiation --
3599 -----------------------------------
3601 -- WARNING: This routine manages Ghost regions. Return statements must be
3602 -- replaced by gotos which jump to the end of the routine and restore the
3605 procedure Analyze_Package_Instantiation (N : Node_Id) is
3606 Loc : constant Source_Ptr := Sloc (N);
3607 Gen_Id : constant Node_Id := Name (N);
3610 Act_Decl_Name : Node_Id;
3611 Act_Decl_Id : Entity_Id;
3617 Gen_Unit : Entity_Id;
3619 Is_Actual_Pack : constant Boolean :=
3620 Is_Internal (Defining_Entity (N));
3622 Env_Installed : Boolean := False;
3623 Parent_Installed : Boolean := False;
3624 Renaming_List : List_Id;
3625 Unit_Renaming : Node_Id;
3626 Needs_Body : Boolean;
3627 Inline_Now : Boolean := False;
3628 Has_Inline_Always : Boolean := False;
3630 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3631 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3633 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3634 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3635 -- Save the SPARK_Mode-related data for restore on exit
3637 Save_Style_Check : constant Boolean := Style_Check;
3638 -- Save style check mode for restore on exit
3640 procedure Delay_Descriptors (E : Entity_Id);
3641 -- Delay generation of subprogram descriptors for given entity
3643 function Might_Inline_Subp return Boolean;
3644 -- If inlining is active and the generic contains inlined subprograms,
3645 -- we instantiate the body. This may cause superfluous instantiations,
3646 -- but it is simpler than detecting the need for the body at the point
3647 -- of inlining, when the context of the instance is not available.
3649 -----------------------
3650 -- Delay_Descriptors --
3651 -----------------------
3653 procedure Delay_Descriptors (E : Entity_Id) is
3655 if not Delay_Subprogram_Descriptors (E) then
3656 Set_Delay_Subprogram_Descriptors (E);
3657 Pending_Descriptor.Append (E);
3659 end Delay_Descriptors;
3661 -----------------------
3662 -- Might_Inline_Subp --
3663 -----------------------
3665 function Might_Inline_Subp return Boolean is
3669 if not Inline_Processing_Required then
3673 E := First_Entity (Gen_Unit);
3674 while Present (E) loop
3675 if Is_Subprogram (E) and then Is_Inlined (E) then
3676 -- Remember if there are any subprograms with Inline_Always
3678 if Has_Pragma_Inline_Always (E) then
3679 Has_Inline_Always := True;
3690 end Might_Inline_Subp;
3692 -- Local declarations
3694 Mode : Ghost_Mode_Type;
3695 Mode_Set : Boolean := False;
3697 Vis_Prims_List : Elist_Id := No_Elist;
3698 -- List of primitives made temporarily visible in the instantiation
3699 -- to match the visibility of the formal type
3701 -- Start of processing for Analyze_Package_Instantiation
3704 Check_SPARK_05_Restriction ("generic is not allowed", N);
3706 -- Very first thing: check for Text_IO special unit in case we are
3707 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3709 Check_Text_IO_Special_Unit (Name (N));
3711 -- Make node global for error reporting
3713 Instantiation_Node := N;
3715 -- Case of instantiation of a generic package
3717 if Nkind (N) = N_Package_Instantiation then
3718 Act_Decl_Id := New_Copy (Defining_Entity (N));
3719 Set_Comes_From_Source (Act_Decl_Id, True);
3721 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3723 Make_Defining_Program_Unit_Name (Loc,
3725 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3726 Defining_Identifier => Act_Decl_Id);
3728 Act_Decl_Name := Act_Decl_Id;
3731 -- Case of instantiation of a formal package
3734 Act_Decl_Id := Defining_Identifier (N);
3735 Act_Decl_Name := Act_Decl_Id;
3738 Generate_Definition (Act_Decl_Id);
3739 Set_Ekind (Act_Decl_Id, E_Package);
3741 -- Initialize list of incomplete actuals before analysis
3743 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3745 Preanalyze_Actuals (N, Act_Decl_Id);
3747 -- Turn off style checking in instances. If the check is enabled on the
3748 -- generic unit, a warning in an instance would just be noise. If not
3749 -- enabled on the generic, then a warning in an instance is just wrong.
3750 -- This must be done after analyzing the actuals, which do come from
3751 -- source and are subject to style checking.
3753 Style_Check := False;
3756 Env_Installed := True;
3758 -- Reset renaming map for formal types. The mapping is established
3759 -- when analyzing the generic associations, but some mappings are
3760 -- inherited from formal packages of parent units, and these are
3761 -- constructed when the parents are installed.
3763 Generic_Renamings.Set_Last (0);
3764 Generic_Renamings_HTable.Reset;
3766 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3767 Gen_Unit := Entity (Gen_Id);
3769 -- A package instantiation is Ghost when it is subject to pragma Ghost
3770 -- or the generic template is Ghost. Set the mode now to ensure that
3771 -- any nodes generated during analysis and expansion are marked as
3774 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
3777 -- Verify that it is the name of a generic package
3779 -- A visibility glitch: if the instance is a child unit and the generic
3780 -- is the generic unit of a parent instance (i.e. both the parent and
3781 -- the child units are instances of the same package) the name now
3782 -- denotes the renaming within the parent, not the intended generic
3783 -- unit. See if there is a homonym that is the desired generic. The
3784 -- renaming declaration must be visible inside the instance of the
3785 -- child, but not when analyzing the name in the instantiation itself.
3787 if Ekind (Gen_Unit) = E_Package
3788 and then Present (Renamed_Entity (Gen_Unit))
3789 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3790 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3791 and then Present (Homonym (Gen_Unit))
3793 Gen_Unit := Homonym (Gen_Unit);
3796 if Etype (Gen_Unit) = Any_Type then
3800 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3802 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3804 if From_Limited_With (Gen_Unit) then
3806 ("cannot instantiate a limited withed package", Gen_Id);
3809 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3816 if In_Extended_Main_Source_Unit (N) then
3817 Set_Is_Instantiated (Gen_Unit);
3818 Generate_Reference (Gen_Unit, N);
3820 if Present (Renamed_Object (Gen_Unit)) then
3821 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3822 Generate_Reference (Renamed_Object (Gen_Unit), N);
3826 if Nkind (Gen_Id) = N_Identifier
3827 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3830 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3832 elsif Nkind (Gen_Id) = N_Expanded_Name
3833 and then Is_Child_Unit (Gen_Unit)
3834 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3835 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3838 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3841 Set_Entity (Gen_Id, Gen_Unit);
3843 -- If generic is a renaming, get original generic unit
3845 if Present (Renamed_Object (Gen_Unit))
3846 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3848 Gen_Unit := Renamed_Object (Gen_Unit);
3851 -- Verify that there are no circular instantiations
3853 if In_Open_Scopes (Gen_Unit) then
3854 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3858 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3859 Error_Msg_Node_2 := Current_Scope;
3861 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3862 Circularity_Detected := True;
3867 -- If the context of the instance is subject to SPARK_Mode "off" or
3868 -- the annotation is altogether missing, set the global flag which
3869 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3872 if SPARK_Mode /= On then
3873 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3875 -- Mark the instance spec in case the body is instantiated at a
3876 -- later pass. This preserves the original context in effect for
3879 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
3882 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3883 Gen_Spec := Specification (Gen_Decl);
3885 -- Initialize renamings map, for error checking, and the list that
3886 -- holds private entities whose views have changed between generic
3887 -- definition and instantiation. If this is the instance created to
3888 -- validate an actual package, the instantiation environment is that
3889 -- of the enclosing instance.
3891 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3893 -- Copy original generic tree, to produce text for instantiation
3897 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3899 Act_Spec := Specification (Act_Tree);
3901 -- If this is the instance created to validate an actual package,
3902 -- only the formals matter, do not examine the package spec itself.
3904 if Is_Actual_Pack then
3905 Set_Visible_Declarations (Act_Spec, New_List);
3906 Set_Private_Declarations (Act_Spec, New_List);
3910 Analyze_Associations
3912 Formals => Generic_Formal_Declarations (Act_Tree),
3913 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3915 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3917 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3918 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3919 Set_Is_Generic_Instance (Act_Decl_Id);
3920 Set_Generic_Parent (Act_Spec, Gen_Unit);
3922 -- References to the generic in its own declaration or its body are
3923 -- references to the instance. Add a renaming declaration for the
3924 -- generic unit itself. This declaration, as well as the renaming
3925 -- declarations for the generic formals, must remain private to the
3926 -- unit: the formals, because this is the language semantics, and
3927 -- the unit because its use is an artifact of the implementation.
3930 Make_Package_Renaming_Declaration (Loc,
3931 Defining_Unit_Name =>
3932 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3933 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3935 Append (Unit_Renaming, Renaming_List);
3937 -- The renaming declarations are the first local declarations of the
3940 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3942 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3944 Set_Visible_Declarations (Act_Spec, Renaming_List);
3947 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3949 -- Propagate the aspect specifications from the package declaration
3950 -- template to the instantiated version of the package declaration.
3952 if Has_Aspects (Act_Tree) then
3953 Set_Aspect_Specifications (Act_Decl,
3954 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3957 -- The generic may have a generated Default_Storage_Pool aspect,
3958 -- set at the point of generic declaration. If the instance has
3959 -- that aspect, it overrides the one inherited from the generic.
3961 if Has_Aspects (Gen_Spec) then
3962 if No (Aspect_Specifications (N)) then
3963 Set_Aspect_Specifications (N,
3965 (Aspect_Specifications (Gen_Spec))));
3969 ASN1, ASN2 : Node_Id;
3972 ASN1 := First (Aspect_Specifications (N));
3973 while Present (ASN1) loop
3974 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3976 -- If generic carries a default storage pool, remove
3977 -- it in favor of the instance one.
3979 ASN2 := First (Aspect_Specifications (Gen_Spec));
3980 while Present (ASN2) loop
3981 if Chars (Identifier (ASN2)) =
3982 Name_Default_Storage_Pool
3995 Prepend_List_To (Aspect_Specifications (N),
3997 (Aspect_Specifications (Gen_Spec))));
4002 -- Save the instantiation node, for subsequent instantiation of the
4003 -- body, if there is one and we are generating code for the current
4004 -- unit. Mark unit as having a body (avoids premature error message).
4006 -- We instantiate the body if we are generating code, if we are
4007 -- generating cross-reference information, or if we are building
4008 -- trees for ASIS use or GNATprove use.
4011 Enclosing_Body_Present : Boolean := False;
4012 -- If the generic unit is not a compilation unit, then a body may
4013 -- be present in its parent even if none is required. We create a
4014 -- tentative pending instantiation for the body, which will be
4015 -- discarded if none is actually present.
4020 if Scope (Gen_Unit) /= Standard_Standard
4021 and then not Is_Child_Unit (Gen_Unit)
4023 Scop := Scope (Gen_Unit);
4024 while Present (Scop) and then Scop /= Standard_Standard loop
4025 if Unit_Requires_Body (Scop) then
4026 Enclosing_Body_Present := True;
4029 elsif In_Open_Scopes (Scop)
4030 and then In_Package_Body (Scop)
4032 Enclosing_Body_Present := True;
4036 exit when Is_Compilation_Unit (Scop);
4037 Scop := Scope (Scop);
4041 -- If front-end inlining is enabled or there are any subprograms
4042 -- marked with Inline_Always, and this is a unit for which code
4043 -- will be generated, we instantiate the body at once.
4045 -- This is done if the instance is not the main unit, and if the
4046 -- generic is not a child unit of another generic, to avoid scope
4047 -- problems and the reinstallation of parent instances.
4050 and then (not Is_Child_Unit (Gen_Unit)
4051 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4052 and then Might_Inline_Subp
4053 and then not Is_Actual_Pack
4055 if not Back_End_Inlining
4056 and then (Front_End_Inlining or else Has_Inline_Always)
4057 and then (Is_In_Main_Unit (N)
4058 or else In_Main_Context (Current_Scope))
4059 and then Nkind (Parent (N)) /= N_Compilation_Unit
4063 -- In configurable_run_time mode we force the inlining of
4064 -- predefined subprograms marked Inline_Always, to minimize
4065 -- the use of the run-time library.
4067 elsif Is_Predefined_File_Name
4068 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4069 and then Configurable_Run_Time_Mode
4070 and then Nkind (Parent (N)) /= N_Compilation_Unit
4075 -- If the current scope is itself an instance within a child
4076 -- unit, there will be duplications in the scope stack, and the
4077 -- unstacking mechanism in Inline_Instance_Body will fail.
4078 -- This loses some rare cases of optimization, and might be
4079 -- improved some day, if we can find a proper abstraction for
4080 -- "the complete compilation context" that can be saved and
4083 if Is_Generic_Instance (Current_Scope) then
4085 Curr_Unit : constant Entity_Id :=
4086 Cunit_Entity (Current_Sem_Unit);
4088 if Curr_Unit /= Current_Scope
4089 and then Is_Child_Unit (Curr_Unit)
4091 Inline_Now := False;
4098 (Unit_Requires_Body (Gen_Unit)
4099 or else Enclosing_Body_Present
4100 or else Present (Corresponding_Body (Gen_Decl)))
4101 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4102 and then not Is_Actual_Pack
4103 and then not Inline_Now
4104 and then (Operating_Mode = Generate_Code
4106 -- Need comment for this check ???
4108 or else (Operating_Mode = Check_Semantics
4109 and then (ASIS_Mode or GNATprove_Mode)));
4111 -- If front-end inlining is enabled or there are any subprograms
4112 -- marked with Inline_Always, do not instantiate body when within
4113 -- a generic context.
4115 if ((Front_End_Inlining or else Has_Inline_Always)
4116 and then not Expander_Active)
4117 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4119 Needs_Body := False;
4122 -- If the current context is generic, and the package being
4123 -- instantiated is declared within a formal package, there is no
4124 -- body to instantiate until the enclosing generic is instantiated
4125 -- and there is an actual for the formal package. If the formal
4126 -- package has parameters, we build a regular package instance for
4127 -- it, that precedes the original formal package declaration.
4129 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4131 Decl : constant Node_Id :=
4133 (Unit_Declaration_Node (Scope (Gen_Unit)));
4135 if Nkind (Decl) = N_Formal_Package_Declaration
4136 or else (Nkind (Decl) = N_Package_Declaration
4137 and then Is_List_Member (Decl)
4138 and then Present (Next (Decl))
4140 Nkind (Next (Decl)) =
4141 N_Formal_Package_Declaration)
4143 Needs_Body := False;
4149 -- For RCI unit calling stubs, we omit the instance body if the
4150 -- instance is the RCI library unit itself.
4152 -- However there is a special case for nested instances: in this case
4153 -- we do generate the instance body, as it might be required, e.g.
4154 -- because it provides stream attributes for some type used in the
4155 -- profile of a remote subprogram. This is consistent with 12.3(12),
4156 -- which indicates that the instance body occurs at the place of the
4157 -- instantiation, and thus is part of the RCI declaration, which is
4158 -- present on all client partitions (this is E.2.3(18)).
4160 -- Note that AI12-0002 may make it illegal at some point to have
4161 -- stream attributes defined in an RCI unit, in which case this
4162 -- special case will become unnecessary. In the meantime, there
4163 -- is known application code in production that depends on this
4164 -- being possible, so we definitely cannot eliminate the body in
4165 -- the case of nested instances for the time being.
4167 -- When we generate a nested instance body, calling stubs for any
4168 -- relevant subprogram will be be inserted immediately after the
4169 -- subprogram declarations, and will take precedence over the
4170 -- subsequent (original) body. (The stub and original body will be
4171 -- complete homographs, but this is permitted in an instance).
4172 -- (Could we do better and remove the original body???)
4174 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4175 and then Comes_From_Source (N)
4176 and then Nkind (Parent (N)) = N_Compilation_Unit
4178 Needs_Body := False;
4183 -- Here is a defence against a ludicrous number of instantiations
4184 -- caused by a circular set of instantiation attempts.
4186 if Pending_Instantiations.Last > Maximum_Instantiations then
4187 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4188 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4189 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4190 raise Unrecoverable_Error;
4193 -- Indicate that the enclosing scopes contain an instantiation,
4194 -- and that cleanup actions should be delayed until after the
4195 -- instance body is expanded.
4197 Check_Forward_Instantiation (Gen_Decl);
4198 if Nkind (N) = N_Package_Instantiation then
4200 Enclosing_Master : Entity_Id;
4203 -- Loop to search enclosing masters
4205 Enclosing_Master := Current_Scope;
4206 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4207 if Ekind (Enclosing_Master) = E_Package then
4208 if Is_Compilation_Unit (Enclosing_Master) then
4209 if In_Package_Body (Enclosing_Master) then
4211 (Body_Entity (Enclosing_Master));
4220 Enclosing_Master := Scope (Enclosing_Master);
4223 elsif Is_Generic_Unit (Enclosing_Master)
4224 or else Ekind (Enclosing_Master) = E_Void
4226 -- Cleanup actions will eventually be performed on the
4227 -- enclosing subprogram or package instance, if any.
4228 -- Enclosing scope is void in the formal part of a
4229 -- generic subprogram.
4234 if Ekind (Enclosing_Master) = E_Entry
4236 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4238 if not Expander_Active then
4242 Protected_Body_Subprogram (Enclosing_Master);
4246 Set_Delay_Cleanups (Enclosing_Master);
4248 while Ekind (Enclosing_Master) = E_Block loop
4249 Enclosing_Master := Scope (Enclosing_Master);
4252 if Is_Subprogram (Enclosing_Master) then
4253 Delay_Descriptors (Enclosing_Master);
4255 elsif Is_Task_Type (Enclosing_Master) then
4257 TBP : constant Node_Id :=
4258 Get_Task_Body_Procedure
4261 if Present (TBP) then
4262 Delay_Descriptors (TBP);
4263 Set_Delay_Cleanups (TBP);
4270 end loop Scope_Loop;
4273 -- Make entry in table
4275 Add_Pending_Instantiation (N, Act_Decl);
4279 Set_Categorization_From_Pragmas (Act_Decl);
4281 if Parent_Installed then
4285 Set_Instance_Spec (N, Act_Decl);
4287 -- If not a compilation unit, insert the package declaration before
4288 -- the original instantiation node.
4290 if Nkind (Parent (N)) /= N_Compilation_Unit then
4291 Mark_Rewrite_Insertion (Act_Decl);
4292 Insert_Before (N, Act_Decl);
4294 if Has_Aspects (N) then
4295 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4297 -- The pragma created for a Default_Storage_Pool aspect must
4298 -- appear ahead of the declarations in the instance spec.
4299 -- Analysis has placed it after the instance node, so remove
4300 -- it and reinsert it properly now.
4303 ASN : constant Node_Id := First (Aspect_Specifications (N));
4304 A_Name : constant Name_Id := Chars (Identifier (ASN));
4308 if A_Name = Name_Default_Storage_Pool then
4309 if No (Visible_Declarations (Act_Spec)) then
4310 Set_Visible_Declarations (Act_Spec, New_List);
4314 while Present (Decl) loop
4315 if Nkind (Decl) = N_Pragma then
4317 Prepend (Decl, Visible_Declarations (Act_Spec));
4329 -- For an instantiation that is a compilation unit, place
4330 -- declaration on current node so context is complete for analysis
4331 -- (including nested instantiations). If this is the main unit,
4332 -- the declaration eventually replaces the instantiation node.
4333 -- If the instance body is created later, it replaces the
4334 -- instance node, and the declaration is attached to it
4335 -- (see Build_Instance_Compilation_Unit_Nodes).
4338 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4340 -- The entity for the current unit is the newly created one,
4341 -- and all semantic information is attached to it.
4343 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4345 -- If this is the main unit, replace the main entity as well
4347 if Current_Sem_Unit = Main_Unit then
4348 Main_Unit_Entity := Act_Decl_Id;
4352 Set_Unit (Parent (N), Act_Decl);
4353 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4354 Set_Package_Instantiation (Act_Decl_Id, N);
4356 -- Process aspect specifications of the instance node, if any, to
4357 -- take into account categorization pragmas before analyzing the
4360 if Has_Aspects (N) then
4361 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4365 Set_Unit (Parent (N), N);
4366 Set_Body_Required (Parent (N), False);
4368 -- We never need elaboration checks on instantiations, since by
4369 -- definition, the body instantiation is elaborated at the same
4370 -- time as the spec instantiation.
4372 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4373 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4376 Check_Elab_Instantiation (N);
4378 if ABE_Is_Certain (N) and then Needs_Body then
4379 Pending_Instantiations.Decrement_Last;
4382 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4384 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4385 First_Private_Entity (Act_Decl_Id));
4387 -- If the instantiation will receive a body, the unit will be
4388 -- transformed into a package body, and receive its own elaboration
4389 -- entity. Otherwise, the nature of the unit is now a package
4392 if Nkind (Parent (N)) = N_Compilation_Unit
4393 and then not Needs_Body
4395 Rewrite (N, Act_Decl);
4398 if Present (Corresponding_Body (Gen_Decl))
4399 or else Unit_Requires_Body (Gen_Unit)
4401 Set_Has_Completion (Act_Decl_Id);
4404 Check_Formal_Packages (Act_Decl_Id);
4406 Restore_Hidden_Primitives (Vis_Prims_List);
4407 Restore_Private_Views (Act_Decl_Id);
4409 Inherit_Context (Gen_Decl, N);
4411 if Parent_Installed then
4416 Env_Installed := False;
4419 Validate_Categorization_Dependency (N, Act_Decl_Id);
4421 -- There used to be a check here to prevent instantiations in local
4422 -- contexts if the No_Local_Allocators restriction was active. This
4423 -- check was removed by a binding interpretation in AI-95-00130/07,
4424 -- but we retain the code for documentation purposes.
4426 -- if Ekind (Act_Decl_Id) /= E_Void
4427 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4429 -- Check_Restriction (No_Local_Allocators, N);
4433 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4436 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4437 -- be used as defining identifiers for a formal package and for the
4438 -- corresponding expanded package.
4440 if Nkind (N) = N_Formal_Package_Declaration then
4441 Act_Decl_Id := New_Copy (Defining_Entity (N));
4442 Set_Comes_From_Source (Act_Decl_Id, True);
4443 Set_Is_Generic_Instance (Act_Decl_Id, False);
4444 Set_Defining_Identifier (N, Act_Decl_Id);
4447 -- Check that if N is an instantiation of System.Dim_Float_IO or
4448 -- System.Dim_Integer_IO, the formal type has a dimension system.
4450 if Nkind (N) = N_Package_Instantiation
4451 and then Is_Dim_IO_Package_Instantiation (N)
4454 Assoc : constant Node_Id := First (Generic_Associations (N));
4456 if not Has_Dimension_System
4457 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4459 Error_Msg_N ("type with a dimension system expected", Assoc);
4465 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4466 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4469 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
4470 SPARK_Mode := Save_SM;
4471 SPARK_Mode_Pragma := Save_SMP;
4472 Style_Check := Save_Style_Check;
4475 Restore_Ghost_Mode (Mode);
4479 when Instantiation_Error =>
4480 if Parent_Installed then
4484 if Env_Installed then
4488 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
4489 SPARK_Mode := Save_SM;
4490 SPARK_Mode_Pragma := Save_SMP;
4491 Style_Check := Save_Style_Check;
4494 Restore_Ghost_Mode (Mode);
4496 end Analyze_Package_Instantiation;
4498 --------------------------
4499 -- Inline_Instance_Body --
4500 --------------------------
4502 procedure Inline_Instance_Body
4504 Gen_Unit : Entity_Id;
4507 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4508 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4509 Gen_Comp : constant Entity_Id :=
4510 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4512 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4513 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4514 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4515 -- to provide a clean environment for analysis of the inlined body will
4516 -- eliminate any previously set SPARK_Mode.
4518 Scope_Stack_Depth : constant Pos :=
4519 Scope_Stack.Last - Scope_Stack.First + 1;
4521 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4522 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4523 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4524 Curr_Scope : Entity_Id := Empty;
4526 Num_Inner : Nat := 0;
4527 Num_Scopes : Nat := 0;
4528 N_Instances : Nat := 0;
4529 Removed : Boolean := False;
4534 -- Case of generic unit defined in another unit. We must remove the
4535 -- complete context of the current unit to install that of the generic.
4537 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4539 -- Add some comments for the following two loops ???
4542 while Present (S) and then S /= Standard_Standard loop
4544 Num_Scopes := Num_Scopes + 1;
4546 Use_Clauses (Num_Scopes) :=
4548 (Scope_Stack.Last - Num_Scopes + 1).
4550 End_Use_Clauses (Use_Clauses (Num_Scopes));
4552 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4553 or else Scope_Stack.Table
4554 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4557 exit when Is_Generic_Instance (S)
4558 and then (In_Package_Body (S)
4559 or else Ekind (S) = E_Procedure
4560 or else Ekind (S) = E_Function);
4564 Vis := Is_Immediately_Visible (Gen_Comp);
4566 -- Find and save all enclosing instances
4571 and then S /= Standard_Standard
4573 if Is_Generic_Instance (S) then
4574 N_Instances := N_Instances + 1;
4575 Instances (N_Instances) := S;
4577 exit when In_Package_Body (S);
4583 -- Remove context of current compilation unit, unless we are within a
4584 -- nested package instantiation, in which case the context has been
4585 -- removed previously.
4587 -- If current scope is the body of a child unit, remove context of
4588 -- spec as well. If an enclosing scope is an instance body, the
4589 -- context has already been removed, but the entities in the body
4590 -- must be made invisible as well.
4593 while Present (S) and then S /= Standard_Standard loop
4594 if Is_Generic_Instance (S)
4595 and then (In_Package_Body (S)
4596 or else Ekind_In (S, E_Procedure, E_Function))
4598 -- We still have to remove the entities of the enclosing
4599 -- instance from direct visibility.
4604 E := First_Entity (S);
4605 while Present (E) loop
4606 Set_Is_Immediately_Visible (E, False);
4615 or else (Ekind (Curr_Unit) = E_Package_Body
4616 and then S = Spec_Entity (Curr_Unit))
4617 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4618 and then S = Corresponding_Spec
4619 (Unit_Declaration_Node (Curr_Unit)))
4623 -- Remove entities in current scopes from visibility, so that
4624 -- instance body is compiled in a clean environment.
4626 List := Save_Scope_Stack (Handle_Use => False);
4628 if Is_Child_Unit (S) then
4630 -- Remove child unit from stack, as well as inner scopes.
4631 -- Removing the context of a child unit removes parent units
4634 while Current_Scope /= S loop
4635 Num_Inner := Num_Inner + 1;
4636 Inner_Scopes (Num_Inner) := Current_Scope;
4641 Remove_Context (Curr_Comp);
4645 Remove_Context (Curr_Comp);
4648 if Ekind (Curr_Unit) = E_Package_Body then
4649 Remove_Context (Library_Unit (Curr_Comp));
4656 pragma Assert (Num_Inner < Num_Scopes);
4658 -- The inlined package body must be analyzed with the SPARK_Mode of
4659 -- the enclosing context, otherwise the body may cause bogus errors
4660 -- if a configuration SPARK_Mode pragma in in effect.
4662 Push_Scope (Standard_Standard);
4663 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4664 Instantiate_Package_Body
4667 Act_Decl => Act_Decl,
4668 Expander_Status => Expander_Active,
4669 Current_Sem_Unit => Current_Sem_Unit,
4670 Scope_Suppress => Scope_Suppress,
4671 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4672 Version => Ada_Version,
4673 Version_Pragma => Ada_Version_Pragma,
4674 Warnings => Save_Warnings,
4675 SPARK_Mode => Save_SM,
4676 SPARK_Mode_Pragma => Save_SMP)),
4677 Inlined_Body => True);
4683 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4685 -- Reset Generic_Instance flag so that use clauses can be installed
4686 -- in the proper order. (See Use_One_Package for effect of enclosing
4687 -- instances on processing of use clauses).
4689 for J in 1 .. N_Instances loop
4690 Set_Is_Generic_Instance (Instances (J), False);
4694 Install_Context (Curr_Comp);
4696 if Present (Curr_Scope)
4697 and then Is_Child_Unit (Curr_Scope)
4699 Push_Scope (Curr_Scope);
4700 Set_Is_Immediately_Visible (Curr_Scope);
4702 -- Finally, restore inner scopes as well
4704 for J in reverse 1 .. Num_Inner loop
4705 Push_Scope (Inner_Scopes (J));
4709 Restore_Scope_Stack (List, Handle_Use => False);
4711 if Present (Curr_Scope)
4713 (In_Private_Part (Curr_Scope)
4714 or else In_Package_Body (Curr_Scope))
4716 -- Install private declaration of ancestor units, which are
4717 -- currently available. Restore_Scope_Stack and Install_Context
4718 -- only install the visible part of parents.
4723 Par := Scope (Curr_Scope);
4724 while (Present (Par)) and then Par /= Standard_Standard loop
4725 Install_Private_Declarations (Par);
4732 -- Restore use clauses. For a child unit, use clauses in the parents
4733 -- are restored when installing the context, so only those in inner
4734 -- scopes (and those local to the child unit itself) need to be
4735 -- installed explicitly.
4737 if Is_Child_Unit (Curr_Unit) and then Removed then
4738 for J in reverse 1 .. Num_Inner + 1 loop
4739 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4741 Install_Use_Clauses (Use_Clauses (J));
4745 for J in reverse 1 .. Num_Scopes loop
4746 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4748 Install_Use_Clauses (Use_Clauses (J));
4752 -- Restore status of instances. If one of them is a body, make its
4753 -- local entities visible again.
4760 for J in 1 .. N_Instances loop
4761 Inst := Instances (J);
4762 Set_Is_Generic_Instance (Inst, True);
4764 if In_Package_Body (Inst)
4765 or else Ekind_In (S, E_Procedure, E_Function)
4767 E := First_Entity (Instances (J));
4768 while Present (E) loop
4769 Set_Is_Immediately_Visible (E);
4776 -- If generic unit is in current unit, current context is correct. Note
4777 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4778 -- enclosing scopes were removed.
4781 Instantiate_Package_Body
4784 Act_Decl => Act_Decl,
4785 Expander_Status => Expander_Active,
4786 Current_Sem_Unit => Current_Sem_Unit,
4787 Scope_Suppress => Scope_Suppress,
4788 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4789 Version => Ada_Version,
4790 Version_Pragma => Ada_Version_Pragma,
4791 Warnings => Save_Warnings,
4792 SPARK_Mode => SPARK_Mode,
4793 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4794 Inlined_Body => True);
4796 end Inline_Instance_Body;
4798 -------------------------------------
4799 -- Analyze_Procedure_Instantiation --
4800 -------------------------------------
4802 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4804 Analyze_Subprogram_Instantiation (N, E_Procedure);
4805 end Analyze_Procedure_Instantiation;
4807 -----------------------------------
4808 -- Need_Subprogram_Instance_Body --
4809 -----------------------------------
4811 function Need_Subprogram_Instance_Body
4813 Subp : Entity_Id) return Boolean
4816 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4817 -- Return True if E is an inlined subprogram, an inlined renaming or a
4818 -- subprogram nested in an inlined subprogram. The inlining machinery
4819 -- totally disregards nested subprograms since it considers that they
4820 -- will always be compiled if the parent is (see Inline.Is_Nested).
4822 ------------------------------------
4823 -- Is_Inlined_Or_Child_Of_Inlined --
4824 ------------------------------------
4826 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4830 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4835 while Scop /= Standard_Standard loop
4836 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4840 Scop := Scope (Scop);
4844 end Is_Inlined_Or_Child_Of_Inlined;
4847 -- Must be in the main unit or inlined (or child of inlined)
4849 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4851 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4853 and then (Operating_Mode = Generate_Code
4854 or else (Operating_Mode = Check_Semantics
4855 and then (ASIS_Mode or GNATprove_Mode)))
4857 -- The body is needed when generating code (full expansion), in ASIS
4858 -- mode for other tools, and in GNATprove mode (special expansion) for
4859 -- formal verification of the body itself.
4861 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4863 -- No point in inlining if ABE is inevitable
4865 and then not ABE_Is_Certain (N)
4867 -- Or if subprogram is eliminated
4869 and then not Is_Eliminated (Subp)
4871 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4874 -- Here if not inlined, or we ignore the inlining
4879 end Need_Subprogram_Instance_Body;
4881 --------------------------------------
4882 -- Analyze_Subprogram_Instantiation --
4883 --------------------------------------
4885 -- WARNING: This routine manages Ghost regions. Return statements must be
4886 -- replaced by gotos which jump to the end of the routine and restore the
4889 procedure Analyze_Subprogram_Instantiation
4893 Loc : constant Source_Ptr := Sloc (N);
4894 Gen_Id : constant Node_Id := Name (N);
4896 Anon_Id : constant Entity_Id :=
4897 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4898 Chars => New_External_Name
4899 (Chars (Defining_Entity (N)), 'R'));
4901 Act_Decl_Id : Entity_Id;
4906 Env_Installed : Boolean := False;
4907 Gen_Unit : Entity_Id;
4909 Pack_Id : Entity_Id;
4910 Parent_Installed : Boolean := False;
4912 Renaming_List : List_Id;
4913 -- The list of declarations that link formals and actuals of the
4914 -- instance. These are subtype declarations for formal types, and
4915 -- renaming declarations for other formals. The subprogram declaration
4916 -- for the instance is then appended to the list, and the last item on
4917 -- the list is the renaming declaration for the instance.
4919 procedure Analyze_Instance_And_Renamings;
4920 -- The instance must be analyzed in a context that includes the mappings
4921 -- of generic parameters into actuals. We create a package declaration
4922 -- for this purpose, and a subprogram with an internal name within the
4923 -- package. The subprogram instance is simply an alias for the internal
4924 -- subprogram, declared in the current scope.
4926 procedure Build_Subprogram_Renaming;
4927 -- If the subprogram is recursive, there are occurrences of the name of
4928 -- the generic within the body, which must resolve to the current
4929 -- instance. We add a renaming declaration after the declaration, which
4930 -- is available in the instance body, as well as in the analysis of
4931 -- aspects that appear in the generic. This renaming declaration is
4932 -- inserted after the instance declaration which it renames.
4934 ------------------------------------
4935 -- Analyze_Instance_And_Renamings --
4936 ------------------------------------
4938 procedure Analyze_Instance_And_Renamings is
4939 Def_Ent : constant Entity_Id := Defining_Entity (N);
4940 Pack_Decl : Node_Id;
4943 if Nkind (Parent (N)) = N_Compilation_Unit then
4945 -- For the case of a compilation unit, the container package has
4946 -- the same name as the instantiation, to insure that the binder
4947 -- calls the elaboration procedure with the right name. Copy the
4948 -- entity of the instance, which may have compilation level flags
4949 -- (e.g. Is_Child_Unit) set.
4951 Pack_Id := New_Copy (Def_Ent);
4954 -- Otherwise we use the name of the instantiation concatenated
4955 -- with its source position to ensure uniqueness if there are
4956 -- several instantiations with the same name.
4959 Make_Defining_Identifier (Loc,
4960 Chars => New_External_Name
4961 (Related_Id => Chars (Def_Ent),
4963 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4967 Make_Package_Declaration (Loc,
4968 Specification => Make_Package_Specification (Loc,
4969 Defining_Unit_Name => Pack_Id,
4970 Visible_Declarations => Renaming_List,
4971 End_Label => Empty));
4973 Set_Instance_Spec (N, Pack_Decl);
4974 Set_Is_Generic_Instance (Pack_Id);
4975 Set_Debug_Info_Needed (Pack_Id);
4977 -- Case of not a compilation unit
4979 if Nkind (Parent (N)) /= N_Compilation_Unit then
4980 Mark_Rewrite_Insertion (Pack_Decl);
4981 Insert_Before (N, Pack_Decl);
4982 Set_Has_Completion (Pack_Id);
4984 -- Case of an instantiation that is a compilation unit
4986 -- Place declaration on current node so context is complete for
4987 -- analysis (including nested instantiations), and for use in a
4988 -- context_clause (see Analyze_With_Clause).
4991 Set_Unit (Parent (N), Pack_Decl);
4992 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4995 Analyze (Pack_Decl);
4996 Check_Formal_Packages (Pack_Id);
4997 Set_Is_Generic_Instance (Pack_Id, False);
4999 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5002 -- Body of the enclosing package is supplied when instantiating the
5003 -- subprogram body, after semantic analysis is completed.
5005 if Nkind (Parent (N)) = N_Compilation_Unit then
5007 -- Remove package itself from visibility, so it does not
5008 -- conflict with subprogram.
5010 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5012 -- Set name and scope of internal subprogram so that the proper
5013 -- external name will be generated. The proper scope is the scope
5014 -- of the wrapper package. We need to generate debugging info for
5015 -- the internal subprogram, so set flag accordingly.
5017 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5018 Set_Scope (Anon_Id, Scope (Pack_Id));
5020 -- Mark wrapper package as referenced, to avoid spurious warnings
5021 -- if the instantiation appears in various with_ clauses of
5022 -- subunits of the main unit.
5024 Set_Referenced (Pack_Id);
5027 Set_Is_Generic_Instance (Anon_Id);
5028 Set_Debug_Info_Needed (Anon_Id);
5029 Act_Decl_Id := New_Copy (Anon_Id);
5031 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5032 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5033 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5035 -- Subprogram instance comes from source only if generic does
5037 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5039 -- If the instance is a child unit, mark the Id accordingly. Mark
5040 -- the anonymous entity as well, which is the real subprogram and
5041 -- which is used when the instance appears in a context clause.
5042 -- Similarly, propagate the Is_Eliminated flag to handle properly
5043 -- nested eliminated subprograms.
5045 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5046 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5047 New_Overloaded_Entity (Act_Decl_Id);
5048 Check_Eliminated (Act_Decl_Id);
5049 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5051 -- In compilation unit case, kill elaboration checks on the
5052 -- instantiation, since they are never needed -- the body is
5053 -- instantiated at the same point as the spec.
5055 if Nkind (Parent (N)) = N_Compilation_Unit then
5056 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5057 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5058 Set_Is_Compilation_Unit (Anon_Id);
5060 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5063 -- The instance is not a freezing point for the new subprogram.
5064 -- The anonymous subprogram may have a freeze node, created for
5065 -- some delayed aspects. This freeze node must not be inherited
5066 -- by the visible subprogram entity.
5068 Set_Is_Frozen (Act_Decl_Id, False);
5069 Set_Freeze_Node (Act_Decl_Id, Empty);
5071 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5072 Valid_Operator_Definition (Act_Decl_Id);
5075 Set_Alias (Act_Decl_Id, Anon_Id);
5076 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5077 Set_Has_Completion (Act_Decl_Id);
5078 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5080 if Nkind (Parent (N)) = N_Compilation_Unit then
5081 Set_Body_Required (Parent (N), False);
5083 end Analyze_Instance_And_Renamings;
5085 -------------------------------
5086 -- Build_Subprogram_Renaming --
5087 -------------------------------
5089 procedure Build_Subprogram_Renaming is
5090 Renaming_Decl : Node_Id;
5091 Unit_Renaming : Node_Id;
5095 Make_Subprogram_Renaming_Declaration (Loc,
5098 (Specification (Original_Node (Gen_Decl)),
5100 Instantiating => True),
5101 Name => New_Occurrence_Of (Anon_Id, Loc));
5103 -- The generic may be a a child unit. The renaming needs an
5104 -- identifier with the proper name.
5106 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5107 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5109 -- If there is a formal subprogram with the same name as the unit
5110 -- itself, do not add this renaming declaration, to prevent
5111 -- ambiguities when there is a call with that name in the body.
5112 -- This is a partial and ugly fix for one ACATS test. ???
5114 Renaming_Decl := First (Renaming_List);
5115 while Present (Renaming_Decl) loop
5116 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5118 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5123 Next (Renaming_Decl);
5126 if No (Renaming_Decl) then
5127 Append (Unit_Renaming, Renaming_List);
5129 end Build_Subprogram_Renaming;
5133 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
5134 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
5136 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5137 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5138 -- Save the SPARK_Mode-related data for restore on exit
5140 Mode : Ghost_Mode_Type;
5141 Mode_Set : Boolean := False;
5143 Vis_Prims_List : Elist_Id := No_Elist;
5144 -- List of primitives made temporarily visible in the instantiation
5145 -- to match the visibility of the formal type
5147 -- Start of processing for Analyze_Subprogram_Instantiation
5150 Check_SPARK_05_Restriction ("generic is not allowed", N);
5152 -- Very first thing: check for special Text_IO unit in case we are
5153 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5154 -- such an instantiation is bogus (these are packages, not subprograms),
5155 -- but we get a better error message if we do this.
5157 Check_Text_IO_Special_Unit (Gen_Id);
5159 -- Make node global for error reporting
5161 Instantiation_Node := N;
5163 -- For package instantiations we turn off style checks, because they
5164 -- will have been emitted in the generic. For subprogram instantiations
5165 -- we want to apply at least the check on overriding indicators so we
5166 -- do not modify the style check status.
5168 -- The renaming declarations for the actuals do not come from source and
5169 -- will not generate spurious warnings.
5171 Preanalyze_Actuals (N);
5174 Env_Installed := True;
5175 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5176 Gen_Unit := Entity (Gen_Id);
5178 -- A subprogram instantiation is Ghost when it is subject to pragma
5179 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5180 -- that any nodes generated during analysis and expansion are marked as
5183 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
5186 Generate_Reference (Gen_Unit, Gen_Id);
5188 if Nkind (Gen_Id) = N_Identifier
5189 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5192 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5195 if Etype (Gen_Unit) = Any_Type then
5200 -- Verify that it is a generic subprogram of the right kind, and that
5201 -- it does not lead to a circular instantiation.
5203 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5205 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5207 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5209 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5211 elsif In_Open_Scopes (Gen_Unit) then
5212 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5215 Set_Entity (Gen_Id, Gen_Unit);
5216 Set_Is_Instantiated (Gen_Unit);
5218 if In_Extended_Main_Source_Unit (N) then
5219 Generate_Reference (Gen_Unit, N);
5222 -- If renaming, get original unit
5224 if Present (Renamed_Object (Gen_Unit))
5225 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5228 Gen_Unit := Renamed_Object (Gen_Unit);
5229 Set_Is_Instantiated (Gen_Unit);
5230 Generate_Reference (Gen_Unit, N);
5233 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5234 Error_Msg_Node_2 := Current_Scope;
5236 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5237 Circularity_Detected := True;
5238 Restore_Hidden_Primitives (Vis_Prims_List);
5242 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5244 -- Initialize renamings map, for error checking
5246 Generic_Renamings.Set_Last (0);
5247 Generic_Renamings_HTable.Reset;
5249 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5251 -- Copy original generic tree, to produce text for instantiation
5255 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5257 -- Inherit overriding indicator from instance node
5259 Act_Spec := Specification (Act_Tree);
5260 Set_Must_Override (Act_Spec, Must_Override (N));
5261 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5264 Analyze_Associations
5266 Formals => Generic_Formal_Declarations (Act_Tree),
5267 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5269 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5271 -- The subprogram itself cannot contain a nested instance, so the
5272 -- current parent is left empty.
5274 Set_Instance_Env (Gen_Unit, Empty);
5276 -- Build the subprogram declaration, which does not appear in the
5277 -- generic template, and give it a sloc consistent with that of the
5280 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5281 Set_Generic_Parent (Act_Spec, Gen_Unit);
5283 Make_Subprogram_Declaration (Sloc (Act_Spec),
5284 Specification => Act_Spec);
5286 -- The aspects have been copied previously, but they have to be
5287 -- linked explicitly to the new subprogram declaration. Explicit
5288 -- pre/postconditions on the instance are analyzed below, in a
5291 Move_Aspects (Act_Tree, To => Act_Decl);
5292 Set_Categorization_From_Pragmas (Act_Decl);
5294 if Parent_Installed then
5298 Append (Act_Decl, Renaming_List);
5300 -- Contract-related source pragmas that follow a generic subprogram
5301 -- must be instantiated explicitly because they are not part of the
5302 -- subprogram template.
5304 Instantiate_Subprogram_Contract
5305 (Original_Node (Gen_Decl), Renaming_List);
5307 Build_Subprogram_Renaming;
5308 Analyze_Instance_And_Renamings;
5310 -- If the generic is marked Import (Intrinsic), then so is the
5311 -- instance. This indicates that there is no body to instantiate. If
5312 -- generic is marked inline, so it the instance, and the anonymous
5313 -- subprogram it renames. If inlined, or else if inlining is enabled
5314 -- for the compilation, we generate the instance body even if it is
5315 -- not within the main unit.
5317 if Is_Intrinsic_Subprogram (Gen_Unit) then
5318 Set_Is_Intrinsic_Subprogram (Anon_Id);
5319 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5321 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5322 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5326 -- Inherit convention from generic unit. Intrinsic convention, as for
5327 -- an instance of unchecked conversion, is not inherited because an
5328 -- explicit Ada instance has been created.
5330 if Has_Convention_Pragma (Gen_Unit)
5331 and then Convention (Gen_Unit) /= Convention_Intrinsic
5333 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5334 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5337 Generate_Definition (Act_Decl_Id);
5339 -- Inherit all inlining-related flags which apply to the generic in
5340 -- the subprogram and its declaration.
5342 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5343 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5345 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5346 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5348 Set_Has_Pragma_Inline_Always
5349 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5350 Set_Has_Pragma_Inline_Always
5351 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5353 -- If the context of the instance is subject to SPARK_Mode "off" or
5354 -- the annotation is altogether missing, set the global flag which
5355 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5358 if SPARK_Mode /= On then
5359 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5361 -- Mark both the instance spec and the anonymous package in case
5362 -- the body is instantiated at a later pass. This preserves the
5363 -- original context in effect for the body.
5365 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5366 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5369 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5370 Check_Elab_Instantiation (N);
5373 if Is_Dispatching_Operation (Act_Decl_Id)
5374 and then Ada_Version >= Ada_2005
5380 Formal := First_Formal (Act_Decl_Id);
5381 while Present (Formal) loop
5382 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5383 and then Is_Controlling_Formal (Formal)
5384 and then not Can_Never_Be_Null (Formal)
5387 ("access parameter& is controlling,", N, Formal);
5389 ("\corresponding parameter of & must be explicitly "
5390 & "null-excluding", N, Gen_Id);
5393 Next_Formal (Formal);
5398 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5400 Validate_Categorization_Dependency (N, Act_Decl_Id);
5402 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5403 Inherit_Context (Gen_Decl, N);
5405 Restore_Private_Views (Pack_Id, False);
5407 -- If the context requires a full instantiation, mark node for
5408 -- subsequent construction of the body.
5410 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5411 Check_Forward_Instantiation (Gen_Decl);
5413 -- The wrapper package is always delayed, because it does not
5414 -- constitute a freeze point, but to insure that the freeze node
5415 -- is placed properly, it is created directly when instantiating
5416 -- the body (otherwise the freeze node might appear to early for
5417 -- nested instantiations). For ASIS purposes, indicate that the
5418 -- wrapper package has replaced the instantiation node.
5420 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5421 Rewrite (N, Unit (Parent (N)));
5422 Set_Unit (Parent (N), N);
5425 -- Replace instance node for library-level instantiations of
5426 -- intrinsic subprograms, for ASIS use.
5428 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5429 Rewrite (N, Unit (Parent (N)));
5430 Set_Unit (Parent (N), N);
5433 if Parent_Installed then
5437 Restore_Hidden_Primitives (Vis_Prims_List);
5439 Env_Installed := False;
5440 Generic_Renamings.Set_Last (0);
5441 Generic_Renamings_HTable.Reset;
5445 if Has_Aspects (N) then
5446 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5449 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
5450 SPARK_Mode := Save_SM;
5451 SPARK_Mode_Pragma := Save_SMP;
5454 Restore_Ghost_Mode (Mode);
5458 when Instantiation_Error =>
5459 if Parent_Installed then
5463 if Env_Installed then
5467 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
5468 SPARK_Mode := Save_SM;
5469 SPARK_Mode_Pragma := Save_SMP;
5472 Restore_Ghost_Mode (Mode);
5474 end Analyze_Subprogram_Instantiation;
5476 -------------------------
5477 -- Get_Associated_Node --
5478 -------------------------
5480 function Get_Associated_Node (N : Node_Id) return Node_Id is
5484 Assoc := Associated_Node (N);
5486 if Nkind (Assoc) /= Nkind (N) then
5489 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5493 -- If the node is part of an inner generic, it may itself have been
5494 -- remapped into a further generic copy. Associated_Node is otherwise
5495 -- used for the entity of the node, and will be of a different node
5496 -- kind, or else N has been rewritten as a literal or function call.
5498 while Present (Associated_Node (Assoc))
5499 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5501 Assoc := Associated_Node (Assoc);
5504 -- Follow and additional link in case the final node was rewritten.
5505 -- This can only happen with nested generic units.
5507 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5508 and then Present (Associated_Node (Assoc))
5509 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5510 N_Explicit_Dereference,
5515 Assoc := Associated_Node (Assoc);
5518 -- An additional special case: an unconstrained type in an object
5519 -- declaration may have been rewritten as a local subtype constrained
5520 -- by the expression in the declaration. We need to recover the
5521 -- original entity which may be global.
5523 if Present (Original_Node (Assoc))
5524 and then Nkind (Parent (N)) = N_Object_Declaration
5526 Assoc := Original_Node (Assoc);
5531 end Get_Associated_Node;
5533 ----------------------------
5534 -- Build_Function_Wrapper --
5535 ----------------------------
5537 function Build_Function_Wrapper
5538 (Formal_Subp : Entity_Id;
5539 Actual_Subp : Entity_Id) return Node_Id
5541 Loc : constant Source_Ptr := Sloc (Current_Scope);
5542 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5545 Func_Name : Node_Id;
5547 Parm_Type : Node_Id;
5548 Profile : List_Id := New_List;
5555 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5557 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5558 Set_Ekind (Func, E_Function);
5559 Set_Is_Generic_Actual_Subprogram (Func);
5561 Actuals := New_List;
5562 Profile := New_List;
5564 Act_F := First_Formal (Actual_Subp);
5565 Form_F := First_Formal (Formal_Subp);
5566 while Present (Form_F) loop
5568 -- Create new formal for profile of wrapper, and add a reference
5569 -- to it in the list of actuals for the enclosing call. The name
5570 -- must be that of the formal in the formal subprogram, because
5571 -- calls to it in the generic body may use named associations.
5573 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5576 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5579 Make_Parameter_Specification (Loc,
5580 Defining_Identifier => New_F,
5581 Parameter_Type => Parm_Type));
5583 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5584 Next_Formal (Form_F);
5586 if Present (Act_F) then
5587 Next_Formal (Act_F);
5592 Make_Function_Specification (Loc,
5593 Defining_Unit_Name => Func,
5594 Parameter_Specifications => Profile,
5595 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5598 Make_Expression_Function (Loc,
5599 Specification => Spec,
5601 Make_Function_Call (Loc,
5603 Parameter_Associations => Actuals));
5606 end Build_Function_Wrapper;
5608 ----------------------------
5609 -- Build_Operator_Wrapper --
5610 ----------------------------
5612 function Build_Operator_Wrapper
5613 (Formal_Subp : Entity_Id;
5614 Actual_Subp : Entity_Id) return Node_Id
5616 Loc : constant Source_Ptr := Sloc (Current_Scope);
5617 Ret_Type : constant Entity_Id :=
5618 Get_Instance_Of (Etype (Formal_Subp));
5619 Op_Type : constant Entity_Id :=
5620 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5621 Is_Binary : constant Boolean :=
5622 Present (Next_Formal (First_Formal (Formal_Subp)));
5633 Op_Name := Chars (Actual_Subp);
5635 -- Create entities for wrapper function and its formals
5637 F1 := Make_Temporary (Loc, 'A');
5638 F2 := Make_Temporary (Loc, 'B');
5639 L := New_Occurrence_Of (F1, Loc);
5640 R := New_Occurrence_Of (F2, Loc);
5642 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5643 Set_Ekind (Func, E_Function);
5644 Set_Is_Generic_Actual_Subprogram (Func);
5647 Make_Function_Specification (Loc,
5648 Defining_Unit_Name => Func,
5649 Parameter_Specifications => New_List (
5650 Make_Parameter_Specification (Loc,
5651 Defining_Identifier => F1,
5652 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5653 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5656 Append_To (Parameter_Specifications (Spec),
5657 Make_Parameter_Specification (Loc,
5658 Defining_Identifier => F2,
5659 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5662 -- Build expression as a function call, or as an operator node
5663 -- that corresponds to the name of the actual, starting with
5664 -- binary operators.
5666 if Op_Name not in Any_Operator_Name then
5668 Make_Function_Call (Loc,
5670 New_Occurrence_Of (Actual_Subp, Loc),
5671 Parameter_Associations => New_List (L));
5674 Append_To (Parameter_Associations (Expr), R);
5679 elsif Is_Binary then
5680 if Op_Name = Name_Op_And then
5681 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5682 elsif Op_Name = Name_Op_Or then
5683 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5684 elsif Op_Name = Name_Op_Xor then
5685 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5686 elsif Op_Name = Name_Op_Eq then
5687 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5688 elsif Op_Name = Name_Op_Ne then
5689 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5690 elsif Op_Name = Name_Op_Le then
5691 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5692 elsif Op_Name = Name_Op_Gt then
5693 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5694 elsif Op_Name = Name_Op_Ge then
5695 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5696 elsif Op_Name = Name_Op_Lt then
5697 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5698 elsif Op_Name = Name_Op_Add then
5699 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5700 elsif Op_Name = Name_Op_Subtract then
5701 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5702 elsif Op_Name = Name_Op_Concat then
5703 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5704 elsif Op_Name = Name_Op_Multiply then
5705 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5706 elsif Op_Name = Name_Op_Divide then
5707 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5708 elsif Op_Name = Name_Op_Mod then
5709 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5710 elsif Op_Name = Name_Op_Rem then
5711 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5712 elsif Op_Name = Name_Op_Expon then
5713 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5719 if Op_Name = Name_Op_Add then
5720 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5721 elsif Op_Name = Name_Op_Subtract then
5722 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5723 elsif Op_Name = Name_Op_Abs then
5724 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5725 elsif Op_Name = Name_Op_Not then
5726 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5731 Make_Expression_Function (Loc,
5732 Specification => Spec,
5733 Expression => Expr);
5736 end Build_Operator_Wrapper;
5738 -------------------------------------------
5739 -- Build_Instance_Compilation_Unit_Nodes --
5740 -------------------------------------------
5742 procedure Build_Instance_Compilation_Unit_Nodes
5747 Decl_Cunit : Node_Id;
5748 Body_Cunit : Node_Id;
5750 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5751 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5754 -- A new compilation unit node is built for the instance declaration
5757 Make_Compilation_Unit (Sloc (N),
5758 Context_Items => Empty_List,
5760 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5762 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5764 -- The new compilation unit is linked to its body, but both share the
5765 -- same file, so we do not set Body_Required on the new unit so as not
5766 -- to create a spurious dependency on a non-existent body in the ali.
5767 -- This simplifies CodePeer unit traversal.
5769 -- We use the original instantiation compilation unit as the resulting
5770 -- compilation unit of the instance, since this is the main unit.
5772 Rewrite (N, Act_Body);
5774 -- Propagate the aspect specifications from the package body template to
5775 -- the instantiated version of the package body.
5777 if Has_Aspects (Act_Body) then
5778 Set_Aspect_Specifications
5779 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5782 Body_Cunit := Parent (N);
5784 -- The two compilation unit nodes are linked by the Library_Unit field
5786 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5787 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5789 -- Preserve the private nature of the package if needed
5791 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5793 -- If the instance is not the main unit, its context, categorization
5794 -- and elaboration entity are not relevant to the compilation.
5796 if Body_Cunit /= Cunit (Main_Unit) then
5797 Make_Instance_Unit (Body_Cunit, In_Main => False);
5801 -- The context clause items on the instantiation, which are now attached
5802 -- to the body compilation unit (since the body overwrote the original
5803 -- instantiation node), semantically belong on the spec, so copy them
5804 -- there. It's harmless to leave them on the body as well. In fact one
5805 -- could argue that they belong in both places.
5807 Citem := First (Context_Items (Body_Cunit));
5808 while Present (Citem) loop
5809 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5813 -- Propagate categorization flags on packages, so that they appear in
5814 -- the ali file for the spec of the unit.
5816 if Ekind (New_Main) = E_Package then
5817 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5818 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5819 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5820 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5821 Set_Is_Remote_Call_Interface
5822 (Old_Main, Is_Remote_Call_Interface (New_Main));
5825 -- Make entry in Units table, so that binder can generate call to
5826 -- elaboration procedure for body, if any.
5828 Make_Instance_Unit (Body_Cunit, In_Main => True);
5829 Main_Unit_Entity := New_Main;
5830 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5832 -- Build elaboration entity, since the instance may certainly generate
5833 -- elaboration code requiring a flag for protection.
5835 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5836 end Build_Instance_Compilation_Unit_Nodes;
5838 -----------------------------
5839 -- Check_Access_Definition --
5840 -----------------------------
5842 procedure Check_Access_Definition (N : Node_Id) is
5845 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5847 end Check_Access_Definition;
5849 -----------------------------------
5850 -- Check_Formal_Package_Instance --
5851 -----------------------------------
5853 -- If the formal has specific parameters, they must match those of the
5854 -- actual. Both of them are instances, and the renaming declarations for
5855 -- their formal parameters appear in the same order in both. The analyzed
5856 -- formal has been analyzed in the context of the current instance.
5858 procedure Check_Formal_Package_Instance
5859 (Formal_Pack : Entity_Id;
5860 Actual_Pack : Entity_Id)
5862 E1 : Entity_Id := First_Entity (Actual_Pack);
5863 E2 : Entity_Id := First_Entity (Formal_Pack);
5864 Prev_E1 : Entity_Id;
5869 procedure Check_Mismatch (B : Boolean);
5870 -- Common error routine for mismatch between the parameters of the
5871 -- actual instance and those of the formal package.
5873 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5874 -- The formal may come from a nested formal package, and the actual may
5875 -- have been constant-folded. To determine whether the two denote the
5876 -- same entity we may have to traverse several definitions to recover
5877 -- the ultimate entity that they refer to.
5879 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5880 -- The formal and the actual must be identical, but if both are
5881 -- given by attributes they end up renaming different generated bodies,
5882 -- and we must verify that the attributes themselves match.
5884 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5885 -- Similarly, if the formal comes from a nested formal package, the
5886 -- actual may designate the formal through multiple renamings, which
5887 -- have to be followed to determine the original variable in question.
5889 --------------------
5890 -- Check_Mismatch --
5891 --------------------
5893 procedure Check_Mismatch (B : Boolean) is
5894 -- A Formal_Type_Declaration for a derived private type is rewritten
5895 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5896 -- which is why we examine the original node.
5898 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5901 if Kind = N_Formal_Type_Declaration then
5904 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5905 N_Formal_Package_Declaration)
5906 or else Kind in N_Formal_Subprogram_Declaration
5910 -- Ada 2012: If both formal and actual are incomplete types they
5913 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5918 ("actual for & in actual instance does not match formal",
5919 Parent (Actual_Pack), E1);
5923 --------------------------------
5924 -- Same_Instantiated_Constant --
5925 --------------------------------
5927 function Same_Instantiated_Constant
5928 (E1, E2 : Entity_Id) return Boolean
5934 while Present (Ent) loop
5938 elsif Ekind (Ent) /= E_Constant then
5941 elsif Is_Entity_Name (Constant_Value (Ent)) then
5942 if Entity (Constant_Value (Ent)) = E1 then
5945 Ent := Entity (Constant_Value (Ent));
5948 -- The actual may be a constant that has been folded. Recover
5951 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5952 Ent := Entity (Original_Node (Constant_Value (Ent)));
5960 end Same_Instantiated_Constant;
5962 --------------------------------
5963 -- Same_Instantiated_Function --
5964 --------------------------------
5966 function Same_Instantiated_Function
5967 (E1, E2 : Entity_Id) return Boolean
5971 if Alias (E1) = Alias (E2) then
5974 elsif Present (Alias (E2)) then
5975 U1 := Original_Node (Unit_Declaration_Node (E1));
5976 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5978 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5979 and then Nkind (Name (U1)) = N_Attribute_Reference
5981 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5982 and then Nkind (Name (U2)) = N_Attribute_Reference
5985 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5989 end Same_Instantiated_Function;
5991 --------------------------------
5992 -- Same_Instantiated_Variable --
5993 --------------------------------
5995 function Same_Instantiated_Variable
5996 (E1, E2 : Entity_Id) return Boolean
5998 function Original_Entity (E : Entity_Id) return Entity_Id;
5999 -- Follow chain of renamings to the ultimate ancestor
6001 ---------------------
6002 -- Original_Entity --
6003 ---------------------
6005 function Original_Entity (E : Entity_Id) return Entity_Id is
6010 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6011 and then Present (Renamed_Object (Orig))
6012 and then Is_Entity_Name (Renamed_Object (Orig))
6014 Orig := Entity (Renamed_Object (Orig));
6018 end Original_Entity;
6020 -- Start of processing for Same_Instantiated_Variable
6023 return Ekind (E1) = Ekind (E2)
6024 and then Original_Entity (E1) = Original_Entity (E2);
6025 end Same_Instantiated_Variable;
6027 -- Start of processing for Check_Formal_Package_Instance
6031 while Present (E1) and then Present (E2) loop
6032 exit when Ekind (E1) = E_Package
6033 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6035 -- If the formal is the renaming of the formal package, this
6036 -- is the end of its formal part, which may occur before the
6037 -- end of the formal part in the actual in the presence of
6038 -- defaulted parameters in the formal package.
6040 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6041 and then Renamed_Entity (E2) = Scope (E2);
6043 -- The analysis of the actual may generate additional internal
6044 -- entities. If the formal is defaulted, there is no corresponding
6045 -- analysis and the internal entities must be skipped, until we
6046 -- find corresponding entities again.
6048 if Comes_From_Source (E2)
6049 and then not Comes_From_Source (E1)
6050 and then Chars (E1) /= Chars (E2)
6052 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6060 -- Entities may be declared without full declaration, such as
6061 -- itypes and predefined operators (concatenation for arrays, eg).
6062 -- Skip it and keep the formal entity to find a later match for it.
6064 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6068 -- If the formal entity comes from a formal declaration, it was
6069 -- defaulted in the formal package, and no check is needed on it.
6071 elsif Nkind_In (Original_Node (Parent (E2)),
6072 N_Formal_Object_Declaration,
6073 N_Formal_Type_Declaration)
6075 -- If the formal is a tagged type the corresponding class-wide
6076 -- type has been generated as well, and it must be skipped.
6078 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6084 -- Ditto for defaulted formal subprograms.
6086 elsif Is_Overloadable (E1)
6087 and then Nkind (Unit_Declaration_Node (E2)) in
6088 N_Formal_Subprogram_Declaration
6092 elsif Is_Type (E1) then
6094 -- Subtypes must statically match. E1, E2 are the local entities
6095 -- that are subtypes of the actuals. Itypes generated for other
6096 -- parameters need not be checked, the check will be performed
6097 -- on the parameters themselves.
6099 -- If E2 is a formal type declaration, it is a defaulted parameter
6100 -- and needs no checking.
6102 if not Is_Itype (E1) and then not Is_Itype (E2) then
6105 or else Etype (E1) /= Etype (E2)
6106 or else not Subtypes_Statically_Match (E1, E2));
6109 elsif Ekind (E1) = E_Constant then
6111 -- IN parameters must denote the same static value, or the same
6112 -- constant, or the literal null.
6114 Expr1 := Expression (Parent (E1));
6116 if Ekind (E2) /= E_Constant then
6117 Check_Mismatch (True);
6120 Expr2 := Expression (Parent (E2));
6123 if Is_OK_Static_Expression (Expr1) then
6124 if not Is_OK_Static_Expression (Expr2) then
6125 Check_Mismatch (True);
6127 elsif Is_Discrete_Type (Etype (E1)) then
6129 V1 : constant Uint := Expr_Value (Expr1);
6130 V2 : constant Uint := Expr_Value (Expr2);
6132 Check_Mismatch (V1 /= V2);
6135 elsif Is_Real_Type (Etype (E1)) then
6137 V1 : constant Ureal := Expr_Value_R (Expr1);
6138 V2 : constant Ureal := Expr_Value_R (Expr2);
6140 Check_Mismatch (V1 /= V2);
6143 elsif Is_String_Type (Etype (E1))
6144 and then Nkind (Expr1) = N_String_Literal
6146 if Nkind (Expr2) /= N_String_Literal then
6147 Check_Mismatch (True);
6150 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6154 elsif Is_Entity_Name (Expr1) then
6155 if Is_Entity_Name (Expr2) then
6156 if Entity (Expr1) = Entity (Expr2) then
6160 (not Same_Instantiated_Constant
6161 (Entity (Expr1), Entity (Expr2)));
6165 Check_Mismatch (True);
6168 elsif Is_Entity_Name (Original_Node (Expr1))
6169 and then Is_Entity_Name (Expr2)
6170 and then Same_Instantiated_Constant
6171 (Entity (Original_Node (Expr1)), Entity (Expr2))
6175 elsif Nkind (Expr1) = N_Null then
6176 Check_Mismatch (Nkind (Expr1) /= N_Null);
6179 Check_Mismatch (True);
6182 elsif Ekind (E1) = E_Variable then
6183 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6185 elsif Ekind (E1) = E_Package then
6187 (Ekind (E1) /= Ekind (E2)
6188 or else Renamed_Object (E1) /= Renamed_Object (E2));
6190 elsif Is_Overloadable (E1) then
6192 -- Verify that the actual subprograms match. Note that actuals
6193 -- that are attributes are rewritten as subprograms. If the
6194 -- subprogram in the formal package is defaulted, no check is
6195 -- needed. Note that this can only happen in Ada 2005 when the
6196 -- formal package can be partially parameterized.
6198 if Nkind (Unit_Declaration_Node (E1)) =
6199 N_Subprogram_Renaming_Declaration
6200 and then From_Default (Unit_Declaration_Node (E1))
6204 -- If the formal package has an "others" box association that
6205 -- covers this formal, there is no need for a check either.
6207 elsif Nkind (Unit_Declaration_Node (E2)) in
6208 N_Formal_Subprogram_Declaration
6209 and then Box_Present (Unit_Declaration_Node (E2))
6213 -- No check needed if subprogram is a defaulted null procedure
6215 elsif No (Alias (E2))
6216 and then Ekind (E2) = E_Procedure
6218 Null_Present (Specification (Unit_Declaration_Node (E2)))
6222 -- Otherwise the actual in the formal and the actual in the
6223 -- instantiation of the formal must match, up to renamings.
6227 (Ekind (E2) /= Ekind (E1)
6228 or else not Same_Instantiated_Function (E1, E2));
6232 raise Program_Error;
6240 end Check_Formal_Package_Instance;
6242 ---------------------------
6243 -- Check_Formal_Packages --
6244 ---------------------------
6246 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6248 Formal_P : Entity_Id;
6249 Formal_Decl : Node_Id;
6252 -- Iterate through the declarations in the instance, looking for package
6253 -- renaming declarations that denote instances of formal packages. Stop
6254 -- when we find the renaming of the current package itself. The
6255 -- declaration for a formal package without a box is followed by an
6256 -- internal entity that repeats the instantiation.
6258 E := First_Entity (P_Id);
6259 while Present (E) loop
6260 if Ekind (E) = E_Package then
6261 if Renamed_Object (E) = P_Id then
6264 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6268 Formal_Decl := Parent (Associated_Formal_Package (E));
6270 -- Nothing to check if the formal has a box or an others_clause
6271 -- (necessarily with a box).
6273 if Box_Present (Formal_Decl) then
6276 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6279 -- The internal validating package was generated but formal
6280 -- and instance are known to be compatible.
6282 Formal_P := Next_Entity (E);
6283 Remove (Unit_Declaration_Node (Formal_P));
6286 Formal_P := Next_Entity (E);
6287 Check_Formal_Package_Instance (Formal_P, E);
6289 -- After checking, remove the internal validating package.
6290 -- It is only needed for semantic checks, and as it may
6291 -- contain generic formal declarations it should not reach
6294 Remove (Unit_Declaration_Node (Formal_P));
6301 end Check_Formal_Packages;
6303 ---------------------------------
6304 -- Check_Forward_Instantiation --
6305 ---------------------------------
6307 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6309 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6312 -- The instantiation appears before the generic body if we are in the
6313 -- scope of the unit containing the generic, either in its spec or in
6314 -- the package body, and before the generic body.
6316 if Ekind (Gen_Comp) = E_Package_Body then
6317 Gen_Comp := Spec_Entity (Gen_Comp);
6320 if In_Open_Scopes (Gen_Comp)
6321 and then No (Corresponding_Body (Decl))
6326 and then not Is_Compilation_Unit (S)
6327 and then not Is_Child_Unit (S)
6329 if Ekind (S) = E_Package then
6330 Set_Has_Forward_Instantiation (S);
6336 end Check_Forward_Instantiation;
6338 ---------------------------
6339 -- Check_Generic_Actuals --
6340 ---------------------------
6342 -- The visibility of the actuals may be different between the point of
6343 -- generic instantiation and the instantiation of the body.
6345 procedure Check_Generic_Actuals
6346 (Instance : Entity_Id;
6347 Is_Formal_Box : Boolean)
6352 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6353 -- For a formal that is an array type, the component type is often a
6354 -- previous formal in the same unit. The privacy status of the component
6355 -- type will have been examined earlier in the traversal of the
6356 -- corresponding actuals, and this status should not be modified for
6357 -- the array (sub)type itself. However, if the base type of the array
6358 -- (sub)type is private, its full view must be restored in the body to
6359 -- be consistent with subsequent index subtypes, etc.
6361 -- To detect this case we have to rescan the list of formals, which is
6362 -- usually short enough to ignore the resulting inefficiency.
6364 -----------------------------
6365 -- Denotes_Previous_Actual --
6366 -----------------------------
6368 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6372 Prev := First_Entity (Instance);
6373 while Present (Prev) loop
6375 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6376 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6377 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6390 end Denotes_Previous_Actual;
6392 -- Start of processing for Check_Generic_Actuals
6395 E := First_Entity (Instance);
6396 while Present (E) loop
6398 and then Nkind (Parent (E)) = N_Subtype_Declaration
6399 and then Scope (Etype (E)) /= Instance
6400 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6402 if Is_Array_Type (E)
6403 and then not Is_Private_Type (Etype (E))
6404 and then Denotes_Previous_Actual (Component_Type (E))
6408 Check_Private_View (Subtype_Indication (Parent (E)));
6411 Set_Is_Generic_Actual_Type (E, True);
6412 Set_Is_Hidden (E, False);
6413 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6415 -- We constructed the generic actual type as a subtype of the
6416 -- supplied type. This means that it normally would not inherit
6417 -- subtype specific attributes of the actual, which is wrong for
6418 -- the generic case.
6420 Astype := Ancestor_Subtype (E);
6424 -- This can happen when E is an itype that is the full view of
6425 -- a private type completed, e.g. with a constrained array. In
6426 -- that case, use the first subtype, which will carry size
6427 -- information. The base type itself is unconstrained and will
6430 Astype := First_Subtype (E);
6433 Set_Size_Info (E, (Astype));
6434 Set_RM_Size (E, RM_Size (Astype));
6435 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6437 if Is_Discrete_Or_Fixed_Point_Type (E) then
6438 Set_RM_Size (E, RM_Size (Astype));
6440 -- In nested instances, the base type of an access actual may
6441 -- itself be private, and need to be exchanged.
6443 elsif Is_Access_Type (E)
6444 and then Is_Private_Type (Etype (E))
6447 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6450 elsif Ekind (E) = E_Package then
6452 -- If this is the renaming for the current instance, we're done.
6453 -- Otherwise it is a formal package. If the corresponding formal
6454 -- was declared with a box, the (instantiations of the) generic
6455 -- formal part are also visible. Otherwise, ignore the entity
6456 -- created to validate the actuals.
6458 if Renamed_Object (E) = Instance then
6461 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6464 -- The visibility of a formal of an enclosing generic is already
6467 elsif Denotes_Formal_Package (E) then
6470 elsif Present (Associated_Formal_Package (E))
6471 and then not Is_Generic_Formal (E)
6473 if Box_Present (Parent (Associated_Formal_Package (E))) then
6474 Check_Generic_Actuals (Renamed_Object (E), True);
6477 Check_Generic_Actuals (Renamed_Object (E), False);
6480 Set_Is_Hidden (E, False);
6483 -- If this is a subprogram instance (in a wrapper package) the
6484 -- actual is fully visible.
6486 elsif Is_Wrapper_Package (Instance) then
6487 Set_Is_Hidden (E, False);
6489 -- If the formal package is declared with a box, or if the formal
6490 -- parameter is defaulted, it is visible in the body.
6492 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6493 Set_Is_Hidden (E, False);
6496 if Ekind (E) = E_Constant then
6498 -- If the type of the actual is a private type declared in the
6499 -- enclosing scope of the generic unit, the body of the generic
6500 -- sees the full view of the type (because it has to appear in
6501 -- the corresponding package body). If the type is private now,
6502 -- exchange views to restore the proper visiblity in the instance.
6505 Typ : constant Entity_Id := Base_Type (Etype (E));
6506 -- The type of the actual
6511 Parent_Scope : Entity_Id;
6512 -- The enclosing scope of the generic unit
6515 if Is_Wrapper_Package (Instance) then
6519 (Unit_Declaration_Node
6520 (Related_Instance (Instance))));
6523 Generic_Parent (Package_Specification (Instance));
6526 Parent_Scope := Scope (Gen_Id);
6528 -- The exchange is only needed if the generic is defined
6529 -- within a package which is not a common ancestor of the
6530 -- scope of the instance, and is not already in scope.
6532 if Is_Private_Type (Typ)
6533 and then Scope (Typ) = Parent_Scope
6534 and then Scope (Instance) /= Parent_Scope
6535 and then Ekind (Parent_Scope) = E_Package
6536 and then not Is_Child_Unit (Gen_Id)
6540 -- If the type of the entity is a subtype, it may also have
6541 -- to be made visible, together with the base type of its
6542 -- full view, after exchange.
6544 if Is_Private_Type (Etype (E)) then
6545 Switch_View (Etype (E));
6546 Switch_View (Base_Type (Etype (E)));
6554 end Check_Generic_Actuals;
6556 ------------------------------
6557 -- Check_Generic_Child_Unit --
6558 ------------------------------
6560 procedure Check_Generic_Child_Unit
6562 Parent_Installed : in out Boolean)
6564 Loc : constant Source_Ptr := Sloc (Gen_Id);
6565 Gen_Par : Entity_Id := Empty;
6567 Inst_Par : Entity_Id;
6570 function Find_Generic_Child
6572 Id : Node_Id) return Entity_Id;
6573 -- Search generic parent for possible child unit with the given name
6575 function In_Enclosing_Instance return Boolean;
6576 -- Within an instance of the parent, the child unit may be denoted by
6577 -- a simple name, or an abbreviated expanded name. Examine enclosing
6578 -- scopes to locate a possible parent instantiation.
6580 ------------------------
6581 -- Find_Generic_Child --
6582 ------------------------
6584 function Find_Generic_Child
6586 Id : Node_Id) return Entity_Id
6591 -- If entity of name is already set, instance has already been
6592 -- resolved, e.g. in an enclosing instantiation.
6594 if Present (Entity (Id)) then
6595 if Scope (Entity (Id)) = Scop then
6602 E := First_Entity (Scop);
6603 while Present (E) loop
6604 if Chars (E) = Chars (Id)
6605 and then Is_Child_Unit (E)
6607 if Is_Child_Unit (E)
6608 and then not Is_Visible_Lib_Unit (E)
6611 ("generic child unit& is not visible", Gen_Id, E);
6623 end Find_Generic_Child;
6625 ---------------------------
6626 -- In_Enclosing_Instance --
6627 ---------------------------
6629 function In_Enclosing_Instance return Boolean is
6630 Enclosing_Instance : Node_Id;
6631 Instance_Decl : Node_Id;
6634 -- We do not inline any call that contains instantiations, except
6635 -- for instantiations of Unchecked_Conversion, so if we are within
6636 -- an inlined body the current instance does not require parents.
6638 if In_Inlined_Body then
6639 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6643 -- Loop to check enclosing scopes
6645 Enclosing_Instance := Current_Scope;
6646 while Present (Enclosing_Instance) loop
6647 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6649 if Ekind (Enclosing_Instance) = E_Package
6650 and then Is_Generic_Instance (Enclosing_Instance)
6652 (Generic_Parent (Specification (Instance_Decl)))
6654 -- Check whether the generic we are looking for is a child of
6657 E := Find_Generic_Child
6658 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6659 exit when Present (E);
6665 Enclosing_Instance := Scope (Enclosing_Instance);
6677 Make_Expanded_Name (Loc,
6679 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6680 Selector_Name => New_Occurrence_Of (E, Loc)));
6682 Set_Entity (Gen_Id, E);
6683 Set_Etype (Gen_Id, Etype (E));
6684 Parent_Installed := False; -- Already in scope.
6687 end In_Enclosing_Instance;
6689 -- Start of processing for Check_Generic_Child_Unit
6692 -- If the name of the generic is given by a selected component, it may
6693 -- be the name of a generic child unit, and the prefix is the name of an
6694 -- instance of the parent, in which case the child unit must be visible.
6695 -- If this instance is not in scope, it must be placed there and removed
6696 -- after instantiation, because what is being instantiated is not the
6697 -- original child, but the corresponding child present in the instance
6700 -- If the child is instantiated within the parent, it can be given by
6701 -- a simple name. In this case the instance is already in scope, but
6702 -- the child generic must be recovered from the generic parent as well.
6704 if Nkind (Gen_Id) = N_Selected_Component then
6705 S := Selector_Name (Gen_Id);
6706 Analyze (Prefix (Gen_Id));
6707 Inst_Par := Entity (Prefix (Gen_Id));
6709 if Ekind (Inst_Par) = E_Package
6710 and then Present (Renamed_Object (Inst_Par))
6712 Inst_Par := Renamed_Object (Inst_Par);
6715 if Ekind (Inst_Par) = E_Package then
6716 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6717 Gen_Par := Generic_Parent (Parent (Inst_Par));
6719 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6721 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6723 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6726 elsif Ekind (Inst_Par) = E_Generic_Package
6727 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6729 -- A formal package may be a real child package, and not the
6730 -- implicit instance within a parent. In this case the child is
6731 -- not visible and has to be retrieved explicitly as well.
6733 Gen_Par := Inst_Par;
6736 if Present (Gen_Par) then
6738 -- The prefix denotes an instantiation. The entity itself may be a
6739 -- nested generic, or a child unit.
6741 E := Find_Generic_Child (Gen_Par, S);
6744 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6745 Set_Entity (Gen_Id, E);
6746 Set_Etype (Gen_Id, Etype (E));
6748 Set_Etype (S, Etype (E));
6750 -- Indicate that this is a reference to the parent
6752 if In_Extended_Main_Source_Unit (Gen_Id) then
6753 Set_Is_Instantiated (Inst_Par);
6756 -- A common mistake is to replicate the naming scheme of a
6757 -- hierarchy by instantiating a generic child directly, rather
6758 -- than the implicit child in a parent instance:
6760 -- generic .. package Gpar is ..
6761 -- generic .. package Gpar.Child is ..
6762 -- package Par is new Gpar ();
6765 -- package Par.Child is new Gpar.Child ();
6766 -- rather than Par.Child
6768 -- In this case the instantiation is within Par, which is an
6769 -- instance, but Gpar does not denote Par because we are not IN
6770 -- the instance of Gpar, so this is illegal. The test below
6771 -- recognizes this particular case.
6773 if Is_Child_Unit (E)
6774 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6775 and then (not In_Instance
6776 or else Nkind (Parent (Parent (Gen_Id))) =
6780 ("prefix of generic child unit must be instance of parent",
6784 if not In_Open_Scopes (Inst_Par)
6785 and then Nkind (Parent (Gen_Id)) not in
6786 N_Generic_Renaming_Declaration
6788 Install_Parent (Inst_Par);
6789 Parent_Installed := True;
6791 elsif In_Open_Scopes (Inst_Par) then
6793 -- If the parent is already installed, install the actuals
6794 -- for its formal packages. This is necessary when the child
6795 -- instance is a child of the parent instance: in this case,
6796 -- the parent is placed on the scope stack but the formal
6797 -- packages are not made visible.
6799 Install_Formal_Packages (Inst_Par);
6803 -- If the generic parent does not contain an entity that
6804 -- corresponds to the selector, the instance doesn't either.
6805 -- Analyzing the node will yield the appropriate error message.
6806 -- If the entity is not a child unit, then it is an inner
6807 -- generic in the parent.
6815 if Is_Child_Unit (Entity (Gen_Id))
6817 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6818 and then not In_Open_Scopes (Inst_Par)
6820 Install_Parent (Inst_Par);
6821 Parent_Installed := True;
6823 -- The generic unit may be the renaming of the implicit child
6824 -- present in an instance. In that case the parent instance is
6825 -- obtained from the name of the renamed entity.
6827 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6828 and then Present (Renamed_Entity (Entity (Gen_Id)))
6829 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6832 Renamed_Package : constant Node_Id :=
6833 Name (Parent (Entity (Gen_Id)));
6835 if Nkind (Renamed_Package) = N_Expanded_Name then
6836 Inst_Par := Entity (Prefix (Renamed_Package));
6837 Install_Parent (Inst_Par);
6838 Parent_Installed := True;
6844 elsif Nkind (Gen_Id) = N_Expanded_Name then
6846 -- Entity already present, analyze prefix, whose meaning may be an
6847 -- instance in the current context. If it is an instance of a
6848 -- relative within another, the proper parent may still have to be
6849 -- installed, if they are not of the same generation.
6851 Analyze (Prefix (Gen_Id));
6853 -- Prevent cascaded errors
6855 if Etype (Prefix (Gen_Id)) = Any_Type then
6859 -- In the unlikely case that a local declaration hides the name of
6860 -- the parent package, locate it on the homonym chain. If the context
6861 -- is an instance of the parent, the renaming entity is flagged as
6864 Inst_Par := Entity (Prefix (Gen_Id));
6865 while Present (Inst_Par)
6866 and then not Is_Package_Or_Generic_Package (Inst_Par)
6868 Inst_Par := Homonym (Inst_Par);
6871 pragma Assert (Present (Inst_Par));
6872 Set_Entity (Prefix (Gen_Id), Inst_Par);
6874 if In_Enclosing_Instance then
6877 elsif Present (Entity (Gen_Id))
6878 and then Is_Child_Unit (Entity (Gen_Id))
6879 and then not In_Open_Scopes (Inst_Par)
6881 Install_Parent (Inst_Par);
6882 Parent_Installed := True;
6885 elsif In_Enclosing_Instance then
6887 -- The child unit is found in some enclosing scope
6894 -- If this is the renaming of the implicit child in a parent
6895 -- instance, recover the parent name and install it.
6897 if Is_Entity_Name (Gen_Id) then
6898 E := Entity (Gen_Id);
6900 if Is_Generic_Unit (E)
6901 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6902 and then Is_Child_Unit (Renamed_Object (E))
6903 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6904 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6906 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6907 Inst_Par := Entity (Prefix (Gen_Id));
6909 if not In_Open_Scopes (Inst_Par) then
6910 Install_Parent (Inst_Par);
6911 Parent_Installed := True;
6914 -- If it is a child unit of a non-generic parent, it may be
6915 -- use-visible and given by a direct name. Install parent as
6918 elsif Is_Generic_Unit (E)
6919 and then Is_Child_Unit (E)
6921 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6922 and then not Is_Generic_Unit (Scope (E))
6924 if not In_Open_Scopes (Scope (E)) then
6925 Install_Parent (Scope (E));
6926 Parent_Installed := True;
6931 end Check_Generic_Child_Unit;
6933 -----------------------------
6934 -- Check_Hidden_Child_Unit --
6935 -----------------------------
6937 procedure Check_Hidden_Child_Unit
6939 Gen_Unit : Entity_Id;
6940 Act_Decl_Id : Entity_Id)
6942 Gen_Id : constant Node_Id := Name (N);
6945 if Is_Child_Unit (Gen_Unit)
6946 and then Is_Child_Unit (Act_Decl_Id)
6947 and then Nkind (Gen_Id) = N_Expanded_Name
6948 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6949 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6951 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6953 ("generic unit & is implicitly declared in &",
6954 Defining_Unit_Name (N), Gen_Unit);
6955 Error_Msg_N ("\instance must have different name",
6956 Defining_Unit_Name (N));
6958 end Check_Hidden_Child_Unit;
6960 ------------------------
6961 -- Check_Private_View --
6962 ------------------------
6964 procedure Check_Private_View (N : Node_Id) is
6965 T : constant Entity_Id := Etype (N);
6969 -- Exchange views if the type was not private in the generic but is
6970 -- private at the point of instantiation. Do not exchange views if
6971 -- the scope of the type is in scope. This can happen if both generic
6972 -- and instance are sibling units, or if type is defined in a parent.
6973 -- In this case the visibility of the type will be correct for all
6977 BT := Base_Type (T);
6979 if Is_Private_Type (T)
6980 and then not Has_Private_View (N)
6981 and then Present (Full_View (T))
6982 and then not In_Open_Scopes (Scope (T))
6984 -- In the generic, the full type was visible. Save the private
6985 -- entity, for subsequent exchange.
6989 elsif Has_Private_View (N)
6990 and then not Is_Private_Type (T)
6991 and then not Has_Been_Exchanged (T)
6992 and then Etype (Get_Associated_Node (N)) /= T
6994 -- Only the private declaration was visible in the generic. If
6995 -- the type appears in a subtype declaration, the subtype in the
6996 -- instance must have a view compatible with that of its parent,
6997 -- which must be exchanged (see corresponding code in Restore_
6998 -- Private_Views). Otherwise, if the type is defined in a parent
6999 -- unit, leave full visibility within instance, which is safe.
7001 if In_Open_Scopes (Scope (Base_Type (T)))
7002 and then not Is_Private_Type (Base_Type (T))
7003 and then Comes_From_Source (Base_Type (T))
7007 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7008 or else not In_Private_Part (Scope (Base_Type (T)))
7010 Prepend_Elmt (T, Exchanged_Views);
7011 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7014 -- For composite types with inconsistent representation exchange
7015 -- component types accordingly.
7017 elsif Is_Access_Type (T)
7018 and then Is_Private_Type (Designated_Type (T))
7019 and then not Has_Private_View (N)
7020 and then Present (Full_View (Designated_Type (T)))
7022 Switch_View (Designated_Type (T));
7024 elsif Is_Array_Type (T) then
7025 if Is_Private_Type (Component_Type (T))
7026 and then not Has_Private_View (N)
7027 and then Present (Full_View (Component_Type (T)))
7029 Switch_View (Component_Type (T));
7032 -- The normal exchange mechanism relies on the setting of a
7033 -- flag on the reference in the generic. However, an additional
7034 -- mechanism is needed for types that are not explicitly
7035 -- mentioned in the generic, but may be needed in expanded code
7036 -- in the instance. This includes component types of arrays and
7037 -- designated types of access types. This processing must also
7038 -- include the index types of arrays which we take care of here.
7045 Indx := First_Index (T);
7046 while Present (Indx) loop
7047 Typ := Base_Type (Etype (Indx));
7049 if Is_Private_Type (Typ)
7050 and then Present (Full_View (Typ))
7059 elsif Is_Private_Type (T)
7060 and then Present (Full_View (T))
7061 and then Is_Array_Type (Full_View (T))
7062 and then Is_Private_Type (Component_Type (Full_View (T)))
7066 -- Finally, a non-private subtype may have a private base type, which
7067 -- must be exchanged for consistency. This can happen when a package
7068 -- body is instantiated, when the scope stack is empty but in fact
7069 -- the subtype and the base type are declared in an enclosing scope.
7071 -- Note that in this case we introduce an inconsistency in the view
7072 -- set, because we switch the base type BT, but there could be some
7073 -- private dependent subtypes of BT which remain unswitched. Such
7074 -- subtypes might need to be switched at a later point (see specific
7075 -- provision for that case in Switch_View).
7077 elsif not Is_Private_Type (T)
7078 and then not Has_Private_View (N)
7079 and then Is_Private_Type (BT)
7080 and then Present (Full_View (BT))
7081 and then not Is_Generic_Type (BT)
7082 and then not In_Open_Scopes (BT)
7084 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7085 Exchange_Declarations (BT);
7088 end Check_Private_View;
7090 -----------------------------
7091 -- Check_Hidden_Primitives --
7092 -----------------------------
7094 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7097 Result : Elist_Id := No_Elist;
7100 if No (Assoc_List) then
7104 -- Traverse the list of associations between formals and actuals
7105 -- searching for renamings of tagged types
7107 Actual := First (Assoc_List);
7108 while Present (Actual) loop
7109 if Nkind (Actual) = N_Subtype_Declaration then
7110 Gen_T := Generic_Parent_Type (Actual);
7112 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7114 -- Traverse the list of primitives of the actual types
7115 -- searching for hidden primitives that are visible in the
7116 -- corresponding generic formal; leave them visible and
7117 -- append them to Result to restore their decoration later.
7119 Install_Hidden_Primitives
7120 (Prims_List => Result,
7122 Act_T => Entity (Subtype_Indication (Actual)));
7130 end Check_Hidden_Primitives;
7132 --------------------------
7133 -- Contains_Instance_Of --
7134 --------------------------
7136 function Contains_Instance_Of
7139 N : Node_Id) return Boolean
7147 -- Verify that there are no circular instantiations. We check whether
7148 -- the unit contains an instance of the current scope or some enclosing
7149 -- scope (in case one of the instances appears in a subunit). Longer
7150 -- circularities involving subunits might seem too pathological to
7151 -- consider, but they were not too pathological for the authors of
7152 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7153 -- enclosing generic scopes as containing an instance.
7156 -- Within a generic subprogram body, the scope is not generic, to
7157 -- allow for recursive subprograms. Use the declaration to determine
7158 -- whether this is a generic unit.
7160 if Ekind (Scop) = E_Generic_Package
7161 or else (Is_Subprogram (Scop)
7162 and then Nkind (Unit_Declaration_Node (Scop)) =
7163 N_Generic_Subprogram_Declaration)
7165 Elmt := First_Elmt (Inner_Instances (Inner));
7167 while Present (Elmt) loop
7168 if Node (Elmt) = Scop then
7169 Error_Msg_Node_2 := Inner;
7171 ("circular Instantiation: & instantiated within &!",
7175 elsif Node (Elmt) = Inner then
7178 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7179 Error_Msg_Node_2 := Inner;
7181 ("circular Instantiation: & instantiated within &!",
7189 -- Indicate that Inner is being instantiated within Scop
7191 Append_Elmt (Inner, Inner_Instances (Scop));
7194 if Scop = Standard_Standard then
7197 Scop := Scope (Scop);
7202 end Contains_Instance_Of;
7204 -----------------------
7205 -- Copy_Generic_Node --
7206 -----------------------
7208 function Copy_Generic_Node
7210 Parent_Id : Node_Id;
7211 Instantiating : Boolean) return Node_Id
7216 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7217 -- Check the given value of one of the Fields referenced by the current
7218 -- node to determine whether to copy it recursively. The field may hold
7219 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7220 -- Char) in which case it need not be copied.
7222 procedure Copy_Descendants;
7223 -- Common utility for various nodes
7225 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7226 -- Make copy of element list
7228 function Copy_Generic_List
7230 Parent_Id : Node_Id) return List_Id;
7231 -- Apply Copy_Node recursively to the members of a node list
7233 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7234 -- True if an identifier is part of the defining program unit name of
7235 -- a child unit. The entity of such an identifier must be kept (for
7236 -- ASIS use) even though as the name of an enclosing generic it would
7237 -- otherwise not be preserved in the generic tree.
7239 ----------------------
7240 -- Copy_Descendants --
7241 ----------------------
7243 procedure Copy_Descendants is
7244 use Atree.Unchecked_Access;
7245 -- This code section is part of the implementation of an untyped
7246 -- tree traversal, so it needs direct access to node fields.
7249 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7250 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7251 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7252 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7253 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7254 end Copy_Descendants;
7256 -----------------------------
7257 -- Copy_Generic_Descendant --
7258 -----------------------------
7260 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7262 if D = Union_Id (Empty) then
7265 elsif D in Node_Range then
7267 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7269 elsif D in List_Range then
7270 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7272 elsif D in Elist_Range then
7273 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7275 -- Nothing else is copyable (e.g. Uint values), return as is
7280 end Copy_Generic_Descendant;
7282 ------------------------
7283 -- Copy_Generic_Elist --
7284 ------------------------
7286 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7293 M := First_Elmt (E);
7294 while Present (M) loop
7296 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7305 end Copy_Generic_Elist;
7307 -----------------------
7308 -- Copy_Generic_List --
7309 -----------------------
7311 function Copy_Generic_List
7313 Parent_Id : Node_Id) return List_Id
7321 Set_Parent (New_L, Parent_Id);
7324 while Present (N) loop
7325 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7334 end Copy_Generic_List;
7336 ---------------------------
7337 -- In_Defining_Unit_Name --
7338 ---------------------------
7340 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7343 Present (Parent (Nam))
7344 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7346 (Nkind (Parent (Nam)) = N_Expanded_Name
7347 and then In_Defining_Unit_Name (Parent (Nam))));
7348 end In_Defining_Unit_Name;
7350 -- Start of processing for Copy_Generic_Node
7357 New_N := New_Copy (N);
7359 -- Copy aspects if present
7361 if Has_Aspects (N) then
7362 Set_Has_Aspects (New_N, False);
7363 Set_Aspect_Specifications
7364 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7367 if Instantiating then
7368 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7371 if not Is_List_Member (N) then
7372 Set_Parent (New_N, Parent_Id);
7375 -- Special casing for identifiers and other entity names and operators
7377 if Nkind_In (New_N, N_Character_Literal,
7381 or else Nkind (New_N) in N_Op
7383 if not Instantiating then
7385 -- Link both nodes in order to assign subsequently the entity of
7386 -- the copy to the original node, in case this is a global
7389 Set_Associated_Node (N, New_N);
7391 -- If we are within an instantiation, this is a nested generic
7392 -- that has already been analyzed at the point of definition.
7393 -- We must preserve references that were global to the enclosing
7394 -- parent at that point. Other occurrences, whether global or
7395 -- local to the current generic, must be resolved anew, so we
7396 -- reset the entity in the generic copy. A global reference has a
7397 -- smaller depth than the parent, or else the same depth in case
7398 -- both are distinct compilation units.
7400 -- A child unit is implicitly declared within the enclosing parent
7401 -- but is in fact global to it, and must be preserved.
7403 -- It is also possible for Current_Instantiated_Parent to be
7404 -- defined, and for this not to be a nested generic, namely if
7405 -- the unit is loaded through Rtsfind. In that case, the entity of
7406 -- New_N is only a link to the associated node, and not a defining
7409 -- The entities for parent units in the defining_program_unit of a
7410 -- generic child unit are established when the context of the unit
7411 -- is first analyzed, before the generic copy is made. They are
7412 -- preserved in the copy for use in ASIS queries.
7414 Ent := Entity (New_N);
7416 if No (Current_Instantiated_Parent.Gen_Id) then
7418 or else Nkind (Ent) /= N_Defining_Identifier
7419 or else not In_Defining_Unit_Name (N)
7421 Set_Associated_Node (New_N, Empty);
7426 not Nkind_In (Ent, N_Defining_Identifier,
7427 N_Defining_Character_Literal,
7428 N_Defining_Operator_Symbol)
7429 or else No (Scope (Ent))
7431 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7432 and then not Is_Child_Unit (Ent))
7434 (Scope_Depth (Scope (Ent)) >
7435 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7437 Get_Source_Unit (Ent) =
7438 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7440 Set_Associated_Node (New_N, Empty);
7443 -- Case of instantiating identifier or some other name or operator
7446 -- If the associated node is still defined, the entity in it
7447 -- is global, and must be copied to the instance. If this copy
7448 -- is being made for a body to inline, it is applied to an
7449 -- instantiated tree, and the entity is already present and
7450 -- must be also preserved.
7453 Assoc : constant Node_Id := Get_Associated_Node (N);
7456 if Present (Assoc) then
7457 if Nkind (Assoc) = Nkind (N) then
7458 Set_Entity (New_N, Entity (Assoc));
7459 Check_Private_View (N);
7461 -- The node is a reference to a global type and acts as the
7462 -- subtype mark of a qualified expression created in order
7463 -- to aid resolution of accidental overloading in instances.
7464 -- Since N is a reference to a type, the Associated_Node of
7465 -- N denotes an entity rather than another identifier. See
7466 -- Qualify_Universal_Operands for details.
7468 elsif Nkind (N) = N_Identifier
7469 and then Nkind (Parent (N)) = N_Qualified_Expression
7470 and then Subtype_Mark (Parent (N)) = N
7471 and then Is_Qualified_Universal_Literal (Parent (N))
7473 Set_Entity (New_N, Assoc);
7475 -- The name in the call may be a selected component if the
7476 -- call has not been analyzed yet, as may be the case for
7477 -- pre/post conditions in a generic unit.
7479 elsif Nkind (Assoc) = N_Function_Call
7480 and then Is_Entity_Name (Name (Assoc))
7482 Set_Entity (New_N, Entity (Name (Assoc)));
7484 elsif Nkind_In (Assoc, N_Defining_Identifier,
7485 N_Defining_Character_Literal,
7486 N_Defining_Operator_Symbol)
7487 and then Expander_Active
7489 -- Inlining case: we are copying a tree that contains
7490 -- global entities, which are preserved in the copy to be
7491 -- used for subsequent inlining.
7496 Set_Entity (New_N, Empty);
7502 -- For expanded name, we must copy the Prefix and Selector_Name
7504 if Nkind (N) = N_Expanded_Name then
7506 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7508 Set_Selector_Name (New_N,
7509 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7511 -- For operators, we must copy the right operand
7513 elsif Nkind (N) in N_Op then
7514 Set_Right_Opnd (New_N,
7515 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7517 -- And for binary operators, the left operand as well
7519 if Nkind (N) in N_Binary_Op then
7520 Set_Left_Opnd (New_N,
7521 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7525 -- Establish a link between an entity from the generic template and the
7526 -- corresponding entity in the generic copy to be analyzed.
7528 elsif Nkind (N) in N_Entity then
7529 if not Instantiating then
7530 Set_Associated_Entity (N, New_N);
7533 -- Clear any existing link the copy may inherit from the replicated
7534 -- generic template entity.
7536 Set_Associated_Entity (New_N, Empty);
7538 -- Special casing for stubs
7540 elsif Nkind (N) in N_Body_Stub then
7542 -- In any case, we must copy the specification or defining
7543 -- identifier as appropriate.
7545 if Nkind (N) = N_Subprogram_Body_Stub then
7546 Set_Specification (New_N,
7547 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7550 Set_Defining_Identifier (New_N,
7552 (Defining_Identifier (N), New_N, Instantiating));
7555 -- If we are not instantiating, then this is where we load and
7556 -- analyze subunits, i.e. at the point where the stub occurs. A
7557 -- more permissive system might defer this analysis to the point
7558 -- of instantiation, but this seems too complicated for now.
7560 if not Instantiating then
7562 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7564 Unum : Unit_Number_Type;
7568 -- Make sure that, if it is a subunit of the main unit that is
7569 -- preprocessed and if -gnateG is specified, the preprocessed
7570 -- file will be written.
7572 Lib.Analysing_Subunit_Of_Main :=
7573 Lib.In_Extended_Main_Source_Unit (N);
7576 (Load_Name => Subunit_Name,
7580 Lib.Analysing_Subunit_Of_Main := False;
7582 -- If the proper body is not found, a warning message will be
7583 -- emitted when analyzing the stub, or later at the point of
7584 -- instantiation. Here we just leave the stub as is.
7586 if Unum = No_Unit then
7587 Subunits_Missing := True;
7588 goto Subunit_Not_Found;
7591 Subunit := Cunit (Unum);
7593 if Nkind (Unit (Subunit)) /= N_Subunit then
7595 ("found child unit instead of expected SEPARATE subunit",
7597 Error_Msg_Sloc := Sloc (N);
7598 Error_Msg_N ("\to complete stub #", Subunit);
7599 goto Subunit_Not_Found;
7602 -- We must create a generic copy of the subunit, in order to
7603 -- perform semantic analysis on it, and we must replace the
7604 -- stub in the original generic unit with the subunit, in order
7605 -- to preserve non-local references within.
7607 -- Only the proper body needs to be copied. Library_Unit and
7608 -- context clause are simply inherited by the generic copy.
7609 -- Note that the copy (which may be recursive if there are
7610 -- nested subunits) must be done first, before attaching it to
7611 -- the enclosing generic.
7615 (Proper_Body (Unit (Subunit)),
7616 Empty, Instantiating => False);
7618 -- Now place the original proper body in the original generic
7619 -- unit. This is a body, not a compilation unit.
7621 Rewrite (N, Proper_Body (Unit (Subunit)));
7622 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7623 Set_Was_Originally_Stub (N);
7625 -- Finally replace the body of the subunit with its copy, and
7626 -- make this new subunit into the library unit of the generic
7627 -- copy, which does not have stubs any longer.
7629 Set_Proper_Body (Unit (Subunit), New_Body);
7630 Set_Library_Unit (New_N, Subunit);
7631 Inherit_Context (Unit (Subunit), N);
7634 -- If we are instantiating, this must be an error case, since
7635 -- otherwise we would have replaced the stub node by the proper body
7636 -- that corresponds. So just ignore it in the copy (i.e. we have
7637 -- copied it, and that is good enough).
7643 <<Subunit_Not_Found>> null;
7645 -- If the node is a compilation unit, it is the subunit of a stub, which
7646 -- has been loaded already (see code below). In this case, the library
7647 -- unit field of N points to the parent unit (which is a compilation
7648 -- unit) and need not (and cannot) be copied.
7650 -- When the proper body of the stub is analyzed, the library_unit link
7651 -- is used to establish the proper context (see sem_ch10).
7653 -- The other fields of a compilation unit are copied as usual
7655 elsif Nkind (N) = N_Compilation_Unit then
7657 -- This code can only be executed when not instantiating, because in
7658 -- the copy made for an instantiation, the compilation unit node has
7659 -- disappeared at the point that a stub is replaced by its proper
7662 pragma Assert (not Instantiating);
7664 Set_Context_Items (New_N,
7665 Copy_Generic_List (Context_Items (N), New_N));
7668 Copy_Generic_Node (Unit (N), New_N, False));
7670 Set_First_Inlined_Subprogram (New_N,
7672 (First_Inlined_Subprogram (N), New_N, False));
7674 Set_Aux_Decls_Node (New_N,
7675 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7677 -- For an assignment node, the assignment is known to be semantically
7678 -- legal if we are instantiating the template. This avoids incorrect
7679 -- diagnostics in generated code.
7681 elsif Nkind (N) = N_Assignment_Statement then
7683 -- Copy name and expression fields in usual manner
7686 Copy_Generic_Node (Name (N), New_N, Instantiating));
7688 Set_Expression (New_N,
7689 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7691 if Instantiating then
7692 Set_Assignment_OK (Name (New_N), True);
7695 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7696 if not Instantiating then
7697 Set_Associated_Node (N, New_N);
7700 if Present (Get_Associated_Node (N))
7701 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7703 -- In the generic the aggregate has some composite type. If at
7704 -- the point of instantiation the type has a private view,
7705 -- install the full view (and that of its ancestors, if any).
7708 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7712 if Present (T) and then Is_Private_Type (T) then
7717 and then Is_Tagged_Type (T)
7718 and then Is_Derived_Type (T)
7720 Rt := Root_Type (T);
7725 if Is_Private_Type (T) then
7736 -- Do not copy the associated node, which points to the generic copy
7737 -- of the aggregate.
7740 use Atree.Unchecked_Access;
7741 -- This code section is part of the implementation of an untyped
7742 -- tree traversal, so it needs direct access to node fields.
7745 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7746 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7747 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7748 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7751 -- Allocators do not have an identifier denoting the access type, so we
7752 -- must locate it through the expression to check whether the views are
7755 elsif Nkind (N) = N_Allocator
7756 and then Nkind (Expression (N)) = N_Qualified_Expression
7757 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7758 and then Instantiating
7761 T : constant Node_Id :=
7762 Get_Associated_Node (Subtype_Mark (Expression (N)));
7768 -- Retrieve the allocator node in the generic copy
7770 Acc_T := Etype (Parent (Parent (T)));
7772 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7773 Switch_View (Acc_T);
7780 -- For a proper body, we must catch the case of a proper body that
7781 -- replaces a stub. This represents the point at which a separate
7782 -- compilation unit, and hence template file, may be referenced, so we
7783 -- must make a new source instantiation entry for the template of the
7784 -- subunit, and ensure that all nodes in the subunit are adjusted using
7785 -- this new source instantiation entry.
7787 elsif Nkind (N) in N_Proper_Body then
7789 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7792 if Instantiating and then Was_Originally_Stub (N) then
7793 Create_Instantiation_Source
7794 (Instantiation_Node,
7795 Defining_Entity (N),
7799 -- Now copy the fields of the proper body, using the new
7800 -- adjustment factor if one was needed as per test above.
7804 -- Restore the original adjustment factor in case changed
7806 S_Adjustment := Save_Adjustment;
7809 elsif Nkind (N) = N_Pragma and then Instantiating then
7811 -- Do not copy Comment or Ident pragmas their content is relevant to
7812 -- the generic unit, not to the instantiating unit.
7814 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7815 New_N := Make_Null_Statement (Sloc (N));
7817 -- Do not copy pragmas generated from aspects because the pragmas do
7818 -- not carry any semantic information, plus they will be regenerated
7821 -- However, generating C we need to copy them since postconditions
7822 -- are inlined by the front end, and the front-end inlining machinery
7823 -- relies on this routine to perform inlining.
7825 elsif From_Aspect_Specification (N)
7826 and then not Modify_Tree_For_C
7828 New_N := Make_Null_Statement (Sloc (N));
7834 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7836 -- No descendant fields need traversing
7840 elsif Nkind (N) = N_String_Literal
7841 and then Present (Etype (N))
7842 and then Instantiating
7844 -- If the string is declared in an outer scope, the string_literal
7845 -- subtype created for it may have the wrong scope. Force reanalysis
7846 -- of the constant to generate a new itype in the proper context.
7848 Set_Etype (New_N, Empty);
7849 Set_Analyzed (New_N, False);
7851 -- For the remaining nodes, copy their descendants recursively
7856 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7857 Set_Generic_Parent (Specification (New_N), N);
7859 -- Should preserve Corresponding_Spec??? (12.3(14))
7863 -- Propagate dimensions if present, so that they are reflected in the
7866 if Nkind (N) in N_Has_Etype
7867 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7868 and then Present (Etype (N))
7869 and then Is_Floating_Point_Type (Etype (N))
7870 and then Has_Dimension_System (Etype (N))
7872 Copy_Dimensions (N, New_N);
7876 end Copy_Generic_Node;
7878 ----------------------------
7879 -- Denotes_Formal_Package --
7880 ----------------------------
7882 function Denotes_Formal_Package
7884 On_Exit : Boolean := False;
7885 Instance : Entity_Id := Empty) return Boolean
7888 Scop : constant Entity_Id := Scope (Pack);
7891 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7892 -- The package in question may be an actual for a previous formal
7893 -- package P of the current instance, so examine its actuals as well.
7894 -- This must be recursive over other formal packages.
7896 ----------------------------------
7897 -- Is_Actual_Of_Previous_Formal --
7898 ----------------------------------
7900 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7904 E1 := First_Entity (P);
7905 while Present (E1) and then E1 /= Instance loop
7906 if Ekind (E1) = E_Package
7907 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7909 if Renamed_Object (E1) = Pack then
7912 elsif E1 = P or else Renamed_Object (E1) = P then
7915 elsif Is_Actual_Of_Previous_Formal (E1) then
7924 end Is_Actual_Of_Previous_Formal;
7926 -- Start of processing for Denotes_Formal_Package
7932 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7934 Par := Current_Instantiated_Parent.Act_Id;
7937 if Ekind (Scop) = E_Generic_Package
7938 or else Nkind (Unit_Declaration_Node (Scop)) =
7939 N_Generic_Subprogram_Declaration
7943 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7944 N_Formal_Package_Declaration
7952 -- Check whether this package is associated with a formal package of
7953 -- the enclosing instantiation. Iterate over the list of renamings.
7955 E := First_Entity (Par);
7956 while Present (E) loop
7957 if Ekind (E) /= E_Package
7958 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7962 elsif Renamed_Object (E) = Par then
7965 elsif Renamed_Object (E) = Pack then
7968 elsif Is_Actual_Of_Previous_Formal (E) then
7978 end Denotes_Formal_Package;
7984 procedure End_Generic is
7986 -- ??? More things could be factored out in this routine. Should
7987 -- probably be done at a later stage.
7989 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7990 Generic_Flags.Decrement_Last;
7992 Expander_Mode_Restore;
7999 function Earlier (N1, N2 : Node_Id) return Boolean is
8000 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8001 -- Find distance from given node to enclosing compilation unit
8007 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8010 and then Nkind (P) /= N_Compilation_Unit
8012 P := True_Parent (P);
8017 -- Local declarations
8026 -- Start of processing for Earlier
8029 Find_Depth (P1, D1);
8030 Find_Depth (P2, D2);
8040 P1 := True_Parent (P1);
8045 P2 := True_Parent (P2);
8049 -- At this point P1 and P2 are at the same distance from the root.
8050 -- We examine their parents until we find a common declarative list.
8051 -- If we reach the root, N1 and N2 do not descend from the same
8052 -- declarative list (e.g. one is nested in the declarative part and
8053 -- the other is in a block in the statement part) and the earlier
8054 -- one is already frozen.
8056 while not Is_List_Member (P1)
8057 or else not Is_List_Member (P2)
8058 or else List_Containing (P1) /= List_Containing (P2)
8060 P1 := True_Parent (P1);
8061 P2 := True_Parent (P2);
8063 if Nkind (Parent (P1)) = N_Subunit then
8064 P1 := Corresponding_Stub (Parent (P1));
8067 if Nkind (Parent (P2)) = N_Subunit then
8068 P2 := Corresponding_Stub (Parent (P2));
8076 -- Expanded code usually shares the source location of the original
8077 -- construct it was generated for. This however may not necessarily
8078 -- reflect the true location of the code within the tree.
8080 -- Before comparing the slocs of the two nodes, make sure that we are
8081 -- working with correct source locations. Assume that P1 is to the left
8082 -- of P2. If either one does not come from source, traverse the common
8083 -- list heading towards the other node and locate the first source
8087 -- ----+===+===+--------------+===+===+----
8088 -- expanded code expanded code
8090 if not Comes_From_Source (P1) then
8091 while Present (P1) loop
8093 -- Neither P2 nor a source statement were located during the
8094 -- search. If we reach the end of the list, then P1 does not
8095 -- occur earlier than P2.
8098 -- start --- P2 ----- P1 --- end
8100 if No (Next (P1)) then
8103 -- We encounter P2 while going to the right of the list. This
8104 -- means that P1 does indeed appear earlier.
8107 -- start --- P1 ===== P2 --- end
8108 -- expanded code in between
8113 -- No need to look any further since we have located a source
8116 elsif Comes_From_Source (P1) then
8126 if not Comes_From_Source (P2) then
8127 while Present (P2) loop
8129 -- Neither P1 nor a source statement were located during the
8130 -- search. If we reach the start of the list, then P1 does not
8131 -- occur earlier than P2.
8134 -- start --- P2 --- P1 --- end
8136 if No (Prev (P2)) then
8139 -- We encounter P1 while going to the left of the list. This
8140 -- means that P1 does indeed appear earlier.
8143 -- start --- P1 ===== P2 --- end
8144 -- expanded code in between
8149 -- No need to look any further since we have located a source
8152 elsif Comes_From_Source (P2) then
8162 -- At this point either both nodes came from source or we approximated
8163 -- their source locations through neighboring source statements.
8165 T1 := Top_Level_Location (Sloc (P1));
8166 T2 := Top_Level_Location (Sloc (P2));
8168 -- When two nodes come from the same instance, they have identical top
8169 -- level locations. To determine proper relation within the tree, check
8170 -- their locations within the template.
8173 return Sloc (P1) < Sloc (P2);
8175 -- The two nodes either come from unrelated instances or do not come
8176 -- from instantiated code at all.
8183 ----------------------
8184 -- Find_Actual_Type --
8185 ----------------------
8187 function Find_Actual_Type
8189 Gen_Type : Entity_Id) return Entity_Id
8191 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8195 -- Special processing only applies to child units
8197 if not Is_Child_Unit (Gen_Scope) then
8198 return Get_Instance_Of (Typ);
8200 -- If designated or component type is itself a formal of the child unit,
8201 -- its instance is available.
8203 elsif Scope (Typ) = Gen_Scope then
8204 return Get_Instance_Of (Typ);
8206 -- If the array or access type is not declared in the parent unit,
8207 -- no special processing needed.
8209 elsif not Is_Generic_Type (Typ)
8210 and then Scope (Gen_Scope) /= Scope (Typ)
8212 return Get_Instance_Of (Typ);
8214 -- Otherwise, retrieve designated or component type by visibility
8217 T := Current_Entity (Typ);
8218 while Present (T) loop
8219 if In_Open_Scopes (Scope (T)) then
8221 elsif Is_Generic_Actual_Type (T) then
8230 end Find_Actual_Type;
8232 ----------------------------
8233 -- Freeze_Subprogram_Body --
8234 ----------------------------
8236 procedure Freeze_Subprogram_Body
8237 (Inst_Node : Node_Id;
8239 Pack_Id : Entity_Id)
8241 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8242 Par : constant Entity_Id := Scope (Gen_Unit);
8248 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8249 -- Find innermost package body that encloses the given node, and which
8250 -- is not a compilation unit. Freeze nodes for the instance, or for its
8251 -- enclosing body, may be inserted after the enclosing_body of the
8252 -- generic unit. Used to determine proper placement of freeze node for
8253 -- both package and subprogram instances.
8255 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8256 -- Find entity for given package body, and locate or create a freeze
8259 ----------------------------
8260 -- Enclosing_Package_Body --
8261 ----------------------------
8263 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8269 and then Nkind (Parent (P)) /= N_Compilation_Unit
8271 if Nkind (P) = N_Package_Body then
8272 if Nkind (Parent (P)) = N_Subunit then
8273 return Corresponding_Stub (Parent (P));
8279 P := True_Parent (P);
8283 end Enclosing_Package_Body;
8285 -------------------------
8286 -- Package_Freeze_Node --
8287 -------------------------
8289 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8293 if Nkind (B) = N_Package_Body then
8294 Id := Corresponding_Spec (B);
8295 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8296 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8299 Ensure_Freeze_Node (Id);
8300 return Freeze_Node (Id);
8301 end Package_Freeze_Node;
8303 -- Start of processing for Freeze_Subprogram_Body
8306 -- If the instance and the generic body appear within the same unit, and
8307 -- the instance precedes the generic, the freeze node for the instance
8308 -- must appear after that of the generic. If the generic is nested
8309 -- within another instance I2, then current instance must be frozen
8310 -- after I2. In both cases, the freeze nodes are those of enclosing
8311 -- packages. Otherwise, the freeze node is placed at the end of the
8312 -- current declarative part.
8314 Enc_G := Enclosing_Package_Body (Gen_Body);
8315 Enc_I := Enclosing_Package_Body (Inst_Node);
8316 Ensure_Freeze_Node (Pack_Id);
8317 F_Node := Freeze_Node (Pack_Id);
8319 if Is_Generic_Instance (Par)
8320 and then Present (Freeze_Node (Par))
8321 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8323 -- The parent was a premature instantiation. Insert freeze node at
8324 -- the end the current declarative part.
8326 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8327 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8329 -- Handle the following case:
8331 -- package Parent_Inst is new ...
8334 -- procedure P ... -- this body freezes Parent_Inst
8336 -- package Inst is new ...
8338 -- In this particular scenario, the freeze node for Inst must be
8339 -- inserted in the same manner as that of Parent_Inst - before the
8340 -- next source body or at the end of the declarative list (body not
8341 -- available). If body P did not exist and Parent_Inst was frozen
8342 -- after Inst, either by a body following Inst or at the end of the
8343 -- declarative region, the freeze node for Inst must be inserted
8344 -- after that of Parent_Inst. This relation is established by
8345 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8347 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8348 List_Containing (Inst_Node)
8349 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8351 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8354 Insert_After (Freeze_Node (Par), F_Node);
8357 -- The body enclosing the instance should be frozen after the body that
8358 -- includes the generic, because the body of the instance may make
8359 -- references to entities therein. If the two are not in the same
8360 -- declarative part, or if the one enclosing the instance is frozen
8361 -- already, freeze the instance at the end of the current declarative
8364 elsif Is_Generic_Instance (Par)
8365 and then Present (Freeze_Node (Par))
8366 and then Present (Enc_I)
8368 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8370 (Nkind (Enc_I) = N_Package_Body
8372 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8374 -- The enclosing package may contain several instances. Rather
8375 -- than computing the earliest point at which to insert its freeze
8376 -- node, we place it at the end of the declarative part of the
8377 -- parent of the generic.
8379 Insert_Freeze_Node_For_Instance
8380 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8383 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8385 elsif Present (Enc_G)
8386 and then Present (Enc_I)
8387 and then Enc_G /= Enc_I
8388 and then Earlier (Inst_Node, Gen_Body)
8390 if Nkind (Enc_G) = N_Package_Body then
8392 Corresponding_Spec (Enc_G);
8393 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8395 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8398 -- Freeze package that encloses instance, and place node after the
8399 -- package that encloses generic. If enclosing package is already
8400 -- frozen we have to assume it is at the proper place. This may be a
8401 -- potential ABE that requires dynamic checking. Do not add a freeze
8402 -- node if the package that encloses the generic is inside the body
8403 -- that encloses the instance, because the freeze node would be in
8404 -- the wrong scope. Additional contortions needed if the bodies are
8405 -- within a subunit.
8408 Enclosing_Body : Node_Id;
8411 if Nkind (Enc_I) = N_Package_Body_Stub then
8412 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8414 Enclosing_Body := Enc_I;
8417 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8418 Insert_Freeze_Node_For_Instance
8419 (Enc_G, Package_Freeze_Node (Enc_I));
8423 -- Freeze enclosing subunit before instance
8425 Ensure_Freeze_Node (E_G_Id);
8427 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8428 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8431 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8434 -- If none of the above, insert freeze node at the end of the current
8435 -- declarative part.
8437 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8439 end Freeze_Subprogram_Body;
8445 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8447 return Generic_Renamings.Table (E).Gen_Id;
8450 ---------------------
8451 -- Get_Instance_Of --
8452 ---------------------
8454 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8455 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8458 if Res /= Assoc_Null then
8459 return Generic_Renamings.Table (Res).Act_Id;
8462 -- On exit, entity is not instantiated: not a generic parameter, or
8463 -- else parameter of an inner generic unit.
8467 end Get_Instance_Of;
8469 ------------------------------------
8470 -- Get_Package_Instantiation_Node --
8471 ------------------------------------
8473 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8474 Decl : Node_Id := Unit_Declaration_Node (A);
8478 -- If the Package_Instantiation attribute has been set on the package
8479 -- entity, then use it directly when it (or its Original_Node) refers
8480 -- to an N_Package_Instantiation node. In principle it should be
8481 -- possible to have this field set in all cases, which should be
8482 -- investigated, and would allow this function to be significantly
8485 Inst := Package_Instantiation (A);
8487 if Present (Inst) then
8488 if Nkind (Inst) = N_Package_Instantiation then
8491 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8492 return Original_Node (Inst);
8496 -- If the instantiation is a compilation unit that does not need body
8497 -- then the instantiation node has been rewritten as a package
8498 -- declaration for the instance, and we return the original node.
8500 -- If it is a compilation unit and the instance node has not been
8501 -- rewritten, then it is still the unit of the compilation. Finally, if
8502 -- a body is present, this is a parent of the main unit whose body has
8503 -- been compiled for inlining purposes, and the instantiation node has
8504 -- been rewritten with the instance body.
8506 -- Otherwise the instantiation node appears after the declaration. If
8507 -- the entity is a formal package, the declaration may have been
8508 -- rewritten as a generic declaration (in the case of a formal with box)
8509 -- or left as a formal package declaration if it has actuals, and is
8510 -- found with a forward search.
8512 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8513 if Nkind (Decl) = N_Package_Declaration
8514 and then Present (Corresponding_Body (Decl))
8516 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8519 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8520 return Original_Node (Decl);
8522 return Unit (Parent (Decl));
8525 elsif Nkind (Decl) = N_Package_Declaration
8526 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8528 return Original_Node (Decl);
8531 Inst := Next (Decl);
8532 while not Nkind_In (Inst, N_Package_Instantiation,
8533 N_Formal_Package_Declaration)
8540 end Get_Package_Instantiation_Node;
8542 ------------------------
8543 -- Has_Been_Exchanged --
8544 ------------------------
8546 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8550 Next := First_Elmt (Exchanged_Views);
8551 while Present (Next) loop
8552 if Full_View (Node (Next)) = E then
8560 end Has_Been_Exchanged;
8566 function Hash (F : Entity_Id) return HTable_Range is
8568 return HTable_Range (F mod HTable_Size);
8571 ------------------------
8572 -- Hide_Current_Scope --
8573 ------------------------
8575 procedure Hide_Current_Scope is
8576 C : constant Entity_Id := Current_Scope;
8580 Set_Is_Hidden_Open_Scope (C);
8582 E := First_Entity (C);
8583 while Present (E) loop
8584 if Is_Immediately_Visible (E) then
8585 Set_Is_Immediately_Visible (E, False);
8586 Append_Elmt (E, Hidden_Entities);
8592 -- Make the scope name invisible as well. This is necessary, but might
8593 -- conflict with calls to Rtsfind later on, in case the scope is a
8594 -- predefined one. There is no clean solution to this problem, so for
8595 -- now we depend on the user not redefining Standard itself in one of
8596 -- the parent units.
8598 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8599 Set_Is_Immediately_Visible (C, False);
8600 Append_Elmt (C, Hidden_Entities);
8603 end Hide_Current_Scope;
8609 procedure Init_Env is
8610 Saved : Instance_Env;
8613 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8614 Saved.Exchanged_Views := Exchanged_Views;
8615 Saved.Hidden_Entities := Hidden_Entities;
8616 Saved.Current_Sem_Unit := Current_Sem_Unit;
8617 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8618 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8620 -- Save configuration switches. These may be reset if the unit is a
8621 -- predefined unit, and the current mode is not Ada 2005.
8623 Save_Opt_Config_Switches (Saved.Switches);
8625 Instance_Envs.Append (Saved);
8627 Exchanged_Views := New_Elmt_List;
8628 Hidden_Entities := New_Elmt_List;
8630 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8631 -- this is set properly in Set_Instance_Env.
8633 Current_Instantiated_Parent :=
8634 (Current_Scope, Current_Scope, Assoc_Null);
8637 ------------------------------
8638 -- In_Same_Declarative_Part --
8639 ------------------------------
8641 function In_Same_Declarative_Part
8643 Inst : Node_Id) return Boolean
8645 Decls : constant Node_Id := Parent (F_Node);
8649 Nod := Parent (Inst);
8650 while Present (Nod) loop
8654 elsif Nkind_In (Nod, N_Subprogram_Body,
8656 N_Package_Declaration,
8663 elsif Nkind (Nod) = N_Subunit then
8664 Nod := Corresponding_Stub (Nod);
8666 elsif Nkind (Nod) = N_Compilation_Unit then
8670 Nod := Parent (Nod);
8675 end In_Same_Declarative_Part;
8677 ---------------------
8678 -- In_Main_Context --
8679 ---------------------
8681 function In_Main_Context (E : Entity_Id) return Boolean is
8687 if not Is_Compilation_Unit (E)
8688 or else Ekind (E) /= E_Package
8689 or else In_Private_Part (E)
8694 Context := Context_Items (Cunit (Main_Unit));
8696 Clause := First (Context);
8697 while Present (Clause) loop
8698 if Nkind (Clause) = N_With_Clause then
8699 Nam := Name (Clause);
8701 -- If the current scope is part of the context of the main unit,
8702 -- analysis of the corresponding with_clause is not complete, and
8703 -- the entity is not set. We use the Chars field directly, which
8704 -- might produce false positives in rare cases, but guarantees
8705 -- that we produce all the instance bodies we will need.
8707 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8708 or else (Nkind (Nam) = N_Selected_Component
8709 and then Chars (Selector_Name (Nam)) = Chars (E))
8719 end In_Main_Context;
8721 ---------------------
8722 -- Inherit_Context --
8723 ---------------------
8725 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8726 Current_Context : List_Id;
8727 Current_Unit : Node_Id;
8736 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8738 -- The inherited context is attached to the enclosing compilation
8739 -- unit. This is either the main unit, or the declaration for the
8740 -- main unit (in case the instantiation appears within the package
8741 -- declaration and the main unit is its body).
8743 Current_Unit := Parent (Inst);
8744 while Present (Current_Unit)
8745 and then Nkind (Current_Unit) /= N_Compilation_Unit
8747 Current_Unit := Parent (Current_Unit);
8750 Current_Context := Context_Items (Current_Unit);
8752 Item := First (Context_Items (Parent (Gen_Decl)));
8753 while Present (Item) loop
8754 if Nkind (Item) = N_With_Clause then
8755 Lib_Unit := Library_Unit (Item);
8757 -- Take care to prevent direct cyclic with's
8759 if Lib_Unit /= Current_Unit then
8761 -- Do not add a unit if it is already in the context
8763 Clause := First (Current_Context);
8765 while Present (Clause) loop
8766 if Nkind (Clause) = N_With_Clause and then
8767 Library_Unit (Clause) = Lib_Unit
8777 New_I := New_Copy (Item);
8778 Set_Implicit_With (New_I, True);
8779 Set_Implicit_With_From_Instantiation (New_I, True);
8780 Append (New_I, Current_Context);
8788 end Inherit_Context;
8794 procedure Initialize is
8796 Generic_Renamings.Init;
8799 Generic_Renamings_HTable.Reset;
8800 Circularity_Detected := False;
8801 Exchanged_Views := No_Elist;
8802 Hidden_Entities := No_Elist;
8805 -------------------------------------
8806 -- Insert_Freeze_Node_For_Instance --
8807 -------------------------------------
8809 procedure Insert_Freeze_Node_For_Instance
8818 function Enclosing_Body (N : Node_Id) return Node_Id;
8819 -- Find enclosing package or subprogram body, if any. Freeze node may
8820 -- be placed at end of current declarative list if previous instance
8821 -- and current one have different enclosing bodies.
8823 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8824 -- Find the local instance, if any, that declares the generic that is
8825 -- being instantiated. If present, the freeze node for this instance
8826 -- must follow the freeze node for the previous instance.
8828 --------------------
8829 -- Enclosing_Body --
8830 --------------------
8832 function Enclosing_Body (N : Node_Id) return Node_Id is
8838 and then Nkind (Parent (P)) /= N_Compilation_Unit
8840 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8841 if Nkind (Parent (P)) = N_Subunit then
8842 return Corresponding_Stub (Parent (P));
8848 P := True_Parent (P);
8854 -----------------------
8855 -- Previous_Instance --
8856 -----------------------
8858 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8863 while Present (S) and then S /= Standard_Standard loop
8864 if Is_Generic_Instance (S)
8865 and then In_Same_Source_Unit (S, N)
8874 end Previous_Instance;
8876 -- Start of processing for Insert_Freeze_Node_For_Instance
8879 if not Is_List_Member (F_Node) then
8881 Decls := List_Containing (N);
8882 Inst := Entity (F_Node);
8883 Par_N := Parent (Decls);
8885 -- When processing a subprogram instantiation, utilize the actual
8886 -- subprogram instantiation rather than its package wrapper as it
8887 -- carries all the context information.
8889 if Is_Wrapper_Package (Inst) then
8890 Inst := Related_Instance (Inst);
8893 -- If this is a package instance, check whether the generic is
8894 -- declared in a previous instance and the current instance is
8895 -- not within the previous one.
8897 if Present (Generic_Parent (Parent (Inst)))
8898 and then Is_In_Main_Unit (N)
8901 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8902 Par_I : constant Entity_Id :=
8904 (Generic_Parent (Parent (Inst)));
8909 and then Earlier (N, Freeze_Node (Par_I))
8911 Scop := Scope (Inst);
8913 -- If the current instance is within the one that contains
8914 -- the generic, the freeze node for the current one must
8915 -- appear in the current declarative part. Ditto, if the
8916 -- current instance is within another package instance or
8917 -- within a body that does not enclose the current instance.
8918 -- In these three cases the freeze node of the previous
8919 -- instance is not relevant.
8921 while Present (Scop) and then Scop /= Standard_Standard loop
8922 exit when Scop = Par_I
8924 (Is_Generic_Instance (Scop)
8925 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8926 Scop := Scope (Scop);
8929 -- Previous instance encloses current instance
8931 if Scop = Par_I then
8934 -- If the next node is a source body we must freeze in
8935 -- the current scope as well.
8937 elsif Present (Next (N))
8938 and then Nkind_In (Next (N), N_Subprogram_Body,
8940 and then Comes_From_Source (Next (N))
8944 -- Current instance is within an unrelated instance
8946 elsif Is_Generic_Instance (Scop) then
8949 -- Current instance is within an unrelated body
8951 elsif Present (Enclosing_N)
8952 and then Enclosing_N /= Enclosing_Body (Par_I)
8957 Insert_After (Freeze_Node (Par_I), F_Node);
8964 -- When the instantiation occurs in a package declaration, append the
8965 -- freeze node to the private declarations (if any).
8967 if Nkind (Par_N) = N_Package_Specification
8968 and then Decls = Visible_Declarations (Par_N)
8969 and then Present (Private_Declarations (Par_N))
8970 and then not Is_Empty_List (Private_Declarations (Par_N))
8972 Decls := Private_Declarations (Par_N);
8973 Decl := First (Decls);
8976 -- Determine the proper freeze point of a package instantiation. We
8977 -- adhere to the general rule of a package or subprogram body causing
8978 -- freezing of anything before it in the same declarative region. In
8979 -- this case, the proper freeze point of a package instantiation is
8980 -- before the first source body which follows, or before a stub. This
8981 -- ensures that entities coming from the instance are already frozen
8982 -- and usable in source bodies.
8984 if Nkind (Par_N) /= N_Package_Declaration
8985 and then Ekind (Inst) = E_Package
8986 and then Is_Generic_Instance (Inst)
8988 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8990 while Present (Decl) loop
8991 if (Nkind (Decl) in N_Unit_Body
8993 Nkind (Decl) in N_Body_Stub)
8994 and then Comes_From_Source (Decl)
8996 Insert_Before (Decl, F_Node);
9004 -- In a package declaration, or if no previous body, insert at end
9007 Set_Sloc (F_Node, Sloc (Last (Decls)));
9008 Insert_After (Last (Decls), F_Node);
9010 end Insert_Freeze_Node_For_Instance;
9016 procedure Install_Body
9017 (Act_Body : Node_Id;
9022 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9023 -- Check if the generic definition and the instantiation come from
9024 -- a common scope, in which case the instance must be frozen after
9025 -- the generic body.
9027 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9028 -- If the instance is nested inside a generic unit, the Sloc of the
9029 -- instance indicates the place of the original definition, not the
9030 -- point of the current enclosing instance. Pending a better usage of
9031 -- Slocs to indicate instantiation places, we determine the place of
9032 -- origin of a node by finding the maximum sloc of any ancestor node.
9033 -- Why is this not equivalent to Top_Level_Location ???
9039 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9040 Act_Scop : Entity_Id := Scope (Act_Id);
9041 Gen_Scop : Entity_Id := Scope (Gen_Id);
9044 while Act_Scop /= Standard_Standard
9045 and then Gen_Scop /= Standard_Standard
9047 if Act_Scop = Gen_Scop then
9051 Act_Scop := Scope (Act_Scop);
9052 Gen_Scop := Scope (Gen_Scop);
9062 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9069 while Present (N1) and then N1 /= Act_Unit loop
9070 if Sloc (N1) > Res then
9080 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9081 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9082 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9083 Par : constant Entity_Id := Scope (Gen_Id);
9084 Gen_Unit : constant Node_Id :=
9085 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9087 Body_Unit : Node_Id;
9089 Must_Delay : Boolean;
9090 Orig_Body : Node_Id := Gen_Body;
9092 -- Start of processing for Install_Body
9095 -- Handle first the case of an instance with incomplete actual types.
9096 -- The instance body cannot be placed after the declaration because
9097 -- full views have not been seen yet. Any use of the non-limited views
9098 -- in the instance body requires the presence of a regular with_clause
9099 -- in the enclosing unit, and will fail if this with_clause is missing.
9100 -- We place the instance body at the beginning of the enclosing body,
9101 -- which is the unit being compiled. The freeze node for the instance
9102 -- is then placed after the instance body.
9104 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9105 and then Expander_Active
9106 and then Ekind (Scope (Act_Id)) = E_Package
9109 Scop : constant Entity_Id := Scope (Act_Id);
9110 Body_Id : constant Node_Id :=
9111 Corresponding_Body (Unit_Declaration_Node (Scop));
9114 Ensure_Freeze_Node (Act_Id);
9115 F_Node := Freeze_Node (Act_Id);
9116 if Present (Body_Id) then
9117 Set_Is_Frozen (Act_Id, False);
9118 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9119 if Is_List_Member (F_Node) then
9123 Insert_After (Act_Body, F_Node);
9129 -- If the body is a subunit, the freeze point is the corresponding stub
9130 -- in the current compilation, not the subunit itself.
9132 if Nkind (Parent (Gen_Body)) = N_Subunit then
9133 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9135 Orig_Body := Gen_Body;
9138 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9140 -- If the instantiation and the generic definition appear in the same
9141 -- package declaration, this is an early instantiation. If they appear
9142 -- in the same declarative part, it is an early instantiation only if
9143 -- the generic body appears textually later, and the generic body is
9144 -- also in the main unit.
9146 -- If instance is nested within a subprogram, and the generic body
9147 -- is not, the instance is delayed because the enclosing body is. If
9148 -- instance and body are within the same scope, or the same subprogram
9149 -- body, indicate explicitly that the instance is delayed.
9152 (Gen_Unit = Act_Unit
9153 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9154 N_Package_Declaration)
9155 or else (Gen_Unit = Body_Unit
9156 and then True_Sloc (N, Act_Unit)
9157 < Sloc (Orig_Body)))
9158 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9159 and then In_Same_Scope (Gen_Id, Act_Id));
9161 -- If this is an early instantiation, the freeze node is placed after
9162 -- the generic body. Otherwise, if the generic appears in an instance,
9163 -- we cannot freeze the current instance until the outer one is frozen.
9164 -- This is only relevant if the current instance is nested within some
9165 -- inner scope not itself within the outer instance. If this scope is
9166 -- a package body in the same declarative part as the outer instance,
9167 -- then that body needs to be frozen after the outer instance. Finally,
9168 -- if no delay is needed, we place the freeze node at the end of the
9169 -- current declarative part.
9171 if Expander_Active then
9172 Ensure_Freeze_Node (Act_Id);
9173 F_Node := Freeze_Node (Act_Id);
9176 Insert_After (Orig_Body, F_Node);
9178 elsif Is_Generic_Instance (Par)
9179 and then Present (Freeze_Node (Par))
9180 and then Scope (Act_Id) /= Par
9182 -- Freeze instance of inner generic after instance of enclosing
9185 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9187 -- Handle the following case:
9189 -- package Parent_Inst is new ...
9192 -- procedure P ... -- this body freezes Parent_Inst
9194 -- package Inst is new ...
9196 -- In this particular scenario, the freeze node for Inst must
9197 -- be inserted in the same manner as that of Parent_Inst,
9198 -- before the next source body or at the end of the declarative
9199 -- list (body not available). If body P did not exist and
9200 -- Parent_Inst was frozen after Inst, either by a body
9201 -- following Inst or at the end of the declarative region,
9202 -- the freeze node for Inst must be inserted after that of
9203 -- Parent_Inst. This relation is established by comparing
9204 -- the Slocs of Parent_Inst freeze node and Inst.
9206 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9208 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9210 Insert_Freeze_Node_For_Instance (N, F_Node);
9212 Insert_After (Freeze_Node (Par), F_Node);
9215 -- Freeze package enclosing instance of inner generic after
9216 -- instance of enclosing generic.
9218 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9219 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9222 Enclosing : Entity_Id;
9225 Enclosing := Corresponding_Spec (Parent (N));
9227 if No (Enclosing) then
9228 Enclosing := Defining_Entity (Parent (N));
9231 Insert_Freeze_Node_For_Instance (N, F_Node);
9232 Ensure_Freeze_Node (Enclosing);
9234 if not Is_List_Member (Freeze_Node (Enclosing)) then
9236 -- The enclosing context is a subunit, insert the freeze
9237 -- node after the stub.
9239 if Nkind (Parent (Parent (N))) = N_Subunit then
9240 Insert_Freeze_Node_For_Instance
9241 (Corresponding_Stub (Parent (Parent (N))),
9242 Freeze_Node (Enclosing));
9244 -- The enclosing context is a package with a stub body
9245 -- which has already been replaced by the real body.
9246 -- Insert the freeze node after the actual body.
9248 elsif Ekind (Enclosing) = E_Package
9249 and then Present (Body_Entity (Enclosing))
9250 and then Was_Originally_Stub
9251 (Parent (Body_Entity (Enclosing)))
9253 Insert_Freeze_Node_For_Instance
9254 (Parent (Body_Entity (Enclosing)),
9255 Freeze_Node (Enclosing));
9257 -- The parent instance has been frozen before the body of
9258 -- the enclosing package, insert the freeze node after
9261 elsif List_Containing (Freeze_Node (Par)) =
9262 List_Containing (Parent (N))
9263 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9265 Insert_Freeze_Node_For_Instance
9266 (Parent (N), Freeze_Node (Enclosing));
9270 (Freeze_Node (Par), Freeze_Node (Enclosing));
9276 Insert_Freeze_Node_For_Instance (N, F_Node);
9280 Insert_Freeze_Node_For_Instance (N, F_Node);
9284 Set_Is_Frozen (Act_Id);
9285 Insert_Before (N, Act_Body);
9286 Mark_Rewrite_Insertion (Act_Body);
9289 -----------------------------
9290 -- Install_Formal_Packages --
9291 -----------------------------
9293 procedure Install_Formal_Packages (Par : Entity_Id) is
9296 Gen_E : Entity_Id := Empty;
9299 E := First_Entity (Par);
9301 -- If we are installing an instance parent, locate the formal packages
9302 -- of its generic parent.
9304 if Is_Generic_Instance (Par) then
9305 Gen := Generic_Parent (Package_Specification (Par));
9306 Gen_E := First_Entity (Gen);
9309 while Present (E) loop
9310 if Ekind (E) = E_Package
9311 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9313 -- If this is the renaming for the parent instance, done
9315 if Renamed_Object (E) = Par then
9318 -- The visibility of a formal of an enclosing generic is already
9321 elsif Denotes_Formal_Package (E) then
9324 elsif Present (Associated_Formal_Package (E)) then
9325 Check_Generic_Actuals (Renamed_Object (E), True);
9326 Set_Is_Hidden (E, False);
9328 -- Find formal package in generic unit that corresponds to
9329 -- (instance of) formal package in instance.
9331 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9332 Next_Entity (Gen_E);
9335 if Present (Gen_E) then
9336 Map_Formal_Package_Entities (Gen_E, E);
9343 if Present (Gen_E) then
9344 Next_Entity (Gen_E);
9347 end Install_Formal_Packages;
9349 --------------------
9350 -- Install_Parent --
9351 --------------------
9353 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9354 Ancestors : constant Elist_Id := New_Elmt_List;
9355 S : constant Entity_Id := Current_Scope;
9356 Inst_Par : Entity_Id;
9357 First_Par : Entity_Id;
9358 Inst_Node : Node_Id;
9359 Gen_Par : Entity_Id;
9360 First_Gen : Entity_Id;
9363 procedure Install_Noninstance_Specs (Par : Entity_Id);
9364 -- Install the scopes of noninstance parent units ending with Par
9366 procedure Install_Spec (Par : Entity_Id);
9367 -- The child unit is within the declarative part of the parent, so the
9368 -- declarations within the parent are immediately visible.
9370 -------------------------------
9371 -- Install_Noninstance_Specs --
9372 -------------------------------
9374 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9377 and then Par /= Standard_Standard
9378 and then not In_Open_Scopes (Par)
9380 Install_Noninstance_Specs (Scope (Par));
9383 end Install_Noninstance_Specs;
9389 procedure Install_Spec (Par : Entity_Id) is
9390 Spec : constant Node_Id := Package_Specification (Par);
9393 -- If this parent of the child instance is a top-level unit,
9394 -- then record the unit and its visibility for later resetting in
9395 -- Remove_Parent. We exclude units that are generic instances, as we
9396 -- only want to record this information for the ultimate top-level
9397 -- noninstance parent (is that always correct???).
9399 if Scope (Par) = Standard_Standard
9400 and then not Is_Generic_Instance (Par)
9402 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9403 Instance_Parent_Unit := Par;
9406 -- Open the parent scope and make it and its declarations visible.
9407 -- If this point is not within a body, then only the visible
9408 -- declarations should be made visible, and installation of the
9409 -- private declarations is deferred until the appropriate point
9410 -- within analysis of the spec being instantiated (see the handling
9411 -- of parent visibility in Analyze_Package_Specification). This is
9412 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9413 -- private view problems that occur when compiling instantiations of
9414 -- a generic child of that package (Generic_Dispatching_Constructor).
9415 -- If the instance freezes a tagged type, inlinings of operations
9416 -- from Ada.Tags may need the full view of type Tag. If inlining took
9417 -- proper account of establishing visibility of inlined subprograms'
9418 -- parents then it should be possible to remove this
9419 -- special check. ???
9422 Set_Is_Immediately_Visible (Par);
9423 Install_Visible_Declarations (Par);
9424 Set_Use (Visible_Declarations (Spec));
9426 if In_Body or else Is_RTU (Par, Ada_Tags) then
9427 Install_Private_Declarations (Par);
9428 Set_Use (Private_Declarations (Spec));
9432 -- Start of processing for Install_Parent
9435 -- We need to install the parent instance to compile the instantiation
9436 -- of the child, but the child instance must appear in the current
9437 -- scope. Given that we cannot place the parent above the current scope
9438 -- in the scope stack, we duplicate the current scope and unstack both
9439 -- after the instantiation is complete.
9441 -- If the parent is itself the instantiation of a child unit, we must
9442 -- also stack the instantiation of its parent, and so on. Each such
9443 -- ancestor is the prefix of the name in a prior instantiation.
9445 -- If this is a nested instance, the parent unit itself resolves to
9446 -- a renaming of the parent instance, whose declaration we need.
9448 -- Finally, the parent may be a generic (not an instance) when the
9449 -- child unit appears as a formal package.
9453 if Present (Renamed_Entity (Inst_Par)) then
9454 Inst_Par := Renamed_Entity (Inst_Par);
9457 First_Par := Inst_Par;
9459 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9461 First_Gen := Gen_Par;
9463 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9465 -- Load grandparent instance as well
9467 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9469 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9470 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9472 if Present (Renamed_Entity (Inst_Par)) then
9473 Inst_Par := Renamed_Entity (Inst_Par);
9476 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9478 if Present (Gen_Par) then
9479 Prepend_Elmt (Inst_Par, Ancestors);
9482 -- Parent is not the name of an instantiation
9484 Install_Noninstance_Specs (Inst_Par);
9495 if Present (First_Gen) then
9496 Append_Elmt (First_Par, Ancestors);
9498 Install_Noninstance_Specs (First_Par);
9501 if not Is_Empty_Elmt_List (Ancestors) then
9502 Elmt := First_Elmt (Ancestors);
9503 while Present (Elmt) loop
9504 Install_Spec (Node (Elmt));
9505 Install_Formal_Packages (Node (Elmt));
9515 -------------------------------
9516 -- Install_Hidden_Primitives --
9517 -------------------------------
9519 procedure Install_Hidden_Primitives
9520 (Prims_List : in out Elist_Id;
9525 List : Elist_Id := No_Elist;
9526 Prim_G_Elmt : Elmt_Id;
9527 Prim_A_Elmt : Elmt_Id;
9532 -- No action needed in case of serious errors because we cannot trust
9533 -- in the order of primitives
9535 if Serious_Errors_Detected > 0 then
9538 -- No action possible if we don't have available the list of primitive
9542 or else not Is_Record_Type (Gen_T)
9543 or else not Is_Tagged_Type (Gen_T)
9544 or else not Is_Record_Type (Act_T)
9545 or else not Is_Tagged_Type (Act_T)
9549 -- There is no need to handle interface types since their primitives
9552 elsif Is_Interface (Gen_T) then
9556 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9558 if not Is_Class_Wide_Type (Act_T) then
9559 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9561 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9565 -- Skip predefined primitives in the generic formal
9567 while Present (Prim_G_Elmt)
9568 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9570 Next_Elmt (Prim_G_Elmt);
9573 -- Skip predefined primitives in the generic actual
9575 while Present (Prim_A_Elmt)
9576 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9578 Next_Elmt (Prim_A_Elmt);
9581 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9583 Prim_G := Node (Prim_G_Elmt);
9584 Prim_A := Node (Prim_A_Elmt);
9586 -- There is no need to handle interface primitives because their
9587 -- primitives are not hidden
9589 exit when Present (Interface_Alias (Prim_G));
9591 -- Here we install one hidden primitive
9593 if Chars (Prim_G) /= Chars (Prim_A)
9594 and then Has_Suffix (Prim_A, 'P')
9595 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9597 Set_Chars (Prim_A, Chars (Prim_G));
9598 Append_New_Elmt (Prim_A, To => List);
9601 Next_Elmt (Prim_A_Elmt);
9602 Next_Elmt (Prim_G_Elmt);
9605 -- Append the elements to the list of temporarily visible primitives
9606 -- avoiding duplicates.
9608 if Present (List) then
9609 if No (Prims_List) then
9610 Prims_List := New_Elmt_List;
9613 Elmt := First_Elmt (List);
9614 while Present (Elmt) loop
9615 Append_Unique_Elmt (Node (Elmt), Prims_List);
9619 end Install_Hidden_Primitives;
9621 -------------------------------
9622 -- Restore_Hidden_Primitives --
9623 -------------------------------
9625 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9626 Prim_Elmt : Elmt_Id;
9630 if Prims_List /= No_Elist then
9631 Prim_Elmt := First_Elmt (Prims_List);
9632 while Present (Prim_Elmt) loop
9633 Prim := Node (Prim_Elmt);
9634 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9635 Next_Elmt (Prim_Elmt);
9638 Prims_List := No_Elist;
9640 end Restore_Hidden_Primitives;
9642 --------------------------------
9643 -- Instantiate_Formal_Package --
9644 --------------------------------
9646 function Instantiate_Formal_Package
9649 Analyzed_Formal : Node_Id) return List_Id
9651 Loc : constant Source_Ptr := Sloc (Actual);
9652 Actual_Pack : Entity_Id;
9653 Formal_Pack : Entity_Id;
9654 Gen_Parent : Entity_Id;
9657 Parent_Spec : Node_Id;
9659 procedure Find_Matching_Actual
9661 Act : in out Entity_Id);
9662 -- We need to associate each formal entity in the formal package with
9663 -- the corresponding entity in the actual package. The actual package
9664 -- has been analyzed and possibly expanded, and as a result there is
9665 -- no one-to-one correspondence between the two lists (for example,
9666 -- the actual may include subtypes, itypes, and inherited primitive
9667 -- operations, interspersed among the renaming declarations for the
9668 -- actuals). We retrieve the corresponding actual by name because each
9669 -- actual has the same name as the formal, and they do appear in the
9672 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9673 -- Retrieve entity of defining entity of generic formal parameter.
9674 -- Only the declarations of formals need to be considered when
9675 -- linking them to actuals, but the declarative list may include
9676 -- internal entities generated during analysis, and those are ignored.
9678 procedure Match_Formal_Entity
9679 (Formal_Node : Node_Id;
9680 Formal_Ent : Entity_Id;
9681 Actual_Ent : Entity_Id);
9682 -- Associates the formal entity with the actual. In the case where
9683 -- Formal_Ent is a formal package, this procedure iterates through all
9684 -- of its formals and enters associations between the actuals occurring
9685 -- in the formal package's corresponding actual package (given by
9686 -- Actual_Ent) and the formal package's formal parameters. This
9687 -- procedure recurses if any of the parameters is itself a package.
9689 function Is_Instance_Of
9690 (Act_Spec : Entity_Id;
9691 Gen_Anc : Entity_Id) return Boolean;
9692 -- The actual can be an instantiation of a generic within another
9693 -- instance, in which case there is no direct link from it to the
9694 -- original generic ancestor. In that case, we recognize that the
9695 -- ultimate ancestor is the same by examining names and scopes.
9697 procedure Process_Nested_Formal (Formal : Entity_Id);
9698 -- If the current formal is declared with a box, its own formals are
9699 -- visible in the instance, as they were in the generic, and their
9700 -- Hidden flag must be reset. If some of these formals are themselves
9701 -- packages declared with a box, the processing must be recursive.
9703 --------------------------
9704 -- Find_Matching_Actual --
9705 --------------------------
9707 procedure Find_Matching_Actual
9709 Act : in out Entity_Id)
9711 Formal_Ent : Entity_Id;
9714 case Nkind (Original_Node (F)) is
9715 when N_Formal_Object_Declaration
9716 | N_Formal_Type_Declaration
9718 Formal_Ent := Defining_Identifier (F);
9720 while Chars (Act) /= Chars (Formal_Ent) loop
9724 when N_Formal_Package_Declaration
9725 | N_Formal_Subprogram_Declaration
9726 | N_Generic_Package_Declaration
9727 | N_Package_Declaration
9729 Formal_Ent := Defining_Entity (F);
9731 while Chars (Act) /= Chars (Formal_Ent) loop
9736 raise Program_Error;
9738 end Find_Matching_Actual;
9740 -------------------------
9741 -- Match_Formal_Entity --
9742 -------------------------
9744 procedure Match_Formal_Entity
9745 (Formal_Node : Node_Id;
9746 Formal_Ent : Entity_Id;
9747 Actual_Ent : Entity_Id)
9749 Act_Pkg : Entity_Id;
9752 Set_Instance_Of (Formal_Ent, Actual_Ent);
9754 if Ekind (Actual_Ent) = E_Package then
9756 -- Record associations for each parameter
9758 Act_Pkg := Actual_Ent;
9761 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9770 -- Retrieve the actual given in the formal package declaration
9772 Actual := Entity (Name (Original_Node (Formal_Node)));
9774 -- The actual in the formal package declaration may be a
9775 -- renamed generic package, in which case we want to retrieve
9776 -- the original generic in order to traverse its formal part.
9778 if Present (Renamed_Entity (Actual)) then
9779 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9781 Gen_Decl := Unit_Declaration_Node (Actual);
9784 Formals := Generic_Formal_Declarations (Gen_Decl);
9786 if Present (Formals) then
9787 F_Node := First_Non_Pragma (Formals);
9792 while Present (A_Ent)
9793 and then Present (F_Node)
9794 and then A_Ent /= First_Private_Entity (Act_Pkg)
9796 F_Ent := Get_Formal_Entity (F_Node);
9798 if Present (F_Ent) then
9800 -- This is a formal of the original package. Record
9801 -- association and recurse.
9803 Find_Matching_Actual (F_Node, A_Ent);
9804 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9805 Next_Entity (A_Ent);
9808 Next_Non_Pragma (F_Node);
9812 end Match_Formal_Entity;
9814 -----------------------
9815 -- Get_Formal_Entity --
9816 -----------------------
9818 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9819 Kind : constant Node_Kind := Nkind (Original_Node (N));
9822 when N_Formal_Object_Declaration =>
9823 return Defining_Identifier (N);
9825 when N_Formal_Type_Declaration =>
9826 return Defining_Identifier (N);
9828 when N_Formal_Subprogram_Declaration =>
9829 return Defining_Unit_Name (Specification (N));
9831 when N_Formal_Package_Declaration =>
9832 return Defining_Identifier (Original_Node (N));
9834 when N_Generic_Package_Declaration =>
9835 return Defining_Identifier (Original_Node (N));
9837 -- All other declarations are introduced by semantic analysis and
9838 -- have no match in the actual.
9843 end Get_Formal_Entity;
9845 --------------------
9846 -- Is_Instance_Of --
9847 --------------------
9849 function Is_Instance_Of
9850 (Act_Spec : Entity_Id;
9851 Gen_Anc : Entity_Id) return Boolean
9853 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9856 if No (Gen_Par) then
9859 -- Simplest case: the generic parent of the actual is the formal
9861 elsif Gen_Par = Gen_Anc then
9864 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9867 -- The actual may be obtained through several instantiations. Its
9868 -- scope must itself be an instance of a generic declared in the
9869 -- same scope as the formal. Any other case is detected above.
9871 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9875 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9879 ---------------------------
9880 -- Process_Nested_Formal --
9881 ---------------------------
9883 procedure Process_Nested_Formal (Formal : Entity_Id) is
9887 if Present (Associated_Formal_Package (Formal))
9888 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9890 Ent := First_Entity (Formal);
9891 while Present (Ent) loop
9892 Set_Is_Hidden (Ent, False);
9893 Set_Is_Visible_Formal (Ent);
9894 Set_Is_Potentially_Use_Visible
9895 (Ent, Is_Potentially_Use_Visible (Formal));
9897 if Ekind (Ent) = E_Package then
9898 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9899 Process_Nested_Formal (Ent);
9905 end Process_Nested_Formal;
9907 -- Start of processing for Instantiate_Formal_Package
9912 if not Is_Entity_Name (Actual)
9913 or else Ekind (Entity (Actual)) /= E_Package
9916 ("expect package instance to instantiate formal", Actual);
9917 Abandon_Instantiation (Actual);
9918 raise Program_Error;
9921 Actual_Pack := Entity (Actual);
9922 Set_Is_Instantiated (Actual_Pack);
9924 -- The actual may be a renamed package, or an outer generic formal
9925 -- package whose instantiation is converted into a renaming.
9927 if Present (Renamed_Object (Actual_Pack)) then
9928 Actual_Pack := Renamed_Object (Actual_Pack);
9931 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9932 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9933 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9936 Generic_Parent (Specification (Analyzed_Formal));
9938 Defining_Unit_Name (Specification (Analyzed_Formal));
9941 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9942 Parent_Spec := Package_Specification (Actual_Pack);
9944 Parent_Spec := Parent (Actual_Pack);
9947 if Gen_Parent = Any_Id then
9949 ("previous error in declaration of formal package", Actual);
9950 Abandon_Instantiation (Actual);
9953 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9959 ("actual parameter must be instance of&", Actual, Gen_Parent);
9960 Abandon_Instantiation (Actual);
9963 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9964 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9967 Make_Package_Renaming_Declaration (Loc,
9968 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9969 Name => New_Occurrence_Of (Actual_Pack, Loc));
9971 Set_Associated_Formal_Package
9972 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9973 Decls := New_List (Nod);
9975 -- If the formal F has a box, then the generic declarations are
9976 -- visible in the generic G. In an instance of G, the corresponding
9977 -- entities in the actual for F (which are the actuals for the
9978 -- instantiation of the generic that F denotes) must also be made
9979 -- visible for analysis of the current instance. On exit from the
9980 -- current instance, those entities are made private again. If the
9981 -- actual is currently in use, these entities are also use-visible.
9983 -- The loop through the actual entities also steps through the formal
9984 -- entities and enters associations from formals to actuals into the
9985 -- renaming map. This is necessary to properly handle checking of
9986 -- actual parameter associations for later formals that depend on
9987 -- actuals declared in the formal package.
9989 -- In Ada 2005, partial parameterization requires that we make
9990 -- visible the actuals corresponding to formals that were defaulted
9991 -- in the formal package. There formals are identified because they
9992 -- remain formal generics within the formal package, rather than
9993 -- being renamings of the actuals supplied.
9996 Gen_Decl : constant Node_Id :=
9997 Unit_Declaration_Node (Gen_Parent);
9998 Formals : constant List_Id :=
9999 Generic_Formal_Declarations (Gen_Decl);
10001 Actual_Ent : Entity_Id;
10002 Actual_Of_Formal : Node_Id;
10003 Formal_Node : Node_Id;
10004 Formal_Ent : Entity_Id;
10007 if Present (Formals) then
10008 Formal_Node := First_Non_Pragma (Formals);
10010 Formal_Node := Empty;
10013 Actual_Ent := First_Entity (Actual_Pack);
10014 Actual_Of_Formal :=
10015 First (Visible_Declarations (Specification (Analyzed_Formal)));
10016 while Present (Actual_Ent)
10017 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10019 if Present (Formal_Node) then
10020 Formal_Ent := Get_Formal_Entity (Formal_Node);
10022 if Present (Formal_Ent) then
10023 Find_Matching_Actual (Formal_Node, Actual_Ent);
10024 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10026 -- We iterate at the same time over the actuals of the
10027 -- local package created for the formal, to determine
10028 -- which one of the formals of the original generic were
10029 -- defaulted in the formal. The corresponding actual
10030 -- entities are visible in the enclosing instance.
10032 if Box_Present (Formal)
10034 (Present (Actual_Of_Formal)
10037 (Get_Formal_Entity (Actual_Of_Formal)))
10039 Set_Is_Hidden (Actual_Ent, False);
10040 Set_Is_Visible_Formal (Actual_Ent);
10041 Set_Is_Potentially_Use_Visible
10042 (Actual_Ent, In_Use (Actual_Pack));
10044 if Ekind (Actual_Ent) = E_Package then
10045 Process_Nested_Formal (Actual_Ent);
10049 Set_Is_Hidden (Actual_Ent);
10050 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10054 Next_Non_Pragma (Formal_Node);
10055 Next (Actual_Of_Formal);
10058 -- No further formals to match, but the generic part may
10059 -- contain inherited operation that are not hidden in the
10060 -- enclosing instance.
10062 Next_Entity (Actual_Ent);
10066 -- Inherited subprograms generated by formal derived types are
10067 -- also visible if the types are.
10069 Actual_Ent := First_Entity (Actual_Pack);
10070 while Present (Actual_Ent)
10071 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10073 if Is_Overloadable (Actual_Ent)
10075 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10077 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10079 Set_Is_Hidden (Actual_Ent, False);
10080 Set_Is_Potentially_Use_Visible
10081 (Actual_Ent, In_Use (Actual_Pack));
10084 Next_Entity (Actual_Ent);
10088 -- If the formal is not declared with a box, reanalyze it as an
10089 -- abbreviated instantiation, to verify the matching rules of 12.7.
10090 -- The actual checks are performed after the generic associations
10091 -- have been analyzed, to guarantee the same visibility for this
10092 -- instantiation and for the actuals.
10094 -- In Ada 2005, the generic associations for the formal can include
10095 -- defaulted parameters. These are ignored during check. This
10096 -- internal instantiation is removed from the tree after conformance
10097 -- checking, because it contains formal declarations for those
10098 -- defaulted parameters, and those should not reach the back-end.
10100 if not Box_Present (Formal) then
10102 I_Pack : constant Entity_Id :=
10103 Make_Temporary (Sloc (Actual), 'P');
10106 Set_Is_Internal (I_Pack);
10109 Make_Package_Instantiation (Sloc (Actual),
10110 Defining_Unit_Name => I_Pack,
10113 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10114 Generic_Associations => Generic_Associations (Formal)));
10120 end Instantiate_Formal_Package;
10122 -----------------------------------
10123 -- Instantiate_Formal_Subprogram --
10124 -----------------------------------
10126 function Instantiate_Formal_Subprogram
10129 Analyzed_Formal : Node_Id) return Node_Id
10131 Analyzed_S : constant Entity_Id :=
10132 Defining_Unit_Name (Specification (Analyzed_Formal));
10133 Formal_Sub : constant Entity_Id :=
10134 Defining_Unit_Name (Specification (Formal));
10136 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10137 -- If the generic is a child unit, the parent has been installed on the
10138 -- scope stack, but a default subprogram cannot resolve to something
10139 -- on the parent because that parent is not really part of the visible
10140 -- context (it is there to resolve explicit local entities). If the
10141 -- default has resolved in this way, we remove the entity from immediate
10142 -- visibility and analyze the node again to emit an error message or
10143 -- find another visible candidate.
10145 procedure Valid_Actual_Subprogram (Act : Node_Id);
10146 -- Perform legality check and raise exception on failure
10148 -----------------------
10149 -- From_Parent_Scope --
10150 -----------------------
10152 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10153 Gen_Scope : Node_Id;
10156 Gen_Scope := Scope (Analyzed_S);
10157 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10158 if Scope (Subp) = Scope (Gen_Scope) then
10162 Gen_Scope := Scope (Gen_Scope);
10166 end From_Parent_Scope;
10168 -----------------------------
10169 -- Valid_Actual_Subprogram --
10170 -----------------------------
10172 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10176 if Is_Entity_Name (Act) then
10177 Act_E := Entity (Act);
10179 elsif Nkind (Act) = N_Selected_Component
10180 and then Is_Entity_Name (Selector_Name (Act))
10182 Act_E := Entity (Selector_Name (Act));
10188 if (Present (Act_E) and then Is_Overloadable (Act_E))
10189 or else Nkind_In (Act, N_Attribute_Reference,
10190 N_Indexed_Component,
10191 N_Character_Literal,
10192 N_Explicit_Dereference)
10198 ("expect subprogram or entry name in instantiation of &",
10199 Instantiation_Node, Formal_Sub);
10200 Abandon_Instantiation (Instantiation_Node);
10201 end Valid_Actual_Subprogram;
10205 Decl_Node : Node_Id;
10208 New_Spec : Node_Id;
10209 New_Subp : Entity_Id;
10211 -- Start of processing for Instantiate_Formal_Subprogram
10214 New_Spec := New_Copy_Tree (Specification (Formal));
10216 -- The tree copy has created the proper instantiation sloc for the
10217 -- new specification. Use this location for all other constructed
10220 Loc := Sloc (Defining_Unit_Name (New_Spec));
10222 -- Create new entity for the actual (New_Copy_Tree does not), and
10223 -- indicate that it is an actual.
10225 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10226 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10227 Set_Is_Generic_Actual_Subprogram (New_Subp);
10228 Set_Defining_Unit_Name (New_Spec, New_Subp);
10230 -- Create new entities for the each of the formals in the specification
10231 -- of the renaming declaration built for the actual.
10233 if Present (Parameter_Specifications (New_Spec)) then
10239 F := First (Parameter_Specifications (New_Spec));
10240 while Present (F) loop
10241 F_Id := Defining_Identifier (F);
10243 Set_Defining_Identifier (F,
10244 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10250 -- Find entity of actual. If the actual is an attribute reference, it
10251 -- cannot be resolved here (its formal is missing) but is handled
10252 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10253 -- fully resolved subsequently, when the renaming declaration for the
10254 -- formal is analyzed. If it is an explicit dereference, resolve the
10255 -- prefix but not the actual itself, to prevent interpretation as call.
10257 if Present (Actual) then
10258 Loc := Sloc (Actual);
10259 Set_Sloc (New_Spec, Loc);
10261 if Nkind (Actual) = N_Operator_Symbol then
10262 Find_Direct_Name (Actual);
10264 elsif Nkind (Actual) = N_Explicit_Dereference then
10265 Analyze (Prefix (Actual));
10267 elsif Nkind (Actual) /= N_Attribute_Reference then
10271 Valid_Actual_Subprogram (Actual);
10274 elsif Present (Default_Name (Formal)) then
10275 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10276 N_Selected_Component,
10277 N_Indexed_Component,
10278 N_Character_Literal)
10279 and then Present (Entity (Default_Name (Formal)))
10281 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10283 Nam := New_Copy (Default_Name (Formal));
10284 Set_Sloc (Nam, Loc);
10287 elsif Box_Present (Formal) then
10289 -- Actual is resolved at the point of instantiation. Create an
10290 -- identifier or operator with the same name as the formal.
10292 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10294 Make_Operator_Symbol (Loc,
10295 Chars => Chars (Formal_Sub),
10296 Strval => No_String);
10298 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10301 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10302 and then Null_Present (Specification (Formal))
10304 -- Generate null body for procedure, for use in the instance
10307 Make_Subprogram_Body (Loc,
10308 Specification => New_Spec,
10309 Declarations => New_List,
10310 Handled_Statement_Sequence =>
10311 Make_Handled_Sequence_Of_Statements (Loc,
10312 Statements => New_List (Make_Null_Statement (Loc))));
10314 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10318 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10320 ("missing actual&", Instantiation_Node, Formal_Sub);
10322 ("\in instantiation of & declared#",
10323 Instantiation_Node, Scope (Analyzed_S));
10324 Abandon_Instantiation (Instantiation_Node);
10328 Make_Subprogram_Renaming_Declaration (Loc,
10329 Specification => New_Spec,
10332 -- If we do not have an actual and the formal specified <> then set to
10333 -- get proper default.
10335 if No (Actual) and then Box_Present (Formal) then
10336 Set_From_Default (Decl_Node);
10339 -- Gather possible interpretations for the actual before analyzing the
10340 -- instance. If overloaded, it will be resolved when analyzing the
10341 -- renaming declaration.
10343 if Box_Present (Formal) and then No (Actual) then
10346 if Is_Child_Unit (Scope (Analyzed_S))
10347 and then Present (Entity (Nam))
10349 if not Is_Overloaded (Nam) then
10350 if From_Parent_Scope (Entity (Nam)) then
10351 Set_Is_Immediately_Visible (Entity (Nam), False);
10352 Set_Entity (Nam, Empty);
10353 Set_Etype (Nam, Empty);
10356 Set_Is_Immediately_Visible (Entity (Nam));
10365 Get_First_Interp (Nam, I, It);
10366 while Present (It.Nam) loop
10367 if From_Parent_Scope (It.Nam) then
10371 Get_Next_Interp (I, It);
10378 -- The generic instantiation freezes the actual. This can only be done
10379 -- once the actual is resolved, in the analysis of the renaming
10380 -- declaration. To make the formal subprogram entity available, we set
10381 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10382 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10383 -- of formal abstract subprograms.
10385 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10387 -- We cannot analyze the renaming declaration, and thus find the actual,
10388 -- until all the actuals are assembled in the instance. For subsequent
10389 -- checks of other actuals, indicate the node that will hold the
10390 -- instance of this formal.
10392 Set_Instance_Of (Analyzed_S, Nam);
10394 if Nkind (Actual) = N_Selected_Component
10395 and then Is_Task_Type (Etype (Prefix (Actual)))
10396 and then not Is_Frozen (Etype (Prefix (Actual)))
10398 -- The renaming declaration will create a body, which must appear
10399 -- outside of the instantiation, We move the renaming declaration
10400 -- out of the instance, and create an additional renaming inside,
10401 -- to prevent freezing anomalies.
10404 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10407 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10408 Insert_Before (Instantiation_Node, Decl_Node);
10409 Analyze (Decl_Node);
10411 -- Now create renaming within the instance
10414 Make_Subprogram_Renaming_Declaration (Loc,
10415 Specification => New_Copy_Tree (New_Spec),
10416 Name => New_Occurrence_Of (Anon_Id, Loc));
10418 Set_Defining_Unit_Name (Specification (Decl_Node),
10419 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10424 end Instantiate_Formal_Subprogram;
10426 ------------------------
10427 -- Instantiate_Object --
10428 ------------------------
10430 function Instantiate_Object
10433 Analyzed_Formal : Node_Id) return List_Id
10435 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10436 A_Gen_Obj : constant Entity_Id :=
10437 Defining_Identifier (Analyzed_Formal);
10438 Acc_Def : Node_Id := Empty;
10439 Act_Assoc : constant Node_Id := Parent (Actual);
10440 Actual_Decl : Node_Id := Empty;
10441 Decl_Node : Node_Id;
10444 List : constant List_Id := New_List;
10445 Loc : constant Source_Ptr := Sloc (Actual);
10446 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10447 Subt_Decl : Node_Id := Empty;
10448 Subt_Mark : Node_Id := Empty;
10450 function Copy_Access_Def return Node_Id;
10451 -- If formal is an anonymous access, copy access definition of formal
10452 -- for generated object declaration.
10454 ---------------------
10455 -- Copy_Access_Def --
10456 ---------------------
10458 function Copy_Access_Def return Node_Id is
10460 Def := New_Copy_Tree (Acc_Def);
10462 -- In addition, if formal is an access to subprogram we need to
10463 -- generate new formals for the signature of the default, so that
10464 -- the tree is properly formatted for ASIS use.
10466 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10468 Par_Spec : Node_Id;
10471 First (Parameter_Specifications
10472 (Access_To_Subprogram_Definition (Def)));
10473 while Present (Par_Spec) loop
10474 Set_Defining_Identifier (Par_Spec,
10475 Make_Defining_Identifier (Sloc (Acc_Def),
10476 Chars => Chars (Defining_Identifier (Par_Spec))));
10483 end Copy_Access_Def;
10485 -- Start of processing for Instantiate_Object
10488 -- Formal may be an anonymous access
10490 if Present (Subtype_Mark (Formal)) then
10491 Subt_Mark := Subtype_Mark (Formal);
10493 Check_Access_Definition (Formal);
10494 Acc_Def := Access_Definition (Formal);
10497 -- Sloc for error message on missing actual
10499 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10501 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10502 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10505 Set_Parent (List, Parent (Actual));
10509 if Out_Present (Formal) then
10511 -- An IN OUT generic actual must be a name. The instantiation is a
10512 -- renaming declaration. The actual is the name being renamed. We
10513 -- use the actual directly, rather than a copy, because it is not
10514 -- used further in the list of actuals, and because a copy or a use
10515 -- of relocate_node is incorrect if the instance is nested within a
10516 -- generic. In order to simplify ASIS searches, the Generic_Parent
10517 -- field links the declaration to the generic association.
10519 if No (Actual) then
10521 ("missing actual &",
10522 Instantiation_Node, Gen_Obj);
10524 ("\in instantiation of & declared#",
10525 Instantiation_Node, Scope (A_Gen_Obj));
10526 Abandon_Instantiation (Instantiation_Node);
10529 if Present (Subt_Mark) then
10531 Make_Object_Renaming_Declaration (Loc,
10532 Defining_Identifier => New_Copy (Gen_Obj),
10533 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10536 else pragma Assert (Present (Acc_Def));
10538 Make_Object_Renaming_Declaration (Loc,
10539 Defining_Identifier => New_Copy (Gen_Obj),
10540 Access_Definition => New_Copy_Tree (Acc_Def),
10544 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10546 -- The analysis of the actual may produce Insert_Action nodes, so
10547 -- the declaration must have a context in which to attach them.
10549 Append (Decl_Node, List);
10552 -- Return if the analysis of the actual reported some error
10554 if Etype (Actual) = Any_Type then
10558 -- This check is performed here because Analyze_Object_Renaming will
10559 -- not check it when Comes_From_Source is False. Note though that the
10560 -- check for the actual being the name of an object will be performed
10561 -- in Analyze_Object_Renaming.
10563 if Is_Object_Reference (Actual)
10564 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10567 ("illegal discriminant-dependent component for in out parameter",
10571 -- The actual has to be resolved in order to check that it is a
10572 -- variable (due to cases such as F (1), where F returns access to
10573 -- an array, and for overloaded prefixes).
10575 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10577 -- If the type of the formal is not itself a formal, and the current
10578 -- unit is a child unit, the formal type must be declared in a
10579 -- parent, and must be retrieved by visibility.
10581 if Ftyp = Orig_Ftyp
10582 and then Is_Generic_Unit (Scope (Ftyp))
10583 and then Is_Child_Unit (Scope (A_Gen_Obj))
10586 Temp : constant Node_Id :=
10587 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10589 Set_Entity (Temp, Empty);
10591 Ftyp := Entity (Temp);
10595 if Is_Private_Type (Ftyp)
10596 and then not Is_Private_Type (Etype (Actual))
10597 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10598 or else Base_Type (Etype (Actual)) = Ftyp)
10600 -- If the actual has the type of the full view of the formal, or
10601 -- else a non-private subtype of the formal, then the visibility
10602 -- of the formal type has changed. Add to the actuals a subtype
10603 -- declaration that will force the exchange of views in the body
10604 -- of the instance as well.
10607 Make_Subtype_Declaration (Loc,
10608 Defining_Identifier => Make_Temporary (Loc, 'P'),
10609 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10611 Prepend (Subt_Decl, List);
10613 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10614 Exchange_Declarations (Ftyp);
10617 Resolve (Actual, Ftyp);
10619 if not Denotes_Variable (Actual) then
10620 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10622 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10624 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10625 -- the type of the actual shall resolve to a specific anonymous
10628 if Ada_Version < Ada_2005
10629 or else Ekind (Base_Type (Ftyp)) /=
10630 E_Anonymous_Access_Type
10631 or else Ekind (Base_Type (Etype (Actual))) /=
10632 E_Anonymous_Access_Type
10635 ("type of actual does not match type of&", Actual, Gen_Obj);
10639 Note_Possible_Modification (Actual, Sure => True);
10641 -- Check for instantiation of atomic/volatile actual for
10642 -- non-atomic/volatile formal (RM C.6 (12)).
10644 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10646 ("cannot instantiate non-atomic formal object "
10647 & "with atomic actual", Actual);
10649 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10652 ("cannot instantiate non-volatile formal object "
10653 & "with volatile actual", Actual);
10656 -- Formal in-parameter
10659 -- The instantiation of a generic formal in-parameter is constant
10660 -- declaration. The actual is the expression for that declaration.
10661 -- Its type is a full copy of the type of the formal. This may be
10662 -- an access to subprogram, for which we need to generate entities
10663 -- for the formals in the new signature.
10665 if Present (Actual) then
10666 if Present (Subt_Mark) then
10667 Def := New_Copy_Tree (Subt_Mark);
10668 else pragma Assert (Present (Acc_Def));
10669 Def := Copy_Access_Def;
10673 Make_Object_Declaration (Loc,
10674 Defining_Identifier => New_Copy (Gen_Obj),
10675 Constant_Present => True,
10676 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10677 Object_Definition => Def,
10678 Expression => Actual);
10680 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10682 -- A generic formal object of a tagged type is defined to be
10683 -- aliased so the new constant must also be treated as aliased.
10685 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10686 Set_Aliased_Present (Decl_Node);
10689 Append (Decl_Node, List);
10691 -- No need to repeat (pre-)analysis of some expression nodes
10692 -- already handled in Preanalyze_Actuals.
10694 if Nkind (Actual) /= N_Allocator then
10697 -- Return if the analysis of the actual reported some error
10699 if Etype (Actual) = Any_Type then
10705 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10709 Typ := Get_Instance_Of (Formal_Type);
10711 -- If the actual appears in the current or an enclosing scope,
10712 -- use its type directly. This is relevant if it has an actual
10713 -- subtype that is distinct from its nominal one. This cannot
10714 -- be done in general because the type of the actual may
10715 -- depend on other actuals, and only be fully determined when
10716 -- the enclosing instance is analyzed.
10718 if Present (Etype (Actual))
10719 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10721 Freeze_Before (Instantiation_Node, Etype (Actual));
10723 Freeze_Before (Instantiation_Node, Typ);
10726 -- If the actual is an aggregate, perform name resolution on
10727 -- its components (the analysis of an aggregate does not do it)
10728 -- to capture local names that may be hidden if the generic is
10731 if Nkind (Actual) = N_Aggregate then
10732 Preanalyze_And_Resolve (Actual, Typ);
10735 if Is_Limited_Type (Typ)
10736 and then not OK_For_Limited_Init (Typ, Actual)
10739 ("initialization not allowed for limited types", Actual);
10740 Explain_Limited_Type (Typ, Actual);
10744 elsif Present (Default_Expression (Formal)) then
10746 -- Use default to construct declaration
10748 if Present (Subt_Mark) then
10749 Def := New_Copy (Subt_Mark);
10750 else pragma Assert (Present (Acc_Def));
10751 Def := Copy_Access_Def;
10755 Make_Object_Declaration (Sloc (Formal),
10756 Defining_Identifier => New_Copy (Gen_Obj),
10757 Constant_Present => True,
10758 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10759 Object_Definition => Def,
10760 Expression => New_Copy_Tree
10761 (Default_Expression (Formal)));
10763 Append (Decl_Node, List);
10764 Set_Analyzed (Expression (Decl_Node), False);
10767 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10768 Error_Msg_NE ("\in instantiation of & declared#",
10769 Instantiation_Node, Scope (A_Gen_Obj));
10771 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10773 -- Create dummy constant declaration so that instance can be
10774 -- analyzed, to minimize cascaded visibility errors.
10776 if Present (Subt_Mark) then
10778 else pragma Assert (Present (Acc_Def));
10783 Make_Object_Declaration (Loc,
10784 Defining_Identifier => New_Copy (Gen_Obj),
10785 Constant_Present => True,
10786 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10787 Object_Definition => New_Copy (Def),
10789 Make_Attribute_Reference (Sloc (Gen_Obj),
10790 Attribute_Name => Name_First,
10791 Prefix => New_Copy (Def)));
10793 Append (Decl_Node, List);
10796 Abandon_Instantiation (Instantiation_Node);
10801 if Nkind (Actual) in N_Has_Entity then
10802 Actual_Decl := Parent (Entity (Actual));
10805 -- Ada 2005 (AI-423): For a formal object declaration with a null
10806 -- exclusion or an access definition that has a null exclusion: If the
10807 -- actual matching the formal object declaration denotes a generic
10808 -- formal object of another generic unit G, and the instantiation
10809 -- containing the actual occurs within the body of G or within the body
10810 -- of a generic unit declared within the declarative region of G, then
10811 -- the declaration of the formal object of G must have a null exclusion.
10812 -- Otherwise, the subtype of the actual matching the formal object
10813 -- declaration shall exclude null.
10815 if Ada_Version >= Ada_2005
10816 and then Present (Actual_Decl)
10817 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10818 N_Object_Declaration)
10819 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10820 and then not Has_Null_Exclusion (Actual_Decl)
10821 and then Has_Null_Exclusion (Analyzed_Formal)
10823 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10825 ("actual must exclude null to match generic formal#", Actual);
10828 -- An effectively volatile object cannot be used as an actual in a
10829 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10830 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10831 -- legality rule, and also verifies that the actual is an object.
10834 and then Present (Actual)
10835 and then Is_Object_Reference (Actual)
10836 and then Is_Effectively_Volatile_Object (Actual)
10839 ("volatile object cannot act as actual in generic instantiation",
10844 end Instantiate_Object;
10846 ------------------------------
10847 -- Instantiate_Package_Body --
10848 ------------------------------
10850 -- WARNING: This routine manages Ghost regions. Return statements must be
10851 -- replaced by gotos which jump to the end of the routine and restore the
10854 procedure Instantiate_Package_Body
10855 (Body_Info : Pending_Body_Info;
10856 Inlined_Body : Boolean := False;
10857 Body_Optional : Boolean := False)
10859 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10860 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10861 Act_Spec : constant Node_Id := Specification (Act_Decl);
10862 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10863 Gen_Id : constant Node_Id := Name (Inst_Node);
10864 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10865 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10866 Loc : constant Source_Ptr := Sloc (Inst_Node);
10868 Save_ISMP : constant Boolean :=
10869 Ignore_SPARK_Mode_Pragmas_In_Instance;
10870 Save_Style_Check : constant Boolean := Style_Check;
10872 procedure Check_Initialized_Types;
10873 -- In a generic package body, an entity of a generic private type may
10874 -- appear uninitialized. This is suspicious, unless the actual is a
10875 -- fully initialized type.
10877 -----------------------------
10878 -- Check_Initialized_Types --
10879 -----------------------------
10881 procedure Check_Initialized_Types is
10883 Formal : Entity_Id;
10884 Actual : Entity_Id;
10885 Uninit_Var : Entity_Id;
10888 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10889 while Present (Decl) loop
10890 Uninit_Var := Empty;
10892 if Nkind (Decl) = N_Private_Extension_Declaration then
10893 Uninit_Var := Uninitialized_Variable (Decl);
10895 elsif Nkind (Decl) = N_Formal_Type_Declaration
10896 and then Nkind (Formal_Type_Definition (Decl)) =
10897 N_Formal_Private_Type_Definition
10900 Uninitialized_Variable (Formal_Type_Definition (Decl));
10903 if Present (Uninit_Var) then
10904 Formal := Defining_Identifier (Decl);
10905 Actual := First_Entity (Act_Decl_Id);
10907 -- For each formal there is a subtype declaration that renames
10908 -- the actual and has the same name as the formal. Locate the
10909 -- formal for warning message about uninitialized variables
10910 -- in the generic, for which the actual type should be a fully
10911 -- initialized type.
10913 while Present (Actual) loop
10914 exit when Ekind (Actual) = E_Package
10915 and then Present (Renamed_Object (Actual));
10917 if Chars (Actual) = Chars (Formal)
10918 and then not Is_Scalar_Type (Actual)
10919 and then not Is_Fully_Initialized_Type (Actual)
10920 and then Warn_On_No_Value_Assigned
10922 Error_Msg_Node_2 := Formal;
10924 ("generic unit has uninitialized variable& of "
10925 & "formal private type &?v?", Actual, Uninit_Var);
10927 ("actual type for& should be fully initialized type?v?",
10932 Next_Entity (Actual);
10938 end Check_Initialized_Types;
10942 Act_Body : Node_Id;
10943 Act_Body_Id : Entity_Id;
10944 Act_Body_Name : Node_Id;
10945 Gen_Body : Node_Id;
10946 Gen_Body_Id : Node_Id;
10947 Mode : Ghost_Mode_Type;
10948 Par_Ent : Entity_Id := Empty;
10949 Par_Vis : Boolean := False;
10951 Parent_Installed : Boolean := False;
10953 Vis_Prims_List : Elist_Id := No_Elist;
10954 -- List of primitives made temporarily visible in the instantiation
10955 -- to match the visibility of the formal type.
10957 -- Start of processing for Instantiate_Package_Body
10960 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10962 -- The instance body may already have been processed, as the parent of
10963 -- another instance that is inlined (Load_Parent_Of_Generic).
10965 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10969 -- The package being instantiated may be subject to pragma Ghost. Set
10970 -- the mode now to ensure that any nodes generated during instantiation
10971 -- are properly marked as Ghost.
10973 Set_Ghost_Mode (Act_Decl_Id, Mode);
10975 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10977 -- Re-establish the state of information on which checks are suppressed.
10978 -- This information was set in Body_Info at the point of instantiation,
10979 -- and now we restore it so that the instance is compiled using the
10980 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10982 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10983 Scope_Suppress := Body_Info.Scope_Suppress;
10984 Opt.Ada_Version := Body_Info.Version;
10985 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10986 Restore_Warnings (Body_Info.Warnings);
10987 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10988 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10990 if No (Gen_Body_Id) then
10992 -- Do not look for parent of generic body if none is required.
10993 -- This may happen when the routine is called as part of the
10994 -- Pending_Instantiations processing, when nested instances
10995 -- may precede the one generated from the main unit.
10997 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10998 and then Body_Optional
11002 Load_Parent_Of_Generic
11003 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11004 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11008 -- Establish global variable for sloc adjustment and for error recovery
11009 -- In the case of an instance body for an instantiation with actuals
11010 -- from a limited view, the instance body is placed at the beginning
11011 -- of the enclosing package body: use the body entity as the source
11012 -- location for nodes of the instance body.
11014 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11016 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11017 Body_Id : constant Node_Id :=
11018 Corresponding_Body (Unit_Declaration_Node (Scop));
11021 Instantiation_Node := Body_Id;
11024 Instantiation_Node := Inst_Node;
11027 if Present (Gen_Body_Id) then
11028 Save_Env (Gen_Unit, Act_Decl_Id);
11029 Style_Check := False;
11031 -- If the context of the instance is subject to SPARK_Mode "off", the
11032 -- annotation is missing, or the body is instantiated at a later pass
11033 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11034 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11037 if SPARK_Mode /= On
11038 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11040 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11043 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11044 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11046 Create_Instantiation_Source
11047 (Inst_Node, Gen_Body_Id, S_Adjustment);
11051 (Original_Node (Gen_Body), Empty, Instantiating => True);
11053 -- Create proper (possibly qualified) defining name for the body, to
11054 -- correspond to the one in the spec.
11057 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11058 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11060 -- Some attributes of spec entity are not inherited by body entity
11062 Set_Handler_Records (Act_Body_Id, No_List);
11064 if Nkind (Defining_Unit_Name (Act_Spec)) =
11065 N_Defining_Program_Unit_Name
11068 Make_Defining_Program_Unit_Name (Loc,
11070 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11071 Defining_Identifier => Act_Body_Id);
11073 Act_Body_Name := Act_Body_Id;
11076 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11078 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11079 Check_Generic_Actuals (Act_Decl_Id, False);
11080 Check_Initialized_Types;
11082 -- Install primitives hidden at the point of the instantiation but
11083 -- visible when processing the generic formals
11089 E := First_Entity (Act_Decl_Id);
11090 while Present (E) loop
11092 and then not Is_Itype (E)
11093 and then Is_Generic_Actual_Type (E)
11094 and then Is_Tagged_Type (E)
11096 Install_Hidden_Primitives
11097 (Prims_List => Vis_Prims_List,
11098 Gen_T => Generic_Parent_Type (Parent (E)),
11106 -- If it is a child unit, make the parent instance (which is an
11107 -- instance of the parent of the generic) visible. The parent
11108 -- instance is the prefix of the name of the generic unit.
11110 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11111 and then Nkind (Gen_Id) = N_Expanded_Name
11113 Par_Ent := Entity (Prefix (Gen_Id));
11114 Par_Vis := Is_Immediately_Visible (Par_Ent);
11115 Install_Parent (Par_Ent, In_Body => True);
11116 Parent_Installed := True;
11118 elsif Is_Child_Unit (Gen_Unit) then
11119 Par_Ent := Scope (Gen_Unit);
11120 Par_Vis := Is_Immediately_Visible (Par_Ent);
11121 Install_Parent (Par_Ent, In_Body => True);
11122 Parent_Installed := True;
11125 -- If the instantiation is a library unit, and this is the main unit,
11126 -- then build the resulting compilation unit nodes for the instance.
11127 -- If this is a compilation unit but it is not the main unit, then it
11128 -- is the body of a unit in the context, that is being compiled
11129 -- because it is encloses some inlined unit or another generic unit
11130 -- being instantiated. In that case, this body is not part of the
11131 -- current compilation, and is not attached to the tree, but its
11132 -- parent must be set for analysis.
11134 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11136 -- Replace instance node with body of instance, and create new
11137 -- node for corresponding instance declaration.
11139 Build_Instance_Compilation_Unit_Nodes
11140 (Inst_Node, Act_Body, Act_Decl);
11141 Analyze (Inst_Node);
11143 if Parent (Inst_Node) = Cunit (Main_Unit) then
11145 -- If the instance is a child unit itself, then set the scope
11146 -- of the expanded body to be the parent of the instantiation
11147 -- (ensuring that the fully qualified name will be generated
11148 -- for the elaboration subprogram).
11150 if Nkind (Defining_Unit_Name (Act_Spec)) =
11151 N_Defining_Program_Unit_Name
11153 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11157 -- Case where instantiation is not a library unit
11160 -- If this is an early instantiation, i.e. appears textually
11161 -- before the corresponding body and must be elaborated first,
11162 -- indicate that the body instance is to be delayed.
11164 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11166 -- Now analyze the body. We turn off all checks if this is an
11167 -- internal unit, since there is no reason to have checks on for
11168 -- any predefined run-time library code. All such code is designed
11169 -- to be compiled with checks off.
11171 -- Note that we do NOT apply this criterion to children of GNAT
11172 -- The latter units must suppress checks explicitly if needed.
11174 -- We also do not suppress checks in CodePeer mode where we are
11175 -- interested in finding possible runtime errors.
11177 if not CodePeer_Mode
11178 and then Is_Predefined_File_Name
11179 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11181 Analyze (Act_Body, Suppress => All_Checks);
11183 Analyze (Act_Body);
11187 Inherit_Context (Gen_Body, Inst_Node);
11189 -- Remove the parent instances if they have been placed on the scope
11190 -- stack to compile the body.
11192 if Parent_Installed then
11193 Remove_Parent (In_Body => True);
11195 -- Restore the previous visibility of the parent
11197 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11200 Restore_Hidden_Primitives (Vis_Prims_List);
11201 Restore_Private_Views (Act_Decl_Id);
11203 -- Remove the current unit from visibility if this is an instance
11204 -- that is not elaborated on the fly for inlining purposes.
11206 if not Inlined_Body then
11207 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11212 -- If we have no body, and the unit requires a body, then complain. This
11213 -- complaint is suppressed if we have detected other errors (since a
11214 -- common reason for missing the body is that it had errors).
11215 -- In CodePeer mode, a warning has been emitted already, no need for
11216 -- further messages.
11218 elsif Unit_Requires_Body (Gen_Unit)
11219 and then not Body_Optional
11221 if CodePeer_Mode then
11224 elsif Serious_Errors_Detected = 0 then
11226 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11228 -- Don't attempt to perform any cleanup actions if some other error
11229 -- was already detected, since this can cause blowups.
11235 -- Case of package that does not need a body
11238 -- If the instantiation of the declaration is a library unit, rewrite
11239 -- the original package instantiation as a package declaration in the
11240 -- compilation unit node.
11242 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11243 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11244 Rewrite (Inst_Node, Act_Decl);
11246 -- Generate elaboration entity, in case spec has elaboration code.
11247 -- This cannot be done when the instance is analyzed, because it
11248 -- is not known yet whether the body exists.
11250 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11251 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11253 -- If the instantiation is not a library unit, then append the
11254 -- declaration to the list of implicitly generated entities, unless
11255 -- it is already a list member which means that it was already
11258 elsif not Is_List_Member (Act_Decl) then
11259 Mark_Rewrite_Insertion (Act_Decl);
11260 Insert_Before (Inst_Node, Act_Decl);
11264 Expander_Mode_Restore;
11267 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
11268 Style_Check := Save_Style_Check;
11270 Restore_Ghost_Mode (Mode);
11271 end Instantiate_Package_Body;
11273 ---------------------------------
11274 -- Instantiate_Subprogram_Body --
11275 ---------------------------------
11277 -- WARNING: This routine manages Ghost regions. Return statements must be
11278 -- replaced by gotos which jump to the end of the routine and restore the
11281 procedure Instantiate_Subprogram_Body
11282 (Body_Info : Pending_Body_Info;
11283 Body_Optional : Boolean := False)
11285 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11286 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11287 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11288 Gen_Id : constant Node_Id := Name (Inst_Node);
11289 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11290 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11291 Loc : constant Source_Ptr := Sloc (Inst_Node);
11292 Pack_Id : constant Entity_Id :=
11293 Defining_Unit_Name (Parent (Act_Decl));
11295 Saved_ISMP : constant Boolean :=
11296 Ignore_SPARK_Mode_Pragmas_In_Instance;
11297 Saved_Style_Check : constant Boolean := Style_Check;
11298 Saved_Warnings : constant Warning_Record := Save_Warnings;
11300 Act_Body : Node_Id;
11301 Act_Body_Id : Entity_Id;
11302 Gen_Body : Node_Id;
11303 Gen_Body_Id : Node_Id;
11304 Mode : Ghost_Mode_Type;
11305 Pack_Body : Node_Id;
11306 Par_Ent : Entity_Id := Empty;
11307 Par_Vis : Boolean := False;
11308 Ret_Expr : Node_Id;
11310 Parent_Installed : Boolean := False;
11313 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11315 -- Subprogram body may have been created already because of an inline
11316 -- pragma, or because of multiple elaborations of the enclosing package
11317 -- when several instances of the subprogram appear in the main unit.
11319 if Present (Corresponding_Body (Act_Decl)) then
11323 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11324 -- the mode now to ensure that any nodes generated during instantiation
11325 -- are properly marked as Ghost.
11327 Set_Ghost_Mode (Act_Decl_Id, Mode);
11329 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11331 -- Re-establish the state of information on which checks are suppressed.
11332 -- This information was set in Body_Info at the point of instantiation,
11333 -- and now we restore it so that the instance is compiled using the
11334 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11336 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11337 Scope_Suppress := Body_Info.Scope_Suppress;
11338 Opt.Ada_Version := Body_Info.Version;
11339 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11340 Restore_Warnings (Body_Info.Warnings);
11341 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11342 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11344 if No (Gen_Body_Id) then
11346 -- For imported generic subprogram, no body to compile, complete
11347 -- the spec entity appropriately.
11349 if Is_Imported (Gen_Unit) then
11350 Set_Is_Imported (Act_Decl_Id);
11351 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11352 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11353 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11354 Set_Has_Completion (Act_Decl_Id);
11357 -- For other cases, compile the body
11360 Load_Parent_Of_Generic
11361 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11362 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11366 Instantiation_Node := Inst_Node;
11368 if Present (Gen_Body_Id) then
11369 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11371 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11373 -- Either body is not present, or context is non-expanding, as
11374 -- when compiling a subunit. Mark the instance as completed, and
11375 -- diagnose a missing body when needed.
11378 and then Operating_Mode = Generate_Code
11380 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11383 Set_Has_Completion (Act_Decl_Id);
11387 Save_Env (Gen_Unit, Act_Decl_Id);
11388 Style_Check := False;
11390 -- If the context of the instance is subject to SPARK_Mode "off", the
11391 -- annotation is missing, or the body is instantiated at a later pass
11392 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11393 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11396 if SPARK_Mode /= On
11397 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11399 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11402 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11403 Create_Instantiation_Source
11410 (Original_Node (Gen_Body), Empty, Instantiating => True);
11412 -- Create proper defining name for the body, to correspond to the one
11416 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11418 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11419 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11421 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11422 Set_Has_Completion (Act_Decl_Id);
11423 Check_Generic_Actuals (Pack_Id, False);
11425 -- Generate a reference to link the visible subprogram instance to
11426 -- the generic body, which for navigation purposes is the only
11427 -- available source for the instance.
11430 (Related_Instance (Pack_Id),
11431 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11433 -- If it is a child unit, make the parent instance (which is an
11434 -- instance of the parent of the generic) visible. The parent
11435 -- instance is the prefix of the name of the generic unit.
11437 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11438 and then Nkind (Gen_Id) = N_Expanded_Name
11440 Par_Ent := Entity (Prefix (Gen_Id));
11441 Par_Vis := Is_Immediately_Visible (Par_Ent);
11442 Install_Parent (Par_Ent, In_Body => True);
11443 Parent_Installed := True;
11445 elsif Is_Child_Unit (Gen_Unit) then
11446 Par_Ent := Scope (Gen_Unit);
11447 Par_Vis := Is_Immediately_Visible (Par_Ent);
11448 Install_Parent (Par_Ent, In_Body => True);
11449 Parent_Installed := True;
11452 -- Subprogram body is placed in the body of wrapper package,
11453 -- whose spec contains the subprogram declaration as well as
11454 -- the renaming declarations for the generic parameters.
11457 Make_Package_Body (Loc,
11458 Defining_Unit_Name => New_Copy (Pack_Id),
11459 Declarations => New_List (Act_Body));
11461 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11463 -- If the instantiation is a library unit, then build resulting
11464 -- compilation unit nodes for the instance. The declaration of
11465 -- the enclosing package is the grandparent of the subprogram
11466 -- declaration. First replace the instantiation node as the unit
11467 -- of the corresponding compilation.
11469 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11470 if Parent (Inst_Node) = Cunit (Main_Unit) then
11471 Set_Unit (Parent (Inst_Node), Inst_Node);
11472 Build_Instance_Compilation_Unit_Nodes
11473 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11474 Analyze (Inst_Node);
11476 Set_Parent (Pack_Body, Parent (Inst_Node));
11477 Analyze (Pack_Body);
11481 Insert_Before (Inst_Node, Pack_Body);
11482 Mark_Rewrite_Insertion (Pack_Body);
11483 Analyze (Pack_Body);
11485 if Expander_Active then
11486 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11490 Inherit_Context (Gen_Body, Inst_Node);
11492 Restore_Private_Views (Pack_Id, False);
11494 if Parent_Installed then
11495 Remove_Parent (In_Body => True);
11497 -- Restore the previous visibility of the parent
11499 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11503 Restore_Warnings (Saved_Warnings);
11505 -- Body not found. Error was emitted already. If there were no previous
11506 -- errors, this may be an instance whose scope is a premature instance.
11507 -- In that case we must insure that the (legal) program does raise
11508 -- program error if executed. We generate a subprogram body for this
11509 -- purpose. See DEC ac30vso.
11511 -- Should not reference proprietary DEC tests in comments ???
11513 elsif Serious_Errors_Detected = 0
11514 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11516 if Body_Optional then
11519 elsif Ekind (Act_Decl_Id) = E_Procedure then
11521 Make_Subprogram_Body (Loc,
11523 Make_Procedure_Specification (Loc,
11524 Defining_Unit_Name =>
11525 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11526 Parameter_Specifications =>
11528 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11530 Declarations => Empty_List,
11531 Handled_Statement_Sequence =>
11532 Make_Handled_Sequence_Of_Statements (Loc,
11533 Statements => New_List (
11534 Make_Raise_Program_Error (Loc,
11535 Reason => PE_Access_Before_Elaboration))));
11539 Make_Raise_Program_Error (Loc,
11540 Reason => PE_Access_Before_Elaboration);
11542 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11543 Set_Analyzed (Ret_Expr);
11546 Make_Subprogram_Body (Loc,
11548 Make_Function_Specification (Loc,
11549 Defining_Unit_Name =>
11550 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11551 Parameter_Specifications =>
11553 (Parameter_Specifications (Parent (Act_Decl_Id))),
11554 Result_Definition =>
11555 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11557 Declarations => Empty_List,
11558 Handled_Statement_Sequence =>
11559 Make_Handled_Sequence_Of_Statements (Loc,
11560 Statements => New_List (
11561 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11565 Make_Package_Body (Loc,
11566 Defining_Unit_Name => New_Copy (Pack_Id),
11567 Declarations => New_List (Act_Body));
11569 Insert_After (Inst_Node, Pack_Body);
11570 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11571 Analyze (Pack_Body);
11574 Expander_Mode_Restore;
11577 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11578 Style_Check := Saved_Style_Check;
11580 Restore_Ghost_Mode (Mode);
11581 end Instantiate_Subprogram_Body;
11583 ----------------------
11584 -- Instantiate_Type --
11585 ----------------------
11587 function Instantiate_Type
11590 Analyzed_Formal : Node_Id;
11591 Actual_Decls : List_Id) return List_Id
11593 A_Gen_T : constant Entity_Id :=
11594 Defining_Identifier (Analyzed_Formal);
11595 Def : constant Node_Id := Formal_Type_Definition (Formal);
11596 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11598 Ancestor : Entity_Id := Empty;
11599 Decl_Node : Node_Id;
11600 Decl_Nodes : List_Id;
11604 procedure Diagnose_Predicated_Actual;
11605 -- There are a number of constructs in which a discrete type with
11606 -- predicates is illegal, e.g. as an index in an array type declaration.
11607 -- If a generic type is used is such a construct in a generic package
11608 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11609 -- of the generic contract that the actual cannot have predicates.
11611 procedure Validate_Array_Type_Instance;
11612 procedure Validate_Access_Subprogram_Instance;
11613 procedure Validate_Access_Type_Instance;
11614 procedure Validate_Derived_Type_Instance;
11615 procedure Validate_Derived_Interface_Type_Instance;
11616 procedure Validate_Discriminated_Formal_Type;
11617 procedure Validate_Interface_Type_Instance;
11618 procedure Validate_Private_Type_Instance;
11619 procedure Validate_Incomplete_Type_Instance;
11620 -- These procedures perform validation tests for the named case.
11621 -- Validate_Discriminated_Formal_Type is shared by formal private
11622 -- types and Ada 2012 formal incomplete types.
11624 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11625 -- Check that base types are the same and that the subtypes match
11626 -- statically. Used in several of the above.
11628 ---------------------------------
11629 -- Diagnose_Predicated_Actual --
11630 ---------------------------------
11632 procedure Diagnose_Predicated_Actual is
11634 if No_Predicate_On_Actual (A_Gen_T)
11635 and then Has_Predicates (Act_T)
11638 ("actual for& cannot be a type with predicate",
11639 Instantiation_Node, A_Gen_T);
11641 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11642 and then Has_Predicates (Act_T)
11643 and then not Has_Static_Predicate_Aspect (Act_T)
11646 ("actual for& cannot be a type with a dynamic predicate",
11647 Instantiation_Node, A_Gen_T);
11649 end Diagnose_Predicated_Actual;
11651 --------------------
11652 -- Subtypes_Match --
11653 --------------------
11655 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11656 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11659 -- Some detailed comments would be useful here ???
11661 return ((Base_Type (T) = Act_T
11662 or else Base_Type (T) = Base_Type (Act_T))
11663 and then Subtypes_Statically_Match (T, Act_T))
11665 or else (Is_Class_Wide_Type (Gen_T)
11666 and then Is_Class_Wide_Type (Act_T)
11667 and then Subtypes_Match
11668 (Get_Instance_Of (Root_Type (Gen_T)),
11669 Root_Type (Act_T)))
11672 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11673 E_Anonymous_Access_Type)
11674 and then Ekind (Act_T) = Ekind (Gen_T)
11675 and then Subtypes_Statically_Match
11676 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11677 end Subtypes_Match;
11679 -----------------------------------------
11680 -- Validate_Access_Subprogram_Instance --
11681 -----------------------------------------
11683 procedure Validate_Access_Subprogram_Instance is
11685 if not Is_Access_Type (Act_T)
11686 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11689 ("expect access type in instantiation of &", Actual, Gen_T);
11690 Abandon_Instantiation (Actual);
11693 -- According to AI05-288, actuals for access_to_subprograms must be
11694 -- subtype conformant with the generic formal. Previous to AI05-288
11695 -- only mode conformance was required.
11697 -- This is a binding interpretation that applies to previous versions
11698 -- of the language, no need to maintain previous weaker checks.
11700 Check_Subtype_Conformant
11701 (Designated_Type (Act_T),
11702 Designated_Type (A_Gen_T),
11706 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11707 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11709 ("protected access type not allowed for formal &",
11713 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11715 ("expect protected access type for formal &",
11719 -- If the formal has a specified convention (which in most cases
11720 -- will be StdCall) verify that the actual has the same convention.
11722 if Has_Convention_Pragma (A_Gen_T)
11723 and then Convention (A_Gen_T) /= Convention (Act_T)
11725 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11727 ("actual for formal & must have convention %", Actual, Gen_T);
11729 end Validate_Access_Subprogram_Instance;
11731 -----------------------------------
11732 -- Validate_Access_Type_Instance --
11733 -----------------------------------
11735 procedure Validate_Access_Type_Instance is
11736 Desig_Type : constant Entity_Id :=
11737 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11738 Desig_Act : Entity_Id;
11741 if not Is_Access_Type (Act_T) then
11743 ("expect access type in instantiation of &", Actual, Gen_T);
11744 Abandon_Instantiation (Actual);
11747 if Is_Access_Constant (A_Gen_T) then
11748 if not Is_Access_Constant (Act_T) then
11750 ("actual type must be access-to-constant type", Actual);
11751 Abandon_Instantiation (Actual);
11754 if Is_Access_Constant (Act_T) then
11756 ("actual type must be access-to-variable type", Actual);
11757 Abandon_Instantiation (Actual);
11759 elsif Ekind (A_Gen_T) = E_General_Access_Type
11760 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11762 Error_Msg_N -- CODEFIX
11763 ("actual must be general access type!", Actual);
11764 Error_Msg_NE -- CODEFIX
11765 ("add ALL to }!", Actual, Act_T);
11766 Abandon_Instantiation (Actual);
11770 -- The designated subtypes, that is to say the subtypes introduced
11771 -- by an access type declaration (and not by a subtype declaration)
11774 Desig_Act := Designated_Type (Base_Type (Act_T));
11776 -- The designated type may have been introduced through a limited_
11777 -- with clause, in which case retrieve the non-limited view. This
11778 -- applies to incomplete types as well as to class-wide types.
11780 if From_Limited_With (Desig_Act) then
11781 Desig_Act := Available_View (Desig_Act);
11784 if not Subtypes_Match (Desig_Type, Desig_Act) then
11786 ("designated type of actual does not match that of formal &",
11789 if not Predicates_Match (Desig_Type, Desig_Act) then
11790 Error_Msg_N ("\predicates do not match", Actual);
11793 Abandon_Instantiation (Actual);
11795 elsif Is_Access_Type (Designated_Type (Act_T))
11796 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11798 Is_Constrained (Designated_Type (Desig_Type))
11801 ("designated type of actual does not match that of formal &",
11804 if not Predicates_Match (Desig_Type, Desig_Act) then
11805 Error_Msg_N ("\predicates do not match", Actual);
11808 Abandon_Instantiation (Actual);
11811 -- Ada 2005: null-exclusion indicators of the two types must agree
11813 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11815 ("non null exclusion of actual and formal & do not match",
11818 end Validate_Access_Type_Instance;
11820 ----------------------------------
11821 -- Validate_Array_Type_Instance --
11822 ----------------------------------
11824 procedure Validate_Array_Type_Instance is
11829 function Formal_Dimensions return Nat;
11830 -- Count number of dimensions in array type formal
11832 -----------------------
11833 -- Formal_Dimensions --
11834 -----------------------
11836 function Formal_Dimensions return Nat is
11841 if Nkind (Def) = N_Constrained_Array_Definition then
11842 Index := First (Discrete_Subtype_Definitions (Def));
11844 Index := First (Subtype_Marks (Def));
11847 while Present (Index) loop
11849 Next_Index (Index);
11853 end Formal_Dimensions;
11855 -- Start of processing for Validate_Array_Type_Instance
11858 if not Is_Array_Type (Act_T) then
11860 ("expect array type in instantiation of &", Actual, Gen_T);
11861 Abandon_Instantiation (Actual);
11863 elsif Nkind (Def) = N_Constrained_Array_Definition then
11864 if not (Is_Constrained (Act_T)) then
11866 ("expect constrained array in instantiation of &",
11868 Abandon_Instantiation (Actual);
11872 if Is_Constrained (Act_T) then
11874 ("expect unconstrained array in instantiation of &",
11876 Abandon_Instantiation (Actual);
11880 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11882 ("dimensions of actual do not match formal &", Actual, Gen_T);
11883 Abandon_Instantiation (Actual);
11886 I1 := First_Index (A_Gen_T);
11887 I2 := First_Index (Act_T);
11888 for J in 1 .. Formal_Dimensions loop
11890 -- If the indexes of the actual were given by a subtype_mark,
11891 -- the index was transformed into a range attribute. Retrieve
11892 -- the original type mark for checking.
11894 if Is_Entity_Name (Original_Node (I2)) then
11895 T2 := Entity (Original_Node (I2));
11900 if not Subtypes_Match
11901 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11904 ("index types of actual do not match those of formal &",
11906 Abandon_Instantiation (Actual);
11913 -- Check matching subtypes. Note that there are complex visibility
11914 -- issues when the generic is a child unit and some aspect of the
11915 -- generic type is declared in a parent unit of the generic. We do
11916 -- the test to handle this special case only after a direct check
11917 -- for static matching has failed. The case where both the component
11918 -- type and the array type are separate formals, and the component
11919 -- type is a private view may also require special checking in
11923 (Component_Type (A_Gen_T), Component_Type (Act_T))
11926 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11927 Component_Type (Act_T))
11932 ("component subtype of actual does not match that of formal &",
11934 Abandon_Instantiation (Actual);
11937 if Has_Aliased_Components (A_Gen_T)
11938 and then not Has_Aliased_Components (Act_T)
11941 ("actual must have aliased components to match formal type &",
11944 end Validate_Array_Type_Instance;
11946 -----------------------------------------------
11947 -- Validate_Derived_Interface_Type_Instance --
11948 -----------------------------------------------
11950 procedure Validate_Derived_Interface_Type_Instance is
11951 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11955 -- First apply interface instance checks
11957 Validate_Interface_Type_Instance;
11959 -- Verify that immediate parent interface is an ancestor of
11963 and then not Interface_Present_In_Ancestor (Act_T, Par)
11966 ("interface actual must include progenitor&", Actual, Par);
11969 -- Now verify that the actual includes all other ancestors of
11972 Elmt := First_Elmt (Interfaces (A_Gen_T));
11973 while Present (Elmt) loop
11974 if not Interface_Present_In_Ancestor
11975 (Act_T, Get_Instance_Of (Node (Elmt)))
11978 ("interface actual must include progenitor&",
11979 Actual, Node (Elmt));
11984 end Validate_Derived_Interface_Type_Instance;
11986 ------------------------------------
11987 -- Validate_Derived_Type_Instance --
11988 ------------------------------------
11990 procedure Validate_Derived_Type_Instance is
11991 Actual_Discr : Entity_Id;
11992 Ancestor_Discr : Entity_Id;
11995 -- If the parent type in the generic declaration is itself a previous
11996 -- formal type, then it is local to the generic and absent from the
11997 -- analyzed generic definition. In that case the ancestor is the
11998 -- instance of the formal (which must have been instantiated
11999 -- previously), unless the ancestor is itself a formal derived type.
12000 -- In this latter case (which is the subject of Corrigendum 8652/0038
12001 -- (AI-202) the ancestor of the formals is the ancestor of its
12002 -- parent. Otherwise, the analyzed generic carries the parent type.
12003 -- If the parent type is defined in a previous formal package, then
12004 -- the scope of that formal package is that of the generic type
12005 -- itself, and it has already been mapped into the corresponding type
12006 -- in the actual package.
12008 -- Common case: parent type defined outside of the generic
12010 if Is_Entity_Name (Subtype_Mark (Def))
12011 and then Present (Entity (Subtype_Mark (Def)))
12013 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12015 -- Check whether parent is defined in a previous formal package
12018 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12021 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12023 -- The type may be a local derivation, or a type extension of a
12024 -- previous formal, or of a formal of a parent package.
12026 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12028 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12030 -- Check whether the parent is another derived formal type in the
12031 -- same generic unit.
12033 if Etype (A_Gen_T) /= A_Gen_T
12034 and then Is_Generic_Type (Etype (A_Gen_T))
12035 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12036 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12038 -- Locate ancestor of parent from the subtype declaration
12039 -- created for the actual.
12045 Decl := First (Actual_Decls);
12046 while Present (Decl) loop
12047 if Nkind (Decl) = N_Subtype_Declaration
12048 and then Chars (Defining_Identifier (Decl)) =
12049 Chars (Etype (A_Gen_T))
12051 Ancestor := Generic_Parent_Type (Decl);
12059 pragma Assert (Present (Ancestor));
12061 -- The ancestor itself may be a previous formal that has been
12064 Ancestor := Get_Instance_Of (Ancestor);
12068 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12071 -- Check whether parent is a previous formal of the current generic
12073 elsif Is_Derived_Type (A_Gen_T)
12074 and then Is_Generic_Type (Etype (A_Gen_T))
12075 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12077 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12079 -- An unusual case: the actual is a type declared in a parent unit,
12080 -- but is not a formal type so there is no instance_of for it.
12081 -- Retrieve it by analyzing the record extension.
12083 elsif Is_Child_Unit (Scope (A_Gen_T))
12084 and then In_Open_Scopes (Scope (Act_T))
12085 and then Is_Generic_Instance (Scope (Act_T))
12087 Analyze (Subtype_Mark (Def));
12088 Ancestor := Entity (Subtype_Mark (Def));
12091 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12094 -- If the formal derived type has pragma Preelaborable_Initialization
12095 -- then the actual type must have preelaborable initialization.
12097 if Known_To_Have_Preelab_Init (A_Gen_T)
12098 and then not Has_Preelaborable_Initialization (Act_T)
12101 ("actual for & must have preelaborable initialization",
12105 -- Ada 2005 (AI-251)
12107 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12108 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12110 ("(Ada 2005) expected type implementing & in instantiation",
12114 -- Finally verify that the (instance of) the ancestor is an ancestor
12117 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12119 ("expect type derived from & in instantiation",
12120 Actual, First_Subtype (Ancestor));
12121 Abandon_Instantiation (Actual);
12124 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12125 -- that the formal type declaration has been rewritten as a private
12128 if Ada_Version >= Ada_2005
12129 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12130 and then Synchronized_Present (Parent (A_Gen_T))
12132 -- The actual must be a synchronized tagged type
12134 if not Is_Tagged_Type (Act_T) then
12136 ("actual of synchronized type must be tagged", Actual);
12137 Abandon_Instantiation (Actual);
12139 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12140 and then Nkind (Type_Definition (Parent (Act_T))) =
12141 N_Derived_Type_Definition
12142 and then not Synchronized_Present
12143 (Type_Definition (Parent (Act_T)))
12146 ("actual of synchronized type must be synchronized", Actual);
12147 Abandon_Instantiation (Actual);
12151 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12152 -- removes the second instance of the phrase "or allow pass by copy".
12154 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12156 ("cannot have atomic actual type for non-atomic formal type",
12159 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12161 ("cannot have volatile actual type for non-volatile formal type",
12165 -- It should not be necessary to check for unknown discriminants on
12166 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12167 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12168 -- needs fixing. ???
12170 if Is_Definite_Subtype (A_Gen_T)
12171 and then not Unknown_Discriminants_Present (Formal)
12172 and then not Is_Definite_Subtype (Act_T)
12174 Error_Msg_N ("actual subtype must be constrained", Actual);
12175 Abandon_Instantiation (Actual);
12178 if not Unknown_Discriminants_Present (Formal) then
12179 if Is_Constrained (Ancestor) then
12180 if not Is_Constrained (Act_T) then
12181 Error_Msg_N ("actual subtype must be constrained", Actual);
12182 Abandon_Instantiation (Actual);
12185 -- Ancestor is unconstrained, Check if generic formal and actual
12186 -- agree on constrainedness. The check only applies to array types
12187 -- and discriminated types.
12189 elsif Is_Constrained (Act_T) then
12190 if Ekind (Ancestor) = E_Access_Type
12191 or else (not Is_Constrained (A_Gen_T)
12192 and then Is_Composite_Type (A_Gen_T))
12194 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12195 Abandon_Instantiation (Actual);
12198 -- A class-wide type is only allowed if the formal has unknown
12201 elsif Is_Class_Wide_Type (Act_T)
12202 and then not Has_Unknown_Discriminants (Ancestor)
12205 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12206 Abandon_Instantiation (Actual);
12208 -- Otherwise, the formal and actual must have the same number
12209 -- of discriminants and each discriminant of the actual must
12210 -- correspond to a discriminant of the formal.
12212 elsif Has_Discriminants (Act_T)
12213 and then not Has_Unknown_Discriminants (Act_T)
12214 and then Has_Discriminants (Ancestor)
12216 Actual_Discr := First_Discriminant (Act_T);
12217 Ancestor_Discr := First_Discriminant (Ancestor);
12218 while Present (Actual_Discr)
12219 and then Present (Ancestor_Discr)
12221 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12222 No (Corresponding_Discriminant (Actual_Discr))
12225 ("discriminant & does not correspond "
12226 & "to ancestor discriminant", Actual, Actual_Discr);
12227 Abandon_Instantiation (Actual);
12230 Next_Discriminant (Actual_Discr);
12231 Next_Discriminant (Ancestor_Discr);
12234 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12236 ("actual for & must have same number of discriminants",
12238 Abandon_Instantiation (Actual);
12241 -- This case should be caught by the earlier check for
12242 -- constrainedness, but the check here is added for completeness.
12244 elsif Has_Discriminants (Act_T)
12245 and then not Has_Unknown_Discriminants (Act_T)
12248 ("actual for & must not have discriminants", Actual, Gen_T);
12249 Abandon_Instantiation (Actual);
12251 elsif Has_Discriminants (Ancestor) then
12253 ("actual for & must have known discriminants", Actual, Gen_T);
12254 Abandon_Instantiation (Actual);
12257 if not Subtypes_Statically_Compatible
12258 (Act_T, Ancestor, Formal_Derived_Matching => True)
12261 ("constraint on actual is incompatible with formal", Actual);
12262 Abandon_Instantiation (Actual);
12266 -- If the formal and actual types are abstract, check that there
12267 -- are no abstract primitives of the actual type that correspond to
12268 -- nonabstract primitives of the formal type (second sentence of
12271 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12272 Check_Abstract_Primitives : declare
12273 Gen_Prims : constant Elist_Id :=
12274 Primitive_Operations (A_Gen_T);
12275 Gen_Elmt : Elmt_Id;
12276 Gen_Subp : Entity_Id;
12277 Anc_Subp : Entity_Id;
12278 Anc_Formal : Entity_Id;
12279 Anc_F_Type : Entity_Id;
12281 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12282 Act_Elmt : Elmt_Id;
12283 Act_Subp : Entity_Id;
12284 Act_Formal : Entity_Id;
12285 Act_F_Type : Entity_Id;
12287 Subprograms_Correspond : Boolean;
12289 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12290 -- Returns true if T2 is derived directly or indirectly from
12291 -- T1, including derivations from interfaces. T1 and T2 are
12292 -- required to be specific tagged base types.
12294 ------------------------
12295 -- Is_Tagged_Ancestor --
12296 ------------------------
12298 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12300 Intfc_Elmt : Elmt_Id;
12303 -- The predicate is satisfied if the types are the same
12308 -- If we've reached the top of the derivation chain then
12309 -- we know that T1 is not an ancestor of T2.
12311 elsif Etype (T2) = T2 then
12314 -- Proceed to check T2's immediate parent
12316 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12319 -- Finally, check to see if T1 is an ancestor of any of T2's
12323 Intfc_Elmt := First_Elmt (Interfaces (T2));
12324 while Present (Intfc_Elmt) loop
12325 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12329 Next_Elmt (Intfc_Elmt);
12334 end Is_Tagged_Ancestor;
12336 -- Start of processing for Check_Abstract_Primitives
12339 -- Loop over all of the formal derived type's primitives
12341 Gen_Elmt := First_Elmt (Gen_Prims);
12342 while Present (Gen_Elmt) loop
12343 Gen_Subp := Node (Gen_Elmt);
12345 -- If the primitive of the formal is not abstract, then
12346 -- determine whether there is a corresponding primitive of
12347 -- the actual type that's abstract.
12349 if not Is_Abstract_Subprogram (Gen_Subp) then
12350 Act_Elmt := First_Elmt (Act_Prims);
12351 while Present (Act_Elmt) loop
12352 Act_Subp := Node (Act_Elmt);
12354 -- If we find an abstract primitive of the actual,
12355 -- then we need to test whether it corresponds to the
12356 -- subprogram from which the generic formal primitive
12359 if Is_Abstract_Subprogram (Act_Subp) then
12360 Anc_Subp := Alias (Gen_Subp);
12362 -- Test whether we have a corresponding primitive
12363 -- by comparing names, kinds, formal types, and
12366 if Chars (Anc_Subp) = Chars (Act_Subp)
12367 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12369 Anc_Formal := First_Formal (Anc_Subp);
12370 Act_Formal := First_Formal (Act_Subp);
12371 while Present (Anc_Formal)
12372 and then Present (Act_Formal)
12374 Anc_F_Type := Etype (Anc_Formal);
12375 Act_F_Type := Etype (Act_Formal);
12377 if Ekind (Anc_F_Type) =
12378 E_Anonymous_Access_Type
12380 Anc_F_Type := Designated_Type (Anc_F_Type);
12382 if Ekind (Act_F_Type) =
12383 E_Anonymous_Access_Type
12386 Designated_Type (Act_F_Type);
12392 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12397 Anc_F_Type := Base_Type (Anc_F_Type);
12398 Act_F_Type := Base_Type (Act_F_Type);
12400 -- If the formal is controlling, then the
12401 -- the type of the actual primitive's formal
12402 -- must be derived directly or indirectly
12403 -- from the type of the ancestor primitive's
12406 if Is_Controlling_Formal (Anc_Formal) then
12407 if not Is_Tagged_Ancestor
12408 (Anc_F_Type, Act_F_Type)
12413 -- Otherwise the types of the formals must
12416 elsif Anc_F_Type /= Act_F_Type then
12420 Next_Entity (Anc_Formal);
12421 Next_Entity (Act_Formal);
12424 -- If we traversed through all of the formals
12425 -- then so far the subprograms correspond, so
12426 -- now check that any result types correspond.
12428 if No (Anc_Formal) and then No (Act_Formal) then
12429 Subprograms_Correspond := True;
12431 if Ekind (Act_Subp) = E_Function then
12432 Anc_F_Type := Etype (Anc_Subp);
12433 Act_F_Type := Etype (Act_Subp);
12435 if Ekind (Anc_F_Type) =
12436 E_Anonymous_Access_Type
12439 Designated_Type (Anc_F_Type);
12441 if Ekind (Act_F_Type) =
12442 E_Anonymous_Access_Type
12445 Designated_Type (Act_F_Type);
12447 Subprograms_Correspond := False;
12452 = E_Anonymous_Access_Type
12454 Subprograms_Correspond := False;
12457 Anc_F_Type := Base_Type (Anc_F_Type);
12458 Act_F_Type := Base_Type (Act_F_Type);
12460 -- Now either the result types must be
12461 -- the same or, if the result type is
12462 -- controlling, the result type of the
12463 -- actual primitive must descend from the
12464 -- result type of the ancestor primitive.
12466 if Subprograms_Correspond
12467 and then Anc_F_Type /= Act_F_Type
12469 Has_Controlling_Result (Anc_Subp)
12470 and then not Is_Tagged_Ancestor
12471 (Anc_F_Type, Act_F_Type)
12473 Subprograms_Correspond := False;
12477 -- Found a matching subprogram belonging to
12478 -- formal ancestor type, so actual subprogram
12479 -- corresponds and this violates 3.9.3(9).
12481 if Subprograms_Correspond then
12483 ("abstract subprogram & overrides "
12484 & "nonabstract subprogram of ancestor",
12491 Next_Elmt (Act_Elmt);
12495 Next_Elmt (Gen_Elmt);
12497 end Check_Abstract_Primitives;
12500 -- Verify that limitedness matches. If parent is a limited
12501 -- interface then the generic formal is not unless declared
12502 -- explicitly so. If not declared limited, the actual cannot be
12503 -- limited (see AI05-0087).
12505 -- Even though this AI is a binding interpretation, we enable the
12506 -- check only in Ada 2012 mode, because this improper construct
12507 -- shows up in user code and in existing B-tests.
12509 if Is_Limited_Type (Act_T)
12510 and then not Is_Limited_Type (A_Gen_T)
12511 and then Ada_Version >= Ada_2012
12513 if In_Instance then
12517 ("actual for non-limited & cannot be a limited type",
12519 Explain_Limited_Type (Act_T, Actual);
12520 Abandon_Instantiation (Actual);
12523 end Validate_Derived_Type_Instance;
12525 ----------------------------------------
12526 -- Validate_Discriminated_Formal_Type --
12527 ----------------------------------------
12529 procedure Validate_Discriminated_Formal_Type is
12530 Formal_Discr : Entity_Id;
12531 Actual_Discr : Entity_Id;
12532 Formal_Subt : Entity_Id;
12535 if Has_Discriminants (A_Gen_T) then
12536 if not Has_Discriminants (Act_T) then
12538 ("actual for & must have discriminants", Actual, Gen_T);
12539 Abandon_Instantiation (Actual);
12541 elsif Is_Constrained (Act_T) then
12543 ("actual for & must be unconstrained", Actual, Gen_T);
12544 Abandon_Instantiation (Actual);
12547 Formal_Discr := First_Discriminant (A_Gen_T);
12548 Actual_Discr := First_Discriminant (Act_T);
12549 while Formal_Discr /= Empty loop
12550 if Actual_Discr = Empty then
12552 ("discriminants on actual do not match formal",
12554 Abandon_Instantiation (Actual);
12557 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12559 -- Access discriminants match if designated types do
12561 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12562 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12563 E_Anonymous_Access_Type
12566 (Designated_Type (Base_Type (Formal_Subt))) =
12567 Designated_Type (Base_Type (Etype (Actual_Discr)))
12571 elsif Base_Type (Formal_Subt) /=
12572 Base_Type (Etype (Actual_Discr))
12575 ("types of actual discriminants must match formal",
12577 Abandon_Instantiation (Actual);
12579 elsif not Subtypes_Statically_Match
12580 (Formal_Subt, Etype (Actual_Discr))
12581 and then Ada_Version >= Ada_95
12584 ("subtypes of actual discriminants must match formal",
12586 Abandon_Instantiation (Actual);
12589 Next_Discriminant (Formal_Discr);
12590 Next_Discriminant (Actual_Discr);
12593 if Actual_Discr /= Empty then
12595 ("discriminants on actual do not match formal",
12597 Abandon_Instantiation (Actual);
12601 end Validate_Discriminated_Formal_Type;
12603 ---------------------------------------
12604 -- Validate_Incomplete_Type_Instance --
12605 ---------------------------------------
12607 procedure Validate_Incomplete_Type_Instance is
12609 if not Is_Tagged_Type (Act_T)
12610 and then Is_Tagged_Type (A_Gen_T)
12613 ("actual for & must be a tagged type", Actual, Gen_T);
12616 Validate_Discriminated_Formal_Type;
12617 end Validate_Incomplete_Type_Instance;
12619 --------------------------------------
12620 -- Validate_Interface_Type_Instance --
12621 --------------------------------------
12623 procedure Validate_Interface_Type_Instance is
12625 if not Is_Interface (Act_T) then
12627 ("actual for formal interface type must be an interface",
12630 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12631 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12632 or else Is_Protected_Interface (A_Gen_T) /=
12633 Is_Protected_Interface (Act_T)
12634 or else Is_Synchronized_Interface (A_Gen_T) /=
12635 Is_Synchronized_Interface (Act_T)
12638 ("actual for interface& does not match (RM 12.5.5(4))",
12641 end Validate_Interface_Type_Instance;
12643 ------------------------------------
12644 -- Validate_Private_Type_Instance --
12645 ------------------------------------
12647 procedure Validate_Private_Type_Instance is
12649 if Is_Limited_Type (Act_T)
12650 and then not Is_Limited_Type (A_Gen_T)
12652 if In_Instance then
12656 ("actual for non-limited & cannot be a limited type", Actual,
12658 Explain_Limited_Type (Act_T, Actual);
12659 Abandon_Instantiation (Actual);
12662 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12663 and then not Has_Preelaborable_Initialization (Act_T)
12666 ("actual for & must have preelaborable initialization", Actual,
12669 elsif not Is_Definite_Subtype (Act_T)
12670 and then Is_Definite_Subtype (A_Gen_T)
12671 and then Ada_Version >= Ada_95
12674 ("actual for & must be a definite subtype", Actual, Gen_T);
12676 elsif not Is_Tagged_Type (Act_T)
12677 and then Is_Tagged_Type (A_Gen_T)
12680 ("actual for & must be a tagged type", Actual, Gen_T);
12683 Validate_Discriminated_Formal_Type;
12685 end Validate_Private_Type_Instance;
12687 -- Start of processing for Instantiate_Type
12690 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12691 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12692 return New_List (Error);
12694 elsif not Is_Entity_Name (Actual)
12695 or else not Is_Type (Entity (Actual))
12698 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12699 Abandon_Instantiation (Actual);
12702 Act_T := Entity (Actual);
12704 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12705 -- as a generic actual parameter if the corresponding formal type
12706 -- does not have a known_discriminant_part, or is a formal derived
12707 -- type that is an Unchecked_Union type.
12709 if Is_Unchecked_Union (Base_Type (Act_T)) then
12710 if not Has_Discriminants (A_Gen_T)
12711 or else (Is_Derived_Type (A_Gen_T)
12712 and then Is_Unchecked_Union (A_Gen_T))
12716 Error_Msg_N ("unchecked union cannot be the actual for a "
12717 & "discriminated formal type", Act_T);
12722 -- Deal with fixed/floating restrictions
12724 if Is_Floating_Point_Type (Act_T) then
12725 Check_Restriction (No_Floating_Point, Actual);
12726 elsif Is_Fixed_Point_Type (Act_T) then
12727 Check_Restriction (No_Fixed_Point, Actual);
12730 -- Deal with error of using incomplete type as generic actual.
12731 -- This includes limited views of a type, even if the non-limited
12732 -- view may be available.
12734 if Ekind (Act_T) = E_Incomplete_Type
12735 or else (Is_Class_Wide_Type (Act_T)
12736 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12738 -- If the formal is an incomplete type, the actual can be
12739 -- incomplete as well.
12741 if Ekind (A_Gen_T) = E_Incomplete_Type then
12744 elsif Is_Class_Wide_Type (Act_T)
12745 or else No (Full_View (Act_T))
12747 Error_Msg_N ("premature use of incomplete type", Actual);
12748 Abandon_Instantiation (Actual);
12750 Act_T := Full_View (Act_T);
12751 Set_Entity (Actual, Act_T);
12753 if Has_Private_Component (Act_T) then
12755 ("premature use of type with private component", Actual);
12759 -- Deal with error of premature use of private type as generic actual
12761 elsif Is_Private_Type (Act_T)
12762 and then Is_Private_Type (Base_Type (Act_T))
12763 and then not Is_Generic_Type (Act_T)
12764 and then not Is_Derived_Type (Act_T)
12765 and then No (Full_View (Root_Type (Act_T)))
12767 -- If the formal is an incomplete type, the actual can be
12768 -- private or incomplete as well.
12770 if Ekind (A_Gen_T) = E_Incomplete_Type then
12773 Error_Msg_N ("premature use of private type", Actual);
12776 elsif Has_Private_Component (Act_T) then
12778 ("premature use of type with private component", Actual);
12781 Set_Instance_Of (A_Gen_T, Act_T);
12783 -- If the type is generic, the class-wide type may also be used
12785 if Is_Tagged_Type (A_Gen_T)
12786 and then Is_Tagged_Type (Act_T)
12787 and then not Is_Class_Wide_Type (A_Gen_T)
12789 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12790 Class_Wide_Type (Act_T));
12793 if not Is_Abstract_Type (A_Gen_T)
12794 and then Is_Abstract_Type (Act_T)
12797 ("actual of non-abstract formal cannot be abstract", Actual);
12800 -- A generic scalar type is a first subtype for which we generate
12801 -- an anonymous base type. Indicate that the instance of this base
12802 -- is the base type of the actual.
12804 if Is_Scalar_Type (A_Gen_T) then
12805 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12809 if Error_Posted (Act_T) then
12812 case Nkind (Def) is
12813 when N_Formal_Private_Type_Definition =>
12814 Validate_Private_Type_Instance;
12816 when N_Formal_Incomplete_Type_Definition =>
12817 Validate_Incomplete_Type_Instance;
12819 when N_Formal_Derived_Type_Definition =>
12820 Validate_Derived_Type_Instance;
12822 when N_Formal_Discrete_Type_Definition =>
12823 if not Is_Discrete_Type (Act_T) then
12825 ("expect discrete type in instantiation of&",
12827 Abandon_Instantiation (Actual);
12830 Diagnose_Predicated_Actual;
12832 when N_Formal_Signed_Integer_Type_Definition =>
12833 if not Is_Signed_Integer_Type (Act_T) then
12835 ("expect signed integer type in instantiation of&",
12837 Abandon_Instantiation (Actual);
12840 Diagnose_Predicated_Actual;
12842 when N_Formal_Modular_Type_Definition =>
12843 if not Is_Modular_Integer_Type (Act_T) then
12845 ("expect modular type in instantiation of &",
12847 Abandon_Instantiation (Actual);
12850 Diagnose_Predicated_Actual;
12852 when N_Formal_Floating_Point_Definition =>
12853 if not Is_Floating_Point_Type (Act_T) then
12855 ("expect float type in instantiation of &", Actual, Gen_T);
12856 Abandon_Instantiation (Actual);
12859 when N_Formal_Ordinary_Fixed_Point_Definition =>
12860 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12862 ("expect ordinary fixed point type in instantiation of &",
12864 Abandon_Instantiation (Actual);
12867 when N_Formal_Decimal_Fixed_Point_Definition =>
12868 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12870 ("expect decimal type in instantiation of &",
12872 Abandon_Instantiation (Actual);
12875 when N_Array_Type_Definition =>
12876 Validate_Array_Type_Instance;
12878 when N_Access_To_Object_Definition =>
12879 Validate_Access_Type_Instance;
12881 when N_Access_Function_Definition
12882 | N_Access_Procedure_Definition
12884 Validate_Access_Subprogram_Instance;
12886 when N_Record_Definition =>
12887 Validate_Interface_Type_Instance;
12889 when N_Derived_Type_Definition =>
12890 Validate_Derived_Interface_Type_Instance;
12893 raise Program_Error;
12897 Subt := New_Copy (Gen_T);
12899 -- Use adjusted sloc of subtype name as the location for other nodes in
12900 -- the subtype declaration.
12902 Loc := Sloc (Subt);
12905 Make_Subtype_Declaration (Loc,
12906 Defining_Identifier => Subt,
12907 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12909 if Is_Private_Type (Act_T) then
12910 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12912 elsif Is_Access_Type (Act_T)
12913 and then Is_Private_Type (Designated_Type (Act_T))
12915 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12918 -- In Ada 2012 the actual may be a limited view. Indicate that
12919 -- the local subtype must be treated as such.
12921 if From_Limited_With (Act_T) then
12922 Set_Ekind (Subt, E_Incomplete_Subtype);
12923 Set_From_Limited_With (Subt);
12926 Decl_Nodes := New_List (Decl_Node);
12928 -- Flag actual derived types so their elaboration produces the
12929 -- appropriate renamings for the primitive operations of the ancestor.
12930 -- Flag actual for formal private types as well, to determine whether
12931 -- operations in the private part may override inherited operations.
12932 -- If the formal has an interface list, the ancestor is not the
12933 -- parent, but the analyzed formal that includes the interface
12934 -- operations of all its progenitors.
12936 -- Same treatment for formal private types, so we can check whether the
12937 -- type is tagged limited when validating derivations in the private
12938 -- part. (See AI05-096).
12940 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12941 if Present (Interface_List (Def)) then
12942 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12944 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12947 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12948 N_Formal_Incomplete_Type_Definition)
12950 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12953 -- If the actual is a synchronized type that implements an interface,
12954 -- the primitive operations are attached to the corresponding record,
12955 -- and we have to treat it as an additional generic actual, so that its
12956 -- primitive operations become visible in the instance. The task or
12957 -- protected type itself does not carry primitive operations.
12959 if Is_Concurrent_Type (Act_T)
12960 and then Is_Tagged_Type (Act_T)
12961 and then Present (Corresponding_Record_Type (Act_T))
12962 and then Present (Ancestor)
12963 and then Is_Interface (Ancestor)
12966 Corr_Rec : constant Entity_Id :=
12967 Corresponding_Record_Type (Act_T);
12968 New_Corr : Entity_Id;
12969 Corr_Decl : Node_Id;
12972 New_Corr := Make_Temporary (Loc, 'S');
12974 Make_Subtype_Declaration (Loc,
12975 Defining_Identifier => New_Corr,
12976 Subtype_Indication =>
12977 New_Occurrence_Of (Corr_Rec, Loc));
12978 Append_To (Decl_Nodes, Corr_Decl);
12980 if Ekind (Act_T) = E_Task_Type then
12981 Set_Ekind (Subt, E_Task_Subtype);
12983 Set_Ekind (Subt, E_Protected_Subtype);
12986 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12987 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12988 Set_Generic_Parent_Type (Decl_Node, Empty);
12992 -- For a floating-point type, capture dimension info if any, because
12993 -- the generated subtype declaration does not come from source and
12994 -- will not process dimensions.
12996 if Is_Floating_Point_Type (Act_T) then
12997 Copy_Dimensions (Act_T, Subt);
13001 end Instantiate_Type;
13003 ---------------------
13004 -- Is_In_Main_Unit --
13005 ---------------------
13007 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13008 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13009 Current_Unit : Node_Id;
13012 if Unum = Main_Unit then
13015 -- If the current unit is a subunit then it is either the main unit or
13016 -- is being compiled as part of the main unit.
13018 elsif Nkind (N) = N_Compilation_Unit then
13019 return Nkind (Unit (N)) = N_Subunit;
13022 Current_Unit := Parent (N);
13023 while Present (Current_Unit)
13024 and then Nkind (Current_Unit) /= N_Compilation_Unit
13026 Current_Unit := Parent (Current_Unit);
13029 -- The instantiation node is in the main unit, or else the current node
13030 -- (perhaps as the result of nested instantiations) is in the main unit,
13031 -- or in the declaration of the main unit, which in this last case must
13035 Current_Unit = Cunit (Main_Unit)
13036 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13037 or else (Present (Current_Unit)
13038 and then Present (Library_Unit (Current_Unit))
13039 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13040 end Is_In_Main_Unit;
13042 ----------------------------
13043 -- Load_Parent_Of_Generic --
13044 ----------------------------
13046 procedure Load_Parent_Of_Generic
13049 Body_Optional : Boolean := False)
13051 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13052 Saved_Style_Check : constant Boolean := Style_Check;
13053 Saved_Warnings : constant Warning_Record := Save_Warnings;
13054 True_Parent : Node_Id;
13055 Inst_Node : Node_Id;
13057 Previous_Instances : constant Elist_Id := New_Elmt_List;
13059 procedure Collect_Previous_Instances (Decls : List_Id);
13060 -- Collect all instantiations in the given list of declarations, that
13061 -- precede the generic that we need to load. If the bodies of these
13062 -- instantiations are available, we must analyze them, to ensure that
13063 -- the public symbols generated are the same when the unit is compiled
13064 -- to generate code, and when it is compiled in the context of a unit
13065 -- that needs a particular nested instance. This process is applied to
13066 -- both package and subprogram instances.
13068 --------------------------------
13069 -- Collect_Previous_Instances --
13070 --------------------------------
13072 procedure Collect_Previous_Instances (Decls : List_Id) is
13076 Decl := First (Decls);
13077 while Present (Decl) loop
13078 if Sloc (Decl) >= Sloc (Inst_Node) then
13081 -- If Decl is an instantiation, then record it as requiring
13082 -- instantiation of the corresponding body, except if it is an
13083 -- abbreviated instantiation generated internally for conformance
13084 -- checking purposes only for the case of a formal package
13085 -- declared without a box (see Instantiate_Formal_Package). Such
13086 -- an instantiation does not generate any code (the actual code
13087 -- comes from actual) and thus does not need to be analyzed here.
13088 -- If the instantiation appears with a generic package body it is
13089 -- not analyzed here either.
13091 elsif Nkind (Decl) = N_Package_Instantiation
13092 and then not Is_Internal (Defining_Entity (Decl))
13094 Append_Elmt (Decl, Previous_Instances);
13096 -- For a subprogram instantiation, omit instantiations intrinsic
13097 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13099 elsif Nkind_In (Decl, N_Function_Instantiation,
13100 N_Procedure_Instantiation)
13101 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13103 Append_Elmt (Decl, Previous_Instances);
13105 elsif Nkind (Decl) = N_Package_Declaration then
13106 Collect_Previous_Instances
13107 (Visible_Declarations (Specification (Decl)));
13108 Collect_Previous_Instances
13109 (Private_Declarations (Specification (Decl)));
13111 -- Previous non-generic bodies may contain instances as well
13113 elsif Nkind (Decl) = N_Package_Body
13114 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13116 Collect_Previous_Instances (Declarations (Decl));
13118 elsif Nkind (Decl) = N_Subprogram_Body
13119 and then not Acts_As_Spec (Decl)
13120 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13122 Collect_Previous_Instances (Declarations (Decl));
13127 end Collect_Previous_Instances;
13129 -- Start of processing for Load_Parent_Of_Generic
13132 if not In_Same_Source_Unit (N, Spec)
13133 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13134 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13135 and then not Is_In_Main_Unit (Spec))
13137 -- Find body of parent of spec, and analyze it. A special case arises
13138 -- when the parent is an instantiation, that is to say when we are
13139 -- currently instantiating a nested generic. In that case, there is
13140 -- no separate file for the body of the enclosing instance. Instead,
13141 -- the enclosing body must be instantiated as if it were a pending
13142 -- instantiation, in order to produce the body for the nested generic
13143 -- we require now. Note that in that case the generic may be defined
13144 -- in a package body, the instance defined in the same package body,
13145 -- and the original enclosing body may not be in the main unit.
13147 Inst_Node := Empty;
13149 True_Parent := Parent (Spec);
13150 while Present (True_Parent)
13151 and then Nkind (True_Parent) /= N_Compilation_Unit
13153 if Nkind (True_Parent) = N_Package_Declaration
13155 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13157 -- Parent is a compilation unit that is an instantiation.
13158 -- Instantiation node has been replaced with package decl.
13160 Inst_Node := Original_Node (True_Parent);
13163 elsif Nkind (True_Parent) = N_Package_Declaration
13164 and then Present (Generic_Parent (Specification (True_Parent)))
13165 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13167 -- Parent is an instantiation within another specification.
13168 -- Declaration for instance has been inserted before original
13169 -- instantiation node. A direct link would be preferable?
13171 Inst_Node := Next (True_Parent);
13172 while Present (Inst_Node)
13173 and then Nkind (Inst_Node) /= N_Package_Instantiation
13178 -- If the instance appears within a generic, and the generic
13179 -- unit is defined within a formal package of the enclosing
13180 -- generic, there is no generic body available, and none
13181 -- needed. A more precise test should be used ???
13183 if No (Inst_Node) then
13190 True_Parent := Parent (True_Parent);
13194 -- Case where we are currently instantiating a nested generic
13196 if Present (Inst_Node) then
13197 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13199 -- Instantiation node and declaration of instantiated package
13200 -- were exchanged when only the declaration was needed.
13201 -- Restore instantiation node before proceeding with body.
13203 Set_Unit (Parent (True_Parent), Inst_Node);
13206 -- Now complete instantiation of enclosing body, if it appears in
13207 -- some other unit. If it appears in the current unit, the body
13208 -- will have been instantiated already.
13210 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13212 -- We need to determine the expander mode to instantiate the
13213 -- enclosing body. Because the generic body we need may use
13214 -- global entities declared in the enclosing package (including
13215 -- aggregates) it is in general necessary to compile this body
13216 -- with expansion enabled, except if we are within a generic
13217 -- package, in which case the usual generic rule applies.
13220 Exp_Status : Boolean := True;
13224 -- Loop through scopes looking for generic package
13226 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13227 while Present (Scop)
13228 and then Scop /= Standard_Standard
13230 if Ekind (Scop) = E_Generic_Package then
13231 Exp_Status := False;
13235 Scop := Scope (Scop);
13238 -- Collect previous instantiations in the unit that contains
13239 -- the desired generic.
13241 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13242 and then not Body_Optional
13246 Info : Pending_Body_Info;
13250 Par := Parent (Inst_Node);
13251 while Present (Par) loop
13252 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13253 Par := Parent (Par);
13256 pragma Assert (Present (Par));
13258 if Nkind (Par) = N_Package_Body then
13259 Collect_Previous_Instances (Declarations (Par));
13261 elsif Nkind (Par) = N_Package_Declaration then
13262 Collect_Previous_Instances
13263 (Visible_Declarations (Specification (Par)));
13264 Collect_Previous_Instances
13265 (Private_Declarations (Specification (Par)));
13268 -- Enclosing unit is a subprogram body. In this
13269 -- case all instance bodies are processed in order
13270 -- and there is no need to collect them separately.
13275 Decl := First_Elmt (Previous_Instances);
13276 while Present (Decl) loop
13278 (Inst_Node => Node (Decl),
13280 Instance_Spec (Node (Decl)),
13281 Expander_Status => Exp_Status,
13282 Current_Sem_Unit =>
13283 Get_Code_Unit (Sloc (Node (Decl))),
13284 Scope_Suppress => Scope_Suppress,
13285 Local_Suppress_Stack_Top =>
13286 Local_Suppress_Stack_Top,
13287 Version => Ada_Version,
13288 Version_Pragma => Ada_Version_Pragma,
13289 Warnings => Save_Warnings,
13290 SPARK_Mode => SPARK_Mode,
13291 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13293 -- Package instance
13296 Nkind (Node (Decl)) = N_Package_Instantiation
13298 Instantiate_Package_Body
13299 (Info, Body_Optional => True);
13301 -- Subprogram instance
13304 -- The instance_spec is in the wrapper package,
13305 -- usually followed by its local renaming
13306 -- declaration. See Build_Subprogram_Renaming
13307 -- for details. If the instance carries aspects,
13308 -- these result in the corresponding pragmas,
13309 -- inserted after the subprogram declaration.
13310 -- They must be skipped as well when retrieving
13311 -- the desired spec. A direct link would be
13316 (Last (Visible_Declarations
13317 (Specification (Info.Act_Decl))));
13319 while Nkind_In (Decl,
13320 N_Subprogram_Renaming_Declaration, N_Pragma)
13322 Decl := Prev (Decl);
13325 Info.Act_Decl := Decl;
13328 Instantiate_Subprogram_Body
13329 (Info, Body_Optional => True);
13337 Instantiate_Package_Body
13339 ((Inst_Node => Inst_Node,
13340 Act_Decl => True_Parent,
13341 Expander_Status => Exp_Status,
13342 Current_Sem_Unit => Get_Code_Unit
13343 (Sloc (Inst_Node)),
13344 Scope_Suppress => Scope_Suppress,
13345 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13346 Version => Ada_Version,
13347 Version_Pragma => Ada_Version_Pragma,
13348 Warnings => Save_Warnings,
13349 SPARK_Mode => SPARK_Mode,
13350 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13351 Body_Optional => Body_Optional);
13355 -- Case where we are not instantiating a nested generic
13358 Opt.Style_Check := False;
13359 Expander_Mode_Save_And_Set (True);
13360 Load_Needed_Body (Comp_Unit, OK);
13361 Opt.Style_Check := Saved_Style_Check;
13362 Restore_Warnings (Saved_Warnings);
13363 Expander_Mode_Restore;
13366 and then Unit_Requires_Body (Defining_Entity (Spec))
13367 and then not Body_Optional
13370 Bname : constant Unit_Name_Type :=
13371 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13374 -- In CodePeer mode, the missing body may make the analysis
13375 -- incomplete, but we do not treat it as fatal.
13377 if CodePeer_Mode then
13381 Error_Msg_Unit_1 := Bname;
13382 Error_Msg_N ("this instantiation requires$!", N);
13383 Error_Msg_File_1 :=
13384 Get_File_Name (Bname, Subunit => False);
13385 Error_Msg_N ("\but file{ was not found!", N);
13386 raise Unrecoverable_Error;
13393 -- If loading parent of the generic caused an instantiation circularity,
13394 -- we abandon compilation at this point, because otherwise in some cases
13395 -- we get into trouble with infinite recursions after this point.
13397 if Circularity_Detected then
13398 raise Unrecoverable_Error;
13400 end Load_Parent_Of_Generic;
13402 ---------------------------------
13403 -- Map_Formal_Package_Entities --
13404 ---------------------------------
13406 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13411 Set_Instance_Of (Form, Act);
13413 -- Traverse formal and actual package to map the corresponding entities.
13414 -- We skip over internal entities that may be generated during semantic
13415 -- analysis, and find the matching entities by name, given that they
13416 -- must appear in the same order.
13418 E1 := First_Entity (Form);
13419 E2 := First_Entity (Act);
13420 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13421 -- Could this test be a single condition??? Seems like it could, and
13422 -- isn't FPE (Form) a constant anyway???
13424 if not Is_Internal (E1)
13425 and then Present (Parent (E1))
13426 and then not Is_Class_Wide_Type (E1)
13427 and then not Is_Internal_Name (Chars (E1))
13429 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13436 Set_Instance_Of (E1, E2);
13438 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13439 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13442 if Is_Constrained (E1) then
13443 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13446 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13447 Map_Formal_Package_Entities (E1, E2);
13454 end Map_Formal_Package_Entities;
13456 -----------------------
13457 -- Move_Freeze_Nodes --
13458 -----------------------
13460 procedure Move_Freeze_Nodes
13461 (Out_Of : Entity_Id;
13466 Next_Decl : Node_Id;
13467 Next_Node : Node_Id := After;
13470 function Is_Outer_Type (T : Entity_Id) return Boolean;
13471 -- Check whether entity is declared in a scope external to that of the
13474 -------------------
13475 -- Is_Outer_Type --
13476 -------------------
13478 function Is_Outer_Type (T : Entity_Id) return Boolean is
13479 Scop : Entity_Id := Scope (T);
13482 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13486 while Scop /= Standard_Standard loop
13487 if Scop = Out_Of then
13490 Scop := Scope (Scop);
13498 -- Start of processing for Move_Freeze_Nodes
13505 -- First remove the freeze nodes that may appear before all other
13509 while Present (Decl)
13510 and then Nkind (Decl) = N_Freeze_Entity
13511 and then Is_Outer_Type (Entity (Decl))
13513 Decl := Remove_Head (L);
13514 Insert_After (Next_Node, Decl);
13515 Set_Analyzed (Decl, False);
13520 -- Next scan the list of declarations and remove each freeze node that
13521 -- appears ahead of the current node.
13523 while Present (Decl) loop
13524 while Present (Next (Decl))
13525 and then Nkind (Next (Decl)) = N_Freeze_Entity
13526 and then Is_Outer_Type (Entity (Next (Decl)))
13528 Next_Decl := Remove_Next (Decl);
13529 Insert_After (Next_Node, Next_Decl);
13530 Set_Analyzed (Next_Decl, False);
13531 Next_Node := Next_Decl;
13534 -- If the declaration is a nested package or concurrent type, then
13535 -- recurse. Nested generic packages will have been processed from the
13538 case Nkind (Decl) is
13539 when N_Package_Declaration =>
13540 Spec := Specification (Decl);
13542 when N_Task_Type_Declaration =>
13543 Spec := Task_Definition (Decl);
13545 when N_Protected_Type_Declaration =>
13546 Spec := Protected_Definition (Decl);
13552 if Present (Spec) then
13553 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13554 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13559 end Move_Freeze_Nodes;
13565 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13567 return Generic_Renamings.Table (E).Next_In_HTable;
13570 ------------------------
13571 -- Preanalyze_Actuals --
13572 ------------------------
13574 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13577 Errs : constant Nat := Serious_Errors_Detected;
13579 Cur : Entity_Id := Empty;
13580 -- Current homograph of the instance name
13583 -- Saved visibility status of the current homograph
13586 Assoc := First (Generic_Associations (N));
13588 -- If the instance is a child unit, its name may hide an outer homonym,
13589 -- so make it invisible to perform name resolution on the actuals.
13591 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13593 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13595 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13597 if Is_Compilation_Unit (Cur) then
13598 Vis := Is_Immediately_Visible (Cur);
13599 Set_Is_Immediately_Visible (Cur, False);
13605 while Present (Assoc) loop
13606 if Nkind (Assoc) /= N_Others_Choice then
13607 Act := Explicit_Generic_Actual_Parameter (Assoc);
13609 -- Within a nested instantiation, a defaulted actual is an empty
13610 -- association, so nothing to analyze. If the subprogram actual
13611 -- is an attribute, analyze prefix only, because actual is not a
13612 -- complete attribute reference.
13614 -- If actual is an allocator, analyze expression only. The full
13615 -- analysis can generate code, and if instance is a compilation
13616 -- unit we have to wait until the package instance is installed
13617 -- to have a proper place to insert this code.
13619 -- String literals may be operators, but at this point we do not
13620 -- know whether the actual is a formal subprogram or a string.
13625 elsif Nkind (Act) = N_Attribute_Reference then
13626 Analyze (Prefix (Act));
13628 elsif Nkind (Act) = N_Explicit_Dereference then
13629 Analyze (Prefix (Act));
13631 elsif Nkind (Act) = N_Allocator then
13633 Expr : constant Node_Id := Expression (Act);
13636 if Nkind (Expr) = N_Subtype_Indication then
13637 Analyze (Subtype_Mark (Expr));
13639 -- Analyze separately each discriminant constraint, when
13640 -- given with a named association.
13646 Constr := First (Constraints (Constraint (Expr)));
13647 while Present (Constr) loop
13648 if Nkind (Constr) = N_Discriminant_Association then
13649 Analyze (Expression (Constr));
13663 elsif Nkind (Act) /= N_Operator_Symbol then
13666 -- Within a package instance, mark actuals that are limited
13667 -- views, so their use can be moved to the body of the
13670 if Is_Entity_Name (Act)
13671 and then Is_Type (Entity (Act))
13672 and then From_Limited_With (Entity (Act))
13673 and then Present (Inst)
13675 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13679 if Errs /= Serious_Errors_Detected then
13681 -- Do a minimal analysis of the generic, to prevent spurious
13682 -- warnings complaining about the generic being unreferenced,
13683 -- before abandoning the instantiation.
13685 Analyze (Name (N));
13687 if Is_Entity_Name (Name (N))
13688 and then Etype (Name (N)) /= Any_Type
13690 Generate_Reference (Entity (Name (N)), Name (N));
13691 Set_Is_Instantiated (Entity (Name (N)));
13694 if Present (Cur) then
13696 -- For the case of a child instance hiding an outer homonym,
13697 -- provide additional warning which might explain the error.
13699 Set_Is_Immediately_Visible (Cur, Vis);
13701 ("& hides outer unit with the same name??",
13702 N, Defining_Unit_Name (N));
13705 Abandon_Instantiation (Act);
13712 if Present (Cur) then
13713 Set_Is_Immediately_Visible (Cur, Vis);
13715 end Preanalyze_Actuals;
13717 -------------------
13718 -- Remove_Parent --
13719 -------------------
13721 procedure Remove_Parent (In_Body : Boolean := False) is
13722 S : Entity_Id := Current_Scope;
13723 -- S is the scope containing the instantiation just completed. The scope
13724 -- stack contains the parent instances of the instantiation, followed by
13733 -- After child instantiation is complete, remove from scope stack the
13734 -- extra copy of the current scope, and then remove parent instances.
13736 if not In_Body then
13739 while Current_Scope /= S loop
13740 P := Current_Scope;
13741 End_Package_Scope (Current_Scope);
13743 if In_Open_Scopes (P) then
13744 E := First_Entity (P);
13745 while Present (E) loop
13746 Set_Is_Immediately_Visible (E, True);
13750 -- If instantiation is declared in a block, it is the enclosing
13751 -- scope that might be a parent instance. Note that only one
13752 -- block can be involved, because the parent instances have
13753 -- been installed within it.
13755 if Ekind (P) = E_Block then
13756 Cur_P := Scope (P);
13761 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13762 -- We are within an instance of some sibling. Retain
13763 -- visibility of parent, for proper subsequent cleanup, and
13764 -- reinstall private declarations as well.
13766 Set_In_Private_Part (P);
13767 Install_Private_Declarations (P);
13770 -- If the ultimate parent is a top-level unit recorded in
13771 -- Instance_Parent_Unit, then reset its visibility to what it was
13772 -- before instantiation. (It's not clear what the purpose is of
13773 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13774 -- present before the ultimate parent test was added.???)
13776 elsif not In_Open_Scopes (Scope (P))
13777 or else (P = Instance_Parent_Unit
13778 and then not Parent_Unit_Visible)
13780 Set_Is_Immediately_Visible (P, False);
13782 -- If the current scope is itself an instantiation of a generic
13783 -- nested within P, and we are in the private part of body of this
13784 -- instantiation, restore the full views of P, that were removed
13785 -- in End_Package_Scope above. This obscure case can occur when a
13786 -- subunit of a generic contains an instance of a child unit of
13787 -- its generic parent unit.
13789 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13791 Par : constant Entity_Id :=
13792 Generic_Parent (Package_Specification (S));
13795 and then P = Scope (Par)
13796 and then (In_Package_Body (S) or else In_Private_Part (S))
13798 Set_In_Private_Part (P);
13799 Install_Private_Declarations (P);
13805 -- Reset visibility of entities in the enclosing scope
13807 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13809 Hidden := First_Elmt (Hidden_Entities);
13810 while Present (Hidden) loop
13811 Set_Is_Immediately_Visible (Node (Hidden), True);
13812 Next_Elmt (Hidden);
13816 -- Each body is analyzed separately, and there is no context that
13817 -- needs preserving from one body instance to the next, so remove all
13818 -- parent scopes that have been installed.
13820 while Present (S) loop
13821 End_Package_Scope (S);
13822 Set_Is_Immediately_Visible (S, False);
13823 S := Current_Scope;
13824 exit when S = Standard_Standard;
13833 procedure Restore_Env is
13834 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13837 if No (Current_Instantiated_Parent.Act_Id) then
13838 -- Restore environment after subprogram inlining
13840 Restore_Private_Views (Empty);
13843 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13844 Exchanged_Views := Saved.Exchanged_Views;
13845 Hidden_Entities := Saved.Hidden_Entities;
13846 Current_Sem_Unit := Saved.Current_Sem_Unit;
13847 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13848 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13850 Restore_Opt_Config_Switches (Saved.Switches);
13852 Instance_Envs.Decrement_Last;
13855 ---------------------------
13856 -- Restore_Private_Views --
13857 ---------------------------
13859 procedure Restore_Private_Views
13860 (Pack_Id : Entity_Id;
13861 Is_Package : Boolean := True)
13866 Dep_Elmt : Elmt_Id;
13869 procedure Restore_Nested_Formal (Formal : Entity_Id);
13870 -- Hide the generic formals of formal packages declared with box which
13871 -- were reachable in the current instantiation.
13873 ---------------------------
13874 -- Restore_Nested_Formal --
13875 ---------------------------
13877 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13881 if Present (Renamed_Object (Formal))
13882 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13886 elsif Present (Associated_Formal_Package (Formal)) then
13887 Ent := First_Entity (Formal);
13888 while Present (Ent) loop
13889 exit when Ekind (Ent) = E_Package
13890 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13892 Set_Is_Hidden (Ent);
13893 Set_Is_Potentially_Use_Visible (Ent, False);
13895 -- If package, then recurse
13897 if Ekind (Ent) = E_Package then
13898 Restore_Nested_Formal (Ent);
13904 end Restore_Nested_Formal;
13906 -- Start of processing for Restore_Private_Views
13909 M := First_Elmt (Exchanged_Views);
13910 while Present (M) loop
13913 -- Subtypes of types whose views have been exchanged, and that are
13914 -- defined within the instance, were not on the Private_Dependents
13915 -- list on entry to the instance, so they have to be exchanged
13916 -- explicitly now, in order to remain consistent with the view of the
13919 if Ekind_In (Typ, E_Private_Type,
13920 E_Limited_Private_Type,
13921 E_Record_Type_With_Private)
13923 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13924 while Present (Dep_Elmt) loop
13925 Dep_Typ := Node (Dep_Elmt);
13927 if Scope (Dep_Typ) = Pack_Id
13928 and then Present (Full_View (Dep_Typ))
13930 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13931 Exchange_Declarations (Dep_Typ);
13934 Next_Elmt (Dep_Elmt);
13938 Exchange_Declarations (Node (M));
13942 if No (Pack_Id) then
13946 -- Make the generic formal parameters private, and make the formal types
13947 -- into subtypes of the actuals again.
13949 E := First_Entity (Pack_Id);
13950 while Present (E) loop
13951 Set_Is_Hidden (E, True);
13954 and then Nkind (Parent (E)) = N_Subtype_Declaration
13956 -- If the actual for E is itself a generic actual type from
13957 -- an enclosing instance, E is still a generic actual type
13958 -- outside of the current instance. This matter when resolving
13959 -- an overloaded call that may be ambiguous in the enclosing
13960 -- instance, when two of its actuals coincide.
13962 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13963 and then Is_Generic_Actual_Type
13964 (Entity (Subtype_Indication (Parent (E))))
13968 Set_Is_Generic_Actual_Type (E, False);
13971 -- An unusual case of aliasing: the actual may also be directly
13972 -- visible in the generic, and be private there, while it is fully
13973 -- visible in the context of the instance. The internal subtype
13974 -- is private in the instance but has full visibility like its
13975 -- parent in the enclosing scope. This enforces the invariant that
13976 -- the privacy status of all private dependents of a type coincide
13977 -- with that of the parent type. This can only happen when a
13978 -- generic child unit is instantiated within a sibling.
13980 if Is_Private_Type (E)
13981 and then not Is_Private_Type (Etype (E))
13983 Exchange_Declarations (E);
13986 elsif Ekind (E) = E_Package then
13988 -- The end of the renaming list is the renaming of the generic
13989 -- package itself. If the instance is a subprogram, all entities
13990 -- in the corresponding package are renamings. If this entity is
13991 -- a formal package, make its own formals private as well. The
13992 -- actual in this case is itself the renaming of an instantiation.
13993 -- If the entity is not a package renaming, it is the entity
13994 -- created to validate formal package actuals: ignore it.
13996 -- If the actual is itself a formal package for the enclosing
13997 -- generic, or the actual for such a formal package, it remains
13998 -- visible on exit from the instance, and therefore nothing needs
13999 -- to be done either, except to keep it accessible.
14001 if Is_Package and then Renamed_Object (E) = Pack_Id then
14004 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14008 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14010 Set_Is_Hidden (E, False);
14014 Act_P : constant Entity_Id := Renamed_Object (E);
14018 Id := First_Entity (Act_P);
14020 and then Id /= First_Private_Entity (Act_P)
14022 exit when Ekind (Id) = E_Package
14023 and then Renamed_Object (Id) = Act_P;
14025 Set_Is_Hidden (Id, True);
14026 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14028 if Ekind (Id) = E_Package then
14029 Restore_Nested_Formal (Id);
14040 end Restore_Private_Views;
14047 (Gen_Unit : Entity_Id;
14048 Act_Unit : Entity_Id)
14052 Set_Instance_Env (Gen_Unit, Act_Unit);
14055 ----------------------------
14056 -- Save_Global_References --
14057 ----------------------------
14059 procedure Save_Global_References (Templ : Node_Id) is
14061 -- ??? it is horrible to use global variables in highly recursive code
14064 -- The entity of the current associated node
14066 Gen_Scope : Entity_Id;
14067 -- The scope of the generic for which references are being saved
14070 -- The current associated node
14072 function Is_Global (E : Entity_Id) return Boolean;
14073 -- Check whether entity is defined outside of generic unit. Examine the
14074 -- scope of an entity, and the scope of the scope, etc, until we find
14075 -- either Standard, in which case the entity is global, or the generic
14076 -- unit itself, which indicates that the entity is local. If the entity
14077 -- is the generic unit itself, as in the case of a recursive call, or
14078 -- the enclosing generic unit, if different from the current scope, then
14079 -- it is local as well, because it will be replaced at the point of
14080 -- instantiation. On the other hand, if it is a reference to a child
14081 -- unit of a common ancestor, which appears in an instantiation, it is
14082 -- global because it is used to denote a specific compilation unit at
14083 -- the time the instantiations will be analyzed.
14085 procedure Qualify_Universal_Operands
14087 Func_Call : Node_Id);
14088 -- Op denotes a binary or unary operator in generic template Templ. Node
14089 -- Func_Call is the function call alternative of the operator within the
14090 -- the analyzed copy of the template. Change each operand which yields a
14091 -- universal type by wrapping it into a qualified expression
14093 -- Actual_Typ'(Operand)
14095 -- where Actual_Typ is the type of corresponding actual parameter of
14096 -- Operand in Func_Call.
14098 procedure Reset_Entity (N : Node_Id);
14099 -- Save semantic information on global entity so that it is not resolved
14100 -- again at instantiation time.
14102 procedure Save_Entity_Descendants (N : Node_Id);
14103 -- Apply Save_Global_References to the two syntactic descendants of
14104 -- non-terminal nodes that carry an Associated_Node and are processed
14105 -- through Reset_Entity. Once the global entity (if any) has been
14106 -- captured together with its type, only two syntactic descendants need
14107 -- to be traversed to complete the processing of the tree rooted at N.
14108 -- This applies to Selected_Components, Expanded_Names, and to Operator
14109 -- nodes. N can also be a character literal, identifier, or operator
14110 -- symbol node, but the call has no effect in these cases.
14112 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14113 -- Default actuals in nested instances must be handled specially
14114 -- because there is no link to them from the original tree. When an
14115 -- actual subprogram is given by a default, we add an explicit generic
14116 -- association for it in the instantiation node. When we save the
14117 -- global references on the name of the instance, we recover the list
14118 -- of generic associations, and add an explicit one to the original
14119 -- generic tree, through which a global actual can be preserved.
14120 -- Similarly, if a child unit is instantiated within a sibling, in the
14121 -- context of the parent, we must preserve the identifier of the parent
14122 -- so that it can be properly resolved in a subsequent instantiation.
14124 procedure Save_Global_Descendant (D : Union_Id);
14125 -- Apply Save_References recursively to the descendants of node D
14127 procedure Save_References (N : Node_Id);
14128 -- This is the recursive procedure that does the work, once the
14129 -- enclosing generic scope has been established.
14135 function Is_Global (E : Entity_Id) return Boolean is
14138 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14139 -- Determine whether the parent node of a reference to a child unit
14140 -- denotes an instantiation or a formal package, in which case the
14141 -- reference to the child unit is global, even if it appears within
14142 -- the current scope (e.g. when the instance appears within the body
14143 -- of an ancestor).
14145 ----------------------
14146 -- Is_Instance_Node --
14147 ----------------------
14149 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14151 return Nkind (Decl) in N_Generic_Instantiation
14153 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14154 end Is_Instance_Node;
14156 -- Start of processing for Is_Global
14159 if E = Gen_Scope then
14162 elsif E = Standard_Standard then
14165 elsif Is_Child_Unit (E)
14166 and then (Is_Instance_Node (Parent (N2))
14167 or else (Nkind (Parent (N2)) = N_Expanded_Name
14168 and then N2 = Selector_Name (Parent (N2))
14170 Is_Instance_Node (Parent (Parent (N2)))))
14176 while Se /= Gen_Scope loop
14177 if Se = Standard_Standard then
14188 --------------------------------
14189 -- Qualify_Universal_Operands --
14190 --------------------------------
14192 procedure Qualify_Universal_Operands
14194 Func_Call : Node_Id)
14196 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14197 -- Rewrite operand Opnd as a qualified expression of the form
14199 -- Actual_Typ'(Opnd)
14201 -- where Actual is the corresponding actual parameter of Opnd in
14202 -- function call Func_Call.
14204 function Qualify_Type
14206 Typ : Entity_Id) return Node_Id;
14207 -- Qualify type Typ by creating a selected component of the form
14209 -- Scope_Of_Typ.Typ
14211 ---------------------
14212 -- Qualify_Operand --
14213 ---------------------
14215 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14216 Loc : constant Source_Ptr := Sloc (Opnd);
14217 Typ : constant Entity_Id := Etype (Actual);
14222 -- Qualify the operand when it is of a universal type. Note that
14223 -- the template is unanalyzed and it is not possible to directly
14224 -- query the type. This transformation is not done when the type
14225 -- of the actual is internally generated because the type will be
14226 -- regenerated in the instance.
14228 if Yields_Universal_Type (Opnd)
14229 and then Comes_From_Source (Typ)
14230 and then not Is_Hidden (Typ)
14232 -- The type of the actual may be a global reference. Save this
14233 -- information by creating a reference to it.
14235 if Is_Global (Typ) then
14236 Mark := New_Occurrence_Of (Typ, Loc);
14238 -- Otherwise rely on resolution to find the proper type within
14242 Mark := Qualify_Type (Loc, Typ);
14246 Make_Qualified_Expression (Loc,
14247 Subtype_Mark => Mark,
14248 Expression => Relocate_Node (Opnd));
14250 -- Mark the qualification to distinguish it from other source
14251 -- constructs and signal the instantiation mechanism that this
14252 -- node requires special processing. See Copy_Generic_Node for
14255 Set_Is_Qualified_Universal_Literal (Qual);
14257 Rewrite (Opnd, Qual);
14259 end Qualify_Operand;
14265 function Qualify_Type
14267 Typ : Entity_Id) return Node_Id
14269 Scop : constant Entity_Id := Scope (Typ);
14273 Result := Make_Identifier (Loc, Chars (Typ));
14275 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14277 Make_Selected_Component (Loc,
14278 Prefix => Make_Identifier (Loc, Chars (Scop)),
14279 Selector_Name => Result);
14287 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14289 -- Start of processing for Qualify_Universal_Operands
14292 if Nkind (Op) in N_Binary_Op then
14293 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14294 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14296 elsif Nkind (Op) in N_Unary_Op then
14297 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14299 end Qualify_Universal_Operands;
14305 procedure Reset_Entity (N : Node_Id) is
14306 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14307 -- If the type of N2 is global to the generic unit, save the type in
14308 -- the generic node. Just as we perform name capture for explicit
14309 -- references within the generic, we must capture the global types
14310 -- of local entities because they may participate in resolution in
14313 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14314 -- Find the ultimate ancestor of the current unit. If it is not a
14315 -- generic unit, then the name of the current unit in the prefix of
14316 -- an expanded name must be replaced with its generic homonym to
14317 -- ensure that it will be properly resolved in an instance.
14319 ---------------------
14320 -- Set_Global_Type --
14321 ---------------------
14323 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14324 Typ : constant Entity_Id := Etype (N2);
14327 Set_Etype (N, Typ);
14329 -- If the entity of N is not the associated node, this is a
14330 -- nested generic and it has an associated node as well, whose
14331 -- type is already the full view (see below). Indicate that the
14332 -- original node has a private view.
14334 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14335 Set_Has_Private_View (N);
14338 -- If not a private type, nothing else to do
14340 if not Is_Private_Type (Typ) then
14341 if Is_Array_Type (Typ)
14342 and then Is_Private_Type (Component_Type (Typ))
14344 Set_Has_Private_View (N);
14347 -- If it is a derivation of a private type in a context where no
14348 -- full view is needed, nothing to do either.
14350 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14353 -- Otherwise mark the type for flipping and use the full view when
14357 Set_Has_Private_View (N);
14359 if Present (Full_View (Typ)) then
14360 Set_Etype (N2, Full_View (Typ));
14364 if Is_Floating_Point_Type (Typ)
14365 and then Has_Dimension_System (Typ)
14367 Copy_Dimensions (N2, N);
14369 end Set_Global_Type;
14375 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14380 while Is_Child_Unit (Par) loop
14381 Par := Scope (Par);
14387 -- Start of processing for Reset_Entity
14390 N2 := Get_Associated_Node (N);
14393 if Present (E) then
14395 -- If the node is an entry call to an entry in an enclosing task,
14396 -- it is rewritten as a selected component. No global entity to
14397 -- preserve in this case, since the expansion will be redone in
14400 if not Nkind_In (E, N_Defining_Character_Literal,
14401 N_Defining_Identifier,
14402 N_Defining_Operator_Symbol)
14404 Set_Associated_Node (N, Empty);
14405 Set_Etype (N, Empty);
14409 -- If the entity is an itype created as a subtype of an access
14410 -- type with a null exclusion restore source entity for proper
14411 -- visibility. The itype will be created anew in the instance.
14414 and then Ekind (E) = E_Access_Subtype
14415 and then Is_Entity_Name (N)
14416 and then Chars (Etype (E)) = Chars (N)
14419 Set_Entity (N2, E);
14423 if Is_Global (E) then
14425 -- If the entity is a package renaming that is the prefix of
14426 -- an expanded name, it has been rewritten as the renamed
14427 -- package, which is necessary semantically but complicates
14428 -- ASIS tree traversal, so we recover the original entity to
14429 -- expose the renaming. Take into account that the context may
14430 -- be a nested generic, that the original node may itself have
14431 -- an associated node that had better be an entity, and that
14432 -- the current node is still a selected component.
14434 if Ekind (E) = E_Package
14435 and then Nkind (N) = N_Selected_Component
14436 and then Nkind (Parent (N)) = N_Expanded_Name
14437 and then Present (Original_Node (N2))
14438 and then Is_Entity_Name (Original_Node (N2))
14439 and then Present (Entity (Original_Node (N2)))
14441 if Is_Global (Entity (Original_Node (N2))) then
14442 N2 := Original_Node (N2);
14443 Set_Associated_Node (N, N2);
14444 Set_Global_Type (N, N2);
14446 -- Renaming is local, and will be resolved in instance
14449 Set_Associated_Node (N, Empty);
14450 Set_Etype (N, Empty);
14454 Set_Global_Type (N, N2);
14457 elsif Nkind (N) = N_Op_Concat
14458 and then Is_Generic_Type (Etype (N2))
14459 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14461 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14462 and then Is_Intrinsic_Subprogram (E)
14466 -- Entity is local. Mark generic node as unresolved. Note that now
14467 -- it does not have an entity.
14470 Set_Associated_Node (N, Empty);
14471 Set_Etype (N, Empty);
14474 if Nkind (Parent (N)) in N_Generic_Instantiation
14475 and then N = Name (Parent (N))
14477 Save_Global_Defaults (Parent (N), Parent (N2));
14480 elsif Nkind (Parent (N)) = N_Selected_Component
14481 and then Nkind (Parent (N2)) = N_Expanded_Name
14483 if Is_Global (Entity (Parent (N2))) then
14484 Change_Selected_Component_To_Expanded_Name (Parent (N));
14485 Set_Associated_Node (Parent (N), Parent (N2));
14486 Set_Global_Type (Parent (N), Parent (N2));
14487 Save_Entity_Descendants (N);
14489 -- If this is a reference to the current generic entity, replace
14490 -- by the name of the generic homonym of the current package. This
14491 -- is because in an instantiation Par.P.Q will not resolve to the
14492 -- name of the instance, whose enclosing scope is not necessarily
14493 -- Par. We use the generic homonym rather that the name of the
14494 -- generic itself because it may be hidden by a local declaration.
14496 elsif In_Open_Scopes (Entity (Parent (N2)))
14498 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14500 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14501 Rewrite (Parent (N),
14502 Make_Identifier (Sloc (N),
14504 Chars (Generic_Homonym (Entity (Parent (N2))))));
14506 Rewrite (Parent (N),
14507 Make_Identifier (Sloc (N),
14508 Chars => Chars (Selector_Name (Parent (N2)))));
14512 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14513 and then Parent (N) = Name (Parent (Parent (N)))
14515 Save_Global_Defaults
14516 (Parent (Parent (N)), Parent (Parent (N2)));
14519 -- A selected component may denote a static constant that has been
14520 -- folded. If the static constant is global to the generic, capture
14521 -- its value. Otherwise the folding will happen in any instantiation.
14523 elsif Nkind (Parent (N)) = N_Selected_Component
14524 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14526 if Present (Entity (Original_Node (Parent (N2))))
14527 and then Is_Global (Entity (Original_Node (Parent (N2))))
14529 Rewrite (Parent (N), New_Copy (Parent (N2)));
14530 Set_Analyzed (Parent (N), False);
14533 -- A selected component may be transformed into a parameterless
14534 -- function call. If the called entity is global, rewrite the node
14535 -- appropriately, i.e. as an extended name for the global entity.
14537 elsif Nkind (Parent (N)) = N_Selected_Component
14538 and then Nkind (Parent (N2)) = N_Function_Call
14539 and then N = Selector_Name (Parent (N))
14541 if No (Parameter_Associations (Parent (N2))) then
14542 if Is_Global (Entity (Name (Parent (N2)))) then
14543 Change_Selected_Component_To_Expanded_Name (Parent (N));
14544 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14545 Set_Global_Type (Parent (N), Name (Parent (N2)));
14546 Save_Entity_Descendants (N);
14549 Set_Is_Prefixed_Call (Parent (N));
14550 Set_Associated_Node (N, Empty);
14551 Set_Etype (N, Empty);
14554 -- In Ada 2005, X.F may be a call to a primitive operation,
14555 -- rewritten as F (X). This rewriting will be done again in an
14556 -- instance, so keep the original node. Global entities will be
14557 -- captured as for other constructs. Indicate that this must
14558 -- resolve as a call, to prevent accidental overloading in the
14559 -- instance, if both a component and a primitive operation appear
14563 Set_Is_Prefixed_Call (Parent (N));
14566 -- Entity is local. Reset in generic unit, so that node is resolved
14567 -- anew at the point of instantiation.
14570 Set_Associated_Node (N, Empty);
14571 Set_Etype (N, Empty);
14575 -----------------------------
14576 -- Save_Entity_Descendants --
14577 -----------------------------
14579 procedure Save_Entity_Descendants (N : Node_Id) is
14582 when N_Binary_Op =>
14583 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14584 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14587 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14589 when N_Expanded_Name
14590 | N_Selected_Component
14592 Save_Global_Descendant (Union_Id (Prefix (N)));
14593 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14595 when N_Character_Literal
14597 | N_Operator_Symbol
14602 raise Program_Error;
14604 end Save_Entity_Descendants;
14606 --------------------------
14607 -- Save_Global_Defaults --
14608 --------------------------
14610 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14611 Loc : constant Source_Ptr := Sloc (N1);
14612 Assoc2 : constant List_Id := Generic_Associations (N2);
14613 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14620 Actual : Entity_Id;
14623 Assoc1 := Generic_Associations (N1);
14625 if Present (Assoc1) then
14626 Act1 := First (Assoc1);
14629 Set_Generic_Associations (N1, New_List);
14630 Assoc1 := Generic_Associations (N1);
14633 if Present (Assoc2) then
14634 Act2 := First (Assoc2);
14639 while Present (Act1) and then Present (Act2) loop
14644 -- Find the associations added for default subprograms
14646 if Present (Act2) then
14647 while Nkind (Act2) /= N_Generic_Association
14648 or else No (Entity (Selector_Name (Act2)))
14649 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14654 -- Add a similar association if the default is global. The
14655 -- renaming declaration for the actual has been analyzed, and
14656 -- its alias is the program it renames. Link the actual in the
14657 -- original generic tree with the node in the analyzed tree.
14659 while Present (Act2) loop
14660 Subp := Entity (Selector_Name (Act2));
14661 Def := Explicit_Generic_Actual_Parameter (Act2);
14663 -- Following test is defence against rubbish errors
14665 if No (Alias (Subp)) then
14669 -- Retrieve the resolved actual from the renaming declaration
14670 -- created for the instantiated formal.
14672 Actual := Entity (Name (Parent (Parent (Subp))));
14673 Set_Entity (Def, Actual);
14674 Set_Etype (Def, Etype (Actual));
14676 if Is_Global (Actual) then
14678 Make_Generic_Association (Loc,
14680 New_Occurrence_Of (Subp, Loc),
14681 Explicit_Generic_Actual_Parameter =>
14682 New_Occurrence_Of (Actual, Loc));
14684 Set_Associated_Node
14685 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14687 Append (Ndec, Assoc1);
14689 -- If there are other defaults, add a dummy association in case
14690 -- there are other defaulted formals with the same name.
14692 elsif Present (Next (Act2)) then
14694 Make_Generic_Association (Loc,
14696 New_Occurrence_Of (Subp, Loc),
14697 Explicit_Generic_Actual_Parameter => Empty);
14699 Append (Ndec, Assoc1);
14706 if Nkind (Name (N1)) = N_Identifier
14707 and then Is_Child_Unit (Gen_Id)
14708 and then Is_Global (Gen_Id)
14709 and then Is_Generic_Unit (Scope (Gen_Id))
14710 and then In_Open_Scopes (Scope (Gen_Id))
14712 -- This is an instantiation of a child unit within a sibling, so
14713 -- that the generic parent is in scope. An eventual instance must
14714 -- occur within the scope of an instance of the parent. Make name
14715 -- in instance into an expanded name, to preserve the identifier
14716 -- of the parent, so it can be resolved subsequently.
14718 Rewrite (Name (N2),
14719 Make_Expanded_Name (Loc,
14720 Chars => Chars (Gen_Id),
14721 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14722 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14723 Set_Entity (Name (N2), Gen_Id);
14725 Rewrite (Name (N1),
14726 Make_Expanded_Name (Loc,
14727 Chars => Chars (Gen_Id),
14728 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14729 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14731 Set_Associated_Node (Name (N1), Name (N2));
14732 Set_Associated_Node (Prefix (Name (N1)), Empty);
14733 Set_Associated_Node
14734 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14735 Set_Etype (Name (N1), Etype (Gen_Id));
14737 end Save_Global_Defaults;
14739 ----------------------------
14740 -- Save_Global_Descendant --
14741 ----------------------------
14743 procedure Save_Global_Descendant (D : Union_Id) is
14747 if D in Node_Range then
14748 if D = Union_Id (Empty) then
14751 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14752 Save_References (Node_Id (D));
14755 elsif D in List_Range then
14756 pragma Assert (D /= Union_Id (No_List));
14757 -- Because No_List = Empty, which is in Node_Range above
14759 if Is_Empty_List (List_Id (D)) then
14763 N1 := First (List_Id (D));
14764 while Present (N1) loop
14765 Save_References (N1);
14770 -- Element list or other non-node field, nothing to do
14775 end Save_Global_Descendant;
14777 ---------------------
14778 -- Save_References --
14779 ---------------------
14781 -- This is the recursive procedure that does the work once the enclosing
14782 -- generic scope has been established. We have to treat specially a
14783 -- number of node rewritings that are required by semantic processing
14784 -- and which change the kind of nodes in the generic copy: typically
14785 -- constant-folding, replacing an operator node by a string literal, or
14786 -- a selected component by an expanded name. In each of those cases, the
14787 -- transformation is propagated to the generic unit.
14789 procedure Save_References (N : Node_Id) is
14790 Loc : constant Source_Ptr := Sloc (N);
14792 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14793 -- Determine whether arbitrary node Nod requires delayed capture of
14794 -- global references within its aspect specifications.
14796 procedure Save_References_In_Aggregate (N : Node_Id);
14797 -- Save all global references in [extension] aggregate node N
14799 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14800 -- Save all global references in a character literal or operator
14801 -- symbol denoted by N.
14803 procedure Save_References_In_Descendants (N : Node_Id);
14804 -- Save all global references in all descendants of node N
14806 procedure Save_References_In_Identifier (N : Node_Id);
14807 -- Save all global references in identifier node N
14809 procedure Save_References_In_Operator (N : Node_Id);
14810 -- Save all global references in operator node N
14812 procedure Save_References_In_Pragma (Prag : Node_Id);
14813 -- Save all global references found within the expression of pragma
14816 ---------------------------
14817 -- Requires_Delayed_Save --
14818 ---------------------------
14820 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14822 -- Generic packages and subprograms require delayed capture of
14823 -- global references within their aspects due to the timing of
14824 -- annotation analysis.
14826 if Nkind_In (Nod, N_Generic_Package_Declaration,
14827 N_Generic_Subprogram_Declaration,
14829 N_Package_Body_Stub,
14831 N_Subprogram_Body_Stub)
14833 -- Since the capture of global references is done on the
14834 -- unanalyzed generic template, there is no information around
14835 -- to infer the context. Use the Associated_Entity linkages to
14836 -- peek into the analyzed generic copy and determine what the
14837 -- template corresponds to.
14839 if Nod = Templ then
14841 Is_Generic_Declaration_Or_Body
14842 (Unit_Declaration_Node
14843 (Associated_Entity (Defining_Entity (Nod))));
14845 -- Otherwise the generic unit being processed is not the top
14846 -- level template. It is safe to capture of global references
14847 -- within the generic unit because at this point the top level
14848 -- copy is fully analyzed.
14854 -- Otherwise capture the global references without interference
14859 end Requires_Delayed_Save;
14861 ----------------------------------
14862 -- Save_References_In_Aggregate --
14863 ----------------------------------
14865 procedure Save_References_In_Aggregate (N : Node_Id) is
14867 Qual : Node_Id := Empty;
14868 Typ : Entity_Id := Empty;
14870 use Atree.Unchecked_Access;
14871 -- This code section is part of implementing an untyped tree
14872 -- traversal, so it needs direct access to node fields.
14875 N2 := Get_Associated_Node (N);
14877 if Present (N2) then
14880 -- In an instance within a generic, use the name of the actual
14881 -- and not the original generic parameter. If the actual is
14882 -- global in the current generic it must be preserved for its
14885 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14886 and then Present (Generic_Parent_Type (Parent (Typ)))
14888 Typ := Base_Type (Typ);
14889 Set_Etype (N2, Typ);
14893 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14894 Set_Associated_Node (N, Empty);
14896 -- If the aggregate is an actual in a call, it has been
14897 -- resolved in the current context, to some local type. The
14898 -- enclosing call may have been disambiguated by the aggregate,
14899 -- and this disambiguation might fail at instantiation time
14900 -- because the type to which the aggregate did resolve is not
14901 -- preserved. In order to preserve some of this information,
14902 -- wrap the aggregate in a qualified expression, using the id
14903 -- of its type. For further disambiguation we qualify the type
14904 -- name with its scope (if visible) because both id's will have
14905 -- corresponding entities in an instance. This resolves most of
14906 -- the problems with missing type information on aggregates in
14910 and then Nkind (N2) = Nkind (N)
14911 and then Nkind (Parent (N2)) in N_Subprogram_Call
14912 and then Present (Typ)
14913 and then Comes_From_Source (Typ)
14915 Nam := Make_Identifier (Loc, Chars (Typ));
14917 if Is_Immediately_Visible (Scope (Typ)) then
14919 Make_Selected_Component (Loc,
14921 Make_Identifier (Loc, Chars (Scope (Typ))),
14922 Selector_Name => Nam);
14926 Make_Qualified_Expression (Loc,
14927 Subtype_Mark => Nam,
14928 Expression => Relocate_Node (N));
14932 Save_Global_Descendant (Field1 (N));
14933 Save_Global_Descendant (Field2 (N));
14934 Save_Global_Descendant (Field3 (N));
14935 Save_Global_Descendant (Field5 (N));
14937 if Present (Qual) then
14940 end Save_References_In_Aggregate;
14942 ----------------------------------------------
14943 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14944 ----------------------------------------------
14946 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14948 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14951 elsif Nkind (N) = N_Operator_Symbol
14952 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14954 Change_Operator_Symbol_To_String_Literal (N);
14956 end Save_References_In_Char_Lit_Or_Op_Symbol;
14958 ------------------------------------
14959 -- Save_References_In_Descendants --
14960 ------------------------------------
14962 procedure Save_References_In_Descendants (N : Node_Id) is
14963 use Atree.Unchecked_Access;
14964 -- This code section is part of implementing an untyped tree
14965 -- traversal, so it needs direct access to node fields.
14968 Save_Global_Descendant (Field1 (N));
14969 Save_Global_Descendant (Field2 (N));
14970 Save_Global_Descendant (Field3 (N));
14971 Save_Global_Descendant (Field4 (N));
14972 Save_Global_Descendant (Field5 (N));
14973 end Save_References_In_Descendants;
14975 -----------------------------------
14976 -- Save_References_In_Identifier --
14977 -----------------------------------
14979 procedure Save_References_In_Identifier (N : Node_Id) is
14981 -- The node did not undergo a transformation
14983 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14985 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14986 Orig_N2_Parent : constant Node_Id :=
14987 Original_Node (Parent (Aux_N2));
14989 -- The parent of this identifier is a selected component
14990 -- which denotes a named number that was constant folded.
14991 -- Preserve the original name for ASIS and link the parent
14992 -- with its expanded name. The constant folding will be
14993 -- repeated in the instance.
14995 if Nkind (Parent (N)) = N_Selected_Component
14996 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14998 and then Is_Entity_Name (Orig_N2_Parent)
14999 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15000 and then Is_Global (Entity (Orig_N2_Parent))
15003 Set_Associated_Node
15004 (Parent (N), Original_Node (Parent (N2)));
15009 -- If this is a discriminant reference, always save it.
15010 -- It is used in the instance to find the corresponding
15011 -- discriminant positionally rather than by name.
15013 Set_Original_Discriminant
15014 (N, Original_Discriminant (Get_Associated_Node (N)));
15020 -- The analysis of the generic copy transformed the identifier
15021 -- into another construct. Propagate the changes to the template.
15024 N2 := Get_Associated_Node (N);
15026 -- The identifier denotes a call to a parameterless function.
15027 -- Mark the node as resolved when the function is external.
15029 if Nkind (N2) = N_Function_Call then
15030 E := Entity (Name (N2));
15032 if Present (E) and then Is_Global (E) then
15033 Set_Etype (N, Etype (N2));
15035 Set_Associated_Node (N, Empty);
15036 Set_Etype (N, Empty);
15039 -- The identifier denotes a named number that was constant
15040 -- folded. Preserve the original name for ASIS and undo the
15041 -- constant folding which will be repeated in the instance.
15043 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15044 and then Is_Entity_Name (Original_Node (N2))
15046 Set_Associated_Node (N, Original_Node (N2));
15049 -- The identifier resolved to a string literal. Propagate this
15050 -- information to the generic template.
15052 elsif Nkind (N2) = N_String_Literal then
15053 Rewrite (N, New_Copy (N2));
15055 -- The identifier is rewritten as a dereference if it is the
15056 -- prefix of an implicit dereference. Preserve the original
15057 -- tree as the analysis of the instance will expand the node
15058 -- again, but preserve the resolved entity if it is global.
15060 elsif Nkind (N2) = N_Explicit_Dereference then
15061 if Is_Entity_Name (Prefix (N2))
15062 and then Present (Entity (Prefix (N2)))
15063 and then Is_Global (Entity (Prefix (N2)))
15065 Set_Associated_Node (N, Prefix (N2));
15067 elsif Nkind (Prefix (N2)) = N_Function_Call
15068 and then Present (Entity (Name (Prefix (N2))))
15069 and then Is_Global (Entity (Name (Prefix (N2))))
15072 Make_Explicit_Dereference (Loc,
15074 Make_Function_Call (Loc,
15077 (Entity (Name (Prefix (N2))), Loc))));
15080 Set_Associated_Node (N, Empty);
15081 Set_Etype (N, Empty);
15084 -- The subtype mark of a nominally unconstrained object is
15085 -- rewritten as a subtype indication using the bounds of the
15086 -- expression. Recover the original subtype mark.
15088 elsif Nkind (N2) = N_Subtype_Indication
15089 and then Is_Entity_Name (Original_Node (N2))
15091 Set_Associated_Node (N, Original_Node (N2));
15095 end Save_References_In_Identifier;
15097 ---------------------------------
15098 -- Save_References_In_Operator --
15099 ---------------------------------
15101 procedure Save_References_In_Operator (N : Node_Id) is
15103 -- The node did not undergo a transformation
15105 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15106 if Nkind (N) = N_Op_Concat then
15107 Set_Is_Component_Left_Opnd (N,
15108 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15110 Set_Is_Component_Right_Opnd (N,
15111 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15116 -- The analysis of the generic copy transformed the operator into
15117 -- some other construct. Propagate the changes to the template if
15121 N2 := Get_Associated_Node (N);
15123 -- The operator resoved to a function call
15125 if Nkind (N2) = N_Function_Call then
15127 -- Add explicit qualifications in the generic template for
15128 -- all operands of universal type. This aids resolution by
15129 -- preserving the actual type of a literal or an attribute
15130 -- that yields a universal result.
15132 Qualify_Universal_Operands (N, N2);
15134 E := Entity (Name (N2));
15136 if Present (E) and then Is_Global (E) then
15137 Set_Etype (N, Etype (N2));
15139 Set_Associated_Node (N, Empty);
15140 Set_Etype (N, Empty);
15143 -- The operator was folded into a literal
15145 elsif Nkind_In (N2, N_Integer_Literal,
15149 if Present (Original_Node (N2))
15150 and then Nkind (Original_Node (N2)) = Nkind (N)
15152 -- Operation was constant-folded. Whenever possible,
15153 -- recover semantic information from unfolded node,
15156 Set_Associated_Node (N, Original_Node (N2));
15158 if Nkind (N) = N_Op_Concat then
15159 Set_Is_Component_Left_Opnd (N,
15160 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15161 Set_Is_Component_Right_Opnd (N,
15162 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15167 -- Propagate the constant folding back to the template
15170 Rewrite (N, New_Copy (N2));
15171 Set_Analyzed (N, False);
15174 -- The operator was folded into an enumeration literal. Retain
15175 -- the entity to avoid spurious ambiguities if it is overloaded
15176 -- at the point of instantiation or inlining.
15178 elsif Nkind (N2) = N_Identifier
15179 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15181 Rewrite (N, New_Copy (N2));
15182 Set_Analyzed (N, False);
15186 -- Complete the operands check if node has not been constant
15189 if Nkind (N) in N_Op then
15190 Save_Entity_Descendants (N);
15192 end Save_References_In_Operator;
15194 -------------------------------
15195 -- Save_References_In_Pragma --
15196 -------------------------------
15198 procedure Save_References_In_Pragma (Prag : Node_Id) is
15200 Do_Save : Boolean := True;
15202 use Atree.Unchecked_Access;
15203 -- This code section is part of implementing an untyped tree
15204 -- traversal, so it needs direct access to node fields.
15207 -- Do not save global references in pragmas generated from aspects
15208 -- because the pragmas will be regenerated at instantiation time.
15210 if From_Aspect_Specification (Prag) then
15213 -- The capture of global references within contract-related source
15214 -- pragmas associated with generic packages, subprograms or their
15215 -- respective bodies must be delayed due to timing of annotation
15216 -- analysis. Global references are still captured in routine
15217 -- Save_Global_References_In_Contract.
15219 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15220 if Is_Package_Contract_Annotation (Prag) then
15221 Context := Find_Related_Package_Or_Body (Prag);
15223 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15224 Context := Find_Related_Declaration_Or_Body (Prag);
15227 -- The use of Original_Node accounts for the case when the
15228 -- related context is generic template.
15230 if Requires_Delayed_Save (Original_Node (Context)) then
15235 -- For all other cases, save all global references within the
15236 -- descendants, but skip the following semantic fields:
15238 -- Field1 - Next_Pragma
15239 -- Field3 - Corresponding_Aspect
15240 -- Field5 - Next_Rep_Item
15243 Save_Global_Descendant (Field2 (Prag));
15244 Save_Global_Descendant (Field4 (Prag));
15246 end Save_References_In_Pragma;
15248 -- Start of processing for Save_References
15256 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15257 Save_References_In_Aggregate (N);
15259 -- Character literals, operator symbols
15261 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15262 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15264 -- Defining identifiers
15266 elsif Nkind (N) in N_Entity then
15271 elsif Nkind (N) = N_Identifier then
15272 Save_References_In_Identifier (N);
15276 elsif Nkind (N) in N_Op then
15277 Save_References_In_Operator (N);
15281 elsif Nkind (N) = N_Pragma then
15282 Save_References_In_Pragma (N);
15285 Save_References_In_Descendants (N);
15288 -- Save all global references found within the aspect specifications
15289 -- of the related node.
15291 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15293 -- The capture of global references within aspects associated with
15294 -- generic packages, subprograms or their bodies must be delayed
15295 -- due to timing of annotation analysis. Global references are
15296 -- still captured in routine Save_Global_References_In_Contract.
15298 if Requires_Delayed_Save (N) then
15301 -- Otherwise save all global references within the aspects
15304 Save_Global_References_In_Aspects (N);
15307 end Save_References;
15309 -- Start of processing for Save_Global_References
15312 Gen_Scope := Current_Scope;
15314 -- If the generic unit is a child unit, references to entities in the
15315 -- parent are treated as local, because they will be resolved anew in
15316 -- the context of the instance of the parent.
15318 while Is_Child_Unit (Gen_Scope)
15319 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15321 Gen_Scope := Scope (Gen_Scope);
15324 Save_References (Templ);
15325 end Save_Global_References;
15327 ---------------------------------------
15328 -- Save_Global_References_In_Aspects --
15329 ---------------------------------------
15331 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15336 Asp := First (Aspect_Specifications (N));
15337 while Present (Asp) loop
15338 Expr := Expression (Asp);
15340 if Present (Expr) then
15341 Save_Global_References (Expr);
15346 end Save_Global_References_In_Aspects;
15348 ------------------------------------------
15349 -- Set_Copied_Sloc_For_Inherited_Pragma --
15350 ------------------------------------------
15352 procedure Set_Copied_Sloc_For_Inherited_Pragma
15357 Create_Instantiation_Source (N, E,
15358 Inlined_Body => False,
15359 Inherited_Pragma => True,
15360 Factor => S_Adjustment);
15361 end Set_Copied_Sloc_For_Inherited_Pragma;
15363 --------------------------------------
15364 -- Set_Copied_Sloc_For_Inlined_Body --
15365 --------------------------------------
15367 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15369 Create_Instantiation_Source (N, E,
15370 Inlined_Body => True,
15371 Inherited_Pragma => False,
15372 Factor => S_Adjustment);
15373 end Set_Copied_Sloc_For_Inlined_Body;
15375 ---------------------
15376 -- Set_Instance_Of --
15377 ---------------------
15379 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15381 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15382 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15383 Generic_Renamings.Increment_Last;
15384 end Set_Instance_Of;
15386 --------------------
15387 -- Set_Next_Assoc --
15388 --------------------
15390 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15392 Generic_Renamings.Table (E).Next_In_HTable := Next;
15393 end Set_Next_Assoc;
15395 -------------------
15396 -- Start_Generic --
15397 -------------------
15399 procedure Start_Generic is
15401 -- ??? More things could be factored out in this routine.
15402 -- Should probably be done at a later stage.
15404 Generic_Flags.Append (Inside_A_Generic);
15405 Inside_A_Generic := True;
15407 Expander_Mode_Save_And_Set (False);
15410 ----------------------
15411 -- Set_Instance_Env --
15412 ----------------------
15414 procedure Set_Instance_Env
15415 (Gen_Unit : Entity_Id;
15416 Act_Unit : Entity_Id)
15418 Assertion_Status : constant Boolean := Assertions_Enabled;
15419 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15420 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15423 -- Regardless of the current mode, predefined units are analyzed in the
15424 -- most current Ada mode, and earlier version Ada checks do not apply
15425 -- to predefined units. Nothing needs to be done for non-internal units.
15426 -- These are always analyzed in the current mode.
15428 if Is_Internal_File_Name
15429 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15430 Renamings_Included => True)
15432 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15434 -- In Ada2012 we may want to enable assertions in an instance of a
15435 -- predefined unit, in which case we need to preserve the current
15436 -- setting for the Assertions_Enabled flag. This will become more
15437 -- critical when pre/postconditions are added to predefined units,
15438 -- as is already the case for some numeric libraries.
15440 if Ada_Version >= Ada_2012 then
15441 Assertions_Enabled := Assertion_Status;
15444 -- SPARK_Mode for an instance is the one applicable at the point of
15447 SPARK_Mode := Save_SPARK_Mode;
15448 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15451 Current_Instantiated_Parent :=
15452 (Gen_Id => Gen_Unit,
15453 Act_Id => Act_Unit,
15454 Next_In_HTable => Assoc_Null);
15455 end Set_Instance_Env;
15461 procedure Switch_View (T : Entity_Id) is
15462 BT : constant Entity_Id := Base_Type (T);
15463 Priv_Elmt : Elmt_Id := No_Elmt;
15464 Priv_Sub : Entity_Id;
15467 -- T may be private but its base type may have been exchanged through
15468 -- some other occurrence, in which case there is nothing to switch
15469 -- besides T itself. Note that a private dependent subtype of a private
15470 -- type might not have been switched even if the base type has been,
15471 -- because of the last branch of Check_Private_View (see comment there).
15473 if not Is_Private_Type (BT) then
15474 Prepend_Elmt (Full_View (T), Exchanged_Views);
15475 Exchange_Declarations (T);
15479 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15481 if Present (Full_View (BT)) then
15482 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15483 Exchange_Declarations (BT);
15486 while Present (Priv_Elmt) loop
15487 Priv_Sub := (Node (Priv_Elmt));
15489 -- We avoid flipping the subtype if the Etype of its full view is
15490 -- private because this would result in a malformed subtype. This
15491 -- occurs when the Etype of the subtype full view is the full view of
15492 -- the base type (and since the base types were just switched, the
15493 -- subtype is pointing to the wrong view). This is currently the case
15494 -- for tagged record types, access types (maybe more?) and needs to
15495 -- be resolved. ???
15497 if Present (Full_View (Priv_Sub))
15498 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15500 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15501 Exchange_Declarations (Priv_Sub);
15504 Next_Elmt (Priv_Elmt);
15512 function True_Parent (N : Node_Id) return Node_Id is
15514 if Nkind (Parent (N)) = N_Subunit then
15515 return Parent (Corresponding_Stub (Parent (N)));
15521 -----------------------------
15522 -- Valid_Default_Attribute --
15523 -----------------------------
15525 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15526 Attr_Id : constant Attribute_Id :=
15527 Get_Attribute_Id (Attribute_Name (Def));
15528 T : constant Entity_Id := Entity (Prefix (Def));
15529 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15535 if No (T) or else T = Any_Id then
15540 F := First_Formal (Nam);
15541 while Present (F) loop
15542 Num_F := Num_F + 1;
15547 when Attribute_Adjacent
15548 | Attribute_Ceiling
15549 | Attribute_Copy_Sign
15551 | Attribute_Fraction
15552 | Attribute_Machine
15554 | Attribute_Remainder
15555 | Attribute_Rounding
15556 | Attribute_Unbiased_Rounding
15560 and then Is_Floating_Point_Type (T);
15562 when Attribute_Image
15566 | Attribute_Wide_Image
15567 | Attribute_Wide_Value
15569 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15574 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15576 when Attribute_Input =>
15577 OK := (Is_Fun and then Num_F = 1);
15579 when Attribute_Output
15583 OK := not Is_Fun and then Num_F = 2;
15591 ("attribute reference has wrong profile for subprogram", Def);
15593 end Valid_Default_Attribute;