ada: Fix fallout of fix to handling of private views in instances
[official-gcc.git] / gcc / ada / sem_ch12.adb
bloba8e7c909c394ade6e370a504993e7ca84e6d1bdb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
79 with Table;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
85 with GNAT.HTable;
87 package body Sem_Ch12 is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
109 -- | copy | | unit |
110 -- | |==============>| |
111 -- |___________| global |__________|
112 -- references | | |
113 -- | | |
114 -- .-----|--|.
115 -- | .-----|---.
116 -- | | .----------.
117 -- | | | generic |
118 -- |__| | |
119 -- |__| instance |
120 -- |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
135 -- actuals.
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
142 -- package.
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
169 -- generic ...
170 -- package Outer is
171 -- ...
172 -- type Semi_Global is ... -- global to inner.
174 -- generic ... -- 1
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
178 -- end Outer;
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
220 -- with B; with A;
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
261 -- as annotations:
263 -- package subprogram [body]
264 -- Abstract_State Always_Terminates
265 -- Initial_Condition Contract_Cases
266 -- Initializes Depends
267 -- Exceptional_Cases
268 -- Extensions_Visible
269 -- Global
270 -- package body Post
271 -- Refined_State Post_Class
272 -- Postcondition
273 -- Pre
274 -- Pre_Class
275 -- Precondition
276 -- Refined_Depends
277 -- Refined_Global
278 -- Refined_Post
279 -- Subprogram_Variant
280 -- Test_Case
282 -- Most package contract annotations utilize forward references to classify
283 -- data declared within the package [body]. Subprogram annotations then use
284 -- the classifications to further refine them. These inter dependencies are
285 -- problematic with respect to the implementation of generics because their
286 -- analysis, capture of global references and instantiation does not mesh
287 -- well with the existing mechanism.
289 -- 1) Analysis of generic contracts is carried out the same way non-generic
290 -- contracts are analyzed:
292 -- 1.1) General rule - a contract is analyzed after all related aspects
293 -- and pragmas are analyzed. This is done by routines
295 -- Analyze_Package_Body_Contract
296 -- Analyze_Package_Contract
297 -- Analyze_Subprogram_Body_Contract
298 -- Analyze_Subprogram_Contract
300 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
301 -- are processed.
303 -- 1.3) Compilation unit body - the contract is analyzed at the end of
304 -- the body declaration list.
306 -- 1.4) Package - the contract is analyzed at the end of the private or
307 -- visible declarations, prior to analyzing the contracts of any nested
308 -- packages or subprograms.
310 -- 1.5) Package body - the contract is analyzed at the end of the body
311 -- declaration list, prior to analyzing the contracts of any nested
312 -- packages or subprograms.
314 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
315 -- package or a subprogram, then its contract is analyzed at the end of
316 -- the enclosing declarations, otherwise the subprogram is a compilation
317 -- unit 1.2).
319 -- 1.7) Subprogram body - if the subprogram body is declared inside a
320 -- block, a package body or a subprogram body, then its contract is
321 -- analyzed at the end of the enclosing declarations, otherwise the
322 -- subprogram is a compilation unit 1.3).
324 -- 2) Capture of global references within contracts is done after capturing
325 -- global references within the generic template. There are two reasons for
326 -- this delay - pragma annotations are not part of the generic template in
327 -- the case of a generic subprogram declaration, and analysis of contracts
328 -- is delayed.
330 -- Contract-related source pragmas within generic templates are prepared
331 -- for delayed capture of global references by routine
333 -- Create_Generic_Contract
335 -- The routine associates these pragmas with the contract of the template.
336 -- In the case of a generic subprogram declaration, the routine creates
337 -- generic templates for the pragmas declared after the subprogram because
338 -- they are not part of the template.
340 -- generic -- template starts
341 -- procedure Gen_Proc (Input : Integer); -- template ends
342 -- pragma Precondition (Input > 0); -- requires own template
344 -- 2.1) The capture of global references with aspect specifications and
345 -- source pragmas that apply to a generic unit must be suppressed when
346 -- the generic template is being processed because the contracts have not
347 -- been analyzed yet. Any attempts to capture global references at that
348 -- point will destroy the Associated_Node linkages and leave the template
349 -- undecorated. This delay is controlled by routine
351 -- Requires_Delayed_Save
353 -- 2.2) The real capture of global references within a contract is done
354 -- after the contract has been analyzed, by routine
356 -- Save_Global_References_In_Contract
358 -- 3) The instantiation of a generic contract occurs as part of the
359 -- instantiation of the contract owner. Generic subprogram declarations
360 -- require additional processing when the contract is specified by pragmas
361 -- because the pragmas are not part of the generic template. This is done
362 -- by routine
364 -- Instantiate_Subprogram_Contract
366 --------------------------------------------------
367 -- Formal packages and partial parameterization --
368 --------------------------------------------------
370 -- When compiling a generic, a formal package is a local instantiation. If
371 -- declared with a box, its generic formals are visible in the enclosing
372 -- generic. If declared with a partial list of actuals, those actuals that
373 -- are defaulted (covered by an Others clause, or given an explicit box
374 -- initialization) are also visible in the enclosing generic, while those
375 -- that have a corresponding actual are not.
377 -- In our source model of instantiation, the same visibility must be
378 -- present in the spec and body of an instance: the names of the formals
379 -- that are defaulted must be made visible within the instance, and made
380 -- invisible (hidden) after the instantiation is complete, so that they
381 -- are not accessible outside of the instance.
383 -- In a generic, a formal package is treated like a special instantiation.
384 -- Our Ada 95 compiler handled formals with and without box in different
385 -- ways. With partial parameterization, we use a single model for both.
386 -- We create a package declaration that consists of the specification of
387 -- the generic package, and a set of declarations that map the actuals
388 -- into local renamings, just as we do for bona fide instantiations. For
389 -- defaulted parameters and formals with a box, we copy directly the
390 -- declarations of the formals into this local package. The result is a
391 -- package whose visible declarations may include generic formals. This
392 -- package is only used for type checking and visibility analysis, and
393 -- never reaches the back end, so it can freely violate the placement
394 -- rules for generic formal declarations.
396 -- The list of declarations (renamings and copies of formals) is built
397 -- by Analyze_Associations, just as for regular instantiations.
399 -- At the point of instantiation, conformance checking must be applied only
400 -- to those parameters that were specified in the formals. We perform this
401 -- checking by creating another internal instantiation, this one including
402 -- only the renamings and the formals (the rest of the package spec is not
403 -- relevant to conformance checking). We can then traverse two lists: the
404 -- list of actuals in the instance that corresponds to the formal package,
405 -- and the list of actuals produced for this bogus instantiation. We apply
406 -- the conformance rules to those actuals that are not defaulted, i.e.
407 -- which still appear as generic formals.
409 -- When we compile an instance body we must make the right parameters
410 -- visible again. The predicate Is_Generic_Formal indicates which of the
411 -- formals should have its Is_Hidden flag reset.
413 -----------------------
414 -- Local subprograms --
415 -----------------------
417 procedure Abandon_Instantiation (N : Node_Id);
418 pragma No_Return (Abandon_Instantiation);
419 -- Posts an error message "instantiation abandoned" at the indicated node
420 -- and then raises the exception Instantiation_Error to do it.
422 procedure Analyze_Formal_Array_Type
423 (T : in out Entity_Id;
424 Def : Node_Id);
425 -- A formal array type is treated like an array type declaration, and
426 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
427 -- in-out, because in the case of an anonymous type the entity is
428 -- actually created in the procedure.
430 -- The following procedures treat other kinds of formal parameters
432 procedure Analyze_Formal_Derived_Interface_Type
433 (N : Node_Id;
434 T : Entity_Id;
435 Def : Node_Id);
437 procedure Analyze_Formal_Derived_Type
438 (N : Node_Id;
439 T : Entity_Id;
440 Def : Node_Id);
442 procedure Analyze_Formal_Interface_Type
443 (N : Node_Id;
444 T : Entity_Id;
445 Def : Node_Id);
447 -- The following subprograms create abbreviated declarations for formal
448 -- scalar types. We introduce an anonymous base of the proper class for
449 -- each of them, and define the formals as constrained first subtypes of
450 -- their bases. The bounds are expressions that are non-static in the
451 -- generic.
453 procedure Analyze_Formal_Decimal_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
460 (T : Entity_Id; Def : Node_Id);
462 procedure Analyze_Formal_Private_Type
463 (N : Node_Id;
464 T : Entity_Id;
465 Def : Node_Id);
466 -- Creates a new private type, which does not require completion
468 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
469 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
471 procedure Analyze_Generic_Formal_Part (N : Node_Id);
472 -- Analyze generic formal part
474 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
475 -- Create a new access type with the given designated type
477 function Analyze_Associations
478 (I_Node : Node_Id;
479 Formals : List_Id;
480 F_Copy : List_Id) return List_Id;
481 -- At instantiation time, build the list of associations between formals
482 -- and actuals. Each association becomes a renaming declaration for the
483 -- formal entity. F_Copy is the analyzed list of formals in the generic
484 -- copy. It is used to apply legality checks to the actuals. I_Node is the
485 -- instantiation node itself.
487 procedure Analyze_Subprogram_Instantiation
488 (N : Node_Id;
489 K : Entity_Kind);
491 procedure Build_Instance_Compilation_Unit_Nodes
492 (N : Node_Id;
493 Act_Body : Node_Id;
494 Act_Decl : Node_Id);
495 -- This procedure is used in the case where the generic instance of a
496 -- subprogram body or package body is a library unit. In this case, the
497 -- original library unit node for the generic instantiation must be
498 -- replaced by the resulting generic body, and a link made to a new
499 -- compilation unit node for the generic declaration. The argument N is
500 -- the original generic instantiation. Act_Body and Act_Decl are the body
501 -- and declaration of the instance (either package body and declaration
502 -- nodes or subprogram body and declaration nodes depending on the case).
503 -- On return, the node N has been rewritten with the actual body.
505 function Build_Subprogram_Decl_Wrapper
506 (Formal_Subp : Entity_Id) return Node_Id;
507 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
508 -- At the point of instantiation these contracts apply to uses of
509 -- the actual subprogram. This is implemented by creating wrapper
510 -- subprograms instead of the renamings previously used to link
511 -- formal subprograms and the corresponding actuals. If the actual
512 -- is not an entity (e.g. an attribute reference) a renaming is
513 -- created to handle the expansion of the attribute.
515 function Build_Subprogram_Body_Wrapper
516 (Formal_Subp : Entity_Id;
517 Actual_Name : Node_Id) return Node_Id;
518 -- The body of the wrapper is a call to the actual, with the generated
519 -- pre/postconditon checks added.
521 procedure Check_Abbreviated_Instance
522 (N : Node_Id;
523 Parent_Installed : in out Boolean);
524 -- If the name of the generic unit in an abbreviated instantiation is an
525 -- expanded name, then the prefix may be an instance and the selector may
526 -- designate a child unit. If the parent is installed as a result of this
527 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
528 -- unchanged by the call.
530 -- This routine needs to be called for declaration nodes of formal objects,
531 -- types and subprograms to check whether they are the copy, present in the
532 -- visible part of the abbreviated instantiation of formal packages, of the
533 -- declaration node of their corresponding formal parameter in the template
534 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
535 -- the proper context for their analysis.
537 procedure Check_Access_Definition (N : Node_Id);
538 -- Subsidiary routine to null exclusion processing. Perform an assertion
539 -- check on Ada version and the presence of an access definition in N.
541 procedure Check_Formal_Packages (P_Id : Entity_Id);
542 -- Apply the following to all formal packages in generic associations.
543 -- Restore the visibility of the formals of the instance that are not
544 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
545 -- created for formal instances that are not defaulted.
547 procedure Check_Formal_Package_Instance
548 (Formal_Pack : Entity_Id;
549 Actual_Pack : Entity_Id);
550 -- Verify that the actuals of the actual instance match the actuals of
551 -- the template for a formal package that is not declared with a box.
553 procedure Check_Forward_Instantiation (Decl : Node_Id);
554 -- If the generic is a local entity and the corresponding body has not
555 -- been seen yet, flag enclosing packages to indicate that it will be
556 -- elaborated after the generic body. Subprograms declared in the same
557 -- package cannot be inlined by the front end because front-end inlining
558 -- requires a strict linear order of elaboration.
560 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
561 -- Check if some association between formals and actuals requires to make
562 -- visible primitives of a tagged type, and make those primitives visible.
563 -- Return the list of primitives whose visibility is modified (to restore
564 -- their visibility later through Restore_Hidden_Primitives). If no
565 -- candidate is found then return No_Elist.
567 procedure Check_Hidden_Child_Unit
568 (N : Node_Id;
569 Gen_Unit : Entity_Id;
570 Act_Decl_Id : Entity_Id);
571 -- If the generic unit is an implicit child instance within a parent
572 -- instance, we need to make an explicit test that it is not hidden by
573 -- a child instance of the same name and parent.
575 procedure Check_Generic_Actuals
576 (Instance : Entity_Id;
577 Is_Formal_Box : Boolean);
578 -- Similar to previous one. Check the actuals in the instantiation,
579 -- whose views can change between the point of instantiation and the point
580 -- of instantiation of the body. In addition, mark the generic renamings
581 -- as generic actuals, so that they are not compatible with other actuals.
582 -- Recurse on an actual that is a formal package whose declaration has
583 -- a box.
585 function Contains_Instance_Of
586 (Inner : Entity_Id;
587 Outer : Entity_Id;
588 N : Node_Id) return Boolean;
589 -- Inner is instantiated within the generic Outer. Check whether Inner
590 -- directly or indirectly contains an instance of Outer or of one of its
591 -- parents, in the case of a subunit. Each generic unit holds a list of
592 -- the entities instantiated within (at any depth). This procedure
593 -- determines whether the set of such lists contains a cycle, i.e. an
594 -- illegal circular instantiation.
596 function Denotes_Formal_Package
597 (Pack : Entity_Id;
598 On_Exit : Boolean := False;
599 Instance : Entity_Id := Empty) return Boolean;
600 -- Returns True if E is a formal package of an enclosing generic, or
601 -- the actual for such a formal in an enclosing instantiation. If such
602 -- a package is used as a formal in an nested generic, or as an actual
603 -- in a nested instantiation, the visibility of ITS formals should not
604 -- be modified. When called from within Restore_Private_Views, the flag
605 -- On_Exit is true, to indicate that the search for a possible enclosing
606 -- instance should ignore the current one. In that case Instance denotes
607 -- the declaration for which this is an actual. This declaration may be
608 -- an instantiation in the source, or the internal instantiation that
609 -- corresponds to the actual for a formal package.
611 function Earlier (N1, N2 : Node_Id) return Boolean;
612 -- Yields True if N1 and N2 appear in the same compilation unit,
613 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
614 -- traversal of the tree for the unit. Used to determine the placement
615 -- of freeze nodes for instance bodies that may depend on other instances.
617 function Find_Actual_Type
618 (Typ : Entity_Id;
619 Gen_Type : Entity_Id) return Entity_Id;
620 -- When validating the actual types of a child instance, check whether
621 -- the formal is a formal type of the parent unit, and retrieve the current
622 -- actual for it. Typ is the entity in the analyzed formal type declaration
623 -- (component or index type of an array type, or designated type of an
624 -- access formal) and Gen_Type is the enclosing analyzed formal array
625 -- or access type. The desired actual may be a formal of a parent, or may
626 -- be declared in a formal package of a parent. In both cases it is a
627 -- generic actual type because it appears within a visible instance.
628 -- Finally, it may be declared in a parent unit without being a formal
629 -- of that unit, in which case it must be retrieved by visibility.
630 -- Ambiguities may still arise if two homonyms are declared in two formal
631 -- packages, and the prefix of the formal type may be needed to resolve
632 -- the ambiguity in the instance ???
634 procedure Freeze_Package_Instance
635 (N : Node_Id;
636 Gen_Body : Node_Id;
637 Gen_Decl : Node_Id;
638 Act_Id : Entity_Id);
639 -- If the instantiation happens textually before the body of the generic,
640 -- the instantiation of the body must be analyzed after the generic body,
641 -- and not at the point of instantiation. Such early instantiations can
642 -- happen if the generic and the instance appear in a package declaration
643 -- because the generic body can only appear in the corresponding package
644 -- body. Early instantiations can also appear if generic, instance and
645 -- body are all in the declarative part of a subprogram or entry. Entities
646 -- of packages that are early instantiations are delayed, and their freeze
647 -- node appears after the generic body. This rather complex machinery is
648 -- needed when nested instantiations are present, because the source does
649 -- not carry any indication of where the corresponding instance bodies must
650 -- be installed and frozen.
652 procedure Freeze_Subprogram_Instance
653 (N : Node_Id;
654 Gen_Body : Node_Id;
655 Pack_Id : Entity_Id);
656 -- The generic body may appear textually after the instance, including
657 -- in the proper body of a stub, or within a different package instance.
658 -- Given that the instance can only be elaborated after the generic, we
659 -- place freeze nodes for the instance and/or for packages that may enclose
660 -- the instance and the generic, so that the back-end can establish the
661 -- proper order of elaboration.
663 function Get_Associated_Node (N : Node_Id) return Node_Id;
664 -- In order to propagate semantic information back from the analyzed copy
665 -- to the original generic, we maintain links between selected nodes in the
666 -- generic and their corresponding copies. At the end of generic analysis,
667 -- the routine Save_Global_References traverses the generic tree, examines
668 -- the semantic information, and preserves the links to those nodes that
669 -- contain global information. At instantiation, the information from the
670 -- associated node is placed on the new copy, so that name resolution is
671 -- not repeated.
673 -- Three kinds of source nodes have associated nodes:
675 -- a) those that can reference (denote) entities, that is identifiers,
676 -- character literals, expanded_names, operator symbols, operators,
677 -- and attribute reference nodes. These nodes have an Entity field
678 -- and are the set of nodes that are in N_Has_Entity.
680 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
682 -- c) selected components (N_Selected_Component)
684 -- For the first class, the associated node preserves the entity if it is
685 -- global. If the generic contains nested instantiations, the associated
686 -- node itself has been recopied, and a chain of them must be followed.
688 -- For aggregates, the associated node allows retrieval of the type, which
689 -- may otherwise not appear in the generic. The view of this type may be
690 -- different between generic and instantiation, and the full view can be
691 -- installed before the instantiation is analyzed. For aggregates of type
692 -- extensions, the same view exchange may have to be performed for some of
693 -- the ancestor types, if their view is private at the point of
694 -- instantiation.
696 -- Nodes that are selected components in the parse tree may be rewritten
697 -- as expanded names after resolution, and must be treated as potential
698 -- entity holders, which is why they also have an Associated_Node.
700 -- Nodes that do not come from source, such as freeze nodes, do not appear
701 -- in the generic tree, and need not have an associated node.
703 -- The associated node is stored in the Associated_Node field. Note that
704 -- this field overlaps Entity, which is fine, because the whole point is
705 -- that we don't need or want the normal Entity field in this situation.
707 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
708 -- Traverse the Exchanged_Views list to see if a type was private
709 -- and has already been flipped during this phase of instantiation.
711 function Has_Contracts (Decl : Node_Id) return Boolean;
712 -- Determine whether a formal subprogram has a Pre- or Postcondition,
713 -- in which case a subprogram wrapper has to be built for the actual.
715 procedure Hide_Current_Scope;
716 -- When instantiating a generic child unit, the parent context must be
717 -- present, but the instance and all entities that may be generated
718 -- must be inserted in the current scope. We leave the current scope
719 -- on the stack, but make its entities invisible to avoid visibility
720 -- problems. This is reversed at the end of the instantiation. This is
721 -- not done for the instantiation of the bodies, which only require the
722 -- instances of the generic parents to be in scope.
724 function In_Main_Context (E : Entity_Id) return Boolean;
725 -- Check whether an instantiation is in the context of the main unit.
726 -- Used to determine whether its body should be elaborated to allow
727 -- front-end inlining.
729 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
730 -- Add the context clause of the unit containing a generic unit to a
731 -- compilation unit that is, or contains, an instantiation.
733 procedure Init_Env;
734 -- Establish environment for subsequent instantiation. Separated from
735 -- Save_Env because data-structures for visibility handling must be
736 -- initialized before call to Check_Generic_Child_Unit.
738 procedure Inline_Instance_Body
739 (N : Node_Id;
740 Gen_Unit : Entity_Id;
741 Act_Decl : Node_Id);
742 -- If front-end inlining is requested, instantiate the package body,
743 -- and preserve the visibility of its compilation unit, to insure
744 -- that successive instantiations succeed.
746 procedure Insert_Freeze_Node_For_Instance
747 (N : Node_Id;
748 F_Node : Node_Id);
749 -- N denotes a package or a subprogram instantiation and F_Node is the
750 -- associated freeze node. Insert the freeze node before the first source
751 -- body which follows immediately after N. If no such body is found, the
752 -- freeze node is inserted at the end of the declarative region which
753 -- contains N, unless the instantiation is done in a package spec that is
754 -- not at library level, in which case it is inserted at the outer level.
755 -- This can also be invoked to insert the freeze node of a package that
756 -- encloses an instantiation, in which case N may denote an arbitrary node.
758 procedure Install_Formal_Packages (Par : Entity_Id);
759 -- Install the visible part of any formal of the parent that is a formal
760 -- package. Note that for the case of a formal package with a box, this
761 -- includes the formal part of the formal package (12.7(10/2)).
763 procedure Install_Hidden_Primitives
764 (Prims_List : in out Elist_Id;
765 Gen_T : Entity_Id;
766 Act_T : Entity_Id);
767 -- Remove suffix 'P' from hidden primitives of Act_T to match the
768 -- visibility of primitives of Gen_T. The list of primitives to which
769 -- the suffix is removed is added to Prims_List to restore them later.
771 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
772 -- When compiling an instance of a child unit the parent (which is
773 -- itself an instance) is an enclosing scope that must be made
774 -- immediately visible. This procedure is also used to install the non-
775 -- generic parent of a generic child unit when compiling its body, so
776 -- that full views of types in the parent are made visible.
778 -- The functions Instantiate_XXX perform various legality checks and build
779 -- the declarations for instantiated generic parameters. In all of these
780 -- Formal is the entity in the generic unit, Actual is the entity of
781 -- expression in the generic associations, and Analyzed_Formal is the
782 -- formal in the generic copy, which contains the semantic information to
783 -- be used to validate the actual.
785 function Instantiate_Object
786 (Formal : Node_Id;
787 Actual : Node_Id;
788 Analyzed_Formal : Node_Id) return List_Id;
790 function Instantiate_Type
791 (Formal : Node_Id;
792 Actual : Node_Id;
793 Analyzed_Formal : Node_Id;
794 Actual_Decls : List_Id) return List_Id;
796 function Instantiate_Formal_Subprogram
797 (Formal : Node_Id;
798 Actual : Node_Id;
799 Analyzed_Formal : Node_Id) return Node_Id;
801 function Instantiate_Formal_Package
802 (Formal : Node_Id;
803 Actual : Node_Id;
804 Analyzed_Formal : Node_Id) return List_Id;
805 -- If the formal package is declared with a box, special visibility rules
806 -- apply to its formals: they are in the visible part of the package. This
807 -- is true in the declarative region of the formal package, that is to say
808 -- in the enclosing generic or instantiation. For an instantiation, the
809 -- parameters of the formal package are made visible in an explicit step.
810 -- Furthermore, if the actual has a visible USE clause, these formals must
811 -- be made potentially use-visible as well. On exit from the enclosing
812 -- instantiation, the reverse must be done.
814 -- For a formal package declared without a box, there are conformance rules
815 -- that apply to the actuals in the generic declaration and the actuals of
816 -- the actual package in the enclosing instantiation. The simplest way to
817 -- apply these rules is to repeat the instantiation of the formal package
818 -- in the context of the enclosing instance, and compare the generic
819 -- associations of this instantiation with those of the actual package.
820 -- This internal instantiation only needs to contain the renamings of the
821 -- formals: the visible and private declarations themselves need not be
822 -- created.
824 -- In Ada 2005, the formal package may be only partially parameterized.
825 -- In that case the visibility step must make visible those actuals whose
826 -- corresponding formals were given with a box. A final complication
827 -- involves inherited operations from formal derived types, which must
828 -- be visible if the type is.
830 function Is_In_Main_Unit (N : Node_Id) return Boolean;
831 -- Test if given node is in the main unit
833 procedure Load_Parent_Of_Generic
834 (N : Node_Id;
835 Spec : Node_Id;
836 Body_Optional : Boolean := False);
837 -- If the generic appears in a separate non-generic library unit, load the
838 -- corresponding body to retrieve the body of the generic. N is the node
839 -- for the generic instantiation, Spec is the generic package declaration.
841 -- Body_Optional is a flag that indicates that the body is being loaded to
842 -- ensure that temporaries are generated consistently when there are other
843 -- instances in the current declarative part that precede the one being
844 -- loaded. In that case a missing body is acceptable.
846 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
847 -- Within the generic part, entities in the formal package are
848 -- visible. To validate subsequent type declarations, indicate
849 -- the correspondence between the entities in the analyzed formal,
850 -- and the entities in the actual package. There are three packages
851 -- involved in the instantiation of a formal package: the parent
852 -- generic P1 which appears in the generic declaration, the fake
853 -- instantiation P2 which appears in the analyzed generic, and whose
854 -- visible entities may be used in subsequent formals, and the actual
855 -- P3 in the instance. To validate subsequent formals, me indicate
856 -- that the entities in P2 are mapped into those of P3. The mapping of
857 -- entities has to be done recursively for nested packages.
859 procedure Move_Freeze_Nodes
860 (Out_Of : Entity_Id;
861 After : Node_Id;
862 L : List_Id);
863 -- Freeze nodes can be generated in the analysis of a generic unit, but
864 -- will not be seen by the back-end. It is necessary to move those nodes
865 -- to the enclosing scope if they freeze an outer entity. We place them
866 -- at the end of the enclosing generic package, which is semantically
867 -- neutral.
869 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
870 -- Analyze actuals to perform name resolution. Full resolution is done
871 -- later, when the expected types are known, but names have to be captured
872 -- before installing parents of generics, that are not visible for the
873 -- actuals themselves.
875 -- If Inst is present, it is the entity of the package instance. This
876 -- entity is marked as having a limited_view actual when some actual is
877 -- a limited view. This is used to place the instance body properly.
879 procedure Provide_Completing_Bodies (N : Node_Id);
880 -- Generate completing bodies for all subprograms found within package or
881 -- subprogram declaration N.
883 procedure Remove_Parent (In_Body : Boolean := False);
884 -- Reverse effect after instantiation of child is complete
886 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
887 -- Determine whether the formal package declaration N requires conformance
888 -- checking with actuals in instantiations.
890 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
891 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
892 -- set to No_Elist.
894 procedure Set_Instance_Env
895 (Gen_Unit : Entity_Id;
896 Act_Unit : Entity_Id);
897 -- Save current instance on saved environment, to be used to determine
898 -- the global status of entities in nested instances. Part of Save_Env.
899 -- called after verifying that the generic unit is legal for the instance,
900 -- The procedure also examines whether the generic unit is a predefined
901 -- unit, in order to set configuration switches accordingly. As a result
902 -- the procedure must be called after analyzing and freezing the actuals.
904 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
905 -- Associate analyzed generic parameter with corresponding instance. Used
906 -- for semantic checks at instantiation time.
908 function True_Parent (N : Node_Id) return Node_Id;
909 -- For a subunit, return parent of corresponding stub, else return
910 -- parent of node.
912 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
913 -- Verify that an attribute that appears as the default for a formal
914 -- subprogram is a function or procedure with the correct profile.
916 procedure Validate_Formal_Type_Default (Decl : Node_Id);
917 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
918 -- that it is the name of a type in the same class as the formal.
919 -- The treatment parallels what is done in Instantiate_Type but differs
920 -- in a few ways so that this machinery cannot be reused as is: on one
921 -- hand there are no visibility issues for a default, because it is
922 -- analyzed in the same context as the formal type definition; on the
923 -- other hand the check needs to take into acount the use of a previous
924 -- formal type in the current formal type definition (see details in
925 -- AI12-0205).
927 -------------------------------------------
928 -- Data Structures for Generic Renamings --
929 -------------------------------------------
931 -- The map Generic_Renamings associates generic entities with their
932 -- corresponding actuals. Currently used to validate type instances. It
933 -- will eventually be used for all generic parameters to eliminate the
934 -- need for overload resolution in the instance.
936 type Assoc_Ptr is new Int;
938 Assoc_Null : constant Assoc_Ptr := -1;
940 type Assoc is record
941 Gen_Id : Entity_Id;
942 Act_Id : Entity_Id;
943 Next_In_HTable : Assoc_Ptr;
944 end record;
946 package Generic_Renamings is new Table.Table
947 (Table_Component_Type => Assoc,
948 Table_Index_Type => Assoc_Ptr,
949 Table_Low_Bound => 0,
950 Table_Initial => 10,
951 Table_Increment => 100,
952 Table_Name => "Generic_Renamings");
954 -- Variable to hold enclosing instantiation. When the environment is
955 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
957 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
959 -- Hash table for associations
961 HTable_Size : constant := 37;
962 type HTable_Range is range 0 .. HTable_Size - 1;
964 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
965 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
966 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
967 function Hash (F : Entity_Id) return HTable_Range;
969 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
970 Header_Num => HTable_Range,
971 Element => Assoc,
972 Elmt_Ptr => Assoc_Ptr,
973 Null_Ptr => Assoc_Null,
974 Set_Next => Set_Next_Assoc,
975 Next => Next_Assoc,
976 Key => Entity_Id,
977 Get_Key => Get_Gen_Id,
978 Hash => Hash,
979 Equal => "=");
981 Exchanged_Views : Elist_Id;
982 -- This list holds the private views that have been exchanged during
983 -- instantiation to restore the visibility of the generic declaration.
984 -- (see comments above). After instantiation, the current visibility is
985 -- reestablished by means of a traversal of this list.
987 Hidden_Entities : Elist_Id;
988 -- This list holds the entities of the current scope that are removed
989 -- from immediate visibility when instantiating a child unit. Their
990 -- visibility is restored in Remove_Parent.
992 -- Because instantiations can be recursive, the following must be saved
993 -- on entry and restored on exit from an instantiation (spec or body).
994 -- This is done by the two procedures Save_Env and Restore_Env. For
995 -- package and subprogram instantiations (but not for the body instances)
996 -- the action of Save_Env is done in two steps: Init_Env is called before
997 -- Check_Generic_Child_Unit, because setting the parent instances requires
998 -- that the visibility data structures be properly initialized. Once the
999 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1001 Parent_Unit_Visible : Boolean := False;
1002 -- Parent_Unit_Visible is used when the generic is a child unit, and
1003 -- indicates whether the ultimate parent of the generic is visible in the
1004 -- instantiation environment. It is used to reset the visibility of the
1005 -- parent at the end of the instantiation (see Remove_Parent).
1007 Instance_Parent_Unit : Entity_Id := Empty;
1008 -- This records the ultimate parent unit of an instance of a generic
1009 -- child unit and is used in conjunction with Parent_Unit_Visible to
1010 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1012 type Instance_Env is record
1013 Instantiated_Parent : Assoc;
1014 Exchanged_Views : Elist_Id;
1015 Hidden_Entities : Elist_Id;
1016 Current_Sem_Unit : Unit_Number_Type;
1017 Parent_Unit_Visible : Boolean := False;
1018 Instance_Parent_Unit : Entity_Id := Empty;
1019 Switches : Config_Switches_Type;
1020 end record;
1022 package Instance_Envs is new Table.Table (
1023 Table_Component_Type => Instance_Env,
1024 Table_Index_Type => Int,
1025 Table_Low_Bound => 0,
1026 Table_Initial => 32,
1027 Table_Increment => 100,
1028 Table_Name => "Instance_Envs");
1030 procedure Restore_Private_Views
1031 (Pack_Id : Entity_Id;
1032 Is_Package : Boolean := True);
1033 -- Restore the private views of external types, and unmark the generic
1034 -- renamings of actuals, so that they become compatible subtypes again.
1035 -- For subprograms, Pack_Id is the package constructed to hold the
1036 -- renamings.
1038 procedure Switch_View (T : Entity_Id);
1039 -- Switch the partial and full views of a type and its private
1040 -- dependents (i.e. its subtypes and derived types).
1042 ------------------------------------
1043 -- Structures for Error Reporting --
1044 ------------------------------------
1046 Instantiation_Node : Node_Id;
1047 -- Used by subprograms that validate instantiation of formal parameters
1048 -- where there might be no actual on which to place the error message.
1049 -- Also used to locate the instantiation node for generic subunits.
1051 Instantiation_Error : exception;
1052 -- When there is a semantic error in the generic parameter matching,
1053 -- there is no point in continuing the instantiation, because the
1054 -- number of cascaded errors is unpredictable. This exception aborts
1055 -- the instantiation process altogether.
1057 S_Adjustment : Sloc_Adjustment;
1058 -- Offset created for each node in an instantiation, in order to keep
1059 -- track of the source position of the instantiation in each of its nodes.
1060 -- A subsequent semantic error or warning on a construct of the instance
1061 -- points to both places: the original generic node, and the point of
1062 -- instantiation. See Sinput and Sinput.L for additional details.
1064 ------------------------------------------------------------
1065 -- Data structure for keeping track when inside a Generic --
1066 ------------------------------------------------------------
1068 -- The following table is used to save values of the Inside_A_Generic
1069 -- flag (see spec of Sem) when they are saved by Start_Generic.
1071 package Generic_Flags is new Table.Table (
1072 Table_Component_Type => Boolean,
1073 Table_Index_Type => Int,
1074 Table_Low_Bound => 0,
1075 Table_Initial => 32,
1076 Table_Increment => 200,
1077 Table_Name => "Generic_Flags");
1079 ---------------------------
1080 -- Abandon_Instantiation --
1081 ---------------------------
1083 procedure Abandon_Instantiation (N : Node_Id) is
1084 begin
1085 Error_Msg_N ("\instantiation abandoned!", N);
1086 raise Instantiation_Error;
1087 end Abandon_Instantiation;
1089 ----------------------------------
1090 -- Adjust_Inherited_Pragma_Sloc --
1091 ----------------------------------
1093 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1094 begin
1095 Adjust_Instantiation_Sloc (N, S_Adjustment);
1096 end Adjust_Inherited_Pragma_Sloc;
1098 --------------------------
1099 -- Analyze_Associations --
1100 --------------------------
1102 function Analyze_Associations
1103 (I_Node : Node_Id;
1104 Formals : List_Id;
1105 F_Copy : List_Id) return List_Id
1107 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1108 Assoc_List : constant List_Id := New_List;
1109 Default_Actuals : constant List_Id := New_List;
1110 Gen_Unit : constant Entity_Id :=
1111 Defining_Entity (Parent (F_Copy));
1113 Actuals : List_Id;
1114 Actual : Node_Id;
1115 Analyzed_Formal : Node_Id;
1116 First_Named : Node_Id := Empty;
1117 Formal : Node_Id;
1118 Match : Node_Id := Empty;
1119 Named : Node_Id;
1120 Saved_Formal : Node_Id;
1122 Default_Formals : constant List_Id := New_List;
1123 -- If an Others_Choice is present, some of the formals may be defaulted.
1124 -- To simplify the treatment of visibility in an instance, we introduce
1125 -- individual defaults for each such formal. These defaults are
1126 -- appended to the list of associations and replace the Others_Choice.
1128 Found_Assoc : Node_Id;
1129 -- Association for the current formal being match. Empty if there are
1130 -- no remaining actuals, or if there is no named association with the
1131 -- name of the formal.
1133 Is_Named_Assoc : Boolean;
1134 Num_Matched : Nat := 0;
1135 Num_Actuals : Nat := 0;
1137 Others_Present : Boolean := False;
1138 Others_Choice : Node_Id := Empty;
1139 -- In Ada 2005, indicates partial parameterization of a formal
1140 -- package. As usual an other association must be last in the list.
1142 procedure Build_Subprogram_Wrappers;
1143 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1144 -- subprograms. The implementation of making the formal into a renaming
1145 -- of the actual does not work, given that subprogram renaming cannot
1146 -- carry aspect specifications. Instead we must create subprogram
1147 -- wrappers whose body is a call to the actual, and whose declaration
1148 -- carries the aspects of the formal.
1150 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1151 -- Warn if an actual fixed-point type has user-defined arithmetic
1152 -- operations, but there is no corresponding formal in the generic,
1153 -- in which case the predefined operations will be used. This merits
1154 -- a warning because of the special semantics of fixed point ops.
1156 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id);
1157 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1158 -- cannot have a named association for it. AI05-0025 extends this rule
1159 -- to formals of formal packages by AI05-0025, and it also applies to
1160 -- box-initialized formals.
1162 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1163 -- Determine whether the parameter types and the return type of Subp
1164 -- are fully defined at the point of instantiation.
1166 function Matching_Actual
1167 (F : Entity_Id;
1168 A_F : Entity_Id) return Node_Id;
1169 -- Find actual that corresponds to a given formal parameter. If the
1170 -- actuals are positional, return the next one, if any. If the actuals
1171 -- are named, scan the parameter associations to find the right one.
1172 -- A_F is the corresponding entity in the analyzed generic, which is
1173 -- placed on the selector name.
1175 -- In Ada 2005, a named association may be given with a box, in which
1176 -- case Matching_Actual sets Found_Assoc to the generic association,
1177 -- but return Empty for the actual itself. In this case the code below
1178 -- creates a corresponding declaration for the formal.
1180 function Partial_Parameterization return Boolean;
1181 -- Ada 2005: if no match is found for a given formal, check if the
1182 -- association for it includes a box, or whether the associations
1183 -- include an Others clause.
1185 procedure Process_Default (Formal : Node_Id);
1186 -- Add a copy of the declaration of a generic formal to the list of
1187 -- associations, and add an explicit box association for its entity
1188 -- if there is none yet, and the default comes from an Others_Choice.
1190 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1191 -- Determine whether Subp renames one of the subprograms defined in the
1192 -- generated package Standard.
1194 procedure Set_Analyzed_Formal;
1195 -- Find the node in the generic copy that corresponds to a given formal.
1196 -- The semantic information on this node is used to perform legality
1197 -- checks on the actuals. Because semantic analysis can introduce some
1198 -- anonymous entities or modify the declaration node itself, the
1199 -- correspondence between the two lists is not one-one. In addition to
1200 -- anonymous types, the presence a formal equality will introduce an
1201 -- implicit declaration for the corresponding inequality.
1203 -------------------------------
1204 -- Build_Subprogram_Wrappers --
1205 -------------------------------
1207 procedure Build_Subprogram_Wrappers is
1208 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
1209 -- Adjust sloc so that errors located at N will be reported with
1210 -- information about the instance and not just about the generic.
1212 ------------------------
1213 -- Adjust_Aspect_Sloc --
1214 ------------------------
1216 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
1217 begin
1218 Adjust_Instantiation_Sloc (N, S_Adjustment);
1219 return OK;
1220 end Adjust_Aspect_Sloc;
1222 procedure Adjust_Aspect_Slocs is new
1223 Traverse_Proc (Adjust_Aspect_Sloc);
1225 Formal : constant Entity_Id :=
1226 Defining_Unit_Name (Specification (Analyzed_Formal));
1227 Aspect_Spec : Node_Id;
1228 Decl_Node : Node_Id;
1229 Actual_Name : Node_Id;
1231 -- Start of processing for Build_Subprogram_Wrappers
1233 begin
1234 -- Create declaration for wrapper subprogram
1235 -- The actual can be overloaded, in which case it will be
1236 -- resolved when the call in the wrapper body is analyzed.
1237 -- We attach the possible interpretations of the actual to
1238 -- the name to be used in the call in the wrapper body.
1240 if Is_Entity_Name (Match) then
1241 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1243 if Is_Overloaded (Match) then
1244 Save_Interps (Match, Actual_Name);
1245 end if;
1247 else
1248 -- Use renaming declaration created when analyzing actual.
1249 -- This may be incomplete if there are several formal
1250 -- subprograms whose actual is an attribute ???
1252 declare
1253 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1255 begin
1256 Actual_Name := New_Occurrence_Of
1257 (Defining_Entity (Renaming_Decl), Sloc (Match));
1258 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1259 end;
1260 end if;
1262 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1264 -- Transfer aspect specifications from formal subprogram to wrapper
1266 Set_Aspect_Specifications (Decl_Node,
1267 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1269 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1270 while Present (Aspect_Spec) loop
1271 Adjust_Aspect_Slocs (Aspect_Spec);
1272 Set_Analyzed (Aspect_Spec, False);
1273 Next (Aspect_Spec);
1274 end loop;
1276 Append_To (Assoc_List, Decl_Node);
1278 -- Create corresponding body, and append it to association list
1279 -- that appears at the head of the declarations in the instance.
1280 -- The subprogram may be called in the analysis of subsequent
1281 -- actuals.
1283 Append_To (Assoc_List,
1284 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1285 end Build_Subprogram_Wrappers;
1287 ----------------------------------------
1288 -- Check_Overloaded_Formal_Subprogram --
1289 ----------------------------------------
1291 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id) is
1292 Temp_Formal : Node_Id;
1294 begin
1295 Temp_Formal := First (Formals);
1296 while Present (Temp_Formal) loop
1297 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1298 and then Temp_Formal /= Formal
1299 and then
1300 Chars (Defining_Unit_Name (Specification (Formal))) =
1301 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1302 then
1303 if Present (Found_Assoc) then
1304 Error_Msg_N
1305 ("named association not allowed for overloaded formal",
1306 Found_Assoc);
1308 else
1309 Error_Msg_N
1310 ("named association not allowed for overloaded formal",
1311 Others_Choice);
1312 end if;
1314 Abandon_Instantiation (Instantiation_Node);
1315 end if;
1317 Next (Temp_Formal);
1318 end loop;
1319 end Check_Overloaded_Formal_Subprogram;
1321 -------------------------------
1322 -- Check_Fixed_Point_Actual --
1323 -------------------------------
1325 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1326 Typ : constant Entity_Id := Entity (Actual);
1327 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1328 Elem : Elmt_Id;
1329 Formal : Node_Id;
1330 Op : Entity_Id;
1332 begin
1333 -- Locate primitive operations of the type that are arithmetic
1334 -- operations.
1336 Elem := First_Elmt (Prims);
1337 while Present (Elem) loop
1338 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1340 -- Check whether the generic unit has a formal subprogram of
1341 -- the same name. This does not check types but is good enough
1342 -- to justify a warning.
1344 Formal := First_Non_Pragma (Formals);
1345 Op := Alias (Node (Elem));
1347 while Present (Formal) loop
1348 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1349 and then Chars (Defining_Entity (Formal)) =
1350 Chars (Node (Elem))
1351 then
1352 exit;
1354 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1355 declare
1356 Assoc : Node_Id;
1357 Ent : Entity_Id;
1359 begin
1360 -- Locate corresponding actual, and check whether it
1361 -- includes a fixed-point type.
1363 Assoc := First (Assoc_List);
1364 while Present (Assoc) loop
1365 exit when
1366 Nkind (Assoc) = N_Package_Renaming_Declaration
1367 and then Chars (Defining_Unit_Name (Assoc)) =
1368 Chars (Defining_Identifier (Formal));
1370 Next (Assoc);
1371 end loop;
1373 if Present (Assoc) then
1375 -- If formal package declares a fixed-point type,
1376 -- and the user-defined operator is derived from
1377 -- a generic instance package, the fixed-point type
1378 -- does not use the corresponding predefined op.
1380 Ent := First_Entity (Entity (Name (Assoc)));
1381 while Present (Ent) loop
1382 if Is_Fixed_Point_Type (Ent)
1383 and then Present (Op)
1384 and then Is_Generic_Instance (Scope (Op))
1385 then
1386 return;
1387 end if;
1389 Next_Entity (Ent);
1390 end loop;
1391 end if;
1392 end;
1393 end if;
1395 Next (Formal);
1396 end loop;
1398 if No (Formal) then
1399 Error_Msg_Sloc := Sloc (Node (Elem));
1400 Error_Msg_NE
1401 ("?instance uses predefined operation, not primitive "
1402 & "operation&#", Actual, Node (Elem));
1403 end if;
1404 end if;
1406 Next_Elmt (Elem);
1407 end loop;
1408 end Check_Fixed_Point_Actual;
1410 -------------------------------
1411 -- Has_Fully_Defined_Profile --
1412 -------------------------------
1414 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1415 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1416 -- Determine whethet type Typ is fully defined
1418 ---------------------------
1419 -- Is_Fully_Defined_Type --
1420 ---------------------------
1422 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1423 begin
1424 -- A private type without a full view is not fully defined
1426 if Is_Private_Type (Typ)
1427 and then No (Full_View (Typ))
1428 then
1429 return False;
1431 -- An incomplete type is never fully defined
1433 elsif Is_Incomplete_Type (Typ) then
1434 return False;
1436 -- All other types are fully defined
1438 else
1439 return True;
1440 end if;
1441 end Is_Fully_Defined_Type;
1443 -- Local declarations
1445 Param : Entity_Id;
1447 -- Start of processing for Has_Fully_Defined_Profile
1449 begin
1450 -- Check the parameters
1452 Param := First_Formal (Subp);
1453 while Present (Param) loop
1454 if not Is_Fully_Defined_Type (Etype (Param)) then
1455 return False;
1456 end if;
1458 Next_Formal (Param);
1459 end loop;
1461 -- Check the return type
1463 return Is_Fully_Defined_Type (Etype (Subp));
1464 end Has_Fully_Defined_Profile;
1466 ---------------------
1467 -- Matching_Actual --
1468 ---------------------
1470 function Matching_Actual
1471 (F : Entity_Id;
1472 A_F : Entity_Id) return Node_Id
1474 Prev : Node_Id;
1475 Act : Node_Id;
1477 begin
1478 Is_Named_Assoc := False;
1480 -- End of list of purely positional parameters
1482 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1483 Found_Assoc := Empty;
1484 Act := Empty;
1486 -- Case of positional parameter corresponding to current formal
1488 elsif No (Selector_Name (Actual)) then
1489 Found_Assoc := Actual;
1490 Act := Explicit_Generic_Actual_Parameter (Actual);
1491 Num_Matched := Num_Matched + 1;
1492 Next (Actual);
1494 -- Otherwise scan list of named actuals to find the one with the
1495 -- desired name. All remaining actuals have explicit names.
1497 else
1498 Is_Named_Assoc := True;
1499 Found_Assoc := Empty;
1500 Act := Empty;
1501 Prev := Empty;
1503 while Present (Actual) loop
1504 if Nkind (Actual) = N_Others_Choice then
1505 Found_Assoc := Empty;
1506 Act := Empty;
1508 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1509 Set_Entity (Selector_Name (Actual), A_F);
1510 Set_Etype (Selector_Name (Actual), Etype (A_F));
1511 Generate_Reference (A_F, Selector_Name (Actual));
1513 Found_Assoc := Actual;
1514 Act := Explicit_Generic_Actual_Parameter (Actual);
1515 Num_Matched := Num_Matched + 1;
1516 exit;
1517 end if;
1519 Prev := Actual;
1520 Next (Actual);
1521 end loop;
1523 -- Reset for subsequent searches. In most cases the named
1524 -- associations are in order. If they are not, we reorder them
1525 -- to avoid scanning twice the same actual. This is not just a
1526 -- question of efficiency: there may be multiple defaults with
1527 -- boxes that have the same name. In a nested instantiation we
1528 -- insert actuals for those defaults, and cannot rely on their
1529 -- names to disambiguate them.
1531 if Actual = First_Named then
1532 Next (First_Named);
1534 elsif Present (Actual) then
1535 Insert_Before (First_Named, Remove_Next (Prev));
1536 end if;
1538 Actual := First_Named;
1539 end if;
1541 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1542 Set_Used_As_Generic_Actual (Entity (Act));
1543 end if;
1545 return Act;
1546 end Matching_Actual;
1548 ------------------------------
1549 -- Partial_Parameterization --
1550 ------------------------------
1552 function Partial_Parameterization return Boolean is
1553 begin
1554 return Others_Present
1555 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1556 end Partial_Parameterization;
1558 ---------------------
1559 -- Process_Default --
1560 ---------------------
1562 procedure Process_Default (Formal : Node_Id) is
1563 Loc : constant Source_Ptr := Sloc (I_Node);
1564 F_Id : constant Entity_Id := Defining_Entity (Formal);
1565 Decl : Node_Id;
1566 Default : Node_Id;
1567 Id : Entity_Id;
1569 begin
1570 -- Append copy of formal declaration to associations, and create new
1571 -- defining identifier for it.
1573 Decl := New_Copy_Tree (Formal);
1574 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1576 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1577 Set_Defining_Unit_Name (Specification (Decl), Id);
1579 else
1580 Set_Defining_Identifier (Decl, Id);
1581 end if;
1583 Append (Decl, Assoc_List);
1585 if No (Found_Assoc) then
1586 Default :=
1587 Make_Generic_Association (Loc,
1588 Selector_Name =>
1589 New_Occurrence_Of (Id, Loc),
1590 Explicit_Generic_Actual_Parameter => Empty);
1591 Set_Box_Present (Default);
1592 Append (Default, Default_Formals);
1593 end if;
1594 end Process_Default;
1596 ---------------------------------
1597 -- Renames_Standard_Subprogram --
1598 ---------------------------------
1600 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1601 Id : Entity_Id;
1603 begin
1604 Id := Alias (Subp);
1605 while Present (Id) loop
1606 if Scope (Id) = Standard_Standard then
1607 return True;
1608 end if;
1610 Id := Alias (Id);
1611 end loop;
1613 return False;
1614 end Renames_Standard_Subprogram;
1616 -------------------------
1617 -- Set_Analyzed_Formal --
1618 -------------------------
1620 procedure Set_Analyzed_Formal is
1621 Kind : Node_Kind;
1623 begin
1624 while Present (Analyzed_Formal) loop
1625 Kind := Nkind (Analyzed_Formal);
1627 case Nkind (Formal) is
1628 when N_Formal_Subprogram_Declaration =>
1629 exit when Kind in N_Formal_Subprogram_Declaration
1630 and then
1631 Chars
1632 (Defining_Unit_Name (Specification (Formal))) =
1633 Chars
1634 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1636 when N_Formal_Package_Declaration =>
1637 exit when Kind in N_Formal_Package_Declaration
1638 | N_Generic_Package_Declaration
1639 | N_Package_Declaration;
1641 when N_Use_Package_Clause
1642 | N_Use_Type_Clause
1644 exit;
1646 when others =>
1648 -- Skip freeze nodes, and nodes inserted to replace
1649 -- unrecognized pragmas.
1651 exit when
1652 Kind not in N_Formal_Subprogram_Declaration
1653 and then Kind not in N_Subprogram_Declaration
1654 | N_Freeze_Entity
1655 | N_Null_Statement
1656 | N_Itype_Reference
1657 and then Chars (Defining_Identifier (Formal)) =
1658 Chars (Defining_Identifier (Analyzed_Formal));
1659 end case;
1661 Next (Analyzed_Formal);
1662 end loop;
1663 end Set_Analyzed_Formal;
1665 -- Start of processing for Analyze_Associations
1667 begin
1668 Actuals := Generic_Associations (I_Node);
1670 if Present (Actuals) then
1672 -- Check for an Others choice, indicating a partial parameterization
1673 -- for a formal package.
1675 Actual := First (Actuals);
1676 while Present (Actual) loop
1677 if Nkind (Actual) = N_Others_Choice then
1678 Others_Present := True;
1679 Others_Choice := Actual;
1681 if Present (Next (Actual)) then
1682 Error_Msg_N ("OTHERS must be last association", Actual);
1683 end if;
1685 -- This subprogram is used both for formal packages and for
1686 -- instantiations. For the latter, associations must all be
1687 -- explicit.
1689 if Nkind (I_Node) /= N_Formal_Package_Declaration
1690 and then Comes_From_Source (I_Node)
1691 then
1692 Error_Msg_N
1693 ("OTHERS association not allowed in an instance",
1694 Actual);
1695 end if;
1697 -- In any case, nothing to do after the others association
1699 exit;
1701 elsif Box_Present (Actual)
1702 and then Comes_From_Source (I_Node)
1703 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1704 then
1705 Error_Msg_N
1706 ("box association not allowed in an instance", Actual);
1707 end if;
1709 Next (Actual);
1710 end loop;
1712 -- If named associations are present, save first named association
1713 -- (it may of course be Empty) to facilitate subsequent name search.
1715 First_Named := First (Actuals);
1716 while Present (First_Named)
1717 and then Nkind (First_Named) /= N_Others_Choice
1718 and then No (Selector_Name (First_Named))
1719 loop
1720 Num_Actuals := Num_Actuals + 1;
1721 Next (First_Named);
1722 end loop;
1723 end if;
1725 Named := First_Named;
1726 while Present (Named) loop
1727 if Nkind (Named) /= N_Others_Choice
1728 and then No (Selector_Name (Named))
1729 then
1730 Error_Msg_N ("invalid positional actual after named one", Named);
1731 Abandon_Instantiation (Named);
1732 end if;
1734 -- A named association may lack an actual parameter, if it was
1735 -- introduced for a default subprogram that turns out to be local
1736 -- to the outer instantiation. If it has a box association it must
1737 -- correspond to some formal in the generic.
1739 if Nkind (Named) /= N_Others_Choice
1740 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1741 or else Box_Present (Named))
1742 then
1743 Num_Actuals := Num_Actuals + 1;
1744 end if;
1746 Next (Named);
1747 end loop;
1749 if Present (Formals) then
1750 Formal := First_Non_Pragma (Formals);
1751 Analyzed_Formal := First_Non_Pragma (F_Copy);
1753 if Present (Actuals) then
1754 Actual := First (Actuals);
1756 -- All formals should have default values
1758 else
1759 Actual := Empty;
1760 end if;
1762 while Present (Formal) loop
1763 Set_Analyzed_Formal;
1764 Saved_Formal := Next_Non_Pragma (Formal);
1766 case Nkind (Formal) is
1767 when N_Formal_Object_Declaration =>
1768 Match :=
1769 Matching_Actual
1770 (Defining_Identifier (Formal),
1771 Defining_Identifier (Analyzed_Formal));
1773 if No (Match) and then Partial_Parameterization then
1774 Process_Default (Formal);
1776 else
1777 Append_List
1778 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1779 Assoc_List);
1781 -- For a defaulted in_parameter, create an entry in the
1782 -- the list of defaulted actuals, for GNATprove use. Do
1783 -- not included these defaults for an instance nested
1784 -- within a generic, because the defaults are also used
1785 -- in the analysis of the enclosing generic, and only
1786 -- defaulted subprograms are relevant there.
1788 if No (Match) and then not Inside_A_Generic then
1789 Append_To (Default_Actuals,
1790 Make_Generic_Association (Sloc (I_Node),
1791 Selector_Name =>
1792 New_Occurrence_Of
1793 (Defining_Identifier (Formal), Sloc (I_Node)),
1794 Explicit_Generic_Actual_Parameter =>
1795 New_Copy_Tree (Default_Expression (Formal))));
1796 end if;
1797 end if;
1799 -- If the object is a call to an expression function, this
1800 -- is a freezing point for it.
1802 if Is_Entity_Name (Match)
1803 and then Present (Entity (Match))
1804 and then Nkind
1805 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1806 = N_Expression_Function
1807 then
1808 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1809 end if;
1811 when N_Formal_Type_Declaration =>
1812 Match :=
1813 Matching_Actual
1814 (Defining_Identifier (Formal),
1815 Defining_Identifier (Analyzed_Formal));
1817 if No (Match) then
1818 if Partial_Parameterization then
1819 Process_Default (Formal);
1821 elsif Present (Default_Subtype_Mark (Formal)) then
1822 Match := New_Copy (Default_Subtype_Mark (Formal));
1823 Append_List
1824 (Instantiate_Type
1825 (Formal, Match, Analyzed_Formal, Assoc_List),
1826 Assoc_List);
1827 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1829 else
1830 Error_Msg_Sloc := Sloc (Gen_Unit);
1831 Error_Msg_NE
1832 ("missing actual&",
1833 Instantiation_Node, Defining_Identifier (Formal));
1834 Error_Msg_NE
1835 ("\in instantiation of & declared#",
1836 Instantiation_Node, Gen_Unit);
1837 Abandon_Instantiation (Instantiation_Node);
1838 end if;
1840 else
1841 Analyze (Match);
1842 Append_List
1843 (Instantiate_Type
1844 (Formal, Match, Analyzed_Formal, Assoc_List),
1845 Assoc_List);
1847 -- Warn when an actual is a fixed-point with user-
1848 -- defined promitives. The warning is superfluous
1849 -- if the formal is private, because there can be
1850 -- no arithmetic operations in the generic so there
1851 -- no danger of confusion.
1853 if Is_Fixed_Point_Type (Entity (Match))
1854 and then not Is_Private_Type
1855 (Defining_Identifier (Analyzed_Formal))
1856 then
1857 Check_Fixed_Point_Actual (Match);
1858 end if;
1860 -- An instantiation is a freeze point for the actuals,
1861 -- unless this is a rewritten formal package, or the
1862 -- formal is an Ada 2012 formal incomplete type.
1864 if Nkind (I_Node) = N_Formal_Package_Declaration
1865 or else
1866 (Ada_Version >= Ada_2012
1867 and then
1868 Ekind (Defining_Identifier (Analyzed_Formal)) =
1869 E_Incomplete_Type)
1870 then
1871 null;
1873 else
1874 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1875 end if;
1876 end if;
1878 -- A remote access-to-class-wide type is not a legal actual
1879 -- for a generic formal of an access type (E.2.2(17/2)).
1880 -- In GNAT an exception to this rule is introduced when
1881 -- the formal is marked as remote using implementation
1882 -- defined aspect/pragma Remote_Access_Type. In that case
1883 -- the actual must be remote as well.
1885 -- If the current instantiation is the construction of a
1886 -- local copy for a formal package the actuals may be
1887 -- defaulted, and there is no matching actual to check.
1889 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1890 and then
1891 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1892 N_Access_To_Object_Definition
1893 and then Present (Match)
1894 then
1895 declare
1896 Formal_Ent : constant Entity_Id :=
1897 Defining_Identifier (Analyzed_Formal);
1898 begin
1899 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1900 = Is_Remote_Types (Formal_Ent)
1901 then
1902 -- Remoteness of formal and actual match
1904 null;
1906 elsif Is_Remote_Types (Formal_Ent) then
1908 -- Remote formal, non-remote actual
1910 Error_Msg_NE
1911 ("actual for& must be remote", Match, Formal_Ent);
1913 else
1914 -- Non-remote formal, remote actual
1916 Error_Msg_NE
1917 ("actual for& may not be remote",
1918 Match, Formal_Ent);
1919 end if;
1920 end;
1921 end if;
1923 when N_Formal_Subprogram_Declaration =>
1924 Match :=
1925 Matching_Actual
1926 (Defining_Unit_Name (Specification (Formal)),
1927 Defining_Unit_Name (Specification (Analyzed_Formal)));
1929 -- If the formal subprogram has the same name as another
1930 -- formal subprogram of the generic, then a named
1931 -- association is illegal (12.3(9)). Exclude named
1932 -- associations that are generated for a nested instance.
1934 if Present (Match)
1935 and then Is_Named_Assoc
1936 and then Comes_From_Source (Found_Assoc)
1937 then
1938 Check_Overloaded_Formal_Subprogram (Formal);
1939 end if;
1941 -- If there is no corresponding actual, this may be case
1942 -- of partial parameterization, or else the formal has a
1943 -- default or a box.
1945 if No (Match) and then Partial_Parameterization then
1946 Process_Default (Formal);
1948 if Nkind (I_Node) = N_Formal_Package_Declaration then
1949 Check_Overloaded_Formal_Subprogram (Formal);
1950 end if;
1952 else
1953 Append_To (Assoc_List,
1954 Instantiate_Formal_Subprogram
1955 (Formal, Match, Analyzed_Formal));
1957 -- If formal subprogram has contracts, create wrappers
1958 -- for it. This is an expansion activity that cannot
1959 -- take place e.g. within an enclosing generic unit.
1961 if Has_Contracts (Analyzed_Formal)
1962 and then (Expander_Active or GNATprove_Mode)
1963 then
1964 Build_Subprogram_Wrappers;
1965 end if;
1967 -- An instantiation is a freeze point for the actuals,
1968 -- unless this is a rewritten formal package.
1970 if Nkind (I_Node) /= N_Formal_Package_Declaration
1971 and then Nkind (Match) = N_Identifier
1972 and then Is_Subprogram (Entity (Match))
1974 -- The actual subprogram may rename a routine defined
1975 -- in Standard. Avoid freezing such renamings because
1976 -- subprograms coming from Standard cannot be frozen.
1978 and then
1979 not Renames_Standard_Subprogram (Entity (Match))
1981 -- If the actual subprogram comes from a different
1982 -- unit, it is already frozen, either by a body in
1983 -- that unit or by the end of the declarative part
1984 -- of the unit. This check avoids the freezing of
1985 -- subprograms defined in Standard which are used
1986 -- as generic actuals.
1988 and then In_Same_Code_Unit (Entity (Match), I_Node)
1989 and then Has_Fully_Defined_Profile (Entity (Match))
1990 then
1991 -- Mark the subprogram as having a delayed freeze
1992 -- since this may be an out-of-order action.
1994 Set_Has_Delayed_Freeze (Entity (Match));
1995 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1996 end if;
1997 end if;
1999 -- If this is a nested generic, preserve default for later
2000 -- instantiations. We do this as well for GNATprove use,
2001 -- so that the list of generic associations is complete.
2003 if No (Match) and then Box_Present (Formal) then
2004 declare
2005 Subp : constant Entity_Id :=
2006 Defining_Unit_Name
2007 (Specification (Last (Assoc_List)));
2009 begin
2010 Append_To (Default_Actuals,
2011 Make_Generic_Association (Sloc (I_Node),
2012 Selector_Name =>
2013 New_Occurrence_Of (Subp, Sloc (I_Node)),
2014 Explicit_Generic_Actual_Parameter =>
2015 New_Occurrence_Of (Subp, Sloc (I_Node))));
2016 end;
2017 end if;
2019 when N_Formal_Package_Declaration =>
2020 -- The name of the formal package may be hidden by the
2021 -- formal parameter itself.
2023 if Error_Posted (Analyzed_Formal) then
2024 Abandon_Instantiation (Instantiation_Node);
2026 else
2027 Match :=
2028 Matching_Actual
2029 (Defining_Identifier (Formal),
2030 Defining_Identifier
2031 (Original_Node (Analyzed_Formal)));
2032 end if;
2034 if No (Match) then
2035 if Partial_Parameterization then
2036 Process_Default (Formal);
2038 else
2039 Error_Msg_Sloc := Sloc (Gen_Unit);
2040 Error_Msg_NE
2041 ("missing actual&",
2042 Instantiation_Node, Defining_Identifier (Formal));
2043 Error_Msg_NE
2044 ("\in instantiation of & declared#",
2045 Instantiation_Node, Gen_Unit);
2047 Abandon_Instantiation (Instantiation_Node);
2048 end if;
2050 else
2051 Analyze (Match);
2052 Append_List
2053 (Instantiate_Formal_Package
2054 (Formal, Match, Analyzed_Formal),
2055 Assoc_List);
2057 -- Determine whether the actual package needs an explicit
2058 -- freeze node. This is only the case if the actual is
2059 -- declared in the same unit and has a body. Normally
2060 -- packages do not have explicit freeze nodes, and gigi
2061 -- only uses them to elaborate entities in a package
2062 -- body.
2064 Explicit_Freeze_Check : declare
2065 Actual : constant Entity_Id := Entity (Match);
2066 Gen_Par : Entity_Id;
2068 Needs_Freezing : Boolean;
2069 P : Node_Id;
2071 procedure Check_Generic_Parent;
2072 -- The actual may be an instantiation of a unit
2073 -- declared in a previous instantiation. If that
2074 -- one is also in the current compilation, it must
2075 -- itself be frozen before the actual. The actual
2076 -- may be an instantiation of a generic child unit,
2077 -- in which case the same applies to the instance
2078 -- of the parent which must be frozen before the
2079 -- actual.
2080 -- Should this itself be recursive ???
2082 --------------------------
2083 -- Check_Generic_Parent --
2084 --------------------------
2086 procedure Check_Generic_Parent is
2087 Inst : constant Node_Id :=
2088 Get_Unit_Instantiation_Node (Actual);
2089 Par : Entity_Id;
2091 begin
2092 Par := Empty;
2094 if Nkind (Parent (Actual)) = N_Package_Specification
2095 then
2096 Par := Scope (Generic_Parent (Parent (Actual)));
2098 if Is_Generic_Instance (Par) then
2099 null;
2101 -- If the actual is a child generic unit, check
2102 -- whether the instantiation of the parent is
2103 -- also local and must also be frozen now. We
2104 -- must retrieve the instance node to locate the
2105 -- parent instance if any.
2107 elsif Ekind (Par) = E_Generic_Package
2108 and then Is_Child_Unit (Gen_Par)
2109 and then Ekind (Scope (Gen_Par)) =
2110 E_Generic_Package
2111 then
2112 if Nkind (Inst) = N_Package_Instantiation
2113 and then Nkind (Name (Inst)) =
2114 N_Expanded_Name
2115 then
2116 -- Retrieve entity of parent instance
2118 Par := Entity (Prefix (Name (Inst)));
2119 end if;
2121 else
2122 Par := Empty;
2123 end if;
2124 end if;
2126 if Present (Par)
2127 and then Is_Generic_Instance (Par)
2128 and then Scope (Par) = Current_Scope
2129 and then
2130 (No (Freeze_Node (Par))
2131 or else
2132 not Is_List_Member (Freeze_Node (Par)))
2133 then
2134 Set_Has_Delayed_Freeze (Par);
2135 Append_Elmt (Par, Actuals_To_Freeze);
2136 end if;
2137 end Check_Generic_Parent;
2139 -- Start of processing for Explicit_Freeze_Check
2141 begin
2142 if Present (Renamed_Entity (Actual)) then
2143 Gen_Par :=
2144 Generic_Parent (Specification
2145 (Unit_Declaration_Node
2146 (Renamed_Entity (Actual))));
2147 else
2148 Gen_Par :=
2149 Generic_Parent (Specification
2150 (Unit_Declaration_Node (Actual)));
2151 end if;
2153 if not Expander_Active
2154 or else not Has_Completion (Actual)
2155 or else not In_Same_Source_Unit (I_Node, Actual)
2156 or else Is_Frozen (Actual)
2157 or else
2158 (Present (Renamed_Entity (Actual))
2159 and then
2160 not In_Same_Source_Unit
2161 (I_Node, (Renamed_Entity (Actual))))
2162 then
2163 null;
2165 else
2166 -- Finally we want to exclude such freeze nodes
2167 -- from statement sequences, which freeze
2168 -- everything before them.
2169 -- Is this strictly necessary ???
2171 Needs_Freezing := True;
2173 P := Parent (I_Node);
2174 while Nkind (P) /= N_Compilation_Unit loop
2175 if Nkind (P) = N_Handled_Sequence_Of_Statements
2176 then
2177 Needs_Freezing := False;
2178 exit;
2179 end if;
2181 P := Parent (P);
2182 end loop;
2184 if Needs_Freezing then
2185 Check_Generic_Parent;
2187 -- If the actual is a renaming of a proper
2188 -- instance of the formal package, indicate
2189 -- that it is the instance that must be frozen.
2191 if Nkind (Parent (Actual)) =
2192 N_Package_Renaming_Declaration
2193 then
2194 Set_Has_Delayed_Freeze
2195 (Renamed_Entity (Actual));
2196 Append_Elmt
2197 (Renamed_Entity (Actual),
2198 Actuals_To_Freeze);
2199 else
2200 Set_Has_Delayed_Freeze (Actual);
2201 Append_Elmt (Actual, Actuals_To_Freeze);
2202 end if;
2203 end if;
2204 end if;
2205 end Explicit_Freeze_Check;
2206 end if;
2208 -- For use type and use package appearing in the generic part,
2209 -- we have already copied them, so we can just move them where
2210 -- they belong (we mustn't recopy them since this would mess up
2211 -- the Sloc values).
2213 when N_Use_Package_Clause
2214 | N_Use_Type_Clause
2216 if Nkind (Original_Node (I_Node)) =
2217 N_Formal_Package_Declaration
2218 then
2219 Append (New_Copy_Tree (Formal), Assoc_List);
2220 else
2221 Remove (Formal);
2222 Append (Formal, Assoc_List);
2223 end if;
2225 when others =>
2226 raise Program_Error;
2227 end case;
2229 -- Check here the correct use of Ghost entities in generic
2230 -- instantiations, as now the generic has been resolved and
2231 -- we know which formal generic parameters are ghost (SPARK
2232 -- RM 6.9(10)).
2234 if Nkind (Formal) not in N_Use_Package_Clause
2235 | N_Use_Type_Clause
2236 then
2237 Check_Ghost_Context_In_Generic_Association
2238 (Actual => Match,
2239 Formal => Defining_Entity (Analyzed_Formal));
2240 end if;
2242 Formal := Saved_Formal;
2243 Next_Non_Pragma (Analyzed_Formal);
2244 end loop;
2246 if Num_Actuals > Num_Matched then
2247 Error_Msg_Sloc := Sloc (Gen_Unit);
2249 if Present (Selector_Name (Actual)) then
2250 Error_Msg_NE
2251 ("unmatched actual &", Actual, Selector_Name (Actual));
2252 Error_Msg_NE
2253 ("\in instantiation of & declared#", Actual, Gen_Unit);
2254 else
2255 Error_Msg_NE
2256 ("unmatched actual in instantiation of & declared#",
2257 Actual, Gen_Unit);
2258 end if;
2259 end if;
2261 elsif Present (Actuals) then
2262 Error_Msg_N
2263 ("too many actuals in generic instantiation", Instantiation_Node);
2264 end if;
2266 -- An instantiation freezes all generic actuals. The only exceptions
2267 -- to this are incomplete types and subprograms which are not fully
2268 -- defined at the point of instantiation.
2270 declare
2271 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2272 begin
2273 while Present (Elmt) loop
2274 Freeze_Before (I_Node, Node (Elmt));
2275 Next_Elmt (Elmt);
2276 end loop;
2277 end;
2279 -- If there are default subprograms, normalize the tree by adding
2280 -- explicit associations for them. This is required if the instance
2281 -- appears within a generic.
2283 if not Is_Empty_List (Default_Actuals) then
2284 declare
2285 Default : Node_Id;
2287 begin
2288 Default := First (Default_Actuals);
2289 while Present (Default) loop
2290 Mark_Rewrite_Insertion (Default);
2291 Next (Default);
2292 end loop;
2294 if No (Actuals) then
2295 Set_Generic_Associations (I_Node, Default_Actuals);
2296 else
2297 Append_List_To (Actuals, Default_Actuals);
2298 end if;
2299 end;
2300 end if;
2302 -- If this is a formal package, normalize the parameter list by adding
2303 -- explicit box associations for the formals that are covered by an
2304 -- Others_Choice.
2306 Append_List (Default_Formals, Formals);
2308 return Assoc_List;
2309 end Analyze_Associations;
2311 -------------------------------
2312 -- Analyze_Formal_Array_Type --
2313 -------------------------------
2315 procedure Analyze_Formal_Array_Type
2316 (T : in out Entity_Id;
2317 Def : Node_Id)
2319 DSS : Node_Id;
2321 begin
2322 -- Treated like a non-generic array declaration, with additional
2323 -- semantic checks.
2325 Enter_Name (T);
2327 if Nkind (Def) = N_Constrained_Array_Definition then
2328 DSS := First (Discrete_Subtype_Definitions (Def));
2329 while Present (DSS) loop
2330 if Nkind (DSS) in N_Subtype_Indication
2331 | N_Range
2332 | N_Attribute_Reference
2333 then
2334 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2335 end if;
2337 Next (DSS);
2338 end loop;
2339 end if;
2341 Array_Type_Declaration (T, Def);
2342 Set_Is_Generic_Type (Base_Type (T));
2344 if Ekind (Component_Type (T)) = E_Incomplete_Type
2345 and then No (Full_View (Component_Type (T)))
2346 then
2347 Error_Msg_N ("premature usage of incomplete type", Def);
2349 -- Check that range constraint is not allowed on the component type
2350 -- of a generic formal array type (AARM 12.5.3(3))
2352 elsif Is_Internal (Component_Type (T))
2353 and then Present (Subtype_Indication (Component_Definition (Def)))
2354 and then Nkind (Original_Node
2355 (Subtype_Indication (Component_Definition (Def)))) =
2356 N_Subtype_Indication
2357 then
2358 Error_Msg_N
2359 ("in a formal, a subtype indication can only be "
2360 & "a subtype mark (RM 12.5.3(3))",
2361 Subtype_Indication (Component_Definition (Def)));
2362 end if;
2364 end Analyze_Formal_Array_Type;
2366 ---------------------------------------------
2367 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2368 ---------------------------------------------
2370 -- As for other generic types, we create a valid type representation with
2371 -- legal but arbitrary attributes, whose values are never considered
2372 -- static. For all scalar types we introduce an anonymous base type, with
2373 -- the same attributes. We choose the corresponding integer type to be
2374 -- Standard_Integer.
2375 -- Here and in other similar routines, the Sloc of the generated internal
2376 -- type must be the same as the sloc of the defining identifier of the
2377 -- formal type declaration, to provide proper source navigation.
2379 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2380 (T : Entity_Id;
2381 Def : Node_Id)
2383 Loc : constant Source_Ptr := Sloc (Def);
2385 Base : constant Entity_Id :=
2386 New_Internal_Entity
2387 (E_Decimal_Fixed_Point_Type,
2388 Current_Scope,
2389 Sloc (Defining_Identifier (Parent (Def))), 'G');
2391 Int_Base : constant Entity_Id := Standard_Integer;
2392 Delta_Val : constant Ureal := Ureal_1;
2393 Digs_Val : constant Uint := Uint_6;
2395 function Make_Dummy_Bound return Node_Id;
2396 -- Return a properly typed universal real literal to use as a bound
2398 ----------------------
2399 -- Make_Dummy_Bound --
2400 ----------------------
2402 function Make_Dummy_Bound return Node_Id is
2403 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2404 begin
2405 Set_Etype (Bound, Universal_Real);
2406 return Bound;
2407 end Make_Dummy_Bound;
2409 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2411 begin
2412 Enter_Name (T);
2414 Set_Etype (Base, Base);
2415 Set_Size_Info (Base, Int_Base);
2416 Set_RM_Size (Base, RM_Size (Int_Base));
2417 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2418 Set_Digits_Value (Base, Digs_Val);
2419 Set_Delta_Value (Base, Delta_Val);
2420 Set_Small_Value (Base, Delta_Val);
2421 Set_Scalar_Range (Base,
2422 Make_Range (Loc,
2423 Low_Bound => Make_Dummy_Bound,
2424 High_Bound => Make_Dummy_Bound));
2426 Set_Is_Generic_Type (Base);
2427 Set_Parent (Base, Parent (Def));
2429 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2430 Set_Etype (T, Base);
2431 Set_Size_Info (T, Int_Base);
2432 Set_RM_Size (T, RM_Size (Int_Base));
2433 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2434 Set_Digits_Value (T, Digs_Val);
2435 Set_Delta_Value (T, Delta_Val);
2436 Set_Small_Value (T, Delta_Val);
2437 Set_Scalar_Range (T, Scalar_Range (Base));
2438 Set_Is_Constrained (T);
2440 Check_Restriction (No_Fixed_Point, Def);
2441 end Analyze_Formal_Decimal_Fixed_Point_Type;
2443 -------------------------------------------
2444 -- Analyze_Formal_Derived_Interface_Type --
2445 -------------------------------------------
2447 procedure Analyze_Formal_Derived_Interface_Type
2448 (N : Node_Id;
2449 T : Entity_Id;
2450 Def : Node_Id)
2452 Loc : constant Source_Ptr := Sloc (Def);
2454 begin
2455 -- Rewrite as a type declaration of a derived type. This ensures that
2456 -- the interface list and primitive operations are properly captured.
2458 Rewrite (N,
2459 Make_Full_Type_Declaration (Loc,
2460 Defining_Identifier => T,
2461 Type_Definition => Def));
2462 Analyze (N);
2463 Set_Is_Generic_Type (T);
2464 end Analyze_Formal_Derived_Interface_Type;
2466 ---------------------------------
2467 -- Analyze_Formal_Derived_Type --
2468 ---------------------------------
2470 procedure Analyze_Formal_Derived_Type
2471 (N : Node_Id;
2472 T : Entity_Id;
2473 Def : Node_Id)
2475 Loc : constant Source_Ptr := Sloc (Def);
2476 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2477 New_N : Node_Id;
2479 begin
2480 Set_Is_Generic_Type (T);
2482 if Private_Present (Def) then
2483 New_N :=
2484 Make_Private_Extension_Declaration (Loc,
2485 Defining_Identifier => T,
2486 Discriminant_Specifications => Discriminant_Specifications (N),
2487 Unknown_Discriminants_Present => Unk_Disc,
2488 Subtype_Indication => Subtype_Mark (Def),
2489 Interface_List => Interface_List (Def));
2491 Set_Abstract_Present (New_N, Abstract_Present (Def));
2492 Set_Limited_Present (New_N, Limited_Present (Def));
2493 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2495 else
2496 New_N :=
2497 Make_Full_Type_Declaration (Loc,
2498 Defining_Identifier => T,
2499 Discriminant_Specifications =>
2500 Discriminant_Specifications (Parent (T)),
2501 Type_Definition =>
2502 Make_Derived_Type_Definition (Loc,
2503 Subtype_Indication => Subtype_Mark (Def)));
2505 Set_Abstract_Present
2506 (Type_Definition (New_N), Abstract_Present (Def));
2507 Set_Limited_Present
2508 (Type_Definition (New_N), Limited_Present (Def));
2509 end if;
2511 Rewrite (N, New_N);
2512 Analyze (N);
2514 if Unk_Disc then
2515 if not Is_Composite_Type (T) then
2516 Error_Msg_N
2517 ("unknown discriminants not allowed for elementary types", N);
2518 else
2519 Set_Has_Unknown_Discriminants (T);
2520 Set_Is_Constrained (T, False);
2521 end if;
2522 end if;
2524 -- If the parent type has a known size, so does the formal, which makes
2525 -- legal representation clauses that involve the formal.
2527 Set_Size_Known_At_Compile_Time
2528 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2529 end Analyze_Formal_Derived_Type;
2531 ----------------------------------
2532 -- Analyze_Formal_Discrete_Type --
2533 ----------------------------------
2535 -- The operations defined for a discrete types are those of an enumeration
2536 -- type. The size is set to an arbitrary value, for use in analyzing the
2537 -- generic unit.
2539 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2540 Loc : constant Source_Ptr := Sloc (Def);
2541 Lo : Node_Id;
2542 Hi : Node_Id;
2544 Base : constant Entity_Id :=
2545 New_Internal_Entity
2546 (E_Floating_Point_Type, Current_Scope,
2547 Sloc (Defining_Identifier (Parent (Def))), 'G');
2549 begin
2550 Enter_Name (T);
2551 Mutate_Ekind (T, E_Enumeration_Subtype);
2552 Set_Etype (T, Base);
2553 Init_Size (T, 8);
2554 Reinit_Alignment (T);
2555 Set_Is_Generic_Type (T);
2556 Set_Is_Constrained (T);
2558 -- For semantic analysis, the bounds of the type must be set to some
2559 -- non-static value. The simplest is to create attribute nodes for those
2560 -- bounds, that refer to the type itself. These bounds are never
2561 -- analyzed but serve as place-holders.
2563 Lo :=
2564 Make_Attribute_Reference (Loc,
2565 Attribute_Name => Name_First,
2566 Prefix => New_Occurrence_Of (T, Loc));
2567 Set_Etype (Lo, T);
2569 Hi :=
2570 Make_Attribute_Reference (Loc,
2571 Attribute_Name => Name_Last,
2572 Prefix => New_Occurrence_Of (T, Loc));
2573 Set_Etype (Hi, T);
2575 Set_Scalar_Range (T,
2576 Make_Range (Loc,
2577 Low_Bound => Lo,
2578 High_Bound => Hi));
2580 Mutate_Ekind (Base, E_Enumeration_Type);
2581 Set_Etype (Base, Base);
2582 Init_Size (Base, 8);
2583 Reinit_Alignment (Base);
2584 Set_Is_Generic_Type (Base);
2585 Set_Scalar_Range (Base, Scalar_Range (T));
2586 Set_Parent (Base, Parent (Def));
2587 end Analyze_Formal_Discrete_Type;
2589 ----------------------------------
2590 -- Analyze_Formal_Floating_Type --
2591 ---------------------------------
2593 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2594 Base : constant Entity_Id :=
2595 New_Internal_Entity
2596 (E_Floating_Point_Type, Current_Scope,
2597 Sloc (Defining_Identifier (Parent (Def))), 'G');
2599 begin
2600 -- The various semantic attributes are taken from the predefined type
2601 -- Float, just so that all of them are initialized. Their values are
2602 -- never used because no constant folding or expansion takes place in
2603 -- the generic itself.
2605 Enter_Name (T);
2606 Mutate_Ekind (T, E_Floating_Point_Subtype);
2607 Set_Etype (T, Base);
2608 Set_Size_Info (T, (Standard_Float));
2609 Set_RM_Size (T, RM_Size (Standard_Float));
2610 Set_Digits_Value (T, Digits_Value (Standard_Float));
2611 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2612 Set_Is_Constrained (T);
2614 Set_Is_Generic_Type (Base);
2615 Set_Etype (Base, Base);
2616 Set_Size_Info (Base, (Standard_Float));
2617 Set_RM_Size (Base, RM_Size (Standard_Float));
2618 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2619 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2620 Set_Parent (Base, Parent (Def));
2622 Check_Restriction (No_Floating_Point, Def);
2623 end Analyze_Formal_Floating_Type;
2625 -----------------------------------
2626 -- Analyze_Formal_Interface_Type;--
2627 -----------------------------------
2629 procedure Analyze_Formal_Interface_Type
2630 (N : Node_Id;
2631 T : Entity_Id;
2632 Def : Node_Id)
2634 Loc : constant Source_Ptr := Sloc (N);
2635 New_N : Node_Id;
2637 begin
2638 New_N :=
2639 Make_Full_Type_Declaration (Loc,
2640 Defining_Identifier => T,
2641 Type_Definition => Def);
2643 Rewrite (N, New_N);
2644 Analyze (N);
2645 Set_Is_Generic_Type (T);
2646 end Analyze_Formal_Interface_Type;
2648 ---------------------------------
2649 -- Analyze_Formal_Modular_Type --
2650 ---------------------------------
2652 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2653 begin
2654 -- Apart from their entity kind, generic modular types are treated like
2655 -- signed integer types, and have the same attributes.
2657 Analyze_Formal_Signed_Integer_Type (T, Def);
2658 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2659 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2661 end Analyze_Formal_Modular_Type;
2663 ---------------------------------------
2664 -- Analyze_Formal_Object_Declaration --
2665 ---------------------------------------
2667 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2668 E : constant Node_Id := Default_Expression (N);
2669 Id : constant Node_Id := Defining_Identifier (N);
2671 K : Entity_Kind;
2672 Parent_Installed : Boolean := False;
2673 T : Node_Id;
2675 begin
2676 Enter_Name (Id);
2678 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2680 -- Determine the mode of the formal object
2682 if Out_Present (N) then
2683 K := E_Generic_In_Out_Parameter;
2685 if not In_Present (N) then
2686 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2687 end if;
2689 else
2690 K := E_Generic_In_Parameter;
2691 end if;
2693 if Present (Subtype_Mark (N)) then
2694 Find_Type (Subtype_Mark (N));
2695 T := Entity (Subtype_Mark (N));
2697 -- Verify that there is no redundant null exclusion
2699 if Null_Exclusion_Present (N) then
2700 if not Is_Access_Type (T) then
2701 Error_Msg_N
2702 ("null exclusion can only apply to an access type", N);
2704 elsif Can_Never_Be_Null (T) then
2705 Error_Msg_NE
2706 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2707 end if;
2708 end if;
2710 -- Ada 2005 (AI-423): Formal object with an access definition
2712 else
2713 Check_Access_Definition (N);
2714 T := Access_Definition
2715 (Related_Nod => N,
2716 N => Access_Definition (N));
2717 end if;
2719 if Ekind (T) = E_Incomplete_Type then
2720 declare
2721 Error_Node : Node_Id;
2723 begin
2724 if Present (Subtype_Mark (N)) then
2725 Error_Node := Subtype_Mark (N);
2726 else
2727 Check_Access_Definition (N);
2728 Error_Node := Access_Definition (N);
2729 end if;
2731 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2732 end;
2733 end if;
2735 if K = E_Generic_In_Parameter then
2737 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2739 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2740 Error_Msg_N
2741 ("generic formal of mode IN must not be of limited type", N);
2742 Explain_Limited_Type (T, N);
2743 end if;
2745 if Is_Abstract_Type (T) then
2746 Error_Msg_N
2747 ("generic formal of mode IN must not be of abstract type", N);
2748 end if;
2750 if Present (E) then
2751 Preanalyze_Spec_Expression (E, T);
2753 -- The default for a ghost generic formal IN parameter of
2754 -- access-to-variable type should be a ghost object (SPARK
2755 -- RM 6.9(13)).
2757 if Is_Access_Variable (T) then
2758 Check_Ghost_Formal_Variable
2759 (Actual => E,
2760 Formal => Id,
2761 Is_Default => True);
2762 end if;
2764 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2765 Error_Msg_N
2766 ("initialization not allowed for limited types", E);
2767 Explain_Limited_Type (T, E);
2768 end if;
2769 end if;
2771 Mutate_Ekind (Id, K);
2772 Set_Etype (Id, T);
2774 -- Case of generic IN OUT parameter
2776 else
2777 -- If the formal has an unconstrained type, construct its actual
2778 -- subtype, as is done for subprogram formals. In this fashion, all
2779 -- its uses can refer to specific bounds.
2781 Mutate_Ekind (Id, K);
2782 Set_Etype (Id, T);
2784 if (Is_Array_Type (T) and then not Is_Constrained (T))
2785 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2786 then
2787 declare
2788 Non_Freezing_Ref : constant Node_Id :=
2789 New_Occurrence_Of (Id, Sloc (Id));
2790 Decl : Node_Id;
2792 begin
2793 -- Make sure the actual subtype doesn't generate bogus freezing
2795 Set_Must_Not_Freeze (Non_Freezing_Ref);
2796 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2797 Insert_Before_And_Analyze (N, Decl);
2798 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2799 end;
2800 else
2801 Set_Actual_Subtype (Id, T);
2802 end if;
2804 if Present (E) then
2805 Error_Msg_N
2806 ("initialization not allowed for `IN OUT` formals", N);
2807 end if;
2808 end if;
2810 if Has_Aspects (N) then
2811 Analyze_Aspect_Specifications (N, Id);
2812 end if;
2814 if Parent_Installed then
2815 Remove_Parent;
2816 end if;
2817 end Analyze_Formal_Object_Declaration;
2819 ----------------------------------------------
2820 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2821 ----------------------------------------------
2823 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2824 (T : Entity_Id;
2825 Def : Node_Id)
2827 Loc : constant Source_Ptr := Sloc (Def);
2828 Base : constant Entity_Id :=
2829 New_Internal_Entity
2830 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2831 Sloc (Defining_Identifier (Parent (Def))), 'G');
2833 begin
2834 -- The semantic attributes are set for completeness only, their values
2835 -- will never be used, since all properties of the type are non-static.
2837 Enter_Name (T);
2838 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2839 Set_Etype (T, Base);
2840 Set_Size_Info (T, Standard_Integer);
2841 Set_RM_Size (T, RM_Size (Standard_Integer));
2842 Set_Small_Value (T, Ureal_1);
2843 Set_Delta_Value (T, Ureal_1);
2844 Set_Scalar_Range (T,
2845 Make_Range (Loc,
2846 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2847 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2848 Set_Is_Constrained (T);
2850 Set_Is_Generic_Type (Base);
2851 Set_Etype (Base, Base);
2852 Set_Size_Info (Base, Standard_Integer);
2853 Set_RM_Size (Base, RM_Size (Standard_Integer));
2854 Set_Small_Value (Base, Ureal_1);
2855 Set_Delta_Value (Base, Ureal_1);
2856 Set_Scalar_Range (Base, Scalar_Range (T));
2857 Set_Parent (Base, Parent (Def));
2859 Check_Restriction (No_Fixed_Point, Def);
2860 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2862 ----------------------------------------
2863 -- Analyze_Formal_Package_Declaration --
2864 ----------------------------------------
2866 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2867 Gen_Id : constant Node_Id := Name (N);
2868 Loc : constant Source_Ptr := Sloc (N);
2869 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2870 Formal : Entity_Id;
2871 Gen_Decl : Node_Id;
2872 Gen_Unit : Entity_Id;
2873 Renaming : Node_Id;
2875 Vis_Prims_List : Elist_Id := No_Elist;
2876 -- List of primitives made temporarily visible in the instantiation
2877 -- to match the visibility of the formal type.
2879 function Build_Local_Package return Node_Id;
2880 -- The formal package is rewritten so that its parameters are replaced
2881 -- with corresponding declarations. For parameters with bona fide
2882 -- associations these declarations are created by Analyze_Associations
2883 -- as for a regular instantiation. For boxed parameters, we preserve
2884 -- the formal declarations and analyze them, in order to introduce
2885 -- entities of the right kind in the environment of the formal.
2887 -------------------------
2888 -- Build_Local_Package --
2889 -------------------------
2891 function Build_Local_Package return Node_Id is
2892 Decls : List_Id;
2893 Pack_Decl : Node_Id;
2895 begin
2896 -- Within the formal, the name of the generic package is a renaming
2897 -- of the formal (as for a regular instantiation).
2899 Pack_Decl :=
2900 Make_Package_Declaration (Loc,
2901 Specification =>
2902 Copy_Generic_Node
2903 (Specification (Original_Node (Gen_Decl)),
2904 Empty, Instantiating => True));
2906 Renaming :=
2907 Make_Package_Renaming_Declaration (Loc,
2908 Defining_Unit_Name =>
2909 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2910 Name => New_Occurrence_Of (Formal, Loc));
2912 if Nkind (Gen_Id) = N_Identifier
2913 and then Chars (Gen_Id) = Chars (Pack_Id)
2914 then
2915 Error_Msg_NE
2916 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2917 end if;
2919 -- If the formal is declared with a box, or with an others choice,
2920 -- create corresponding declarations for all entities in the formal
2921 -- part, so that names with the proper types are available in the
2922 -- specification of the formal package.
2924 -- On the other hand, if there are no associations, then all the
2925 -- formals must have defaults, and this will be checked by the
2926 -- call to Analyze_Associations.
2928 if Box_Present (N)
2929 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2930 then
2931 declare
2932 Formal_Decl : Node_Id;
2934 begin
2935 -- TBA : for a formal package, need to recurse ???
2937 Decls := New_List;
2938 Formal_Decl :=
2939 First
2940 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2941 while Present (Formal_Decl) loop
2942 Append_To
2943 (Decls,
2944 Copy_Generic_Node
2945 (Formal_Decl, Empty, Instantiating => True));
2946 Next (Formal_Decl);
2947 end loop;
2948 end;
2950 -- If generic associations are present, use Analyze_Associations to
2951 -- create the proper renaming declarations.
2953 else
2954 declare
2955 Act_Tree : constant Node_Id :=
2956 Copy_Generic_Node
2957 (Original_Node (Gen_Decl), Empty,
2958 Instantiating => True);
2960 begin
2961 Generic_Renamings.Set_Last (0);
2962 Generic_Renamings_HTable.Reset;
2963 Instantiation_Node := N;
2965 Decls :=
2966 Analyze_Associations
2967 (I_Node => Original_Node (N),
2968 Formals => Generic_Formal_Declarations (Act_Tree),
2969 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2971 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2972 end;
2973 end if;
2975 Append (Renaming, To => Decls);
2977 -- Add generated declarations ahead of local declarations in
2978 -- the package.
2980 if No (Visible_Declarations (Specification (Pack_Decl))) then
2981 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2982 else
2983 Insert_List_Before
2984 (First (Visible_Declarations (Specification (Pack_Decl))),
2985 Decls);
2986 end if;
2988 return Pack_Decl;
2989 end Build_Local_Package;
2991 -- Local variables
2993 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2994 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2996 Associations : Boolean := True;
2997 New_N : Node_Id;
2998 Parent_Installed : Boolean := False;
2999 Parent_Instance : Entity_Id;
3000 Renaming_In_Par : Entity_Id;
3002 -- Start of processing for Analyze_Formal_Package_Declaration
3004 begin
3005 Check_Text_IO_Special_Unit (Gen_Id);
3007 Init_Env;
3008 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3009 Gen_Unit := Entity (Gen_Id);
3011 -- Check for a formal package that is a package renaming
3013 if Present (Renamed_Entity (Gen_Unit)) then
3015 -- Indicate that unit is used, before replacing it with renamed
3016 -- entity for use below.
3018 if In_Extended_Main_Source_Unit (N) then
3019 Set_Is_Instantiated (Gen_Unit);
3020 Generate_Reference (Gen_Unit, N);
3021 end if;
3023 Gen_Unit := Renamed_Entity (Gen_Unit);
3024 end if;
3026 if Ekind (Gen_Unit) /= E_Generic_Package then
3027 Error_Msg_N ("expect generic package name", Gen_Id);
3028 Restore_Env;
3029 goto Leave;
3031 elsif Gen_Unit = Current_Scope then
3032 Error_Msg_N
3033 ("generic package cannot be used as a formal package of itself",
3034 Gen_Id);
3035 Restore_Env;
3036 goto Leave;
3038 elsif In_Open_Scopes (Gen_Unit) then
3039 if Is_Compilation_Unit (Gen_Unit)
3040 and then Is_Child_Unit (Current_Scope)
3041 then
3042 -- Special-case the error when the formal is a parent, and
3043 -- continue analysis to minimize cascaded errors.
3045 Error_Msg_N
3046 ("generic parent cannot be used as formal package of a child "
3047 & "unit", Gen_Id);
3049 else
3050 Error_Msg_N
3051 ("generic package cannot be used as a formal package within "
3052 & "itself", Gen_Id);
3053 Restore_Env;
3054 goto Leave;
3055 end if;
3056 end if;
3058 -- Check that name of formal package does not hide name of generic,
3059 -- or its leading prefix. This check must be done separately because
3060 -- the name of the generic has already been analyzed.
3062 declare
3063 Gen_Name : Entity_Id;
3065 begin
3066 Gen_Name := Gen_Id;
3067 while Nkind (Gen_Name) = N_Expanded_Name loop
3068 Gen_Name := Prefix (Gen_Name);
3069 end loop;
3071 if Chars (Gen_Name) = Chars (Pack_Id) then
3072 Error_Msg_NE
3073 ("& is hidden within declaration of formal package",
3074 Gen_Id, Gen_Name);
3075 end if;
3076 end;
3078 if Box_Present (N)
3079 or else No (Generic_Associations (N))
3080 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3081 then
3082 Associations := False;
3083 end if;
3085 -- If there are no generic associations, the generic parameters appear
3086 -- as local entities and are instantiated like them. We copy the generic
3087 -- package declaration as if it were an instantiation, and analyze it
3088 -- like a regular package, except that we treat the formals as
3089 -- additional visible components.
3091 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3093 if In_Extended_Main_Source_Unit (N) then
3094 Set_Is_Instantiated (Gen_Unit);
3095 Generate_Reference (Gen_Unit, N);
3096 end if;
3098 Formal := New_Copy (Pack_Id);
3099 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3101 -- Make local generic without formals. The formals will be replaced with
3102 -- internal declarations.
3104 begin
3105 New_N := Build_Local_Package;
3107 -- If there are errors in the parameter list, Analyze_Associations
3108 -- raises Instantiation_Error. Patch the declaration to prevent further
3109 -- exception propagation.
3111 exception
3112 when Instantiation_Error =>
3113 Enter_Name (Formal);
3114 Mutate_Ekind (Formal, E_Variable);
3115 Set_Etype (Formal, Any_Type);
3116 Restore_Hidden_Primitives (Vis_Prims_List);
3118 if Parent_Installed then
3119 Remove_Parent;
3120 end if;
3122 goto Leave;
3123 end;
3125 Rewrite (N, New_N);
3126 Set_Defining_Unit_Name (Specification (New_N), Formal);
3127 Set_Generic_Parent (Specification (N), Gen_Unit);
3128 Set_Instance_Env (Gen_Unit, Formal);
3129 Set_Is_Generic_Instance (Formal);
3131 Enter_Name (Formal);
3132 Mutate_Ekind (Formal, E_Package);
3133 Set_Etype (Formal, Standard_Void_Type);
3134 Set_Inner_Instances (Formal, New_Elmt_List);
3136 -- It is unclear that any aspects can apply to a formal package
3137 -- declaration, given that they look like a hidden conformance
3138 -- requirement on the corresponding actual. However, Abstract_State
3139 -- must be treated specially because it generates declarations that
3140 -- must appear before other declarations in the specification and
3141 -- must be analyzed at once.
3143 if Present (Aspect_Specifications (Gen_Decl)) then
3144 if No (Aspect_Specifications (N)) then
3145 Set_Aspect_Specifications (N, New_List);
3146 end if;
3148 declare
3149 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3150 New_A : Node_Id;
3152 begin
3153 while Present (ASN) loop
3154 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3155 New_A :=
3156 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3157 Set_Entity (New_A, Formal);
3158 Set_Analyzed (New_A, False);
3159 Append (New_A, Aspect_Specifications (N));
3160 Analyze_Aspect_Specifications (N, Formal);
3161 exit;
3162 end if;
3164 Next (ASN);
3165 end loop;
3166 end;
3167 end if;
3169 Push_Scope (Formal);
3171 -- Manually set the SPARK_Mode from the context because the package
3172 -- declaration is never analyzed.
3174 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3175 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3176 Set_SPARK_Pragma_Inherited (Formal);
3177 Set_SPARK_Aux_Pragma_Inherited (Formal);
3179 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3181 -- Similarly, we have to make the name of the formal visible in the
3182 -- parent instance, to resolve properly fully qualified names that
3183 -- may appear in the generic unit. The parent instance has been
3184 -- placed on the scope stack ahead of the current scope.
3186 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3188 Renaming_In_Par :=
3189 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3190 Mutate_Ekind (Renaming_In_Par, E_Package);
3191 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3192 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3193 Set_Scope (Renaming_In_Par, Parent_Instance);
3194 Set_Parent (Renaming_In_Par, Parent (Formal));
3195 Set_Renamed_Entity (Renaming_In_Par, Formal);
3196 Append_Entity (Renaming_In_Par, Parent_Instance);
3197 end if;
3199 -- A formal package declaration behaves as a package instantiation with
3200 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3201 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3202 -- all SPARK_Mode pragmas within the generic_package_name.
3204 if SPARK_Mode /= On then
3205 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3207 -- Mark the formal spec in case the body is instantiated at a later
3208 -- pass. This preserves the original context in effect for the body.
3210 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3211 end if;
3213 Analyze (Specification (N));
3215 -- The formals for which associations are provided are not visible
3216 -- outside of the formal package. The others are still declared by a
3217 -- formal parameter declaration.
3219 -- If there are no associations, the only local entity to hide is the
3220 -- generated package renaming itself.
3222 declare
3223 E : Entity_Id;
3225 begin
3226 E := First_Entity (Formal);
3227 while Present (E) loop
3228 if Associations and then not Is_Generic_Formal (E) then
3229 Set_Is_Hidden (E);
3230 end if;
3232 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3233 Set_Is_Hidden (E);
3234 exit;
3235 end if;
3237 Next_Entity (E);
3238 end loop;
3239 end;
3241 End_Package_Scope (Formal);
3242 Restore_Hidden_Primitives (Vis_Prims_List);
3244 if Parent_Installed then
3245 Remove_Parent;
3246 end if;
3248 Restore_Env;
3250 -- Inside the generic unit, the formal package is a regular package, but
3251 -- no body is needed for it. Note that after instantiation, the defining
3252 -- unit name we need is in the new tree and not in the original (see
3253 -- Package_Instantiation). A generic formal package is an instance, and
3254 -- can be used as an actual for an inner instance.
3256 Set_Has_Completion (Formal, True);
3258 -- Add semantic information to the original defining identifier.
3260 Mutate_Ekind (Pack_Id, E_Package);
3261 Set_Etype (Pack_Id, Standard_Void_Type);
3262 Set_Scope (Pack_Id, Scope (Formal));
3263 Set_Has_Completion (Pack_Id, True);
3265 <<Leave>>
3266 if Has_Aspects (N) then
3267 -- Unclear that any other aspects may appear here, analyze them
3268 -- for completion, given that the grammar allows their appearance.
3270 Analyze_Aspect_Specifications (N, Pack_Id);
3271 end if;
3273 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3274 end Analyze_Formal_Package_Declaration;
3276 ---------------------------------
3277 -- Analyze_Formal_Private_Type --
3278 ---------------------------------
3280 procedure Analyze_Formal_Private_Type
3281 (N : Node_Id;
3282 T : Entity_Id;
3283 Def : Node_Id)
3285 begin
3286 New_Private_Type (N, T, Def);
3288 -- Set the size to an arbitrary but legal value
3290 Set_Size_Info (T, Standard_Integer);
3291 Set_RM_Size (T, RM_Size (Standard_Integer));
3292 end Analyze_Formal_Private_Type;
3294 ------------------------------------
3295 -- Analyze_Formal_Incomplete_Type --
3296 ------------------------------------
3298 procedure Analyze_Formal_Incomplete_Type
3299 (T : Entity_Id;
3300 Def : Node_Id)
3302 begin
3303 Enter_Name (T);
3304 Mutate_Ekind (T, E_Incomplete_Type);
3305 Set_Etype (T, T);
3306 Set_Private_Dependents (T, New_Elmt_List);
3308 if Tagged_Present (Def) then
3309 Set_Is_Tagged_Type (T);
3310 Make_Class_Wide_Type (T);
3311 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3312 end if;
3313 end Analyze_Formal_Incomplete_Type;
3315 ----------------------------------------
3316 -- Analyze_Formal_Signed_Integer_Type --
3317 ----------------------------------------
3319 procedure Analyze_Formal_Signed_Integer_Type
3320 (T : Entity_Id;
3321 Def : Node_Id)
3323 Base : constant Entity_Id :=
3324 New_Internal_Entity
3325 (E_Signed_Integer_Type,
3326 Current_Scope,
3327 Sloc (Defining_Identifier (Parent (Def))), 'G');
3329 begin
3330 Enter_Name (T);
3332 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3333 Set_Etype (T, Base);
3334 Set_Size_Info (T, Standard_Integer);
3335 Set_RM_Size (T, RM_Size (Standard_Integer));
3336 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3337 Set_Is_Constrained (T);
3339 Set_Is_Generic_Type (Base);
3340 Set_Size_Info (Base, Standard_Integer);
3341 Set_RM_Size (Base, RM_Size (Standard_Integer));
3342 Set_Etype (Base, Base);
3343 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3344 Set_Parent (Base, Parent (Def));
3345 end Analyze_Formal_Signed_Integer_Type;
3347 -------------------------------------------
3348 -- Analyze_Formal_Subprogram_Declaration --
3349 -------------------------------------------
3351 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3352 Spec : constant Node_Id := Specification (N);
3353 Def : constant Node_Id := Default_Name (N);
3354 Expr : constant Node_Id := Expression (N);
3355 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3357 Parent_Installed : Boolean := False;
3358 Subp : Entity_Id;
3360 begin
3361 if Nam = Error then
3362 return;
3363 end if;
3365 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3366 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3367 goto Leave;
3368 end if;
3370 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3372 Analyze_Subprogram_Declaration (N);
3373 Set_Is_Formal_Subprogram (Nam);
3374 Set_Has_Completion (Nam);
3376 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3377 Set_Is_Abstract_Subprogram (Nam);
3379 Set_Is_Dispatching_Operation (Nam);
3381 -- A formal abstract procedure cannot have a null default
3382 -- (RM 12.6(4.1/2)).
3384 if Nkind (Spec) = N_Procedure_Specification
3385 and then Null_Present (Spec)
3386 then
3387 Error_Msg_N
3388 ("a formal abstract subprogram cannot default to null", Spec);
3389 end if;
3391 -- A formal abstract function cannot have an expression default
3392 -- (expression defaults are allowed for nonabstract formal functions
3393 -- when extensions are enabled).
3395 if Nkind (Spec) = N_Function_Specification
3396 and then Present (Expr)
3397 then
3398 Error_Msg_N
3399 ("a formal abstract subprogram cannot default to an expression",
3400 Spec);
3401 end if;
3403 declare
3404 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3405 begin
3406 if No (Ctrl_Type) then
3407 Error_Msg_N
3408 ("abstract formal subprogram must have a controlling type",
3411 elsif Ada_Version >= Ada_2012
3412 and then Is_Incomplete_Type (Ctrl_Type)
3413 then
3414 Error_Msg_NE
3415 ("controlling type of abstract formal subprogram cannot "
3416 & "be incomplete type", N, Ctrl_Type);
3418 else
3419 Check_Controlling_Formals (Ctrl_Type, Nam);
3420 end if;
3421 end;
3422 end if;
3424 -- Default name is resolved at the point of instantiation
3426 if Box_Present (N) then
3427 null;
3429 -- Default name is bound at the point of generic declaration
3431 elsif Present (Def) then
3432 if Nkind (Def) = N_Operator_Symbol then
3433 Find_Direct_Name (Def);
3435 elsif Nkind (Def) /= N_Attribute_Reference then
3436 Analyze (Def);
3438 else
3439 -- For an attribute reference, analyze the prefix and verify
3440 -- that it has the proper profile for the subprogram.
3442 Analyze (Prefix (Def));
3443 Valid_Default_Attribute (Nam, Def);
3444 goto Leave;
3445 end if;
3447 -- The default for a ghost generic formal procedure should be a ghost
3448 -- procedure (SPARK RM 6.9(13)).
3450 if Ekind (Nam) = E_Procedure then
3451 declare
3452 Def_E : Entity_Id := Empty;
3453 begin
3454 if Nkind (Def) in N_Has_Entity then
3455 Def_E := Entity (Def);
3456 end if;
3458 Check_Ghost_Formal_Procedure_Or_Package
3459 (N => Def,
3460 Actual => Def_E,
3461 Formal => Nam,
3462 Is_Default => True);
3463 end;
3464 end if;
3466 -- Default name may be overloaded, in which case the interpretation
3467 -- with the correct profile must be selected, as for a renaming.
3468 -- If the definition is an indexed component, it must denote a
3469 -- member of an entry family. If it is a selected component, it
3470 -- can be a protected operation.
3472 if Etype (Def) = Any_Type then
3473 goto Leave;
3475 elsif Nkind (Def) = N_Selected_Component then
3476 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3477 Error_Msg_N ("expect valid subprogram name as default", Def);
3478 end if;
3480 elsif Nkind (Def) = N_Indexed_Component then
3481 if Is_Entity_Name (Prefix (Def)) then
3482 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3483 Error_Msg_N ("expect valid subprogram name as default", Def);
3484 end if;
3486 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3487 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3488 E_Entry_Family
3489 then
3490 Error_Msg_N ("expect valid subprogram name as default", Def);
3491 end if;
3493 else
3494 Error_Msg_N ("expect valid subprogram name as default", Def);
3495 goto Leave;
3496 end if;
3498 elsif Nkind (Def) = N_Character_Literal then
3500 -- Needs some type checks: subprogram should be parameterless???
3502 Resolve (Def, (Etype (Nam)));
3504 elsif not Is_Entity_Name (Def)
3505 or else not Is_Overloadable (Entity (Def))
3506 then
3507 Error_Msg_N ("expect valid subprogram name as default", Def);
3508 goto Leave;
3510 elsif not Is_Overloaded (Def) then
3511 Subp := Entity (Def);
3513 if Subp = Nam then
3514 Error_Msg_N ("premature usage of formal subprogram", Def);
3516 elsif not Entity_Matches_Spec (Subp, Nam) then
3517 Error_Msg_N ("no visible entity matches specification", Def);
3518 end if;
3520 -- More than one interpretation, so disambiguate as for a renaming
3522 else
3523 declare
3524 I : Interp_Index;
3525 I1 : Interp_Index := 0;
3526 It : Interp;
3527 It1 : Interp;
3529 begin
3530 Subp := Any_Id;
3531 Get_First_Interp (Def, I, It);
3532 while Present (It.Nam) loop
3533 if Entity_Matches_Spec (It.Nam, Nam) then
3534 if Subp /= Any_Id then
3535 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3537 if It1 = No_Interp then
3538 Error_Msg_N ("ambiguous default subprogram", Def);
3539 else
3540 Subp := It1.Nam;
3541 end if;
3543 exit;
3545 else
3546 I1 := I;
3547 Subp := It.Nam;
3548 end if;
3549 end if;
3551 Get_Next_Interp (I, It);
3552 end loop;
3553 end;
3555 if Subp /= Any_Id then
3557 -- Subprogram found, generate reference to it
3559 Set_Entity (Def, Subp);
3560 Generate_Reference (Subp, Def);
3562 if Subp = Nam then
3563 Error_Msg_N ("premature usage of formal subprogram", Def);
3565 elsif Ekind (Subp) /= E_Operator then
3566 Check_Mode_Conformant (Subp, Nam);
3567 end if;
3569 else
3570 Error_Msg_N ("no visible subprogram matches specification", N);
3571 end if;
3572 end if;
3574 -- When extensions are enabled, an expression can be given as default
3575 -- for a formal function. The expression must be of the function result
3576 -- type and can reference formal parameters of the function.
3578 elsif Present (Expr) then
3579 Push_Scope (Nam);
3580 Install_Formals (Nam);
3581 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3582 End_Scope;
3583 end if;
3585 <<Leave>>
3586 if Has_Aspects (N) then
3587 Analyze_Aspect_Specifications (N, Nam);
3588 end if;
3590 if Parent_Installed then
3591 Remove_Parent;
3592 end if;
3593 end Analyze_Formal_Subprogram_Declaration;
3595 -------------------------------------
3596 -- Analyze_Formal_Type_Declaration --
3597 -------------------------------------
3599 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3600 Def : constant Node_Id := Formal_Type_Definition (N);
3602 Parent_Installed : Boolean := False;
3603 T : Entity_Id;
3605 begin
3606 T := Defining_Identifier (N);
3608 if Present (Discriminant_Specifications (N))
3609 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3610 then
3611 Error_Msg_N
3612 ("discriminants not allowed for this formal type", T);
3613 end if;
3615 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3617 -- Enter the new name, and branch to specific routine
3619 case Nkind (Def) is
3620 when N_Formal_Private_Type_Definition =>
3621 Analyze_Formal_Private_Type (N, T, Def);
3623 when N_Formal_Derived_Type_Definition =>
3624 Analyze_Formal_Derived_Type (N, T, Def);
3626 when N_Formal_Incomplete_Type_Definition =>
3627 Analyze_Formal_Incomplete_Type (T, Def);
3629 when N_Formal_Discrete_Type_Definition =>
3630 Analyze_Formal_Discrete_Type (T, Def);
3632 when N_Formal_Signed_Integer_Type_Definition =>
3633 Analyze_Formal_Signed_Integer_Type (T, Def);
3635 when N_Formal_Modular_Type_Definition =>
3636 Analyze_Formal_Modular_Type (T, Def);
3638 when N_Formal_Floating_Point_Definition =>
3639 Analyze_Formal_Floating_Type (T, Def);
3641 when N_Formal_Ordinary_Fixed_Point_Definition =>
3642 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3644 when N_Formal_Decimal_Fixed_Point_Definition =>
3645 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3647 when N_Array_Type_Definition =>
3648 Analyze_Formal_Array_Type (T, Def);
3650 when N_Access_Function_Definition
3651 | N_Access_Procedure_Definition
3652 | N_Access_To_Object_Definition
3654 Analyze_Generic_Access_Type (T, Def);
3656 -- Ada 2005: a interface declaration is encoded as an abstract
3657 -- record declaration or a abstract type derivation.
3659 when N_Record_Definition =>
3660 Analyze_Formal_Interface_Type (N, T, Def);
3662 when N_Derived_Type_Definition =>
3663 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3665 when N_Error =>
3666 null;
3668 when others =>
3669 raise Program_Error;
3670 end case;
3672 -- A formal type declaration declares a type and its first
3673 -- subtype.
3675 Set_Is_Generic_Type (T);
3676 Set_Is_First_Subtype (T);
3678 if Present (Default_Subtype_Mark (Original_Node (N))) then
3679 Validate_Formal_Type_Default (N);
3680 end if;
3682 if Has_Aspects (N) then
3683 Analyze_Aspect_Specifications (N, T);
3684 end if;
3686 if Parent_Installed then
3687 Remove_Parent;
3688 end if;
3689 end Analyze_Formal_Type_Declaration;
3691 ------------------------------------
3692 -- Analyze_Function_Instantiation --
3693 ------------------------------------
3695 procedure Analyze_Function_Instantiation (N : Node_Id) is
3696 begin
3697 Analyze_Subprogram_Instantiation (N, E_Function);
3698 end Analyze_Function_Instantiation;
3700 ---------------------------------
3701 -- Analyze_Generic_Access_Type --
3702 ---------------------------------
3704 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3705 begin
3706 Enter_Name (T);
3708 if Nkind (Def) = N_Access_To_Object_Definition then
3709 Access_Type_Declaration (T, Def);
3711 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3712 and then No (Full_View (Designated_Type (T)))
3713 and then not Is_Generic_Type (Designated_Type (T))
3714 then
3715 Error_Msg_N ("premature usage of incomplete type", Def);
3717 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3718 Error_Msg_N
3719 ("only a subtype mark is allowed in a formal", Def);
3720 end if;
3722 else
3723 Access_Subprogram_Declaration (T, Def);
3724 end if;
3725 end Analyze_Generic_Access_Type;
3727 ---------------------------------
3728 -- Analyze_Generic_Formal_Part --
3729 ---------------------------------
3731 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3732 Gen_Parm_Decl : Node_Id;
3734 begin
3735 -- The generic formals are processed in the scope of the generic unit,
3736 -- where they are immediately visible. The scope is installed by the
3737 -- caller.
3739 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3740 while Present (Gen_Parm_Decl) loop
3741 Analyze (Gen_Parm_Decl);
3742 Next (Gen_Parm_Decl);
3743 end loop;
3745 Generate_Reference_To_Generic_Formals (Current_Scope);
3747 -- For Ada 2022, some formal parameters can carry aspects, which must
3748 -- be name-resolved at the end of the list of formal parameters (which
3749 -- has the semantics of a declaration list).
3751 Analyze_Contracts (Generic_Formal_Declarations (N));
3752 end Analyze_Generic_Formal_Part;
3754 ------------------------------------------
3755 -- Analyze_Generic_Package_Declaration --
3756 ------------------------------------------
3758 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3759 Decls : constant List_Id := Visible_Declarations (Specification (N));
3760 Loc : constant Source_Ptr := Sloc (N);
3762 Decl : Node_Id;
3763 Id : Entity_Id;
3764 New_N : Node_Id;
3765 Renaming : Node_Id;
3766 Save_Parent : Node_Id;
3768 begin
3769 -- A generic may grant access to its private enclosing context depending
3770 -- on the placement of its corresponding body. From elaboration point of
3771 -- view, the flow of execution may enter this private context, and then
3772 -- reach an external unit, thus producing a dependency on that external
3773 -- unit. For such a path to be properly discovered and encoded in the
3774 -- ALI file of the main unit, let the ABE mechanism process the body of
3775 -- the main unit, and encode all relevant invocation constructs and the
3776 -- relations between them.
3778 Mark_Save_Invocation_Graph_Of_Body;
3780 -- We introduce a renaming of the enclosing package, to have a usable
3781 -- entity as the prefix of an expanded name for a local entity of the
3782 -- form Par.P.Q, where P is the generic package. This is because a local
3783 -- entity named P may hide it, so that the usual visibility rules in
3784 -- the instance will not resolve properly.
3786 Renaming :=
3787 Make_Package_Renaming_Declaration (Loc,
3788 Defining_Unit_Name =>
3789 Make_Defining_Identifier (Loc,
3790 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3791 Name =>
3792 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3794 -- The declaration is inserted before other declarations, but before
3795 -- pragmas that may be library-unit pragmas and must appear before other
3796 -- declarations. The pragma Compile_Time_Error is not in this class, and
3797 -- may contain an expression that includes such a qualified name, so the
3798 -- renaming declaration must appear before it.
3800 -- Are there other pragmas that require this special handling ???
3802 if Present (Decls) then
3803 Decl := First (Decls);
3804 while Present (Decl)
3805 and then Nkind (Decl) = N_Pragma
3806 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3807 loop
3808 Next (Decl);
3809 end loop;
3811 if Present (Decl) then
3812 Insert_Before (Decl, Renaming);
3813 else
3814 Append (Renaming, Visible_Declarations (Specification (N)));
3815 end if;
3817 else
3818 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3819 end if;
3821 -- Create copy of generic unit, and save for instantiation. If the unit
3822 -- is a child unit, do not copy the specifications for the parent, which
3823 -- are not part of the generic tree.
3825 Save_Parent := Parent_Spec (N);
3826 Set_Parent_Spec (N, Empty);
3828 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3829 Set_Parent_Spec (New_N, Save_Parent);
3830 Rewrite (N, New_N);
3832 -- Once the contents of the generic copy and the template are swapped,
3833 -- do the same for their respective aspect specifications.
3835 Exchange_Aspects (N, New_N);
3837 -- Collect all contract-related source pragmas found within the template
3838 -- and attach them to the contract of the package spec. This contract is
3839 -- used in the capture of global references within annotations.
3841 Create_Generic_Contract (N);
3843 Id := Defining_Entity (N);
3844 Generate_Definition (Id);
3846 -- Expansion is not applied to generic units
3848 Start_Generic;
3850 Enter_Name (Id);
3851 Mutate_Ekind (Id, E_Generic_Package);
3852 Set_Is_Not_Self_Hidden (Id);
3853 Set_Etype (Id, Standard_Void_Type);
3855 -- Set SPARK_Mode from context
3857 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3858 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3859 Set_SPARK_Pragma_Inherited (Id);
3860 Set_SPARK_Aux_Pragma_Inherited (Id);
3862 -- Preserve relevant elaboration-related attributes of the context which
3863 -- are no longer available or very expensive to recompute once analysis,
3864 -- resolution, and expansion are over.
3866 Mark_Elaboration_Attributes
3867 (N_Id => Id,
3868 Checks => True,
3869 Warnings => True);
3871 -- Analyze aspects now, so that generated pragmas appear in the
3872 -- declarations before building and analyzing the generic copy.
3874 if Has_Aspects (N) then
3875 Analyze_Aspect_Specifications (N, Id);
3876 end if;
3878 Push_Scope (Id);
3879 Enter_Generic_Scope (Id);
3880 Set_Inner_Instances (Id, New_Elmt_List);
3882 Set_Categorization_From_Pragmas (N);
3883 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3885 -- Link the declaration of the generic homonym in the generic copy to
3886 -- the package it renames, so that it is always resolved properly.
3888 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3889 Set_Entity (Associated_Node (Name (Renaming)), Id);
3891 -- For a library unit, we have reconstructed the entity for the unit,
3892 -- and must reset it in the library tables.
3894 if Nkind (Parent (N)) = N_Compilation_Unit then
3895 Set_Cunit_Entity (Current_Sem_Unit, Id);
3896 end if;
3898 Analyze_Generic_Formal_Part (N);
3900 -- After processing the generic formals, analysis proceeds as for a
3901 -- non-generic package.
3903 Analyze (Specification (N));
3905 Validate_Categorization_Dependency (N, Id);
3907 End_Generic;
3909 End_Package_Scope (Id);
3910 Exit_Generic_Scope (Id);
3912 -- If the generic appears within a package unit, the body of that unit
3913 -- has to be present for instantiation and inlining.
3915 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3916 Set_Body_Needed_For_Inlining
3917 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3918 end if;
3920 if Nkind (Parent (N)) /= N_Compilation_Unit then
3921 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3922 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3923 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3925 else
3926 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3927 Validate_RT_RAT_Component (N);
3929 -- If this is a spec without a body, check that generic parameters
3930 -- are referenced.
3932 if not Body_Required (Parent (N)) then
3933 Check_References (Id);
3934 end if;
3935 end if;
3937 -- If there is a specified storage pool in the context, create an
3938 -- aspect on the package declaration, so that it is used in any
3939 -- instance that does not override it.
3941 if Present (Default_Pool) then
3942 declare
3943 ASN : Node_Id;
3945 begin
3946 ASN :=
3947 Make_Aspect_Specification (Loc,
3948 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3949 Expression => New_Copy (Default_Pool));
3951 if No (Aspect_Specifications (Specification (N))) then
3952 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3953 else
3954 Append (ASN, Aspect_Specifications (Specification (N)));
3955 end if;
3956 end;
3957 end if;
3958 end Analyze_Generic_Package_Declaration;
3960 --------------------------------------------
3961 -- Analyze_Generic_Subprogram_Declaration --
3962 --------------------------------------------
3964 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3965 Formals : List_Id;
3966 Id : Entity_Id;
3967 New_N : Node_Id;
3968 Result_Type : Entity_Id;
3969 Save_Parent : Node_Id;
3970 Spec : Node_Id;
3971 Typ : Entity_Id;
3973 begin
3974 -- A generic may grant access to its private enclosing context depending
3975 -- on the placement of its corresponding body. From elaboration point of
3976 -- view, the flow of execution may enter this private context, and then
3977 -- reach an external unit, thus producing a dependency on that external
3978 -- unit. For such a path to be properly discovered and encoded in the
3979 -- ALI file of the main unit, let the ABE mechanism process the body of
3980 -- the main unit, and encode all relevant invocation constructs and the
3981 -- relations between them.
3983 Mark_Save_Invocation_Graph_Of_Body;
3985 -- Create copy of generic unit, and save for instantiation. If the unit
3986 -- is a child unit, do not copy the specifications for the parent, which
3987 -- are not part of the generic tree.
3989 Save_Parent := Parent_Spec (N);
3990 Set_Parent_Spec (N, Empty);
3992 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3993 Set_Parent_Spec (New_N, Save_Parent);
3994 Rewrite (N, New_N);
3996 -- Once the contents of the generic copy and the template are swapped,
3997 -- do the same for their respective aspect specifications.
3999 Exchange_Aspects (N, New_N);
4001 -- Collect all contract-related source pragmas found within the template
4002 -- and attach them to the contract of the subprogram spec. This contract
4003 -- is used in the capture of global references within annotations.
4005 Create_Generic_Contract (N);
4007 Spec := Specification (N);
4008 Id := Defining_Entity (Spec);
4009 Generate_Definition (Id);
4011 if Nkind (Id) = N_Defining_Operator_Symbol then
4012 Error_Msg_N
4013 ("operator symbol not allowed for generic subprogram", Id);
4014 end if;
4016 Start_Generic;
4018 Enter_Name (Id);
4019 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4021 Push_Scope (Id);
4022 Enter_Generic_Scope (Id);
4023 Set_Inner_Instances (Id, New_Elmt_List);
4024 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4026 Analyze_Generic_Formal_Part (N);
4028 if Nkind (Spec) = N_Function_Specification then
4029 Mutate_Ekind (Id, E_Generic_Function);
4030 else
4031 Mutate_Ekind (Id, E_Generic_Procedure);
4032 end if;
4034 -- Set SPARK_Mode from context
4036 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4037 Set_SPARK_Pragma_Inherited (Id);
4039 -- Preserve relevant elaboration-related attributes of the context which
4040 -- are no longer available or very expensive to recompute once analysis,
4041 -- resolution, and expansion are over.
4043 Mark_Elaboration_Attributes
4044 (N_Id => Id,
4045 Checks => True,
4046 Warnings => True);
4048 Formals := Parameter_Specifications (Spec);
4050 if Present (Formals) then
4051 Process_Formals (Formals, Spec);
4052 end if;
4054 if Nkind (Spec) = N_Function_Specification then
4055 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4056 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4057 Set_Etype (Id, Result_Type);
4059 -- Check restriction imposed by AI05-073: a generic function
4060 -- cannot return an abstract type or an access to such.
4062 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4063 Error_Msg_N
4064 ("generic function cannot have an access result "
4065 & "that designates an abstract type", Spec);
4066 end if;
4068 else
4069 Find_Type (Result_Definition (Spec));
4070 Typ := Entity (Result_Definition (Spec));
4072 if Is_Abstract_Type (Typ)
4073 and then Ada_Version >= Ada_2012
4074 then
4075 Error_Msg_N
4076 ("generic function cannot have abstract result type", Spec);
4077 end if;
4079 -- If a null exclusion is imposed on the result type, then create
4080 -- a null-excluding itype (an access subtype) and use it as the
4081 -- function's Etype.
4083 if Is_Access_Type (Typ)
4084 and then Null_Exclusion_Present (Spec)
4085 then
4086 Set_Etype (Id,
4087 Create_Null_Excluding_Itype
4088 (T => Typ,
4089 Related_Nod => Spec,
4090 Scope_Id => Defining_Unit_Name (Spec)));
4091 else
4092 Set_Etype (Id, Typ);
4093 end if;
4094 end if;
4096 else
4097 Set_Etype (Id, Standard_Void_Type);
4098 end if;
4100 Set_Is_Not_Self_Hidden (Id);
4102 -- Analyze the aspects of the generic copy to ensure that all generated
4103 -- pragmas (if any) perform their semantic effects.
4105 if Has_Aspects (N) then
4106 Analyze_Aspect_Specifications (N, Id);
4107 end if;
4109 -- For a library unit, we have reconstructed the entity for the unit,
4110 -- and must reset it in the library tables. We also make sure that
4111 -- Body_Required is set properly in the original compilation unit node.
4113 if Nkind (Parent (N)) = N_Compilation_Unit then
4114 Set_Cunit_Entity (Current_Sem_Unit, Id);
4115 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4116 end if;
4118 -- If the generic appears within a package unit, the body of that unit
4119 -- has to be present for instantiation and inlining.
4121 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4122 and then Unit_Requires_Body (Id)
4123 then
4124 Set_Body_Needed_For_Inlining
4125 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4126 end if;
4128 Set_Categorization_From_Pragmas (N);
4129 Validate_Categorization_Dependency (N, Id);
4131 -- Capture all global references that occur within the profile of the
4132 -- generic subprogram. Aspects are not part of this processing because
4133 -- they must be delayed. If processed now, Save_Global_References will
4134 -- destroy the Associated_Node links and prevent the capture of global
4135 -- references when the contract of the generic subprogram is analyzed.
4137 Save_Global_References (Original_Node (N));
4139 End_Generic;
4140 End_Scope;
4141 Exit_Generic_Scope (Id);
4142 Generate_Reference_To_Formals (Id);
4144 List_Inherited_Pre_Post_Aspects (Id);
4145 end Analyze_Generic_Subprogram_Declaration;
4147 -----------------------------------
4148 -- Analyze_Package_Instantiation --
4149 -----------------------------------
4151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4152 -- must be replaced by gotos which jump to the end of the routine in order
4153 -- to restore the Ghost and SPARK modes.
4155 procedure Analyze_Package_Instantiation (N : Node_Id) is
4156 Has_Inline_Always : Boolean := False;
4157 -- Set if the generic unit contains any subprograms with Inline_Always.
4158 -- Only relevant when back-end inlining is not enabled.
4160 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4161 -- Return True if inlining is active and Gen_Unit contains inlined
4162 -- subprograms. In this case, we may either instantiate the body when
4163 -- front-end inlining is enabled, or add a pending instantiation when
4164 -- back-end inlining is enabled. In the former case, this may cause
4165 -- superfluous instantiations, but in either case we need to perform
4166 -- the instantiation of the body in the context of the instance and
4167 -- not in that of the point of inlining.
4169 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4170 -- Return True if Gen_Unit needs to have its body instantiated in the
4171 -- context of N. This in particular excludes generic contexts.
4173 -----------------------
4174 -- Might_Inline_Subp --
4175 -----------------------
4177 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4178 E : Entity_Id;
4180 begin
4181 if Inline_Processing_Required then
4182 -- No need to recompute the answer if we know it is positive
4183 -- and back-end inlining is enabled.
4185 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4186 return True;
4187 end if;
4189 E := First_Entity (Gen_Unit);
4190 while Present (E) loop
4191 if Is_Subprogram (E) and then Is_Inlined (E) then
4192 -- Remember if there are any subprograms with Inline_Always
4194 if Has_Pragma_Inline_Always (E) then
4195 Has_Inline_Always := True;
4196 end if;
4198 Set_Is_Inlined (Gen_Unit);
4199 return True;
4200 end if;
4202 Next_Entity (E);
4203 end loop;
4204 end if;
4206 return False;
4207 end Might_Inline_Subp;
4209 -------------------------------
4210 -- Needs_Body_Instantiated --
4211 -------------------------------
4213 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4214 begin
4215 -- No need to instantiate bodies in generic units
4217 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4218 return False;
4219 end if;
4221 -- If the instantiation is in the main unit, then the body is needed
4223 if Is_In_Main_Unit (N) then
4224 return True;
4225 end if;
4227 -- In GNATprove mode, never instantiate bodies outside of the main
4228 -- unit, as it does not use frontend/backend inlining in the way that
4229 -- GNAT does, so does not benefit from such instantiations. On the
4230 -- contrary, such instantiations may bring artificial constraints,
4231 -- as for example such bodies may require preprocessing.
4233 if GNATprove_Mode then
4234 return False;
4235 end if;
4237 -- If not, then again no need to instantiate bodies in generic units
4239 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4240 return False;
4241 end if;
4243 -- Here we have a special handling for back-end inlining: if inline
4244 -- processing is required, then we unconditionally want to have the
4245 -- body instantiated. The reason is that Might_Inline_Subp does not
4246 -- catch all the cases (as it does not recurse into nested packages)
4247 -- so this avoids the need to patch things up afterwards. Moreover,
4248 -- these instantiations are only performed on demand when back-end
4249 -- inlining is enabled, so this causes very little extra work.
4251 if Inline_Processing_Required and then Back_End_Inlining then
4252 return True;
4253 end if;
4255 -- We want to have the bodies instantiated in non-main units if
4256 -- they might contribute inlined subprograms.
4258 return Might_Inline_Subp (Gen_Unit);
4259 end Needs_Body_Instantiated;
4261 -- Local declarations
4263 Gen_Id : constant Node_Id := Name (N);
4264 Inst_Id : constant Entity_Id := Defining_Entity (N);
4265 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4266 Loc : constant Source_Ptr := Sloc (N);
4268 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4269 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4270 Saved_ISMP : constant Boolean :=
4271 Ignore_SPARK_Mode_Pragmas_In_Instance;
4272 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4273 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4274 -- Save the Ghost and SPARK mode-related data to restore on exit
4276 Saved_Style_Check : constant Boolean := Style_Check;
4277 -- Save style check mode for restore on exit
4279 Act_Decl : Node_Id;
4280 Act_Decl_Name : Node_Id;
4281 Act_Decl_Id : Entity_Id;
4282 Act_Spec : Node_Id;
4283 Act_Tree : Node_Id;
4284 Env_Installed : Boolean := False;
4285 Gen_Decl : Node_Id;
4286 Gen_Spec : Node_Id;
4287 Gen_Unit : Entity_Id;
4288 Inline_Now : Boolean := False;
4289 Needs_Body : Boolean;
4290 Parent_Installed : Boolean := False;
4291 Renaming_List : List_Id;
4292 Unit_Renaming : Node_Id;
4294 Vis_Prims_List : Elist_Id := No_Elist;
4295 -- List of primitives made temporarily visible in the instantiation
4296 -- to match the visibility of the formal type
4298 -- Start of processing for Analyze_Package_Instantiation
4300 begin
4301 -- Preserve relevant elaboration-related attributes of the context which
4302 -- are no longer available or very expensive to recompute once analysis,
4303 -- resolution, and expansion are over.
4305 Mark_Elaboration_Attributes
4306 (N_Id => N,
4307 Checks => True,
4308 Level => True,
4309 Modes => True,
4310 Warnings => True);
4312 -- Very first thing: check for Text_IO special unit in case we are
4313 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4315 Check_Text_IO_Special_Unit (Name (N));
4317 -- Make node global for error reporting
4319 Instantiation_Node := N;
4321 -- Case of instantiation of a generic package
4323 if Nkind (N) = N_Package_Instantiation then
4324 Act_Decl_Id := New_Copy (Defining_Entity (N));
4326 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4327 Act_Decl_Name :=
4328 Make_Defining_Program_Unit_Name (Loc,
4329 Name =>
4330 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4331 Defining_Identifier => Act_Decl_Id);
4332 else
4333 Act_Decl_Name := Act_Decl_Id;
4334 end if;
4336 -- Case of instantiation of a formal package
4338 else
4339 Act_Decl_Id := Defining_Identifier (N);
4340 Act_Decl_Name := Act_Decl_Id;
4341 end if;
4343 Generate_Definition (Act_Decl_Id);
4344 Mutate_Ekind (Act_Decl_Id, E_Package);
4345 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4347 -- Initialize list of incomplete actuals before analysis
4349 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4351 Preanalyze_Actuals (N, Act_Decl_Id);
4353 -- Turn off style checking in instances. If the check is enabled on the
4354 -- generic unit, a warning in an instance would just be noise. If not
4355 -- enabled on the generic, then a warning in an instance is just wrong.
4356 -- This must be done after analyzing the actuals, which do come from
4357 -- source and are subject to style checking.
4359 Style_Check := False;
4361 Init_Env;
4362 Env_Installed := True;
4364 -- Reset renaming map for formal types. The mapping is established
4365 -- when analyzing the generic associations, but some mappings are
4366 -- inherited from formal packages of parent units, and these are
4367 -- constructed when the parents are installed.
4369 Generic_Renamings.Set_Last (0);
4370 Generic_Renamings_HTable.Reset;
4372 -- Except for an abbreviated instance created to check a formal package,
4373 -- install the parent if this is a generic child unit.
4375 if not Is_Abbreviated_Instance (Inst_Id) then
4376 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4377 end if;
4379 Gen_Unit := Entity (Gen_Id);
4381 -- A package instantiation is Ghost when it is subject to pragma Ghost
4382 -- or the generic template is Ghost. Set the mode now to ensure that
4383 -- any nodes generated during analysis and expansion are marked as
4384 -- Ghost.
4386 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4388 -- Verify that it is the name of a generic package
4390 -- A visibility glitch: if the instance is a child unit and the generic
4391 -- is the generic unit of a parent instance (i.e. both the parent and
4392 -- the child units are instances of the same package) the name now
4393 -- denotes the renaming within the parent, not the intended generic
4394 -- unit. See if there is a homonym that is the desired generic. The
4395 -- renaming declaration must be visible inside the instance of the
4396 -- child, but not when analyzing the name in the instantiation itself.
4398 if Ekind (Gen_Unit) = E_Package
4399 and then Present (Renamed_Entity (Gen_Unit))
4400 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4401 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4402 and then Present (Homonym (Gen_Unit))
4403 then
4404 Gen_Unit := Homonym (Gen_Unit);
4405 end if;
4407 if Etype (Gen_Unit) = Any_Type then
4408 Restore_Env;
4409 goto Leave;
4411 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4413 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4415 if From_Limited_With (Gen_Unit) then
4416 Error_Msg_N
4417 ("cannot instantiate a limited withed package", Gen_Id);
4418 else
4419 Error_Msg_NE
4420 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4421 end if;
4423 Restore_Env;
4424 goto Leave;
4425 end if;
4427 if In_Extended_Main_Source_Unit (N) then
4428 Set_Is_Instantiated (Gen_Unit);
4429 Generate_Reference (Gen_Unit, N);
4431 if Present (Renamed_Entity (Gen_Unit)) then
4432 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4433 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4434 end if;
4435 end if;
4437 if Nkind (Gen_Id) = N_Identifier
4438 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4439 then
4440 Error_Msg_NE
4441 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4443 elsif Nkind (Gen_Id) = N_Expanded_Name
4444 and then Is_Child_Unit (Gen_Unit)
4445 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4446 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4447 then
4448 Error_Msg_N
4449 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4450 end if;
4452 Set_Entity (Gen_Id, Gen_Unit);
4454 -- If generic is a renaming, get original generic unit
4456 if Present (Renamed_Entity (Gen_Unit))
4457 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4458 then
4459 Gen_Unit := Renamed_Entity (Gen_Unit);
4460 end if;
4462 -- Verify that there are no circular instantiations
4464 if In_Open_Scopes (Gen_Unit) then
4465 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4466 Restore_Env;
4467 goto Leave;
4469 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4470 Error_Msg_Node_2 := Current_Scope;
4471 Error_Msg_NE
4472 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4473 Circularity_Detected := True;
4474 Restore_Env;
4475 goto Leave;
4477 else
4478 Mutate_Ekind (Inst_Id, E_Package);
4479 Set_Scope (Inst_Id, Current_Scope);
4481 -- If the context of the instance is subject to SPARK_Mode "off" or
4482 -- the annotation is altogether missing, set the global flag which
4483 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4484 -- the instance.
4486 if SPARK_Mode /= On then
4487 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4489 -- Mark the instance spec in case the body is instantiated at a
4490 -- later pass. This preserves the original context in effect for
4491 -- the body.
4493 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4494 end if;
4496 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4497 Gen_Spec := Specification (Gen_Decl);
4499 -- Initialize renamings map, for error checking, and the list that
4500 -- holds private entities whose views have changed between generic
4501 -- definition and instantiation. If this is the instance created to
4502 -- validate an actual package, the instantiation environment is that
4503 -- of the enclosing instance.
4505 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4507 -- Copy original generic tree, to produce text for instantiation
4509 Act_Tree :=
4510 Copy_Generic_Node
4511 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4513 Act_Spec := Specification (Act_Tree);
4515 -- If this is the instance created to validate an actual package,
4516 -- only the formals matter, do not examine the package spec itself.
4518 if Is_Actual_Pack then
4519 Set_Visible_Declarations (Act_Spec, New_List);
4520 Set_Private_Declarations (Act_Spec, New_List);
4521 end if;
4523 Renaming_List :=
4524 Analyze_Associations
4525 (I_Node => N,
4526 Formals => Generic_Formal_Declarations (Act_Tree),
4527 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4529 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4531 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4532 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4533 Set_Is_Generic_Instance (Act_Decl_Id);
4534 Set_Generic_Parent (Act_Spec, Gen_Unit);
4536 -- References to the generic in its own declaration or its body are
4537 -- references to the instance. Add a renaming declaration for the
4538 -- generic unit itself. This declaration, as well as the renaming
4539 -- declarations for the generic formals, must remain private to the
4540 -- unit: the formals, because this is the language semantics, and
4541 -- the unit because its use is an artifact of the implementation.
4543 Unit_Renaming :=
4544 Make_Package_Renaming_Declaration (Loc,
4545 Defining_Unit_Name =>
4546 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4547 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4549 Append (Unit_Renaming, Renaming_List);
4551 -- The renaming declarations are the first local declarations of the
4552 -- new unit.
4554 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4555 Insert_List_Before
4556 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4557 else
4558 Set_Visible_Declarations (Act_Spec, Renaming_List);
4559 end if;
4561 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4563 -- Propagate the aspect specifications from the package declaration
4564 -- template to the instantiated version of the package declaration.
4566 if Has_Aspects (Act_Tree) then
4567 Set_Aspect_Specifications (Act_Decl,
4568 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4569 end if;
4571 -- The generic may have a generated Default_Storage_Pool aspect,
4572 -- set at the point of generic declaration. If the instance has
4573 -- that aspect, it overrides the one inherited from the generic.
4575 if Has_Aspects (Gen_Spec) then
4576 if No (Aspect_Specifications (N)) then
4577 Set_Aspect_Specifications (N,
4578 (New_Copy_List_Tree
4579 (Aspect_Specifications (Gen_Spec))));
4581 else
4582 declare
4583 Inherited_Aspects : constant List_Id :=
4584 New_Copy_List_Tree
4585 (Aspect_Specifications (Gen_Spec));
4587 ASN1 : Node_Id;
4588 ASN2 : Node_Id;
4589 Pool_Present : Boolean := False;
4591 begin
4592 ASN1 := First (Aspect_Specifications (N));
4593 while Present (ASN1) loop
4594 if Chars (Identifier (ASN1)) =
4595 Name_Default_Storage_Pool
4596 then
4597 Pool_Present := True;
4598 exit;
4599 end if;
4601 Next (ASN1);
4602 end loop;
4604 if Pool_Present then
4606 -- If generic carries a default storage pool, remove it
4607 -- in favor of the instance one.
4609 ASN2 := First (Inherited_Aspects);
4610 while Present (ASN2) loop
4611 if Chars (Identifier (ASN2)) =
4612 Name_Default_Storage_Pool
4613 then
4614 Remove (ASN2);
4615 exit;
4616 end if;
4618 Next (ASN2);
4619 end loop;
4620 end if;
4622 Prepend_List_To
4623 (Aspect_Specifications (N), Inherited_Aspects);
4624 end;
4625 end if;
4626 end if;
4628 -- Save the instantiation node for a subsequent instantiation of the
4629 -- body if there is one and it needs to be instantiated here.
4631 -- We instantiate the body only if we are generating code, or if we
4632 -- are generating cross-reference information, or for GNATprove use.
4634 declare
4635 Enclosing_Body_Present : Boolean := False;
4636 -- If the generic unit is not a compilation unit, then a body may
4637 -- be present in its parent even if none is required. We create a
4638 -- tentative pending instantiation for the body, which will be
4639 -- discarded if none is actually present.
4641 Scop : Entity_Id;
4643 begin
4644 if Scope (Gen_Unit) /= Standard_Standard
4645 and then not Is_Child_Unit (Gen_Unit)
4646 then
4647 Scop := Scope (Gen_Unit);
4648 while Present (Scop) and then Scop /= Standard_Standard loop
4649 if Unit_Requires_Body (Scop) then
4650 Enclosing_Body_Present := True;
4651 exit;
4653 elsif In_Open_Scopes (Scop)
4654 and then In_Package_Body (Scop)
4655 then
4656 Enclosing_Body_Present := True;
4657 exit;
4658 end if;
4660 exit when Is_Compilation_Unit (Scop);
4661 Scop := Scope (Scop);
4662 end loop;
4663 end if;
4665 -- If front-end inlining is enabled or there are any subprograms
4666 -- marked with Inline_Always, and this is a unit for which code
4667 -- will be generated, we instantiate the body at once.
4669 -- This is done if the instance is not the main unit, and if the
4670 -- generic is not a child unit of another generic, to avoid scope
4671 -- problems and the reinstallation of parent instances.
4673 if Expander_Active
4674 and then (not Is_Child_Unit (Gen_Unit)
4675 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4676 and then Might_Inline_Subp (Gen_Unit)
4677 and then not Is_Actual_Pack
4678 then
4679 if not Back_End_Inlining
4680 and then (Front_End_Inlining or else Has_Inline_Always)
4681 and then (Is_In_Main_Unit (N)
4682 or else In_Main_Context (Current_Scope))
4683 and then Nkind (Parent (N)) /= N_Compilation_Unit
4684 then
4685 Inline_Now := True;
4687 -- In configurable_run_time mode we force the inlining of
4688 -- predefined subprograms marked Inline_Always, to minimize
4689 -- the use of the run-time library.
4691 elsif In_Predefined_Unit (Gen_Decl)
4692 and then Configurable_Run_Time_Mode
4693 and then Nkind (Parent (N)) /= N_Compilation_Unit
4694 then
4695 Inline_Now := True;
4696 end if;
4698 -- If the current scope is itself an instance within a child
4699 -- unit, there will be duplications in the scope stack, and the
4700 -- unstacking mechanism in Inline_Instance_Body will fail.
4701 -- This loses some rare cases of optimization.
4703 if Is_Generic_Instance (Current_Scope) then
4704 declare
4705 Curr_Unit : constant Entity_Id :=
4706 Cunit_Entity (Current_Sem_Unit);
4707 begin
4708 if Curr_Unit /= Current_Scope
4709 and then Is_Child_Unit (Curr_Unit)
4710 then
4711 Inline_Now := False;
4712 end if;
4713 end;
4714 end if;
4715 end if;
4717 Needs_Body :=
4718 (Unit_Requires_Body (Gen_Unit)
4719 or else Enclosing_Body_Present
4720 or else Present (Corresponding_Body (Gen_Decl)))
4721 and then Needs_Body_Instantiated (Gen_Unit)
4722 and then not Is_Actual_Pack
4723 and then not Inline_Now
4724 and then (Operating_Mode = Generate_Code
4725 or else (Operating_Mode = Check_Semantics
4726 and then GNATprove_Mode));
4728 -- If front-end inlining is enabled or there are any subprograms
4729 -- marked with Inline_Always, do not instantiate body when within
4730 -- a generic context.
4732 if not Back_End_Inlining
4733 and then (Front_End_Inlining or else Has_Inline_Always)
4734 and then not Expander_Active
4735 then
4736 Needs_Body := False;
4737 end if;
4739 -- If the current context is generic, and the package being
4740 -- instantiated is declared within a formal package, there is no
4741 -- body to instantiate until the enclosing generic is instantiated
4742 -- and there is an actual for the formal package. If the formal
4743 -- package has parameters, we build a regular package instance for
4744 -- it, that precedes the original formal package declaration.
4746 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4747 declare
4748 Decl : constant Node_Id :=
4749 Original_Node
4750 (Unit_Declaration_Node (Scope (Gen_Unit)));
4751 begin
4752 if Nkind (Decl) = N_Formal_Package_Declaration
4753 or else (Nkind (Decl) = N_Package_Declaration
4754 and then Is_List_Member (Decl)
4755 and then Present (Next (Decl))
4756 and then
4757 Nkind (Next (Decl)) =
4758 N_Formal_Package_Declaration)
4759 then
4760 Needs_Body := False;
4761 end if;
4762 end;
4763 end if;
4764 end;
4766 -- For RCI unit calling stubs, we omit the instance body if the
4767 -- instance is the RCI library unit itself.
4769 -- However there is a special case for nested instances: in this case
4770 -- we do generate the instance body, as it might be required, e.g.
4771 -- because it provides stream attributes for some type used in the
4772 -- profile of a remote subprogram. This is consistent with 12.3(12),
4773 -- which indicates that the instance body occurs at the place of the
4774 -- instantiation, and thus is part of the RCI declaration, which is
4775 -- present on all client partitions (this is E.2.3(18)).
4777 -- Note that AI12-0002 may make it illegal at some point to have
4778 -- stream attributes defined in an RCI unit, in which case this
4779 -- special case will become unnecessary. In the meantime, there
4780 -- is known application code in production that depends on this
4781 -- being possible, so we definitely cannot eliminate the body in
4782 -- the case of nested instances for the time being.
4784 -- When we generate a nested instance body, calling stubs for any
4785 -- relevant subprogram will be inserted immediately after the
4786 -- subprogram declarations, and will take precedence over the
4787 -- subsequent (original) body. (The stub and original body will be
4788 -- complete homographs, but this is permitted in an instance).
4789 -- (Could we do better and remove the original body???)
4791 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4792 and then Comes_From_Source (N)
4793 and then Nkind (Parent (N)) = N_Compilation_Unit
4794 then
4795 Needs_Body := False;
4796 end if;
4798 -- If the context requires a full instantiation, set things up for
4799 -- subsequent construction of the body.
4801 if Needs_Body then
4802 declare
4803 Fin_Scop, S : Entity_Id;
4805 begin
4806 Check_Forward_Instantiation (Gen_Decl);
4808 Fin_Scop := Empty;
4810 -- For a package instantiation that is not a compilation unit,
4811 -- indicate that cleanup actions of the innermost enclosing
4812 -- scope for which they are generated should be delayed until
4813 -- after the package body is instantiated.
4815 if Nkind (N) = N_Package_Instantiation
4816 and then not Is_Compilation_Unit (Act_Decl_Id)
4817 then
4818 S := Current_Scope;
4820 while S /= Standard_Standard loop
4821 -- Cleanup actions are not generated within generic units
4822 -- or in the formal part of generic units.
4824 if Inside_A_Generic
4825 or else Is_Generic_Unit (S)
4826 or else Ekind (S) = E_Void
4827 then
4828 exit;
4830 -- For package scopes, cleanup actions are generated only
4831 -- for compilation units, for spec and body separately.
4833 elsif Ekind (S) = E_Package then
4834 if Is_Compilation_Unit (S) then
4835 if In_Package_Body (S) then
4836 Fin_Scop := Body_Entity (S);
4837 else
4838 Fin_Scop := S;
4839 end if;
4841 Set_Delay_Cleanups (Fin_Scop);
4842 exit;
4844 else
4845 S := Scope (S);
4846 end if;
4848 -- Cleanup actions are generated for all dynamic scopes
4850 else
4851 Fin_Scop := S;
4852 Set_Delay_Cleanups (Fin_Scop);
4853 exit;
4854 end if;
4855 end loop;
4856 end if;
4858 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
4859 end;
4860 end if;
4862 Set_Categorization_From_Pragmas (Act_Decl);
4864 if Parent_Installed then
4865 Hide_Current_Scope;
4866 end if;
4868 Set_Instance_Spec (N, Act_Decl);
4870 -- If not a compilation unit, insert the package declaration before
4871 -- the original instantiation node.
4873 if Nkind (Parent (N)) /= N_Compilation_Unit then
4874 Mark_Rewrite_Insertion (Act_Decl);
4875 Insert_Before (N, Act_Decl);
4877 if Has_Aspects (N) then
4878 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4880 -- The pragma created for a Default_Storage_Pool aspect must
4881 -- appear ahead of the declarations in the instance spec.
4882 -- Analysis has placed it after the instance node, so remove
4883 -- it and reinsert it properly now.
4885 declare
4886 ASN : constant Node_Id := First (Aspect_Specifications (N));
4887 A_Name : constant Name_Id := Chars (Identifier (ASN));
4888 Decl : Node_Id;
4890 begin
4891 if A_Name = Name_Default_Storage_Pool then
4892 if No (Visible_Declarations (Act_Spec)) then
4893 Set_Visible_Declarations (Act_Spec, New_List);
4894 end if;
4896 Decl := Next (N);
4897 while Present (Decl) loop
4898 if Nkind (Decl) = N_Pragma then
4899 Remove (Decl);
4900 Prepend (Decl, Visible_Declarations (Act_Spec));
4901 exit;
4902 end if;
4904 Next (Decl);
4905 end loop;
4906 end if;
4907 end;
4908 end if;
4910 Analyze (Act_Decl);
4912 -- For an instantiation that is a compilation unit, place
4913 -- declaration on current node so context is complete for analysis
4914 -- (including nested instantiations). If this is the main unit,
4915 -- the declaration eventually replaces the instantiation node.
4916 -- If the instance body is created later, it replaces the
4917 -- instance node, and the declaration is attached to it
4918 -- (see Build_Instance_Compilation_Unit_Nodes).
4920 else
4921 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4923 -- The entity for the current unit is the newly created one,
4924 -- and all semantic information is attached to it.
4926 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4928 -- If this is the main unit, replace the main entity as well
4930 if Current_Sem_Unit = Main_Unit then
4931 Main_Unit_Entity := Act_Decl_Id;
4932 end if;
4933 end if;
4935 Set_Unit (Parent (N), Act_Decl);
4936 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4937 Set_Package_Instantiation (Act_Decl_Id, N);
4939 -- Process aspect specifications of the instance node, if any, to
4940 -- take into account categorization pragmas before analyzing the
4941 -- instance.
4943 if Has_Aspects (N) then
4944 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4945 end if;
4947 Analyze (Act_Decl);
4948 Set_Unit (Parent (N), N);
4949 Set_Body_Required (Parent (N), False);
4951 -- We never need elaboration checks on instantiations, since by
4952 -- definition, the body instantiation is elaborated at the same
4953 -- time as the spec instantiation.
4955 if Legacy_Elaboration_Checks then
4956 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4957 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4958 end if;
4959 end if;
4961 if Legacy_Elaboration_Checks then
4962 Check_Elab_Instantiation (N);
4963 end if;
4965 -- Save the scenario for later examination by the ABE Processing
4966 -- phase.
4968 Record_Elaboration_Scenario (N);
4970 -- The instantiation results in a guaranteed ABE
4972 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4973 -- Do not instantiate the corresponding body because gigi cannot
4974 -- handle certain types of premature instantiations.
4976 Remove_Dead_Instance (N);
4978 -- Create completing bodies for all subprogram declarations since
4979 -- their real bodies will not be instantiated.
4981 Provide_Completing_Bodies (Instance_Spec (N));
4982 end if;
4984 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4986 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4987 First_Private_Entity (Act_Decl_Id));
4989 -- If the instantiation needs a body, the unit will be turned into
4990 -- a package body and receive its own elaboration entity. Otherwise,
4991 -- the nature of the unit is now a package declaration.
4993 -- Note that the below rewriting means that Act_Decl, which has been
4994 -- analyzed and expanded, will be re-expanded as the rewritten N.
4996 if Nkind (Parent (N)) = N_Compilation_Unit
4997 and then not Needs_Body
4998 then
4999 Rewrite (N, Act_Decl);
5000 end if;
5002 if Present (Corresponding_Body (Gen_Decl))
5003 or else Unit_Requires_Body (Gen_Unit)
5004 then
5005 Set_Has_Completion (Act_Decl_Id);
5006 end if;
5008 Check_Formal_Packages (Act_Decl_Id);
5010 Restore_Hidden_Primitives (Vis_Prims_List);
5011 Restore_Private_Views (Act_Decl_Id);
5013 Inherit_Context (Gen_Decl, N);
5015 if Parent_Installed then
5016 Remove_Parent;
5017 end if;
5019 Restore_Env;
5020 Env_Installed := False;
5021 end if;
5023 Validate_Categorization_Dependency (N, Act_Decl_Id);
5025 -- There used to be a check here to prevent instantiations in local
5026 -- contexts if the No_Local_Allocators restriction was active. This
5027 -- check was removed by a binding interpretation in AI-95-00130/07,
5028 -- but we retain the code for documentation purposes.
5030 -- if Ekind (Act_Decl_Id) /= E_Void
5031 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5032 -- then
5033 -- Check_Restriction (No_Local_Allocators, N);
5034 -- end if;
5036 if Inline_Now then
5037 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5038 end if;
5040 -- Check that if N is an instantiation of System.Dim_Float_IO or
5041 -- System.Dim_Integer_IO, the formal type has a dimension system.
5043 if Nkind (N) = N_Package_Instantiation
5044 and then Is_Dim_IO_Package_Instantiation (N)
5045 then
5046 declare
5047 Assoc : constant Node_Id := First (Generic_Associations (N));
5048 begin
5049 if not Has_Dimension_System
5050 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5051 then
5052 Error_Msg_N ("type with a dimension system expected", Assoc);
5053 end if;
5054 end;
5055 end if;
5057 <<Leave>>
5058 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
5059 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5060 end if;
5062 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5063 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5064 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5065 Style_Check := Saved_Style_Check;
5067 exception
5068 when Instantiation_Error =>
5069 if Parent_Installed then
5070 Remove_Parent;
5071 end if;
5073 if Env_Installed then
5074 Restore_Env;
5075 end if;
5077 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5078 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5079 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5080 Style_Check := Saved_Style_Check;
5081 end Analyze_Package_Instantiation;
5083 --------------------------
5084 -- Inline_Instance_Body --
5085 --------------------------
5087 -- WARNING: This routine manages SPARK regions. Return statements must be
5088 -- replaced by gotos which jump to the end of the routine and restore the
5089 -- SPARK mode.
5091 procedure Inline_Instance_Body
5092 (N : Node_Id;
5093 Gen_Unit : Entity_Id;
5094 Act_Decl : Node_Id)
5096 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5098 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5099 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5100 Gen_Comp : constant Entity_Id :=
5101 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5103 Scope_Stack_Depth : constant Pos :=
5104 Scope_Stack.Last - Scope_Stack.First + 1;
5106 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5107 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5108 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5110 Curr_Scope : Entity_Id := Empty;
5111 List : Elist_Id := No_Elist; -- init to avoid warning
5112 N_Instances : Nat := 0;
5113 Num_Inner : Nat := 0;
5114 Num_Scopes : Nat := 0;
5115 Removed : Boolean := False;
5116 S : Entity_Id;
5117 Vis : Boolean;
5119 begin
5120 -- Case of generic unit defined in another unit. We must remove the
5121 -- complete context of the current unit to install that of the generic.
5123 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5125 -- Loop through enclosing scopes until we reach a generic instance,
5126 -- package body, or subprogram.
5128 S := Current_Scope;
5129 while Present (S) and then S /= Standard_Standard loop
5131 -- Save use clauses from enclosing scopes into Use_Clauses
5133 loop
5134 Num_Scopes := Num_Scopes + 1;
5136 Use_Clauses (Num_Scopes) :=
5137 (Scope_Stack.Table
5138 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5139 End_Use_Clauses (Use_Clauses (Num_Scopes));
5141 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5142 or else Scope_Stack.Table
5143 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5144 end loop;
5146 exit when Is_Generic_Instance (S)
5147 and then (In_Package_Body (S)
5148 or else Ekind (S) = E_Procedure
5149 or else Ekind (S) = E_Function);
5150 S := Scope (S);
5151 end loop;
5153 Vis := Is_Immediately_Visible (Gen_Comp);
5155 -- Find and save all enclosing instances
5157 S := Current_Scope;
5159 while Present (S)
5160 and then S /= Standard_Standard
5161 loop
5162 if Is_Generic_Instance (S) then
5163 N_Instances := N_Instances + 1;
5164 Instances (N_Instances) := S;
5166 exit when In_Package_Body (S);
5167 end if;
5169 S := Scope (S);
5170 end loop;
5172 -- Remove context of current compilation unit, unless we are within a
5173 -- nested package instantiation, in which case the context has been
5174 -- removed previously.
5176 -- If current scope is the body of a child unit, remove context of
5177 -- spec as well. If an enclosing scope is an instance body, the
5178 -- context has already been removed, but the entities in the body
5179 -- must be made invisible as well.
5181 S := Current_Scope;
5182 while Present (S) and then S /= Standard_Standard loop
5183 if Is_Generic_Instance (S)
5184 and then (In_Package_Body (S)
5185 or else Ekind (S) in E_Procedure | E_Function)
5186 then
5187 -- We still have to remove the entities of the enclosing
5188 -- instance from direct visibility.
5190 declare
5191 E : Entity_Id;
5192 begin
5193 E := First_Entity (S);
5194 while Present (E) loop
5195 Set_Is_Immediately_Visible (E, False);
5196 Next_Entity (E);
5197 end loop;
5198 end;
5200 exit;
5201 end if;
5203 if S = Curr_Unit
5204 or else (Ekind (Curr_Unit) = E_Package_Body
5205 and then S = Spec_Entity (Curr_Unit))
5206 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5207 and then S = Corresponding_Spec
5208 (Unit_Declaration_Node (Curr_Unit)))
5209 then
5210 Removed := True;
5212 -- Remove entities in current scopes from visibility, so that
5213 -- instance body is compiled in a clean environment.
5215 List := Save_Scope_Stack (Handle_Use => False);
5217 if Is_Child_Unit (S) then
5219 -- Remove child unit from stack, as well as inner scopes.
5220 -- Removing the context of a child unit removes parent units
5221 -- as well.
5223 while Current_Scope /= S loop
5224 Num_Inner := Num_Inner + 1;
5225 Inner_Scopes (Num_Inner) := Current_Scope;
5226 Pop_Scope;
5227 end loop;
5229 Pop_Scope;
5230 Remove_Context (Curr_Comp);
5231 Curr_Scope := S;
5233 else
5234 Remove_Context (Curr_Comp);
5235 end if;
5237 if Ekind (Curr_Unit) = E_Package_Body then
5238 Remove_Context (Library_Unit (Curr_Comp));
5239 end if;
5240 end if;
5242 S := Scope (S);
5243 end loop;
5245 pragma Assert (Num_Inner < Num_Scopes);
5247 Push_Scope (Standard_Standard);
5248 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5250 -- The inlined package body is analyzed with the configuration state
5251 -- of the context prior to the scope manipulations performed above.
5253 -- ??? shouldn't this also use the warning state of the context prior
5254 -- to the scope manipulations?
5256 Instantiate_Package_Body
5257 (Body_Info =>
5258 ((Inst_Node => N,
5259 Act_Decl => Act_Decl,
5260 Fin_Scop => Empty,
5261 Config_Switches => Config_Attrs,
5262 Current_Sem_Unit => Current_Sem_Unit,
5263 Expander_Status => Expander_Active,
5264 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5265 Scope_Suppress => Scope_Suppress,
5266 Warnings => Save_Warnings)),
5267 Inlined_Body => True);
5269 Pop_Scope;
5271 -- Restore context
5273 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5275 -- Reset Generic_Instance flag so that use clauses can be installed
5276 -- in the proper order. (See Use_One_Package for effect of enclosing
5277 -- instances on processing of use clauses).
5279 for J in 1 .. N_Instances loop
5280 Set_Is_Generic_Instance (Instances (J), False);
5281 end loop;
5283 if Removed then
5284 Install_Context (Curr_Comp, Chain => False);
5286 if Present (Curr_Scope)
5287 and then Is_Child_Unit (Curr_Scope)
5288 then
5289 Push_Scope (Curr_Scope);
5290 Set_Is_Immediately_Visible (Curr_Scope);
5292 -- Finally, restore inner scopes as well
5294 for J in reverse 1 .. Num_Inner loop
5295 Push_Scope (Inner_Scopes (J));
5296 end loop;
5297 end if;
5299 Restore_Scope_Stack (List, Handle_Use => False);
5301 if Present (Curr_Scope)
5302 and then
5303 (In_Private_Part (Curr_Scope)
5304 or else In_Package_Body (Curr_Scope))
5305 then
5306 -- Install private declaration of ancestor units, which are
5307 -- currently available. Restore_Scope_Stack and Install_Context
5308 -- only install the visible part of parents.
5310 declare
5311 Par : Entity_Id;
5312 begin
5313 Par := Scope (Curr_Scope);
5314 while Present (Par) and then Par /= Standard_Standard loop
5315 Install_Private_Declarations (Par);
5316 Par := Scope (Par);
5317 end loop;
5318 end;
5319 end if;
5320 end if;
5322 -- Restore use clauses. For a child unit, use clauses in the parents
5323 -- are restored when installing the context, so only those in inner
5324 -- scopes (and those local to the child unit itself) need to be
5325 -- installed explicitly.
5327 if Is_Child_Unit (Curr_Unit) and then Removed then
5328 for J in reverse 1 .. Num_Inner + 1 loop
5329 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5330 Use_Clauses (J);
5331 Install_Use_Clauses (Use_Clauses (J));
5332 end loop;
5334 else
5335 for J in reverse 1 .. Num_Scopes loop
5336 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5337 Use_Clauses (J);
5338 Install_Use_Clauses (Use_Clauses (J));
5339 end loop;
5340 end if;
5342 -- Restore status of instances. If one of them is a body, make its
5343 -- local entities visible again.
5345 declare
5346 E : Entity_Id;
5347 Inst : Entity_Id;
5349 begin
5350 for J in 1 .. N_Instances loop
5351 Inst := Instances (J);
5352 Set_Is_Generic_Instance (Inst, True);
5354 if In_Package_Body (Inst)
5355 or else Ekind (S) in E_Procedure | E_Function
5356 then
5357 E := First_Entity (Instances (J));
5358 while Present (E) loop
5359 Set_Is_Immediately_Visible (E);
5360 Next_Entity (E);
5361 end loop;
5362 end if;
5363 end loop;
5364 end;
5366 -- If generic unit is in current unit, current context is correct. Note
5367 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5368 -- enclosing scopes were removed.
5370 else
5371 Instantiate_Package_Body
5372 (Body_Info =>
5373 ((Inst_Node => N,
5374 Act_Decl => Act_Decl,
5375 Fin_Scop => Empty,
5376 Config_Switches => Save_Config_Switches,
5377 Current_Sem_Unit => Current_Sem_Unit,
5378 Expander_Status => Expander_Active,
5379 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5380 Scope_Suppress => Scope_Suppress,
5381 Warnings => Save_Warnings)),
5382 Inlined_Body => True);
5383 end if;
5384 end Inline_Instance_Body;
5386 -------------------------------------
5387 -- Analyze_Procedure_Instantiation --
5388 -------------------------------------
5390 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5391 begin
5392 Analyze_Subprogram_Instantiation (N, E_Procedure);
5393 end Analyze_Procedure_Instantiation;
5395 -----------------------------------
5396 -- Need_Subprogram_Instance_Body --
5397 -----------------------------------
5399 function Need_Subprogram_Instance_Body
5400 (N : Node_Id;
5401 Subp : Entity_Id) return Boolean
5403 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5404 -- Return True if E is an inlined subprogram, an inlined renaming or a
5405 -- subprogram nested in an inlined subprogram. The inlining machinery
5406 -- totally disregards nested subprograms since it considers that they
5407 -- will always be compiled if the parent is (see Inline.Is_Nested).
5409 ------------------------------------
5410 -- Is_Inlined_Or_Child_Of_Inlined --
5411 ------------------------------------
5413 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5414 Scop : Entity_Id;
5416 begin
5417 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5418 return True;
5419 end if;
5421 Scop := Scope (E);
5422 while Scop /= Standard_Standard loop
5423 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5424 return True;
5425 end if;
5427 Scop := Scope (Scop);
5428 end loop;
5430 return False;
5431 end Is_Inlined_Or_Child_Of_Inlined;
5433 begin
5434 -- Must be in the main unit or inlined (or child of inlined)
5436 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5438 -- Must be generating code or analyzing code in GNATprove mode
5440 and then (Operating_Mode = Generate_Code
5441 or else (Operating_Mode = Check_Semantics
5442 and then GNATprove_Mode))
5444 -- The body is needed when generating code (full expansion) and in
5445 -- in GNATprove mode (special expansion) for formal verification of
5446 -- the body itself.
5448 and then (Expander_Active or GNATprove_Mode)
5450 -- No point in inlining if ABE is inevitable
5452 and then not Is_Known_Guaranteed_ABE (N)
5454 -- Or if subprogram is eliminated
5456 and then not Is_Eliminated (Subp)
5457 then
5458 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5459 return True;
5461 -- Here if not inlined, or we ignore the inlining
5463 else
5464 return False;
5465 end if;
5466 end Need_Subprogram_Instance_Body;
5468 --------------------------------------
5469 -- Analyze_Subprogram_Instantiation --
5470 --------------------------------------
5472 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5473 -- must be replaced by gotos which jump to the end of the routine in order
5474 -- to restore the Ghost and SPARK modes.
5476 procedure Analyze_Subprogram_Instantiation
5477 (N : Node_Id;
5478 K : Entity_Kind)
5480 Errs : constant Nat := Serious_Errors_Detected;
5481 Gen_Id : constant Node_Id := Name (N);
5482 Inst_Id : constant Entity_Id := Defining_Entity (N);
5483 Anon_Id : constant Entity_Id :=
5484 Make_Defining_Identifier (Sloc (Inst_Id),
5485 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5486 Loc : constant Source_Ptr := Sloc (N);
5488 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5489 Act_Decl : Node_Id;
5490 Act_Spec : Node_Id;
5491 Act_Tree : Node_Id;
5493 Env_Installed : Boolean := False;
5494 Gen_Unit : Entity_Id;
5495 Gen_Decl : Node_Id;
5496 Pack_Id : Entity_Id;
5497 Parent_Installed : Boolean := False;
5499 Renaming_List : List_Id;
5500 -- The list of declarations that link formals and actuals of the
5501 -- instance. These are subtype declarations for formal types, and
5502 -- renaming declarations for other formals. The subprogram declaration
5503 -- for the instance is then appended to the list, and the last item on
5504 -- the list is the renaming declaration for the instance.
5506 procedure Analyze_Instance_And_Renamings;
5507 -- The instance must be analyzed in a context that includes the mappings
5508 -- of generic parameters into actuals. We create a package declaration
5509 -- for this purpose, and a subprogram with an internal name within the
5510 -- package. The subprogram instance is simply an alias for the internal
5511 -- subprogram, declared in the current scope.
5513 procedure Build_Subprogram_Renaming;
5514 -- If the subprogram is recursive, there are occurrences of the name of
5515 -- the generic within the body, which must resolve to the current
5516 -- instance. We add a renaming declaration after the declaration, which
5517 -- is available in the instance body, as well as in the analysis of
5518 -- aspects that appear in the generic. This renaming declaration is
5519 -- inserted after the instance declaration which it renames.
5521 ------------------------------------
5522 -- Analyze_Instance_And_Renamings --
5523 ------------------------------------
5525 procedure Analyze_Instance_And_Renamings is
5526 Def_Ent : constant Entity_Id := Defining_Entity (N);
5527 Pack_Decl : Node_Id;
5529 begin
5530 if Nkind (Parent (N)) = N_Compilation_Unit then
5532 -- For the case of a compilation unit, the container package has
5533 -- the same name as the instantiation, to insure that the binder
5534 -- calls the elaboration procedure with the right name. Copy the
5535 -- entity of the instance, which may have compilation level flags
5536 -- (e.g. Is_Child_Unit) set.
5538 Pack_Id := New_Copy (Def_Ent);
5540 else
5541 -- Otherwise we use the name of the instantiation concatenated
5542 -- with its source position to ensure uniqueness if there are
5543 -- several instantiations with the same name.
5545 Pack_Id :=
5546 Make_Defining_Identifier (Loc,
5547 Chars => New_External_Name
5548 (Related_Id => Chars (Def_Ent),
5549 Suffix => "GP",
5550 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5551 end if;
5553 Pack_Decl :=
5554 Make_Package_Declaration (Loc,
5555 Specification => Make_Package_Specification (Loc,
5556 Defining_Unit_Name => Pack_Id,
5557 Visible_Declarations => Renaming_List,
5558 End_Label => Empty));
5560 Set_Instance_Spec (N, Pack_Decl);
5561 Set_Is_Generic_Instance (Pack_Id);
5562 Set_Debug_Info_Needed (Pack_Id);
5564 -- Case of not a compilation unit
5566 if Nkind (Parent (N)) /= N_Compilation_Unit then
5567 Mark_Rewrite_Insertion (Pack_Decl);
5568 Insert_Before (N, Pack_Decl);
5569 Set_Has_Completion (Pack_Id);
5571 -- Case of an instantiation that is a compilation unit
5573 -- Place declaration on current node so context is complete for
5574 -- analysis (including nested instantiations), and for use in a
5575 -- context_clause (see Analyze_With_Clause).
5577 else
5578 Set_Unit (Parent (N), Pack_Decl);
5579 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5580 end if;
5582 Analyze (Pack_Decl);
5583 Check_Formal_Packages (Pack_Id);
5585 -- Body of the enclosing package is supplied when instantiating the
5586 -- subprogram body, after semantic analysis is completed.
5588 if Nkind (Parent (N)) = N_Compilation_Unit then
5590 -- Remove package itself from visibility, so it does not
5591 -- conflict with subprogram.
5593 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5595 -- Set name and scope of internal subprogram so that the proper
5596 -- external name will be generated. The proper scope is the scope
5597 -- of the wrapper package. We need to generate debugging info for
5598 -- the internal subprogram, so set flag accordingly.
5600 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5601 Set_Scope (Anon_Id, Scope (Pack_Id));
5603 -- Mark wrapper package as referenced, to avoid spurious warnings
5604 -- if the instantiation appears in various with_ clauses of
5605 -- subunits of the main unit.
5607 Set_Referenced (Pack_Id);
5608 end if;
5610 Set_Is_Generic_Instance (Anon_Id);
5611 Set_Debug_Info_Needed (Anon_Id);
5612 Act_Decl_Id := New_Copy (Anon_Id);
5614 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5615 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5616 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5618 -- Subprogram instance comes from source only if generic does
5620 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5622 -- If the instance is a child unit, mark the Id accordingly. Mark
5623 -- the anonymous entity as well, which is the real subprogram and
5624 -- which is used when the instance appears in a context clause.
5625 -- Similarly, propagate the Is_Eliminated flag to handle properly
5626 -- nested eliminated subprograms.
5628 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5629 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5630 New_Overloaded_Entity (Act_Decl_Id);
5631 Check_Eliminated (Act_Decl_Id);
5632 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5634 if Nkind (Parent (N)) = N_Compilation_Unit then
5636 -- In compilation unit case, kill elaboration checks on the
5637 -- instantiation, since they are never needed - the body is
5638 -- instantiated at the same point as the spec.
5640 if Legacy_Elaboration_Checks then
5641 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5642 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5643 end if;
5645 Set_Is_Compilation_Unit (Anon_Id);
5646 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5647 end if;
5649 -- The instance is not a freezing point for the new subprogram.
5650 -- The anonymous subprogram may have a freeze node, created for
5651 -- some delayed aspects. This freeze node must not be inherited
5652 -- by the visible subprogram entity.
5654 Set_Is_Frozen (Act_Decl_Id, False);
5655 Set_Freeze_Node (Act_Decl_Id, Empty);
5657 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5658 Valid_Operator_Definition (Act_Decl_Id);
5659 end if;
5661 Set_Alias (Act_Decl_Id, Anon_Id);
5662 Set_Has_Completion (Act_Decl_Id);
5663 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5665 if Nkind (Parent (N)) = N_Compilation_Unit then
5666 Set_Body_Required (Parent (N), False);
5667 end if;
5668 end Analyze_Instance_And_Renamings;
5670 -------------------------------
5671 -- Build_Subprogram_Renaming --
5672 -------------------------------
5674 procedure Build_Subprogram_Renaming is
5675 Renaming_Decl : Node_Id;
5676 Unit_Renaming : Node_Id;
5678 begin
5679 Unit_Renaming :=
5680 Make_Subprogram_Renaming_Declaration (Loc,
5681 Specification =>
5682 Copy_Generic_Node
5683 (Specification (Original_Node (Gen_Decl)),
5684 Empty,
5685 Instantiating => True),
5686 Name => New_Occurrence_Of (Anon_Id, Loc));
5688 -- The generic may be a child unit. The renaming needs an identifier
5689 -- with the proper name.
5691 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5692 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5694 -- If there is a formal subprogram with the same name as the unit
5695 -- itself, do not add this renaming declaration, to prevent
5696 -- ambiguities when there is a call with that name in the body.
5698 Renaming_Decl := First (Renaming_List);
5699 while Present (Renaming_Decl) loop
5700 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5701 and then
5702 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5703 then
5704 exit;
5705 end if;
5707 Next (Renaming_Decl);
5708 end loop;
5710 if No (Renaming_Decl) then
5711 Append (Unit_Renaming, Renaming_List);
5712 end if;
5713 end Build_Subprogram_Renaming;
5715 -- Local variables
5717 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5718 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5719 Saved_ISMP : constant Boolean :=
5720 Ignore_SPARK_Mode_Pragmas_In_Instance;
5721 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5722 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5723 -- Save the Ghost and SPARK mode-related data to restore on exit
5725 Vis_Prims_List : Elist_Id := No_Elist;
5726 -- List of primitives made temporarily visible in the instantiation
5727 -- to match the visibility of the formal type
5729 -- Start of processing for Analyze_Subprogram_Instantiation
5731 begin
5732 -- Preserve relevant elaboration-related attributes of the context which
5733 -- are no longer available or very expensive to recompute once analysis,
5734 -- resolution, and expansion are over.
5736 Mark_Elaboration_Attributes
5737 (N_Id => N,
5738 Checks => True,
5739 Level => True,
5740 Modes => True,
5741 Warnings => True);
5743 -- Very first thing: check for special Text_IO unit in case we are
5744 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5745 -- such an instantiation is bogus (these are packages, not subprograms),
5746 -- but we get a better error message if we do this.
5748 Check_Text_IO_Special_Unit (Gen_Id);
5750 -- Make node global for error reporting
5752 Instantiation_Node := N;
5754 -- For package instantiations we turn off style checks, because they
5755 -- will have been emitted in the generic. For subprogram instantiations
5756 -- we want to apply at least the check on overriding indicators so we
5757 -- do not modify the style check status.
5759 -- The renaming declarations for the actuals do not come from source and
5760 -- will not generate spurious warnings.
5762 Preanalyze_Actuals (N);
5764 Init_Env;
5765 Env_Installed := True;
5766 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5767 Gen_Unit := Entity (Gen_Id);
5769 -- A subprogram instantiation is Ghost when it is subject to pragma
5770 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5771 -- that any nodes generated during analysis and expansion are marked as
5772 -- Ghost.
5774 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5776 Generate_Reference (Gen_Unit, Gen_Id);
5778 if Nkind (Gen_Id) = N_Identifier
5779 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5780 then
5781 Error_Msg_NE
5782 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5783 end if;
5785 if Etype (Gen_Unit) = Any_Type then
5786 Restore_Env;
5787 goto Leave;
5788 end if;
5790 -- Verify that it is a generic subprogram of the right kind, and that
5791 -- it does not lead to a circular instantiation.
5793 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5794 Error_Msg_NE
5795 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5797 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5798 Error_Msg_NE
5799 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5801 elsif In_Open_Scopes (Gen_Unit) then
5802 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5804 else
5805 Mutate_Ekind (Inst_Id, K);
5806 Set_Scope (Inst_Id, Current_Scope);
5808 Set_Entity (Gen_Id, Gen_Unit);
5810 if In_Extended_Main_Source_Unit (N) then
5811 Set_Is_Instantiated (Gen_Unit);
5812 Generate_Reference (Gen_Unit, N);
5813 end if;
5815 -- If renaming, get original unit
5817 if Present (Renamed_Entity (Gen_Unit))
5818 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5819 then
5820 Gen_Unit := Renamed_Entity (Gen_Unit);
5821 Set_Is_Instantiated (Gen_Unit);
5822 Generate_Reference (Gen_Unit, N);
5823 end if;
5825 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5826 Error_Msg_Node_2 := Current_Scope;
5827 Error_Msg_NE
5828 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5829 Circularity_Detected := True;
5830 Restore_Hidden_Primitives (Vis_Prims_List);
5831 goto Leave;
5832 end if;
5834 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5836 -- Initialize renamings map, for error checking
5838 Generic_Renamings.Set_Last (0);
5839 Generic_Renamings_HTable.Reset;
5841 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5843 -- Copy original generic tree, to produce text for instantiation
5845 Act_Tree :=
5846 Copy_Generic_Node
5847 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5849 -- Inherit overriding indicator from instance node
5851 Act_Spec := Specification (Act_Tree);
5852 Set_Must_Override (Act_Spec, Must_Override (N));
5853 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5855 Renaming_List :=
5856 Analyze_Associations
5857 (I_Node => N,
5858 Formals => Generic_Formal_Declarations (Act_Tree),
5859 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5861 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5863 -- The subprogram itself cannot contain a nested instance, so the
5864 -- current parent is left empty.
5866 Set_Instance_Env (Gen_Unit, Empty);
5868 -- Build the subprogram declaration, which does not appear in the
5869 -- generic template, and give it a sloc consistent with that of the
5870 -- template.
5872 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5873 Set_Generic_Parent (Act_Spec, Gen_Unit);
5874 Act_Decl :=
5875 Make_Subprogram_Declaration (Sloc (Act_Spec),
5876 Specification => Act_Spec);
5878 -- The aspects have been copied previously, but they have to be
5879 -- linked explicitly to the new subprogram declaration. Explicit
5880 -- pre/postconditions on the instance are analyzed below, in a
5881 -- separate step.
5883 Move_Aspects (Act_Tree, To => Act_Decl);
5884 Set_Categorization_From_Pragmas (Act_Decl);
5886 if Parent_Installed then
5887 Hide_Current_Scope;
5888 end if;
5890 Append (Act_Decl, Renaming_List);
5892 -- Contract-related source pragmas that follow a generic subprogram
5893 -- must be instantiated explicitly because they are not part of the
5894 -- subprogram template.
5896 Instantiate_Subprogram_Contract
5897 (Original_Node (Gen_Decl), Renaming_List);
5899 Build_Subprogram_Renaming;
5901 -- If the context of the instance is subject to SPARK_Mode "off" or
5902 -- the annotation is altogether missing, set the global flag which
5903 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5904 -- the instance. This should be done prior to analyzing the instance.
5906 if SPARK_Mode /= On then
5907 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5908 end if;
5910 -- If the context of an instance is not subject to SPARK_Mode "off",
5911 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5912 -- the latter should be the one applicable to the instance.
5914 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5915 and then Saved_SM /= Off
5916 and then Present (SPARK_Pragma (Gen_Unit))
5917 then
5918 Set_SPARK_Mode (Gen_Unit);
5919 end if;
5921 -- Need to mark Anon_Id intrinsic before calling
5922 -- Analyze_Instance_And_Renamings because this flag may be propagated
5923 -- to other nodes.
5925 if Is_Intrinsic_Subprogram (Gen_Unit) then
5926 Set_Is_Intrinsic_Subprogram (Anon_Id);
5927 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5928 end if;
5930 Analyze_Instance_And_Renamings;
5932 -- Restore SPARK_Mode from the context after analysis of the package
5933 -- declaration, so that the SPARK_Mode on the generic spec does not
5934 -- apply to the pending instance for the instance body.
5936 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5937 and then Saved_SM /= Off
5938 and then Present (SPARK_Pragma (Gen_Unit))
5939 then
5940 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5941 end if;
5943 -- If the generic is marked Import (Intrinsic), then so is the
5944 -- instance; this indicates that there is no body to instantiate.
5945 -- We also copy the interface name in case this is handled by the
5946 -- back-end and deal with an instance of unchecked conversion.
5948 if Is_Intrinsic_Subprogram (Gen_Unit) then
5949 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5950 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5952 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5953 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5954 end if;
5955 end if;
5957 -- Inherit convention from generic unit. Intrinsic convention, as for
5958 -- an instance of unchecked conversion, is not inherited because an
5959 -- explicit Ada instance has been created.
5961 if Has_Convention_Pragma (Gen_Unit)
5962 and then Convention (Gen_Unit) /= Convention_Intrinsic
5963 then
5964 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5965 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5966 end if;
5968 Generate_Definition (Act_Decl_Id);
5970 -- Inherit all inlining-related flags which apply to the generic in
5971 -- the subprogram and its declaration.
5973 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5974 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5976 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5977 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5979 Set_Has_Pragma_Inline_Always
5980 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5981 Set_Has_Pragma_Inline_Always
5982 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5984 Set_Has_Pragma_No_Inline
5985 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5986 Set_Has_Pragma_No_Inline
5987 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5989 -- Propagate No_Return if pragma applied to generic unit. This must
5990 -- be done explicitly because pragma does not appear in generic
5991 -- declaration (unlike the aspect case).
5993 if No_Return (Gen_Unit) then
5994 Set_No_Return (Act_Decl_Id);
5995 Set_No_Return (Anon_Id);
5996 end if;
5998 -- Mark both the instance spec and the anonymous package in case the
5999 -- body is instantiated at a later pass. This preserves the original
6000 -- context in effect for the body.
6002 if SPARK_Mode /= On then
6003 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6004 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6005 end if;
6007 if Legacy_Elaboration_Checks
6008 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6009 then
6010 Check_Elab_Instantiation (N);
6011 end if;
6013 -- Save the scenario for later examination by the ABE Processing
6014 -- phase.
6016 Record_Elaboration_Scenario (N);
6018 -- The instantiation results in a guaranteed ABE. Create a completing
6019 -- body for the subprogram declaration because the real body will not
6020 -- be instantiated.
6022 if Is_Known_Guaranteed_ABE (N) then
6023 Provide_Completing_Bodies (Instance_Spec (N));
6024 end if;
6026 if Is_Dispatching_Operation (Act_Decl_Id)
6027 and then Ada_Version >= Ada_2005
6028 then
6029 declare
6030 Formal : Entity_Id;
6032 begin
6033 Formal := First_Formal (Act_Decl_Id);
6034 while Present (Formal) loop
6035 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6036 and then Is_Controlling_Formal (Formal)
6037 and then not Can_Never_Be_Null (Formal)
6038 then
6039 Error_Msg_NE
6040 ("access parameter& is controlling,", N, Formal);
6041 Error_Msg_NE
6042 ("\corresponding parameter of & must be explicitly "
6043 & "null-excluding", N, Gen_Id);
6044 end if;
6046 Next_Formal (Formal);
6047 end loop;
6048 end;
6049 end if;
6051 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6053 Validate_Categorization_Dependency (N, Act_Decl_Id);
6055 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6056 Inherit_Context (Gen_Decl, N);
6058 Restore_Private_Views (Pack_Id, False);
6060 -- If the context requires a full instantiation, mark node for
6061 -- subsequent construction of the body.
6063 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6064 Check_Forward_Instantiation (Gen_Decl);
6066 -- The wrapper package is always delayed, because it does not
6067 -- constitute a freeze point, but to insure that the freeze node
6068 -- is placed properly, it is created directly when instantiating
6069 -- the body (otherwise the freeze node might appear to early for
6070 -- nested instantiations).
6072 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6073 Rewrite (N, Unit (Parent (N)));
6074 Set_Unit (Parent (N), N);
6075 end if;
6077 -- Replace instance node for library-level instantiations of
6078 -- intrinsic subprograms.
6080 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6081 Rewrite (N, Unit (Parent (N)));
6082 Set_Unit (Parent (N), N);
6083 end if;
6085 if Parent_Installed then
6086 Remove_Parent;
6087 end if;
6089 Restore_Hidden_Primitives (Vis_Prims_List);
6090 Restore_Env;
6091 Env_Installed := False;
6092 Generic_Renamings.Set_Last (0);
6093 Generic_Renamings_HTable.Reset;
6094 end if;
6096 <<Leave>>
6097 -- Analyze aspects in declaration if no errors appear in the instance.
6099 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6100 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6101 end if;
6103 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6104 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6105 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6107 exception
6108 when Instantiation_Error =>
6109 if Parent_Installed then
6110 Remove_Parent;
6111 end if;
6113 if Env_Installed then
6114 Restore_Env;
6115 end if;
6117 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6118 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6119 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6120 end Analyze_Subprogram_Instantiation;
6122 -------------------------
6123 -- Get_Associated_Node --
6124 -------------------------
6126 function Get_Associated_Node (N : Node_Id) return Node_Id is
6127 Assoc : Node_Id;
6129 begin
6130 Assoc := Associated_Node (N);
6132 if Nkind (Assoc) /= Nkind (N) then
6133 return Assoc;
6135 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6136 return Assoc;
6138 else
6139 -- If the node is part of an inner generic, it may itself have been
6140 -- remapped into a further generic copy. Associated_Node is otherwise
6141 -- used for the entity of the node, and will be of a different node
6142 -- kind, or else N has been rewritten as a literal or function call.
6144 while Present (Associated_Node (Assoc))
6145 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6146 loop
6147 Assoc := Associated_Node (Assoc);
6148 end loop;
6150 -- Follow an additional link in case the final node was rewritten.
6151 -- This can only happen with nested generic units.
6153 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6154 and then Present (Associated_Node (Assoc))
6155 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6156 | N_Explicit_Dereference
6157 | N_Integer_Literal
6158 | N_Real_Literal
6159 | N_String_Literal
6160 then
6161 Assoc := Associated_Node (Assoc);
6162 end if;
6164 -- An additional special case: an unconstrained type in an object
6165 -- declaration may have been rewritten as a local subtype constrained
6166 -- by the expression in the declaration. We need to recover the
6167 -- original entity, which may be global.
6169 if Present (Original_Node (Assoc))
6170 and then Nkind (Parent (N)) = N_Object_Declaration
6171 then
6172 Assoc := Original_Node (Assoc);
6173 end if;
6175 return Assoc;
6176 end if;
6177 end Get_Associated_Node;
6179 -----------------------------------
6180 -- Build_Subprogram_Decl_Wrapper --
6181 -----------------------------------
6183 function Build_Subprogram_Decl_Wrapper
6184 (Formal_Subp : Entity_Id) return Node_Id
6186 Loc : constant Source_Ptr := Sloc (Current_Scope);
6187 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6188 Decl : Node_Id;
6189 Subp : Entity_Id;
6190 Parm_Spec : Node_Id;
6191 Profile : List_Id := New_List;
6192 Spec : Node_Id;
6193 Form_F : Entity_Id;
6194 New_F : Entity_Id;
6196 begin
6198 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6199 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6200 Set_Is_Generic_Actual_Subprogram (Subp);
6202 Profile := Parameter_Specifications (
6203 New_Copy_Tree
6204 (Specification (Unit_Declaration_Node (Formal_Subp))));
6206 Form_F := First_Formal (Formal_Subp);
6207 Parm_Spec := First (Profile);
6209 -- Create new entities for the formals. Reset entities so that
6210 -- parameter types are properly resolved when wrapper declaration
6211 -- is analyzed.
6213 while Present (Parm_Spec) loop
6214 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6215 Set_Defining_Identifier (Parm_Spec, New_F);
6216 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6217 Next (Parm_Spec);
6218 Next_Formal (Form_F);
6219 end loop;
6221 if Ret_Type = Standard_Void_Type then
6222 Spec :=
6223 Make_Procedure_Specification (Loc,
6224 Defining_Unit_Name => Subp,
6225 Parameter_Specifications => Profile);
6226 else
6227 Spec :=
6228 Make_Function_Specification (Loc,
6229 Defining_Unit_Name => Subp,
6230 Parameter_Specifications => Profile,
6231 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6232 end if;
6234 Decl :=
6235 Make_Subprogram_Declaration (Loc, Specification => Spec);
6237 return Decl;
6238 end Build_Subprogram_Decl_Wrapper;
6240 -----------------------------------
6241 -- Build_Subprogram_Body_Wrapper --
6242 -----------------------------------
6244 function Build_Subprogram_Body_Wrapper
6245 (Formal_Subp : Entity_Id;
6246 Actual_Name : Node_Id) return Node_Id
6248 Loc : constant Source_Ptr := Sloc (Current_Scope);
6249 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6250 Spec_Node : constant Node_Id :=
6251 Specification
6252 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6253 Act : Node_Id;
6254 Actuals : List_Id;
6255 Body_Node : Node_Id;
6256 Stmt : Node_Id;
6257 begin
6258 Actuals := New_List;
6259 Act := First (Parameter_Specifications (Spec_Node));
6261 while Present (Act) loop
6262 Append_To (Actuals,
6263 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6264 Next (Act);
6265 end loop;
6267 if Ret_Type = Standard_Void_Type then
6268 Stmt := Make_Procedure_Call_Statement (Loc,
6269 Name => Actual_Name,
6270 Parameter_Associations => Actuals);
6272 else
6273 Stmt := Make_Simple_Return_Statement (Loc,
6274 Expression =>
6275 Make_Function_Call (Loc,
6276 Name => Actual_Name,
6277 Parameter_Associations => Actuals));
6278 end if;
6280 Body_Node := Make_Subprogram_Body (Loc,
6281 Specification => Spec_Node,
6282 Declarations => New_List,
6283 Handled_Statement_Sequence =>
6284 Make_Handled_Sequence_Of_Statements (Loc,
6285 Statements => New_List (Stmt)));
6287 return Body_Node;
6288 end Build_Subprogram_Body_Wrapper;
6290 -------------------------------------------
6291 -- Build_Instance_Compilation_Unit_Nodes --
6292 -------------------------------------------
6294 procedure Build_Instance_Compilation_Unit_Nodes
6295 (N : Node_Id;
6296 Act_Body : Node_Id;
6297 Act_Decl : Node_Id)
6299 Decl_Cunit : Node_Id;
6300 Body_Cunit : Node_Id;
6301 Citem : Node_Id;
6302 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6303 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6305 begin
6306 -- A new compilation unit node is built for the instance declaration.
6307 -- It relocates the auxiliary declaration node from the compilation unit
6308 -- where the instance appeared, so that declarations that originally
6309 -- followed the instance will be attached to the spec compilation unit.
6311 Decl_Cunit :=
6312 Make_Compilation_Unit (Sloc (N),
6313 Context_Items => Empty_List,
6314 Unit => Act_Decl,
6315 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6317 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6319 -- The new compilation unit is linked to its body, but both share the
6320 -- same file, so we do not set Body_Required on the new unit so as not
6321 -- to create a spurious dependency on a non-existent body in the ali.
6322 -- This simplifies CodePeer unit traversal.
6324 -- We use the original instantiation compilation unit as the resulting
6325 -- compilation unit of the instance, since this is the main unit.
6327 Rewrite (N, Act_Body);
6329 -- Propagate the aspect specifications from the package body template to
6330 -- the instantiated version of the package body.
6332 if Has_Aspects (Act_Body) then
6333 Set_Aspect_Specifications
6334 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6335 end if;
6337 Body_Cunit := Parent (N);
6339 -- The two compilation unit nodes are linked by the Library_Unit field
6341 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6342 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6344 -- Preserve the private nature of the package if needed
6346 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6348 -- If the instance is not the main unit, its context, categorization
6349 -- and elaboration entity are not relevant to the compilation.
6351 if Body_Cunit /= Cunit (Main_Unit) then
6352 Make_Instance_Unit (Body_Cunit, In_Main => False);
6353 return;
6354 end if;
6356 -- The context clause items on the instantiation, which are now attached
6357 -- to the body compilation unit (since the body overwrote the original
6358 -- instantiation node), semantically belong on the spec, so copy them
6359 -- there. It's harmless to leave them on the body as well. In fact one
6360 -- could argue that they belong in both places.
6362 Citem := First (Context_Items (Body_Cunit));
6363 while Present (Citem) loop
6364 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6365 Next (Citem);
6366 end loop;
6368 -- Propagate categorization flags on packages, so that they appear in
6369 -- the ali file for the spec of the unit.
6371 if Ekind (New_Main) = E_Package then
6372 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6373 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6374 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6375 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6376 Set_Is_Remote_Call_Interface
6377 (Old_Main, Is_Remote_Call_Interface (New_Main));
6378 end if;
6380 -- Make entry in Units table, so that binder can generate call to
6381 -- elaboration procedure for body, if any.
6383 Make_Instance_Unit (Body_Cunit, In_Main => True);
6384 Main_Unit_Entity := New_Main;
6385 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6387 -- Build elaboration entity, since the instance may certainly generate
6388 -- elaboration code requiring a flag for protection.
6390 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6391 end Build_Instance_Compilation_Unit_Nodes;
6393 --------------------------------
6394 -- Check_Abbreviated_Instance --
6395 --------------------------------
6397 procedure Check_Abbreviated_Instance
6398 (N : Node_Id;
6399 Parent_Installed : in out Boolean)
6401 Inst_Node : Node_Id;
6403 begin
6404 if Nkind (N) = N_Package_Specification
6405 and then Is_Abbreviated_Instance (Defining_Entity (N))
6406 then
6407 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6408 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6409 end if;
6410 end Check_Abbreviated_Instance;
6412 -----------------------------
6413 -- Check_Access_Definition --
6414 -----------------------------
6416 procedure Check_Access_Definition (N : Node_Id) is
6417 begin
6418 pragma Assert
6419 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6420 null;
6421 end Check_Access_Definition;
6423 -----------------------------------
6424 -- Check_Formal_Package_Instance --
6425 -----------------------------------
6427 -- If the formal has specific parameters, they must match those of the
6428 -- actual. Both of them are instances, and the renaming declarations for
6429 -- their formal parameters appear in the same order in both. The analyzed
6430 -- formal has been analyzed in the context of the current instance.
6432 procedure Check_Formal_Package_Instance
6433 (Formal_Pack : Entity_Id;
6434 Actual_Pack : Entity_Id)
6436 E1 : Entity_Id := First_Entity (Actual_Pack);
6437 E2 : Entity_Id := First_Entity (Formal_Pack);
6438 Prev_E1 : Entity_Id;
6440 Expr1 : Node_Id;
6441 Expr2 : Node_Id;
6443 procedure Check_Mismatch (B : Boolean);
6444 -- Common error routine for mismatch between the parameters of the
6445 -- actual instance and those of the formal package.
6447 function Is_Defaulted (Param : Entity_Id) return Boolean;
6448 -- If the formal package has partly box-initialized formals, skip
6449 -- conformance check for these formals. Previously the code assumed
6450 -- that box initialization for a formal package applied to all its
6451 -- formal parameters.
6453 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6454 -- The formal may come from a nested formal package, and the actual may
6455 -- have been constant-folded. To determine whether the two denote the
6456 -- same entity we may have to traverse several definitions to recover
6457 -- the ultimate entity that they refer to.
6459 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6460 -- The formal and the actual must be identical, but if both are
6461 -- given by attributes they end up renaming different generated bodies,
6462 -- and we must verify that the attributes themselves match.
6464 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6465 -- Similarly, if the formal comes from a nested formal package, the
6466 -- actual may designate the formal through multiple renamings, which
6467 -- have to be followed to determine the original variable in question.
6469 --------------------
6470 -- Check_Mismatch --
6471 --------------------
6473 procedure Check_Mismatch (B : Boolean) is
6474 -- A Formal_Type_Declaration for a derived private type is rewritten
6475 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6476 -- which is why we examine the original node.
6478 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6480 begin
6481 if Kind = N_Formal_Type_Declaration then
6482 return;
6484 elsif Kind in N_Formal_Object_Declaration
6485 | N_Formal_Package_Declaration
6486 | N_Formal_Subprogram_Declaration
6487 then
6488 null;
6490 -- Ada 2012: If both formal and actual are incomplete types they
6491 -- are conformant.
6493 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6494 null;
6496 elsif B then
6497 Error_Msg_NE
6498 ("actual for & in actual instance does not match formal",
6499 Parent (Actual_Pack), E1);
6500 end if;
6501 end Check_Mismatch;
6503 ------------------
6504 -- Is_Defaulted --
6505 ------------------
6507 function Is_Defaulted (Param : Entity_Id) return Boolean is
6508 Assoc : Node_Id;
6510 begin
6511 Assoc :=
6512 First (Generic_Associations (Parent
6513 (Associated_Formal_Package (Actual_Pack))));
6515 while Present (Assoc) loop
6516 if Nkind (Assoc) = N_Others_Choice then
6517 return True;
6519 elsif Nkind (Assoc) = N_Generic_Association
6520 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6521 then
6522 return Box_Present (Assoc);
6523 end if;
6525 Next (Assoc);
6526 end loop;
6528 return False;
6529 end Is_Defaulted;
6531 --------------------------------
6532 -- Same_Instantiated_Constant --
6533 --------------------------------
6535 function Same_Instantiated_Constant
6536 (E1, E2 : Entity_Id) return Boolean
6538 Ent : Entity_Id;
6540 begin
6541 Ent := E2;
6542 while Present (Ent) loop
6543 if E1 = Ent then
6544 return True;
6546 elsif Ekind (Ent) /= E_Constant then
6547 return False;
6549 elsif Is_Entity_Name (Constant_Value (Ent)) then
6550 if Entity (Constant_Value (Ent)) = E1 then
6551 return True;
6552 else
6553 Ent := Entity (Constant_Value (Ent));
6554 end if;
6556 -- The actual may be a constant that has been folded. Recover
6557 -- original name.
6559 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6560 Ent := Entity (Original_Node (Constant_Value (Ent)));
6562 else
6563 return False;
6564 end if;
6565 end loop;
6567 return False;
6568 end Same_Instantiated_Constant;
6570 --------------------------------
6571 -- Same_Instantiated_Function --
6572 --------------------------------
6574 function Same_Instantiated_Function
6575 (E1, E2 : Entity_Id) return Boolean
6577 U1, U2 : Node_Id;
6578 begin
6579 if Alias (E1) = Alias (E2) then
6580 return True;
6582 elsif Present (Alias (E2)) then
6583 U1 := Original_Node (Unit_Declaration_Node (E1));
6584 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6586 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6587 and then Nkind (Name (U1)) = N_Attribute_Reference
6589 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6590 and then Nkind (Name (U2)) = N_Attribute_Reference
6592 and then
6593 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6594 else
6595 return False;
6596 end if;
6597 end Same_Instantiated_Function;
6599 --------------------------------
6600 -- Same_Instantiated_Variable --
6601 --------------------------------
6603 function Same_Instantiated_Variable
6604 (E1, E2 : Entity_Id) return Boolean
6606 function Original_Entity (E : Entity_Id) return Entity_Id;
6607 -- Follow chain of renamings to the ultimate ancestor
6609 ---------------------
6610 -- Original_Entity --
6611 ---------------------
6613 function Original_Entity (E : Entity_Id) return Entity_Id is
6614 Orig : Entity_Id;
6616 begin
6617 Orig := E;
6618 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6619 and then Present (Renamed_Object (Orig))
6620 and then Is_Entity_Name (Renamed_Object (Orig))
6621 loop
6622 Orig := Entity (Renamed_Object (Orig));
6623 end loop;
6625 return Orig;
6626 end Original_Entity;
6628 -- Start of processing for Same_Instantiated_Variable
6630 begin
6631 return Ekind (E1) = Ekind (E2)
6632 and then Original_Entity (E1) = Original_Entity (E2);
6633 end Same_Instantiated_Variable;
6635 -- Start of processing for Check_Formal_Package_Instance
6637 begin
6638 Prev_E1 := E1;
6639 while Present (E1) and then Present (E2) loop
6640 exit when Ekind (E1) = E_Package
6641 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6643 -- If the formal is the renaming of the formal package, this
6644 -- is the end of its formal part, which may occur before the
6645 -- end of the formal part in the actual in the presence of
6646 -- defaulted parameters in the formal package.
6648 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6649 and then Renamed_Entity (E2) = Scope (E2);
6651 -- The analysis of the actual may generate additional internal
6652 -- entities. If the formal is defaulted, there is no corresponding
6653 -- analysis and the internal entities must be skipped, until we
6654 -- find corresponding entities again.
6656 if Comes_From_Source (E2)
6657 and then not Comes_From_Source (E1)
6658 and then Chars (E1) /= Chars (E2)
6659 then
6660 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6661 Next_Entity (E1);
6662 end loop;
6663 end if;
6665 if No (E1) then
6666 return;
6668 -- Entities may be declared without full declaration, such as
6669 -- itypes and predefined operators (concatenation for arrays, eg).
6670 -- Skip it and keep the formal entity to find a later match for it.
6672 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6673 E1 := Prev_E1;
6674 goto Next_E;
6676 -- If the formal entity comes from a formal declaration, it was
6677 -- defaulted in the formal package, and no check is needed on it.
6679 elsif Nkind (Original_Node (Parent (E2))) in
6680 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6681 then
6682 -- If the formal is a tagged type the corresponding class-wide
6683 -- type has been generated as well, and it must be skipped.
6685 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6686 Next_Entity (E2);
6687 end if;
6689 goto Next_E;
6691 -- Ditto for defaulted formal subprograms.
6693 elsif Is_Overloadable (E1)
6694 and then Nkind (Unit_Declaration_Node (E2)) in
6695 N_Formal_Subprogram_Declaration
6696 then
6697 goto Next_E;
6699 elsif Is_Defaulted (E1) then
6700 goto Next_E;
6702 elsif Is_Type (E1) then
6704 -- Subtypes must statically match. E1, E2 are the local entities
6705 -- that are subtypes of the actuals. Itypes generated for other
6706 -- parameters need not be checked, the check will be performed
6707 -- on the parameters themselves.
6709 -- If E2 is a formal type declaration, it is a defaulted parameter
6710 -- and needs no checking.
6712 if not Is_Itype (E1) and then not Is_Itype (E2) then
6713 Check_Mismatch
6714 (not Is_Type (E2)
6715 or else Etype (E1) /= Etype (E2)
6716 or else not Subtypes_Statically_Match (E1, E2));
6717 end if;
6719 elsif Ekind (E1) = E_Constant then
6721 -- IN parameters must denote the same static value, or the same
6722 -- constant, or the literal null.
6724 Expr1 := Expression (Parent (E1));
6726 if Ekind (E2) /= E_Constant then
6727 Check_Mismatch (True);
6728 goto Next_E;
6729 else
6730 Expr2 := Expression (Parent (E2));
6731 end if;
6733 if Is_OK_Static_Expression (Expr1) then
6734 if not Is_OK_Static_Expression (Expr2) then
6735 Check_Mismatch (True);
6737 elsif Is_Discrete_Type (Etype (E1)) then
6738 declare
6739 V1 : constant Uint := Expr_Value (Expr1);
6740 V2 : constant Uint := Expr_Value (Expr2);
6741 begin
6742 Check_Mismatch (V1 /= V2);
6743 end;
6745 elsif Is_Real_Type (Etype (E1)) then
6746 declare
6747 V1 : constant Ureal := Expr_Value_R (Expr1);
6748 V2 : constant Ureal := Expr_Value_R (Expr2);
6749 begin
6750 Check_Mismatch (V1 /= V2);
6751 end;
6753 elsif Is_String_Type (Etype (E1))
6754 and then Nkind (Expr1) = N_String_Literal
6755 then
6756 if Nkind (Expr2) /= N_String_Literal then
6757 Check_Mismatch (True);
6758 else
6759 Check_Mismatch
6760 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6761 end if;
6762 end if;
6764 elsif Is_Entity_Name (Expr1) then
6765 if Is_Entity_Name (Expr2) then
6766 if Entity (Expr1) = Entity (Expr2) then
6767 null;
6768 else
6769 Check_Mismatch
6770 (not Same_Instantiated_Constant
6771 (Entity (Expr1), Entity (Expr2)));
6772 end if;
6774 else
6775 Check_Mismatch (True);
6776 end if;
6778 elsif Is_Entity_Name (Original_Node (Expr1))
6779 and then Is_Entity_Name (Expr2)
6780 and then Same_Instantiated_Constant
6781 (Entity (Original_Node (Expr1)), Entity (Expr2))
6782 then
6783 null;
6785 elsif Nkind (Expr1) = N_Null then
6786 Check_Mismatch (Nkind (Expr1) /= N_Null);
6788 else
6789 Check_Mismatch (True);
6790 end if;
6792 elsif Ekind (E1) = E_Variable then
6793 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6795 elsif Ekind (E1) = E_Package then
6796 Check_Mismatch
6797 (Ekind (E1) /= Ekind (E2)
6798 or else (Present (Renamed_Entity (E2))
6799 and then Renamed_Entity (E1) /=
6800 Renamed_Entity (E2)));
6802 elsif Is_Overloadable (E1) then
6803 -- Verify that the actual subprograms match. Note that actuals
6804 -- that are attributes are rewritten as subprograms. If the
6805 -- subprogram in the formal package is defaulted, no check is
6806 -- needed. Note that this can only happen in Ada 2005 when the
6807 -- formal package can be partially parameterized.
6809 if Nkind (Unit_Declaration_Node (E1)) =
6810 N_Subprogram_Renaming_Declaration
6811 and then From_Default (Unit_Declaration_Node (E1))
6812 then
6813 null;
6815 -- If the formal package has an "others" box association that
6816 -- covers this formal, there is no need for a check either.
6818 elsif Nkind (Unit_Declaration_Node (E2)) in
6819 N_Formal_Subprogram_Declaration
6820 and then Box_Present (Unit_Declaration_Node (E2))
6821 then
6822 null;
6824 -- No check needed if subprogram is a defaulted null procedure
6826 elsif No (Alias (E2))
6827 and then Ekind (E2) = E_Procedure
6828 and then
6829 Null_Present (Specification (Unit_Declaration_Node (E2)))
6830 then
6831 null;
6833 -- Otherwise the actual in the formal and the actual in the
6834 -- instantiation of the formal must match, up to renamings.
6836 else
6837 Check_Mismatch
6838 (Ekind (E2) /= Ekind (E1)
6839 or else not Same_Instantiated_Function (E1, E2));
6840 end if;
6842 else
6843 raise Program_Error;
6844 end if;
6846 <<Next_E>>
6847 Prev_E1 := E1;
6848 Next_Entity (E1);
6849 Next_Entity (E2);
6850 end loop;
6851 end Check_Formal_Package_Instance;
6853 ---------------------------
6854 -- Check_Formal_Packages --
6855 ---------------------------
6857 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6858 E : Entity_Id;
6859 Formal_P : Entity_Id;
6860 Formal_Decl : Node_Id;
6862 begin
6863 -- Iterate through the declarations in the instance, looking for package
6864 -- renaming declarations that denote instances of formal packages, until
6865 -- we find the renaming of the current package itself. The declaration
6866 -- of a formal package that requires conformance checking is followed by
6867 -- an internal entity that is the abbreviated instance.
6869 E := First_Entity (P_Id);
6870 while Present (E) loop
6871 if Ekind (E) = E_Package then
6872 exit when Renamed_Entity (E) = P_Id;
6874 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6875 Formal_Decl := Parent (Associated_Formal_Package (E));
6877 if Requires_Conformance_Checking (Formal_Decl) then
6878 Formal_P := Next_Entity (E);
6880 -- If the instance is within an enclosing instance body
6881 -- there is no need to verify the legality of current formal
6882 -- packages because they were legal in the generic body.
6883 -- This optimization may be applicable elsewhere, and it
6884 -- also removes spurious errors that may arise with
6885 -- on-the-fly inlining and confusion between private and
6886 -- full views.
6888 if not In_Instance_Body then
6889 Check_Formal_Package_Instance (Formal_P, E);
6890 end if;
6892 -- Restore the visibility of formals of the formal instance
6893 -- that are not defaulted, and are hidden within the current
6894 -- generic. These formals may be visible within an enclosing
6895 -- generic.
6897 declare
6898 Elmt : Elmt_Id;
6899 begin
6900 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6901 while Present (Elmt) loop
6902 Set_Is_Hidden (Node (Elmt), False);
6903 Next_Elmt (Elmt);
6904 end loop;
6905 end;
6907 -- After checking, remove the internal validating package.
6908 -- It is only needed for semantic checks, and as it may
6909 -- contain generic formal declarations it should not reach
6910 -- gigi.
6912 Remove (Unit_Declaration_Node (Formal_P));
6913 end if;
6914 end if;
6915 end if;
6917 Next_Entity (E);
6918 end loop;
6919 end Check_Formal_Packages;
6921 ---------------------------------
6922 -- Check_Forward_Instantiation --
6923 ---------------------------------
6925 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6926 S : Entity_Id;
6927 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6929 begin
6930 -- The instantiation appears before the generic body if we are in the
6931 -- scope of the unit containing the generic, either in its spec or in
6932 -- the package body, and before the generic body.
6934 if Ekind (Gen_Comp) = E_Package_Body then
6935 Gen_Comp := Spec_Entity (Gen_Comp);
6936 end if;
6938 if In_Open_Scopes (Gen_Comp)
6939 and then No (Corresponding_Body (Decl))
6940 then
6941 S := Current_Scope;
6943 while Present (S)
6944 and then not Is_Compilation_Unit (S)
6945 and then not Is_Child_Unit (S)
6946 loop
6947 if Ekind (S) = E_Package then
6948 Set_Has_Forward_Instantiation (S);
6949 end if;
6951 S := Scope (S);
6952 end loop;
6953 end if;
6954 end Check_Forward_Instantiation;
6956 ---------------------------
6957 -- Check_Generic_Actuals --
6958 ---------------------------
6960 -- The visibility of the actuals may be different between the point of
6961 -- generic instantiation and the instantiation of the body.
6963 procedure Check_Generic_Actuals
6964 (Instance : Entity_Id;
6965 Is_Formal_Box : Boolean)
6967 Gen_Id : constant Entity_Id
6968 := (if Is_Generic_Unit (Instance) then
6969 Instance
6970 elsif Is_Wrapper_Package (Instance) then
6971 Generic_Parent
6972 (Specification
6973 (Unit_Declaration_Node (Related_Instance (Instance))))
6974 else
6975 Generic_Parent (Package_Specification (Instance)));
6976 -- The generic unit
6978 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
6979 -- The enclosing scope of the generic unit
6981 procedure Check_Actual_Type (Typ : Entity_Id);
6982 -- If the type of the actual is a private type declared in the
6983 -- enclosing scope of the generic unit, but not a derived type
6984 -- of a private type declared elsewhere, the body of the generic
6985 -- sees the full view of the type (because it has to appear in
6986 -- the corresponding package body). If the type is private now,
6987 -- exchange views to restore the proper visibility in the instance.
6989 -----------------------
6990 -- Check_Actual_Type --
6991 -----------------------
6993 procedure Check_Actual_Type (Typ : Entity_Id) is
6994 Btyp : constant Entity_Id := Base_Type (Typ);
6996 begin
6997 -- The exchange is only needed if the generic is defined
6998 -- within a package which is not a common ancestor of the
6999 -- scope of the instance, and is not already in scope.
7001 if Is_Private_Type (Btyp)
7002 and then Scope (Btyp) = Parent_Scope
7003 and then not Has_Private_Ancestor (Btyp)
7004 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7005 and then Scope (Instance) /= Parent_Scope
7006 and then not Is_Child_Unit (Gen_Id)
7007 then
7008 Switch_View (Btyp);
7010 -- If the type of the entity is a subtype, it may also have
7011 -- to be made visible, together with the base type of its
7012 -- full view, after exchange.
7014 if Is_Private_Type (Typ) then
7015 Switch_View (Typ);
7016 Switch_View (Base_Type (Typ));
7017 end if;
7018 end if;
7019 end Check_Actual_Type;
7021 Astype : Entity_Id;
7022 E : Entity_Id;
7023 Formal : Node_Id;
7025 begin
7026 E := First_Entity (Instance);
7027 while Present (E) loop
7028 if Is_Type (E)
7029 and then Nkind (Parent (E)) = N_Subtype_Declaration
7030 and then Scope (Etype (E)) /= Instance
7031 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7032 then
7033 -- Restore the proper view of the actual from the information
7034 -- saved earlier by Instantiate_Type.
7036 Check_Private_View (Subtype_Indication (Parent (E)));
7038 -- If the actual is itself the formal of a parent instance,
7039 -- then also restore the proper view of its actual and so on.
7040 -- That's necessary for nested instantiations of the form
7042 -- generic
7043 -- type Component is private;
7044 -- type Array_Type is array (Positive range <>) of Component;
7045 -- procedure Proc;
7047 -- when the outermost actuals have inconsistent views, because
7048 -- the Component_Type of Array_Type of the inner instantiations
7049 -- is the actual of Component of the outermost one and not that
7050 -- of the corresponding inner instantiations.
7052 Astype := Ancestor_Subtype (E);
7053 while Present (Astype)
7054 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7055 and then Present (Generic_Parent_Type (Parent (Astype)))
7056 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7057 loop
7058 Check_Private_View (Subtype_Indication (Parent (Astype)));
7059 Astype := Ancestor_Subtype (Astype);
7060 end loop;
7062 Set_Is_Generic_Actual_Type (E);
7064 if Is_Private_Type (E) and then Present (Full_View (E)) then
7065 Set_Is_Generic_Actual_Type (Full_View (E));
7066 end if;
7068 Set_Is_Hidden (E, False);
7069 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7071 -- We constructed the generic actual type as a subtype of the
7072 -- supplied type. This means that it normally would not inherit
7073 -- subtype specific attributes of the actual, which is wrong for
7074 -- the generic case.
7076 Astype := Ancestor_Subtype (E);
7078 if No (Astype) then
7080 -- This can happen when E is an itype that is the full view of
7081 -- a private type completed, e.g. with a constrained array. In
7082 -- that case, use the first subtype, which will carry size
7083 -- information. The base type itself is unconstrained and will
7084 -- not carry it.
7086 Astype := First_Subtype (E);
7087 end if;
7089 Set_Size_Info (E, Astype);
7090 Copy_RM_Size (To => E, From => Astype);
7091 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7093 if Is_Discrete_Or_Fixed_Point_Type (E) then
7094 Set_RM_Size (E, RM_Size (Astype));
7095 end if;
7097 elsif Ekind (E) = E_Package then
7099 -- If this is the renaming for the current instance, we're done.
7100 -- Otherwise it is a formal package. If the corresponding formal
7101 -- was declared with a box, the (instantiations of the) generic
7102 -- formal part are also visible. Otherwise, ignore the entity
7103 -- created to validate the actuals.
7105 if Renamed_Entity (E) = Instance then
7106 exit;
7108 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7109 null;
7111 -- The visibility of a formal of an enclosing generic is already
7112 -- correct.
7114 elsif Denotes_Formal_Package (E) then
7115 null;
7117 elsif Present (Associated_Formal_Package (E))
7118 and then not Is_Generic_Formal (E)
7119 then
7120 Check_Generic_Actuals
7121 (Renamed_Entity (E),
7122 Is_Formal_Box =>
7123 Box_Present (Parent (Associated_Formal_Package (E))));
7125 Set_Is_Hidden (E, False);
7126 end if;
7128 -- If this is a subprogram instance (in a wrapper package) the
7129 -- actual is fully visible.
7131 elsif Is_Wrapper_Package (Instance) then
7132 Set_Is_Hidden (E, False);
7134 -- If the formal package is declared with a box, or if the formal
7135 -- parameter is defaulted, it is visible in the body.
7137 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7138 Set_Is_Hidden (E, False);
7139 end if;
7141 -- Check directly the type of the actual objects
7143 if Ekind (E) in E_Constant | E_Variable then
7144 Check_Actual_Type (Etype (E));
7146 -- As well as the type of formal parameters of actual subprograms
7148 elsif Ekind (E) in E_Function | E_Procedure
7149 and then Is_Generic_Actual_Subprogram (E)
7150 and then Present (Alias (E))
7151 then
7152 Formal := First_Formal (Alias (E));
7153 while Present (Formal) loop
7154 Check_Actual_Type (Etype (Formal));
7155 Next_Formal (Formal);
7156 end loop;
7157 end if;
7159 Next_Entity (E);
7160 end loop;
7161 end Check_Generic_Actuals;
7163 ------------------------------
7164 -- Check_Generic_Child_Unit --
7165 ------------------------------
7167 procedure Check_Generic_Child_Unit
7168 (Gen_Id : Node_Id;
7169 Parent_Installed : in out Boolean)
7171 Loc : constant Source_Ptr := Sloc (Gen_Id);
7172 Gen_Par : Entity_Id := Empty;
7173 E : Entity_Id;
7174 Inst_Par : Entity_Id;
7175 S : Node_Id;
7177 function Find_Generic_Child
7178 (Scop : Entity_Id;
7179 Id : Node_Id) return Entity_Id;
7180 -- Search generic parent for possible child unit with the given name
7182 function In_Enclosing_Instance return Boolean;
7183 -- Within an instance of the parent, the child unit may be denoted by
7184 -- a simple name, or an abbreviated expanded name. Examine enclosing
7185 -- scopes to locate a possible parent instantiation.
7187 ------------------------
7188 -- Find_Generic_Child --
7189 ------------------------
7191 function Find_Generic_Child
7192 (Scop : Entity_Id;
7193 Id : Node_Id) return Entity_Id
7195 E : Entity_Id;
7197 begin
7198 -- If entity of name is already set, instance has already been
7199 -- resolved, e.g. in an enclosing instantiation.
7201 if Present (Entity (Id)) then
7202 if Scope (Entity (Id)) = Scop then
7203 return Entity (Id);
7204 else
7205 return Empty;
7206 end if;
7208 else
7209 E := First_Entity (Scop);
7210 while Present (E) loop
7211 if Chars (E) = Chars (Id)
7212 and then Is_Child_Unit (E)
7213 then
7214 if Is_Child_Unit (E)
7215 and then not Is_Visible_Lib_Unit (E)
7216 then
7217 Error_Msg_NE
7218 ("generic child unit& is not visible", Gen_Id, E);
7219 end if;
7221 Set_Entity (Id, E);
7222 return E;
7223 end if;
7225 Next_Entity (E);
7226 end loop;
7228 return Empty;
7229 end if;
7230 end Find_Generic_Child;
7232 ---------------------------
7233 -- In_Enclosing_Instance --
7234 ---------------------------
7236 function In_Enclosing_Instance return Boolean is
7237 Enclosing_Instance : Node_Id;
7238 Instance_Decl : Node_Id;
7240 begin
7241 -- We do not inline any call that contains instantiations, except
7242 -- for instantiations of Unchecked_Conversion, so if we are within
7243 -- an inlined body the current instance does not require parents.
7245 if In_Inlined_Body then
7246 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7247 return False;
7248 end if;
7250 -- Loop to check enclosing scopes
7252 Enclosing_Instance := Current_Scope;
7253 while Present (Enclosing_Instance) loop
7254 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7256 if Ekind (Enclosing_Instance) = E_Package
7257 and then Is_Generic_Instance (Enclosing_Instance)
7258 and then Present
7259 (Generic_Parent (Specification (Instance_Decl)))
7260 then
7261 -- Check whether the generic we are looking for is a child of
7262 -- this instance.
7264 E := Find_Generic_Child
7265 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7266 exit when Present (E);
7268 else
7269 E := Empty;
7270 end if;
7272 Enclosing_Instance := Scope (Enclosing_Instance);
7273 end loop;
7275 if No (E) then
7277 -- Not a child unit
7279 Analyze (Gen_Id);
7280 return False;
7282 else
7283 Rewrite (Gen_Id,
7284 Make_Expanded_Name (Loc,
7285 Chars => Chars (E),
7286 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7287 Selector_Name => New_Occurrence_Of (E, Loc)));
7289 Set_Entity (Gen_Id, E);
7290 Set_Etype (Gen_Id, Etype (E));
7291 Parent_Installed := False; -- Already in scope.
7292 return True;
7293 end if;
7294 end In_Enclosing_Instance;
7296 -- Start of processing for Check_Generic_Child_Unit
7298 begin
7299 -- If the name of the generic is given by a selected component, it may
7300 -- be the name of a generic child unit, and the prefix is the name of an
7301 -- instance of the parent, in which case the child unit must be visible.
7302 -- If this instance is not in scope, it must be placed there and removed
7303 -- after instantiation, because what is being instantiated is not the
7304 -- original child, but the corresponding child present in the instance
7305 -- of the parent.
7307 -- If the child is instantiated within the parent, it can be given by
7308 -- a simple name. In this case the instance is already in scope, but
7309 -- the child generic must be recovered from the generic parent as well.
7311 if Nkind (Gen_Id) = N_Selected_Component then
7312 S := Selector_Name (Gen_Id);
7313 Analyze (Prefix (Gen_Id));
7314 Inst_Par := Entity (Prefix (Gen_Id));
7316 if Ekind (Inst_Par) = E_Package
7317 and then Present (Renamed_Entity (Inst_Par))
7318 then
7319 Inst_Par := Renamed_Entity (Inst_Par);
7320 end if;
7322 if Ekind (Inst_Par) = E_Package then
7323 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7324 Gen_Par := Generic_Parent (Parent (Inst_Par));
7326 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7327 and then
7328 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7329 then
7330 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7331 end if;
7333 elsif Ekind (Inst_Par) = E_Generic_Package
7334 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7335 then
7336 -- A formal package may be a real child package, and not the
7337 -- implicit instance within a parent. In this case the child is
7338 -- not visible and has to be retrieved explicitly as well.
7340 Gen_Par := Inst_Par;
7341 end if;
7343 if Present (Gen_Par) then
7345 -- The prefix denotes an instantiation. The entity itself may be a
7346 -- nested generic, or a child unit.
7348 E := Find_Generic_Child (Gen_Par, S);
7350 if Present (E) then
7351 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7352 Set_Entity (Gen_Id, E);
7353 Set_Etype (Gen_Id, Etype (E));
7354 Set_Entity (S, E);
7355 Set_Etype (S, Etype (E));
7357 -- Indicate that this is a reference to the parent
7359 if In_Extended_Main_Source_Unit (Gen_Id) then
7360 Set_Is_Instantiated (Inst_Par);
7361 end if;
7363 -- A common mistake is to replicate the naming scheme of a
7364 -- hierarchy by instantiating a generic child directly, rather
7365 -- than the implicit child in a parent instance:
7367 -- generic .. package Gpar is ..
7368 -- generic .. package Gpar.Child is ..
7369 -- package Par is new Gpar ();
7371 -- with Gpar.Child;
7372 -- package Par.Child is new Gpar.Child ();
7373 -- rather than Par.Child
7375 -- In this case the instantiation is within Par, which is an
7376 -- instance, but Gpar does not denote Par because we are not IN
7377 -- the instance of Gpar, so this is illegal. The test below
7378 -- recognizes this particular case.
7380 if Is_Child_Unit (E)
7381 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7382 and then (not In_Instance
7383 or else Nkind (Parent (Parent (Gen_Id))) =
7384 N_Compilation_Unit)
7385 then
7386 Error_Msg_N
7387 ("prefix of generic child unit must be instance of parent",
7388 Gen_Id);
7389 end if;
7391 if not In_Open_Scopes (Inst_Par)
7392 and then Nkind (Parent (Gen_Id)) not in
7393 N_Generic_Renaming_Declaration
7394 then
7395 Install_Parent (Inst_Par);
7396 Parent_Installed := True;
7398 elsif In_Open_Scopes (Inst_Par) then
7400 -- If the parent is already installed, install the actuals
7401 -- for its formal packages. This is necessary when the child
7402 -- instance is a child of the parent instance: in this case,
7403 -- the parent is placed on the scope stack but the formal
7404 -- packages are not made visible.
7406 Install_Formal_Packages (Inst_Par);
7407 end if;
7409 else
7410 -- If the generic parent does not contain an entity that
7411 -- corresponds to the selector, the instance doesn't either.
7412 -- Analyzing the node will yield the appropriate error message.
7413 -- If the entity is not a child unit, then it is an inner
7414 -- generic in the parent.
7416 Analyze (Gen_Id);
7417 end if;
7419 else
7420 Analyze (Gen_Id);
7422 if Is_Child_Unit (Entity (Gen_Id))
7423 and then
7424 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7425 and then not In_Open_Scopes (Inst_Par)
7426 then
7427 Install_Parent (Inst_Par);
7428 Parent_Installed := True;
7430 -- The generic unit may be the renaming of the implicit child
7431 -- present in an instance. In that case the parent instance is
7432 -- obtained from the name of the renamed entity.
7434 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7435 and then Present (Renamed_Entity (Entity (Gen_Id)))
7436 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7437 then
7438 declare
7439 Renamed_Package : constant Node_Id :=
7440 Name (Parent (Entity (Gen_Id)));
7441 begin
7442 if Nkind (Renamed_Package) = N_Expanded_Name then
7443 Inst_Par := Entity (Prefix (Renamed_Package));
7444 Install_Parent (Inst_Par);
7445 Parent_Installed := True;
7446 end if;
7447 end;
7448 end if;
7449 end if;
7451 elsif Nkind (Gen_Id) = N_Expanded_Name then
7453 -- Entity already present, analyze prefix, whose meaning may be an
7454 -- instance in the current context. If it is an instance of a
7455 -- relative within another, the proper parent may still have to be
7456 -- installed, if they are not of the same generation.
7458 Analyze (Prefix (Gen_Id));
7460 -- Prevent cascaded errors
7462 if Etype (Prefix (Gen_Id)) = Any_Type then
7463 return;
7464 end if;
7466 -- In the unlikely case that a local declaration hides the name of
7467 -- the parent package, locate it on the homonym chain. If the context
7468 -- is an instance of the parent, the renaming entity is flagged as
7469 -- such.
7471 Inst_Par := Entity (Prefix (Gen_Id));
7472 while Present (Inst_Par)
7473 and then not Is_Package_Or_Generic_Package (Inst_Par)
7474 loop
7475 Inst_Par := Homonym (Inst_Par);
7476 end loop;
7478 pragma Assert (Present (Inst_Par));
7479 Set_Entity (Prefix (Gen_Id), Inst_Par);
7481 if In_Enclosing_Instance then
7482 null;
7484 elsif Present (Entity (Gen_Id))
7485 and then No (Renamed_Entity (Entity (Gen_Id)))
7486 and then Is_Child_Unit (Entity (Gen_Id))
7487 and then not In_Open_Scopes (Inst_Par)
7488 then
7489 Install_Parent (Inst_Par);
7490 Parent_Installed := True;
7492 -- Handle renaming of generic child unit
7494 elsif Present (Entity (Gen_Id))
7495 and then Present (Renamed_Entity (Entity (Gen_Id)))
7496 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7497 then
7498 declare
7499 E : Entity_Id;
7500 Ren_Decl : Node_Id;
7502 begin
7503 -- The entity of the renamed generic child unit does not
7504 -- have any reference to the instantiated parent. In order to
7505 -- locate it we traverse the scope containing the renaming
7506 -- declaration; the instance of the parent is available in
7507 -- the prefix of the renaming declaration. For example:
7509 -- package A is
7510 -- package Inst_Par is new ...
7511 -- generic package Ren_Child renames Ins_Par.Child;
7512 -- end;
7514 -- with A;
7515 -- package B is
7516 -- package Inst_Child is new A.Ren_Child;
7517 -- end;
7519 E := First_Entity (Entity (Prefix (Gen_Id)));
7520 while Present (E) loop
7521 if not Is_Object (E)
7522 and then Present (Renamed_Entity (E))
7523 and then
7524 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7525 then
7526 Ren_Decl := Parent (E);
7527 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7529 if not In_Open_Scopes (Inst_Par) then
7530 Install_Parent (Inst_Par);
7531 Parent_Installed := True;
7532 end if;
7534 exit;
7535 end if;
7537 E := Next_Entity (E);
7538 end loop;
7539 end;
7540 end if;
7542 elsif In_Enclosing_Instance then
7544 -- The child unit is found in some enclosing scope
7546 null;
7548 else
7549 Analyze (Gen_Id);
7551 -- If this is the renaming of the implicit child in a parent
7552 -- instance, recover the parent name and install it.
7554 if Is_Entity_Name (Gen_Id) then
7555 E := Entity (Gen_Id);
7557 if Is_Generic_Unit (E)
7558 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7559 and then Is_Child_Unit (Renamed_Entity (E))
7560 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7561 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7562 then
7563 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7564 Inst_Par := Entity (Prefix (Gen_Id));
7566 if not In_Open_Scopes (Inst_Par) then
7567 Install_Parent (Inst_Par);
7568 Parent_Installed := True;
7569 end if;
7571 -- If it is a child unit of a non-generic parent, it may be
7572 -- use-visible and given by a direct name. Install parent as
7573 -- for other cases.
7575 elsif Is_Generic_Unit (E)
7576 and then Is_Child_Unit (E)
7577 and then
7578 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7579 and then not Is_Generic_Unit (Scope (E))
7580 then
7581 if not In_Open_Scopes (Scope (E)) then
7582 Install_Parent (Scope (E));
7583 Parent_Installed := True;
7584 end if;
7585 end if;
7586 end if;
7587 end if;
7588 end Check_Generic_Child_Unit;
7590 -----------------------------
7591 -- Check_Hidden_Child_Unit --
7592 -----------------------------
7594 procedure Check_Hidden_Child_Unit
7595 (N : Node_Id;
7596 Gen_Unit : Entity_Id;
7597 Act_Decl_Id : Entity_Id)
7599 Gen_Id : constant Node_Id := Name (N);
7601 begin
7602 if Is_Child_Unit (Gen_Unit)
7603 and then Is_Child_Unit (Act_Decl_Id)
7604 and then Nkind (Gen_Id) = N_Expanded_Name
7605 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7606 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7607 then
7608 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7609 Error_Msg_NE
7610 ("generic unit & is implicitly declared in &",
7611 Defining_Unit_Name (N), Gen_Unit);
7612 Error_Msg_N ("\instance must have different name",
7613 Defining_Unit_Name (N));
7614 end if;
7615 end Check_Hidden_Child_Unit;
7617 ------------------------
7618 -- Check_Private_View --
7619 ------------------------
7621 procedure Check_Private_View (N : Node_Id) is
7622 T : constant Entity_Id := Etype (N);
7623 BT : Entity_Id;
7625 begin
7626 -- Exchange views if the type was not private in the generic but is
7627 -- private at the point of instantiation. Do not exchange views if
7628 -- the scope of the type is in scope. This can happen if both generic
7629 -- and instance are sibling units, or if type is defined in a parent.
7630 -- In this case the visibility of the type will be correct for all
7631 -- semantic checks.
7633 if Present (T) then
7634 BT := Base_Type (T);
7636 if Is_Private_Type (T)
7637 and then not Has_Private_View (N)
7638 and then Present (Full_View (T))
7639 and then not In_Open_Scopes (Scope (T))
7640 then
7641 -- In the generic, the full declaration was visible
7643 Switch_View (T);
7645 elsif Has_Private_View (N)
7646 and then not Is_Private_Type (T)
7647 and then not Has_Been_Exchanged (T)
7648 and then (not In_Open_Scopes (Scope (T))
7649 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7650 then
7651 -- In the generic, only the private declaration was visible
7653 -- If the type appears in a subtype declaration, the subtype in
7654 -- instance must have a view compatible with that of its parent,
7655 -- which must be exchanged (see corresponding code in Restore_
7656 -- Private_Views) so we make an exception to the open scope rule.
7658 Prepend_Elmt (T, Exchanged_Views);
7659 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7661 -- Finally, a non-private subtype may have a private base type, which
7662 -- must be exchanged for consistency. This can happen when a package
7663 -- body is instantiated, when the scope stack is empty but in fact
7664 -- the subtype and the base type are declared in an enclosing scope.
7666 -- Note that in this case we introduce an inconsistency in the view
7667 -- set, because we switch the base type BT, but there could be some
7668 -- private dependent subtypes of BT which remain unswitched. Such
7669 -- subtypes might need to be switched at a later point (see specific
7670 -- provision for that case in Switch_View).
7672 elsif not Is_Private_Type (T)
7673 and then not Has_Private_View (N)
7674 and then Is_Private_Type (BT)
7675 and then Present (Full_View (BT))
7676 and then not Is_Generic_Type (BT)
7677 and then not In_Open_Scopes (BT)
7678 then
7679 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7680 Exchange_Declarations (BT);
7681 end if;
7682 end if;
7683 end Check_Private_View;
7685 -----------------------------
7686 -- Check_Hidden_Primitives --
7687 -----------------------------
7689 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7690 Actual : Node_Id;
7691 Gen_T : Entity_Id;
7692 Result : Elist_Id := No_Elist;
7694 begin
7695 if No (Assoc_List) then
7696 return No_Elist;
7697 end if;
7699 -- Traverse the list of associations between formals and actuals
7700 -- searching for renamings of tagged types
7702 Actual := First (Assoc_List);
7703 while Present (Actual) loop
7704 if Nkind (Actual) = N_Subtype_Declaration then
7705 Gen_T := Generic_Parent_Type (Actual);
7707 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7709 -- Traverse the list of primitives of the actual types
7710 -- searching for hidden primitives that are visible in the
7711 -- corresponding generic formal; leave them visible and
7712 -- append them to Result to restore their decoration later.
7714 Install_Hidden_Primitives
7715 (Prims_List => Result,
7716 Gen_T => Gen_T,
7717 Act_T => Entity (Subtype_Indication (Actual)));
7718 end if;
7719 end if;
7721 Next (Actual);
7722 end loop;
7724 return Result;
7725 end Check_Hidden_Primitives;
7727 --------------------------
7728 -- Contains_Instance_Of --
7729 --------------------------
7731 function Contains_Instance_Of
7732 (Inner : Entity_Id;
7733 Outer : Entity_Id;
7734 N : Node_Id) return Boolean
7736 Elmt : Elmt_Id;
7737 Scop : Entity_Id;
7739 begin
7740 Scop := Outer;
7742 -- Verify that there are no circular instantiations. We check whether
7743 -- the unit contains an instance of the current scope or some enclosing
7744 -- scope (in case one of the instances appears in a subunit). Longer
7745 -- circularities involving subunits might seem too pathological to
7746 -- consider, but they were not too pathological for the authors of
7747 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7748 -- enclosing generic scopes as containing an instance.
7750 loop
7751 -- Within a generic subprogram body, the scope is not generic, to
7752 -- allow for recursive subprograms. Use the declaration to determine
7753 -- whether this is a generic unit.
7755 if Ekind (Scop) = E_Generic_Package
7756 or else (Is_Subprogram (Scop)
7757 and then Nkind (Unit_Declaration_Node (Scop)) =
7758 N_Generic_Subprogram_Declaration)
7759 then
7760 Elmt := First_Elmt (Inner_Instances (Inner));
7762 while Present (Elmt) loop
7763 if Node (Elmt) = Scop then
7764 Error_Msg_Node_2 := Inner;
7765 Error_Msg_NE
7766 ("circular instantiation: & instantiated within &!",
7767 N, Scop);
7768 return True;
7770 elsif Node (Elmt) = Inner then
7771 return True;
7773 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7774 Error_Msg_Node_2 := Inner;
7775 Error_Msg_NE
7776 ("circular instantiation: & instantiated within &!",
7777 N, Node (Elmt));
7778 return True;
7779 end if;
7781 Next_Elmt (Elmt);
7782 end loop;
7784 -- Indicate that Inner is being instantiated within Scop
7786 Append_Elmt (Inner, Inner_Instances (Scop));
7787 end if;
7789 if Scop = Standard_Standard then
7790 exit;
7791 else
7792 Scop := Scope (Scop);
7793 end if;
7794 end loop;
7796 return False;
7797 end Contains_Instance_Of;
7799 -----------------------
7800 -- Copy_Generic_Node --
7801 -----------------------
7803 function Copy_Generic_Node
7804 (N : Node_Id;
7805 Parent_Id : Node_Id;
7806 Instantiating : Boolean) return Node_Id
7808 Ent : Entity_Id;
7809 New_N : Node_Id;
7811 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7812 -- Check the given value of one of the Fields referenced by the current
7813 -- node to determine whether to copy it recursively. The field may hold
7814 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7815 -- Char) in which case it need not be copied.
7817 procedure Copy_Descendants;
7818 -- Common utility for various nodes
7820 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7821 -- Make copy of element list
7823 function Copy_Generic_List
7824 (L : List_Id;
7825 Parent_Id : Node_Id) return List_Id;
7826 -- Apply Copy_Generic_Node recursively to the members of a node list
7828 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7829 -- True if an identifier is part of the defining program unit name of
7830 -- a child unit.
7831 -- Consider removing this subprogram now that ASIS no longer uses it.
7833 ----------------------
7834 -- Copy_Descendants --
7835 ----------------------
7837 procedure Copy_Descendants is
7838 procedure Walk is new
7839 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7840 begin
7841 Walk (New_N, N);
7842 end Copy_Descendants;
7844 -----------------------------
7845 -- Copy_Generic_Descendant --
7846 -----------------------------
7848 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7849 begin
7850 if D = Union_Id (Empty) then
7851 return D;
7853 elsif D in Node_Range then
7854 return Union_Id
7855 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7857 elsif D in List_Range then
7858 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7860 elsif D in Elist_Range then
7861 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7863 -- Nothing else is copyable (e.g. Uint values), return as is
7865 else
7866 return D;
7867 end if;
7868 end Copy_Generic_Descendant;
7870 ------------------------
7871 -- Copy_Generic_Elist --
7872 ------------------------
7874 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7875 M : Elmt_Id;
7876 L : Elist_Id;
7878 begin
7879 if Present (E) then
7880 L := New_Elmt_List;
7881 M := First_Elmt (E);
7882 while Present (M) loop
7883 Append_Elmt
7884 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7885 Next_Elmt (M);
7886 end loop;
7888 return L;
7890 else
7891 return No_Elist;
7892 end if;
7893 end Copy_Generic_Elist;
7895 -----------------------
7896 -- Copy_Generic_List --
7897 -----------------------
7899 function Copy_Generic_List
7900 (L : List_Id;
7901 Parent_Id : Node_Id) return List_Id
7903 N : Node_Id;
7904 New_L : List_Id;
7906 begin
7907 if Present (L) then
7908 New_L := New_List;
7909 Set_Parent (New_L, Parent_Id);
7911 N := First (L);
7912 while Present (N) loop
7913 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7914 Next (N);
7915 end loop;
7917 return New_L;
7919 else
7920 return No_List;
7921 end if;
7922 end Copy_Generic_List;
7924 ---------------------------
7925 -- In_Defining_Unit_Name --
7926 ---------------------------
7928 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7929 begin
7930 return
7931 Present (Parent (Nam))
7932 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7933 or else
7934 (Nkind (Parent (Nam)) = N_Expanded_Name
7935 and then In_Defining_Unit_Name (Parent (Nam))));
7936 end In_Defining_Unit_Name;
7938 -- Start of processing for Copy_Generic_Node
7940 begin
7941 if N = Empty then
7942 return N;
7943 end if;
7945 New_N := New_Copy (N);
7947 -- Copy aspects if present
7949 if Has_Aspects (N) then
7950 Set_Has_Aspects (New_N, False);
7951 Set_Aspect_Specifications
7952 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7953 end if;
7955 -- If we are instantiating, we want to adjust the sloc based on the
7956 -- current S_Adjustment. However, if this is the root node of a subunit,
7957 -- we need to defer that adjustment to below (see "elsif Instantiating
7958 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7959 -- computed the adjustment.
7961 if Instantiating
7962 and then not (Nkind (N) in N_Proper_Body
7963 and then Was_Originally_Stub (N))
7964 then
7965 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7966 end if;
7968 if not Is_List_Member (N) then
7969 Set_Parent (New_N, Parent_Id);
7970 end if;
7972 -- Special casing for identifiers and other entity names and operators
7974 if Nkind (N) in N_Character_Literal
7975 | N_Expanded_Name
7976 | N_Identifier
7977 | N_Operator_Symbol
7978 | N_Op
7979 then
7980 if not Instantiating then
7982 -- Link both nodes in order to assign subsequently the entity of
7983 -- the copy to the original node, in case this is a global
7984 -- reference.
7986 Set_Associated_Node (N, New_N);
7988 -- If we are within an instantiation, this is a nested generic
7989 -- that has already been analyzed at the point of definition.
7990 -- We must preserve references that were global to the enclosing
7991 -- parent at that point. Other occurrences, whether global or
7992 -- local to the current generic, must be resolved anew, so we
7993 -- reset the entity in the generic copy. A global reference has a
7994 -- smaller depth than the parent, or else the same depth in case
7995 -- both are distinct compilation units.
7997 -- A child unit is implicitly declared within the enclosing parent
7998 -- but is in fact global to it, and must be preserved.
8000 -- It is also possible for Current_Instantiated_Parent to be
8001 -- defined, and for this not to be a nested generic, namely if
8002 -- the unit is loaded through Rtsfind. In that case, the entity of
8003 -- New_N is only a link to the associated node, and not a defining
8004 -- occurrence.
8006 -- The entities for parent units in the defining_program_unit of a
8007 -- generic child unit are established when the context of the unit
8008 -- is first analyzed, before the generic copy is made. They are
8009 -- preserved in the copy for use in e.g. ASIS queries.
8011 Ent := Entity (New_N);
8013 if No (Current_Instantiated_Parent.Gen_Id) then
8014 if No (Ent)
8015 or else Nkind (Ent) /= N_Defining_Identifier
8016 or else not In_Defining_Unit_Name (N)
8017 then
8018 Set_Associated_Node (New_N, Empty);
8019 end if;
8021 elsif No (Ent)
8022 or else Nkind (Ent) not in N_Entity
8023 or else No (Scope (Ent))
8024 or else
8025 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8026 and then not Is_Child_Unit (Ent))
8027 or else
8028 (Scope_Depth_Set (Scope (Ent))
8029 and then
8030 Scope_Depth (Scope (Ent)) >
8031 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8032 and then
8033 Get_Source_Unit (Ent) =
8034 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8035 then
8036 Set_Associated_Node (New_N, Empty);
8037 end if;
8039 -- Case of instantiating identifier or some other name or operator
8041 else
8042 -- If the associated node is still defined, the entity in it
8043 -- is global, and must be copied to the instance. If this copy
8044 -- is being made for a body to inline, it is applied to an
8045 -- instantiated tree, and the entity is already present and
8046 -- must be also preserved.
8048 declare
8049 Assoc : constant Node_Id := Get_Associated_Node (N);
8051 begin
8052 if Present (Assoc) then
8053 if Nkind (Assoc) = Nkind (N) then
8054 Set_Entity (New_N, Entity (Assoc));
8055 Check_Private_View (N);
8057 -- Here we deal with a very peculiar case for which the
8058 -- Has_Private_View mechanism is not sufficient, because
8059 -- the reference to the type is implicit in the tree,
8060 -- that is to say, it's not referenced from a node but
8061 -- only from another type, namely through Component_Type.
8063 -- package P is
8065 -- type Pt is private;
8067 -- generic
8068 -- type Ft is array (Positive range <>) of Pt;
8069 -- package G is
8070 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8071 -- end G;
8073 -- private
8074 -- type Pt is new Boolean;
8075 -- end P;
8077 -- package body P is
8078 -- package body G is
8079 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8080 -- begin
8081 -- if (F1 < F2) /= Lt then
8082 -- null;
8083 -- end if;
8084 -- end Check;
8085 -- end G;
8086 -- end P;
8088 -- type Arr is array (Positive range <>) of P.Pt;
8090 -- package Inst is new P.G (Arr);
8092 -- Pt is a global type for the generic package G and it
8093 -- is not referenced in its body, but only as component
8094 -- type of Ft, which is a local type. This means that no
8095 -- references to Pt or Ft are seen during the copy of the
8096 -- body, the only reference to Pt being seen is when the
8097 -- actuals are checked by Check_Generic_Actuals, but Pt
8098 -- is still private at this point. In the end, the views
8099 -- of Pt are not switched in the body and, therefore, the
8100 -- array comparison is rejected because the component is
8101 -- still private.
8103 -- Adding e.g. a dummy variable of type Pt in the body is
8104 -- sufficient to make everything work, so we generate an
8105 -- artificial reference to Pt on the fly and thus force
8106 -- the switching of views on the grounds that, if the
8107 -- comparison was accepted during the semantic analysis
8108 -- of the generic, this means that the component cannot
8109 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8111 if Nkind (Assoc) in N_Op_Compare
8112 and then Present (Etype (Left_Opnd (Assoc)))
8113 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8114 and then Present (Etype (Right_Opnd (Assoc)))
8115 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8116 then
8117 declare
8118 Ltyp : constant Entity_Id :=
8119 Etype (Left_Opnd (Assoc));
8120 Rtyp : constant Entity_Id :=
8121 Etype (Right_Opnd (Assoc));
8122 begin
8123 if Is_Private_Type (Component_Type (Ltyp)) then
8124 Check_Private_View
8125 (New_Occurrence_Of (Component_Type (Ltyp),
8126 Sloc (N)));
8127 end if;
8128 if Is_Private_Type (Component_Type (Rtyp)) then
8129 Check_Private_View
8130 (New_Occurrence_Of (Component_Type (Rtyp),
8131 Sloc (N)));
8132 end if;
8133 end;
8135 -- Here is a similar case, for the Designated_Type of an
8136 -- access type that is present as target type in a type
8137 -- conversion from another access type. In this case, if
8138 -- the base types of the designated types are different
8139 -- and the conversion was accepted during the semantic
8140 -- analysis of the generic, this means that the target
8141 -- type cannot have been private (see Valid_Conversion).
8143 elsif Nkind (Assoc) = N_Identifier
8144 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8145 and then Subtype_Mark (Parent (Assoc)) = Assoc
8146 and then Present (Etype (Assoc))
8147 and then Is_Access_Type (Etype (Assoc))
8148 and then Present (Etype (Expression (Parent (Assoc))))
8149 and then
8150 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8151 then
8152 declare
8153 Targ_Desig : constant Entity_Id :=
8154 Designated_Type (Etype (Assoc));
8155 Expr_Desig : constant Entity_Id :=
8156 Designated_Type
8157 (Etype (Expression (Parent (Assoc))));
8158 begin
8159 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8160 and then Is_Private_Type (Targ_Desig)
8161 then
8162 Check_Private_View
8163 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8164 end if;
8165 end;
8166 end if;
8168 -- The node is a reference to a global type and acts as the
8169 -- subtype mark of a qualified expression created in order
8170 -- to aid resolution of accidental overloading in instances.
8171 -- Since N is a reference to a type, the Associated_Node of
8172 -- N denotes an entity rather than another identifier. See
8173 -- Qualify_Universal_Operands for details.
8175 elsif Nkind (N) = N_Identifier
8176 and then Nkind (Parent (N)) = N_Qualified_Expression
8177 and then Subtype_Mark (Parent (N)) = N
8178 and then Is_Qualified_Universal_Literal (Parent (N))
8179 then
8180 Set_Entity (New_N, Assoc);
8182 -- Cope with the rewriting into expanded name that may have
8183 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8184 -- generic renaming declarations.
8186 elsif Nkind (Assoc) = N_Expanded_Name then
8187 Rewrite (N, New_Copy_Tree (Assoc));
8188 Set_Associated_Node (N, Assoc);
8189 return Copy_Generic_Node (N, Parent_Id, Instantiating);
8191 -- The name in the call may be a selected component if the
8192 -- call has not been analyzed yet, as may be the case for
8193 -- pre/post conditions in a generic unit.
8195 elsif Nkind (Assoc) = N_Function_Call
8196 and then Is_Entity_Name (Name (Assoc))
8197 then
8198 Set_Entity (New_N, Entity (Name (Assoc)));
8199 Check_Private_View (N);
8201 elsif Nkind (Assoc) in N_Entity
8202 and then (Expander_Active
8203 or else (GNATprove_Mode
8204 and then not In_Spec_Expression
8205 and then not Inside_A_Generic))
8206 then
8207 -- Inlining case: we are copying a tree that contains
8208 -- global entities, which are preserved in the copy to be
8209 -- used for subsequent inlining.
8211 null;
8213 else
8214 Set_Entity (New_N, Empty);
8215 end if;
8216 end if;
8217 end;
8218 end if;
8220 -- For expanded name, we must copy the Prefix and Selector_Name
8222 if Nkind (N) = N_Expanded_Name then
8223 Set_Prefix
8224 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8226 Set_Selector_Name (New_N,
8227 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8229 -- For operators, copy the operands
8231 elsif Nkind (N) in N_Op then
8232 if Nkind (N) in N_Binary_Op then
8233 Set_Left_Opnd (New_N,
8234 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8235 end if;
8237 Set_Right_Opnd (New_N,
8238 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8239 end if;
8241 -- Establish a link between an entity from the generic template and the
8242 -- corresponding entity in the generic copy to be analyzed.
8244 elsif Nkind (N) in N_Entity then
8245 if not Instantiating then
8246 Set_Associated_Entity (N, New_N);
8247 end if;
8249 -- Clear any existing link the copy may inherit from the replicated
8250 -- generic template entity.
8252 Set_Associated_Entity (New_N, Empty);
8254 -- Special casing for stubs
8256 elsif Nkind (N) in N_Body_Stub then
8258 -- In any case, we must copy the specification or defining
8259 -- identifier as appropriate.
8261 if Nkind (N) = N_Subprogram_Body_Stub then
8262 Set_Specification (New_N,
8263 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8265 else
8266 Set_Defining_Identifier (New_N,
8267 Copy_Generic_Node
8268 (Defining_Identifier (N), New_N, Instantiating));
8269 end if;
8271 -- If we are not instantiating, then this is where we load and
8272 -- analyze subunits, i.e. at the point where the stub occurs. A
8273 -- more permissive system might defer this analysis to the point
8274 -- of instantiation, but this seems too complicated for now.
8276 if not Instantiating then
8277 declare
8278 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8279 Subunit : Node_Id;
8280 Unum : Unit_Number_Type;
8281 New_Body : Node_Id;
8283 begin
8284 -- Make sure that, if it is a subunit of the main unit that is
8285 -- preprocessed and if -gnateG is specified, the preprocessed
8286 -- file will be written.
8288 Lib.Analysing_Subunit_Of_Main :=
8289 Lib.In_Extended_Main_Source_Unit (N);
8290 Unum :=
8291 Load_Unit
8292 (Load_Name => Subunit_Name,
8293 Required => False,
8294 Subunit => True,
8295 Error_Node => N);
8296 Lib.Analysing_Subunit_Of_Main := False;
8298 -- If the proper body is not found, a warning message will be
8299 -- emitted when analyzing the stub, or later at the point of
8300 -- instantiation. Here we just leave the stub as is.
8302 if Unum = No_Unit then
8303 Subunits_Missing := True;
8304 goto Subunit_Not_Found;
8305 end if;
8307 Subunit := Cunit (Unum);
8309 if Nkind (Unit (Subunit)) /= N_Subunit then
8310 Error_Msg_N
8311 ("found child unit instead of expected SEPARATE subunit",
8312 Subunit);
8313 Error_Msg_Sloc := Sloc (N);
8314 Error_Msg_N ("\to complete stub #", Subunit);
8315 goto Subunit_Not_Found;
8316 end if;
8318 -- We must create a generic copy of the subunit, in order to
8319 -- perform semantic analysis on it, and we must replace the
8320 -- stub in the original generic unit with the subunit, in order
8321 -- to preserve non-local references within.
8323 -- Only the proper body needs to be copied. Library_Unit and
8324 -- context clause are simply inherited by the generic copy.
8325 -- Note that the copy (which may be recursive if there are
8326 -- nested subunits) must be done first, before attaching it to
8327 -- the enclosing generic.
8329 New_Body :=
8330 Copy_Generic_Node
8331 (Proper_Body (Unit (Subunit)),
8332 Empty, Instantiating => False);
8334 -- Now place the original proper body in the original generic
8335 -- unit. This is a body, not a compilation unit.
8337 Rewrite (N, Proper_Body (Unit (Subunit)));
8338 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8339 Set_Was_Originally_Stub (N);
8341 -- Finally replace the body of the subunit with its copy, and
8342 -- make this new subunit into the library unit of the generic
8343 -- copy, which does not have stubs any longer.
8345 Set_Proper_Body (Unit (Subunit), New_Body);
8346 Set_Library_Unit (New_N, Subunit);
8347 Inherit_Context (Unit (Subunit), N);
8348 end;
8350 -- If we are instantiating, this must be an error case, since
8351 -- otherwise we would have replaced the stub node by the proper body
8352 -- that corresponds. So just ignore it in the copy (i.e. we have
8353 -- copied it, and that is good enough).
8355 else
8356 null;
8357 end if;
8359 <<Subunit_Not_Found>> null;
8361 -- If the node is a compilation unit, it is the subunit of a stub, which
8362 -- has been loaded already (see code below). In this case, the library
8363 -- unit field of N points to the parent unit (which is a compilation
8364 -- unit) and need not (and cannot) be copied.
8366 -- When the proper body of the stub is analyzed, the library_unit link
8367 -- is used to establish the proper context (see sem_ch10).
8369 -- The other fields of a compilation unit are copied as usual
8371 elsif Nkind (N) = N_Compilation_Unit then
8373 -- This code can only be executed when not instantiating, because in
8374 -- the copy made for an instantiation, the compilation unit node has
8375 -- disappeared at the point that a stub is replaced by its proper
8376 -- body.
8378 pragma Assert (not Instantiating);
8380 Set_Context_Items (New_N,
8381 Copy_Generic_List (Context_Items (N), New_N));
8383 Set_Unit (New_N,
8384 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8386 Set_First_Inlined_Subprogram (New_N,
8387 Copy_Generic_Node
8388 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8390 Set_Aux_Decls_Node
8391 (New_N,
8392 Copy_Generic_Node
8393 (Aux_Decls_Node (N), New_N, Instantiating => False));
8395 -- For an assignment node, the assignment is known to be semantically
8396 -- legal if we are instantiating the template. This avoids incorrect
8397 -- diagnostics in generated code.
8399 elsif Nkind (N) = N_Assignment_Statement then
8401 -- Copy name and expression fields in usual manner
8403 Set_Name (New_N,
8404 Copy_Generic_Node (Name (N), New_N, Instantiating));
8406 Set_Expression (New_N,
8407 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8409 if Instantiating then
8410 Set_Assignment_OK (Name (New_N), True);
8411 end if;
8413 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8414 if not Instantiating then
8415 Set_Associated_Node (N, New_N);
8417 else
8418 if Present (Get_Associated_Node (N))
8419 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8420 then
8421 -- In the generic the aggregate has some composite type. If at
8422 -- the point of instantiation the type has a private view,
8423 -- install the full view (and that of its ancestors, if any).
8425 declare
8426 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8427 Rt : Entity_Id;
8429 begin
8430 if Present (T) and then Is_Private_Type (T) then
8431 Switch_View (T);
8432 end if;
8434 if Present (T)
8435 and then Is_Tagged_Type (T)
8436 and then Is_Derived_Type (T)
8437 then
8438 Rt := Root_Type (T);
8440 loop
8441 T := Etype (T);
8443 if Is_Private_Type (T) then
8444 Switch_View (T);
8445 end if;
8447 exit when T = Rt;
8448 end loop;
8449 end if;
8450 end;
8451 end if;
8452 end if;
8454 -- Do not copy the associated node, which points to the generic copy
8455 -- of the aggregate.
8457 if Nkind (N) = N_Aggregate then
8458 Set_Aggregate_Bounds
8459 (New_N,
8460 Node_Id (Copy_Generic_Descendant
8461 (Union_Id (Aggregate_Bounds (N)))));
8463 elsif Nkind (N) = N_Extension_Aggregate then
8464 Set_Ancestor_Part
8465 (New_N,
8466 Node_Id (Copy_Generic_Descendant
8467 (Union_Id (Ancestor_Part (N)))));
8469 else
8470 pragma Assert (False);
8471 end if;
8473 Set_Expressions
8474 (New_N,
8475 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8476 Set_Component_Associations
8477 (New_N,
8478 List_Id (Copy_Generic_Descendant
8479 (Union_Id (Component_Associations (N)))));
8480 Set_Etype
8481 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8483 -- Allocators do not have an identifier denoting the access type, so we
8484 -- must locate it through the expression to check whether the views are
8485 -- consistent.
8487 elsif Nkind (N) = N_Allocator
8488 and then Nkind (Expression (N)) = N_Qualified_Expression
8489 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8490 and then Instantiating
8491 then
8492 declare
8493 T : constant Node_Id :=
8494 Get_Associated_Node (Subtype_Mark (Expression (N)));
8495 Acc_T : Entity_Id;
8497 begin
8498 if Present (T) then
8500 -- Retrieve the allocator node in the generic copy
8502 Acc_T := Etype (Parent (Parent (T)));
8504 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8505 Switch_View (Acc_T);
8506 end if;
8507 end if;
8509 Copy_Descendants;
8510 end;
8512 -- For a proper body, we must catch the case of a proper body that
8513 -- replaces a stub. This represents the point at which a separate
8514 -- compilation unit, and hence template file, may be referenced, so we
8515 -- must make a new source instantiation entry for the template of the
8516 -- subunit, and ensure that all nodes in the subunit are adjusted using
8517 -- this new source instantiation entry.
8519 elsif Nkind (N) in N_Proper_Body then
8520 declare
8521 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8522 begin
8523 if Instantiating and then Was_Originally_Stub (N) then
8524 Create_Instantiation_Source
8525 (Instantiation_Node,
8526 Defining_Entity (N),
8527 S_Adjustment);
8529 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8530 end if;
8532 -- Now copy the fields of the proper body, using the new
8533 -- adjustment factor if one was needed as per test above.
8535 Copy_Descendants;
8537 -- Restore the original adjustment factor
8539 S_Adjustment := Save_Adjustment;
8540 end;
8542 elsif Nkind (N) = N_Pragma and then Instantiating then
8544 -- Do not copy Comment or Ident pragmas their content is relevant to
8545 -- the generic unit, not to the instantiating unit.
8547 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8548 New_N := Make_Null_Statement (Sloc (N));
8550 -- Do not copy pragmas generated from aspects because the pragmas do
8551 -- not carry any semantic information, plus they will be regenerated
8552 -- in the instance.
8554 -- However, generating C we need to copy them since postconditions
8555 -- are inlined by the front end, and the front-end inlining machinery
8556 -- relies on this routine to perform inlining.
8558 elsif From_Aspect_Specification (N)
8559 and then not Modify_Tree_For_C
8560 then
8561 New_N := Make_Null_Statement (Sloc (N));
8563 else
8564 Copy_Descendants;
8565 end if;
8567 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8569 -- No descendant fields need traversing
8571 null;
8573 elsif Nkind (N) = N_String_Literal
8574 and then Present (Etype (N))
8575 and then Instantiating
8576 then
8577 -- If the string is declared in an outer scope, the string_literal
8578 -- subtype created for it may have the wrong scope. Force reanalysis
8579 -- of the constant to generate a new itype in the proper context.
8581 Set_Etype (New_N, Empty);
8582 Set_Analyzed (New_N, False);
8584 -- For the remaining nodes, copy their descendants recursively
8586 else
8587 Copy_Descendants;
8589 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8590 Set_Generic_Parent (Specification (New_N), N);
8592 -- Should preserve Corresponding_Spec??? (12.3(14))
8593 end if;
8594 end if;
8596 -- Propagate dimensions if present, so that they are reflected in the
8597 -- instance.
8599 if Nkind (N) in N_Has_Etype
8600 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8601 and then Present (Etype (N))
8602 and then Is_Floating_Point_Type (Etype (N))
8603 and then Has_Dimension_System (Etype (N))
8604 then
8605 Copy_Dimensions (N, New_N);
8606 end if;
8608 return New_N;
8609 end Copy_Generic_Node;
8611 ----------------------------
8612 -- Denotes_Formal_Package --
8613 ----------------------------
8615 function Denotes_Formal_Package
8616 (Pack : Entity_Id;
8617 On_Exit : Boolean := False;
8618 Instance : Entity_Id := Empty) return Boolean
8620 Par : Entity_Id;
8621 Scop : constant Entity_Id := Scope (Pack);
8622 E : Entity_Id;
8624 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8625 -- The package in question may be an actual for a previous formal
8626 -- package P of the current instance, so examine its actuals as well.
8627 -- This must be recursive over other formal packages.
8629 ----------------------------------
8630 -- Is_Actual_Of_Previous_Formal --
8631 ----------------------------------
8633 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8634 E1 : Entity_Id;
8636 begin
8637 E1 := First_Entity (P);
8638 while Present (E1) and then E1 /= Instance loop
8639 if Ekind (E1) = E_Package
8640 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8641 then
8642 if Renamed_Entity (E1) = Pack then
8643 return True;
8645 elsif E1 = P or else Renamed_Entity (E1) = P then
8646 return False;
8648 elsif Is_Actual_Of_Previous_Formal (E1) then
8649 return True;
8650 end if;
8651 end if;
8653 Next_Entity (E1);
8654 end loop;
8656 return False;
8657 end Is_Actual_Of_Previous_Formal;
8659 -- Start of processing for Denotes_Formal_Package
8661 begin
8662 if On_Exit then
8663 Par :=
8664 Instance_Envs.Table
8665 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8666 else
8667 Par := Current_Instantiated_Parent.Act_Id;
8668 end if;
8670 if Ekind (Scop) = E_Generic_Package
8671 or else Nkind (Unit_Declaration_Node (Scop)) =
8672 N_Generic_Subprogram_Declaration
8673 then
8674 return True;
8676 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8677 N_Formal_Package_Declaration
8678 then
8679 return True;
8681 elsif No (Par) then
8682 return False;
8684 else
8685 -- Check whether this package is associated with a formal package of
8686 -- the enclosing instantiation. Iterate over the list of renamings.
8688 E := First_Entity (Par);
8689 while Present (E) loop
8690 if Ekind (E) /= E_Package
8691 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8692 then
8693 null;
8695 elsif Renamed_Entity (E) = Par then
8696 return False;
8698 elsif Renamed_Entity (E) = Pack then
8699 return True;
8701 elsif Is_Actual_Of_Previous_Formal (E) then
8702 return True;
8704 end if;
8706 Next_Entity (E);
8707 end loop;
8709 return False;
8710 end if;
8711 end Denotes_Formal_Package;
8713 -----------------
8714 -- End_Generic --
8715 -----------------
8717 procedure End_Generic is
8718 begin
8719 -- ??? More things could be factored out in this routine. Should
8720 -- probably be done at a later stage.
8722 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8723 Generic_Flags.Decrement_Last;
8725 Expander_Mode_Restore;
8726 end End_Generic;
8728 -------------
8729 -- Earlier --
8730 -------------
8732 function Earlier (N1, N2 : Node_Id) return Boolean is
8733 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8734 -- Find distance from given node to enclosing compilation unit
8736 ----------------
8737 -- Find_Depth --
8738 ----------------
8740 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8741 begin
8742 while Present (P)
8743 and then Nkind (P) /= N_Compilation_Unit
8744 loop
8745 P := True_Parent (P);
8746 D := D + 1;
8747 end loop;
8748 end Find_Depth;
8750 -- Local declarations
8752 D1 : Integer := 0;
8753 D2 : Integer := 0;
8754 P1 : Node_Id := N1;
8755 P2 : Node_Id := N2;
8756 T1 : Source_Ptr;
8757 T2 : Source_Ptr;
8759 -- Start of processing for Earlier
8761 begin
8762 Find_Depth (P1, D1);
8763 Find_Depth (P2, D2);
8765 if P1 /= P2 then
8766 return False;
8767 else
8768 P1 := N1;
8769 P2 := N2;
8770 end if;
8772 while D1 > D2 loop
8773 P1 := True_Parent (P1);
8774 D1 := D1 - 1;
8775 end loop;
8777 while D2 > D1 loop
8778 P2 := True_Parent (P2);
8779 D2 := D2 - 1;
8780 end loop;
8782 -- At this point P1 and P2 are at the same distance from the root.
8783 -- We examine their parents until we find a common declarative list.
8784 -- If we reach the root, N1 and N2 do not descend from the same
8785 -- declarative list (e.g. one is nested in the declarative part and
8786 -- the other is in a block in the statement part) and the earlier
8787 -- one is already frozen.
8789 while not Is_List_Member (P1)
8790 or else not Is_List_Member (P2)
8791 or else not In_Same_List (P1, P2)
8792 loop
8793 P1 := True_Parent (P1);
8794 P2 := True_Parent (P2);
8796 if Nkind (Parent (P1)) = N_Subunit then
8797 P1 := Corresponding_Stub (Parent (P1));
8798 end if;
8800 if Nkind (Parent (P2)) = N_Subunit then
8801 P2 := Corresponding_Stub (Parent (P2));
8802 end if;
8804 if P1 = P2 then
8805 return False;
8806 end if;
8807 end loop;
8809 -- Expanded code usually shares the source location of the original
8810 -- construct it was generated for. This however may not necessarily
8811 -- reflect the true location of the code within the tree.
8813 -- Before comparing the slocs of the two nodes, make sure that we are
8814 -- working with correct source locations. Assume that P1 is to the left
8815 -- of P2. If either one does not come from source, traverse the common
8816 -- list heading towards the other node and locate the first source
8817 -- statement.
8819 -- P1 P2
8820 -- ----+===+===+--------------+===+===+----
8821 -- expanded code expanded code
8823 if not Comes_From_Source (P1) then
8824 while Present (P1) loop
8826 -- Neither P2 nor a source statement were located during the
8827 -- search. If we reach the end of the list, then P1 does not
8828 -- occur earlier than P2.
8830 -- ---->
8831 -- start --- P2 ----- P1 --- end
8833 if No (Next (P1)) then
8834 return False;
8836 -- We encounter P2 while going to the right of the list. This
8837 -- means that P1 does indeed appear earlier.
8839 -- ---->
8840 -- start --- P1 ===== P2 --- end
8841 -- expanded code in between
8843 elsif P1 = P2 then
8844 return True;
8846 -- No need to look any further since we have located a source
8847 -- statement.
8849 elsif Comes_From_Source (P1) then
8850 exit;
8851 end if;
8853 -- Keep going right
8855 Next (P1);
8856 end loop;
8857 end if;
8859 if not Comes_From_Source (P2) then
8860 while Present (P2) loop
8862 -- Neither P1 nor a source statement were located during the
8863 -- search. If we reach the start of the list, then P1 does not
8864 -- occur earlier than P2.
8866 -- <----
8867 -- start --- P2 --- P1 --- end
8869 if No (Prev (P2)) then
8870 return False;
8872 -- We encounter P1 while going to the left of the list. This
8873 -- means that P1 does indeed appear earlier.
8875 -- <----
8876 -- start --- P1 ===== P2 --- end
8877 -- expanded code in between
8879 elsif P2 = P1 then
8880 return True;
8882 -- No need to look any further since we have located a source
8883 -- statement.
8885 elsif Comes_From_Source (P2) then
8886 exit;
8887 end if;
8889 -- Keep going left
8891 Prev (P2);
8892 end loop;
8893 end if;
8895 -- At this point either both nodes came from source or we approximated
8896 -- their source locations through neighboring source statements.
8898 T1 := Top_Level_Location (Sloc (P1));
8899 T2 := Top_Level_Location (Sloc (P2));
8901 -- When two nodes come from the same instance, they have identical top
8902 -- level locations. To determine proper relation within the tree, check
8903 -- their locations within the template.
8905 if T1 = T2 then
8906 return Sloc (P1) < Sloc (P2);
8908 -- The two nodes either come from unrelated instances or do not come
8909 -- from instantiated code at all.
8911 else
8912 return T1 < T2;
8913 end if;
8914 end Earlier;
8916 ----------------------
8917 -- Find_Actual_Type --
8918 ----------------------
8920 function Find_Actual_Type
8921 (Typ : Entity_Id;
8922 Gen_Type : Entity_Id) return Entity_Id
8924 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8925 T : Entity_Id;
8927 begin
8928 -- Special processing only applies to child units
8930 if not Is_Child_Unit (Gen_Scope) then
8931 return Get_Instance_Of (Typ);
8933 -- If designated or component type is itself a formal of the child unit,
8934 -- its instance is available.
8936 elsif Scope (Typ) = Gen_Scope then
8937 return Get_Instance_Of (Typ);
8939 -- If the array or access type is not declared in the parent unit,
8940 -- no special processing needed.
8942 elsif not Is_Generic_Type (Typ)
8943 and then Scope (Gen_Scope) /= Scope (Typ)
8944 then
8945 return Get_Instance_Of (Typ);
8947 -- Otherwise, retrieve designated or component type by visibility
8949 else
8950 T := Current_Entity (Typ);
8951 while Present (T) loop
8952 if In_Open_Scopes (Scope (T)) then
8953 return T;
8954 elsif Is_Generic_Actual_Type (T) then
8955 return T;
8956 end if;
8958 T := Homonym (T);
8959 end loop;
8961 return Typ;
8962 end if;
8963 end Find_Actual_Type;
8965 -----------------------------
8966 -- Freeze_Package_Instance --
8967 -----------------------------
8969 procedure Freeze_Package_Instance
8970 (N : Node_Id;
8971 Gen_Body : Node_Id;
8972 Gen_Decl : Node_Id;
8973 Act_Id : Entity_Id)
8975 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8976 -- Check if the generic definition and the instantiation come from
8977 -- a common scope, in which case the instance must be frozen after
8978 -- the generic body.
8980 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8981 -- If the instance is nested inside a generic unit, the Sloc of the
8982 -- instance indicates the place of the original definition, not the
8983 -- point of the current enclosing instance. Pending a better usage of
8984 -- Slocs to indicate instantiation places, we determine the place of
8985 -- origin of a node by finding the maximum sloc of any ancestor node.
8987 -- Why is this not equivalent to Top_Level_Location ???
8989 -------------------
8990 -- In_Same_Scope --
8991 -------------------
8993 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8994 Act_Scop : Entity_Id := Scope (Act_Id);
8995 Gen_Scop : Entity_Id := Scope (Gen_Id);
8997 begin
8998 while Act_Scop /= Standard_Standard
8999 and then Gen_Scop /= Standard_Standard
9000 loop
9001 if Act_Scop = Gen_Scop then
9002 return True;
9003 end if;
9005 Act_Scop := Scope (Act_Scop);
9006 Gen_Scop := Scope (Gen_Scop);
9007 end loop;
9009 return False;
9010 end In_Same_Scope;
9012 ---------------
9013 -- True_Sloc --
9014 ---------------
9016 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9017 N1 : Node_Id;
9018 Res : Source_Ptr;
9020 begin
9021 Res := Sloc (N);
9022 N1 := N;
9023 while Present (N1) and then N1 /= Act_Unit loop
9024 if Sloc (N1) > Res then
9025 Res := Sloc (N1);
9026 end if;
9028 N1 := Parent (N1);
9029 end loop;
9031 return Res;
9032 end True_Sloc;
9034 -- Local variables
9036 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9037 Par_Id : constant Entity_Id := Scope (Gen_Id);
9038 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9039 Gen_Unit : constant Node_Id :=
9040 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9042 Body_Unit : Node_Id;
9043 F_Node : Node_Id;
9044 Must_Delay : Boolean;
9045 Orig_Body : Node_Id;
9047 -- Start of processing for Freeze_Package_Instance
9049 begin
9050 -- If the body is a subunit, the freeze point is the corresponding stub
9051 -- in the current compilation, not the subunit itself.
9053 if Nkind (Parent (Gen_Body)) = N_Subunit then
9054 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9055 else
9056 Orig_Body := Gen_Body;
9057 end if;
9059 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9061 -- If the instantiation and the generic definition appear in the same
9062 -- package declaration, this is an early instantiation. If they appear
9063 -- in the same declarative part, it is an early instantiation only if
9064 -- the generic body appears textually later, and the generic body is
9065 -- also in the main unit.
9067 -- If instance is nested within a subprogram, and the generic body
9068 -- is not, the instance is delayed because the enclosing body is. If
9069 -- instance and body are within the same scope, or the same subprogram
9070 -- body, indicate explicitly that the instance is delayed.
9072 Must_Delay :=
9073 (Gen_Unit = Act_Unit
9074 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9075 | N_Package_Declaration
9076 or else (Gen_Unit = Body_Unit
9077 and then
9078 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9079 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9080 and then In_Same_Scope (Gen_Id, Act_Id));
9082 -- If this is an early instantiation, the freeze node is placed after
9083 -- the generic body. Otherwise, if the generic appears in an instance,
9084 -- we cannot freeze the current instance until the outer one is frozen.
9085 -- This is only relevant if the current instance is nested within some
9086 -- inner scope not itself within the outer instance. If this scope is
9087 -- a package body in the same declarative part as the outer instance,
9088 -- then that body needs to be frozen after the outer instance. Finally,
9089 -- if no delay is needed, we place the freeze node at the end of the
9090 -- current declarative part.
9092 if No (Freeze_Node (Act_Id))
9093 or else not Is_List_Member (Freeze_Node (Act_Id))
9094 then
9095 Ensure_Freeze_Node (Act_Id);
9096 F_Node := Freeze_Node (Act_Id);
9098 if Must_Delay then
9099 Insert_After (Orig_Body, F_Node);
9101 elsif Is_Generic_Instance (Par_Id)
9102 and then Present (Freeze_Node (Par_Id))
9103 and then Scope (Act_Id) /= Par_Id
9104 then
9105 -- Freeze instance of inner generic after instance of enclosing
9106 -- generic.
9108 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9110 -- Handle the following case:
9112 -- package Parent_Inst is new ...
9113 -- freeze Parent_Inst []
9115 -- procedure P ... -- this body freezes Parent_Inst
9117 -- package Inst is new ...
9119 -- In this particular scenario, the freeze node for Inst must
9120 -- be inserted in the same manner as that of Parent_Inst,
9121 -- before the next source body or at the end of the declarative
9122 -- list (body not available). If body P did not exist and
9123 -- Parent_Inst was frozen after Inst, either by a body
9124 -- following Inst or at the end of the declarative region,
9125 -- the freeze node for Inst must be inserted after that of
9126 -- Parent_Inst. This relation is established by comparing
9127 -- the Slocs of Parent_Inst freeze node and Inst.
9128 -- We examine the parents of the enclosing lists to handle
9129 -- the case where the parent instance is in the visible part
9130 -- of a package declaration, and the inner instance is in
9131 -- the corresponding private part.
9133 if Parent (List_Containing (Freeze_Node (Par_Id)))
9134 = Parent (List_Containing (N))
9135 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9136 then
9137 Insert_Freeze_Node_For_Instance (N, F_Node);
9138 else
9139 Insert_After (Freeze_Node (Par_Id), F_Node);
9140 end if;
9142 -- Freeze package enclosing instance of inner generic after
9143 -- instance of enclosing generic.
9145 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9146 and then In_Same_Declarative_Part
9147 (Parent (Freeze_Node (Par_Id)), Parent (N))
9148 then
9149 declare
9150 Enclosing : Entity_Id;
9152 begin
9153 Enclosing := Corresponding_Spec (Parent (N));
9155 if No (Enclosing) then
9156 Enclosing := Defining_Entity (Parent (N));
9157 end if;
9159 Insert_Freeze_Node_For_Instance (N, F_Node);
9160 Ensure_Freeze_Node (Enclosing);
9162 if not Is_List_Member (Freeze_Node (Enclosing)) then
9164 -- The enclosing context is a subunit, insert the freeze
9165 -- node after the stub.
9167 if Nkind (Parent (Parent (N))) = N_Subunit then
9168 Insert_Freeze_Node_For_Instance
9169 (Corresponding_Stub (Parent (Parent (N))),
9170 Freeze_Node (Enclosing));
9172 -- The enclosing context is a package with a stub body
9173 -- which has already been replaced by the real body.
9174 -- Insert the freeze node after the actual body.
9176 elsif Ekind (Enclosing) = E_Package
9177 and then Present (Body_Entity (Enclosing))
9178 and then Was_Originally_Stub
9179 (Parent (Body_Entity (Enclosing)))
9180 then
9181 Insert_Freeze_Node_For_Instance
9182 (Parent (Body_Entity (Enclosing)),
9183 Freeze_Node (Enclosing));
9185 -- The parent instance has been frozen before the body of
9186 -- the enclosing package, insert the freeze node after
9187 -- the body.
9189 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9190 and then
9191 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9192 then
9193 Insert_Freeze_Node_For_Instance
9194 (Parent (N), Freeze_Node (Enclosing));
9196 else
9197 Insert_After
9198 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9199 end if;
9200 end if;
9201 end;
9203 else
9204 Insert_Freeze_Node_For_Instance (N, F_Node);
9205 end if;
9207 else
9208 Insert_Freeze_Node_For_Instance (N, F_Node);
9209 end if;
9210 end if;
9211 end Freeze_Package_Instance;
9213 --------------------------------
9214 -- Freeze_Subprogram_Instance --
9215 --------------------------------
9217 procedure Freeze_Subprogram_Instance
9218 (N : Node_Id;
9219 Gen_Body : Node_Id;
9220 Pack_Id : Entity_Id)
9222 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9223 -- Find innermost package body that encloses the given node, and which
9224 -- is not a compilation unit. Freeze nodes for the instance, or for its
9225 -- enclosing body, may be inserted after the enclosing_body of the
9226 -- generic unit. Used to determine proper placement of freeze node for
9227 -- both package and subprogram instances.
9229 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9230 -- Find entity for given package body, and locate or create a freeze
9231 -- node for it.
9233 ----------------------------
9234 -- Enclosing_Package_Body --
9235 ----------------------------
9237 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9238 P : Node_Id;
9240 begin
9241 P := Parent (N);
9242 while Present (P)
9243 and then Nkind (Parent (P)) /= N_Compilation_Unit
9244 loop
9245 if Nkind (P) = N_Package_Body then
9246 if Nkind (Parent (P)) = N_Subunit then
9247 return Corresponding_Stub (Parent (P));
9248 else
9249 return P;
9250 end if;
9251 end if;
9253 P := True_Parent (P);
9254 end loop;
9256 return Empty;
9257 end Enclosing_Package_Body;
9259 -------------------------
9260 -- Package_Freeze_Node --
9261 -------------------------
9263 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9264 Id : Entity_Id;
9266 begin
9267 if Nkind (B) = N_Package_Body then
9268 Id := Corresponding_Spec (B);
9269 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9270 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9271 end if;
9273 Ensure_Freeze_Node (Id);
9274 return Freeze_Node (Id);
9275 end Package_Freeze_Node;
9277 -- Local variables
9279 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9280 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9281 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9283 Enc_G_F : Node_Id;
9284 F_Node : Node_Id;
9286 -- Start of processing for Freeze_Subprogram_Instance
9288 begin
9289 -- If the instance and the generic body appear within the same unit, and
9290 -- the instance precedes the generic, the freeze node for the instance
9291 -- must appear after that of the generic. If the generic is nested
9292 -- within another instance I2, then current instance must be frozen
9293 -- after I2. In both cases, the freeze nodes are those of enclosing
9294 -- packages. Otherwise, the freeze node is placed at the end of the
9295 -- current declarative part.
9297 Ensure_Freeze_Node (Pack_Id);
9298 F_Node := Freeze_Node (Pack_Id);
9300 if Is_Generic_Instance (Par_Id)
9301 and then Present (Freeze_Node (Par_Id))
9302 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9303 then
9304 -- The parent was a premature instantiation. Insert freeze node at
9305 -- the end the current declarative part.
9307 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9308 Insert_Freeze_Node_For_Instance (N, F_Node);
9310 -- Handle the following case:
9312 -- package Parent_Inst is new ...
9313 -- freeze Parent_Inst []
9315 -- procedure P ... -- this body freezes Parent_Inst
9317 -- procedure Inst is new ...
9319 -- In this particular scenario, the freeze node for Inst must be
9320 -- inserted in the same manner as that of Parent_Inst - before the
9321 -- next source body or at the end of the declarative list (body not
9322 -- available). If body P did not exist and Parent_Inst was frozen
9323 -- after Inst, either by a body following Inst or at the end of the
9324 -- declarative region, the freeze node for Inst must be inserted
9325 -- after that of Parent_Inst. This relation is established by
9326 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9328 elsif In_Same_List (Freeze_Node (Par_Id), N)
9329 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9330 then
9331 Insert_Freeze_Node_For_Instance (N, F_Node);
9333 else
9334 Insert_After (Freeze_Node (Par_Id), F_Node);
9335 end if;
9337 -- The body enclosing the instance should be frozen after the body that
9338 -- includes the generic, because the body of the instance may make
9339 -- references to entities therein. If the two are not in the same
9340 -- declarative part, or if the one enclosing the instance is frozen
9341 -- already, freeze the instance at the end of the current declarative
9342 -- part.
9344 elsif Is_Generic_Instance (Par_Id)
9345 and then Present (Freeze_Node (Par_Id))
9346 and then Present (Enc_N)
9347 then
9348 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9349 then
9350 -- The enclosing package may contain several instances. Rather
9351 -- than computing the earliest point at which to insert its freeze
9352 -- node, we place it at the end of the declarative part of the
9353 -- parent of the generic.
9355 Insert_Freeze_Node_For_Instance
9356 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9357 end if;
9359 Insert_Freeze_Node_For_Instance (N, F_Node);
9361 elsif Present (Enc_G)
9362 and then Present (Enc_N)
9363 and then Enc_G /= Enc_N
9364 and then Earlier (N, Gen_Body)
9365 then
9366 -- Freeze package that encloses instance, and place node after the
9367 -- package that encloses generic. If enclosing package is already
9368 -- frozen we have to assume it is at the proper place. This may be a
9369 -- potential ABE that requires dynamic checking. Do not add a freeze
9370 -- node if the package that encloses the generic is inside the body
9371 -- that encloses the instance, because the freeze node would be in
9372 -- the wrong scope. Additional contortions needed if the bodies are
9373 -- within a subunit.
9375 declare
9376 Enclosing_Body : Node_Id;
9378 begin
9379 if Nkind (Enc_N) = N_Package_Body_Stub then
9380 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9381 else
9382 Enclosing_Body := Enc_N;
9383 end if;
9385 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9386 Insert_Freeze_Node_For_Instance
9387 (Enc_G, Package_Freeze_Node (Enc_N));
9388 end if;
9389 end;
9391 -- Freeze enclosing subunit before instance
9393 Enc_G_F := Package_Freeze_Node (Enc_G);
9395 if not Is_List_Member (Enc_G_F) then
9396 Insert_After (Enc_G, Enc_G_F);
9397 end if;
9399 Insert_Freeze_Node_For_Instance (N, F_Node);
9401 else
9402 -- If none of the above, insert freeze node at the end of the current
9403 -- declarative part.
9405 Insert_Freeze_Node_For_Instance (N, F_Node);
9406 end if;
9407 end Freeze_Subprogram_Instance;
9409 ----------------
9410 -- Get_Gen_Id --
9411 ----------------
9413 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9414 begin
9415 return Generic_Renamings.Table (E).Gen_Id;
9416 end Get_Gen_Id;
9418 ---------------------
9419 -- Get_Instance_Of --
9420 ---------------------
9422 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9423 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9425 begin
9426 if Res /= Assoc_Null then
9427 return Generic_Renamings.Table (Res).Act_Id;
9429 else
9430 -- On exit, entity is not instantiated: not a generic parameter, or
9431 -- else parameter of an inner generic unit.
9433 return A;
9434 end if;
9435 end Get_Instance_Of;
9437 ---------------------------------
9438 -- Get_Unit_Instantiation_Node --
9439 ---------------------------------
9441 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9442 Decl : Node_Id := Unit_Declaration_Node (A);
9443 Inst : Node_Id;
9445 begin
9446 -- If the Package_Instantiation attribute has been set on the package
9447 -- entity, then use it directly when it (or its Original_Node) refers
9448 -- to an N_Package_Instantiation node. In principle it should be
9449 -- possible to have this field set in all cases, which should be
9450 -- investigated, and would allow this function to be significantly
9451 -- simplified. ???
9453 Inst := Package_Instantiation (A);
9455 if Present (Inst) then
9456 if Nkind (Inst) = N_Package_Instantiation then
9457 return Inst;
9459 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9460 return Original_Node (Inst);
9461 end if;
9462 end if;
9464 -- If the instantiation is a compilation unit that does not need body
9465 -- then the instantiation node has been rewritten as a package
9466 -- declaration for the instance, and we return the original node.
9468 -- If it is a compilation unit and the instance node has not been
9469 -- rewritten, then it is still the unit of the compilation. Finally, if
9470 -- a body is present, this is a parent of the main unit whose body has
9471 -- been compiled for inlining purposes, and the instantiation node has
9472 -- been rewritten with the instance body.
9474 -- Otherwise the instantiation node appears after the declaration. If
9475 -- the entity is a formal package, the declaration may have been
9476 -- rewritten as a generic declaration (in the case of a formal with box)
9477 -- or left as a formal package declaration if it has actuals, and is
9478 -- found with a forward search.
9480 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9481 if Nkind (Decl) = N_Package_Declaration
9482 and then Present (Corresponding_Body (Decl))
9483 then
9484 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9485 end if;
9487 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9488 return Original_Node (Decl);
9489 else
9490 return Unit (Parent (Decl));
9491 end if;
9493 elsif Nkind (Decl) = N_Package_Declaration
9494 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9495 then
9496 return Original_Node (Decl);
9498 else
9499 Inst := Next (Decl);
9500 while Nkind (Inst) not in N_Formal_Package_Declaration
9501 | N_Function_Instantiation
9502 | N_Package_Instantiation
9503 | N_Procedure_Instantiation
9504 loop
9505 Next (Inst);
9506 end loop;
9508 return Inst;
9509 end if;
9510 end Get_Unit_Instantiation_Node;
9512 ------------------------
9513 -- Has_Been_Exchanged --
9514 ------------------------
9516 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9517 Next : Elmt_Id;
9519 begin
9520 Next := First_Elmt (Exchanged_Views);
9521 while Present (Next) loop
9522 if Full_View (Node (Next)) = E then
9523 return True;
9524 end if;
9526 Next_Elmt (Next);
9527 end loop;
9529 return False;
9530 end Has_Been_Exchanged;
9532 -------------------
9533 -- Has_Contracts --
9534 -------------------
9536 function Has_Contracts (Decl : Node_Id) return Boolean is
9537 A_List : constant List_Id := Aspect_Specifications (Decl);
9538 A_Spec : Node_Id;
9539 A_Id : Aspect_Id;
9540 begin
9541 if No (A_List) then
9542 return False;
9543 else
9544 A_Spec := First (A_List);
9545 while Present (A_Spec) loop
9546 A_Id := Get_Aspect_Id (A_Spec);
9547 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9548 return True;
9549 end if;
9551 Next (A_Spec);
9552 end loop;
9554 return False;
9555 end if;
9556 end Has_Contracts;
9558 ----------
9559 -- Hash --
9560 ----------
9562 function Hash (F : Entity_Id) return HTable_Range is
9563 begin
9564 return HTable_Range (F mod HTable_Size);
9565 end Hash;
9567 ------------------------
9568 -- Hide_Current_Scope --
9569 ------------------------
9571 procedure Hide_Current_Scope is
9572 C : constant Entity_Id := Current_Scope;
9573 E : Entity_Id;
9575 begin
9576 Set_Is_Hidden_Open_Scope (C);
9578 E := First_Entity (C);
9579 while Present (E) loop
9580 if Is_Immediately_Visible (E) then
9581 Set_Is_Immediately_Visible (E, False);
9582 Append_Elmt (E, Hidden_Entities);
9583 end if;
9585 Next_Entity (E);
9586 end loop;
9588 -- Make the scope name invisible as well. This is necessary, but might
9589 -- conflict with calls to Rtsfind later on, in case the scope is a
9590 -- predefined one. There is no clean solution to this problem, so for
9591 -- now we depend on the user not redefining Standard itself in one of
9592 -- the parent units.
9594 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9595 Set_Is_Immediately_Visible (C, False);
9596 Append_Elmt (C, Hidden_Entities);
9597 end if;
9599 end Hide_Current_Scope;
9601 --------------
9602 -- Init_Env --
9603 --------------
9605 procedure Init_Env is
9606 Saved : Instance_Env;
9608 begin
9609 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9610 Saved.Exchanged_Views := Exchanged_Views;
9611 Saved.Hidden_Entities := Hidden_Entities;
9612 Saved.Current_Sem_Unit := Current_Sem_Unit;
9613 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9614 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9616 -- Save configuration switches. These may be reset if the unit is a
9617 -- predefined unit, and the current mode is not Ada 2005.
9619 Saved.Switches := Save_Config_Switches;
9621 Instance_Envs.Append (Saved);
9623 Exchanged_Views := New_Elmt_List;
9624 Hidden_Entities := New_Elmt_List;
9626 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9627 -- this is set properly in Set_Instance_Env.
9629 Current_Instantiated_Parent :=
9630 (Current_Scope, Current_Scope, Assoc_Null);
9631 end Init_Env;
9633 ---------------------
9634 -- In_Main_Context --
9635 ---------------------
9637 function In_Main_Context (E : Entity_Id) return Boolean is
9638 Context : List_Id;
9639 Clause : Node_Id;
9640 Nam : Node_Id;
9642 begin
9643 if not Is_Compilation_Unit (E)
9644 or else Ekind (E) /= E_Package
9645 or else In_Private_Part (E)
9646 then
9647 return False;
9648 end if;
9650 Context := Context_Items (Cunit (Main_Unit));
9652 Clause := First (Context);
9653 while Present (Clause) loop
9654 if Nkind (Clause) = N_With_Clause then
9655 Nam := Name (Clause);
9657 -- If the current scope is part of the context of the main unit,
9658 -- analysis of the corresponding with_clause is not complete, and
9659 -- the entity is not set. We use the Chars field directly, which
9660 -- might produce false positives in rare cases, but guarantees
9661 -- that we produce all the instance bodies we will need.
9663 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9664 or else (Nkind (Nam) = N_Selected_Component
9665 and then Chars (Selector_Name (Nam)) = Chars (E))
9666 then
9667 return True;
9668 end if;
9669 end if;
9671 Next (Clause);
9672 end loop;
9674 return False;
9675 end In_Main_Context;
9677 ---------------------
9678 -- Inherit_Context --
9679 ---------------------
9681 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9682 Current_Context : List_Id;
9683 Current_Unit : Node_Id;
9684 Item : Node_Id;
9685 New_I : Node_Id;
9687 Clause : Node_Id;
9688 OK : Boolean;
9689 Lib_Unit : Node_Id;
9691 begin
9692 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9694 -- The inherited context is attached to the enclosing compilation
9695 -- unit. This is either the main unit, or the declaration for the
9696 -- main unit (in case the instantiation appears within the package
9697 -- declaration and the main unit is its body).
9699 Current_Unit := Parent (Inst);
9700 while Present (Current_Unit)
9701 and then Nkind (Current_Unit) /= N_Compilation_Unit
9702 loop
9703 Current_Unit := Parent (Current_Unit);
9704 end loop;
9706 Current_Context := Context_Items (Current_Unit);
9708 Item := First (Context_Items (Parent (Gen_Decl)));
9709 while Present (Item) loop
9710 if Nkind (Item) = N_With_Clause then
9711 Lib_Unit := Library_Unit (Item);
9713 -- Take care to prevent direct cyclic with's
9715 if Lib_Unit /= Current_Unit then
9717 -- Do not add a unit if it is already in the context
9719 Clause := First (Current_Context);
9720 OK := True;
9721 while Present (Clause) loop
9722 if Nkind (Clause) = N_With_Clause
9723 and then Library_Unit (Clause) = Lib_Unit
9724 then
9725 OK := False;
9726 exit;
9727 end if;
9729 Next (Clause);
9730 end loop;
9732 if OK then
9733 New_I := New_Copy (Item);
9734 Set_Implicit_With (New_I);
9736 Append (New_I, Current_Context);
9737 end if;
9738 end if;
9739 end if;
9741 Next (Item);
9742 end loop;
9743 end if;
9744 end Inherit_Context;
9746 ----------------
9747 -- Initialize --
9748 ----------------
9750 procedure Initialize is
9751 begin
9752 Generic_Renamings.Init;
9753 Instance_Envs.Init;
9754 Generic_Flags.Init;
9755 Generic_Renamings_HTable.Reset;
9756 Circularity_Detected := False;
9757 Exchanged_Views := No_Elist;
9758 Hidden_Entities := No_Elist;
9759 end Initialize;
9761 -------------------------------------
9762 -- Insert_Freeze_Node_For_Instance --
9763 -------------------------------------
9765 procedure Insert_Freeze_Node_For_Instance
9766 (N : Node_Id;
9767 F_Node : Node_Id)
9769 function Enclosing_Body (N : Node_Id) return Node_Id;
9770 -- Find enclosing package or subprogram body, if any. Freeze node may
9771 -- be placed at end of current declarative list if previous instance
9772 -- and current one have different enclosing bodies.
9774 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9775 -- Find the local instance, if any, that declares the generic that is
9776 -- being instantiated. If present, the freeze node for this instance
9777 -- must follow the freeze node for the previous instance.
9779 --------------------
9780 -- Enclosing_Body --
9781 --------------------
9783 function Enclosing_Body (N : Node_Id) return Node_Id is
9784 P : Node_Id;
9786 begin
9787 P := Parent (N);
9788 while Present (P)
9789 and then Nkind (Parent (P)) /= N_Compilation_Unit
9790 loop
9791 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9792 if Nkind (Parent (P)) = N_Subunit then
9793 return Corresponding_Stub (Parent (P));
9794 else
9795 return P;
9796 end if;
9797 end if;
9799 P := True_Parent (P);
9800 end loop;
9802 return Empty;
9803 end Enclosing_Body;
9805 -----------------------
9806 -- Previous_Instance --
9807 -----------------------
9809 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9810 S : Entity_Id;
9812 begin
9813 S := Scope (Gen);
9814 while Present (S) and then S /= Standard_Standard loop
9815 if Is_Generic_Instance (S)
9816 and then In_Same_Source_Unit (S, N)
9817 then
9818 return S;
9819 end if;
9821 S := Scope (S);
9822 end loop;
9824 return Empty;
9825 end Previous_Instance;
9827 -- Local variables
9829 Decl : Node_Id;
9830 Decls : List_Id;
9831 Inst : Entity_Id;
9832 Origin : Entity_Id;
9833 Par_Inst : Node_Id;
9834 Par_N : Node_Id;
9836 -- Start of processing for Insert_Freeze_Node_For_Instance
9838 begin
9839 -- Nothing to do if the freeze node has already been inserted
9841 if Is_List_Member (F_Node) then
9842 return;
9843 end if;
9845 Inst := Entity (F_Node);
9847 -- When processing a subprogram instantiation, utilize the actual
9848 -- subprogram instantiation rather than its package wrapper as it
9849 -- carries all the context information.
9851 if Is_Wrapper_Package (Inst) then
9852 Inst := Related_Instance (Inst);
9853 end if;
9855 Par_Inst := Parent (Inst);
9857 -- If this is a package instance, check whether the generic is declared
9858 -- in a previous instance and the current instance is not within the
9859 -- previous one.
9861 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9862 declare
9863 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9864 Par_I : constant Entity_Id :=
9865 Previous_Instance (Generic_Parent (Par_Inst));
9866 Scop : Entity_Id;
9868 begin
9869 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9870 Scop := Scope (Inst);
9872 -- If the current instance is within the one that contains
9873 -- the generic, the freeze node for the current one must
9874 -- appear in the current declarative part. Ditto, if the
9875 -- current instance is within another package instance or
9876 -- within a body that does not enclose the current instance.
9877 -- In these three cases the freeze node of the previous
9878 -- instance is not relevant.
9880 while Present (Scop) and then Scop /= Standard_Standard loop
9881 exit when Scop = Par_I
9882 or else
9883 (Is_Generic_Instance (Scop)
9884 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9885 Scop := Scope (Scop);
9886 end loop;
9888 -- Previous instance encloses current instance
9890 if Scop = Par_I then
9891 null;
9893 -- If the next node is a source body we must freeze in the
9894 -- current scope as well.
9896 elsif Present (Next (N))
9897 and then Nkind (Next (N)) in N_Subprogram_Body
9898 | N_Package_Body
9899 and then Comes_From_Source (Next (N))
9900 then
9901 null;
9903 -- Current instance is within an unrelated instance
9905 elsif Is_Generic_Instance (Scop) then
9906 null;
9908 -- Current instance is within an unrelated body
9910 elsif Present (Enclosing_N)
9911 and then Enclosing_N /= Enclosing_Body (Par_I)
9912 then
9913 null;
9915 else
9916 Insert_After (Freeze_Node (Par_I), F_Node);
9917 return;
9918 end if;
9919 end if;
9920 end;
9921 end if;
9923 Decl := N;
9924 Decls := List_Containing (N);
9925 Par_N := Parent (Decls);
9926 Origin := Empty;
9928 -- Determine the proper freeze point of an instantiation
9930 if Is_Generic_Instance (Inst) then
9931 loop
9932 -- When the instantiation occurs in a package spec, append the
9933 -- freeze node to the private declarations (if any).
9935 if Nkind (Par_N) = N_Package_Specification
9936 and then Decls = Visible_Declarations (Par_N)
9937 and then not Is_Empty_List (Private_Declarations (Par_N))
9938 then
9939 Decls := Private_Declarations (Par_N);
9940 Decl := First (Decls);
9941 end if;
9943 -- We adhere to the general rule of a package or subprogram body
9944 -- causing freezing of anything before it in the same declarative
9945 -- region. In this respect, the proper freeze point of a package
9946 -- instantiation is before the first source body which follows, or
9947 -- before a stub. This ensures that entities from the instance are
9948 -- already frozen and therefore usable in source bodies.
9950 if Nkind (Par_N) /= N_Package_Declaration
9951 and then
9952 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
9953 then
9954 while Present (Decl) loop
9955 if ((Nkind (Decl) in N_Unit_Body
9956 or else
9957 Nkind (Decl) in N_Body_Stub)
9958 and then Comes_From_Source (Decl))
9959 or else (Present (Origin)
9960 and then Nkind (Decl) in N_Generic_Instantiation
9961 and then Instance_Spec (Decl) /= Origin)
9962 then
9963 Set_Sloc (F_Node, Sloc (Decl));
9964 Insert_Before (Decl, F_Node);
9965 return;
9966 end if;
9968 Next (Decl);
9969 end loop;
9970 end if;
9972 -- When the instantiation occurs in a package spec and there is
9973 -- no source body which follows, and the package has a body but
9974 -- is delayed, then insert immediately before its freeze node.
9976 if Nkind (Par_N) = N_Package_Specification
9977 and then Present (Corresponding_Body (Parent (Par_N)))
9978 and then Present (Freeze_Node (Defining_Entity (Par_N)))
9979 then
9980 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
9981 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
9982 return;
9984 -- When the instantiation occurs in a package spec and there is
9985 -- no source body which follows, not even of the package itself,
9986 -- then insert into the declaration list of the outer level, but
9987 -- do not jump over following instantiations in this list because
9988 -- they may have a body that has not materialized yet, see above.
9990 elsif Nkind (Par_N) = N_Package_Specification
9991 and then No (Corresponding_Body (Parent (Par_N)))
9992 and then Is_List_Member (Parent (Par_N))
9993 then
9994 Decl := Parent (Par_N);
9995 Decls := List_Containing (Decl);
9996 Par_N := Parent (Decls);
9997 Origin := Decl;
9999 -- In a package declaration, or if no source body which follows
10000 -- and at library level, then insert at end of list.
10002 else
10003 exit;
10004 end if;
10005 end loop;
10006 end if;
10008 -- Insert and adjust the Sloc of the freeze node
10010 Set_Sloc (F_Node, Sloc (Last (Decls)));
10011 Insert_After (Last (Decls), F_Node);
10012 end Insert_Freeze_Node_For_Instance;
10014 -----------------------------
10015 -- Install_Formal_Packages --
10016 -----------------------------
10018 procedure Install_Formal_Packages (Par : Entity_Id) is
10019 E : Entity_Id;
10020 Gen : Entity_Id;
10021 Gen_E : Entity_Id := Empty;
10023 begin
10024 E := First_Entity (Par);
10026 -- If we are installing an instance parent, locate the formal packages
10027 -- of its generic parent.
10029 if Is_Generic_Instance (Par) then
10030 Gen := Generic_Parent (Package_Specification (Par));
10031 Gen_E := First_Entity (Gen);
10032 end if;
10034 while Present (E) loop
10035 if Ekind (E) = E_Package
10036 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10037 then
10038 -- If this is the renaming for the parent instance, done
10040 if Renamed_Entity (E) = Par then
10041 exit;
10043 -- The visibility of a formal of an enclosing generic is already
10044 -- correct.
10046 elsif Denotes_Formal_Package (E) then
10047 null;
10049 elsif Present (Associated_Formal_Package (E)) then
10050 Check_Generic_Actuals (Renamed_Entity (E), True);
10051 Set_Is_Hidden (E, False);
10053 -- Find formal package in generic unit that corresponds to
10054 -- (instance of) formal package in instance.
10056 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10057 Next_Entity (Gen_E);
10058 end loop;
10060 if Present (Gen_E) then
10061 Map_Formal_Package_Entities (Gen_E, E);
10062 end if;
10063 end if;
10064 end if;
10066 Next_Entity (E);
10068 if Present (Gen_E) then
10069 Next_Entity (Gen_E);
10070 end if;
10071 end loop;
10072 end Install_Formal_Packages;
10074 --------------------
10075 -- Install_Parent --
10076 --------------------
10078 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10079 Ancestors : constant Elist_Id := New_Elmt_List;
10080 S : constant Entity_Id := Current_Scope;
10081 Inst_Par : Entity_Id;
10082 First_Par : Entity_Id;
10083 Inst_Node : Node_Id;
10084 Gen_Par : Entity_Id;
10085 First_Gen : Entity_Id;
10086 Elmt : Elmt_Id;
10088 procedure Install_Noninstance_Specs (Par : Entity_Id);
10089 -- Install the scopes of noninstance parent units ending with Par
10091 procedure Install_Spec (Par : Entity_Id);
10092 -- The child unit is within the declarative part of the parent, so the
10093 -- declarations within the parent are immediately visible.
10095 -------------------------------
10096 -- Install_Noninstance_Specs --
10097 -------------------------------
10099 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10100 begin
10101 if Present (Par)
10102 and then Par /= Standard_Standard
10103 and then not In_Open_Scopes (Par)
10104 then
10105 Install_Noninstance_Specs (Scope (Par));
10106 Install_Spec (Par);
10107 end if;
10108 end Install_Noninstance_Specs;
10110 ------------------
10111 -- Install_Spec --
10112 ------------------
10114 procedure Install_Spec (Par : Entity_Id) is
10115 Spec : constant Node_Id := Package_Specification (Par);
10117 begin
10118 -- If this parent of the child instance is a top-level unit,
10119 -- then record the unit and its visibility for later resetting in
10120 -- Remove_Parent. We exclude units that are generic instances, as we
10121 -- only want to record this information for the ultimate top-level
10122 -- noninstance parent (is that always correct???).
10124 if Scope (Par) = Standard_Standard
10125 and then not Is_Generic_Instance (Par)
10126 then
10127 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10128 Instance_Parent_Unit := Par;
10129 end if;
10131 -- Open the parent scope and make it and its declarations visible.
10132 -- If this point is not within a body, then only the visible
10133 -- declarations should be made visible, and installation of the
10134 -- private declarations is deferred until the appropriate point
10135 -- within analysis of the spec being instantiated (see the handling
10136 -- of parent visibility in Analyze_Package_Specification). This is
10137 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10138 -- private view problems that occur when compiling instantiations of
10139 -- a generic child of that package (Generic_Dispatching_Constructor).
10140 -- If the instance freezes a tagged type, inlinings of operations
10141 -- from Ada.Tags may need the full view of type Tag. If inlining took
10142 -- proper account of establishing visibility of inlined subprograms'
10143 -- parents then it should be possible to remove this
10144 -- special check. ???
10146 Push_Scope (Par);
10147 Set_Is_Immediately_Visible (Par);
10148 Install_Visible_Declarations (Par);
10149 Set_Use (Visible_Declarations (Spec));
10151 if In_Body or else Is_RTU (Par, Ada_Tags) then
10152 Install_Private_Declarations (Par);
10153 Set_Use (Private_Declarations (Spec));
10154 end if;
10155 end Install_Spec;
10157 -- Start of processing for Install_Parent
10159 begin
10160 -- We need to install the parent instance to compile the instantiation
10161 -- of the child, but the child instance must appear in the current
10162 -- scope. Given that we cannot place the parent above the current scope
10163 -- in the scope stack, we duplicate the current scope and unstack both
10164 -- after the instantiation is complete.
10166 -- If the parent is itself the instantiation of a child unit, we must
10167 -- also stack the instantiation of its parent, and so on. Each such
10168 -- ancestor is the prefix of the name in a prior instantiation.
10170 -- If this is a nested instance, the parent unit itself resolves to
10171 -- a renaming of the parent instance, whose declaration we need.
10173 -- Finally, the parent may be a generic (not an instance) when the
10174 -- child unit appears as a formal package.
10176 Inst_Par := P;
10178 if Present (Renamed_Entity (Inst_Par)) then
10179 Inst_Par := Renamed_Entity (Inst_Par);
10180 end if;
10182 First_Par := Inst_Par;
10184 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10186 First_Gen := Gen_Par;
10188 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10190 -- Load grandparent instance as well
10192 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10194 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10195 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10197 if Present (Renamed_Entity (Inst_Par)) then
10198 Inst_Par := Renamed_Entity (Inst_Par);
10199 end if;
10201 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10203 if Present (Gen_Par) then
10204 Prepend_Elmt (Inst_Par, Ancestors);
10206 else
10207 -- Parent is not the name of an instantiation
10209 Install_Noninstance_Specs (Inst_Par);
10210 exit;
10211 end if;
10213 else
10214 -- Previous error
10216 exit;
10217 end if;
10218 end loop;
10220 if Present (First_Gen) then
10221 Append_Elmt (First_Par, Ancestors);
10222 else
10223 Install_Noninstance_Specs (First_Par);
10224 end if;
10226 if not Is_Empty_Elmt_List (Ancestors) then
10227 Elmt := First_Elmt (Ancestors);
10228 while Present (Elmt) loop
10229 Install_Spec (Node (Elmt));
10230 Install_Formal_Packages (Node (Elmt));
10231 Next_Elmt (Elmt);
10232 end loop;
10233 end if;
10235 if not In_Body then
10236 Push_Scope (S);
10237 end if;
10238 end Install_Parent;
10240 -------------------------------
10241 -- Install_Hidden_Primitives --
10242 -------------------------------
10244 procedure Install_Hidden_Primitives
10245 (Prims_List : in out Elist_Id;
10246 Gen_T : Entity_Id;
10247 Act_T : Entity_Id)
10249 Elmt : Elmt_Id;
10250 List : Elist_Id := No_Elist;
10251 Prim_G_Elmt : Elmt_Id;
10252 Prim_A_Elmt : Elmt_Id;
10253 Prim_G : Node_Id;
10254 Prim_A : Node_Id;
10256 begin
10257 -- No action needed in case of serious errors because we cannot trust
10258 -- in the order of primitives
10260 if Serious_Errors_Detected > 0 then
10261 return;
10263 -- No action possible if we don't have available the list of primitive
10264 -- operations
10266 elsif No (Gen_T)
10267 or else not Is_Record_Type (Gen_T)
10268 or else not Is_Tagged_Type (Gen_T)
10269 or else not Is_Record_Type (Act_T)
10270 or else not Is_Tagged_Type (Act_T)
10271 then
10272 return;
10274 -- There is no need to handle interface types since their primitives
10275 -- cannot be hidden
10277 elsif Is_Interface (Gen_T) then
10278 return;
10279 end if;
10281 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10283 if not Is_Class_Wide_Type (Act_T) then
10284 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10285 else
10286 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10287 end if;
10289 loop
10290 -- Skip predefined primitives in the generic formal
10292 while Present (Prim_G_Elmt)
10293 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10294 loop
10295 Next_Elmt (Prim_G_Elmt);
10296 end loop;
10298 -- Skip predefined primitives in the generic actual
10300 while Present (Prim_A_Elmt)
10301 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10302 loop
10303 Next_Elmt (Prim_A_Elmt);
10304 end loop;
10306 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10308 Prim_G := Node (Prim_G_Elmt);
10309 Prim_A := Node (Prim_A_Elmt);
10311 -- There is no need to handle interface primitives because their
10312 -- primitives are not hidden
10314 exit when Present (Interface_Alias (Prim_G));
10316 -- Here we install one hidden primitive
10318 if Chars (Prim_G) /= Chars (Prim_A)
10319 and then Has_Suffix (Prim_A, 'P')
10320 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10321 then
10322 Set_Chars (Prim_A, Chars (Prim_G));
10323 Append_New_Elmt (Prim_A, To => List);
10324 end if;
10326 Next_Elmt (Prim_A_Elmt);
10327 Next_Elmt (Prim_G_Elmt);
10328 end loop;
10330 -- Append the elements to the list of temporarily visible primitives
10331 -- avoiding duplicates.
10333 if Present (List) then
10334 if No (Prims_List) then
10335 Prims_List := New_Elmt_List;
10336 end if;
10338 Elmt := First_Elmt (List);
10339 while Present (Elmt) loop
10340 Append_Unique_Elmt (Node (Elmt), Prims_List);
10341 Next_Elmt (Elmt);
10342 end loop;
10343 end if;
10344 end Install_Hidden_Primitives;
10346 -------------------------------
10347 -- Restore_Hidden_Primitives --
10348 -------------------------------
10350 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10351 Prim_Elmt : Elmt_Id;
10352 Prim : Node_Id;
10354 begin
10355 if Present (Prims_List) then
10356 Prim_Elmt := First_Elmt (Prims_List);
10357 while Present (Prim_Elmt) loop
10358 Prim := Node (Prim_Elmt);
10359 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10360 Next_Elmt (Prim_Elmt);
10361 end loop;
10363 Prims_List := No_Elist;
10364 end if;
10365 end Restore_Hidden_Primitives;
10367 --------------------------------
10368 -- Instantiate_Formal_Package --
10369 --------------------------------
10371 function Instantiate_Formal_Package
10372 (Formal : Node_Id;
10373 Actual : Node_Id;
10374 Analyzed_Formal : Node_Id) return List_Id
10376 Loc : constant Source_Ptr := Sloc (Actual);
10377 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10379 Actual_Pack : Entity_Id;
10380 Formal_Pack : Entity_Id;
10381 Gen_Parent : Entity_Id;
10382 Decls : List_Id;
10383 Nod : Node_Id;
10384 Parent_Spec : Node_Id;
10386 procedure Find_Matching_Actual
10387 (F : Node_Id;
10388 Act : in out Entity_Id);
10389 -- We need to associate each formal entity in the formal package with
10390 -- the corresponding entity in the actual package. The actual package
10391 -- has been analyzed and possibly expanded, and as a result there is
10392 -- no one-to-one correspondence between the two lists (for example,
10393 -- the actual may include subtypes, itypes, and inherited primitive
10394 -- operations, interspersed among the renaming declarations for the
10395 -- actuals). We retrieve the corresponding actual by name because each
10396 -- actual has the same name as the formal, and they do appear in the
10397 -- same order.
10399 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10400 -- Retrieve entity of defining entity of generic formal parameter.
10401 -- Only the declarations of formals need to be considered when
10402 -- linking them to actuals, but the declarative list may include
10403 -- internal entities generated during analysis, and those are ignored.
10405 procedure Match_Formal_Entity
10406 (Formal_Node : Node_Id;
10407 Formal_Ent : Entity_Id;
10408 Actual_Ent : Entity_Id);
10409 -- Associates the formal entity with the actual. In the case where
10410 -- Formal_Ent is a formal package, this procedure iterates through all
10411 -- of its formals and enters associations between the actuals occurring
10412 -- in the formal package's corresponding actual package (given by
10413 -- Actual_Ent) and the formal package's formal parameters. This
10414 -- procedure recurses if any of the parameters is itself a package.
10416 function Is_Instance_Of
10417 (Act_Spec : Entity_Id;
10418 Gen_Anc : Entity_Id) return Boolean;
10419 -- The actual can be an instantiation of a generic within another
10420 -- instance, in which case there is no direct link from it to the
10421 -- original generic ancestor. In that case, we recognize that the
10422 -- ultimate ancestor is the same by examining names and scopes.
10424 procedure Process_Nested_Formal (Formal : Entity_Id);
10425 -- If the current formal is declared with a box, its own formals are
10426 -- visible in the instance, as they were in the generic, and their
10427 -- Hidden flag must be reset. If some of these formals are themselves
10428 -- packages declared with a box, the processing must be recursive.
10430 --------------------------
10431 -- Find_Matching_Actual --
10432 --------------------------
10434 procedure Find_Matching_Actual
10435 (F : Node_Id;
10436 Act : in out Entity_Id)
10438 Formal_Ent : Entity_Id;
10440 begin
10441 case Nkind (Original_Node (F)) is
10442 when N_Formal_Object_Declaration
10443 | N_Formal_Type_Declaration
10445 Formal_Ent := Defining_Identifier (F);
10447 while Present (Act)
10448 and then Chars (Act) /= Chars (Formal_Ent)
10449 loop
10450 Next_Entity (Act);
10451 end loop;
10453 when N_Formal_Package_Declaration
10454 | N_Formal_Subprogram_Declaration
10455 | N_Generic_Package_Declaration
10456 | N_Package_Declaration
10458 Formal_Ent := Defining_Entity (F);
10460 while Present (Act)
10461 and then Chars (Act) /= Chars (Formal_Ent)
10462 loop
10463 Next_Entity (Act);
10464 end loop;
10466 when others =>
10467 raise Program_Error;
10468 end case;
10469 end Find_Matching_Actual;
10471 -------------------------
10472 -- Match_Formal_Entity --
10473 -------------------------
10475 procedure Match_Formal_Entity
10476 (Formal_Node : Node_Id;
10477 Formal_Ent : Entity_Id;
10478 Actual_Ent : Entity_Id)
10480 Act_Pkg : Entity_Id;
10482 begin
10483 Set_Instance_Of (Formal_Ent, Actual_Ent);
10485 if Ekind (Actual_Ent) = E_Package then
10487 -- Record associations for each parameter
10489 Act_Pkg := Actual_Ent;
10491 declare
10492 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10493 F_Ent : Entity_Id;
10494 F_Node : Node_Id;
10496 Gen_Decl : Node_Id;
10497 Formals : List_Id;
10498 Actual : Entity_Id;
10500 begin
10501 -- Retrieve the actual given in the formal package declaration
10503 Actual := Entity (Name (Original_Node (Formal_Node)));
10505 -- The actual in the formal package declaration may be a
10506 -- renamed generic package, in which case we want to retrieve
10507 -- the original generic in order to traverse its formal part.
10509 if Present (Renamed_Entity (Actual)) then
10510 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10511 else
10512 Gen_Decl := Unit_Declaration_Node (Actual);
10513 end if;
10515 Formals := Generic_Formal_Declarations (Gen_Decl);
10517 if Present (Formals) then
10518 F_Node := First_Non_Pragma (Formals);
10519 else
10520 F_Node := Empty;
10521 end if;
10523 while Present (A_Ent)
10524 and then Present (F_Node)
10525 and then A_Ent /= First_Private_Entity (Act_Pkg)
10526 loop
10527 F_Ent := Get_Formal_Entity (F_Node);
10529 if Present (F_Ent) then
10531 -- This is a formal of the original package. Record
10532 -- association and recurse.
10534 Find_Matching_Actual (F_Node, A_Ent);
10535 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10536 Next_Entity (A_Ent);
10537 end if;
10539 Next_Non_Pragma (F_Node);
10540 end loop;
10541 end;
10542 end if;
10543 end Match_Formal_Entity;
10545 -----------------------
10546 -- Get_Formal_Entity --
10547 -----------------------
10549 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10550 Kind : constant Node_Kind := Nkind (Original_Node (N));
10551 begin
10552 case Kind is
10553 when N_Formal_Object_Declaration =>
10554 return Defining_Identifier (N);
10556 when N_Formal_Type_Declaration =>
10557 return Defining_Identifier (N);
10559 when N_Formal_Subprogram_Declaration =>
10560 return Defining_Unit_Name (Specification (N));
10562 when N_Formal_Package_Declaration =>
10563 return Defining_Identifier (Original_Node (N));
10565 when N_Generic_Package_Declaration =>
10566 return Defining_Identifier (Original_Node (N));
10568 -- All other declarations are introduced by semantic analysis and
10569 -- have no match in the actual.
10571 when others =>
10572 return Empty;
10573 end case;
10574 end Get_Formal_Entity;
10576 --------------------
10577 -- Is_Instance_Of --
10578 --------------------
10580 function Is_Instance_Of
10581 (Act_Spec : Entity_Id;
10582 Gen_Anc : Entity_Id) return Boolean
10584 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10586 begin
10587 if No (Gen_Par) then
10588 return False;
10590 -- Simplest case: the generic parent of the actual is the formal
10592 elsif Gen_Par = Gen_Anc then
10593 return True;
10595 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10596 return False;
10598 -- The actual may be obtained through several instantiations. Its
10599 -- scope must itself be an instance of a generic declared in the
10600 -- same scope as the formal. Any other case is detected above.
10602 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10603 return False;
10605 else
10606 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10607 end if;
10608 end Is_Instance_Of;
10610 ---------------------------
10611 -- Process_Nested_Formal --
10612 ---------------------------
10614 procedure Process_Nested_Formal (Formal : Entity_Id) is
10615 Ent : Entity_Id;
10617 begin
10618 if Present (Associated_Formal_Package (Formal))
10619 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10620 then
10621 Ent := First_Entity (Formal);
10622 while Present (Ent) loop
10623 Set_Is_Hidden (Ent, False);
10624 Set_Is_Visible_Formal (Ent);
10625 Set_Is_Potentially_Use_Visible
10626 (Ent, Is_Potentially_Use_Visible (Formal));
10628 if Ekind (Ent) = E_Package then
10629 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10630 Process_Nested_Formal (Ent);
10631 end if;
10633 Next_Entity (Ent);
10634 end loop;
10635 end if;
10636 end Process_Nested_Formal;
10638 -- Start of processing for Instantiate_Formal_Package
10640 begin
10641 Analyze (Actual);
10643 -- The actual must be a package instance, or else a current instance
10644 -- such as a parent generic within the body of a generic child.
10646 if not Is_Entity_Name (Actual)
10647 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10648 then
10649 Error_Msg_N
10650 ("expect package instance to instantiate formal", Actual);
10651 Abandon_Instantiation (Actual);
10653 else
10654 Actual_Pack := Entity (Actual);
10655 Set_Is_Instantiated (Actual_Pack);
10657 -- The actual may be a renamed package, or an outer generic formal
10658 -- package whose instantiation is converted into a renaming.
10660 if Present (Renamed_Entity (Actual_Pack)) then
10661 Actual_Pack := Renamed_Entity (Actual_Pack);
10662 end if;
10664 -- The analyzed formal is expected to be the result of the rewriting
10665 -- of the formal package into a regular package by analysis.
10667 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10668 and then Nkind (Original_Node (Analyzed_Formal)) =
10669 N_Formal_Package_Declaration);
10671 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10672 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10674 -- The actual for a ghost generic formal package should be a ghost
10675 -- package (SPARK RM 6.9(14)).
10677 Check_Ghost_Formal_Procedure_Or_Package
10678 (N => Actual,
10679 Actual => Actual_Pack,
10680 Formal => Formal_Pack);
10682 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10683 Parent_Spec := Package_Specification (Actual_Pack);
10684 else
10685 Parent_Spec := Parent (Actual_Pack);
10686 end if;
10688 if Gen_Parent = Any_Id then
10689 Error_Msg_N
10690 ("previous error in declaration of formal package", Actual);
10691 Abandon_Instantiation (Actual);
10693 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10694 null;
10696 -- If this is the current instance of an enclosing generic, that unit
10697 -- is the generic package we need.
10699 elsif In_Open_Scopes (Actual_Pack)
10700 and then Ekind (Actual_Pack) = E_Generic_Package
10701 then
10702 null;
10704 else
10705 Error_Msg_NE
10706 ("actual parameter must be instance of&", Actual, Gen_Parent);
10707 Abandon_Instantiation (Actual);
10708 end if;
10710 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10711 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10713 Nod :=
10714 Make_Package_Renaming_Declaration (Loc,
10715 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10716 Name => New_Occurrence_Of (Actual_Pack, Loc));
10718 Set_Associated_Formal_Package
10719 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10720 Decls := New_List (Nod);
10722 -- If the formal F has a box, then the generic declarations are
10723 -- visible in the generic G. In an instance of G, the corresponding
10724 -- entities in the actual for F (which are the actuals for the
10725 -- instantiation of the generic that F denotes) must also be made
10726 -- visible for analysis of the current instance. On exit from the
10727 -- current instance, those entities are made private again. If the
10728 -- actual is currently in use, these entities are also use-visible.
10730 -- The loop through the actual entities also steps through the formal
10731 -- entities and enters associations from formals to actuals into the
10732 -- renaming map. This is necessary to properly handle checking of
10733 -- actual parameter associations for later formals that depend on
10734 -- actuals declared in the formal package.
10736 -- In Ada 2005, partial parameterization requires that we make
10737 -- visible the actuals corresponding to formals that were defaulted
10738 -- in the formal package. There formals are identified because they
10739 -- remain formal generics within the formal package, rather than
10740 -- being renamings of the actuals supplied.
10742 declare
10743 Gen_Decl : constant Node_Id :=
10744 Unit_Declaration_Node (Gen_Parent);
10745 Formals : constant List_Id :=
10746 Generic_Formal_Declarations (Gen_Decl);
10748 Actual_Ent : Entity_Id;
10749 Actual_Of_Formal : Node_Id;
10750 Formal_Node : Node_Id;
10751 Formal_Ent : Entity_Id;
10753 begin
10754 if Present (Formals) then
10755 Formal_Node := First_Non_Pragma (Formals);
10756 else
10757 Formal_Node := Empty;
10758 end if;
10760 Actual_Ent := First_Entity (Actual_Pack);
10761 Actual_Of_Formal :=
10762 First (Visible_Declarations (Specification (Analyzed_Formal)));
10763 while Present (Actual_Ent)
10764 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10765 loop
10766 if Present (Formal_Node) then
10767 Formal_Ent := Get_Formal_Entity (Formal_Node);
10769 if Present (Formal_Ent) then
10770 Find_Matching_Actual (Formal_Node, Actual_Ent);
10771 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10773 -- We iterate at the same time over the actuals of the
10774 -- local package created for the formal, to determine
10775 -- which one of the formals of the original generic were
10776 -- defaulted in the formal. The corresponding actual
10777 -- entities are visible in the enclosing instance.
10779 if Box_Present (Formal)
10780 or else
10781 (Present (Actual_Of_Formal)
10782 and then
10783 Is_Generic_Formal
10784 (Get_Formal_Entity (Actual_Of_Formal)))
10785 then
10786 Set_Is_Hidden (Actual_Ent, False);
10787 Set_Is_Visible_Formal (Actual_Ent);
10788 Set_Is_Potentially_Use_Visible
10789 (Actual_Ent, In_Use (Actual_Pack));
10791 if Ekind (Actual_Ent) = E_Package then
10792 Process_Nested_Formal (Actual_Ent);
10793 end if;
10795 else
10796 if not Is_Hidden (Actual_Ent) then
10797 Append_Elmt (Actual_Ent, Hidden_Formals);
10798 end if;
10800 Set_Is_Hidden (Actual_Ent);
10801 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10802 end if;
10803 end if;
10805 Next_Non_Pragma (Formal_Node);
10806 Next (Actual_Of_Formal);
10808 -- A formal subprogram may be overloaded, so advance in
10809 -- the list of actuals to make sure we do not match two
10810 -- successive formals to the same actual. This is only
10811 -- relevant for overloadable entities, others have
10812 -- distinct names.
10814 if Is_Overloadable (Actual_Ent) then
10815 Next_Entity (Actual_Ent);
10816 end if;
10818 else
10819 -- No further formals to match, but the generic part may
10820 -- contain inherited operation that are not hidden in the
10821 -- enclosing instance.
10823 Next_Entity (Actual_Ent);
10824 end if;
10825 end loop;
10827 -- Inherited subprograms generated by formal derived types are
10828 -- also visible if the types are.
10830 Actual_Ent := First_Entity (Actual_Pack);
10831 while Present (Actual_Ent)
10832 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10833 loop
10834 if Is_Overloadable (Actual_Ent)
10835 and then
10836 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10837 and then
10838 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10839 then
10840 Set_Is_Hidden (Actual_Ent, False);
10841 Set_Is_Potentially_Use_Visible
10842 (Actual_Ent, In_Use (Actual_Pack));
10843 end if;
10845 Next_Entity (Actual_Ent);
10846 end loop;
10847 end;
10849 -- If the formal requires conformance checking, reanalyze it as an
10850 -- abbreviated instantiation, to verify the matching rules of 12.7.
10851 -- The actual checks are performed after the generic associations
10852 -- have been analyzed, to guarantee the same visibility for this
10853 -- instantiation and for the actuals.
10855 -- In Ada 2005, the generic associations for the formal can include
10856 -- defaulted parameters. These are ignored during check. This
10857 -- internal instantiation is removed from the tree after conformance
10858 -- checking, because it contains formal declarations for those
10859 -- defaulted parameters, and those should not reach the back-end.
10861 if Requires_Conformance_Checking (Formal) then
10862 declare
10863 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10865 I_Nam : Node_Id;
10867 begin
10868 Set_Is_Internal (I_Pack);
10869 Mutate_Ekind (I_Pack, E_Package);
10871 -- Insert the package into the list of its hidden entities so
10872 -- that the list is not empty for Is_Abbreviated_Instance.
10874 Append_Elmt (I_Pack, Hidden_Formals);
10876 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10878 -- If the generic is a child unit, Check_Generic_Child_Unit
10879 -- needs its original name in case it is qualified.
10881 if Is_Child_Unit (Gen_Parent) then
10882 I_Nam :=
10883 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
10884 pragma Assert (Entity (I_Nam) = Gen_Parent);
10886 else
10887 I_Nam :=
10888 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
10889 end if;
10891 Append_To (Decls,
10892 Make_Package_Instantiation (Loc,
10893 Defining_Unit_Name => I_Pack,
10894 Name => I_Nam,
10895 Generic_Associations => Generic_Associations (Formal)));
10896 end;
10897 end if;
10899 return Decls;
10900 end if;
10901 end Instantiate_Formal_Package;
10903 -----------------------------------
10904 -- Instantiate_Formal_Subprogram --
10905 -----------------------------------
10907 function Instantiate_Formal_Subprogram
10908 (Formal : Node_Id;
10909 Actual : Node_Id;
10910 Analyzed_Formal : Node_Id) return Node_Id
10912 Analyzed_S : constant Entity_Id :=
10913 Defining_Unit_Name (Specification (Analyzed_Formal));
10914 Formal_Sub : constant Entity_Id :=
10915 Defining_Unit_Name (Specification (Formal));
10917 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10918 -- If the generic is a child unit, the parent has been installed on the
10919 -- scope stack, but a default subprogram cannot resolve to something
10920 -- on the parent because that parent is not really part of the visible
10921 -- context (it is there to resolve explicit local entities). If the
10922 -- default has resolved in this way, we remove the entity from immediate
10923 -- visibility and analyze the node again to emit an error message or
10924 -- find another visible candidate.
10926 procedure Valid_Actual_Subprogram (Act : Node_Id);
10927 -- Perform legality check and raise exception on failure
10929 -----------------------
10930 -- From_Parent_Scope --
10931 -----------------------
10933 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10934 Gen_Scope : Node_Id;
10936 begin
10937 Gen_Scope := Scope (Analyzed_S);
10938 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10939 if Scope (Subp) = Scope (Gen_Scope) then
10940 return True;
10941 end if;
10943 Gen_Scope := Scope (Gen_Scope);
10944 end loop;
10946 return False;
10947 end From_Parent_Scope;
10949 -----------------------------
10950 -- Valid_Actual_Subprogram --
10951 -----------------------------
10953 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10954 Act_E : Entity_Id;
10956 begin
10957 if Is_Entity_Name (Act) then
10958 Act_E := Entity (Act);
10960 elsif Nkind (Act) = N_Selected_Component
10961 and then Is_Entity_Name (Selector_Name (Act))
10962 then
10963 Act_E := Entity (Selector_Name (Act));
10965 else
10966 Act_E := Empty;
10967 end if;
10969 -- The actual for a ghost generic formal procedure should be a ghost
10970 -- procedure (SPARK RM 6.9(14)).
10972 if Present (Act_E)
10973 and then Ekind (Act_E) = E_Procedure
10974 then
10975 Check_Ghost_Formal_Procedure_Or_Package
10976 (N => Act,
10977 Actual => Act_E,
10978 Formal => Analyzed_S);
10979 end if;
10981 if (Present (Act_E) and then Is_Overloadable (Act_E))
10982 or else Nkind (Act) in N_Attribute_Reference
10983 | N_Indexed_Component
10984 | N_Character_Literal
10985 | N_Explicit_Dereference
10986 then
10987 return;
10988 end if;
10990 Error_Msg_NE
10991 ("expect subprogram or entry name in instantiation of &",
10992 Instantiation_Node, Formal_Sub);
10993 Abandon_Instantiation (Instantiation_Node);
10994 end Valid_Actual_Subprogram;
10996 -- Local variables
10998 Decl_Node : Node_Id;
10999 Loc : Source_Ptr;
11000 Nam : Node_Id;
11001 New_Spec : Node_Id;
11002 New_Subp : Entity_Id;
11004 -- Start of processing for Instantiate_Formal_Subprogram
11006 begin
11007 New_Spec := New_Copy_Tree (Specification (Formal));
11009 -- The tree copy has created the proper instantiation sloc for the
11010 -- new specification. Use this location for all other constructed
11011 -- declarations.
11013 Loc := Sloc (Defining_Unit_Name (New_Spec));
11015 -- Create new entity for the actual (New_Copy_Tree does not), and
11016 -- indicate that it is an actual.
11018 -- If the actual is not an entity (i.e. an attribute reference)
11019 -- and the formal includes aspect specifications for contracts,
11020 -- we create an internal name for the renaming declaration. The
11021 -- constructed wrapper contains a call to the entity in the renaming.
11022 -- This is an expansion activity, as is the wrapper creation.
11024 if Ada_Version >= Ada_2022
11025 and then Has_Contracts (Analyzed_Formal)
11026 and then not Is_Entity_Name (Actual)
11027 and then Expander_Active
11028 then
11029 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11030 else
11031 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11032 end if;
11034 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11035 Set_Is_Generic_Actual_Subprogram (New_Subp);
11036 Set_Defining_Unit_Name (New_Spec, New_Subp);
11038 -- Create new entities for the each of the formals in the specification
11039 -- of the renaming declaration built for the actual.
11041 if Present (Parameter_Specifications (New_Spec)) then
11042 declare
11043 F : Node_Id;
11044 F_Id : Entity_Id;
11046 begin
11047 F := First (Parameter_Specifications (New_Spec));
11048 while Present (F) loop
11049 F_Id := Defining_Identifier (F);
11051 Set_Defining_Identifier (F,
11052 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11053 Next (F);
11054 end loop;
11055 end;
11056 end if;
11058 -- Find entity of actual. If the actual is an attribute reference, it
11059 -- cannot be resolved here (its formal is missing) but is handled
11060 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11061 -- fully resolved subsequently, when the renaming declaration for the
11062 -- formal is analyzed. If it is an explicit dereference, resolve the
11063 -- prefix but not the actual itself, to prevent interpretation as call.
11065 if Present (Actual) then
11066 Loc := Sloc (Actual);
11067 Set_Sloc (New_Spec, Loc);
11069 if Nkind (Actual) = N_Operator_Symbol then
11070 Find_Direct_Name (Actual);
11072 elsif Nkind (Actual) = N_Explicit_Dereference then
11073 Analyze (Prefix (Actual));
11075 elsif Nkind (Actual) /= N_Attribute_Reference then
11076 Analyze (Actual);
11077 end if;
11079 Valid_Actual_Subprogram (Actual);
11080 Nam := Actual;
11082 elsif Present (Default_Name (Formal)) then
11083 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11084 | N_Selected_Component
11085 | N_Indexed_Component
11086 | N_Character_Literal
11087 and then Present (Entity (Default_Name (Formal)))
11088 then
11089 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11090 else
11091 Nam := New_Copy (Default_Name (Formal));
11092 Set_Sloc (Nam, Loc);
11093 end if;
11095 elsif Box_Present (Formal) then
11097 -- Actual is resolved at the point of instantiation. Create an
11098 -- identifier or operator with the same name as the formal.
11100 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11101 Nam :=
11102 Make_Operator_Symbol (Loc,
11103 Chars => Chars (Formal_Sub),
11104 Strval => No_String);
11105 else
11106 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11107 end if;
11109 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11110 and then Null_Present (Specification (Formal))
11111 then
11112 -- Generate null body for procedure, for use in the instance
11114 Decl_Node :=
11115 Make_Subprogram_Body (Loc,
11116 Specification => New_Spec,
11117 Declarations => New_List,
11118 Handled_Statement_Sequence =>
11119 Make_Handled_Sequence_Of_Statements (Loc,
11120 Statements => New_List (Make_Null_Statement (Loc))));
11122 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11124 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11126 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11128 -- Eliminate the calls to it when optimization is enabled
11130 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11131 return Decl_Node;
11133 -- Handle case of a formal function with an expression default (allowed
11134 -- when extensions are enabled).
11136 elsif Nkind (Specification (Formal)) = N_Function_Specification
11137 and then Present (Expression (Formal))
11138 then
11139 -- Generate body for function, for use in the instance
11141 declare
11142 Expr : constant Node_Id := New_Copy (Expression (Formal));
11143 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11144 begin
11145 Set_Sloc (Expr, Loc);
11146 Set_Expression (Stmt, Expr);
11148 Decl_Node :=
11149 Make_Subprogram_Body (Loc,
11150 Specification => New_Spec,
11151 Declarations => New_List,
11152 Handled_Statement_Sequence =>
11153 Make_Handled_Sequence_Of_Statements (Loc,
11154 Statements => New_List (Stmt)));
11155 end;
11157 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11158 -- has convention Intrinsic.
11160 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11162 -- Inline calls to it when optimization is enabled
11164 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11165 return Decl_Node;
11167 else
11168 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11169 Error_Msg_NE
11170 ("missing actual&", Instantiation_Node, Formal_Sub);
11171 Error_Msg_NE
11172 ("\in instantiation of & declared#",
11173 Instantiation_Node, Scope (Analyzed_S));
11174 Abandon_Instantiation (Instantiation_Node);
11175 end if;
11177 Decl_Node :=
11178 Make_Subprogram_Renaming_Declaration (Loc,
11179 Specification => New_Spec,
11180 Name => Nam);
11182 -- If we do not have an actual and the formal specified <> then set to
11183 -- get proper default.
11185 if No (Actual) and then Box_Present (Formal) then
11186 Set_From_Default (Decl_Node);
11187 end if;
11189 -- Gather possible interpretations for the actual before analyzing the
11190 -- instance. If overloaded, it will be resolved when analyzing the
11191 -- renaming declaration.
11193 if Box_Present (Formal) and then No (Actual) then
11194 Analyze (Nam);
11196 if Is_Child_Unit (Scope (Analyzed_S))
11197 and then Present (Entity (Nam))
11198 then
11199 if not Is_Overloaded (Nam) then
11200 if From_Parent_Scope (Entity (Nam)) then
11201 Set_Is_Immediately_Visible (Entity (Nam), False);
11202 Set_Entity (Nam, Empty);
11203 Set_Etype (Nam, Empty);
11205 Analyze (Nam);
11206 Set_Is_Immediately_Visible (Entity (Nam));
11207 end if;
11209 else
11210 declare
11211 I : Interp_Index;
11212 It : Interp;
11214 begin
11215 Get_First_Interp (Nam, I, It);
11216 while Present (It.Nam) loop
11217 if From_Parent_Scope (It.Nam) then
11218 Remove_Interp (I);
11219 end if;
11221 Get_Next_Interp (I, It);
11222 end loop;
11223 end;
11224 end if;
11225 end if;
11226 end if;
11228 -- The generic instantiation freezes the actual. This can only be done
11229 -- once the actual is resolved, in the analysis of the renaming
11230 -- declaration. To make the formal subprogram entity available, we set
11231 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11232 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11233 -- of formal abstract subprograms.
11235 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11237 -- We cannot analyze the renaming declaration, and thus find the actual,
11238 -- until all the actuals are assembled in the instance. For subsequent
11239 -- checks of other actuals, indicate the node that will hold the
11240 -- instance of this formal.
11242 Set_Instance_Of (Analyzed_S, Nam);
11244 if Nkind (Actual) = N_Selected_Component
11245 and then Is_Task_Type (Etype (Prefix (Actual)))
11246 and then not Is_Frozen (Etype (Prefix (Actual)))
11247 then
11248 -- The renaming declaration will create a body, which must appear
11249 -- outside of the instantiation, We move the renaming declaration
11250 -- out of the instance, and create an additional renaming inside,
11251 -- to prevent freezing anomalies.
11253 declare
11254 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11256 begin
11257 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11258 Insert_Before (Instantiation_Node, Decl_Node);
11259 Analyze (Decl_Node);
11261 -- Now create renaming within the instance
11263 Decl_Node :=
11264 Make_Subprogram_Renaming_Declaration (Loc,
11265 Specification => New_Copy_Tree (New_Spec),
11266 Name => New_Occurrence_Of (Anon_Id, Loc));
11268 Set_Defining_Unit_Name (Specification (Decl_Node),
11269 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11270 end;
11271 end if;
11273 return Decl_Node;
11274 end Instantiate_Formal_Subprogram;
11276 ------------------------
11277 -- Instantiate_Object --
11278 ------------------------
11280 function Instantiate_Object
11281 (Formal : Node_Id;
11282 Actual : Node_Id;
11283 Analyzed_Formal : Node_Id) return List_Id
11285 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11286 A_Gen_Obj : constant Entity_Id :=
11287 Defining_Identifier (Analyzed_Formal);
11288 Acc_Def : Node_Id := Empty;
11289 Act_Assoc : constant Node_Id :=
11290 (if No (Actual) then Empty else Parent (Actual));
11291 Actual_Decl : Node_Id := Empty;
11292 Decl_Node : Node_Id;
11293 Def : Node_Id;
11294 Ftyp : Entity_Id;
11295 List : constant List_Id := New_List;
11296 Loc : constant Source_Ptr := Sloc (Actual);
11297 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11298 Subt_Decl : Node_Id := Empty;
11299 Subt_Mark : Node_Id := Empty;
11301 -- Start of processing for Instantiate_Object
11303 begin
11304 -- Formal may be an anonymous access
11306 if Present (Subtype_Mark (Formal)) then
11307 Subt_Mark := Subtype_Mark (Formal);
11308 else
11309 Check_Access_Definition (Formal);
11310 Acc_Def := Access_Definition (Formal);
11311 end if;
11313 -- Sloc for error message on missing actual
11315 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11317 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11318 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11319 end if;
11321 Set_Parent (List, Act_Assoc);
11323 -- OUT present
11325 if Out_Present (Formal) then
11327 -- An IN OUT generic actual must be a name. The instantiation is a
11328 -- renaming declaration. The actual is the name being renamed. We
11329 -- use the actual directly, rather than a copy, because it is not
11330 -- used further in the list of actuals, and because a copy or a use
11331 -- of relocate_node is incorrect if the instance is nested within a
11332 -- generic. In order to simplify e.g. ASIS queries, the
11333 -- Generic_Parent field links the declaration to the generic
11334 -- association.
11336 if No (Actual) then
11337 Error_Msg_NE
11338 ("missing actual &",
11339 Instantiation_Node, Gen_Obj);
11340 Error_Msg_NE
11341 ("\in instantiation of & declared#",
11342 Instantiation_Node, Scope (A_Gen_Obj));
11343 Abandon_Instantiation (Instantiation_Node);
11344 end if;
11346 if Present (Subt_Mark) then
11347 Decl_Node :=
11348 Make_Object_Renaming_Declaration (Loc,
11349 Defining_Identifier => New_Copy (Gen_Obj),
11350 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11351 Name => Actual);
11353 else pragma Assert (Present (Acc_Def));
11354 Decl_Node :=
11355 Make_Object_Renaming_Declaration (Loc,
11356 Defining_Identifier => New_Copy (Gen_Obj),
11357 Access_Definition => New_Copy_Tree (Acc_Def),
11358 Name => Actual);
11359 end if;
11361 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11363 -- The analysis of the actual may produce Insert_Action nodes, so
11364 -- the declaration must have a context in which to attach them.
11366 Append (Decl_Node, List);
11367 Analyze (Actual);
11369 -- Return if the analysis of the actual reported some error
11371 if Etype (Actual) = Any_Type then
11372 return List;
11373 end if;
11375 -- This check is performed here because Analyze_Object_Renaming will
11376 -- not check it when Comes_From_Source is False. Note though that the
11377 -- check for the actual being the name of an object will be performed
11378 -- in Analyze_Object_Renaming.
11380 if Is_Object_Reference (Actual)
11381 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11382 then
11383 Error_Msg_N
11384 ("illegal discriminant-dependent component for in out parameter",
11385 Actual);
11386 end if;
11388 -- The actual has to be resolved in order to check that it is a
11389 -- variable (due to cases such as F (1), where F returns access to
11390 -- an array, and for overloaded prefixes).
11392 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11394 -- If the type of the formal is not itself a formal, and the current
11395 -- unit is a child unit, the formal type must be declared in a
11396 -- parent, and must be retrieved by visibility.
11398 if Ftyp = Orig_Ftyp
11399 and then Is_Generic_Unit (Scope (Ftyp))
11400 and then Is_Child_Unit (Scope (A_Gen_Obj))
11401 then
11402 declare
11403 Temp : constant Node_Id :=
11404 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11405 begin
11406 Set_Entity (Temp, Empty);
11407 Find_Type (Temp);
11408 Ftyp := Entity (Temp);
11409 end;
11410 end if;
11412 if Is_Private_Type (Ftyp)
11413 and then not Is_Private_Type (Etype (Actual))
11414 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11415 or else Base_Type (Etype (Actual)) = Ftyp)
11416 then
11417 -- If the actual has the type of the full view of the formal, or
11418 -- else a non-private subtype of the formal, then the visibility
11419 -- of the formal type has changed. Add to the actuals a subtype
11420 -- declaration that will force the exchange of views in the body
11421 -- of the instance as well.
11423 Subt_Decl :=
11424 Make_Subtype_Declaration (Loc,
11425 Defining_Identifier => Make_Temporary (Loc, 'P'),
11426 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11428 Prepend (Subt_Decl, List);
11430 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11431 Exchange_Declarations (Ftyp);
11432 end if;
11434 Resolve (Actual, Ftyp);
11436 if not Denotes_Variable (Actual) then
11437 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11439 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11441 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11442 -- the type of the actual shall resolve to a specific anonymous
11443 -- access type.
11445 if Ada_Version < Ada_2005
11446 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11447 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11448 then
11449 Error_Msg_NE
11450 ("type of actual does not match type of&", Actual, Gen_Obj);
11451 end if;
11452 end if;
11454 Note_Possible_Modification (Actual, Sure => True);
11456 -- Check for instantiation with atomic/volatile/VFA object actual for
11457 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11459 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11460 Error_Msg_NE
11461 ("cannot instantiate nonatomic formal & of mode in out",
11462 Actual, Gen_Obj);
11463 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11465 elsif Is_Volatile_Object_Ref (Actual)
11466 and then not Is_Volatile (Orig_Ftyp)
11467 then
11468 Error_Msg_NE
11469 ("cannot instantiate nonvolatile formal & of mode in out",
11470 Actual, Gen_Obj);
11471 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11473 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11474 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11475 then
11476 Error_Msg_NE
11477 ("cannot instantiate nonfull access formal & of mode in out",
11478 Actual, Gen_Obj);
11479 Error_Msg_N
11480 ("\with full access object actual (RM C.6(12))", Actual);
11481 end if;
11483 -- Check for instantiation on nonatomic subcomponent of a full access
11484 -- object in Ada 2022 (RM C.6 (12)).
11486 if Ada_Version >= Ada_2022
11487 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11488 and then not Is_Atomic_Object (Actual)
11489 then
11490 Error_Msg_NE
11491 ("cannot instantiate formal & of mode in out with actual",
11492 Actual, Gen_Obj);
11493 Error_Msg_N
11494 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11495 Actual);
11496 end if;
11498 -- Check actual/formal compatibility with respect to the four
11499 -- volatility refinement aspects.
11501 declare
11502 Actual_Obj : constant Entity_Id :=
11503 Get_Enclosing_Deep_Object (Actual);
11504 begin
11505 Check_Volatility_Compatibility
11506 (Actual_Obj, A_Gen_Obj, "actual object",
11507 "its corresponding formal object of mode in out",
11508 Srcpos_Bearer => Actual);
11509 end;
11511 -- The actual for a ghost generic formal IN OUT parameter should be a
11512 -- ghost object (SPARK RM 6.9(14)).
11514 Check_Ghost_Formal_Variable
11515 (Actual => Actual,
11516 Formal => A_Gen_Obj);
11518 -- Formal in-parameter
11520 else
11521 -- The instantiation of a generic formal in-parameter is constant
11522 -- declaration. The actual is the expression for that declaration.
11523 -- Its type is a full copy of the type of the formal. This may be
11524 -- an access to subprogram, for which we need to generate entities
11525 -- for the formals in the new signature.
11527 if Present (Actual) then
11528 if Present (Subt_Mark) then
11529 Def := New_Copy_Tree (Subt_Mark);
11530 else
11531 pragma Assert (Present (Acc_Def));
11532 Def := New_Copy_Tree (Acc_Def);
11533 end if;
11535 Decl_Node :=
11536 Make_Object_Declaration (Loc,
11537 Defining_Identifier => New_Copy (Gen_Obj),
11538 Constant_Present => True,
11539 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11540 Object_Definition => Def,
11541 Expression => Actual);
11543 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11544 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11546 -- A generic formal object of a tagged type is defined to be
11547 -- aliased so the new constant must also be treated as aliased.
11549 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11550 Set_Aliased_Present (Decl_Node);
11551 end if;
11553 Append (Decl_Node, List);
11555 -- The actual for a ghost generic formal IN parameter of
11556 -- access-to-variable type should be a ghost object (SPARK
11557 -- RM 6.9(14)).
11559 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11560 Check_Ghost_Formal_Variable
11561 (Actual => Actual,
11562 Formal => A_Gen_Obj);
11563 end if;
11565 -- No need to repeat (pre-)analysis of some expression nodes
11566 -- already handled in Preanalyze_Actuals.
11568 if Nkind (Actual) /= N_Allocator then
11569 Analyze (Actual);
11571 -- Return if the analysis of the actual reported some error
11573 if Etype (Actual) = Any_Type then
11574 return List;
11575 end if;
11576 end if;
11578 declare
11579 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11580 Typ : Entity_Id;
11582 begin
11583 Typ := Get_Instance_Of (Formal_Type);
11585 -- If the actual appears in the current or an enclosing scope,
11586 -- use its type directly. This is relevant if it has an actual
11587 -- subtype that is distinct from its nominal one. This cannot
11588 -- be done in general because the type of the actual may
11589 -- depend on other actuals, and only be fully determined when
11590 -- the enclosing instance is analyzed.
11592 if Present (Etype (Actual))
11593 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11594 then
11595 Freeze_Before (Instantiation_Node, Etype (Actual));
11596 else
11597 Freeze_Before (Instantiation_Node, Typ);
11598 end if;
11600 -- If the actual is an aggregate, perform name resolution on
11601 -- its components (the analysis of an aggregate does not do it)
11602 -- to capture local names that may be hidden if the generic is
11603 -- a child unit.
11605 if Nkind (Actual) = N_Aggregate then
11606 Preanalyze_And_Resolve (Actual, Typ);
11607 end if;
11609 if Is_Limited_Type (Typ)
11610 and then not OK_For_Limited_Init (Typ, Actual)
11611 then
11612 Error_Msg_N
11613 ("initialization not allowed for limited types", Actual);
11614 Explain_Limited_Type (Typ, Actual);
11615 end if;
11616 end;
11618 elsif Present (Default_Expression (Formal)) then
11620 -- Use default to construct declaration
11622 if Present (Subt_Mark) then
11623 Def := New_Copy_Tree (Subt_Mark);
11624 else
11625 pragma Assert (Present (Acc_Def));
11626 Def := New_Copy_Tree (Acc_Def);
11627 end if;
11629 Decl_Node :=
11630 Make_Object_Declaration (Sloc (Formal),
11631 Defining_Identifier => New_Copy (Gen_Obj),
11632 Constant_Present => True,
11633 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11634 Object_Definition => Def,
11635 Expression => New_Copy_Tree
11636 (Default_Expression (Formal)));
11638 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11639 Set_Corresponding_Generic_Association
11640 (Decl_Node, Expression (Decl_Node));
11642 Append (Decl_Node, List);
11643 Set_Analyzed (Expression (Decl_Node), False);
11645 else
11646 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11647 Error_Msg_NE ("\in instantiation of & declared#",
11648 Instantiation_Node, Scope (A_Gen_Obj));
11650 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11652 -- Create dummy constant declaration so that instance can be
11653 -- analyzed, to minimize cascaded visibility errors.
11655 if Present (Subt_Mark) then
11656 Def := Subt_Mark;
11657 else pragma Assert (Present (Acc_Def));
11658 Def := Acc_Def;
11659 end if;
11661 Decl_Node :=
11662 Make_Object_Declaration (Loc,
11663 Defining_Identifier => New_Copy (Gen_Obj),
11664 Constant_Present => True,
11665 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11666 Object_Definition => New_Copy (Def),
11667 Expression =>
11668 Make_Attribute_Reference (Sloc (Gen_Obj),
11669 Attribute_Name => Name_First,
11670 Prefix => New_Copy (Def)));
11672 Append (Decl_Node, List);
11674 else
11675 Abandon_Instantiation (Instantiation_Node);
11676 end if;
11677 end if;
11678 end if;
11680 if Nkind (Actual) in N_Has_Entity
11681 and then Present (Entity (Actual))
11682 then
11683 Actual_Decl := Parent (Entity (Actual));
11684 end if;
11686 -- Ada 2005 (AI-423) refined by AI12-0287:
11687 -- For an object_renaming_declaration with a null_exclusion or an
11688 -- access_definition that has a null_exclusion, the subtype of the
11689 -- object_name shall exclude null. In addition, if the
11690 -- object_renaming_declaration occurs within the body of a generic unit
11691 -- G or within the body of a generic unit declared within the
11692 -- declarative region of generic unit G, then:
11693 -- * if the object_name statically denotes a generic formal object of
11694 -- mode in out of G, then the declaration of that object shall have a
11695 -- null_exclusion;
11696 -- * if the object_name statically denotes a call of a generic formal
11697 -- function of G, then the declaration of the result of that function
11698 -- shall have a null_exclusion.
11700 if Ada_Version >= Ada_2005
11701 and then Present (Actual_Decl)
11702 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11703 | N_Object_Declaration
11704 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11705 and then not Has_Null_Exclusion (Actual_Decl)
11706 and then Has_Null_Exclusion (Analyzed_Formal)
11707 and then Ekind (Defining_Identifier (Analyzed_Formal))
11708 = E_Generic_In_Out_Parameter
11709 and then ((In_Generic_Scope (Entity (Actual))
11710 and then In_Package_Body (Scope (Entity (Actual))))
11711 or else not Can_Never_Be_Null (Etype (Actual)))
11712 then
11713 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11714 Error_Msg_N
11715 ("actual must exclude null to match generic formal#", Actual);
11716 end if;
11718 -- An effectively volatile object cannot be used as an actual in a
11719 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11720 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11721 -- legality rule, and also verifies that the actual is an object.
11723 if SPARK_Mode = On
11724 and then Present (Actual)
11725 and then Is_Object_Reference (Actual)
11726 and then Is_Effectively_Volatile_Object (Actual)
11727 and then not Is_Effectively_Volatile (A_Gen_Obj)
11728 then
11729 Error_Msg_N
11730 ("volatile object cannot act as actual in generic instantiation",
11731 Actual);
11732 end if;
11734 return List;
11735 end Instantiate_Object;
11737 ------------------------------
11738 -- Instantiate_Package_Body --
11739 ------------------------------
11741 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11742 -- must be replaced by gotos which jump to the end of the routine in order
11743 -- to restore the Ghost and SPARK modes.
11745 procedure Instantiate_Package_Body
11746 (Body_Info : Pending_Body_Info;
11747 Inlined_Body : Boolean := False;
11748 Body_Optional : Boolean := False)
11750 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11751 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11752 Act_Spec : constant Node_Id := Specification (Act_Decl);
11753 Ctx_Parents : Elist_Id := No_Elist;
11754 Ctx_Top : Int := 0;
11755 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11756 Gen_Id : constant Node_Id := Name (Inst_Node);
11757 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11758 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11759 Loc : constant Source_Ptr := Sloc (Inst_Node);
11761 procedure Check_Initialized_Types;
11762 -- In a generic package body, an entity of a generic private type may
11763 -- appear uninitialized. This is suspicious, unless the actual is a
11764 -- fully initialized type.
11766 procedure Install_Parents_Of_Generic_Context
11767 (Inst_Scope : Entity_Id;
11768 Ctx_Parents : out Elist_Id);
11769 -- Inst_Scope is the scope where the instance appears within; when it
11770 -- appears within a generic child package G, this routine collects and
11771 -- installs the enclosing packages of G in the scopes stack; installed
11772 -- packages are returned in Ctx_Parents.
11774 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11775 -- Reverse effect after instantiation is complete
11777 -----------------------------
11778 -- Check_Initialized_Types --
11779 -----------------------------
11781 procedure Check_Initialized_Types is
11782 Decl : Node_Id;
11783 Formal : Entity_Id;
11784 Actual : Entity_Id;
11785 Uninit_Var : Entity_Id;
11787 begin
11788 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11789 while Present (Decl) loop
11790 Uninit_Var := Empty;
11792 if Nkind (Decl) = N_Private_Extension_Declaration then
11793 Uninit_Var := Uninitialized_Variable (Decl);
11795 elsif Nkind (Decl) = N_Formal_Type_Declaration
11796 and then Nkind (Formal_Type_Definition (Decl)) =
11797 N_Formal_Private_Type_Definition
11798 then
11799 Uninit_Var :=
11800 Uninitialized_Variable (Formal_Type_Definition (Decl));
11801 end if;
11803 if Present (Uninit_Var) then
11804 Formal := Defining_Identifier (Decl);
11805 Actual := First_Entity (Act_Decl_Id);
11807 -- For each formal there is a subtype declaration that renames
11808 -- the actual and has the same name as the formal. Locate the
11809 -- formal for warning message about uninitialized variables
11810 -- in the generic, for which the actual type should be a fully
11811 -- initialized type.
11813 while Present (Actual) loop
11814 exit when Ekind (Actual) = E_Package
11815 and then Present (Renamed_Entity (Actual));
11817 if Chars (Actual) = Chars (Formal)
11818 and then not Is_Scalar_Type (Actual)
11819 and then not Is_Fully_Initialized_Type (Actual)
11820 and then Warn_On_No_Value_Assigned
11821 then
11822 Error_Msg_Node_2 := Formal;
11823 Error_Msg_NE
11824 ("generic unit has uninitialized variable& of "
11825 & "formal private type &?v?", Actual, Uninit_Var);
11826 Error_Msg_NE
11827 ("actual type for& should be fully initialized type?v?",
11828 Actual, Formal);
11829 exit;
11830 end if;
11832 Next_Entity (Actual);
11833 end loop;
11834 end if;
11836 Next (Decl);
11837 end loop;
11838 end Check_Initialized_Types;
11840 ----------------------------------------
11841 -- Install_Parents_Of_Generic_Context --
11842 ----------------------------------------
11844 procedure Install_Parents_Of_Generic_Context
11845 (Inst_Scope : Entity_Id;
11846 Ctx_Parents : out Elist_Id)
11848 Elmt : Elmt_Id;
11849 S : Entity_Id;
11851 begin
11852 Ctx_Parents := New_Elmt_List;
11854 -- Collect context parents (ie. parents where the instantiation
11855 -- appears within).
11857 S := Inst_Scope;
11858 while S /= Standard_Standard loop
11859 Prepend_Elmt (S, Ctx_Parents);
11860 S := Scope (S);
11861 end loop;
11863 -- Install enclosing parents
11865 Elmt := First_Elmt (Ctx_Parents);
11866 while Present (Elmt) loop
11867 Push_Scope (Node (Elmt));
11868 Set_Is_Immediately_Visible (Node (Elmt));
11869 Next_Elmt (Elmt);
11870 end loop;
11871 end Install_Parents_Of_Generic_Context;
11873 ---------------------------------------
11874 -- Remove_Parents_Of_Generic_Context --
11875 ---------------------------------------
11877 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11878 Elmt : Elmt_Id;
11880 begin
11881 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11883 Elmt := Last_Elmt (Ctx_Parents);
11884 while Present (Elmt) loop
11885 pragma Assert (Current_Scope = Node (Elmt));
11886 Set_Is_Immediately_Visible (Current_Scope, False);
11887 Pop_Scope;
11889 Remove_Last_Elmt (Ctx_Parents);
11890 Elmt := Last_Elmt (Ctx_Parents);
11891 end loop;
11892 end Remove_Parents_Of_Generic_Context;
11894 -- Local variables
11896 -- The following constants capture the context prior to instantiating
11897 -- the package body.
11899 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11900 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11901 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11902 Saved_ISMP : constant Boolean :=
11903 Ignore_SPARK_Mode_Pragmas_In_Instance;
11904 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11905 Local_Suppress_Stack_Top;
11906 Saved_SC : constant Boolean := Style_Check;
11907 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11908 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11909 Saved_SS : constant Suppress_Record := Scope_Suppress;
11910 Saved_Warn : constant Warnings_State := Save_Warnings;
11912 Act_Body : Node_Id;
11913 Act_Body_Id : Entity_Id;
11914 Act_Body_Name : Node_Id;
11915 Gen_Body : Node_Id;
11916 Gen_Body_Id : Node_Id;
11917 Par_Ent : Entity_Id := Empty;
11918 Par_Installed : Boolean := False;
11919 Par_Vis : Boolean := False;
11921 Scope_Check_Id : Entity_Id;
11922 Scope_Check_Last : Nat;
11923 -- Value of Current_Scope before calls to Install_Parents; used to check
11924 -- that scopes are correctly removed after instantiation.
11926 Vis_Prims_List : Elist_Id := No_Elist;
11927 -- List of primitives made temporarily visible in the instantiation
11928 -- to match the visibility of the formal type.
11930 -- Start of processing for Instantiate_Package_Body
11932 begin
11933 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11935 -- The instance body may already have been processed, as the parent of
11936 -- another instance that is inlined (Load_Parent_Of_Generic).
11938 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11939 return;
11940 end if;
11942 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11944 -- Re-establish the state of information on which checks are suppressed.
11945 -- This information was set in Body_Info at the point of instantiation,
11946 -- and now we restore it so that the instance is compiled using the
11947 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11949 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11950 Scope_Suppress := Body_Info.Scope_Suppress;
11952 Restore_Config_Switches (Body_Info.Config_Switches);
11953 Restore_Warnings (Body_Info.Warnings);
11955 if No (Gen_Body_Id) then
11957 -- Do not look for parent of generic body if none is required.
11958 -- This may happen when the routine is called as part of the
11959 -- Pending_Instantiations processing, when nested instances
11960 -- may precede the one generated from the main unit.
11962 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11963 and then Body_Optional
11964 then
11965 goto Leave;
11966 else
11967 Load_Parent_Of_Generic
11968 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11970 -- Surprisingly enough, loading the body of the parent can cause
11971 -- the body to be instantiated and the double instantiation needs
11972 -- to be prevented in order to avoid giving bogus semantic errors.
11974 -- This case can occur because of the Collect_Previous_Instances
11975 -- machinery of Load_Parent_Of_Generic, which will instantiate
11976 -- bodies that are deemed to be ahead of the body of the parent
11977 -- in the compilation unit. But the relative position of these
11978 -- bodies is computed using the mere comparison of their Sloc.
11980 -- Now suppose that you have two generic packages G and H, with
11981 -- G containing a mere instantiation of H:
11983 -- generic
11984 -- package H is
11986 -- generic
11987 -- package Nested_G is
11988 -- ...
11989 -- end Nested_G;
11991 -- end H;
11993 -- with H;
11995 -- generic
11996 -- package G is
11998 -- package My_H is new H;
12000 -- end G;
12002 -- and a third package Q instantiating G and Nested_G:
12004 -- with G;
12006 -- package Q is
12008 -- package My_G is new G;
12010 -- package My_Nested_G is new My_G.My_H.Nested_G;
12012 -- end Q;
12014 -- The body to be instantiated is that of My_Nested_G and its
12015 -- parent is the instance My_G.My_H. This latter instantiation
12016 -- is done when My_G is analyzed, i.e. after the declarations
12017 -- of My_G and My_Nested_G have been parsed; as a result, the
12018 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12020 -- Therefore loading the body of My_G.My_H will cause the body
12021 -- of My_Nested_G to be instantiated because it is deemed to be
12022 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12023 -- will again be invoked on My_G.My_H, but this time with the
12024 -- Collect_Previous_Instances machinery disabled, so there is
12025 -- no endless mutual recursion and things are done in order.
12027 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12028 goto Leave;
12029 end if;
12031 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12032 end if;
12033 end if;
12035 -- Establish global variable for sloc adjustment and for error recovery
12036 -- In the case of an instance body for an instantiation with actuals
12037 -- from a limited view, the instance body is placed at the beginning
12038 -- of the enclosing package body: use the body entity as the source
12039 -- location for nodes of the instance body.
12041 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12042 declare
12043 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12044 Body_Id : constant Node_Id :=
12045 Corresponding_Body (Unit_Declaration_Node (Scop));
12047 begin
12048 Instantiation_Node := Body_Id;
12049 end;
12050 else
12051 Instantiation_Node := Inst_Node;
12052 end if;
12054 -- The package being instantiated may be subject to pragma Ghost. Set
12055 -- the mode now to ensure that any nodes generated during instantiation
12056 -- are properly marked as Ghost.
12058 Set_Ghost_Mode (Act_Decl_Id);
12060 if Present (Gen_Body_Id) then
12061 Save_Env (Gen_Unit, Act_Decl_Id);
12062 Style_Check := False;
12064 -- If the context of the instance is subject to SPARK_Mode "off", the
12065 -- annotation is missing, or the body is instantiated at a later pass
12066 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12067 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12068 -- instance.
12070 if SPARK_Mode /= On
12071 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12072 then
12073 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12074 end if;
12076 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12077 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12079 Create_Instantiation_Source
12080 (Inst_Node, Gen_Body_Id, S_Adjustment);
12082 Act_Body :=
12083 Copy_Generic_Node
12084 (Original_Node (Gen_Body), Empty, Instantiating => True);
12086 -- Create proper (possibly qualified) defining name for the body, to
12087 -- correspond to the one in the spec.
12089 Act_Body_Id :=
12090 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12091 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12093 -- Some attributes of spec entity are not inherited by body entity
12095 Set_Handler_Records (Act_Body_Id, No_List);
12097 if Nkind (Defining_Unit_Name (Act_Spec)) =
12098 N_Defining_Program_Unit_Name
12099 then
12100 Act_Body_Name :=
12101 Make_Defining_Program_Unit_Name (Loc,
12102 Name =>
12103 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12104 Defining_Identifier => Act_Body_Id);
12105 else
12106 Act_Body_Name := Act_Body_Id;
12107 end if;
12109 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12111 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12112 Check_Generic_Actuals (Act_Decl_Id, False);
12113 Check_Initialized_Types;
12115 -- Install primitives hidden at the point of the instantiation but
12116 -- visible when processing the generic formals
12118 declare
12119 E : Entity_Id;
12121 begin
12122 E := First_Entity (Act_Decl_Id);
12123 while Present (E) loop
12124 if Is_Type (E)
12125 and then not Is_Itype (E)
12126 and then Is_Generic_Actual_Type (E)
12127 and then Is_Tagged_Type (E)
12128 then
12129 Install_Hidden_Primitives
12130 (Prims_List => Vis_Prims_List,
12131 Gen_T => Generic_Parent_Type (Parent (E)),
12132 Act_T => E);
12133 end if;
12135 Next_Entity (E);
12136 end loop;
12137 end;
12139 Scope_Check_Id := Current_Scope;
12140 Scope_Check_Last := Scope_Stack.Last;
12142 -- If the instantiation appears within a generic child some actual
12143 -- parameter may be the current instance of the enclosing generic
12144 -- parent.
12146 declare
12147 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12149 begin
12150 if Is_Child_Unit (Inst_Scope)
12151 and then Ekind (Inst_Scope) = E_Generic_Package
12152 and then Present (Generic_Associations (Inst_Node))
12153 then
12154 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12156 -- Hide them from visibility; required to avoid conflicts
12157 -- installing the parent instance.
12159 if Present (Ctx_Parents) then
12160 Push_Scope (Standard_Standard);
12161 Ctx_Top := Scope_Stack.Last;
12162 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12163 end if;
12164 end if;
12165 end;
12167 -- If it is a child unit, make the parent instance (which is an
12168 -- instance of the parent of the generic) visible.
12170 -- 1) The child unit's parent is an explicit parent instance (the
12171 -- prefix of the name of the generic unit):
12173 -- package Child_Package is new Parent_Instance.Child_Unit;
12175 -- 2) The child unit's parent is an implicit parent instance (e.g.
12176 -- when instantiating a sibling package):
12178 -- generic
12179 -- package Parent.Second_Child is
12180 -- ...
12182 -- generic
12183 -- package Parent.First_Child is
12184 -- package Sibling_Package is new Second_Child;
12186 -- 3) The child unit's parent is not an instance, so the scope is
12187 -- simply the one of the unit.
12189 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12190 and then Nkind (Gen_Id) = N_Expanded_Name
12191 then
12192 Par_Ent := Entity (Prefix (Gen_Id));
12194 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12195 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12196 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12197 and then Nkind
12198 (Name (Get_Unit_Instantiation_Node
12199 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12200 then
12201 Par_Ent := Entity
12202 (Prefix (Name (Get_Unit_Instantiation_Node
12203 (Scope (Act_Decl_Id)))));
12205 elsif Is_Child_Unit (Gen_Unit) then
12206 Par_Ent := Scope (Gen_Unit);
12207 end if;
12209 if Present (Par_Ent) then
12210 Par_Vis := Is_Immediately_Visible (Par_Ent);
12211 Install_Parent (Par_Ent, In_Body => True);
12212 Par_Installed := True;
12213 end if;
12215 -- If the instantiation is a library unit, and this is the main unit,
12216 -- then build the resulting compilation unit nodes for the instance.
12217 -- If this is a compilation unit but it is not the main unit, then it
12218 -- is the body of a unit in the context, that is being compiled
12219 -- because it is encloses some inlined unit or another generic unit
12220 -- being instantiated. In that case, this body is not part of the
12221 -- current compilation, and is not attached to the tree, but its
12222 -- parent must be set for analysis.
12224 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12226 -- Replace instance node with body of instance, and create new
12227 -- node for corresponding instance declaration.
12229 Build_Instance_Compilation_Unit_Nodes
12230 (Inst_Node, Act_Body, Act_Decl);
12232 -- If the instantiation appears within a generic child package
12233 -- enable visibility of current instance of enclosing generic
12234 -- parents.
12236 if Present (Ctx_Parents) then
12237 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12238 Analyze (Inst_Node);
12239 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12240 else
12241 Analyze (Inst_Node);
12242 end if;
12244 if Parent (Inst_Node) = Cunit (Main_Unit) then
12246 -- If the instance is a child unit itself, then set the scope
12247 -- of the expanded body to be the parent of the instantiation
12248 -- (ensuring that the fully qualified name will be generated
12249 -- for the elaboration subprogram).
12251 if Nkind (Defining_Unit_Name (Act_Spec)) =
12252 N_Defining_Program_Unit_Name
12253 then
12254 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12255 end if;
12256 end if;
12258 -- Case where instantiation is not a library unit
12260 else
12261 -- Handle the case of an instance with incomplete actual types.
12262 -- The instance body cannot be placed just after the declaration
12263 -- because full views have not been seen yet. Any use of the non-
12264 -- limited views in the instance body requires the presence of a
12265 -- regular with_clause in the enclosing unit. Therefore we place
12266 -- the instance body at the beginning of the enclosing body, and
12267 -- the freeze node for the instance is then placed after the body.
12269 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12270 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12271 then
12272 declare
12273 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12274 Body_Id : constant Node_Id :=
12275 Corresponding_Body (Unit_Declaration_Node (Scop));
12277 F_Node : Node_Id;
12279 begin
12280 pragma Assert (Present (Body_Id));
12282 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12284 if Expander_Active then
12285 Ensure_Freeze_Node (Act_Decl_Id);
12286 F_Node := Freeze_Node (Act_Decl_Id);
12287 Set_Is_Frozen (Act_Decl_Id, False);
12288 if Is_List_Member (F_Node) then
12289 Remove (F_Node);
12290 end if;
12292 Insert_After (Act_Body, F_Node);
12293 end if;
12294 end;
12296 else
12297 Insert_Before (Inst_Node, Act_Body);
12298 Mark_Rewrite_Insertion (Act_Body);
12300 -- Insert the freeze node for the instance if need be
12302 if Expander_Active then
12303 Freeze_Package_Instance
12304 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12305 Set_Is_Frozen (Act_Decl_Id);
12306 end if;
12307 end if;
12309 -- If the instantiation appears within a generic child package
12310 -- enable visibility of current instance of enclosing generic
12311 -- parents.
12313 if Present (Ctx_Parents) then
12314 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12315 Analyze (Act_Body);
12316 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12317 else
12318 Analyze (Act_Body);
12319 end if;
12320 end if;
12322 Inherit_Context (Gen_Body, Inst_Node);
12324 if Par_Installed then
12325 Remove_Parent (In_Body => True);
12327 -- Restore the previous visibility of the parent
12329 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12330 end if;
12332 -- Remove the parent instances if they have been placed on the scope
12333 -- stack to compile the body.
12335 if Present (Ctx_Parents) then
12336 pragma Assert (Scope_Stack.Last = Ctx_Top
12337 and then Current_Scope = Standard_Standard);
12338 Pop_Scope;
12340 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12341 end if;
12343 pragma Assert (Current_Scope = Scope_Check_Id);
12344 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12346 Restore_Hidden_Primitives (Vis_Prims_List);
12348 -- Restore the private views that were made visible when the body of
12349 -- the instantiation was created. Note that, in the case where one of
12350 -- these private views is declared in the parent, there is a nesting
12351 -- issue with the calls to Install_Parent and Remove_Parent made in
12352 -- between above with In_Body set to True, because these calls also
12353 -- want to swap and restore this private view respectively. In this
12354 -- case, the call to Install_Parent does nothing, but the call to
12355 -- Remove_Parent does restore the private view, thus undercutting the
12356 -- call to Restore_Private_Views. That's OK under the condition that
12357 -- the two mechanisms swap exactly the same entities, in particular
12358 -- the private entities dependent on the primary private entities.
12360 Restore_Private_Views (Act_Decl_Id);
12362 -- Remove the current unit from visibility if this is an instance
12363 -- that is not elaborated on the fly for inlining purposes.
12365 if not Inlined_Body then
12366 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12367 end if;
12369 Restore_Env;
12371 -- If we have no body, and the unit requires a body, then complain. This
12372 -- complaint is suppressed if we have detected other errors (since a
12373 -- common reason for missing the body is that it had errors).
12374 -- In CodePeer mode, a warning has been emitted already, no need for
12375 -- further messages.
12377 elsif Unit_Requires_Body (Gen_Unit)
12378 and then not Body_Optional
12379 then
12380 if CodePeer_Mode then
12381 null;
12383 elsif Serious_Errors_Detected = 0 then
12384 Error_Msg_NE
12385 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12387 -- Don't attempt to perform any cleanup actions if some other error
12388 -- was already detected, since this can cause blowups.
12390 else
12391 goto Leave;
12392 end if;
12394 -- Case of package that does not need a body
12396 else
12397 -- If the instantiation of the declaration is a library unit, rewrite
12398 -- the original package instantiation as a package declaration in the
12399 -- compilation unit node.
12401 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12402 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12403 Rewrite (Inst_Node, Act_Decl);
12405 -- Generate elaboration entity, in case spec has elaboration code.
12406 -- This cannot be done when the instance is analyzed, because it
12407 -- is not known yet whether the body exists.
12409 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12410 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12412 -- If the instantiation is not a library unit, then append the
12413 -- declaration to the list of implicitly generated entities, unless
12414 -- it is already a list member which means that it was already
12415 -- processed
12417 elsif not Is_List_Member (Act_Decl) then
12418 Mark_Rewrite_Insertion (Act_Decl);
12419 Insert_Before (Inst_Node, Act_Decl);
12420 end if;
12421 end if;
12423 <<Leave>>
12425 -- Restore the context that was in effect prior to instantiating the
12426 -- package body.
12428 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12429 Local_Suppress_Stack_Top := Saved_LSST;
12430 Scope_Suppress := Saved_SS;
12431 Style_Check := Saved_SC;
12433 Expander_Mode_Restore;
12434 Restore_Config_Switches (Saved_CS);
12435 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12436 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12437 Restore_Warnings (Saved_Warn);
12438 end Instantiate_Package_Body;
12440 ---------------------------------
12441 -- Instantiate_Subprogram_Body --
12442 ---------------------------------
12444 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12445 -- must be replaced by gotos which jump to the end of the routine in order
12446 -- to restore the Ghost and SPARK modes.
12448 procedure Instantiate_Subprogram_Body
12449 (Body_Info : Pending_Body_Info;
12450 Body_Optional : Boolean := False)
12452 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12453 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12454 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12455 Gen_Id : constant Node_Id := Name (Inst_Node);
12456 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12457 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12458 Loc : constant Source_Ptr := Sloc (Inst_Node);
12459 Pack_Id : constant Entity_Id :=
12460 Defining_Unit_Name (Parent (Act_Decl));
12462 -- The following constants capture the context prior to instantiating
12463 -- the subprogram body.
12465 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12466 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12467 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12468 Saved_ISMP : constant Boolean :=
12469 Ignore_SPARK_Mode_Pragmas_In_Instance;
12470 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12471 Local_Suppress_Stack_Top;
12472 Saved_SC : constant Boolean := Style_Check;
12473 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12474 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12475 Saved_SS : constant Suppress_Record := Scope_Suppress;
12476 Saved_Warn : constant Warnings_State := Save_Warnings;
12478 Act_Body : Node_Id;
12479 Act_Body_Id : Entity_Id;
12480 Gen_Body : Node_Id;
12481 Gen_Body_Id : Node_Id;
12482 Pack_Body : Node_Id;
12483 Par_Ent : Entity_Id := Empty;
12484 Par_Installed : Boolean := False;
12485 Par_Vis : Boolean := False;
12486 Ret_Expr : Node_Id;
12488 begin
12489 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12491 -- Subprogram body may have been created already because of an inline
12492 -- pragma, or because of multiple elaborations of the enclosing package
12493 -- when several instances of the subprogram appear in the main unit.
12495 if Present (Corresponding_Body (Act_Decl)) then
12496 return;
12497 end if;
12499 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12501 -- Re-establish the state of information on which checks are suppressed.
12502 -- This information was set in Body_Info at the point of instantiation,
12503 -- and now we restore it so that the instance is compiled using the
12504 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12506 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12507 Scope_Suppress := Body_Info.Scope_Suppress;
12509 Restore_Config_Switches (Body_Info.Config_Switches);
12510 Restore_Warnings (Body_Info.Warnings);
12512 if No (Gen_Body_Id) then
12514 -- For imported generic subprogram, no body to compile, complete
12515 -- the spec entity appropriately.
12517 if Is_Imported (Gen_Unit) then
12518 Set_Is_Imported (Act_Decl_Id);
12519 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12520 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12521 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12522 Set_Has_Completion (Act_Decl_Id);
12523 goto Leave;
12525 -- For other cases, compile the body
12527 else
12528 Load_Parent_Of_Generic
12529 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12530 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12531 end if;
12532 end if;
12534 Instantiation_Node := Inst_Node;
12536 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12537 -- the mode now to ensure that any nodes generated during instantiation
12538 -- are properly marked as Ghost.
12540 Set_Ghost_Mode (Act_Decl_Id);
12542 if Present (Gen_Body_Id) then
12543 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12545 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12547 -- Either body is not present, or context is non-expanding, as
12548 -- when compiling a subunit. Mark the instance as completed, and
12549 -- diagnose a missing body when needed.
12551 if Expander_Active
12552 and then Operating_Mode = Generate_Code
12553 then
12554 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12555 end if;
12557 Set_Has_Completion (Act_Decl_Id);
12558 goto Leave;
12559 end if;
12561 Save_Env (Gen_Unit, Act_Decl_Id);
12562 Style_Check := False;
12564 -- If the context of the instance is subject to SPARK_Mode "off", the
12565 -- annotation is missing, or the body is instantiated at a later pass
12566 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12567 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12568 -- instance.
12570 if SPARK_Mode /= On
12571 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12572 then
12573 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12574 end if;
12576 -- If the context of an instance is not subject to SPARK_Mode "off",
12577 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12578 -- the latter should be the one applicable to the instance.
12580 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12581 and then SPARK_Mode /= Off
12582 and then Present (SPARK_Pragma (Gen_Body_Id))
12583 then
12584 Set_SPARK_Mode (Gen_Body_Id);
12585 end if;
12587 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12588 Create_Instantiation_Source
12589 (Inst_Node,
12590 Gen_Body_Id,
12591 S_Adjustment);
12593 Act_Body :=
12594 Copy_Generic_Node
12595 (Original_Node (Gen_Body), Empty, Instantiating => True);
12597 -- Create proper defining name for the body, to correspond to the one
12598 -- in the spec.
12600 Act_Body_Id :=
12601 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12603 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12604 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12606 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12607 Set_Has_Completion (Act_Decl_Id);
12608 Check_Generic_Actuals (Pack_Id, False);
12610 -- Generate a reference to link the visible subprogram instance to
12611 -- the generic body, which for navigation purposes is the only
12612 -- available source for the instance.
12614 Generate_Reference
12615 (Related_Instance (Pack_Id),
12616 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12618 -- If it is a child unit, make the parent instance (which is an
12619 -- instance of the parent of the generic) visible. The parent
12620 -- instance is the prefix of the name of the generic unit.
12622 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12623 and then Nkind (Gen_Id) = N_Expanded_Name
12624 then
12625 Par_Ent := Entity (Prefix (Gen_Id));
12626 elsif Is_Child_Unit (Gen_Unit) then
12627 Par_Ent := Scope (Gen_Unit);
12628 end if;
12630 if Present (Par_Ent) then
12631 Par_Vis := Is_Immediately_Visible (Par_Ent);
12632 Install_Parent (Par_Ent, In_Body => True);
12633 Par_Installed := True;
12634 end if;
12636 -- Subprogram body is placed in the body of wrapper package,
12637 -- whose spec contains the subprogram declaration as well as
12638 -- the renaming declarations for the generic parameters.
12640 Pack_Body :=
12641 Make_Package_Body (Loc,
12642 Defining_Unit_Name => New_Copy (Pack_Id),
12643 Declarations => New_List (Act_Body));
12645 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12647 -- If the instantiation is a library unit, then build resulting
12648 -- compilation unit nodes for the instance. The declaration of
12649 -- the enclosing package is the grandparent of the subprogram
12650 -- declaration. First replace the instantiation node as the unit
12651 -- of the corresponding compilation.
12653 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12654 if Parent (Inst_Node) = Cunit (Main_Unit) then
12655 Set_Unit (Parent (Inst_Node), Inst_Node);
12656 Build_Instance_Compilation_Unit_Nodes
12657 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12658 Analyze (Inst_Node);
12659 else
12660 Set_Parent (Pack_Body, Parent (Inst_Node));
12661 Analyze (Pack_Body);
12662 end if;
12664 else
12665 Insert_Before (Inst_Node, Pack_Body);
12666 Mark_Rewrite_Insertion (Pack_Body);
12668 -- Insert the freeze node for the instance if need be
12670 if Expander_Active then
12671 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12672 end if;
12674 Analyze (Pack_Body);
12675 end if;
12677 Inherit_Context (Gen_Body, Inst_Node);
12679 Restore_Private_Views (Pack_Id, False);
12681 if Par_Installed then
12682 Remove_Parent (In_Body => True);
12684 -- Restore the previous visibility of the parent
12686 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12687 end if;
12689 Restore_Env;
12691 -- Body not found. Error was emitted already. If there were no previous
12692 -- errors, this may be an instance whose scope is a premature instance.
12693 -- In that case we must insure that the (legal) program does raise
12694 -- program error if executed. We generate a subprogram body for this
12695 -- purpose.
12697 elsif Serious_Errors_Detected = 0
12698 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12699 then
12700 if Body_Optional then
12701 goto Leave;
12703 elsif Ekind (Act_Decl_Id) = E_Procedure then
12704 Act_Body :=
12705 Make_Subprogram_Body (Loc,
12706 Specification =>
12707 Make_Procedure_Specification (Loc,
12708 Defining_Unit_Name =>
12709 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12710 Parameter_Specifications =>
12711 New_Copy_List
12712 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12714 Declarations => Empty_List,
12715 Handled_Statement_Sequence =>
12716 Make_Handled_Sequence_Of_Statements (Loc,
12717 Statements => New_List (
12718 Make_Raise_Program_Error (Loc,
12719 Reason => PE_Access_Before_Elaboration))));
12721 else
12722 Ret_Expr :=
12723 Make_Raise_Program_Error (Loc,
12724 Reason => PE_Access_Before_Elaboration);
12726 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12727 Set_Analyzed (Ret_Expr);
12729 Act_Body :=
12730 Make_Subprogram_Body (Loc,
12731 Specification =>
12732 Make_Function_Specification (Loc,
12733 Defining_Unit_Name =>
12734 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12735 Parameter_Specifications =>
12736 New_Copy_List
12737 (Parameter_Specifications (Parent (Act_Decl_Id))),
12738 Result_Definition =>
12739 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12741 Declarations => Empty_List,
12742 Handled_Statement_Sequence =>
12743 Make_Handled_Sequence_Of_Statements (Loc,
12744 Statements => New_List (
12745 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12746 end if;
12748 Pack_Body :=
12749 Make_Package_Body (Loc,
12750 Defining_Unit_Name => New_Copy (Pack_Id),
12751 Declarations => New_List (Act_Body));
12753 Insert_After (Inst_Node, Pack_Body);
12754 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12755 Analyze (Pack_Body);
12756 end if;
12758 <<Leave>>
12760 -- Restore the context that was in effect prior to instantiating the
12761 -- subprogram body.
12763 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12764 Local_Suppress_Stack_Top := Saved_LSST;
12765 Scope_Suppress := Saved_SS;
12766 Style_Check := Saved_SC;
12768 Expander_Mode_Restore;
12769 Restore_Config_Switches (Saved_CS);
12770 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12771 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12772 Restore_Warnings (Saved_Warn);
12773 end Instantiate_Subprogram_Body;
12775 ----------------------
12776 -- Instantiate_Type --
12777 ----------------------
12779 function Instantiate_Type
12780 (Formal : Node_Id;
12781 Actual : Node_Id;
12782 Analyzed_Formal : Node_Id;
12783 Actual_Decls : List_Id) return List_Id
12785 A_Gen_T : constant Entity_Id :=
12786 Defining_Identifier (Analyzed_Formal);
12787 Def : constant Node_Id := Formal_Type_Definition (Formal);
12788 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12789 Act_T : Entity_Id;
12790 Ancestor : Entity_Id := Empty;
12791 Decl_Node : Node_Id;
12792 Decl_Nodes : List_Id;
12793 Loc : Source_Ptr;
12794 Subt : Entity_Id;
12796 procedure Check_Shared_Variable_Control_Aspects;
12797 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12798 -- that may be specified for a formal type are obeyed by the actual.
12800 procedure Diagnose_Predicated_Actual;
12801 -- There are a number of constructs in which a discrete type with
12802 -- predicates is illegal, e.g. as an index in an array type declaration.
12803 -- If a generic type is used is such a construct in a generic package
12804 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12805 -- of the generic contract that the actual cannot have predicates.
12807 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12808 -- Check that base types are the same and that the subtypes match
12809 -- statically. Used in several of the validation subprograms for
12810 -- actuals in instantiations.
12812 procedure Validate_Array_Type_Instance;
12813 procedure Validate_Access_Subprogram_Instance;
12814 procedure Validate_Access_Type_Instance;
12815 procedure Validate_Derived_Type_Instance;
12816 procedure Validate_Derived_Interface_Type_Instance;
12817 procedure Validate_Discriminated_Formal_Type;
12818 procedure Validate_Interface_Type_Instance;
12819 procedure Validate_Private_Type_Instance;
12820 procedure Validate_Incomplete_Type_Instance;
12821 -- These procedures perform validation tests for the named case.
12822 -- Validate_Discriminated_Formal_Type is shared by formal private
12823 -- types and Ada 2012 formal incomplete types.
12825 --------------------------------------------
12826 -- Check_Shared_Variable_Control_Aspects --
12827 --------------------------------------------
12829 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12830 -- that may be specified for the formal are obeyed by the actual.
12831 -- If the formal is a derived type the aspect specifications must match.
12832 -- NOTE: AI12-0282 implies that matching of aspects is required between
12833 -- formal and actual in all cases, but this is too restrictive.
12834 -- In particular it violates a language design rule: a limited private
12835 -- indefinite formal can be matched by any actual. The current code
12836 -- reflects an older and more permissive version of RM C.6 (12/5).
12838 procedure Check_Shared_Variable_Control_Aspects is
12839 begin
12840 if Ada_Version >= Ada_2022 then
12841 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12842 Error_Msg_NE
12843 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12845 elsif Is_Derived_Type (A_Gen_T)
12846 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12847 then
12848 Error_Msg_NE
12849 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12850 end if;
12852 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12853 Error_Msg_NE
12854 ("actual for& must have Volatile aspect",
12855 Actual, A_Gen_T);
12857 elsif Is_Derived_Type (A_Gen_T)
12858 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12859 then
12860 Error_Msg_NE
12861 ("actual for& has different Volatile aspect",
12862 Actual, A_Gen_T);
12863 end if;
12865 -- We assume that an array type whose atomic component type
12866 -- is Atomic is equivalent to an array type with the explicit
12867 -- aspect Has_Atomic_Components. This is a reasonable inference
12868 -- from the intent of AI12-0282, and makes it legal to use an
12869 -- actual that does not have the identical aspect as the formal.
12870 -- Ditto for volatile components.
12872 declare
12873 Actual_Atomic_Comp : constant Boolean :=
12874 Has_Atomic_Components (Act_T)
12875 or else (Is_Array_Type (Act_T)
12876 and then Is_Atomic (Component_Type (Act_T)));
12877 begin
12878 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12879 Error_Msg_NE
12880 ("formal and actual for& must agree on atomic components",
12881 Actual, A_Gen_T);
12882 end if;
12883 end;
12885 declare
12886 Actual_Volatile_Comp : constant Boolean :=
12887 Has_Volatile_Components (Act_T)
12888 or else (Is_Array_Type (Act_T)
12889 and then Is_Volatile (Component_Type (Act_T)));
12890 begin
12891 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12892 then
12893 Error_Msg_NE
12894 ("actual for& must have volatile components",
12895 Actual, A_Gen_T);
12896 end if;
12897 end;
12899 -- The following two aspects do not require exact matching,
12900 -- but only one-way agreement. See RM C.6.
12902 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12903 then
12904 Error_Msg_NE
12905 ("actual for& must have Independent aspect specified",
12906 Actual, A_Gen_T);
12907 end if;
12909 if Has_Independent_Components (A_Gen_T)
12910 and then not Has_Independent_Components (Act_T)
12911 then
12912 Error_Msg_NE
12913 ("actual for& must have Independent_Components specified",
12914 Actual, A_Gen_T);
12915 end if;
12917 -- Check actual/formal compatibility with respect to the four
12918 -- volatility refinement aspects.
12920 Check_Volatility_Compatibility
12921 (Act_T, A_Gen_T,
12922 "actual type", "its corresponding formal type",
12923 Srcpos_Bearer => Actual);
12924 end if;
12925 end Check_Shared_Variable_Control_Aspects;
12927 ---------------------------------
12928 -- Diagnose_Predicated_Actual --
12929 ---------------------------------
12931 procedure Diagnose_Predicated_Actual is
12932 begin
12933 if No_Predicate_On_Actual (A_Gen_T)
12934 and then Has_Predicates (Act_T)
12935 then
12936 Error_Msg_NE
12937 ("actual for& cannot be a type with predicate",
12938 Instantiation_Node, A_Gen_T);
12940 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12941 and then Has_Predicates (Act_T)
12942 and then not Has_Static_Predicate_Aspect (Act_T)
12943 then
12944 Error_Msg_NE
12945 ("actual for& cannot be a type with a dynamic predicate",
12946 Instantiation_Node, A_Gen_T);
12947 end if;
12948 end Diagnose_Predicated_Actual;
12950 --------------------
12951 -- Subtypes_Match --
12952 --------------------
12954 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12955 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12957 begin
12958 -- Check that the base types, root types (when dealing with class
12959 -- wide types), or designated types (when dealing with anonymous
12960 -- access types) of Gen_T and Act_T are statically matching subtypes.
12962 return ((Base_Type (T) = Act_T
12963 or else Base_Type (T) = Base_Type (Act_T))
12964 and then Subtypes_Statically_Match (T, Act_T))
12966 or else (Is_Class_Wide_Type (Gen_T)
12967 and then Is_Class_Wide_Type (Act_T)
12968 and then Subtypes_Match
12969 (Get_Instance_Of (Root_Type (Gen_T)),
12970 Root_Type (Act_T)))
12972 or else (Is_Anonymous_Access_Type (Gen_T)
12973 and then Ekind (Act_T) = Ekind (Gen_T)
12974 and then Subtypes_Statically_Match
12975 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12976 end Subtypes_Match;
12978 -----------------------------------------
12979 -- Validate_Access_Subprogram_Instance --
12980 -----------------------------------------
12982 procedure Validate_Access_Subprogram_Instance is
12983 begin
12984 if not Is_Access_Type (Act_T)
12985 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12986 then
12987 Error_Msg_NE
12988 ("expect access type in instantiation of &", Actual, Gen_T);
12989 Abandon_Instantiation (Actual);
12990 end if;
12992 -- According to AI05-288, actuals for access_to_subprograms must be
12993 -- subtype conformant with the generic formal. Previous to AI05-288
12994 -- only mode conformance was required.
12996 -- This is a binding interpretation that applies to previous versions
12997 -- of the language, no need to maintain previous weaker checks.
12999 Check_Subtype_Conformant
13000 (Designated_Type (Act_T),
13001 Designated_Type (A_Gen_T),
13002 Actual,
13003 Get_Inst => True);
13005 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13006 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13007 Error_Msg_NE
13008 ("protected access type not allowed for formal &",
13009 Actual, Gen_T);
13010 end if;
13012 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13013 Error_Msg_NE
13014 ("expect protected access type for formal &",
13015 Actual, Gen_T);
13016 end if;
13018 -- If the formal has a specified convention (which in most cases
13019 -- will be StdCall) verify that the actual has the same convention.
13021 if Has_Convention_Pragma (A_Gen_T)
13022 and then Convention (A_Gen_T) /= Convention (Act_T)
13023 then
13024 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13025 Error_Msg_NE
13026 ("actual for formal & must have convention %", Actual, Gen_T);
13027 end if;
13029 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13030 Error_Msg_NE
13031 ("non null exclusion of actual and formal & do not match",
13032 Actual, Gen_T);
13033 end if;
13034 end Validate_Access_Subprogram_Instance;
13036 -----------------------------------
13037 -- Validate_Access_Type_Instance --
13038 -----------------------------------
13040 procedure Validate_Access_Type_Instance is
13041 Desig_Type : constant Entity_Id :=
13042 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13043 Desig_Act : Entity_Id;
13045 begin
13046 if not Is_Access_Type (Act_T) then
13047 Error_Msg_NE
13048 ("expect access type in instantiation of &", Actual, Gen_T);
13049 Abandon_Instantiation (Actual);
13050 end if;
13052 if Is_Access_Constant (A_Gen_T) then
13053 if not Is_Access_Constant (Act_T) then
13054 Error_Msg_N
13055 ("actual type must be access-to-constant type", Actual);
13056 Abandon_Instantiation (Actual);
13057 end if;
13058 else
13059 if Is_Access_Constant (Act_T) then
13060 Error_Msg_N
13061 ("actual type must be access-to-variable type", Actual);
13062 Abandon_Instantiation (Actual);
13064 elsif Ekind (A_Gen_T) = E_General_Access_Type
13065 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13066 then
13067 Error_Msg_N
13068 ("actual must be general access type!", Actual);
13069 Error_Msg_NE -- CODEFIX
13070 ("\add ALL to }!", Actual, Act_T);
13071 Abandon_Instantiation (Actual);
13072 end if;
13073 end if;
13075 -- The designated subtypes, that is to say the subtypes introduced
13076 -- by an access type declaration (and not by a subtype declaration)
13077 -- must match.
13079 Desig_Act := Designated_Type (Base_Type (Act_T));
13081 -- The designated type may have been introduced through a limited_
13082 -- with clause, in which case retrieve the non-limited view. This
13083 -- applies to incomplete types as well as to class-wide types.
13085 if From_Limited_With (Desig_Act) then
13086 Desig_Act := Available_View (Desig_Act);
13087 end if;
13089 if not Subtypes_Match (Desig_Type, Desig_Act) then
13090 Error_Msg_NE
13091 ("designated type of actual does not match that of formal &",
13092 Actual, Gen_T);
13094 if not Predicates_Match (Desig_Type, Desig_Act) then
13095 Error_Msg_N ("\predicates do not match", Actual);
13096 end if;
13098 Abandon_Instantiation (Actual);
13099 end if;
13101 -- Ada 2005: null-exclusion indicators of the two types must agree
13103 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13104 Error_Msg_NE
13105 ("non null exclusion of actual and formal & do not match",
13106 Actual, Gen_T);
13107 end if;
13108 end Validate_Access_Type_Instance;
13110 ----------------------------------
13111 -- Validate_Array_Type_Instance --
13112 ----------------------------------
13114 procedure Validate_Array_Type_Instance is
13115 I1 : Node_Id;
13116 I2 : Node_Id;
13117 T2 : Entity_Id;
13119 function Formal_Dimensions return Nat;
13120 -- Count number of dimensions in array type formal
13122 -----------------------
13123 -- Formal_Dimensions --
13124 -----------------------
13126 function Formal_Dimensions return Nat is
13127 Num : Nat := 0;
13128 Index : Node_Id;
13130 begin
13131 if Nkind (Def) = N_Constrained_Array_Definition then
13132 Index := First (Discrete_Subtype_Definitions (Def));
13133 else
13134 Index := First (Subtype_Marks (Def));
13135 end if;
13137 while Present (Index) loop
13138 Num := Num + 1;
13139 Next (Index);
13140 end loop;
13142 return Num;
13143 end Formal_Dimensions;
13145 -- Start of processing for Validate_Array_Type_Instance
13147 begin
13148 if not Is_Array_Type (Act_T) then
13149 Error_Msg_NE
13150 ("expect array type in instantiation of &", Actual, Gen_T);
13151 Abandon_Instantiation (Actual);
13153 elsif Nkind (Def) = N_Constrained_Array_Definition then
13154 if not (Is_Constrained (Act_T)) then
13155 Error_Msg_NE
13156 ("expect constrained array in instantiation of &",
13157 Actual, Gen_T);
13158 Abandon_Instantiation (Actual);
13159 end if;
13161 else
13162 if Is_Constrained (Act_T) then
13163 Error_Msg_NE
13164 ("expect unconstrained array in instantiation of &",
13165 Actual, Gen_T);
13166 Abandon_Instantiation (Actual);
13167 end if;
13168 end if;
13170 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13171 Error_Msg_NE
13172 ("dimensions of actual do not match formal &", Actual, Gen_T);
13173 Abandon_Instantiation (Actual);
13174 end if;
13176 I1 := First_Index (A_Gen_T);
13177 I2 := First_Index (Act_T);
13178 for J in 1 .. Formal_Dimensions loop
13180 -- If the indexes of the actual were given by a subtype_mark,
13181 -- the index was transformed into a range attribute. Retrieve
13182 -- the original type mark for checking.
13184 if Is_Entity_Name (Original_Node (I2)) then
13185 T2 := Entity (Original_Node (I2));
13186 else
13187 T2 := Etype (I2);
13188 end if;
13190 if not Subtypes_Match
13191 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13192 then
13193 Error_Msg_NE
13194 ("index types of actual do not match those of formal &",
13195 Actual, Gen_T);
13196 Abandon_Instantiation (Actual);
13197 end if;
13199 Next_Index (I1);
13200 Next_Index (I2);
13201 end loop;
13203 -- Check matching subtypes. Note that there are complex visibility
13204 -- issues when the generic is a child unit and some aspect of the
13205 -- generic type is declared in a parent unit of the generic. We do
13206 -- the test to handle this special case only after a direct check
13207 -- for static matching has failed. The case where both the component
13208 -- type and the array type are separate formals, and the component
13209 -- type is a private view may also require special checking in
13210 -- Subtypes_Match. Finally, we assume that a child instance where
13211 -- the component type comes from a formal of a parent instance is
13212 -- correct because the generic was correct. A more precise check
13213 -- seems too complex to install???
13215 if Subtypes_Match
13216 (Component_Type (A_Gen_T), Component_Type (Act_T))
13217 or else
13218 Subtypes_Match
13219 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13220 Component_Type (Act_T))
13221 or else
13222 (not Inside_A_Generic
13223 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13224 then
13225 null;
13226 else
13227 Error_Msg_NE
13228 ("component subtype of actual does not match that of formal &",
13229 Actual, Gen_T);
13230 Abandon_Instantiation (Actual);
13231 end if;
13233 if Has_Aliased_Components (A_Gen_T)
13234 and then not Has_Aliased_Components (Act_T)
13235 then
13236 Error_Msg_NE
13237 ("actual must have aliased components to match formal type &",
13238 Actual, Gen_T);
13239 end if;
13240 end Validate_Array_Type_Instance;
13242 -----------------------------------------------
13243 -- Validate_Derived_Interface_Type_Instance --
13244 -----------------------------------------------
13246 procedure Validate_Derived_Interface_Type_Instance is
13247 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13248 Elmt : Elmt_Id;
13250 begin
13251 -- First apply interface instance checks
13253 Validate_Interface_Type_Instance;
13255 -- Verify that immediate parent interface is an ancestor of
13256 -- the actual.
13258 if Present (Par)
13259 and then not Interface_Present_In_Ancestor (Act_T, Par)
13260 then
13261 Error_Msg_NE
13262 ("interface actual must include progenitor&", Actual, Par);
13263 end if;
13265 -- Now verify that the actual includes all other ancestors of
13266 -- the formal.
13268 Elmt := First_Elmt (Interfaces (A_Gen_T));
13269 while Present (Elmt) loop
13270 if not Interface_Present_In_Ancestor
13271 (Act_T, Get_Instance_Of (Node (Elmt)))
13272 then
13273 Error_Msg_NE
13274 ("interface actual must include progenitor&",
13275 Actual, Node (Elmt));
13276 end if;
13278 Next_Elmt (Elmt);
13279 end loop;
13280 end Validate_Derived_Interface_Type_Instance;
13282 ------------------------------------
13283 -- Validate_Derived_Type_Instance --
13284 ------------------------------------
13286 procedure Validate_Derived_Type_Instance is
13287 Actual_Discr : Entity_Id;
13288 Ancestor_Discr : Entity_Id;
13290 begin
13291 -- Verify that the actual includes the progenitors of the formal,
13292 -- if any. The formal may depend on previous formals and their
13293 -- instance, so we must examine instance of interfaces if present.
13294 -- The actual may be an extension of an interface, in which case
13295 -- it does not appear in the interface list, so this must be
13296 -- checked separately.
13298 if Present (Interface_List (Def)) then
13299 if not Has_Interfaces (Act_T) then
13300 Error_Msg_NE
13301 ("actual must implement all interfaces of formal&",
13302 Actual, A_Gen_T);
13304 else
13305 declare
13306 Act_Iface_List : Elist_Id;
13307 Iface : Node_Id;
13308 Iface_Ent : Entity_Id;
13310 function Instance_Exists (I : Entity_Id) return Boolean;
13311 -- If the interface entity is declared in a generic unit,
13312 -- this can only be legal if we are within an instantiation
13313 -- of a child of that generic. There is currently no
13314 -- mechanism to relate an interface declared within a
13315 -- generic to the corresponding interface in an instance,
13316 -- so we traverse the list of interfaces of the actual,
13317 -- looking for a name match.
13319 ---------------------
13320 -- Instance_Exists --
13321 ---------------------
13323 function Instance_Exists (I : Entity_Id) return Boolean is
13324 Iface_Elmt : Elmt_Id;
13326 begin
13327 Iface_Elmt := First_Elmt (Act_Iface_List);
13328 while Present (Iface_Elmt) loop
13329 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13330 and then Chars (Node (Iface_Elmt)) = Chars (I)
13331 then
13332 return True;
13333 end if;
13335 Next_Elmt (Iface_Elmt);
13336 end loop;
13338 return False;
13339 end Instance_Exists;
13341 begin
13342 Iface := First (Abstract_Interface_List (A_Gen_T));
13343 Collect_Interfaces (Act_T, Act_Iface_List);
13345 while Present (Iface) loop
13346 Iface_Ent := Get_Instance_Of (Entity (Iface));
13348 if Is_Ancestor (Iface_Ent, Act_T)
13349 or else Is_Progenitor (Iface_Ent, Act_T)
13350 then
13351 null;
13353 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13354 and then Instance_Exists (Iface_Ent)
13355 then
13356 null;
13358 else
13359 Error_Msg_Name_1 := Chars (Act_T);
13360 Error_Msg_NE
13361 ("actual% must implement interface&",
13362 Actual, Etype (Iface));
13363 end if;
13365 Next (Iface);
13366 end loop;
13367 end;
13368 end if;
13369 end if;
13371 -- If the parent type in the generic declaration is itself a previous
13372 -- formal type, then it is local to the generic and absent from the
13373 -- analyzed generic definition. In that case the ancestor is the
13374 -- instance of the formal (which must have been instantiated
13375 -- previously), unless the ancestor is itself a formal derived type.
13376 -- In this latter case (which is the subject of Corrigendum 8652/0038
13377 -- (AI-202) the ancestor of the formals is the ancestor of its
13378 -- parent. Otherwise, the analyzed generic carries the parent type.
13379 -- If the parent type is defined in a previous formal package, then
13380 -- the scope of that formal package is that of the generic type
13381 -- itself, and it has already been mapped into the corresponding type
13382 -- in the actual package.
13384 -- Common case: parent type defined outside of the generic
13386 if Is_Entity_Name (Subtype_Mark (Def))
13387 and then Present (Entity (Subtype_Mark (Def)))
13388 then
13389 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13391 -- Check whether parent is defined in a previous formal package
13393 elsif
13394 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13395 then
13396 Ancestor :=
13397 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13399 -- The type may be a local derivation, or a type extension of a
13400 -- previous formal, or of a formal of a parent package.
13402 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13403 or else
13404 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13405 then
13406 -- Check whether the parent is another derived formal type in the
13407 -- same generic unit.
13409 if Etype (A_Gen_T) /= A_Gen_T
13410 and then Is_Generic_Type (Etype (A_Gen_T))
13411 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13412 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13413 then
13414 -- Locate ancestor of parent from the subtype declaration
13415 -- created for the actual.
13417 declare
13418 Decl : Node_Id;
13420 begin
13421 Decl := First (Actual_Decls);
13422 while Present (Decl) loop
13423 if Nkind (Decl) = N_Subtype_Declaration
13424 and then Chars (Defining_Identifier (Decl)) =
13425 Chars (Etype (A_Gen_T))
13426 then
13427 Ancestor := Generic_Parent_Type (Decl);
13428 exit;
13429 else
13430 Next (Decl);
13431 end if;
13432 end loop;
13433 end;
13435 pragma Assert (Present (Ancestor));
13437 -- The ancestor itself may be a previous formal that has been
13438 -- instantiated.
13440 Ancestor := Get_Instance_Of (Ancestor);
13442 else
13443 Ancestor :=
13444 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13445 end if;
13447 -- Check whether parent is a previous formal of the current generic
13449 elsif Is_Derived_Type (A_Gen_T)
13450 and then Is_Generic_Type (Etype (A_Gen_T))
13451 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13452 then
13453 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13455 -- An unusual case: the actual is a type declared in a parent unit,
13456 -- but is not a formal type so there is no instance_of for it.
13457 -- Retrieve it by analyzing the record extension.
13459 elsif Is_Child_Unit (Scope (A_Gen_T))
13460 and then In_Open_Scopes (Scope (Act_T))
13461 and then Is_Generic_Instance (Scope (Act_T))
13462 then
13463 Analyze (Subtype_Mark (Def));
13464 Ancestor := Entity (Subtype_Mark (Def));
13466 else
13467 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13468 end if;
13470 -- If the formal derived type has pragma Preelaborable_Initialization
13471 -- then the actual type must have preelaborable initialization.
13473 if Known_To_Have_Preelab_Init (A_Gen_T)
13474 and then not Has_Preelaborable_Initialization (Act_T)
13475 then
13476 Error_Msg_NE
13477 ("actual for & must have preelaborable initialization",
13478 Actual, Gen_T);
13479 end if;
13481 -- Ada 2005 (AI-251)
13483 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13484 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13485 Error_Msg_NE
13486 ("(Ada 2005) expected type implementing & in instantiation",
13487 Actual, Ancestor);
13488 end if;
13490 -- Finally verify that the (instance of) the ancestor is an ancestor
13491 -- of the actual.
13493 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13494 Error_Msg_NE
13495 ("expect type derived from & in instantiation",
13496 Actual, First_Subtype (Ancestor));
13497 Abandon_Instantiation (Actual);
13498 end if;
13500 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13501 -- that the formal type declaration has been rewritten as a private
13502 -- extension.
13504 if Ada_Version >= Ada_2005
13505 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13506 and then Synchronized_Present (Parent (A_Gen_T))
13507 then
13508 -- The actual must be a synchronized tagged type
13510 if not Is_Tagged_Type (Act_T) then
13511 Error_Msg_N
13512 ("actual of synchronized type must be tagged", Actual);
13513 Abandon_Instantiation (Actual);
13515 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13516 and then Nkind (Type_Definition (Parent (Act_T))) =
13517 N_Derived_Type_Definition
13518 and then not Synchronized_Present
13519 (Type_Definition (Parent (Act_T)))
13520 then
13521 Error_Msg_N
13522 ("actual of synchronized type must be synchronized", Actual);
13523 Abandon_Instantiation (Actual);
13524 end if;
13525 end if;
13527 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13528 -- removes the second instance of the phrase "or allow pass by copy".
13530 -- For Ada 2022, the aspect may be specified explicitly for the
13531 -- formal regardless of whether an ancestor obeys it.
13533 if Is_Atomic (Act_T)
13534 and then not Is_Atomic (Ancestor)
13535 and then not Is_Atomic (A_Gen_T)
13536 then
13537 Error_Msg_N
13538 ("cannot have atomic actual type for non-atomic formal type",
13539 Actual);
13541 elsif Is_Volatile (Act_T)
13542 and then not Is_Volatile (Ancestor)
13543 and then not Is_Volatile (A_Gen_T)
13544 then
13545 Error_Msg_N
13546 ("cannot have volatile actual type for non-volatile formal type",
13547 Actual);
13548 end if;
13550 -- It should not be necessary to check for unknown discriminants on
13551 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13552 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13553 -- needs fixing. ???
13555 if Is_Definite_Subtype (A_Gen_T)
13556 and then not Unknown_Discriminants_Present (Formal)
13557 and then not Is_Definite_Subtype (Act_T)
13558 then
13559 Error_Msg_N ("actual subtype must be constrained", Actual);
13560 Abandon_Instantiation (Actual);
13561 end if;
13563 if not Unknown_Discriminants_Present (Formal) then
13564 if Is_Constrained (Ancestor) then
13565 if not Is_Constrained (Act_T) then
13566 Error_Msg_N ("actual subtype must be constrained", Actual);
13567 Abandon_Instantiation (Actual);
13568 end if;
13570 -- Ancestor is unconstrained, Check if generic formal and actual
13571 -- agree on constrainedness. The check only applies to array types
13572 -- and discriminated types.
13574 elsif Is_Constrained (Act_T) then
13575 if Ekind (Ancestor) = E_Access_Type
13576 or else (not Is_Constrained (A_Gen_T)
13577 and then Is_Composite_Type (A_Gen_T))
13578 then
13579 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13580 Abandon_Instantiation (Actual);
13581 end if;
13583 -- A class-wide type is only allowed if the formal has unknown
13584 -- discriminants.
13586 elsif Is_Class_Wide_Type (Act_T)
13587 and then not Has_Unknown_Discriminants (Ancestor)
13588 then
13589 Error_Msg_NE
13590 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13591 Abandon_Instantiation (Actual);
13593 -- Otherwise, the formal and actual must have the same number
13594 -- of discriminants and each discriminant of the actual must
13595 -- correspond to a discriminant of the formal.
13597 elsif Has_Discriminants (Act_T)
13598 and then not Has_Unknown_Discriminants (Act_T)
13599 and then Has_Discriminants (Ancestor)
13600 then
13601 Actual_Discr := First_Discriminant (Act_T);
13602 Ancestor_Discr := First_Discriminant (Ancestor);
13603 while Present (Actual_Discr)
13604 and then Present (Ancestor_Discr)
13605 loop
13606 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13607 No (Corresponding_Discriminant (Actual_Discr))
13608 then
13609 Error_Msg_NE
13610 ("discriminant & does not correspond "
13611 & "to ancestor discriminant", Actual, Actual_Discr);
13612 Abandon_Instantiation (Actual);
13613 end if;
13615 Next_Discriminant (Actual_Discr);
13616 Next_Discriminant (Ancestor_Discr);
13617 end loop;
13619 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13620 Error_Msg_NE
13621 ("actual for & must have same number of discriminants",
13622 Actual, Gen_T);
13623 Abandon_Instantiation (Actual);
13624 end if;
13626 -- This case should be caught by the earlier check for
13627 -- constrainedness, but the check here is added for completeness.
13629 elsif Has_Discriminants (Act_T)
13630 and then not Has_Unknown_Discriminants (Act_T)
13631 then
13632 Error_Msg_NE
13633 ("actual for & must not have discriminants", Actual, Gen_T);
13634 Abandon_Instantiation (Actual);
13636 elsif Has_Discriminants (Ancestor) then
13637 Error_Msg_NE
13638 ("actual for & must have known discriminants", Actual, Gen_T);
13639 Abandon_Instantiation (Actual);
13640 end if;
13642 if not Subtypes_Statically_Compatible
13643 (Act_T, Ancestor, Formal_Derived_Matching => True)
13644 then
13645 Error_Msg_NE
13646 ("actual for & must be statically compatible with ancestor",
13647 Actual, Gen_T);
13649 if not Predicates_Compatible (Act_T, Ancestor) then
13650 Error_Msg_N
13651 ("\predicate on actual is not compatible with ancestor",
13652 Actual);
13653 end if;
13655 Abandon_Instantiation (Actual);
13656 end if;
13657 end if;
13659 -- If the formal and actual types are abstract, check that there
13660 -- are no abstract primitives of the actual type that correspond to
13661 -- nonabstract primitives of the formal type (second sentence of
13662 -- RM95 3.9.3(9)).
13664 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13665 Check_Abstract_Primitives : declare
13666 Gen_Prims : constant Elist_Id :=
13667 Primitive_Operations (A_Gen_T);
13668 Gen_Elmt : Elmt_Id;
13669 Gen_Subp : Entity_Id;
13670 Anc_Subp : Entity_Id;
13671 Anc_Formal : Entity_Id;
13672 Anc_F_Type : Entity_Id;
13674 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13675 Act_Elmt : Elmt_Id;
13676 Act_Subp : Entity_Id;
13677 Act_Formal : Entity_Id;
13678 Act_F_Type : Entity_Id;
13680 Subprograms_Correspond : Boolean;
13682 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13683 -- Returns true if T2 is derived directly or indirectly from
13684 -- T1, including derivations from interfaces. T1 and T2 are
13685 -- required to be specific tagged base types.
13687 ------------------------
13688 -- Is_Tagged_Ancestor --
13689 ------------------------
13691 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13693 Intfc_Elmt : Elmt_Id;
13695 begin
13696 -- The predicate is satisfied if the types are the same
13698 if T1 = T2 then
13699 return True;
13701 -- If we've reached the top of the derivation chain then
13702 -- we know that T1 is not an ancestor of T2.
13704 elsif Etype (T2) = T2 then
13705 return False;
13707 -- Proceed to check T2's immediate parent
13709 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13710 return True;
13712 -- Finally, check to see if T1 is an ancestor of any of T2's
13713 -- progenitors.
13715 else
13716 Intfc_Elmt := First_Elmt (Interfaces (T2));
13717 while Present (Intfc_Elmt) loop
13718 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13719 return True;
13720 end if;
13722 Next_Elmt (Intfc_Elmt);
13723 end loop;
13724 end if;
13726 return False;
13727 end Is_Tagged_Ancestor;
13729 -- Start of processing for Check_Abstract_Primitives
13731 begin
13732 -- Loop over all of the formal derived type's primitives
13734 Gen_Elmt := First_Elmt (Gen_Prims);
13735 while Present (Gen_Elmt) loop
13736 Gen_Subp := Node (Gen_Elmt);
13738 -- If the primitive of the formal is not abstract, then
13739 -- determine whether there is a corresponding primitive of
13740 -- the actual type that's abstract.
13742 if not Is_Abstract_Subprogram (Gen_Subp) then
13743 Act_Elmt := First_Elmt (Act_Prims);
13744 while Present (Act_Elmt) loop
13745 Act_Subp := Node (Act_Elmt);
13747 -- If we find an abstract primitive of the actual,
13748 -- then we need to test whether it corresponds to the
13749 -- subprogram from which the generic formal primitive
13750 -- is inherited.
13752 if Is_Abstract_Subprogram (Act_Subp) then
13753 Anc_Subp := Alias (Gen_Subp);
13755 -- Test whether we have a corresponding primitive
13756 -- by comparing names, kinds, formal types, and
13757 -- result types.
13759 if Chars (Anc_Subp) = Chars (Act_Subp)
13760 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13761 then
13762 Anc_Formal := First_Formal (Anc_Subp);
13763 Act_Formal := First_Formal (Act_Subp);
13764 while Present (Anc_Formal)
13765 and then Present (Act_Formal)
13766 loop
13767 Anc_F_Type := Etype (Anc_Formal);
13768 Act_F_Type := Etype (Act_Formal);
13770 if Ekind (Anc_F_Type) =
13771 E_Anonymous_Access_Type
13772 then
13773 Anc_F_Type := Designated_Type (Anc_F_Type);
13775 if Ekind (Act_F_Type) =
13776 E_Anonymous_Access_Type
13777 then
13778 Act_F_Type :=
13779 Designated_Type (Act_F_Type);
13780 else
13781 exit;
13782 end if;
13784 elsif
13785 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13786 then
13787 exit;
13788 end if;
13790 Anc_F_Type := Base_Type (Anc_F_Type);
13791 Act_F_Type := Base_Type (Act_F_Type);
13793 -- If the formal is controlling, then the
13794 -- the type of the actual primitive's formal
13795 -- must be derived directly or indirectly
13796 -- from the type of the ancestor primitive's
13797 -- formal.
13799 if Is_Controlling_Formal (Anc_Formal) then
13800 if not Is_Tagged_Ancestor
13801 (Anc_F_Type, Act_F_Type)
13802 then
13803 exit;
13804 end if;
13806 -- Otherwise the types of the formals must
13807 -- be the same.
13809 elsif Anc_F_Type /= Act_F_Type then
13810 exit;
13811 end if;
13813 Next_Formal (Anc_Formal);
13814 Next_Formal (Act_Formal);
13815 end loop;
13817 -- If we traversed through all of the formals
13818 -- then so far the subprograms correspond, so
13819 -- now check that any result types correspond.
13821 if No (Anc_Formal) and then No (Act_Formal) then
13822 Subprograms_Correspond := True;
13824 if Ekind (Act_Subp) = E_Function then
13825 Anc_F_Type := Etype (Anc_Subp);
13826 Act_F_Type := Etype (Act_Subp);
13828 if Ekind (Anc_F_Type) =
13829 E_Anonymous_Access_Type
13830 then
13831 Anc_F_Type :=
13832 Designated_Type (Anc_F_Type);
13834 if Ekind (Act_F_Type) =
13835 E_Anonymous_Access_Type
13836 then
13837 Act_F_Type :=
13838 Designated_Type (Act_F_Type);
13839 else
13840 Subprograms_Correspond := False;
13841 end if;
13843 elsif
13844 Ekind (Act_F_Type)
13845 = E_Anonymous_Access_Type
13846 then
13847 Subprograms_Correspond := False;
13848 end if;
13850 Anc_F_Type := Base_Type (Anc_F_Type);
13851 Act_F_Type := Base_Type (Act_F_Type);
13853 -- Now either the result types must be
13854 -- the same or, if the result type is
13855 -- controlling, the result type of the
13856 -- actual primitive must descend from the
13857 -- result type of the ancestor primitive.
13859 if Subprograms_Correspond
13860 and then Anc_F_Type /= Act_F_Type
13861 and then
13862 Has_Controlling_Result (Anc_Subp)
13863 and then not Is_Tagged_Ancestor
13864 (Anc_F_Type, Act_F_Type)
13865 then
13866 Subprograms_Correspond := False;
13867 end if;
13868 end if;
13870 -- Found a matching subprogram belonging to
13871 -- formal ancestor type, so actual subprogram
13872 -- corresponds and this violates 3.9.3(9).
13874 if Subprograms_Correspond then
13875 Error_Msg_NE
13876 ("abstract subprogram & overrides "
13877 & "nonabstract subprogram of ancestor",
13878 Actual, Act_Subp);
13879 end if;
13880 end if;
13881 end if;
13882 end if;
13884 Next_Elmt (Act_Elmt);
13885 end loop;
13886 end if;
13888 Next_Elmt (Gen_Elmt);
13889 end loop;
13890 end Check_Abstract_Primitives;
13891 end if;
13893 -- Verify that limitedness matches. If parent is a limited
13894 -- interface then the generic formal is not unless declared
13895 -- explicitly so. If not declared limited, the actual cannot be
13896 -- limited (see AI05-0087).
13898 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13899 if not In_Instance then
13900 Error_Msg_NE
13901 ("actual for non-limited & cannot be a limited type",
13902 Actual, Gen_T);
13903 Explain_Limited_Type (Act_T, Actual);
13904 Abandon_Instantiation (Actual);
13905 end if;
13906 end if;
13908 -- Check for AI12-0036
13910 declare
13911 Formal_Is_Private_Extension : constant Boolean :=
13912 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13914 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13916 begin
13917 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13918 if not In_Instance then
13919 if Actual_Is_Tagged then
13920 Error_Msg_NE
13921 ("actual for & cannot be a tagged type", Actual, Gen_T);
13922 else
13923 Error_Msg_NE
13924 ("actual for & must be a tagged type", Actual, Gen_T);
13925 end if;
13927 Abandon_Instantiation (Actual);
13928 end if;
13929 end if;
13930 end;
13931 end Validate_Derived_Type_Instance;
13933 ----------------------------------------
13934 -- Validate_Discriminated_Formal_Type --
13935 ----------------------------------------
13937 procedure Validate_Discriminated_Formal_Type is
13938 Formal_Discr : Entity_Id;
13939 Actual_Discr : Entity_Id;
13940 Formal_Subt : Entity_Id;
13942 begin
13943 if Has_Discriminants (A_Gen_T) then
13944 if not Has_Discriminants (Act_T) then
13945 Error_Msg_NE
13946 ("actual for & must have discriminants", Actual, Gen_T);
13947 Abandon_Instantiation (Actual);
13949 elsif Is_Constrained (Act_T) then
13950 Error_Msg_NE
13951 ("actual for & must be unconstrained", Actual, Gen_T);
13952 Abandon_Instantiation (Actual);
13954 else
13955 Formal_Discr := First_Discriminant (A_Gen_T);
13956 Actual_Discr := First_Discriminant (Act_T);
13957 while Formal_Discr /= Empty loop
13958 if Actual_Discr = Empty then
13959 Error_Msg_N
13960 ("discriminants on actual do not match formal",
13961 Actual);
13962 Abandon_Instantiation (Actual);
13963 end if;
13965 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13967 -- Access discriminants match if designated types do
13969 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13970 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13971 E_Anonymous_Access_Type
13972 and then
13973 Get_Instance_Of
13974 (Designated_Type (Base_Type (Formal_Subt))) =
13975 Designated_Type (Base_Type (Etype (Actual_Discr)))
13976 then
13977 null;
13979 elsif Base_Type (Formal_Subt) /=
13980 Base_Type (Etype (Actual_Discr))
13981 then
13982 Error_Msg_N
13983 ("types of actual discriminants must match formal",
13984 Actual);
13985 Abandon_Instantiation (Actual);
13987 elsif not Subtypes_Statically_Match
13988 (Formal_Subt, Etype (Actual_Discr))
13989 and then Ada_Version >= Ada_95
13990 then
13991 Error_Msg_N
13992 ("subtypes of actual discriminants must match formal",
13993 Actual);
13994 Abandon_Instantiation (Actual);
13995 end if;
13997 Next_Discriminant (Formal_Discr);
13998 Next_Discriminant (Actual_Discr);
13999 end loop;
14001 if Actual_Discr /= Empty then
14002 Error_Msg_NE
14003 ("discriminants on actual do not match formal",
14004 Actual, Gen_T);
14005 Abandon_Instantiation (Actual);
14006 end if;
14007 end if;
14008 end if;
14009 end Validate_Discriminated_Formal_Type;
14011 ---------------------------------------
14012 -- Validate_Incomplete_Type_Instance --
14013 ---------------------------------------
14015 procedure Validate_Incomplete_Type_Instance is
14016 begin
14017 if not Is_Tagged_Type (Act_T)
14018 and then Is_Tagged_Type (A_Gen_T)
14019 then
14020 Error_Msg_NE
14021 ("actual for & must be a tagged type", Actual, Gen_T);
14022 end if;
14024 Validate_Discriminated_Formal_Type;
14025 end Validate_Incomplete_Type_Instance;
14027 --------------------------------------
14028 -- Validate_Interface_Type_Instance --
14029 --------------------------------------
14031 procedure Validate_Interface_Type_Instance is
14032 begin
14033 if not Is_Interface (Act_T) then
14034 Error_Msg_NE
14035 ("actual for formal interface type must be an interface",
14036 Actual, Gen_T);
14038 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14039 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14040 or else Is_Protected_Interface (A_Gen_T) /=
14041 Is_Protected_Interface (Act_T)
14042 or else Is_Synchronized_Interface (A_Gen_T) /=
14043 Is_Synchronized_Interface (Act_T)
14044 then
14045 Error_Msg_NE
14046 ("actual for interface& does not match (RM 12.5.5(4))",
14047 Actual, Gen_T);
14048 end if;
14049 end Validate_Interface_Type_Instance;
14051 ------------------------------------
14052 -- Validate_Private_Type_Instance --
14053 ------------------------------------
14055 procedure Validate_Private_Type_Instance is
14056 begin
14057 if Is_Limited_Type (Act_T)
14058 and then not Is_Limited_Type (A_Gen_T)
14059 then
14060 if In_Instance then
14061 null;
14062 else
14063 Error_Msg_NE
14064 ("actual for non-limited & cannot be a limited type", Actual,
14065 Gen_T);
14066 Explain_Limited_Type (Act_T, Actual);
14067 Abandon_Instantiation (Actual);
14068 end if;
14070 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14071 and then not Has_Preelaborable_Initialization (Act_T)
14072 then
14073 Error_Msg_NE
14074 ("actual for & must have preelaborable initialization", Actual,
14075 Gen_T);
14077 elsif not Is_Definite_Subtype (Act_T)
14078 and then Is_Definite_Subtype (A_Gen_T)
14079 and then Ada_Version >= Ada_95
14080 then
14081 Error_Msg_NE
14082 ("actual for & must be a definite subtype", Actual, Gen_T);
14084 elsif not Is_Tagged_Type (Act_T)
14085 and then Is_Tagged_Type (A_Gen_T)
14086 then
14087 Error_Msg_NE
14088 ("actual for & must be a tagged type", Actual, Gen_T);
14089 end if;
14091 Validate_Discriminated_Formal_Type;
14092 Ancestor := Gen_T;
14093 end Validate_Private_Type_Instance;
14095 -- Start of processing for Instantiate_Type
14097 begin
14098 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14099 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14100 return New_List (Error);
14102 elsif not Is_Entity_Name (Actual)
14103 or else not Is_Type (Entity (Actual))
14104 then
14105 Error_Msg_NE
14106 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14107 Abandon_Instantiation (Actual);
14109 else
14110 Act_T := Entity (Actual);
14112 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14113 -- as a generic actual parameter if the corresponding formal type
14114 -- does not have a known_discriminant_part, or is a formal derived
14115 -- type that is an Unchecked_Union type.
14117 if Is_Unchecked_Union (Base_Type (Act_T)) then
14118 if not Has_Discriminants (A_Gen_T)
14119 or else (Is_Derived_Type (A_Gen_T)
14120 and then Is_Unchecked_Union (A_Gen_T))
14121 then
14122 null;
14123 else
14124 Error_Msg_N ("unchecked union cannot be the actual for a "
14125 & "discriminated formal type", Act_T);
14127 end if;
14128 end if;
14130 -- Deal with fixed/floating restrictions
14132 if Is_Floating_Point_Type (Act_T) then
14133 Check_Restriction (No_Floating_Point, Actual);
14134 elsif Is_Fixed_Point_Type (Act_T) then
14135 Check_Restriction (No_Fixed_Point, Actual);
14136 end if;
14138 -- Deal with error of using incomplete type as generic actual.
14139 -- This includes limited views of a type, even if the non-limited
14140 -- view may be available.
14142 if Ekind (Act_T) = E_Incomplete_Type
14143 or else (Is_Class_Wide_Type (Act_T)
14144 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14145 then
14146 -- If the formal is an incomplete type, the actual can be
14147 -- incomplete as well, but if an actual incomplete type has
14148 -- a full view, then we'll retrieve that.
14150 if Ekind (A_Gen_T) = E_Incomplete_Type
14151 and then No (Full_View (Act_T))
14152 then
14153 null;
14155 elsif Is_Class_Wide_Type (Act_T)
14156 or else No (Full_View (Act_T))
14157 then
14158 Error_Msg_N ("premature use of incomplete type", Actual);
14159 Abandon_Instantiation (Actual);
14161 else
14162 Act_T := Full_View (Act_T);
14163 Set_Entity (Actual, Act_T);
14165 if Has_Private_Component (Act_T) then
14166 Error_Msg_N
14167 ("premature use of type with private component", Actual);
14168 end if;
14169 end if;
14171 -- Deal with error of premature use of private type as generic actual
14173 elsif Is_Private_Type (Act_T)
14174 and then Is_Private_Type (Base_Type (Act_T))
14175 and then not Is_Generic_Type (Act_T)
14176 and then not Is_Derived_Type (Act_T)
14177 and then No (Full_View (Root_Type (Act_T)))
14178 then
14179 -- If the formal is an incomplete type, the actual can be
14180 -- private or incomplete as well.
14182 if Ekind (A_Gen_T) = E_Incomplete_Type then
14183 null;
14184 else
14185 Error_Msg_N ("premature use of private type", Actual);
14186 end if;
14188 elsif Has_Private_Component (Act_T) then
14189 Error_Msg_N
14190 ("premature use of type with private component", Actual);
14191 end if;
14193 Set_Instance_Of (A_Gen_T, Act_T);
14195 -- If the type is generic, the class-wide type may also be used
14197 if Is_Tagged_Type (A_Gen_T)
14198 and then Is_Tagged_Type (Act_T)
14199 and then not Is_Class_Wide_Type (A_Gen_T)
14200 then
14201 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14202 Class_Wide_Type (Act_T));
14203 end if;
14205 if not Is_Abstract_Type (A_Gen_T)
14206 and then Is_Abstract_Type (Act_T)
14207 then
14208 Error_Msg_N
14209 ("actual of non-abstract formal cannot be abstract", Actual);
14210 end if;
14212 -- A generic scalar type is a first subtype for which we generate
14213 -- an anonymous base type. Indicate that the instance of this base
14214 -- is the base type of the actual.
14216 if Is_Scalar_Type (A_Gen_T) then
14217 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14218 end if;
14219 end if;
14221 Check_Shared_Variable_Control_Aspects;
14223 if Error_Posted (Act_T) then
14224 null;
14225 else
14226 case Nkind (Def) is
14227 when N_Formal_Private_Type_Definition =>
14228 Validate_Private_Type_Instance;
14230 when N_Formal_Incomplete_Type_Definition =>
14231 Validate_Incomplete_Type_Instance;
14233 when N_Formal_Derived_Type_Definition =>
14234 Validate_Derived_Type_Instance;
14236 when N_Formal_Discrete_Type_Definition =>
14237 if not Is_Discrete_Type (Act_T) then
14238 Error_Msg_NE
14239 ("expect discrete type in instantiation of&",
14240 Actual, Gen_T);
14241 Abandon_Instantiation (Actual);
14242 end if;
14244 Diagnose_Predicated_Actual;
14246 when N_Formal_Signed_Integer_Type_Definition =>
14247 if not Is_Signed_Integer_Type (Act_T) then
14248 Error_Msg_NE
14249 ("expect signed integer type in instantiation of&",
14250 Actual, Gen_T);
14251 Abandon_Instantiation (Actual);
14252 end if;
14254 Diagnose_Predicated_Actual;
14256 when N_Formal_Modular_Type_Definition =>
14257 if not Is_Modular_Integer_Type (Act_T) then
14258 Error_Msg_NE
14259 ("expect modular type in instantiation of &",
14260 Actual, Gen_T);
14261 Abandon_Instantiation (Actual);
14262 end if;
14264 Diagnose_Predicated_Actual;
14266 when N_Formal_Floating_Point_Definition =>
14267 if not Is_Floating_Point_Type (Act_T) then
14268 Error_Msg_NE
14269 ("expect float type in instantiation of &", Actual, Gen_T);
14270 Abandon_Instantiation (Actual);
14271 end if;
14273 when N_Formal_Ordinary_Fixed_Point_Definition =>
14274 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14275 Error_Msg_NE
14276 ("expect ordinary fixed point type in instantiation of &",
14277 Actual, Gen_T);
14278 Abandon_Instantiation (Actual);
14279 end if;
14281 when N_Formal_Decimal_Fixed_Point_Definition =>
14282 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14283 Error_Msg_NE
14284 ("expect decimal type in instantiation of &",
14285 Actual, Gen_T);
14286 Abandon_Instantiation (Actual);
14287 end if;
14289 when N_Array_Type_Definition =>
14290 Validate_Array_Type_Instance;
14292 when N_Access_To_Object_Definition =>
14293 Validate_Access_Type_Instance;
14295 when N_Access_Function_Definition
14296 | N_Access_Procedure_Definition
14298 Validate_Access_Subprogram_Instance;
14300 when N_Record_Definition =>
14301 Validate_Interface_Type_Instance;
14303 when N_Derived_Type_Definition =>
14304 Validate_Derived_Interface_Type_Instance;
14306 when others =>
14307 raise Program_Error;
14308 end case;
14309 end if;
14311 Subt := New_Copy (Gen_T);
14313 -- Use adjusted sloc of subtype name as the location for other nodes in
14314 -- the subtype declaration.
14316 Loc := Sloc (Subt);
14318 Decl_Node :=
14319 Make_Subtype_Declaration (Loc,
14320 Defining_Identifier => Subt,
14321 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14323 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14325 -- Record whether the actual is private at this point, so that
14326 -- Check_Generic_Actuals can restore its proper view before the
14327 -- semantic analysis of the instance.
14329 if Is_Private_Type (Act_T) then
14330 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14331 end if;
14333 -- In Ada 2012 the actual may be a limited view. Indicate that
14334 -- the local subtype must be treated as such.
14336 if From_Limited_With (Act_T) then
14337 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14338 Set_From_Limited_With (Subt);
14339 end if;
14341 Decl_Nodes := New_List (Decl_Node);
14343 -- Flag actual derived types so their elaboration produces the
14344 -- appropriate renamings for the primitive operations of the ancestor.
14345 -- Flag actual for formal private types as well, to determine whether
14346 -- operations in the private part may override inherited operations.
14347 -- If the formal has an interface list, the ancestor is not the
14348 -- parent, but the analyzed formal that includes the interface
14349 -- operations of all its progenitors.
14351 -- Same treatment for formal private types, so we can check whether the
14352 -- type is tagged limited when validating derivations in the private
14353 -- part. (See AI05-096).
14355 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14356 if Present (Interface_List (Def)) then
14357 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14358 else
14359 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14360 end if;
14362 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14363 | N_Formal_Incomplete_Type_Definition
14364 then
14365 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14366 end if;
14368 -- If the actual is a synchronized type that implements an interface,
14369 -- the primitive operations are attached to the corresponding record,
14370 -- and we have to treat it as an additional generic actual, so that its
14371 -- primitive operations become visible in the instance. The task or
14372 -- protected type itself does not carry primitive operations.
14374 if Is_Concurrent_Type (Act_T)
14375 and then Is_Tagged_Type (Act_T)
14376 and then Present (Corresponding_Record_Type (Act_T))
14377 and then Present (Ancestor)
14378 and then Is_Interface (Ancestor)
14379 then
14380 declare
14381 Corr_Rec : constant Entity_Id :=
14382 Corresponding_Record_Type (Act_T);
14383 New_Corr : Entity_Id;
14384 Corr_Decl : Node_Id;
14386 begin
14387 New_Corr := Make_Temporary (Loc, 'S');
14388 Corr_Decl :=
14389 Make_Subtype_Declaration (Loc,
14390 Defining_Identifier => New_Corr,
14391 Subtype_Indication =>
14392 New_Occurrence_Of (Corr_Rec, Loc));
14393 Append_To (Decl_Nodes, Corr_Decl);
14395 if Ekind (Act_T) = E_Task_Type then
14396 Mutate_Ekind (Subt, E_Task_Subtype);
14397 else
14398 Mutate_Ekind (Subt, E_Protected_Subtype);
14399 end if;
14401 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14402 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14403 Set_Generic_Parent_Type (Decl_Node, Empty);
14404 end;
14405 end if;
14407 -- For a floating-point type, capture dimension info if any, because
14408 -- the generated subtype declaration does not come from source and
14409 -- will not process dimensions.
14411 if Is_Floating_Point_Type (Act_T) then
14412 Copy_Dimensions (Act_T, Subt);
14413 end if;
14415 return Decl_Nodes;
14416 end Instantiate_Type;
14418 -----------------------------
14419 -- Is_Abbreviated_Instance --
14420 -----------------------------
14422 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14423 begin
14424 return Ekind (E) = E_Package
14425 and then Present (Hidden_In_Formal_Instance (E));
14426 end Is_Abbreviated_Instance;
14428 ---------------------
14429 -- Is_In_Main_Unit --
14430 ---------------------
14432 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14433 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14434 Current_Unit : Node_Id;
14436 begin
14437 if Unum = Main_Unit then
14438 return True;
14440 -- If the current unit is a subunit then it is either the main unit or
14441 -- is being compiled as part of the main unit.
14443 elsif Nkind (N) = N_Compilation_Unit then
14444 return Nkind (Unit (N)) = N_Subunit;
14445 end if;
14447 Current_Unit := Parent (N);
14448 while Present (Current_Unit)
14449 and then Nkind (Current_Unit) /= N_Compilation_Unit
14450 loop
14451 Current_Unit := Parent (Current_Unit);
14452 end loop;
14454 -- The instantiation node is in the main unit, or else the current node
14455 -- (perhaps as the result of nested instantiations) is in the main unit,
14456 -- or in the declaration of the main unit, which in this last case must
14457 -- be a body.
14459 return
14460 Current_Unit = Cunit (Main_Unit)
14461 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14462 or else (Present (Current_Unit)
14463 and then Present (Library_Unit (Current_Unit))
14464 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14465 end Is_In_Main_Unit;
14467 ----------------------------
14468 -- Load_Parent_Of_Generic --
14469 ----------------------------
14471 procedure Load_Parent_Of_Generic
14472 (N : Node_Id;
14473 Spec : Node_Id;
14474 Body_Optional : Boolean := False)
14476 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14477 Saved_Style_Check : constant Boolean := Style_Check;
14478 Saved_Warn : constant Warnings_State := Save_Warnings;
14479 True_Parent : Node_Id;
14480 Inst_Node : Node_Id;
14481 OK : Boolean;
14482 Previous_Instances : constant Elist_Id := New_Elmt_List;
14484 procedure Collect_Previous_Instances (Decls : List_Id);
14485 -- Collect all instantiations in the given list of declarations, that
14486 -- precede the generic that we need to load. If the bodies of these
14487 -- instantiations are available, we must analyze them, to ensure that
14488 -- the public symbols generated are the same when the unit is compiled
14489 -- to generate code, and when it is compiled in the context of a unit
14490 -- that needs a particular nested instance. This process is applied to
14491 -- both package and subprogram instances.
14493 --------------------------------
14494 -- Collect_Previous_Instances --
14495 --------------------------------
14497 procedure Collect_Previous_Instances (Decls : List_Id) is
14498 Decl : Node_Id;
14500 begin
14501 Decl := First (Decls);
14502 while Present (Decl) loop
14503 if Sloc (Decl) >= Sloc (Inst_Node) then
14504 return;
14506 -- If Decl is an instantiation, then record it as requiring
14507 -- instantiation of the corresponding body, except if it is an
14508 -- abbreviated instantiation generated internally for conformance
14509 -- checking purposes only for the case of a formal package
14510 -- declared without a box (see Instantiate_Formal_Package). Such
14511 -- an instantiation does not generate any code (the actual code
14512 -- comes from actual) and thus does not need to be analyzed here.
14513 -- If the instantiation appears with a generic package body it is
14514 -- not analyzed here either.
14516 elsif Nkind (Decl) = N_Package_Instantiation
14517 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14518 then
14519 Append_Elmt (Decl, Previous_Instances);
14521 -- For a subprogram instantiation, omit instantiations intrinsic
14522 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14524 elsif Nkind (Decl) in N_Function_Instantiation
14525 | N_Procedure_Instantiation
14526 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14527 then
14528 Append_Elmt (Decl, Previous_Instances);
14530 elsif Nkind (Decl) = N_Package_Declaration then
14531 Collect_Previous_Instances
14532 (Visible_Declarations (Specification (Decl)));
14533 Collect_Previous_Instances
14534 (Private_Declarations (Specification (Decl)));
14536 -- Previous non-generic bodies may contain instances as well
14538 elsif Nkind (Decl) = N_Package_Body
14539 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14540 then
14541 Collect_Previous_Instances (Declarations (Decl));
14543 elsif Nkind (Decl) = N_Subprogram_Body
14544 and then not Acts_As_Spec (Decl)
14545 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14546 then
14547 Collect_Previous_Instances (Declarations (Decl));
14548 end if;
14550 Next (Decl);
14551 end loop;
14552 end Collect_Previous_Instances;
14554 -- Start of processing for Load_Parent_Of_Generic
14556 begin
14557 if not In_Same_Source_Unit (N, Spec)
14558 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14559 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14560 and then not Is_In_Main_Unit (Spec))
14561 then
14562 -- Find body of parent of spec, and analyze it. A special case arises
14563 -- when the parent is an instantiation, that is to say when we are
14564 -- currently instantiating a nested generic. In that case, there is
14565 -- no separate file for the body of the enclosing instance. Instead,
14566 -- the enclosing body must be instantiated as if it were a pending
14567 -- instantiation, in order to produce the body for the nested generic
14568 -- we require now. Note that in that case the generic may be defined
14569 -- in a package body, the instance defined in the same package body,
14570 -- and the original enclosing body may not be in the main unit.
14572 Inst_Node := Empty;
14574 True_Parent := Parent (Spec);
14575 while Present (True_Parent)
14576 and then Nkind (True_Parent) /= N_Compilation_Unit
14577 loop
14578 if Nkind (True_Parent) = N_Package_Declaration
14579 and then
14580 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14581 then
14582 -- Parent is a compilation unit that is an instantiation, and
14583 -- instantiation node has been replaced with package decl.
14585 Inst_Node := Original_Node (True_Parent);
14586 exit;
14588 elsif Nkind (True_Parent) = N_Package_Declaration
14589 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14590 and then
14591 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14592 then
14593 -- Parent is a compilation unit that is an instantiation, but
14594 -- instantiation node has not been replaced with package decl.
14596 Inst_Node := Unit (Parent (True_Parent));
14597 exit;
14599 elsif Nkind (True_Parent) = N_Package_Declaration
14600 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14601 and then Present (Generic_Parent (Specification (True_Parent)))
14602 then
14603 -- Parent is an instantiation within another specification.
14604 -- Declaration for instance has been inserted before original
14605 -- instantiation node. A direct link would be preferable?
14607 Inst_Node := Next (True_Parent);
14608 while Present (Inst_Node)
14609 and then Nkind (Inst_Node) /= N_Package_Instantiation
14610 loop
14611 Next (Inst_Node);
14612 end loop;
14614 -- If the instance appears within a generic, and the generic
14615 -- unit is defined within a formal package of the enclosing
14616 -- generic, there is no generic body available, and none
14617 -- needed. A more precise test should be used ???
14619 if No (Inst_Node) then
14620 return;
14621 end if;
14623 exit;
14625 -- If an ancestor of the generic comes from a formal package
14626 -- there is no source for the ancestor body. This is detected
14627 -- by examining the scope of the ancestor and its declaration.
14628 -- The body, if any is needed, will be available when the
14629 -- current unit (containing a formal package) is instantiated.
14631 elsif Nkind (True_Parent) = N_Package_Specification
14632 and then Present (Generic_Parent (True_Parent))
14633 and then Nkind
14634 (Original_Node (Unit_Declaration_Node
14635 (Scope (Generic_Parent (True_Parent)))))
14636 = N_Formal_Package_Declaration
14637 then
14638 return;
14640 else
14641 True_Parent := Parent (True_Parent);
14642 end if;
14643 end loop;
14645 -- Case where we are currently instantiating a nested generic
14647 if Present (Inst_Node) then
14648 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14650 -- Instantiation node and declaration of instantiated package
14651 -- were exchanged when only the declaration was needed.
14652 -- Restore instantiation node before proceeding with body.
14654 Set_Unit (Parent (True_Parent), Inst_Node);
14655 end if;
14657 -- Now complete instantiation of enclosing body, if it appears in
14658 -- some other unit. If it appears in the current unit, the body
14659 -- will have been instantiated already.
14661 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14663 -- We need to determine the expander mode to instantiate the
14664 -- enclosing body. Because the generic body we need may use
14665 -- global entities declared in the enclosing package (including
14666 -- aggregates) it is in general necessary to compile this body
14667 -- with expansion enabled, except if we are within a generic
14668 -- package, in which case the usual generic rule applies.
14670 declare
14671 Exp_Status : Boolean := True;
14672 Scop : Entity_Id;
14674 begin
14675 -- Loop through scopes looking for generic package
14677 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14678 while Present (Scop)
14679 and then Scop /= Standard_Standard
14680 loop
14681 if Ekind (Scop) = E_Generic_Package then
14682 Exp_Status := False;
14683 exit;
14684 end if;
14686 Scop := Scope (Scop);
14687 end loop;
14689 -- Collect previous instantiations in the unit that contains
14690 -- the desired generic.
14692 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14693 and then not Body_Optional
14694 then
14695 declare
14696 Decl : Elmt_Id;
14697 Info : Pending_Body_Info;
14698 Par : Node_Id;
14700 begin
14701 Par := Parent (Inst_Node);
14702 while Present (Par) loop
14703 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14704 Par := Parent (Par);
14705 end loop;
14707 pragma Assert (Present (Par));
14709 if Nkind (Par) = N_Package_Body then
14710 Collect_Previous_Instances (Declarations (Par));
14712 elsif Nkind (Par) = N_Package_Declaration then
14713 Collect_Previous_Instances
14714 (Visible_Declarations (Specification (Par)));
14715 Collect_Previous_Instances
14716 (Private_Declarations (Specification (Par)));
14718 else
14719 -- Enclosing unit is a subprogram body. In this
14720 -- case all instance bodies are processed in order
14721 -- and there is no need to collect them separately.
14723 null;
14724 end if;
14726 Decl := First_Elmt (Previous_Instances);
14727 while Present (Decl) loop
14728 Info :=
14729 (Inst_Node => Node (Decl),
14730 Act_Decl =>
14731 Instance_Spec (Node (Decl)),
14732 Fin_Scop => Empty,
14733 Config_Switches => Save_Config_Switches,
14734 Current_Sem_Unit =>
14735 Get_Code_Unit (Sloc (Node (Decl))),
14736 Expander_Status => Exp_Status,
14737 Local_Suppress_Stack_Top =>
14738 Local_Suppress_Stack_Top,
14739 Scope_Suppress => Scope_Suppress,
14740 Warnings => Save_Warnings);
14742 -- Package instance
14744 if Nkind (Node (Decl)) = N_Package_Instantiation
14745 then
14746 Instantiate_Package_Body
14747 (Info, Body_Optional => True);
14749 -- Subprogram instance
14751 else
14752 -- The instance_spec is in the wrapper package,
14753 -- usually followed by its local renaming
14754 -- declaration. See Build_Subprogram_Renaming
14755 -- for details. If the instance carries aspects,
14756 -- these result in the corresponding pragmas,
14757 -- inserted after the subprogram declaration.
14758 -- They must be skipped as well when retrieving
14759 -- the desired spec. Some of them may have been
14760 -- rewritten as null statements.
14761 -- A direct link would be more robust ???
14763 declare
14764 Decl : Node_Id :=
14765 (Last (Visible_Declarations
14766 (Specification (Info.Act_Decl))));
14767 begin
14768 while Nkind (Decl) in
14769 N_Null_Statement |
14770 N_Pragma |
14771 N_Subprogram_Renaming_Declaration
14772 loop
14773 Decl := Prev (Decl);
14774 end loop;
14776 Info.Act_Decl := Decl;
14777 end;
14779 Instantiate_Subprogram_Body
14780 (Info, Body_Optional => True);
14781 end if;
14783 Next_Elmt (Decl);
14784 end loop;
14785 end;
14786 end if;
14788 Instantiate_Package_Body
14789 (Body_Info =>
14790 ((Inst_Node => Inst_Node,
14791 Act_Decl => True_Parent,
14792 Fin_Scop => Empty,
14793 Config_Switches => Save_Config_Switches,
14794 Current_Sem_Unit =>
14795 Get_Code_Unit (Sloc (Inst_Node)),
14796 Expander_Status => Exp_Status,
14797 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14798 Scope_Suppress => Scope_Suppress,
14799 Warnings => Save_Warnings)),
14800 Body_Optional => Body_Optional);
14801 end;
14802 end if;
14804 -- Case where we are not instantiating a nested generic
14806 else
14807 Opt.Style_Check := False;
14808 Expander_Mode_Save_And_Set (True);
14809 Load_Needed_Body (Comp_Unit, OK);
14810 Opt.Style_Check := Saved_Style_Check;
14811 Restore_Warnings (Saved_Warn);
14812 Expander_Mode_Restore;
14814 if not OK
14815 and then Unit_Requires_Body (Defining_Entity (Spec))
14816 and then not Body_Optional
14817 then
14818 declare
14819 Bname : constant Unit_Name_Type :=
14820 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14822 begin
14823 -- In CodePeer mode, the missing body may make the analysis
14824 -- incomplete, but we do not treat it as fatal.
14826 if CodePeer_Mode then
14827 return;
14829 else
14830 Error_Msg_Unit_1 := Bname;
14831 Error_Msg_N ("this instantiation requires$!", N);
14832 Error_Msg_File_1 :=
14833 Get_File_Name (Bname, Subunit => False);
14834 Error_Msg_N ("\but file{ was not found!", N);
14835 raise Unrecoverable_Error;
14836 end if;
14837 end;
14838 end if;
14839 end if;
14840 end if;
14842 -- If loading parent of the generic caused an instantiation circularity,
14843 -- we abandon compilation at this point, because otherwise in some cases
14844 -- we get into trouble with infinite recursions after this point.
14846 if Circularity_Detected then
14847 raise Unrecoverable_Error;
14848 end if;
14849 end Load_Parent_Of_Generic;
14851 ---------------------------------
14852 -- Map_Formal_Package_Entities --
14853 ---------------------------------
14855 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14856 E1 : Entity_Id;
14857 E2 : Entity_Id;
14859 begin
14860 Set_Instance_Of (Form, Act);
14862 -- Traverse formal and actual package to map the corresponding entities.
14863 -- We skip over internal entities that may be generated during semantic
14864 -- analysis, and find the matching entities by name, given that they
14865 -- must appear in the same order.
14867 E1 := First_Entity (Form);
14868 E2 := First_Entity (Act);
14869 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14870 -- Could this test be a single condition??? Seems like it could, and
14871 -- isn't FPE (Form) a constant anyway???
14873 if not Is_Internal (E1)
14874 and then Present (Parent (E1))
14875 and then not Is_Class_Wide_Type (E1)
14876 and then not Is_Internal_Name (Chars (E1))
14877 then
14878 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14879 Next_Entity (E2);
14880 end loop;
14882 if No (E2) then
14883 exit;
14884 else
14885 Set_Instance_Of (E1, E2);
14887 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14888 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14889 end if;
14891 if Is_Constrained (E1) then
14892 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14893 end if;
14895 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14896 Map_Formal_Package_Entities (E1, E2);
14897 end if;
14898 end if;
14899 end if;
14901 Next_Entity (E1);
14902 end loop;
14903 end Map_Formal_Package_Entities;
14905 -----------------------
14906 -- Move_Freeze_Nodes --
14907 -----------------------
14909 procedure Move_Freeze_Nodes
14910 (Out_Of : Entity_Id;
14911 After : Node_Id;
14912 L : List_Id)
14914 Decl : Node_Id;
14915 Next_Decl : Node_Id;
14916 Next_Node : Node_Id := After;
14917 Spec : Node_Id;
14919 function Is_Outer_Type (T : Entity_Id) return Boolean;
14920 -- Check whether entity is declared in a scope external to that of the
14921 -- generic unit.
14923 -------------------
14924 -- Is_Outer_Type --
14925 -------------------
14927 function Is_Outer_Type (T : Entity_Id) return Boolean is
14928 Scop : Entity_Id := Scope (T);
14930 begin
14931 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14932 return True;
14934 else
14935 while Scop /= Standard_Standard loop
14936 if Scop = Out_Of then
14937 return False;
14938 else
14939 Scop := Scope (Scop);
14940 end if;
14941 end loop;
14943 return True;
14944 end if;
14945 end Is_Outer_Type;
14947 -- Start of processing for Move_Freeze_Nodes
14949 begin
14950 if No (L) then
14951 return;
14952 end if;
14954 -- First remove the freeze nodes that may appear before all other
14955 -- declarations.
14957 Decl := First (L);
14958 while Present (Decl)
14959 and then Nkind (Decl) = N_Freeze_Entity
14960 and then Is_Outer_Type (Entity (Decl))
14961 loop
14962 Decl := Remove_Head (L);
14963 Insert_After (Next_Node, Decl);
14964 Set_Analyzed (Decl, False);
14965 Next_Node := Decl;
14966 Decl := First (L);
14967 end loop;
14969 -- Next scan the list of declarations and remove each freeze node that
14970 -- appears ahead of the current node.
14972 while Present (Decl) loop
14973 while Present (Next (Decl))
14974 and then Nkind (Next (Decl)) = N_Freeze_Entity
14975 and then Is_Outer_Type (Entity (Next (Decl)))
14976 loop
14977 Next_Decl := Remove_Next (Decl);
14978 Insert_After (Next_Node, Next_Decl);
14979 Set_Analyzed (Next_Decl, False);
14980 Next_Node := Next_Decl;
14981 end loop;
14983 -- If the declaration is a nested package or concurrent type, then
14984 -- recurse. Nested generic packages will have been processed from the
14985 -- inside out.
14987 case Nkind (Decl) is
14988 when N_Package_Declaration =>
14989 Spec := Specification (Decl);
14991 when N_Task_Type_Declaration =>
14992 Spec := Task_Definition (Decl);
14994 when N_Protected_Type_Declaration =>
14995 Spec := Protected_Definition (Decl);
14997 when others =>
14998 Spec := Empty;
14999 end case;
15001 if Present (Spec) then
15002 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15003 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15004 end if;
15006 Next (Decl);
15007 end loop;
15008 end Move_Freeze_Nodes;
15010 ----------------
15011 -- Next_Assoc --
15012 ----------------
15014 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15015 begin
15016 return Generic_Renamings.Table (E).Next_In_HTable;
15017 end Next_Assoc;
15019 ------------------------
15020 -- Preanalyze_Actuals --
15021 ------------------------
15023 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15024 procedure Perform_Appropriate_Analysis (N : Node_Id);
15025 -- Determine if the actuals we are analyzing come from a generic
15026 -- instantiation that is a library unit and dispatch accordingly.
15028 ----------------------------------
15029 -- Perform_Appropriate_Analysis --
15030 ----------------------------------
15032 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15033 begin
15034 -- When we have a library instantiation we cannot allow any expansion
15035 -- to occur, since there may be no place to put it. Instead, in that
15036 -- case we perform a preanalysis of the actual.
15038 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15039 Preanalyze (N);
15040 else
15041 Analyze (N);
15042 end if;
15043 end Perform_Appropriate_Analysis;
15045 -- Local variables
15047 Errs : constant Nat := Serious_Errors_Detected;
15049 Assoc : Node_Id;
15050 Act : Node_Id;
15052 Cur : Entity_Id := Empty;
15053 -- Current homograph of the instance name
15055 Vis : Boolean := False;
15056 -- Saved visibility status of the current homograph
15058 -- Start of processing for Preanalyze_Actuals
15060 begin
15061 Assoc := First (Generic_Associations (N));
15063 -- If the instance is a child unit, its name may hide an outer homonym,
15064 -- so make it invisible to perform name resolution on the actuals.
15066 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15067 and then Present
15068 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15069 then
15070 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15072 if Is_Compilation_Unit (Cur) then
15073 Vis := Is_Immediately_Visible (Cur);
15074 Set_Is_Immediately_Visible (Cur, False);
15075 else
15076 Cur := Empty;
15077 end if;
15078 end if;
15080 while Present (Assoc) loop
15081 if Nkind (Assoc) /= N_Others_Choice then
15082 Act := Explicit_Generic_Actual_Parameter (Assoc);
15084 -- Within a nested instantiation, a defaulted actual is an empty
15085 -- association, so nothing to analyze. If the subprogram actual
15086 -- is an attribute, analyze prefix only, because actual is not a
15087 -- complete attribute reference.
15089 -- If actual is an allocator, analyze expression only. The full
15090 -- analysis can generate code, and if instance is a compilation
15091 -- unit we have to wait until the package instance is installed
15092 -- to have a proper place to insert this code.
15094 -- String literals may be operators, but at this point we do not
15095 -- know whether the actual is a formal subprogram or a string.
15097 if No (Act) then
15098 null;
15100 elsif Nkind (Act) = N_Attribute_Reference then
15101 Perform_Appropriate_Analysis (Prefix (Act));
15103 elsif Nkind (Act) = N_Explicit_Dereference then
15104 Perform_Appropriate_Analysis (Prefix (Act));
15106 elsif Nkind (Act) = N_Allocator then
15107 declare
15108 Expr : constant Node_Id := Expression (Act);
15110 begin
15111 if Nkind (Expr) = N_Subtype_Indication then
15112 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15114 -- Analyze separately each discriminant constraint, when
15115 -- given with a named association.
15117 declare
15118 Constr : Node_Id;
15120 begin
15121 Constr := First (Constraints (Constraint (Expr)));
15122 while Present (Constr) loop
15123 if Nkind (Constr) = N_Discriminant_Association then
15124 Perform_Appropriate_Analysis
15125 (Expression (Constr));
15126 else
15127 Perform_Appropriate_Analysis (Constr);
15128 end if;
15130 Next (Constr);
15131 end loop;
15132 end;
15134 else
15135 Perform_Appropriate_Analysis (Expr);
15136 end if;
15137 end;
15139 elsif Nkind (Act) /= N_Operator_Symbol then
15140 Perform_Appropriate_Analysis (Act);
15142 -- Within a package instance, mark actuals that are limited
15143 -- views, so their use can be moved to the body of the
15144 -- enclosing unit.
15146 if Is_Entity_Name (Act)
15147 and then Is_Type (Entity (Act))
15148 and then From_Limited_With (Entity (Act))
15149 and then Present (Inst)
15150 then
15151 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15152 end if;
15153 end if;
15155 if Errs /= Serious_Errors_Detected then
15157 -- Do a minimal analysis of the generic, to prevent spurious
15158 -- warnings complaining about the generic being unreferenced,
15159 -- before abandoning the instantiation.
15161 Perform_Appropriate_Analysis (Name (N));
15163 if Is_Entity_Name (Name (N))
15164 and then Etype (Name (N)) /= Any_Type
15165 then
15166 Generate_Reference (Entity (Name (N)), Name (N));
15167 Set_Is_Instantiated (Entity (Name (N)));
15168 end if;
15170 if Present (Cur) then
15172 -- For the case of a child instance hiding an outer homonym,
15173 -- provide additional warning which might explain the error.
15175 Set_Is_Immediately_Visible (Cur, Vis);
15176 Error_Msg_NE
15177 ("& hides outer unit with the same name??",
15178 N, Defining_Unit_Name (N));
15179 end if;
15181 Abandon_Instantiation (Act);
15182 end if;
15183 end if;
15185 Next (Assoc);
15186 end loop;
15188 if Present (Cur) then
15189 Set_Is_Immediately_Visible (Cur, Vis);
15190 end if;
15191 end Preanalyze_Actuals;
15193 -------------------------------
15194 -- Provide_Completing_Bodies --
15195 -------------------------------
15197 procedure Provide_Completing_Bodies (N : Node_Id) is
15198 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15199 -- Generate the completing body for subprogram declaration Subp_Decl
15201 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15202 -- Generating completing bodies for all subprograms found in declarative
15203 -- list Decls.
15205 ---------------------------
15206 -- Build_Completing_Body --
15207 ---------------------------
15209 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15210 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15211 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15212 Spec : Node_Id;
15214 begin
15215 -- Nothing to do if the subprogram already has a completing body
15217 if Present (Corresponding_Body (Subp_Decl)) then
15218 return;
15220 -- Mark the function as having a valid return statement even though
15221 -- the body contains a single raise statement.
15223 elsif Ekind (Subp_Id) = E_Function then
15224 Set_Return_Present (Subp_Id);
15225 end if;
15227 -- Clone the specification to obtain new entities and reset the only
15228 -- semantic field.
15230 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15231 Set_Generic_Parent (Spec, Empty);
15233 -- Generate:
15234 -- function Func ... return ... is
15235 -- <or>
15236 -- procedure Proc ... is
15237 -- begin
15238 -- raise Program_Error with "access before elaboration";
15239 -- edn Proc;
15241 Insert_After_And_Analyze (Subp_Decl,
15242 Make_Subprogram_Body (Loc,
15243 Specification => Spec,
15244 Declarations => New_List,
15245 Handled_Statement_Sequence =>
15246 Make_Handled_Sequence_Of_Statements (Loc,
15247 Statements => New_List (
15248 Make_Raise_Program_Error (Loc,
15249 Reason => PE_Access_Before_Elaboration)))));
15250 end Build_Completing_Body;
15252 ----------------------------------
15253 -- Provide_Completing_Bodies_In --
15254 ----------------------------------
15256 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15257 Decl : Node_Id;
15259 begin
15260 if Present (Decls) then
15261 Decl := First (Decls);
15262 while Present (Decl) loop
15263 Provide_Completing_Bodies (Decl);
15264 Next (Decl);
15265 end loop;
15266 end if;
15267 end Provide_Completing_Bodies_In;
15269 -- Local variables
15271 Spec : Node_Id;
15273 -- Start of processing for Provide_Completing_Bodies
15275 begin
15276 if Nkind (N) = N_Package_Declaration then
15277 Spec := Specification (N);
15279 Push_Scope (Defining_Entity (N));
15280 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15281 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15282 Pop_Scope;
15284 elsif Nkind (N) = N_Subprogram_Declaration then
15285 Build_Completing_Body (N);
15286 end if;
15287 end Provide_Completing_Bodies;
15289 -------------------
15290 -- Remove_Parent --
15291 -------------------
15293 procedure Remove_Parent (In_Body : Boolean := False) is
15294 S : Entity_Id := Current_Scope;
15295 -- S is the scope containing the instantiation just completed. The scope
15296 -- stack contains the parent instances of the instantiation, followed by
15297 -- the original S.
15299 Cur_P : Entity_Id;
15300 E : Entity_Id;
15301 P : Entity_Id;
15302 Hidden : Elmt_Id;
15304 begin
15305 -- After child instantiation is complete, remove from scope stack the
15306 -- extra copy of the current scope, and then remove parent instances.
15308 if not In_Body then
15309 Pop_Scope;
15311 while Current_Scope /= S loop
15312 P := Current_Scope;
15313 End_Package_Scope (Current_Scope);
15315 if In_Open_Scopes (P) then
15316 E := First_Entity (P);
15317 while Present (E) loop
15318 Set_Is_Immediately_Visible (E, True);
15319 Next_Entity (E);
15320 end loop;
15322 -- If instantiation is declared in a block, it is the enclosing
15323 -- scope that might be a parent instance. Note that only one
15324 -- block can be involved, because the parent instances have
15325 -- been installed within it.
15327 if Ekind (P) = E_Block then
15328 Cur_P := Scope (P);
15329 else
15330 Cur_P := P;
15331 end if;
15333 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15334 -- We are within an instance of some sibling. Retain
15335 -- visibility of parent, for proper subsequent cleanup, and
15336 -- reinstall private declarations as well.
15338 Set_In_Private_Part (P);
15339 Install_Private_Declarations (P);
15340 end if;
15342 -- If the ultimate parent is a top-level unit recorded in
15343 -- Instance_Parent_Unit, then reset its visibility to what it was
15344 -- before instantiation. (It's not clear what the purpose is of
15345 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15346 -- present before the ultimate parent test was added.???)
15348 elsif not In_Open_Scopes (Scope (P))
15349 or else (P = Instance_Parent_Unit
15350 and then not Parent_Unit_Visible)
15351 then
15352 Set_Is_Immediately_Visible (P, False);
15354 -- If the current scope is itself an instantiation of a generic
15355 -- nested within P, and we are in the private part of body of this
15356 -- instantiation, restore the full views of P, that were removed
15357 -- in End_Package_Scope above. This obscure case can occur when a
15358 -- subunit of a generic contains an instance of a child unit of
15359 -- its generic parent unit.
15361 elsif S = Current_Scope and then Is_Generic_Instance (S)
15362 and then (In_Package_Body (S) or else In_Private_Part (S))
15363 then
15364 declare
15365 Par : constant Entity_Id :=
15366 Generic_Parent (Package_Specification (S));
15367 begin
15368 if Present (Par)
15369 and then P = Scope (Par)
15370 then
15371 Set_In_Private_Part (P);
15372 Install_Private_Declarations (P);
15373 end if;
15374 end;
15375 end if;
15376 end loop;
15378 -- Reset visibility of entities in the enclosing scope
15380 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15382 Hidden := First_Elmt (Hidden_Entities);
15383 while Present (Hidden) loop
15384 Set_Is_Immediately_Visible (Node (Hidden), True);
15385 Next_Elmt (Hidden);
15386 end loop;
15388 else
15389 -- Each body is analyzed separately, and there is no context that
15390 -- needs preserving from one body instance to the next, so remove all
15391 -- parent scopes that have been installed.
15393 while Present (S) loop
15394 End_Package_Scope (S);
15395 Set_Is_Immediately_Visible (S, False);
15396 S := Current_Scope;
15397 exit when S = Standard_Standard;
15398 end loop;
15399 end if;
15400 end Remove_Parent;
15402 -----------------------------------
15403 -- Requires_Conformance_Checking --
15404 -----------------------------------
15406 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15407 begin
15408 -- No conformance checking required if the generic actual part is empty,
15409 -- or is a box or an others_clause (necessarily with a box).
15411 return Present (Generic_Associations (N))
15412 and then not Box_Present (N)
15413 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15414 end Requires_Conformance_Checking;
15416 -----------------
15417 -- Restore_Env --
15418 -----------------
15420 procedure Restore_Env is
15421 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15423 begin
15424 if No (Current_Instantiated_Parent.Act_Id) then
15425 -- Restore environment after subprogram inlining
15427 Restore_Private_Views (Empty);
15428 end if;
15430 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15431 Exchanged_Views := Saved.Exchanged_Views;
15432 Hidden_Entities := Saved.Hidden_Entities;
15433 Current_Sem_Unit := Saved.Current_Sem_Unit;
15434 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15435 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15437 Restore_Config_Switches (Saved.Switches);
15439 Instance_Envs.Decrement_Last;
15440 end Restore_Env;
15442 ---------------------------
15443 -- Restore_Private_Views --
15444 ---------------------------
15446 procedure Restore_Private_Views
15447 (Pack_Id : Entity_Id;
15448 Is_Package : Boolean := True)
15450 M : Elmt_Id;
15451 E : Entity_Id;
15452 Typ : Entity_Id;
15453 Dep_Elmt : Elmt_Id;
15454 Dep_Typ : Node_Id;
15456 procedure Restore_Nested_Formal (Formal : Entity_Id);
15457 -- Hide the generic formals of formal packages declared with box which
15458 -- were reachable in the current instantiation.
15460 ---------------------------
15461 -- Restore_Nested_Formal --
15462 ---------------------------
15464 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15465 pragma Assert (Ekind (Formal) = E_Package);
15466 Ent : Entity_Id;
15467 begin
15468 if Present (Renamed_Entity (Formal))
15469 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15470 then
15471 return;
15473 elsif Present (Associated_Formal_Package (Formal)) then
15474 Ent := First_Entity (Formal);
15475 while Present (Ent) loop
15476 exit when Ekind (Ent) = E_Package
15477 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15479 Set_Is_Hidden (Ent);
15480 Set_Is_Potentially_Use_Visible (Ent, False);
15482 -- If package, then recurse
15484 if Ekind (Ent) = E_Package then
15485 Restore_Nested_Formal (Ent);
15486 end if;
15488 Next_Entity (Ent);
15489 end loop;
15490 end if;
15491 end Restore_Nested_Formal;
15493 -- Start of processing for Restore_Private_Views
15495 begin
15496 M := First_Elmt (Exchanged_Views);
15497 while Present (M) loop
15498 Typ := Node (M);
15500 -- Subtypes of types whose views have been exchanged, and that are
15501 -- defined within the instance, were not on the Private_Dependents
15502 -- list on entry to the instance, so they have to be exchanged
15503 -- explicitly now, in order to remain consistent with the view of the
15504 -- parent type.
15506 if Ekind (Typ) in E_Private_Type
15507 | E_Limited_Private_Type
15508 | E_Record_Type_With_Private
15509 then
15510 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15511 while Present (Dep_Elmt) loop
15512 Dep_Typ := Node (Dep_Elmt);
15514 if Scope (Dep_Typ) = Pack_Id
15515 and then Present (Full_View (Dep_Typ))
15516 then
15517 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15518 Exchange_Declarations (Dep_Typ);
15519 end if;
15521 Next_Elmt (Dep_Elmt);
15522 end loop;
15523 end if;
15525 Exchange_Declarations (Typ);
15526 Next_Elmt (M);
15527 end loop;
15529 if No (Pack_Id) then
15530 return;
15531 end if;
15533 -- Make the generic formal parameters private, and make the formal types
15534 -- into subtypes of the actuals again.
15536 E := First_Entity (Pack_Id);
15537 while Present (E) loop
15538 Set_Is_Hidden (E, True);
15540 if Is_Type (E)
15541 and then Nkind (Parent (E)) = N_Subtype_Declaration
15542 then
15543 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15544 -- as it is needed to identify the subtype with the type it
15545 -- renames, when there are conversions between access types
15546 -- to these.
15548 if GNATprove_Mode then
15549 null;
15551 -- If the actual for E is itself a generic actual type from
15552 -- an enclosing instance, E is still a generic actual type
15553 -- outside of the current instance. This matter when resolving
15554 -- an overloaded call that may be ambiguous in the enclosing
15555 -- instance, when two of its actuals coincide.
15557 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15558 and then Is_Generic_Actual_Type
15559 (Entity (Subtype_Indication (Parent (E))))
15560 then
15561 null;
15562 else
15563 Set_Is_Generic_Actual_Type (E, False);
15565 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15566 -- flag also on the Full_View if the type is private, since it
15567 -- was set also on this Full_View. However, this flag is relied
15568 -- upon by Covers to spot "types exported from instantiations"
15569 -- which are implicit Full_Views built for instantiations made
15570 -- on private types and we get type mismatches if we do it when
15571 -- the block exchanging the declarations below triggers ???
15573 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15574 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15575 -- end if;
15576 end if;
15578 -- An unusual case of aliasing: the actual may also be directly
15579 -- visible in the generic, and be private there, while it is fully
15580 -- visible in the context of the instance. The internal subtype
15581 -- is private in the instance but has full visibility like its
15582 -- parent in the enclosing scope. This enforces the invariant that
15583 -- the privacy status of all private dependents of a type coincide
15584 -- with that of the parent type. This can only happen when a
15585 -- generic child unit is instantiated within a sibling.
15587 if Is_Private_Type (E)
15588 and then not Is_Private_Type (Etype (E))
15589 then
15590 Exchange_Declarations (E);
15591 end if;
15593 elsif Ekind (E) = E_Package then
15595 -- The end of the renaming list is the renaming of the generic
15596 -- package itself. If the instance is a subprogram, all entities
15597 -- in the corresponding package are renamings. If this entity is
15598 -- a formal package, make its own formals private as well. The
15599 -- actual in this case is itself the renaming of an instantiation.
15600 -- If the entity is not a package renaming, it is the entity
15601 -- created to validate formal package actuals: ignore it.
15603 -- If the actual is itself a formal package for the enclosing
15604 -- generic, or the actual for such a formal package, it remains
15605 -- visible on exit from the instance, and therefore nothing needs
15606 -- to be done either, except to keep it accessible.
15608 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15609 exit;
15611 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15612 null;
15614 elsif
15615 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15616 then
15617 Set_Is_Hidden (E, False);
15619 else
15620 declare
15621 Act_P : constant Entity_Id := Renamed_Entity (E);
15622 Id : Entity_Id;
15624 begin
15625 Id := First_Entity (Act_P);
15626 while Present (Id)
15627 and then Id /= First_Private_Entity (Act_P)
15628 loop
15629 exit when Ekind (Id) = E_Package
15630 and then Renamed_Entity (Id) = Act_P;
15632 Set_Is_Hidden (Id, True);
15633 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15635 if Ekind (Id) = E_Package then
15636 Restore_Nested_Formal (Id);
15637 end if;
15639 Next_Entity (Id);
15640 end loop;
15641 end;
15642 end if;
15643 end if;
15645 Next_Entity (E);
15646 end loop;
15647 end Restore_Private_Views;
15649 --------------
15650 -- Save_Env --
15651 --------------
15653 procedure Save_Env
15654 (Gen_Unit : Entity_Id;
15655 Act_Unit : Entity_Id)
15657 begin
15658 Init_Env;
15659 Set_Instance_Env (Gen_Unit, Act_Unit);
15660 end Save_Env;
15662 ----------------------------
15663 -- Save_Global_References --
15664 ----------------------------
15666 procedure Save_Global_References (Templ : Node_Id) is
15668 -- ??? it is horrible to use global variables in highly recursive code
15670 E : Entity_Id;
15671 -- The entity of the current associated node
15673 Gen_Scope : Entity_Id;
15674 -- The scope of the generic for which references are being saved
15676 N2 : Node_Id;
15677 -- The current associated node
15679 function Is_Global (E : Entity_Id) return Boolean;
15680 -- Check whether entity is defined outside of generic unit. Examine the
15681 -- scope of an entity, and the scope of the scope, etc, until we find
15682 -- either Standard, in which case the entity is global, or the generic
15683 -- unit itself, which indicates that the entity is local. If the entity
15684 -- is the generic unit itself, as in the case of a recursive call, or
15685 -- the enclosing generic unit, if different from the current scope, then
15686 -- it is local as well, because it will be replaced at the point of
15687 -- instantiation. On the other hand, if it is a reference to a child
15688 -- unit of a common ancestor, which appears in an instantiation, it is
15689 -- global because it is used to denote a specific compilation unit at
15690 -- the time the instantiations will be analyzed.
15692 procedure Qualify_Universal_Operands
15693 (Op : Node_Id;
15694 Func_Call : Node_Id);
15695 -- Op denotes a binary or unary operator in generic template Templ. Node
15696 -- Func_Call is the function call alternative of the operator within the
15697 -- the analyzed copy of the template. Change each operand which yields a
15698 -- universal type by wrapping it into a qualified expression
15700 -- Actual_Typ'(Operand)
15702 -- where Actual_Typ is the type of corresponding actual parameter of
15703 -- Operand in Func_Call.
15705 procedure Reset_Entity (N : Node_Id);
15706 -- Save semantic information on global entity so that it is not resolved
15707 -- again at instantiation time.
15709 procedure Save_Entity_Descendants (N : Node_Id);
15710 -- Apply Save_Global_References to the two syntactic descendants of
15711 -- non-terminal nodes that carry an Associated_Node and are processed
15712 -- through Reset_Entity. Once the global entity (if any) has been
15713 -- captured together with its type, only two syntactic descendants need
15714 -- to be traversed to complete the processing of the tree rooted at N.
15715 -- This applies to Selected_Components, Expanded_Names, and to Operator
15716 -- nodes. N can also be a character literal, identifier, or operator
15717 -- symbol node, but the call has no effect in these cases.
15719 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15720 -- Default actuals in nested instances must be handled specially
15721 -- because there is no link to them from the original tree. When an
15722 -- actual subprogram is given by a default, we add an explicit generic
15723 -- association for it in the instantiation node. When we save the
15724 -- global references on the name of the instance, we recover the list
15725 -- of generic associations, and add an explicit one to the original
15726 -- generic tree, through which a global actual can be preserved.
15727 -- Similarly, if a child unit is instantiated within a sibling, in the
15728 -- context of the parent, we must preserve the identifier of the parent
15729 -- so that it can be properly resolved in a subsequent instantiation.
15731 procedure Save_Global_Descendant (D : Union_Id);
15732 -- Apply Save_References recursively to the descendants of node D
15734 procedure Save_References (N : Node_Id);
15735 -- This is the recursive procedure that does the work, once the
15736 -- enclosing generic scope has been established.
15738 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15739 -- If the type of N2 is global to the generic unit, save the type in
15740 -- the generic node. Just as we perform name capture for explicit
15741 -- references within the generic, we must capture the global types
15742 -- of local entities because they may participate in resolution in
15743 -- the instance.
15745 ---------------
15746 -- Is_Global --
15747 ---------------
15749 function Is_Global (E : Entity_Id) return Boolean is
15750 Se : Entity_Id;
15752 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15753 -- Determine whether the parent node of a reference to a child unit
15754 -- denotes an instantiation or a formal package, in which case the
15755 -- reference to the child unit is global, even if it appears within
15756 -- the current scope (e.g. when the instance appears within the body
15757 -- of an ancestor).
15759 ----------------------
15760 -- Is_Instance_Node --
15761 ----------------------
15763 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15764 begin
15765 return Nkind (Decl) in N_Generic_Instantiation
15766 or else
15767 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15768 end Is_Instance_Node;
15770 -- Start of processing for Is_Global
15772 begin
15773 if E = Gen_Scope then
15774 return False;
15776 elsif E = Standard_Standard then
15777 return True;
15779 -- E should be an entity, but it is not always
15781 elsif Nkind (E) not in N_Entity then
15782 return False;
15784 elsif Nkind (E) /= N_Expanded_Name
15785 and then Is_Child_Unit (E)
15786 and then (Is_Instance_Node (Parent (N2))
15787 or else (Nkind (Parent (N2)) = N_Expanded_Name
15788 and then N2 = Selector_Name (Parent (N2))
15789 and then
15790 Is_Instance_Node (Parent (Parent (N2)))))
15791 then
15792 return True;
15794 else
15795 -- E may be an expanded name - typically an operator - in which
15796 -- case we must find its enclosing scope since expanded names
15797 -- don't have corresponding scopes.
15799 if Nkind (E) = N_Expanded_Name then
15800 Se := Find_Enclosing_Scope (E);
15802 -- Otherwise, E is an entity and will have Scope set
15804 else
15805 Se := Scope (E);
15806 end if;
15808 while Se /= Gen_Scope loop
15809 if Se = Standard_Standard then
15810 return True;
15811 else
15812 Se := Scope (Se);
15813 end if;
15814 end loop;
15816 return False;
15817 end if;
15818 end Is_Global;
15820 --------------------------------
15821 -- Qualify_Universal_Operands --
15822 --------------------------------
15824 procedure Qualify_Universal_Operands
15825 (Op : Node_Id;
15826 Func_Call : Node_Id)
15828 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15829 -- Rewrite operand Opnd as a qualified expression of the form
15831 -- Actual_Typ'(Opnd)
15833 -- where Actual is the corresponding actual parameter of Opnd in
15834 -- function call Func_Call.
15836 function Qualify_Type
15837 (Loc : Source_Ptr;
15838 Typ : Entity_Id) return Node_Id;
15839 -- Qualify type Typ by creating a selected component of the form
15841 -- Scope_Of_Typ.Typ
15843 ---------------------
15844 -- Qualify_Operand --
15845 ---------------------
15847 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15848 Loc : constant Source_Ptr := Sloc (Opnd);
15849 Typ : constant Entity_Id := Etype (Actual);
15850 Mark : Node_Id;
15851 Qual : Node_Id;
15853 begin
15854 -- Qualify the operand when it is of a universal type. Note that
15855 -- the template is unanalyzed and it is not possible to directly
15856 -- query the type. This transformation is not done when the type
15857 -- of the actual is internally generated because the type will be
15858 -- regenerated in the instance.
15860 if Yields_Universal_Type (Opnd)
15861 and then Comes_From_Source (Typ)
15862 and then not Is_Hidden (Typ)
15863 then
15864 -- The type of the actual may be a global reference. Save this
15865 -- information by creating a reference to it.
15867 if Is_Global (Typ) then
15868 Mark := New_Occurrence_Of (Typ, Loc);
15870 -- Otherwise rely on resolution to find the proper type within
15871 -- the instance.
15873 else
15874 Mark := Qualify_Type (Loc, Typ);
15875 end if;
15877 Qual :=
15878 Make_Qualified_Expression (Loc,
15879 Subtype_Mark => Mark,
15880 Expression => Relocate_Node (Opnd));
15882 -- Mark the qualification to distinguish it from other source
15883 -- constructs and signal the instantiation mechanism that this
15884 -- node requires special processing. See Copy_Generic_Node for
15885 -- details.
15887 Set_Is_Qualified_Universal_Literal (Qual);
15889 Rewrite (Opnd, Qual);
15890 end if;
15891 end Qualify_Operand;
15893 ------------------
15894 -- Qualify_Type --
15895 ------------------
15897 function Qualify_Type
15898 (Loc : Source_Ptr;
15899 Typ : Entity_Id) return Node_Id
15901 Scop : constant Entity_Id := Scope (Typ);
15902 Result : Node_Id;
15904 begin
15905 Result := Make_Identifier (Loc, Chars (Typ));
15907 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15908 Result :=
15909 Make_Selected_Component (Loc,
15910 Prefix => Make_Identifier (Loc, Chars (Scop)),
15911 Selector_Name => Result);
15912 end if;
15914 return Result;
15915 end Qualify_Type;
15917 -- Local variables
15919 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15921 -- Start of processing for Qualify_Universal_Operands
15923 begin
15924 if Nkind (Op) in N_Binary_Op then
15925 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15926 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15928 elsif Nkind (Op) in N_Unary_Op then
15929 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15930 end if;
15931 end Qualify_Universal_Operands;
15933 ------------------
15934 -- Reset_Entity --
15935 ------------------
15937 procedure Reset_Entity (N : Node_Id) is
15938 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15939 -- Find the ultimate ancestor of the current unit. If it is not a
15940 -- generic unit, then the name of the current unit in the prefix of
15941 -- an expanded name must be replaced with its generic homonym to
15942 -- ensure that it will be properly resolved in an instance.
15944 ------------------
15945 -- Top_Ancestor --
15946 ------------------
15948 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15949 Par : Entity_Id;
15951 begin
15952 Par := E;
15953 while Is_Child_Unit (Par) loop
15954 Par := Scope (Par);
15955 end loop;
15957 return Par;
15958 end Top_Ancestor;
15960 -- Start of processing for Reset_Entity
15962 begin
15963 N2 := Get_Associated_Node (N);
15964 E := Entity (N2);
15966 if Present (E) then
15968 -- If the node is an entry call to an entry in an enclosing task,
15969 -- it is rewritten as a selected component. No global entity to
15970 -- preserve in this case, since the expansion will be redone in
15971 -- the instance.
15973 if Nkind (E) not in N_Entity then
15974 Set_Associated_Node (N, Empty);
15975 Set_Etype (N, Empty);
15976 return;
15977 end if;
15979 -- If the entity is an itype created as a subtype of an access
15980 -- type with a null exclusion restore source entity for proper
15981 -- visibility. The itype will be created anew in the instance.
15983 if Is_Itype (E)
15984 and then Ekind (E) = E_Access_Subtype
15985 and then Is_Entity_Name (N)
15986 and then Chars (Etype (E)) = Chars (N)
15987 then
15988 E := Etype (E);
15989 Set_Entity (N2, E);
15990 Set_Etype (N2, E);
15991 end if;
15993 if Is_Global (E) then
15994 Set_Global_Type (N, N2);
15996 elsif Nkind (N) = N_Op_Concat
15997 and then Is_Generic_Type (Etype (N2))
15998 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15999 or else
16000 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16001 and then Is_Intrinsic_Subprogram (E)
16002 then
16003 null;
16005 -- Entity is local. Mark generic node as unresolved. Note that now
16006 -- it does not have an entity.
16008 else
16009 Set_Associated_Node (N, Empty);
16010 Set_Etype (N, Empty);
16011 end if;
16013 if Nkind (Parent (N)) in N_Generic_Instantiation
16014 and then N = Name (Parent (N))
16015 then
16016 Save_Global_Defaults (Parent (N), Parent (N2));
16017 end if;
16019 elsif Nkind (Parent (N)) = N_Selected_Component
16020 and then Nkind (Parent (N2)) = N_Expanded_Name
16021 then
16022 -- In case of previous errors, the tree might be malformed
16024 if No (Entity (Parent (N2))) then
16025 null;
16027 elsif Is_Global (Entity (Parent (N2))) then
16028 Change_Selected_Component_To_Expanded_Name (Parent (N));
16029 Set_Associated_Node (Parent (N), Parent (N2));
16030 Set_Global_Type (Parent (N), Parent (N2));
16031 Save_Entity_Descendants (N);
16033 -- If this is a reference to the current generic entity, replace
16034 -- by the name of the generic homonym of the current package. This
16035 -- is because in an instantiation Par.P.Q will not resolve to the
16036 -- name of the instance, whose enclosing scope is not necessarily
16037 -- Par. We use the generic homonym rather that the name of the
16038 -- generic itself because it may be hidden by a local declaration.
16040 elsif In_Open_Scopes (Entity (Parent (N2)))
16041 and then not
16042 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16043 then
16044 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16045 Rewrite (Parent (N),
16046 Make_Identifier (Sloc (N),
16047 Chars =>
16048 Chars (Generic_Homonym (Entity (Parent (N2))))));
16049 else
16050 Rewrite (Parent (N),
16051 Make_Identifier (Sloc (N),
16052 Chars => Chars (Selector_Name (Parent (N2)))));
16053 end if;
16054 end if;
16056 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16057 and then Parent (N) = Name (Parent (Parent (N)))
16058 then
16059 Save_Global_Defaults
16060 (Parent (Parent (N)), Parent (Parent (N2)));
16061 end if;
16063 -- A selected component may denote a static constant that has been
16064 -- folded. If the static constant is global to the generic, capture
16065 -- its value. Otherwise the folding will happen in any instantiation.
16067 elsif Nkind (Parent (N)) = N_Selected_Component
16068 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16069 then
16070 if Present (Entity (Original_Node (Parent (N2))))
16071 and then Is_Global (Entity (Original_Node (Parent (N2))))
16072 then
16073 Rewrite (Parent (N), New_Copy (Parent (N2)));
16074 Set_Analyzed (Parent (N), False);
16075 end if;
16077 -- A selected component may be transformed into a parameterless
16078 -- function call. If the called entity is global, rewrite the node
16079 -- appropriately, i.e. as an extended name for the global entity.
16081 elsif Nkind (Parent (N)) = N_Selected_Component
16082 and then Nkind (Parent (N2)) = N_Function_Call
16083 and then N = Selector_Name (Parent (N))
16084 then
16085 if No (Parameter_Associations (Parent (N2))) then
16086 if Is_Global (Entity (Name (Parent (N2)))) then
16087 Change_Selected_Component_To_Expanded_Name (Parent (N));
16088 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16089 Set_Global_Type (Parent (N), Name (Parent (N2)));
16090 Save_Entity_Descendants (N);
16092 else
16093 Set_Is_Prefixed_Call (Parent (N));
16094 Set_Associated_Node (N, Empty);
16095 Set_Etype (N, Empty);
16096 end if;
16098 -- In Ada 2005, X.F may be a call to a primitive operation,
16099 -- rewritten as F (X). This rewriting will be done again in an
16100 -- instance, so keep the original node. Global entities will be
16101 -- captured as for other constructs. Indicate that this must
16102 -- resolve as a call, to prevent accidental overloading in the
16103 -- instance, if both a component and a primitive operation appear
16104 -- as candidates.
16106 else
16107 Set_Is_Prefixed_Call (Parent (N));
16108 end if;
16110 -- Entity is local. Reset in generic unit, so that node is resolved
16111 -- anew at the point of instantiation.
16113 else
16114 Set_Associated_Node (N, Empty);
16115 Set_Etype (N, Empty);
16116 end if;
16117 end Reset_Entity;
16119 -----------------------------
16120 -- Save_Entity_Descendants --
16121 -----------------------------
16123 procedure Save_Entity_Descendants (N : Node_Id) is
16124 begin
16125 case Nkind (N) is
16126 when N_Binary_Op =>
16127 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16128 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16130 when N_Unary_Op =>
16131 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16133 when N_Expanded_Name
16134 | N_Selected_Component
16136 Save_Global_Descendant (Union_Id (Prefix (N)));
16137 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16139 when N_Character_Literal
16140 | N_Identifier
16141 | N_Operator_Symbol
16143 null;
16145 when others =>
16146 raise Program_Error;
16147 end case;
16148 end Save_Entity_Descendants;
16150 --------------------------
16151 -- Save_Global_Defaults --
16152 --------------------------
16154 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16155 Loc : constant Source_Ptr := Sloc (N1);
16156 Assoc2 : constant List_Id := Generic_Associations (N2);
16157 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16158 Assoc1 : List_Id;
16159 Act1 : Node_Id;
16160 Act2 : Node_Id;
16161 Def : Node_Id;
16162 Ndec : Node_Id;
16163 Subp : Entity_Id;
16164 Actual : Entity_Id;
16166 begin
16167 Assoc1 := Generic_Associations (N1);
16169 if Present (Assoc1) then
16170 Act1 := First (Assoc1);
16171 else
16172 Act1 := Empty;
16173 Set_Generic_Associations (N1, New_List);
16174 Assoc1 := Generic_Associations (N1);
16175 end if;
16177 if Present (Assoc2) then
16178 Act2 := First (Assoc2);
16179 else
16180 return;
16181 end if;
16183 while Present (Act1) and then Present (Act2) loop
16184 Next (Act1);
16185 Next (Act2);
16186 end loop;
16188 -- Find the associations added for default subprograms
16190 if Present (Act2) then
16191 while Nkind (Act2) /= N_Generic_Association
16192 or else No (Entity (Selector_Name (Act2)))
16193 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16194 loop
16195 Next (Act2);
16196 end loop;
16198 -- Add a similar association if the default is global. The
16199 -- renaming declaration for the actual has been analyzed, and
16200 -- its alias is the program it renames. Link the actual in the
16201 -- original generic tree with the node in the analyzed tree.
16203 while Present (Act2) loop
16204 Subp := Entity (Selector_Name (Act2));
16205 Def := Explicit_Generic_Actual_Parameter (Act2);
16207 -- Following test is defence against rubbish errors
16209 if No (Alias (Subp)) then
16210 return;
16211 end if;
16213 -- Retrieve the resolved actual from the renaming declaration
16214 -- created for the instantiated formal.
16216 Actual := Entity (Name (Parent (Parent (Subp))));
16217 Set_Entity (Def, Actual);
16218 Set_Etype (Def, Etype (Actual));
16220 if Is_Global (Actual) then
16221 Ndec :=
16222 Make_Generic_Association (Loc,
16223 Selector_Name =>
16224 New_Occurrence_Of (Subp, Loc),
16225 Explicit_Generic_Actual_Parameter =>
16226 New_Occurrence_Of (Actual, Loc));
16228 Set_Associated_Node
16229 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16231 Append (Ndec, Assoc1);
16233 -- If there are other defaults, add a dummy association in case
16234 -- there are other defaulted formals with the same name.
16236 elsif Present (Next (Act2)) then
16237 Ndec :=
16238 Make_Generic_Association (Loc,
16239 Selector_Name =>
16240 New_Occurrence_Of (Subp, Loc),
16241 Explicit_Generic_Actual_Parameter => Empty);
16243 Append (Ndec, Assoc1);
16244 end if;
16246 Next (Act2);
16247 end loop;
16248 end if;
16250 if Nkind (Name (N1)) = N_Identifier
16251 and then Is_Child_Unit (Gen_Id)
16252 and then Is_Global (Gen_Id)
16253 and then Is_Generic_Unit (Scope (Gen_Id))
16254 and then In_Open_Scopes (Scope (Gen_Id))
16255 then
16256 -- This is an instantiation of a child unit within a sibling, so
16257 -- that the generic parent is in scope. An eventual instance must
16258 -- occur within the scope of an instance of the parent. Make name
16259 -- in instance into an expanded name, to preserve the identifier
16260 -- of the parent, so it can be resolved subsequently.
16262 Rewrite (Name (N2),
16263 Make_Expanded_Name (Loc,
16264 Chars => Chars (Gen_Id),
16265 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16266 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16267 Set_Entity (Name (N2), Gen_Id);
16269 Rewrite (Name (N1),
16270 Make_Expanded_Name (Loc,
16271 Chars => Chars (Gen_Id),
16272 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16273 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16275 Set_Associated_Node (Name (N1), Name (N2));
16276 Set_Associated_Node (Prefix (Name (N1)), Empty);
16277 Set_Associated_Node
16278 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16279 Set_Etype (Name (N1), Etype (Gen_Id));
16280 end if;
16281 end Save_Global_Defaults;
16283 ----------------------------
16284 -- Save_Global_Descendant --
16285 ----------------------------
16287 procedure Save_Global_Descendant (D : Union_Id) is
16288 N1 : Node_Id;
16290 begin
16291 if D in Node_Range then
16292 if D = Union_Id (Empty) then
16293 null;
16295 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16296 Save_References (Node_Id (D));
16297 end if;
16299 elsif D in List_Range then
16300 pragma Assert (D /= Union_Id (No_List));
16301 -- Because No_List = Empty, which is in Node_Range above
16303 N1 := First (List_Id (D));
16304 while Present (N1) loop
16305 Save_References (N1);
16306 Next (N1);
16307 end loop;
16309 -- Element list or other non-node field, nothing to do
16311 else
16312 null;
16313 end if;
16314 end Save_Global_Descendant;
16316 ---------------------
16317 -- Save_References --
16318 ---------------------
16320 -- This is the recursive procedure that does the work once the enclosing
16321 -- generic scope has been established. We have to treat specially a
16322 -- number of node rewritings that are required by semantic processing
16323 -- and which change the kind of nodes in the generic copy: typically
16324 -- constant-folding, replacing an operator node by a string literal, or
16325 -- a selected component by an expanded name. In each of those cases, the
16326 -- transformation is propagated to the generic unit.
16328 procedure Save_References (N : Node_Id) is
16329 Loc : constant Source_Ptr := Sloc (N);
16331 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16332 -- Determine whether arbitrary node Nod requires delayed capture of
16333 -- global references within its aspect specifications.
16335 procedure Save_References_In_Aggregate (N : Node_Id);
16336 -- Save all global references in [extension] aggregate node N
16338 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16339 -- Save all global references in a character literal or operator
16340 -- symbol denoted by N.
16342 procedure Save_References_In_Descendants (N : Node_Id);
16343 -- Save all global references in all descendants of node N
16345 procedure Save_References_In_Identifier (N : Node_Id);
16346 -- Save all global references in identifier node N
16348 procedure Save_References_In_Operator (N : Node_Id);
16349 -- Save all global references in operator node N
16351 procedure Save_References_In_Pragma (Prag : Node_Id);
16352 -- Save all global references found within the expression of pragma
16353 -- Prag.
16355 ---------------------------
16356 -- Requires_Delayed_Save --
16357 ---------------------------
16359 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16360 begin
16361 -- Generic packages and subprograms require delayed capture of
16362 -- global references within their aspects due to the timing of
16363 -- annotation analysis.
16365 if Nkind (Nod) in N_Generic_Package_Declaration
16366 | N_Generic_Subprogram_Declaration
16367 | N_Package_Body
16368 | N_Package_Body_Stub
16369 | N_Subprogram_Body
16370 | N_Subprogram_Body_Stub
16371 then
16372 -- Since the capture of global references is done on the
16373 -- unanalyzed generic template, there is no information around
16374 -- to infer the context. Use the Associated_Entity linkages to
16375 -- peek into the analyzed generic copy and determine what the
16376 -- template corresponds to.
16378 if Nod = Templ then
16379 return
16380 Is_Generic_Declaration_Or_Body
16381 (Unit_Declaration_Node
16382 (Associated_Entity (Defining_Entity (Nod))));
16384 -- Otherwise the generic unit being processed is not the top
16385 -- level template. It is safe to capture of global references
16386 -- within the generic unit because at this point the top level
16387 -- copy is fully analyzed.
16389 else
16390 return False;
16391 end if;
16393 -- Otherwise capture the global references without interference
16395 else
16396 return False;
16397 end if;
16398 end Requires_Delayed_Save;
16400 ----------------------------------
16401 -- Save_References_In_Aggregate --
16402 ----------------------------------
16404 procedure Save_References_In_Aggregate (N : Node_Id) is
16405 Nam : Node_Id;
16406 Qual : Node_Id := Empty;
16407 Typ : Entity_Id := Empty;
16409 begin
16410 N2 := Get_Associated_Node (N);
16412 if Present (N2) then
16413 Typ := Etype (N2);
16415 -- In an instance within a generic, use the name of the actual
16416 -- and not the original generic parameter. If the actual is
16417 -- global in the current generic it must be preserved for its
16418 -- instantiation.
16420 if Parent_Kind (Typ) = N_Subtype_Declaration
16421 and then Present (Generic_Parent_Type (Parent (Typ)))
16422 then
16423 Typ := Base_Type (Typ);
16424 Set_Etype (N2, Typ);
16425 end if;
16426 end if;
16428 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16429 Set_Associated_Node (N, Empty);
16431 -- If the aggregate is an actual in a call, it has been
16432 -- resolved in the current context, to some local type. The
16433 -- enclosing call may have been disambiguated by the aggregate,
16434 -- and this disambiguation might fail at instantiation time
16435 -- because the type to which the aggregate did resolve is not
16436 -- preserved. In order to preserve some of this information,
16437 -- wrap the aggregate in a qualified expression, using the id
16438 -- of its type. For further disambiguation we qualify the type
16439 -- name with its scope (if visible and not hidden by a local
16440 -- homograph) because both id's will have corresponding
16441 -- entities in an instance. This resolves most of the problems
16442 -- with missing type information on aggregates in instances.
16444 if Present (N2)
16445 and then Nkind (N2) = Nkind (N)
16446 and then Nkind (Parent (N2)) in N_Subprogram_Call
16447 and then Present (Typ)
16448 and then Comes_From_Source (Typ)
16449 then
16450 Nam := Make_Identifier (Loc, Chars (Typ));
16452 if Is_Immediately_Visible (Scope (Typ))
16453 and then
16454 (not In_Open_Scopes (Scope (Typ))
16455 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16456 then
16457 Nam :=
16458 Make_Selected_Component (Loc,
16459 Prefix =>
16460 Make_Identifier (Loc, Chars (Scope (Typ))),
16461 Selector_Name => Nam);
16462 end if;
16464 Qual :=
16465 Make_Qualified_Expression (Loc,
16466 Subtype_Mark => Nam,
16467 Expression => Relocate_Node (N));
16468 end if;
16469 end if;
16471 if Nkind (N) = N_Aggregate then
16472 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16474 elsif Nkind (N) = N_Extension_Aggregate then
16475 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16477 else
16478 pragma Assert (False);
16479 end if;
16481 Save_Global_Descendant (Union_Id (Expressions (N)));
16482 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16483 Save_Global_Descendant (Union_Id (Etype (N)));
16485 if Present (Qual) then
16486 Rewrite (N, Qual);
16487 end if;
16488 end Save_References_In_Aggregate;
16490 ----------------------------------------------
16491 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16492 ----------------------------------------------
16494 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16495 begin
16496 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16497 Reset_Entity (N);
16499 elsif Nkind (N) = N_Operator_Symbol
16500 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16501 then
16502 Change_Operator_Symbol_To_String_Literal (N);
16503 end if;
16504 end Save_References_In_Char_Lit_Or_Op_Symbol;
16506 ------------------------------------
16507 -- Save_References_In_Descendants --
16508 ------------------------------------
16510 procedure Save_References_In_Descendants (N : Node_Id) is
16511 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16512 begin
16513 Walk (N);
16514 end Save_References_In_Descendants;
16516 -----------------------------------
16517 -- Save_References_In_Identifier --
16518 -----------------------------------
16520 procedure Save_References_In_Identifier (N : Node_Id) is
16521 begin
16522 -- The node did not undergo a transformation
16524 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16525 -- If this is a discriminant reference, always save it.
16526 -- It is used in the instance to find the corresponding
16527 -- discriminant positionally rather than by name.
16529 Set_Original_Discriminant
16530 (N, Original_Discriminant (Get_Associated_Node (N)));
16532 Reset_Entity (N);
16534 -- The analysis of the generic copy transformed the identifier
16535 -- into another construct. Propagate the changes to the template.
16537 else
16538 N2 := Get_Associated_Node (N);
16540 -- The identifier denotes a call to a parameterless function.
16541 -- Mark the node as resolved when the function is external.
16543 if Nkind (N2) = N_Function_Call then
16544 E := Entity (Name (N2));
16546 if Present (E) and then Is_Global (E) then
16547 Set_Global_Type (N, N2);
16548 else
16549 Set_Associated_Node (N, Empty);
16550 Set_Etype (N, Empty);
16551 end if;
16553 -- The identifier denotes a named number that was constant
16554 -- folded. Preserve the original name for ASIS and undo the
16555 -- constant folding which will be repeated in the instance.
16556 -- Is this still needed???
16558 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16559 and then Is_Entity_Name (Original_Node (N2))
16560 then
16561 Set_Associated_Node (N, Original_Node (N2));
16562 Reset_Entity (N);
16564 -- The identifier resolved to a string literal. Propagate this
16565 -- information to the generic template.
16567 elsif Nkind (N2) = N_String_Literal then
16568 Rewrite (N, New_Copy (N2));
16570 -- The identifier is rewritten as a dereference if it is the
16571 -- prefix of an implicit dereference. Preserve the original
16572 -- tree as the analysis of the instance will expand the node
16573 -- again, but preserve the resolved entity if it is global.
16575 elsif Nkind (N2) = N_Explicit_Dereference then
16576 if Is_Entity_Name (Prefix (N2))
16577 and then Present (Entity (Prefix (N2)))
16578 and then Is_Global (Entity (Prefix (N2)))
16579 then
16580 Set_Associated_Node (N, Prefix (N2));
16581 Set_Global_Type (N, Prefix (N2));
16583 elsif Nkind (Prefix (N2)) = N_Function_Call
16584 and then Is_Entity_Name (Name (Prefix (N2)))
16585 and then Present (Entity (Name (Prefix (N2))))
16586 and then Is_Global (Entity (Name (Prefix (N2))))
16587 then
16588 Rewrite (N,
16589 Make_Explicit_Dereference (Loc,
16590 Prefix =>
16591 Make_Function_Call (Loc,
16592 Name =>
16593 New_Occurrence_Of
16594 (Entity (Name (Prefix (N2))), Loc))));
16595 Set_Associated_Node
16596 (Name (Prefix (N)), Name (Prefix (N2)));
16597 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
16599 else
16600 Set_Associated_Node (N, Empty);
16601 Set_Etype (N, Empty);
16602 end if;
16604 -- The subtype mark of a nominally unconstrained object is
16605 -- rewritten as a subtype indication using the bounds of the
16606 -- expression. Recover the original subtype mark.
16608 elsif Nkind (N2) = N_Subtype_Indication
16609 and then Is_Entity_Name (Original_Node (N2))
16610 then
16611 Set_Associated_Node (N, Original_Node (N2));
16612 Reset_Entity (N);
16613 end if;
16614 end if;
16615 end Save_References_In_Identifier;
16617 ---------------------------------
16618 -- Save_References_In_Operator --
16619 ---------------------------------
16621 procedure Save_References_In_Operator (N : Node_Id) is
16622 begin
16623 N2 := Get_Associated_Node (N);
16625 -- The node did not undergo a transformation
16627 if Nkind (N) = Nkind (N2) then
16628 if Nkind (N) = N_Op_Concat then
16629 Set_Is_Component_Left_Opnd
16630 (N, Is_Component_Left_Opnd (N2));
16631 Set_Is_Component_Right_Opnd
16632 (N, Is_Component_Right_Opnd (N2));
16633 end if;
16635 Reset_Entity (N);
16637 -- The analysis of the generic copy transformed the operator into
16638 -- some other construct. Propagate the changes to the template if
16639 -- applicable.
16641 else
16642 -- The operator resoved to a function call
16644 if Nkind (N2) = N_Function_Call then
16646 -- Add explicit qualifications in the generic template for
16647 -- all operands of universal type. This aids resolution by
16648 -- preserving the actual type of a literal or an attribute
16649 -- that yields a universal result.
16651 Qualify_Universal_Operands (N, N2);
16653 E := Entity (Name (N2));
16655 if Present (E) and then Is_Global (E) then
16656 Set_Global_Type (N, N2);
16657 else
16658 Set_Associated_Node (N, Empty);
16659 Set_Etype (N, Empty);
16660 end if;
16662 -- The operator was folded into a literal
16664 elsif Nkind (N2) in N_Integer_Literal
16665 | N_Real_Literal
16666 | N_String_Literal
16667 then
16668 if Present (Original_Node (N2))
16669 and then Nkind (Original_Node (N2)) = Nkind (N)
16670 then
16671 -- Operation was constant-folded. Whenever possible,
16672 -- recover semantic information from unfolded node.
16673 -- This was initially done for ASIS but is apparently
16674 -- needed also for e.g. compiling a-nbnbin.adb.
16676 Set_Associated_Node (N, Original_Node (N2));
16678 if Nkind (N) = N_Op_Concat then
16679 Set_Is_Component_Left_Opnd (N,
16680 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16681 Set_Is_Component_Right_Opnd (N,
16682 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16683 end if;
16685 Reset_Entity (N);
16687 -- Propagate the constant folding back to the template
16689 else
16690 Rewrite (N, New_Copy (N2));
16691 Set_Analyzed (N, False);
16692 end if;
16694 -- The operator was folded into an enumeration literal. Retain
16695 -- the entity to avoid spurious ambiguities if it is overloaded
16696 -- at the point of instantiation or inlining.
16698 elsif Nkind (N2) = N_Identifier
16699 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16700 then
16701 Rewrite (N, New_Copy (N2));
16702 Set_Analyzed (N, False);
16703 end if;
16704 end if;
16706 -- Complete the operands check if node has not been constant
16707 -- folded.
16709 if Nkind (N) in N_Op then
16710 Save_Entity_Descendants (N);
16711 end if;
16712 end Save_References_In_Operator;
16714 -------------------------------
16715 -- Save_References_In_Pragma --
16716 -------------------------------
16718 procedure Save_References_In_Pragma (Prag : Node_Id) is
16719 Context : Node_Id;
16720 Do_Save : Boolean := True;
16722 begin
16723 -- Do not save global references in pragmas generated from aspects
16724 -- because the pragmas will be regenerated at instantiation time.
16726 if From_Aspect_Specification (Prag) then
16727 Do_Save := False;
16729 -- The capture of global references within contract-related source
16730 -- pragmas associated with generic packages, subprograms or their
16731 -- respective bodies must be delayed due to timing of annotation
16732 -- analysis. Global references are still captured in routine
16733 -- Save_Global_References_In_Contract.
16735 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16736 if Is_Package_Contract_Annotation (Prag) then
16737 Context := Find_Related_Package_Or_Body (Prag);
16738 else
16739 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16740 Context := Find_Related_Declaration_Or_Body (Prag);
16741 end if;
16743 -- The use of Original_Node accounts for the case when the
16744 -- related context is generic template.
16746 if Requires_Delayed_Save (Original_Node (Context)) then
16747 Do_Save := False;
16748 end if;
16749 end if;
16751 -- For all other cases, save all global references within the
16752 -- descendants, but skip the following semantic fields:
16753 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16755 if Do_Save then
16756 Save_Global_Descendant
16757 (Union_Id (Pragma_Argument_Associations (N)));
16758 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16759 end if;
16760 end Save_References_In_Pragma;
16762 -- Start of processing for Save_References
16764 begin
16765 if N = Empty then
16766 null;
16768 -- Aggregates
16770 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16771 Save_References_In_Aggregate (N);
16773 -- Character literals, operator symbols
16775 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16776 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16778 -- Defining identifiers
16780 elsif Nkind (N) in N_Entity then
16781 null;
16783 -- Identifiers
16785 elsif Nkind (N) = N_Identifier then
16786 Save_References_In_Identifier (N);
16788 -- Operators
16790 elsif Nkind (N) in N_Op then
16791 Save_References_In_Operator (N);
16793 -- Pragmas
16795 elsif Nkind (N) = N_Pragma then
16796 Save_References_In_Pragma (N);
16798 else
16799 Save_References_In_Descendants (N);
16800 end if;
16802 -- Save all global references found within the aspect specifications
16803 -- of the related node.
16805 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16807 -- The capture of global references within aspects associated with
16808 -- generic packages, subprograms or their bodies must be delayed
16809 -- due to timing of annotation analysis. Global references are
16810 -- still captured in routine Save_Global_References_In_Contract.
16812 if Requires_Delayed_Save (N) then
16813 null;
16815 -- Otherwise save all global references within the aspects
16817 else
16818 Save_Global_References_In_Aspects (N);
16819 end if;
16820 end if;
16821 end Save_References;
16823 ---------------------
16824 -- Set_Global_Type --
16825 ---------------------
16827 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
16828 Typ : constant Entity_Id := Etype (N2);
16830 begin
16831 Set_Etype (N, Typ);
16833 -- If the entity of N is not the associated node, this is a
16834 -- nested generic and it has an associated node as well, whose
16835 -- type is already the full view (see below). Indicate that the
16836 -- original node has a private view.
16838 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
16839 Set_Has_Private_View (N);
16840 end if;
16842 -- If not a private type, nothing else to do
16844 if not Is_Private_Type (Typ) then
16845 null;
16847 -- If it is a derivation of a private type in a context where no
16848 -- full view is needed, nothing to do either.
16850 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
16851 null;
16853 -- Otherwise mark the type for flipping and use the full view when
16854 -- available.
16856 else
16857 Set_Has_Private_View (N);
16859 if Present (Full_View (Typ)) then
16860 Set_Etype (N2, Full_View (Typ));
16861 end if;
16862 end if;
16864 if Is_Floating_Point_Type (Typ)
16865 and then Has_Dimension_System (Typ)
16866 then
16867 Copy_Dimensions (N2, N);
16868 end if;
16869 end Set_Global_Type;
16871 -- Start of processing for Save_Global_References
16873 begin
16874 Gen_Scope := Current_Scope;
16876 -- If the generic unit is a child unit, references to entities in the
16877 -- parent are treated as local, because they will be resolved anew in
16878 -- the context of the instance of the parent.
16880 while Is_Child_Unit (Gen_Scope)
16881 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16882 loop
16883 Gen_Scope := Scope (Gen_Scope);
16884 end loop;
16886 Save_References (Templ);
16887 end Save_Global_References;
16889 ---------------------------------------
16890 -- Save_Global_References_In_Aspects --
16891 ---------------------------------------
16893 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16894 Asp : Node_Id;
16895 Expr : Node_Id;
16897 begin
16898 Asp := First (Aspect_Specifications (N));
16899 while Present (Asp) loop
16900 Expr := Expression (Asp);
16902 if Present (Expr) then
16903 Save_Global_References (Expr);
16904 end if;
16906 Next (Asp);
16907 end loop;
16908 end Save_Global_References_In_Aspects;
16910 ------------------------------------------
16911 -- Set_Copied_Sloc_For_Inherited_Pragma --
16912 ------------------------------------------
16914 procedure Set_Copied_Sloc_For_Inherited_Pragma
16915 (N : Node_Id;
16916 E : Entity_Id)
16918 begin
16919 Create_Instantiation_Source (N, E,
16920 Inlined_Body => False,
16921 Inherited_Pragma => True,
16922 Factor => S_Adjustment);
16923 end Set_Copied_Sloc_For_Inherited_Pragma;
16925 --------------------------------------
16926 -- Set_Copied_Sloc_For_Inlined_Body --
16927 --------------------------------------
16929 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16930 begin
16931 Create_Instantiation_Source (N, E,
16932 Inlined_Body => True,
16933 Inherited_Pragma => False,
16934 Factor => S_Adjustment);
16935 end Set_Copied_Sloc_For_Inlined_Body;
16937 ---------------------
16938 -- Set_Instance_Of --
16939 ---------------------
16941 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16942 begin
16943 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16944 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16945 Generic_Renamings.Increment_Last;
16946 end Set_Instance_Of;
16948 --------------------
16949 -- Set_Next_Assoc --
16950 --------------------
16952 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16953 begin
16954 Generic_Renamings.Table (E).Next_In_HTable := Next;
16955 end Set_Next_Assoc;
16957 -------------------
16958 -- Start_Generic --
16959 -------------------
16961 procedure Start_Generic is
16962 begin
16963 -- ??? More things could be factored out in this routine.
16964 -- Should probably be done at a later stage.
16966 Generic_Flags.Append (Inside_A_Generic);
16967 Inside_A_Generic := True;
16969 Expander_Mode_Save_And_Set (False);
16970 end Start_Generic;
16972 ----------------------
16973 -- Set_Instance_Env --
16974 ----------------------
16976 -- WARNING: This routine manages SPARK regions
16978 procedure Set_Instance_Env
16979 (Gen_Unit : Entity_Id;
16980 Act_Unit : Entity_Id)
16982 Saved_AE : constant Boolean := Assertions_Enabled;
16983 Saved_CPL : constant Node_Id := Check_Policy_List;
16984 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16985 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16986 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16988 begin
16989 -- Regardless of the current mode, predefined units are analyzed in the
16990 -- most current Ada mode, and earlier version Ada checks do not apply
16991 -- to predefined units. Nothing needs to be done for non-internal units.
16992 -- These are always analyzed in the current mode.
16994 if In_Internal_Unit (Gen_Unit) then
16996 -- The following call resets all configuration attributes to default
16997 -- or the xxx_Config versions of the attributes when the current sem
16998 -- unit is the main unit. At the same time, internal units must also
16999 -- inherit certain configuration attributes from their context. It
17000 -- is unclear what these two sets are.
17002 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17004 -- Reinstall relevant configuration attributes of the context
17006 Assertions_Enabled := Saved_AE;
17007 Check_Policy_List := Saved_CPL;
17008 Dynamic_Elaboration_Checks := Saved_DEC;
17010 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17011 end if;
17013 Current_Instantiated_Parent :=
17014 (Gen_Id => Gen_Unit,
17015 Act_Id => Act_Unit,
17016 Next_In_HTable => Assoc_Null);
17017 end Set_Instance_Env;
17019 -----------------
17020 -- Switch_View --
17021 -----------------
17023 procedure Switch_View (T : Entity_Id) is
17024 BT : constant Entity_Id := Base_Type (T);
17025 Priv_Elmt : Elmt_Id := No_Elmt;
17026 Priv_Sub : Entity_Id;
17028 begin
17029 -- T may be private but its base type may have been exchanged through
17030 -- some other occurrence, in which case there is nothing to switch
17031 -- besides T itself. Note that a private dependent subtype of a private
17032 -- type might not have been switched even if the base type has been,
17033 -- because of the last branch of Check_Private_View (see comment there).
17035 if not Is_Private_Type (BT) then
17036 Prepend_Elmt (Full_View (T), Exchanged_Views);
17037 Exchange_Declarations (T);
17038 return;
17039 end if;
17041 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17043 if Present (Full_View (BT)) then
17044 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17045 Exchange_Declarations (BT);
17046 end if;
17048 while Present (Priv_Elmt) loop
17049 Priv_Sub := Node (Priv_Elmt);
17051 if Present (Full_View (Priv_Sub)) then
17052 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17053 Exchange_Declarations (Priv_Sub);
17054 end if;
17056 Next_Elmt (Priv_Elmt);
17057 end loop;
17058 end Switch_View;
17060 -----------------
17061 -- True_Parent --
17062 -----------------
17064 function True_Parent (N : Node_Id) return Node_Id is
17065 begin
17066 if Nkind (Parent (N)) = N_Subunit then
17067 return Parent (Corresponding_Stub (Parent (N)));
17068 else
17069 return Parent (N);
17070 end if;
17071 end True_Parent;
17073 -----------------------------
17074 -- Valid_Default_Attribute --
17075 -----------------------------
17077 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17078 Attr_Id : constant Attribute_Id :=
17079 Get_Attribute_Id (Attribute_Name (Def));
17080 T : constant Entity_Id := Entity (Prefix (Def));
17081 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17082 F : Entity_Id;
17083 Num_F : Nat;
17084 OK : Boolean;
17086 begin
17087 if No (T) or else T = Any_Id then
17088 return;
17089 end if;
17091 Num_F := 0;
17092 F := First_Formal (Nam);
17093 while Present (F) loop
17094 Num_F := Num_F + 1;
17095 Next_Formal (F);
17096 end loop;
17098 case Attr_Id is
17099 when Attribute_Adjacent
17100 | Attribute_Ceiling
17101 | Attribute_Copy_Sign
17102 | Attribute_Floor
17103 | Attribute_Fraction
17104 | Attribute_Machine
17105 | Attribute_Model
17106 | Attribute_Remainder
17107 | Attribute_Rounding
17108 | Attribute_Unbiased_Rounding
17110 OK := Is_Fun
17111 and then Num_F = 1
17112 and then Is_Floating_Point_Type (T);
17114 when Attribute_Image
17115 | Attribute_Pred
17116 | Attribute_Succ
17117 | Attribute_Value
17118 | Attribute_Wide_Image
17119 | Attribute_Wide_Value
17121 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17123 when Attribute_Max
17124 | Attribute_Min
17126 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17128 when Attribute_Input =>
17129 OK := (Is_Fun and then Num_F = 1);
17131 when Attribute_Output
17132 | Attribute_Put_Image
17133 | Attribute_Read
17134 | Attribute_Write
17136 OK := not Is_Fun and then Num_F = 2;
17138 when others =>
17139 OK := False;
17140 end case;
17142 if not OK then
17143 Error_Msg_N
17144 ("attribute reference has wrong profile for subprogram", Def);
17145 end if;
17146 end Valid_Default_Attribute;
17148 ----------------------------------
17149 -- Validate_Formal_Type_Default --
17150 ----------------------------------
17152 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17153 Default : constant Node_Id :=
17154 Default_Subtype_Mark (Original_Node (Decl));
17155 Formal : constant Entity_Id := Defining_Identifier (Decl);
17157 Def_Sub : Entity_Id; -- Default subtype mark
17158 Type_Def : Node_Id;
17160 procedure Check_Discriminated_Formal;
17161 -- Check that discriminants of default for private or incomplete
17162 -- type match those of formal type.
17164 function Reference_Formal (N : Node_Id) return Traverse_Result;
17165 -- Check whether formal type definition mentions a previous formal
17166 -- type of the same generic.
17168 ----------------------
17169 -- Reference_Formal --
17170 ----------------------
17172 function Reference_Formal (N : Node_Id) return Traverse_Result is
17173 begin
17174 if Is_Entity_Name (N)
17175 and then Scope (Entity (N)) = Current_Scope
17176 then
17177 return Abandon;
17178 else
17179 return OK;
17180 end if;
17181 end Reference_Formal;
17183 function Depends_On_Other_Formals is
17184 new Traverse_Func (Reference_Formal);
17186 function Default_Subtype_Matches
17187 (Gen_T, Def_T : Entity_Id) return Boolean;
17189 procedure Validate_Array_Type_Default;
17190 -- Verify that dimension, indices, and component types of default
17191 -- are compatible with formal array type definition.
17193 procedure Validate_Derived_Type_Default;
17194 -- Verify that ancestor and progenitor types match.
17196 ---------------------------------
17197 -- Check_Discriminated_Formal --
17198 ---------------------------------
17200 procedure Check_Discriminated_Formal is
17201 Formal_Discr : Entity_Id;
17202 Actual_Discr : Entity_Id;
17203 Formal_Subt : Entity_Id;
17205 begin
17206 if Has_Discriminants (Formal) then
17207 if not Has_Discriminants (Def_Sub) then
17208 Error_Msg_NE
17209 ("default for & must have discriminants", Default, Formal);
17211 elsif Is_Constrained (Def_Sub) then
17212 Error_Msg_NE
17213 ("default for & must be unconstrained", Default, Formal);
17215 else
17216 Formal_Discr := First_Discriminant (Formal);
17217 Actual_Discr := First_Discriminant (Def_Sub);
17218 while Formal_Discr /= Empty loop
17219 if Actual_Discr = Empty then
17220 Error_Msg_N
17221 ("discriminants on Formal do not match formal",
17222 Default);
17223 end if;
17225 Formal_Subt := Etype (Formal_Discr);
17227 -- Access discriminants match if designated types do
17229 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17230 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17231 E_Anonymous_Access_Type
17232 and then
17233 Designated_Type (Base_Type (Formal_Subt)) =
17234 Designated_Type (Base_Type (Etype (Actual_Discr)))
17235 then
17236 null;
17238 elsif Base_Type (Formal_Subt) /=
17239 Base_Type (Etype (Actual_Discr))
17240 then
17241 Error_Msg_N
17242 ("types of discriminants of default must match formal",
17243 Default);
17245 elsif not Subtypes_Statically_Match
17246 (Formal_Subt, Etype (Actual_Discr))
17247 and then Ada_Version >= Ada_95
17248 then
17249 Error_Msg_N
17250 ("subtypes of discriminants of default "
17251 & "must match formal",
17252 Default);
17253 end if;
17255 Next_Discriminant (Formal_Discr);
17256 Next_Discriminant (Actual_Discr);
17257 end loop;
17259 if Actual_Discr /= Empty then
17260 Error_Msg_NE
17261 ("discriminants on default do not match formal",
17262 Default, Formal);
17263 end if;
17264 end if;
17265 end if;
17266 end Check_Discriminated_Formal;
17268 ---------------------------
17269 -- Default_Subtype_Matches --
17270 ---------------------------
17272 function Default_Subtype_Matches
17273 (Gen_T, Def_T : Entity_Id) return Boolean
17275 begin
17276 -- Check that the base types, root types (when dealing with class
17277 -- wide types), or designated types (when dealing with anonymous
17278 -- access types) of Gen_T and Def_T are statically matching subtypes.
17280 return (Base_Type (Gen_T) = Base_Type (Def_T)
17281 and then Subtypes_Statically_Match (Gen_T, Def_T))
17283 or else (Is_Class_Wide_Type (Gen_T)
17284 and then Is_Class_Wide_Type (Def_T)
17285 and then Default_Subtype_Matches
17286 (Root_Type (Gen_T), Root_Type (Def_T)))
17288 or else (Is_Anonymous_Access_Type (Gen_T)
17289 and then Ekind (Def_T) = Ekind (Gen_T)
17290 and then Subtypes_Statically_Match
17291 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17293 end Default_Subtype_Matches;
17295 ----------------------------------
17296 -- Validate_Array_Type_Default --
17297 ----------------------------------
17299 procedure Validate_Array_Type_Default is
17300 I1, I2 : Node_Id;
17301 T2 : Entity_Id;
17302 begin
17303 if not Is_Array_Type (Def_Sub) then
17304 Error_Msg_NE ("default for& must be an array type ",
17305 Default, Formal);
17306 return;
17308 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17309 or else Is_Constrained (Def_Sub) /=
17310 Is_Constrained (Formal)
17311 then
17312 Error_Msg_NE ("default array type does not match&",
17313 Default, Formal);
17314 return;
17315 end if;
17317 I1 := First_Index (Formal);
17318 I2 := First_Index (Def_Sub);
17319 for J in 1 .. Number_Dimensions (Formal) loop
17321 -- If the indexes of the actual were given by a subtype_mark,
17322 -- the index was transformed into a range attribute. Retrieve
17323 -- the original type mark for checking.
17325 if Is_Entity_Name (Original_Node (I2)) then
17326 T2 := Entity (Original_Node (I2));
17327 else
17328 T2 := Etype (I2);
17329 end if;
17331 if not Subtypes_Statically_Match (Etype (I1), T2) then
17332 Error_Msg_NE
17333 ("index types of default do not match those of formal &",
17334 Default, Formal);
17335 end if;
17337 Next_Index (I1);
17338 Next_Index (I2);
17339 end loop;
17341 if not Default_Subtype_Matches
17342 (Component_Type (Formal), Component_Type (Def_Sub))
17343 then
17344 Error_Msg_NE
17345 ("component subtype of default does not match that of formal &",
17346 Default, Formal);
17347 end if;
17349 if Has_Aliased_Components (Formal)
17350 and then not Has_Aliased_Components (Default)
17351 then
17352 Error_Msg_NE
17353 ("default must have aliased components to match formal type &",
17354 Default, Formal);
17355 end if;
17356 end Validate_Array_Type_Default;
17358 -----------------------------------
17359 -- Validate_Derived_Type_Default --
17360 -----------------------------------
17362 procedure Validate_Derived_Type_Default is
17363 begin
17364 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17365 Error_Msg_NE ("default must be a descendent of&",
17366 Default, Etype (Formal));
17367 end if;
17369 if Has_Interfaces (Formal) then
17370 if not Has_Interfaces (Def_Sub) then
17371 Error_Msg_NE
17372 ("default must implement all interfaces of formal&",
17373 Default, Formal);
17375 else
17376 declare
17377 Iface : Node_Id;
17378 Iface_Ent : Entity_Id;
17380 begin
17381 Iface := First (Abstract_Interface_List (Formal));
17383 while Present (Iface) loop
17384 Iface_Ent := Entity (Iface);
17386 if Is_Ancestor (Iface_Ent, Def_Sub)
17387 or else Is_Progenitor (Iface_Ent, Def_Sub)
17388 then
17389 null;
17391 else
17392 Error_Msg_NE
17393 ("Default must implement interface&",
17394 Default, Etype (Iface));
17395 end if;
17397 Next (Iface);
17398 end loop;
17399 end;
17400 end if;
17401 end if;
17402 end Validate_Derived_Type_Default;
17404 -- Start of processing for Validate_Formal_Type_Default
17406 begin
17407 Analyze (Default);
17408 if not Is_Entity_Name (Default)
17409 or else not Is_Type (Entity (Default))
17410 then
17411 Error_Msg_N
17412 ("Expect type name for default of formal type", Default);
17413 return;
17414 else
17415 Def_Sub := Entity (Default);
17416 end if;
17418 -- Formal derived_type declarations are transformed into full
17419 -- type declarations or Private_Type_Extensions for ease of processing.
17421 if Nkind (Decl) = N_Full_Type_Declaration then
17422 Type_Def := Type_Definition (Decl);
17424 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17425 Type_Def := Subtype_Indication (Decl);
17427 else
17428 Type_Def := Formal_Type_Definition (Decl);
17429 end if;
17431 if Depends_On_Other_Formals (Type_Def) = Abandon
17432 and then Scope (Def_Sub) /= Current_Scope
17433 then
17434 Error_Msg_N ("default of formal type that depends on "
17435 & "other formals must be a previous formal type", Default);
17436 return;
17438 elsif Def_Sub = Formal then
17439 Error_Msg_N
17440 ("default for formal type cannot be formal itsef", Default);
17441 return;
17442 end if;
17444 case Nkind (Type_Def) is
17446 when N_Formal_Private_Type_Definition =>
17447 if (Is_Abstract_Type (Formal)
17448 and then not Is_Abstract_Type (Def_Sub))
17449 or else (Is_Limited_Type (Formal)
17450 and then not Is_Limited_Type (Def_Sub))
17451 then
17452 Error_Msg_NE
17453 ("default for private type$ does not match",
17454 Default, Formal);
17455 end if;
17457 Check_Discriminated_Formal;
17459 when N_Formal_Derived_Type_Definition =>
17460 Check_Discriminated_Formal;
17461 Validate_Derived_Type_Default;
17463 when N_Formal_Incomplete_Type_Definition =>
17464 if Is_Tagged_Type (Formal)
17465 and then not Is_Tagged_Type (Def_Sub)
17466 then
17467 Error_Msg_NE
17468 ("default for & must be a tagged type", Default, Formal);
17469 end if;
17471 Check_Discriminated_Formal;
17473 when N_Formal_Discrete_Type_Definition =>
17474 if not Is_Discrete_Type (Def_Sub) then
17475 Error_Msg_NE ("default for& must be a discrete type",
17476 Default, Formal);
17477 end if;
17479 when N_Formal_Signed_Integer_Type_Definition =>
17480 if not Is_Integer_Type (Def_Sub) then
17481 Error_Msg_NE ("default for& must be a discrete type",
17482 Default, Formal);
17483 end if;
17485 when N_Formal_Modular_Type_Definition =>
17486 if not Is_Modular_Integer_Type (Def_Sub) then
17487 Error_Msg_NE ("default for& must be a modular_integer Type",
17488 Default, Formal);
17489 end if;
17491 when N_Formal_Floating_Point_Definition =>
17492 if not Is_Floating_Point_Type (Def_Sub) then
17493 Error_Msg_NE ("default for& must be a floating_point type",
17494 Default, Formal);
17495 end if;
17497 when N_Formal_Ordinary_Fixed_Point_Definition =>
17498 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17499 Error_Msg_NE ("default for& must be "
17500 & "an ordinary_fixed_point type ",
17501 Default, Formal);
17502 end if;
17504 when N_Formal_Decimal_Fixed_Point_Definition =>
17505 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17506 Error_Msg_NE ("default for& must be "
17507 & "an Decimal_fixed_point type ",
17508 Default, Formal);
17509 end if;
17511 when N_Array_Type_Definition =>
17512 Validate_Array_Type_Default;
17514 when N_Access_Function_Definition |
17515 N_Access_Procedure_Definition =>
17516 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17517 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17518 Default, Formal);
17519 end if;
17520 Check_Subtype_Conformant
17521 (Designated_Type (Formal), Designated_Type (Def_Sub));
17523 when N_Access_To_Object_Definition =>
17524 if not Is_Access_Object_Type (Def_Sub) then
17525 Error_Msg_NE ("default for& must be an Access_To_Object",
17526 Default, Formal);
17528 elsif not Default_Subtype_Matches
17529 (Designated_Type (Formal), Designated_Type (Def_Sub))
17530 then
17531 Error_Msg_NE ("designated type of defaul does not match "
17532 & "designated type of formal type",
17533 Default, Formal);
17534 end if;
17536 when N_Record_Definition => -- Formal interface type
17537 if not Is_Interface (Def_Sub) then
17538 Error_Msg_NE
17539 ("default for formal interface type must be an interface",
17540 Default, Formal);
17542 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17543 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17544 or else Is_Protected_Interface (Formal) /=
17545 Is_Protected_Interface (Def_Sub)
17546 or else Is_Synchronized_Interface (Formal) /=
17547 Is_Synchronized_Interface (Def_Sub)
17548 then
17549 Error_Msg_NE
17550 ("default for interface& does not match", Def_Sub, Formal);
17551 end if;
17553 when N_Derived_Type_Definition =>
17554 Validate_Derived_Type_Default;
17556 when N_Identifier => -- case of a private extension
17557 Validate_Derived_Type_Default;
17559 when N_Error =>
17560 null;
17562 when others =>
17563 raise Program_Error;
17564 end case;
17565 end Validate_Formal_Type_Default;
17566 end Sem_Ch12;