analyzer: Restore g++ 4.8 bootstrap; use std::move to return std::unique_ptr.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob9919cda6340ccb5a59118a6aad23e44fe5819064
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-2024, 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 Component_Type_For_Private_View (T : Entity_Id) return Entity_Id;
586 -- Return the component type of array type T, with the following addition:
587 -- if this component type itself is an array type which has not been first
588 -- declared as private, then recurse on it. This makes it possible to deal
589 -- with arrays of arrays the same way as multi-dimensional arrays in the
590 -- mechanism handling private views.
592 function Contains_Instance_Of
593 (Inner : Entity_Id;
594 Outer : Entity_Id;
595 N : Node_Id) return Boolean;
596 -- Inner is instantiated within the generic Outer. Check whether Inner
597 -- directly or indirectly contains an instance of Outer or of one of its
598 -- parents, in the case of a subunit. Each generic unit holds a list of
599 -- the entities instantiated within (at any depth). This procedure
600 -- determines whether the set of such lists contains a cycle, i.e. an
601 -- illegal circular instantiation.
603 function Denotes_Formal_Package
604 (Pack : Entity_Id;
605 On_Exit : Boolean := False;
606 Instance : Entity_Id := Empty) return Boolean;
607 -- Returns True if E is a formal package of an enclosing generic, or
608 -- the actual for such a formal in an enclosing instantiation. If such
609 -- a package is used as a formal in an nested generic, or as an actual
610 -- in a nested instantiation, the visibility of ITS formals should not
611 -- be modified. When called from within Restore_Private_Views, the flag
612 -- On_Exit is true, to indicate that the search for a possible enclosing
613 -- instance should ignore the current one. In that case Instance denotes
614 -- the declaration for which this is an actual. This declaration may be
615 -- an instantiation in the source, or the internal instantiation that
616 -- corresponds to the actual for a formal package.
618 function Earlier (N1, N2 : Node_Id) return Boolean;
619 -- Yields True if N1 and N2 appear in the same compilation unit,
620 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
621 -- traversal of the tree for the unit. Used to determine the placement
622 -- of freeze nodes for instance bodies that may depend on other instances.
624 function Find_Actual_Type
625 (Typ : Entity_Id;
626 Gen_Type : Entity_Id) return Entity_Id;
627 -- When validating the actual types of a child instance, check whether
628 -- the formal is a formal type of the parent unit, and retrieve the current
629 -- actual for it. Typ is the entity in the analyzed formal type declaration
630 -- (component or index type of an array type, or designated type of an
631 -- access formal) and Gen_Type is the enclosing analyzed formal array
632 -- or access type. The desired actual may be a formal of a parent, or may
633 -- be declared in a formal package of a parent. In both cases it is a
634 -- generic actual type because it appears within a visible instance.
635 -- Finally, it may be declared in a parent unit without being a formal
636 -- of that unit, in which case it must be retrieved by visibility.
637 -- Ambiguities may still arise if two homonyms are declared in two formal
638 -- packages, and the prefix of the formal type may be needed to resolve
639 -- the ambiguity in the instance ???
641 procedure Freeze_Package_Instance
642 (N : Node_Id;
643 Gen_Body : Node_Id;
644 Gen_Decl : Node_Id;
645 Act_Id : Entity_Id);
646 -- If the instantiation happens textually before the body of the generic,
647 -- the instantiation of the body must be analyzed after the generic body,
648 -- and not at the point of instantiation. Such early instantiations can
649 -- happen if the generic and the instance appear in a package declaration
650 -- because the generic body can only appear in the corresponding package
651 -- body. Early instantiations can also appear if generic, instance and
652 -- body are all in the declarative part of a subprogram or entry. Entities
653 -- of packages that are early instantiations are delayed, and their freeze
654 -- node appears after the generic body. This rather complex machinery is
655 -- needed when nested instantiations are present, because the source does
656 -- not carry any indication of where the corresponding instance bodies must
657 -- be installed and frozen.
659 procedure Freeze_Subprogram_Instance
660 (N : Node_Id;
661 Gen_Body : Node_Id;
662 Pack_Id : Entity_Id);
663 -- The generic body may appear textually after the instance, including
664 -- in the proper body of a stub, or within a different package instance.
665 -- Given that the instance can only be elaborated after the generic, we
666 -- place freeze nodes for the instance and/or for packages that may enclose
667 -- the instance and the generic, so that the back-end can establish the
668 -- proper order of elaboration.
670 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id;
671 -- Similar to Get_Associated_Node below, but for entities
673 function Get_Associated_Node (N : Node_Id) return Node_Id;
674 -- In order to propagate semantic information back from the analyzed copy
675 -- to the original generic, we maintain links between selected nodes in the
676 -- generic and their corresponding copies. At the end of generic analysis,
677 -- the routine Save_Global_References traverses the generic tree, examines
678 -- the semantic information, and preserves the links to those nodes that
679 -- contain global information. At instantiation, the information from the
680 -- associated node is placed on the new copy, so that name resolution is
681 -- not repeated.
683 -- Three kinds of source nodes have associated nodes:
685 -- a) those that can reference (denote) entities, that is identifiers,
686 -- character literals, expanded_names, operator symbols, operators,
687 -- and attribute reference nodes. These nodes have an Entity field
688 -- and are the set of nodes that are in N_Has_Entity.
690 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
692 -- c) selected components (N_Selected_Component)
694 -- For the first class, the associated node preserves the entity if it is
695 -- global. If the generic contains nested instantiations, the associated
696 -- node itself has been recopied, and a chain of them must be followed.
698 -- For aggregates, the associated node allows retrieval of the type, which
699 -- may otherwise not appear in the generic. The view of this type may be
700 -- different between generic and instantiation, and the full view can be
701 -- installed before the instantiation is analyzed. For aggregates of type
702 -- extensions, the same view exchange may have to be performed for some of
703 -- the ancestor types, if their view is private at the point of
704 -- instantiation.
706 -- Nodes that are selected components in the parse tree may be rewritten
707 -- as expanded names after resolution, and must be treated as potential
708 -- entity holders, which is why they also have an Associated_Node.
710 -- Nodes that do not come from source, such as freeze nodes, do not appear
711 -- in the generic tree, and need not have an associated node.
713 -- The associated node is stored in the Associated_Node field. Note that
714 -- this field overlaps Entity, which is fine, because the whole point is
715 -- that we don't need or want the normal Entity field in this situation.
717 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
718 -- Traverse the Exchanged_Views list to see if a type was private
719 -- and has already been flipped during this phase of instantiation.
721 function Has_Contracts (Decl : Node_Id) return Boolean;
722 -- Determine whether a formal subprogram has a Pre- or Postcondition,
723 -- in which case a subprogram wrapper has to be built for the actual.
725 procedure Hide_Current_Scope;
726 -- When instantiating a generic child unit, the parent context must be
727 -- present, but the instance and all entities that may be generated
728 -- must be inserted in the current scope. We leave the current scope
729 -- on the stack, but make its entities invisible to avoid visibility
730 -- problems. This is reversed at the end of the instantiation. This is
731 -- not done for the instantiation of the bodies, which only require the
732 -- instances of the generic parents to be in scope.
734 function In_Main_Context (E : Entity_Id) return Boolean;
735 -- Check whether an instantiation is in the context of the main unit.
736 -- Used to determine whether its body should be elaborated to allow
737 -- front-end inlining.
739 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
740 -- Add the context clause of the unit containing a generic unit to a
741 -- compilation unit that is, or contains, an instantiation.
743 procedure Init_Env;
744 -- Establish environment for subsequent instantiation. Separated from
745 -- Save_Env because data-structures for visibility handling must be
746 -- initialized before call to Check_Generic_Child_Unit.
748 procedure Inline_Instance_Body
749 (N : Node_Id;
750 Gen_Unit : Entity_Id;
751 Act_Decl : Node_Id);
752 -- If front-end inlining is requested, instantiate the package body,
753 -- and preserve the visibility of its compilation unit, to insure
754 -- that successive instantiations succeed.
756 procedure Insert_Freeze_Node_For_Instance
757 (N : Node_Id;
758 F_Node : Node_Id);
759 -- N denotes a package or a subprogram instantiation and F_Node is the
760 -- associated freeze node. Insert the freeze node before the first source
761 -- body which follows immediately after N. If no such body is found, the
762 -- freeze node is inserted at the end of the declarative region which
763 -- contains N, unless the instantiation is done in a package spec that is
764 -- not at library level, in which case it is inserted at the outer level.
765 -- This can also be invoked to insert the freeze node of a package that
766 -- encloses an instantiation, in which case N may denote an arbitrary node.
768 procedure Install_Formal_Packages (Par : Entity_Id);
769 -- Install the visible part of any formal of the parent that is a formal
770 -- package. Note that for the case of a formal package with a box, this
771 -- includes the formal part of the formal package (12.7(10/2)).
773 procedure Install_Hidden_Primitives
774 (Prims_List : in out Elist_Id;
775 Gen_T : Entity_Id;
776 Act_T : Entity_Id);
777 -- Remove suffix 'P' from hidden primitives of Act_T to match the
778 -- visibility of primitives of Gen_T. The list of primitives to which
779 -- the suffix is removed is added to Prims_List to restore them later.
781 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
782 -- When compiling an instance of a child unit the parent (which is
783 -- itself an instance) is an enclosing scope that must be made
784 -- immediately visible. This procedure is also used to install the non-
785 -- generic parent of a generic child unit when compiling its body, so
786 -- that full views of types in the parent are made visible.
788 -- The functions Instantiate_XXX perform various legality checks and build
789 -- the declarations for instantiated generic parameters. In all of these
790 -- Formal is the entity in the generic unit, Actual is the entity of
791 -- expression in the generic associations, and Analyzed_Formal is the
792 -- formal in the generic copy, which contains the semantic information to
793 -- be used to validate the actual.
795 function Instantiate_Object
796 (Formal : Node_Id;
797 Actual : Node_Id;
798 Analyzed_Formal : Node_Id) return List_Id;
800 function Instantiate_Type
801 (Formal : Node_Id;
802 Actual : Node_Id;
803 Analyzed_Formal : Node_Id;
804 Actual_Decls : List_Id) return List_Id;
806 function Instantiate_Formal_Subprogram
807 (Formal : Node_Id;
808 Actual : Node_Id;
809 Analyzed_Formal : Node_Id) return Node_Id;
811 function Instantiate_Formal_Package
812 (Formal : Node_Id;
813 Actual : Node_Id;
814 Analyzed_Formal : Node_Id) return List_Id;
815 -- If the formal package is declared with a box, special visibility rules
816 -- apply to its formals: they are in the visible part of the package. This
817 -- is true in the declarative region of the formal package, that is to say
818 -- in the enclosing generic or instantiation. For an instantiation, the
819 -- parameters of the formal package are made visible in an explicit step.
820 -- Furthermore, if the actual has a visible USE clause, these formals must
821 -- be made potentially use-visible as well. On exit from the enclosing
822 -- instantiation, the reverse must be done.
824 -- For a formal package declared without a box, there are conformance rules
825 -- that apply to the actuals in the generic declaration and the actuals of
826 -- the actual package in the enclosing instantiation. The simplest way to
827 -- apply these rules is to repeat the instantiation of the formal package
828 -- in the context of the enclosing instance, and compare the generic
829 -- associations of this instantiation with those of the actual package.
830 -- This internal instantiation only needs to contain the renamings of the
831 -- formals: the visible and private declarations themselves need not be
832 -- created.
834 -- In Ada 2005, the formal package may be only partially parameterized.
835 -- In that case the visibility step must make visible those actuals whose
836 -- corresponding formals were given with a box. A final complication
837 -- involves inherited operations from formal derived types, which must
838 -- be visible if the type is.
840 function Is_In_Main_Unit (N : Node_Id) return Boolean;
841 -- Test if given node is in the main unit
843 procedure Load_Parent_Of_Generic
844 (N : Node_Id;
845 Spec : Node_Id;
846 Body_Optional : Boolean := False);
847 -- If the generic appears in a separate non-generic library unit, load the
848 -- corresponding body to retrieve the body of the generic. N is the node
849 -- for the generic instantiation, Spec is the generic package declaration.
851 -- Body_Optional is a flag that indicates that the body is being loaded to
852 -- ensure that temporaries are generated consistently when there are other
853 -- instances in the current declarative part that precede the one being
854 -- loaded. In that case a missing body is acceptable.
856 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
857 -- Within the generic part, entities in the formal package are
858 -- visible. To validate subsequent type declarations, indicate
859 -- the correspondence between the entities in the analyzed formal,
860 -- and the entities in the actual package. There are three packages
861 -- involved in the instantiation of a formal package: the parent
862 -- generic P1 which appears in the generic declaration, the fake
863 -- instantiation P2 which appears in the analyzed generic, and whose
864 -- visible entities may be used in subsequent formals, and the actual
865 -- P3 in the instance. To validate subsequent formals, me indicate
866 -- that the entities in P2 are mapped into those of P3. The mapping of
867 -- entities has to be done recursively for nested packages.
869 procedure Move_Freeze_Nodes
870 (Out_Of : Entity_Id;
871 After : Node_Id;
872 L : List_Id);
873 -- Freeze nodes can be generated in the analysis of a generic unit, but
874 -- will not be seen by the back-end. It is necessary to move those nodes
875 -- to the enclosing scope if they freeze an outer entity. We place them
876 -- at the end of the enclosing generic package, which is semantically
877 -- neutral.
879 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
880 -- Analyze actuals to perform name resolution. Full resolution is done
881 -- later, when the expected types are known, but names have to be captured
882 -- before installing parents of generics, that are not visible for the
883 -- actuals themselves.
885 -- If Inst is present, it is the entity of the package instance. This
886 -- entity is marked as having a limited_view actual when some actual is
887 -- a limited view. This is used to place the instance body properly.
889 procedure Provide_Completing_Bodies (N : Node_Id);
890 -- Generate completing bodies for all subprograms found within package or
891 -- subprogram declaration N.
893 procedure Remove_Parent (In_Body : Boolean := False);
894 -- Reverse effect after instantiation of child is complete
896 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
897 -- Determine whether the formal package declaration N requires conformance
898 -- checking with actuals in instantiations.
900 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
901 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
902 -- set to No_Elist.
904 procedure Set_Instance_Env
905 (Gen_Unit : Entity_Id;
906 Act_Unit : Entity_Id);
907 -- Save current instance on saved environment, to be used to determine
908 -- the global status of entities in nested instances. Part of Save_Env.
909 -- called after verifying that the generic unit is legal for the instance,
910 -- The procedure also examines whether the generic unit is a predefined
911 -- unit, in order to set configuration switches accordingly. As a result
912 -- the procedure must be called after analyzing and freezing the actuals.
914 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
915 -- Associate analyzed generic parameter with corresponding instance. Used
916 -- for semantic checks at instantiation time.
918 function True_Parent (N : Node_Id) return Node_Id;
919 -- For a subunit, return parent of corresponding stub, else return
920 -- parent of node.
922 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
923 -- Verify that an attribute that appears as the default for a formal
924 -- subprogram is a function or procedure with the correct profile.
926 procedure Validate_Formal_Type_Default (Decl : Node_Id);
927 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
928 -- that it is the name of a type in the same class as the formal.
929 -- The treatment parallels what is done in Instantiate_Type but differs
930 -- in a few ways so that this machinery cannot be reused as is: on one
931 -- hand there are no visibility issues for a default, because it is
932 -- analyzed in the same context as the formal type definition; on the
933 -- other hand the check needs to take into acount the use of a previous
934 -- formal type in the current formal type definition (see details in
935 -- AI12-0205).
937 -------------------------------------------
938 -- Data Structures for Generic Renamings --
939 -------------------------------------------
941 -- The map Generic_Renamings associates generic entities with their
942 -- corresponding actuals. Currently used to validate type instances. It
943 -- will eventually be used for all generic parameters to eliminate the
944 -- need for overload resolution in the instance.
946 type Assoc_Ptr is new Int;
948 Assoc_Null : constant Assoc_Ptr := -1;
950 type Assoc is record
951 Gen_Id : Entity_Id;
952 Act_Id : Entity_Id;
953 Next_In_HTable : Assoc_Ptr;
954 end record;
956 package Generic_Renamings is new Table.Table
957 (Table_Component_Type => Assoc,
958 Table_Index_Type => Assoc_Ptr,
959 Table_Low_Bound => 0,
960 Table_Initial => 10,
961 Table_Increment => 100,
962 Table_Name => "Generic_Renamings");
964 -- Variable to hold enclosing instantiation. When the environment is
965 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
967 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
969 -- Hash table for associations
971 HTable_Size : constant := 37;
972 type HTable_Range is range 0 .. HTable_Size - 1;
974 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
975 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
976 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
977 function Hash (F : Entity_Id) return HTable_Range;
979 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
980 Header_Num => HTable_Range,
981 Element => Assoc,
982 Elmt_Ptr => Assoc_Ptr,
983 Null_Ptr => Assoc_Null,
984 Set_Next => Set_Next_Assoc,
985 Next => Next_Assoc,
986 Key => Entity_Id,
987 Get_Key => Get_Gen_Id,
988 Hash => Hash,
989 Equal => "=");
991 Exchanged_Views : Elist_Id;
992 -- This list holds the private views that have been exchanged during
993 -- instantiation to restore the visibility of the generic declaration.
994 -- (see comments above). After instantiation, the current visibility is
995 -- reestablished by means of a traversal of this list.
997 Hidden_Entities : Elist_Id;
998 -- This list holds the entities of the current scope that are removed
999 -- from immediate visibility when instantiating a child unit. Their
1000 -- visibility is restored in Remove_Parent.
1002 -- Because instantiations can be recursive, the following must be saved
1003 -- on entry and restored on exit from an instantiation (spec or body).
1004 -- This is done by the two procedures Save_Env and Restore_Env. For
1005 -- package and subprogram instantiations (but not for the body instances)
1006 -- the action of Save_Env is done in two steps: Init_Env is called before
1007 -- Check_Generic_Child_Unit, because setting the parent instances requires
1008 -- that the visibility data structures be properly initialized. Once the
1009 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1011 Parent_Unit_Visible : Boolean := False;
1012 -- Parent_Unit_Visible is used when the generic is a child unit, and
1013 -- indicates whether the ultimate parent of the generic is visible in the
1014 -- instantiation environment. It is used to reset the visibility of the
1015 -- parent at the end of the instantiation (see Remove_Parent).
1017 Instance_Parent_Unit : Entity_Id := Empty;
1018 -- This records the ultimate parent unit of an instance of a generic
1019 -- child unit and is used in conjunction with Parent_Unit_Visible to
1020 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1022 type Instance_Env is record
1023 Instantiated_Parent : Assoc;
1024 Exchanged_Views : Elist_Id;
1025 Hidden_Entities : Elist_Id;
1026 Current_Sem_Unit : Unit_Number_Type;
1027 Parent_Unit_Visible : Boolean := False;
1028 Instance_Parent_Unit : Entity_Id := Empty;
1029 Switches : Config_Switches_Type;
1030 end record;
1032 package Instance_Envs is new Table.Table (
1033 Table_Component_Type => Instance_Env,
1034 Table_Index_Type => Int,
1035 Table_Low_Bound => 0,
1036 Table_Initial => 32,
1037 Table_Increment => 100,
1038 Table_Name => "Instance_Envs");
1040 procedure Restore_Private_Views
1041 (Pack_Id : Entity_Id;
1042 Is_Package : Boolean := True);
1043 -- Restore the private views of external types, and unmark the generic
1044 -- renamings of actuals, so that they become compatible subtypes again.
1045 -- For subprograms, Pack_Id is the package constructed to hold the
1046 -- renamings.
1048 procedure Switch_View (T : Entity_Id);
1049 -- Switch the partial and full views of a type and its private
1050 -- dependents (i.e. its subtypes and derived types).
1052 ------------------------------------
1053 -- Structures for Error Reporting --
1054 ------------------------------------
1056 Instantiation_Node : Node_Id;
1057 -- Used by subprograms that validate instantiation of formal parameters
1058 -- where there might be no actual on which to place the error message.
1059 -- Also used to locate the instantiation node for generic subunits.
1061 Instantiation_Error : exception;
1062 -- When there is a semantic error in the generic parameter matching,
1063 -- there is no point in continuing the instantiation, because the
1064 -- number of cascaded errors is unpredictable. This exception aborts
1065 -- the instantiation process altogether.
1067 S_Adjustment : Sloc_Adjustment;
1068 -- Offset created for each node in an instantiation, in order to keep
1069 -- track of the source position of the instantiation in each of its nodes.
1070 -- A subsequent semantic error or warning on a construct of the instance
1071 -- points to both places: the original generic node, and the point of
1072 -- instantiation. See Sinput and Sinput.L for additional details.
1074 ------------------------------------------------------------
1075 -- Data structure for keeping track when inside a Generic --
1076 ------------------------------------------------------------
1078 -- The following table is used to save values of the Inside_A_Generic
1079 -- flag (see spec of Sem) when they are saved by Start_Generic.
1081 package Generic_Flags is new Table.Table (
1082 Table_Component_Type => Boolean,
1083 Table_Index_Type => Int,
1084 Table_Low_Bound => 0,
1085 Table_Initial => 32,
1086 Table_Increment => 200,
1087 Table_Name => "Generic_Flags");
1089 ---------------------------
1090 -- Abandon_Instantiation --
1091 ---------------------------
1093 procedure Abandon_Instantiation (N : Node_Id) is
1094 begin
1095 Error_Msg_N ("\instantiation abandoned!", N);
1096 raise Instantiation_Error;
1097 end Abandon_Instantiation;
1099 ----------------------------------
1100 -- Adjust_Inherited_Pragma_Sloc --
1101 ----------------------------------
1103 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1104 begin
1105 Adjust_Instantiation_Sloc (N, S_Adjustment);
1106 end Adjust_Inherited_Pragma_Sloc;
1108 --------------------------
1109 -- Analyze_Associations --
1110 --------------------------
1112 function Analyze_Associations
1113 (I_Node : Node_Id;
1114 Formals : List_Id;
1115 F_Copy : List_Id) return List_Id
1117 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1118 Assoc_List : constant List_Id := New_List;
1119 Default_Actuals : constant List_Id := New_List;
1120 Gen_Unit : constant Entity_Id :=
1121 Defining_Entity (Parent (F_Copy));
1123 Actuals : List_Id;
1124 Actual : Node_Id;
1125 Analyzed_Formal : Node_Id;
1126 First_Named : Node_Id := Empty;
1127 Formal : Node_Id;
1128 Match : Node_Id := Empty;
1129 Named : Node_Id;
1130 Saved_Formal : Node_Id;
1132 Default_Formals : constant List_Id := New_List;
1133 -- If an N_Others_Choice is present, some of the formals may be
1134 -- defaulted. To simplify the treatment of visibility in an instance,
1135 -- we introduce individual defaults for each such formal. These
1136 -- defaults are appended to the list of associations and replace the
1137 -- N_Others_Choice.
1139 Found_Assoc : Node_Id;
1140 -- Association for the current formal being match. Empty if there are
1141 -- no remaining actuals, or if there is no named association with the
1142 -- name of the formal.
1144 Is_Named_Assoc : Boolean;
1145 Num_Matched : Nat := 0;
1146 Num_Actuals : Nat := 0;
1148 Others_Present : Boolean := False;
1149 -- In Ada 2005, indicates partial parameterization of a formal
1150 -- package. As usual an 'others' association must be last in the list.
1152 procedure Build_Subprogram_Wrappers;
1153 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1154 -- subprograms. The implementation of making the formal into a renaming
1155 -- of the actual does not work, given that subprogram renaming cannot
1156 -- carry aspect specifications. Instead we must create subprogram
1157 -- wrappers whose body is a call to the actual, and whose declaration
1158 -- carries the aspects of the formal.
1160 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1161 -- Warn if an actual fixed-point type has user-defined arithmetic
1162 -- operations, but there is no corresponding formal in the generic,
1163 -- in which case the predefined operations will be used. This merits
1164 -- a warning because of the special semantics of fixed point ops.
1166 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id);
1167 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1168 -- cannot have a named association for it. AI05-0025 extends this rule
1169 -- to formals of formal packages by AI05-0025, and it also applies to
1170 -- box-initialized formals.
1172 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1173 -- Determine whether the parameter types and the return type of Subp
1174 -- are fully defined at the point of instantiation.
1176 function Matching_Actual
1177 (F : Entity_Id;
1178 A_F : Entity_Id) return Node_Id;
1179 -- Find actual that corresponds to a given formal parameter. If the
1180 -- actuals are positional, return the next one, if any. If the actuals
1181 -- are named, scan the parameter associations to find the right one.
1182 -- A_F is the corresponding entity in the analyzed generic, which is
1183 -- placed on the selector name.
1185 -- In Ada 2005, a named association may be given with a box, in which
1186 -- case Matching_Actual sets Found_Assoc to the generic association,
1187 -- but return Empty for the actual itself. In this case the code below
1188 -- creates a corresponding declaration for the formal.
1190 function Partial_Parameterization return Boolean;
1191 -- Ada 2005: if no match is found for a given formal, check if the
1192 -- association for it includes a box, or whether the associations
1193 -- include an Others clause.
1195 procedure Process_Default (Formal : Node_Id);
1196 -- Add a copy of the declaration of a generic formal to the list of
1197 -- associations, and add an explicit box association for its entity
1198 -- if there is none yet, and the default comes from an N_Others_Choice.
1200 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1201 -- Determine whether Subp renames one of the subprograms defined in the
1202 -- generated package Standard.
1204 procedure Set_Analyzed_Formal;
1205 -- Find the node in the generic copy that corresponds to a given formal.
1206 -- The semantic information on this node is used to perform legality
1207 -- checks on the actuals. Because semantic analysis can introduce some
1208 -- anonymous entities or modify the declaration node itself, the
1209 -- correspondence between the two lists is not one-one. In addition to
1210 -- anonymous types, the presence a formal equality will introduce an
1211 -- implicit declaration for the corresponding inequality.
1213 -------------------------------
1214 -- Build_Subprogram_Wrappers --
1215 -------------------------------
1217 procedure Build_Subprogram_Wrappers is
1218 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
1219 -- Adjust sloc so that errors located at N will be reported with
1220 -- information about the instance and not just about the generic.
1222 ------------------------
1223 -- Adjust_Aspect_Sloc --
1224 ------------------------
1226 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
1227 begin
1228 Adjust_Instantiation_Sloc (N, S_Adjustment);
1229 return OK;
1230 end Adjust_Aspect_Sloc;
1232 procedure Adjust_Aspect_Slocs is new
1233 Traverse_Proc (Adjust_Aspect_Sloc);
1235 Formal : constant Entity_Id :=
1236 Defining_Unit_Name (Specification (Analyzed_Formal));
1237 Aspect_Spec : Node_Id;
1238 Decl_Node : Node_Id;
1239 Actual_Name : Node_Id;
1241 -- Start of processing for Build_Subprogram_Wrappers
1243 begin
1244 -- Create declaration for wrapper subprogram
1245 -- The actual can be overloaded, in which case it will be
1246 -- resolved when the call in the wrapper body is analyzed.
1247 -- We attach the possible interpretations of the actual to
1248 -- the name to be used in the call in the wrapper body.
1250 if Is_Entity_Name (Match) then
1251 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1253 if Is_Overloaded (Match) then
1254 Save_Interps (Match, Actual_Name);
1255 end if;
1257 else
1258 -- Use renaming declaration created when analyzing actual.
1259 -- This may be incomplete if there are several formal
1260 -- subprograms whose actual is an attribute ???
1262 declare
1263 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1265 begin
1266 Actual_Name := New_Occurrence_Of
1267 (Defining_Entity (Renaming_Decl), Sloc (Match));
1268 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1269 end;
1270 end if;
1272 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1274 -- Transfer aspect specifications from formal subprogram to wrapper
1276 Set_Aspect_Specifications (Decl_Node,
1277 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1279 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1280 while Present (Aspect_Spec) loop
1281 Adjust_Aspect_Slocs (Aspect_Spec);
1282 Set_Analyzed (Aspect_Spec, False);
1283 Next (Aspect_Spec);
1284 end loop;
1286 Append_To (Assoc_List, Decl_Node);
1288 -- Create corresponding body, and append it to association list
1289 -- that appears at the head of the declarations in the instance.
1290 -- The subprogram may be called in the analysis of subsequent
1291 -- actuals.
1293 Append_To (Assoc_List,
1294 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1295 end Build_Subprogram_Wrappers;
1297 ----------------------------------------
1298 -- Check_Overloaded_Formal_Subprogram --
1299 ----------------------------------------
1301 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id) is
1302 Temp_Formal : Node_Id;
1304 begin
1305 Temp_Formal := First (Formals);
1306 while Present (Temp_Formal) loop
1307 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1308 and then Temp_Formal /= Formal
1309 and then
1310 Chars (Defining_Unit_Name (Specification (Formal))) =
1311 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1312 then
1313 if Present (Found_Assoc) then
1314 Error_Msg_N
1315 ("named association not allowed for overloaded formal",
1316 Found_Assoc);
1317 Abandon_Instantiation (Instantiation_Node);
1318 end if;
1319 end if;
1321 Next (Temp_Formal);
1322 end loop;
1323 end Check_Overloaded_Formal_Subprogram;
1325 -------------------------------
1326 -- Check_Fixed_Point_Actual --
1327 -------------------------------
1329 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1330 Typ : constant Entity_Id := Entity (Actual);
1331 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1332 Elem : Elmt_Id;
1333 Formal : Node_Id;
1334 Op : Entity_Id;
1336 begin
1337 -- Locate primitive operations of the type that are arithmetic
1338 -- operations.
1340 Elem := First_Elmt (Prims);
1341 while Present (Elem) loop
1342 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1344 -- Check whether the generic unit has a formal subprogram of
1345 -- the same name. This does not check types but is good enough
1346 -- to justify a warning.
1348 Formal := First_Non_Pragma (Formals);
1349 Op := Alias (Node (Elem));
1351 while Present (Formal) loop
1352 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1353 and then Chars (Defining_Entity (Formal)) =
1354 Chars (Node (Elem))
1355 then
1356 exit;
1358 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1359 declare
1360 Assoc : Node_Id;
1361 Ent : Entity_Id;
1363 begin
1364 -- Locate corresponding actual, and check whether it
1365 -- includes a fixed-point type.
1367 Assoc := First (Assoc_List);
1368 while Present (Assoc) loop
1369 exit when
1370 Nkind (Assoc) = N_Package_Renaming_Declaration
1371 and then Chars (Defining_Unit_Name (Assoc)) =
1372 Chars (Defining_Identifier (Formal));
1374 Next (Assoc);
1375 end loop;
1377 if Present (Assoc) then
1379 -- If formal package declares a fixed-point type,
1380 -- and the user-defined operator is derived from
1381 -- a generic instance package, the fixed-point type
1382 -- does not use the corresponding predefined op.
1384 Ent := First_Entity (Entity (Name (Assoc)));
1385 while Present (Ent) loop
1386 if Is_Fixed_Point_Type (Ent)
1387 and then Present (Op)
1388 and then Is_Generic_Instance (Scope (Op))
1389 then
1390 return;
1391 end if;
1393 Next_Entity (Ent);
1394 end loop;
1395 end if;
1396 end;
1397 end if;
1399 Next (Formal);
1400 end loop;
1402 if No (Formal) then
1403 Error_Msg_Sloc := Sloc (Node (Elem));
1404 Error_Msg_NE
1405 ("?instance uses predefined operation, not primitive "
1406 & "operation&#", Actual, Node (Elem));
1407 end if;
1408 end if;
1410 Next_Elmt (Elem);
1411 end loop;
1412 end Check_Fixed_Point_Actual;
1414 -------------------------------
1415 -- Has_Fully_Defined_Profile --
1416 -------------------------------
1418 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1419 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1420 -- Determine whethet type Typ is fully defined
1422 ---------------------------
1423 -- Is_Fully_Defined_Type --
1424 ---------------------------
1426 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1427 begin
1428 -- A private type without a full view is not fully defined
1430 if Is_Private_Type (Typ)
1431 and then No (Full_View (Typ))
1432 then
1433 return False;
1435 -- An incomplete type is never fully defined
1437 elsif Is_Incomplete_Type (Typ) then
1438 return False;
1440 -- All other types are fully defined
1442 else
1443 return True;
1444 end if;
1445 end Is_Fully_Defined_Type;
1447 -- Local declarations
1449 Param : Entity_Id;
1451 -- Start of processing for Has_Fully_Defined_Profile
1453 begin
1454 -- Check the parameters
1456 Param := First_Formal (Subp);
1457 while Present (Param) loop
1458 if not Is_Fully_Defined_Type (Etype (Param)) then
1459 return False;
1460 end if;
1462 Next_Formal (Param);
1463 end loop;
1465 -- Check the return type
1467 return Is_Fully_Defined_Type (Etype (Subp));
1468 end Has_Fully_Defined_Profile;
1470 ---------------------
1471 -- Matching_Actual --
1472 ---------------------
1474 function Matching_Actual
1475 (F : Entity_Id;
1476 A_F : Entity_Id) return Node_Id
1478 Prev : Node_Id;
1479 Act : Node_Id;
1481 begin
1482 Is_Named_Assoc := False;
1484 -- End of list of purely positional parameters
1486 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1487 Found_Assoc := Empty;
1488 Act := Empty;
1490 -- Case of positional parameter corresponding to current formal
1492 elsif No (Selector_Name (Actual)) then
1493 Found_Assoc := Actual;
1494 Act := Explicit_Generic_Actual_Parameter (Actual);
1495 Num_Matched := Num_Matched + 1;
1496 Next (Actual);
1498 -- Otherwise scan list of named actuals to find the one with the
1499 -- desired name. All remaining actuals have explicit names.
1501 else
1502 Is_Named_Assoc := True;
1503 Found_Assoc := Empty;
1504 Act := Empty;
1505 Prev := Empty;
1507 while Present (Actual) loop
1508 if Nkind (Actual) = N_Others_Choice then
1509 Found_Assoc := Empty;
1510 Act := Empty;
1512 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1513 Set_Entity (Selector_Name (Actual), A_F);
1514 Set_Etype (Selector_Name (Actual), Etype (A_F));
1515 Generate_Reference (A_F, Selector_Name (Actual));
1517 Found_Assoc := Actual;
1518 Act := Explicit_Generic_Actual_Parameter (Actual);
1519 Num_Matched := Num_Matched + 1;
1520 exit;
1521 end if;
1523 Prev := Actual;
1524 Next (Actual);
1525 end loop;
1527 -- Reset for subsequent searches. In most cases the named
1528 -- associations are in order. If they are not, we reorder them
1529 -- to avoid scanning twice the same actual. This is not just a
1530 -- question of efficiency: there may be multiple defaults with
1531 -- boxes that have the same name. In a nested instantiation we
1532 -- insert actuals for those defaults, and cannot rely on their
1533 -- names to disambiguate them.
1535 if Actual = First_Named then
1536 Next (First_Named);
1538 elsif Present (Actual) then
1539 Insert_Before (First_Named, Remove_Next (Prev));
1540 end if;
1542 Actual := First_Named;
1543 end if;
1545 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1546 Set_Used_As_Generic_Actual (Entity (Act));
1547 end if;
1549 return Act;
1550 end Matching_Actual;
1552 ------------------------------
1553 -- Partial_Parameterization --
1554 ------------------------------
1556 function Partial_Parameterization return Boolean is
1557 begin
1558 return Others_Present
1559 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1560 end Partial_Parameterization;
1562 ---------------------
1563 -- Process_Default --
1564 ---------------------
1566 procedure Process_Default (Formal : Node_Id) is
1567 Loc : constant Source_Ptr := Sloc (I_Node);
1568 F_Id : constant Entity_Id := Defining_Entity (Formal);
1569 Decl : Node_Id;
1570 Default : Node_Id;
1571 Id : Entity_Id;
1573 begin
1574 -- Append copy of formal declaration to associations, and create new
1575 -- defining identifier for it.
1577 Decl := New_Copy_Tree (Formal);
1578 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1580 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1581 Set_Defining_Unit_Name (Specification (Decl), Id);
1583 else
1584 Set_Defining_Identifier (Decl, Id);
1585 end if;
1587 Append (Decl, Assoc_List);
1589 if No (Found_Assoc) then -- i.e. 'others'
1590 Default :=
1591 Make_Generic_Association (Loc,
1592 Selector_Name =>
1593 New_Occurrence_Of (Id, Loc),
1594 Explicit_Generic_Actual_Parameter => Empty);
1595 Set_Box_Present (Default);
1596 Append (Default, Default_Formals);
1597 end if;
1598 end Process_Default;
1600 ---------------------------------
1601 -- Renames_Standard_Subprogram --
1602 ---------------------------------
1604 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1605 Id : Entity_Id;
1607 begin
1608 Id := Alias (Subp);
1609 while Present (Id) loop
1610 if Scope (Id) = Standard_Standard then
1611 return True;
1612 end if;
1614 Id := Alias (Id);
1615 end loop;
1617 return False;
1618 end Renames_Standard_Subprogram;
1620 -------------------------
1621 -- Set_Analyzed_Formal --
1622 -------------------------
1624 procedure Set_Analyzed_Formal is
1625 Kind : Node_Kind;
1627 begin
1628 while Present (Analyzed_Formal) loop
1629 Kind := Nkind (Analyzed_Formal);
1631 case Nkind (Formal) is
1632 when N_Formal_Subprogram_Declaration =>
1633 exit when Kind in N_Formal_Subprogram_Declaration
1634 and then
1635 Chars
1636 (Defining_Unit_Name (Specification (Formal))) =
1637 Chars
1638 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1640 when N_Formal_Package_Declaration =>
1641 exit when Kind in N_Formal_Package_Declaration
1642 | N_Generic_Package_Declaration
1643 | N_Package_Declaration;
1645 when N_Use_Package_Clause
1646 | N_Use_Type_Clause
1648 exit;
1650 when others =>
1652 -- Skip freeze nodes, and nodes inserted to replace
1653 -- unrecognized pragmas.
1655 exit when
1656 Kind not in N_Formal_Subprogram_Declaration
1657 and then Kind not in N_Subprogram_Declaration
1658 | N_Freeze_Entity
1659 | N_Null_Statement
1660 | N_Itype_Reference
1661 and then Chars (Defining_Identifier (Formal)) =
1662 Chars (Defining_Identifier (Analyzed_Formal));
1663 end case;
1665 Next (Analyzed_Formal);
1666 end loop;
1667 end Set_Analyzed_Formal;
1669 -- Start of processing for Analyze_Associations
1671 begin
1672 Actuals := Generic_Associations (I_Node);
1674 if Present (Actuals) then
1676 -- Check for an Others choice, indicating a partial parameterization
1677 -- for a formal package.
1679 Actual := First (Actuals);
1680 while Present (Actual) loop
1681 if Nkind (Actual) = N_Others_Choice then
1682 Others_Present := True;
1684 if Present (Next (Actual)) then
1685 Error_Msg_N ("OTHERS must be last association", Actual);
1686 end if;
1688 -- This subprogram is used both for formal packages and for
1689 -- instantiations. For the latter, associations must all be
1690 -- explicit.
1692 if Nkind (I_Node) /= N_Formal_Package_Declaration
1693 and then Comes_From_Source (I_Node)
1694 then
1695 Error_Msg_N
1696 ("OTHERS association not allowed in an instance",
1697 Actual);
1698 end if;
1700 -- In any case, nothing to do after the others association
1702 exit;
1704 elsif Box_Present (Actual)
1705 and then Comes_From_Source (I_Node)
1706 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1707 then
1708 Error_Msg_N
1709 ("box association not allowed in an instance", Actual);
1710 end if;
1712 Next (Actual);
1713 end loop;
1715 -- If named associations are present, save first named association
1716 -- (it may of course be Empty) to facilitate subsequent name search.
1718 First_Named := First (Actuals);
1719 while Present (First_Named)
1720 and then Nkind (First_Named) /= N_Others_Choice
1721 and then No (Selector_Name (First_Named))
1722 loop
1723 Num_Actuals := Num_Actuals + 1;
1724 Next (First_Named);
1725 end loop;
1726 end if;
1728 Named := First_Named;
1729 while Present (Named) loop
1730 if Nkind (Named) /= N_Others_Choice
1731 and then No (Selector_Name (Named))
1732 then
1733 Error_Msg_N ("invalid positional actual after named one", Named);
1734 Abandon_Instantiation (Named);
1735 end if;
1737 -- A named association may lack an actual parameter, if it was
1738 -- introduced for a default subprogram that turns out to be local
1739 -- to the outer instantiation. If it has a box association it must
1740 -- correspond to some formal in the generic.
1742 if Nkind (Named) /= N_Others_Choice
1743 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1744 or else Box_Present (Named))
1745 then
1746 Num_Actuals := Num_Actuals + 1;
1747 end if;
1749 Next (Named);
1750 end loop;
1752 if Present (Formals) then
1753 Formal := First_Non_Pragma (Formals);
1754 Analyzed_Formal := First_Non_Pragma (F_Copy);
1756 if Present (Actuals) then
1757 Actual := First (Actuals);
1759 -- All formals should have default values
1761 else
1762 Actual := Empty;
1763 end if;
1765 while Present (Formal) loop
1766 Set_Analyzed_Formal;
1767 Saved_Formal := Next_Non_Pragma (Formal);
1769 case Nkind (Formal) is
1770 when N_Formal_Object_Declaration =>
1771 Match :=
1772 Matching_Actual
1773 (Defining_Identifier (Formal),
1774 Defining_Identifier (Analyzed_Formal));
1776 if No (Match) and then Partial_Parameterization then
1777 Process_Default (Formal);
1779 else
1780 Append_List
1781 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1782 Assoc_List);
1784 -- For a defaulted in_parameter, create an entry in the
1785 -- the list of defaulted actuals, for GNATprove use. Do
1786 -- not included these defaults for an instance nested
1787 -- within a generic, because the defaults are also used
1788 -- in the analysis of the enclosing generic, and only
1789 -- defaulted subprograms are relevant there.
1791 if No (Match) and then not Inside_A_Generic then
1792 Append_To (Default_Actuals,
1793 Make_Generic_Association (Sloc (I_Node),
1794 Selector_Name =>
1795 New_Occurrence_Of
1796 (Defining_Identifier (Formal), Sloc (I_Node)),
1797 Explicit_Generic_Actual_Parameter =>
1798 New_Copy_Tree (Default_Expression (Formal))));
1799 end if;
1800 end if;
1802 -- If the object is a call to an expression function, this
1803 -- is a freezing point for it.
1805 if Is_Entity_Name (Match)
1806 and then Present (Entity (Match))
1807 and then Nkind
1808 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1809 = N_Expression_Function
1810 then
1811 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1812 end if;
1814 when N_Formal_Type_Declaration =>
1815 Match :=
1816 Matching_Actual
1817 (Defining_Identifier (Formal),
1818 Defining_Identifier (Analyzed_Formal));
1820 if No (Match) then
1821 if Partial_Parameterization then
1822 Process_Default (Formal);
1824 elsif Present (Default_Subtype_Mark (Formal)) then
1825 Match := New_Copy (Default_Subtype_Mark (Formal));
1826 Append_List
1827 (Instantiate_Type
1828 (Formal, Match, Analyzed_Formal, Assoc_List),
1829 Assoc_List);
1830 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1832 else
1833 Error_Msg_Sloc := Sloc (Gen_Unit);
1834 Error_Msg_NE
1835 ("missing actual&",
1836 Instantiation_Node, Defining_Identifier (Formal));
1837 Error_Msg_NE
1838 ("\in instantiation of & declared#",
1839 Instantiation_Node, Gen_Unit);
1840 Abandon_Instantiation (Instantiation_Node);
1841 end if;
1843 else
1844 Analyze (Match);
1845 Append_List
1846 (Instantiate_Type
1847 (Formal, Match, Analyzed_Formal, Assoc_List),
1848 Assoc_List);
1850 -- Warn when an actual is a fixed-point with user-
1851 -- defined promitives. The warning is superfluous
1852 -- if the formal is private, because there can be
1853 -- no arithmetic operations in the generic so there
1854 -- no danger of confusion.
1856 if Is_Fixed_Point_Type (Entity (Match))
1857 and then not Is_Private_Type
1858 (Defining_Identifier (Analyzed_Formal))
1859 then
1860 Check_Fixed_Point_Actual (Match);
1861 end if;
1863 -- An instantiation is a freeze point for the actuals,
1864 -- unless this is a rewritten formal package, or the
1865 -- formal is an Ada 2012 formal incomplete type.
1867 if Nkind (I_Node) = N_Formal_Package_Declaration
1868 or else
1869 (Ada_Version >= Ada_2012
1870 and then
1871 Ekind (Defining_Identifier (Analyzed_Formal)) =
1872 E_Incomplete_Type)
1873 then
1874 null;
1876 else
1877 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1878 end if;
1879 end if;
1881 -- A remote access-to-class-wide type is not a legal actual
1882 -- for a generic formal of an access type (E.2.2(17/2)).
1883 -- In GNAT an exception to this rule is introduced when
1884 -- the formal is marked as remote using implementation
1885 -- defined aspect/pragma Remote_Access_Type. In that case
1886 -- the actual must be remote as well.
1888 -- If the current instantiation is the construction of a
1889 -- local copy for a formal package the actuals may be
1890 -- defaulted, and there is no matching actual to check.
1892 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1893 and then
1894 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1895 N_Access_To_Object_Definition
1896 and then Present (Match)
1897 then
1898 declare
1899 Formal_Ent : constant Entity_Id :=
1900 Defining_Identifier (Analyzed_Formal);
1901 begin
1902 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1903 = Is_Remote_Types (Formal_Ent)
1904 then
1905 -- Remoteness of formal and actual match
1907 null;
1909 elsif Is_Remote_Types (Formal_Ent) then
1911 -- Remote formal, non-remote actual
1913 Error_Msg_NE
1914 ("actual for& must be remote", Match, Formal_Ent);
1916 else
1917 -- Non-remote formal, remote actual
1919 Error_Msg_NE
1920 ("actual for& may not be remote",
1921 Match, Formal_Ent);
1922 end if;
1923 end;
1924 end if;
1926 when N_Formal_Subprogram_Declaration =>
1927 Match :=
1928 Matching_Actual
1929 (Defining_Unit_Name (Specification (Formal)),
1930 Defining_Unit_Name (Specification (Analyzed_Formal)));
1932 -- If the formal subprogram has the same name as another
1933 -- formal subprogram of the generic, then a named
1934 -- association is illegal (12.3(9)). Exclude named
1935 -- associations that are generated for a nested instance.
1937 if Present (Match)
1938 and then Is_Named_Assoc
1939 and then Comes_From_Source (Found_Assoc)
1940 then
1941 Check_Overloaded_Formal_Subprogram (Formal);
1942 end if;
1944 -- If there is no corresponding actual, this may be case
1945 -- of partial parameterization, or else the formal has a
1946 -- default or a box.
1948 if No (Match) and then Partial_Parameterization then
1949 Process_Default (Formal);
1951 if Nkind (I_Node) = N_Formal_Package_Declaration then
1952 Check_Overloaded_Formal_Subprogram (Formal);
1953 end if;
1955 else
1956 Append_To (Assoc_List,
1957 Instantiate_Formal_Subprogram
1958 (Formal, Match, Analyzed_Formal));
1960 -- If formal subprogram has contracts, create wrappers
1961 -- for it. This is an expansion activity that cannot
1962 -- take place e.g. within an enclosing generic unit.
1964 if Has_Contracts (Analyzed_Formal)
1965 and then (Expander_Active or GNATprove_Mode)
1966 then
1967 Build_Subprogram_Wrappers;
1968 end if;
1970 -- An instantiation is a freeze point for the actuals,
1971 -- unless this is a rewritten formal package.
1973 if Nkind (I_Node) /= N_Formal_Package_Declaration
1974 and then Nkind (Match) = N_Identifier
1975 and then Is_Subprogram (Entity (Match))
1977 -- The actual subprogram may rename a routine defined
1978 -- in Standard. Avoid freezing such renamings because
1979 -- subprograms coming from Standard cannot be frozen.
1981 and then
1982 not Renames_Standard_Subprogram (Entity (Match))
1984 -- If the actual subprogram comes from a different
1985 -- unit, it is already frozen, either by a body in
1986 -- that unit or by the end of the declarative part
1987 -- of the unit. This check avoids the freezing of
1988 -- subprograms defined in Standard which are used
1989 -- as generic actuals.
1991 and then In_Same_Code_Unit (Entity (Match), I_Node)
1992 and then Has_Fully_Defined_Profile (Entity (Match))
1993 then
1994 -- Mark the subprogram as having a delayed freeze
1995 -- since this may be an out-of-order action.
1997 Set_Has_Delayed_Freeze (Entity (Match));
1998 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1999 end if;
2000 end if;
2002 -- If this is a nested generic, preserve default for later
2003 -- instantiations. We do this as well for GNATprove use,
2004 -- so that the list of generic associations is complete.
2006 if No (Match) and then Box_Present (Formal) then
2007 declare
2008 Subp : constant Entity_Id :=
2009 Defining_Unit_Name
2010 (Specification (Last (Assoc_List)));
2012 begin
2013 Append_To (Default_Actuals,
2014 Make_Generic_Association (Sloc (I_Node),
2015 Selector_Name =>
2016 New_Occurrence_Of (Subp, Sloc (I_Node)),
2017 Explicit_Generic_Actual_Parameter =>
2018 New_Occurrence_Of (Subp, Sloc (I_Node))));
2019 end;
2020 end if;
2022 when N_Formal_Package_Declaration =>
2023 -- The name of the formal package may be hidden by the
2024 -- formal parameter itself.
2026 if Error_Posted (Analyzed_Formal) then
2027 Abandon_Instantiation (Instantiation_Node);
2029 else
2030 Match :=
2031 Matching_Actual
2032 (Defining_Identifier (Formal),
2033 Defining_Identifier
2034 (Original_Node (Analyzed_Formal)));
2035 end if;
2037 if No (Match) then
2038 if Partial_Parameterization then
2039 Process_Default (Formal);
2041 else
2042 Error_Msg_Sloc := Sloc (Gen_Unit);
2043 Error_Msg_NE
2044 ("missing actual&",
2045 Instantiation_Node, Defining_Identifier (Formal));
2046 Error_Msg_NE
2047 ("\in instantiation of & declared#",
2048 Instantiation_Node, Gen_Unit);
2050 Abandon_Instantiation (Instantiation_Node);
2051 end if;
2053 else
2054 Analyze (Match);
2055 Append_List
2056 (Instantiate_Formal_Package
2057 (Formal, Match, Analyzed_Formal),
2058 Assoc_List);
2060 -- Determine whether the actual package needs an explicit
2061 -- freeze node. This is only the case if the actual is
2062 -- declared in the same unit and has a body. Normally
2063 -- packages do not have explicit freeze nodes, and gigi
2064 -- only uses them to elaborate entities in a package
2065 -- body.
2067 Explicit_Freeze_Check : declare
2068 Actual : constant Entity_Id := Entity (Match);
2069 Gen_Par : Entity_Id;
2071 Needs_Freezing : Boolean;
2072 P : Node_Id;
2074 procedure Check_Generic_Parent;
2075 -- The actual may be an instantiation of a unit
2076 -- declared in a previous instantiation. If that
2077 -- one is also in the current compilation, it must
2078 -- itself be frozen before the actual. The actual
2079 -- may be an instantiation of a generic child unit,
2080 -- in which case the same applies to the instance
2081 -- of the parent which must be frozen before the
2082 -- actual.
2083 -- Should this itself be recursive ???
2085 --------------------------
2086 -- Check_Generic_Parent --
2087 --------------------------
2089 procedure Check_Generic_Parent is
2090 Inst : constant Node_Id :=
2091 Get_Unit_Instantiation_Node (Actual);
2092 Par : Entity_Id;
2094 begin
2095 Par := Empty;
2097 if Nkind (Parent (Actual)) = N_Package_Specification
2098 then
2099 Par := Scope (Generic_Parent (Parent (Actual)));
2101 if Is_Generic_Instance (Par) then
2102 null;
2104 -- If the actual is a child generic unit, check
2105 -- whether the instantiation of the parent is
2106 -- also local and must also be frozen now. We
2107 -- must retrieve the instance node to locate the
2108 -- parent instance if any.
2110 elsif Ekind (Par) = E_Generic_Package
2111 and then Is_Child_Unit (Gen_Par)
2112 and then Ekind (Scope (Gen_Par)) =
2113 E_Generic_Package
2114 then
2115 if Nkind (Inst) = N_Package_Instantiation
2116 and then Nkind (Name (Inst)) =
2117 N_Expanded_Name
2118 then
2119 -- Retrieve entity of parent instance
2121 Par := Entity (Prefix (Name (Inst)));
2122 end if;
2124 else
2125 Par := Empty;
2126 end if;
2127 end if;
2129 if Present (Par)
2130 and then Is_Generic_Instance (Par)
2131 and then Scope (Par) = Current_Scope
2132 and then
2133 (No (Freeze_Node (Par))
2134 or else
2135 not Is_List_Member (Freeze_Node (Par)))
2136 then
2137 Set_Has_Delayed_Freeze (Par);
2138 Append_Elmt (Par, Actuals_To_Freeze);
2139 end if;
2140 end Check_Generic_Parent;
2142 -- Start of processing for Explicit_Freeze_Check
2144 begin
2145 if Present (Renamed_Entity (Actual)) then
2146 Gen_Par :=
2147 Generic_Parent (Specification
2148 (Unit_Declaration_Node
2149 (Renamed_Entity (Actual))));
2150 else
2151 Gen_Par :=
2152 Generic_Parent (Specification
2153 (Unit_Declaration_Node (Actual)));
2154 end if;
2156 if not Expander_Active
2157 or else not Has_Completion (Actual)
2158 or else not In_Same_Source_Unit (I_Node, Actual)
2159 or else Is_Frozen (Actual)
2160 or else
2161 (Present (Renamed_Entity (Actual))
2162 and then
2163 not In_Same_Source_Unit
2164 (I_Node, (Renamed_Entity (Actual))))
2165 then
2166 null;
2168 else
2169 -- Finally we want to exclude such freeze nodes
2170 -- from statement sequences, which freeze
2171 -- everything before them.
2172 -- Is this strictly necessary ???
2174 Needs_Freezing := True;
2176 P := Parent (I_Node);
2177 while Nkind (P) /= N_Compilation_Unit loop
2178 if Nkind (P) = N_Handled_Sequence_Of_Statements
2179 then
2180 Needs_Freezing := False;
2181 exit;
2182 end if;
2184 P := Parent (P);
2185 end loop;
2187 if Needs_Freezing then
2188 Check_Generic_Parent;
2190 -- If the actual is a renaming of a proper
2191 -- instance of the formal package, indicate
2192 -- that it is the instance that must be frozen.
2194 if Nkind (Parent (Actual)) =
2195 N_Package_Renaming_Declaration
2196 then
2197 Set_Has_Delayed_Freeze
2198 (Renamed_Entity (Actual));
2199 Append_Elmt
2200 (Renamed_Entity (Actual),
2201 Actuals_To_Freeze);
2202 else
2203 Set_Has_Delayed_Freeze (Actual);
2204 Append_Elmt (Actual, Actuals_To_Freeze);
2205 end if;
2206 end if;
2207 end if;
2208 end Explicit_Freeze_Check;
2209 end if;
2211 -- For use type and use package appearing in the generic part,
2212 -- we have already copied them, so we can just move them where
2213 -- they belong (we mustn't recopy them since this would mess up
2214 -- the Sloc values).
2216 when N_Use_Package_Clause
2217 | N_Use_Type_Clause
2219 if Nkind (Original_Node (I_Node)) =
2220 N_Formal_Package_Declaration
2221 then
2222 Append (New_Copy_Tree (Formal), Assoc_List);
2223 else
2224 Remove (Formal);
2225 Append (Formal, Assoc_List);
2226 end if;
2228 when others =>
2229 raise Program_Error;
2230 end case;
2232 -- Check here the correct use of Ghost entities in generic
2233 -- instantiations, as now the generic has been resolved and
2234 -- we know which formal generic parameters are ghost (SPARK
2235 -- RM 6.9(10)).
2237 if Nkind (Formal) not in N_Use_Package_Clause
2238 | N_Use_Type_Clause
2239 then
2240 Check_Ghost_Context_In_Generic_Association
2241 (Actual => Match,
2242 Formal => Defining_Entity (Analyzed_Formal));
2243 end if;
2245 Formal := Saved_Formal;
2246 Next_Non_Pragma (Analyzed_Formal);
2247 end loop;
2249 if Num_Actuals > Num_Matched then
2250 Error_Msg_Sloc := Sloc (Gen_Unit);
2252 if Present (Selector_Name (Actual)) then
2253 Error_Msg_NE
2254 ("unmatched actual &", Actual, Selector_Name (Actual));
2255 Error_Msg_NE
2256 ("\in instantiation of & declared#", Actual, Gen_Unit);
2257 else
2258 Error_Msg_NE
2259 ("unmatched actual in instantiation of & declared#",
2260 Actual, Gen_Unit);
2261 end if;
2262 end if;
2264 elsif Present (Actuals) then
2265 Error_Msg_N
2266 ("too many actuals in generic instantiation", Instantiation_Node);
2267 end if;
2269 -- An instantiation freezes all generic actuals. The only exceptions
2270 -- to this are incomplete types and subprograms which are not fully
2271 -- defined at the point of instantiation.
2273 declare
2274 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2275 begin
2276 while Present (Elmt) loop
2277 Freeze_Before (I_Node, Node (Elmt));
2278 Next_Elmt (Elmt);
2279 end loop;
2280 end;
2282 -- If there are default subprograms, normalize the tree by adding
2283 -- explicit associations for them. This is required if the instance
2284 -- appears within a generic.
2286 if not Is_Empty_List (Default_Actuals) then
2287 declare
2288 Default : Node_Id;
2290 begin
2291 Default := First (Default_Actuals);
2292 while Present (Default) loop
2293 Mark_Rewrite_Insertion (Default);
2294 Next (Default);
2295 end loop;
2297 if No (Actuals) then
2298 Set_Generic_Associations (I_Node, Default_Actuals);
2299 else
2300 Append_List_To (Actuals, Default_Actuals);
2301 end if;
2302 end;
2303 end if;
2305 -- If this is a formal package, normalize the parameter list by adding
2306 -- explicit box associations for the formals that are covered by an
2307 -- N_Others_Choice.
2309 Append_List (Default_Formals, Formals);
2311 return Assoc_List;
2312 end Analyze_Associations;
2314 -------------------------------
2315 -- Analyze_Formal_Array_Type --
2316 -------------------------------
2318 procedure Analyze_Formal_Array_Type
2319 (T : in out Entity_Id;
2320 Def : Node_Id)
2322 DSS : Node_Id;
2324 begin
2325 -- Treated like a non-generic array declaration, with additional
2326 -- semantic checks.
2328 Enter_Name (T);
2330 if Nkind (Def) = N_Constrained_Array_Definition then
2331 DSS := First (Discrete_Subtype_Definitions (Def));
2332 while Present (DSS) loop
2333 if Nkind (DSS) in N_Subtype_Indication
2334 | N_Range
2335 | N_Attribute_Reference
2336 then
2337 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2338 end if;
2340 Next (DSS);
2341 end loop;
2342 end if;
2344 Array_Type_Declaration (T, Def);
2345 Set_Is_Generic_Type (Base_Type (T));
2347 if Ekind (Component_Type (T)) = E_Incomplete_Type
2348 and then No (Full_View (Component_Type (T)))
2349 then
2350 Error_Msg_N ("premature usage of incomplete type", Def);
2352 -- Check that range constraint is not allowed on the component type
2353 -- of a generic formal array type (AARM 12.5.3(3))
2355 elsif Is_Internal (Component_Type (T))
2356 and then Present (Subtype_Indication (Component_Definition (Def)))
2357 and then Nkind (Original_Node
2358 (Subtype_Indication (Component_Definition (Def)))) =
2359 N_Subtype_Indication
2360 then
2361 Error_Msg_N
2362 ("in a formal, a subtype indication can only be "
2363 & "a subtype mark (RM 12.5.3(3))",
2364 Subtype_Indication (Component_Definition (Def)));
2365 end if;
2367 end Analyze_Formal_Array_Type;
2369 ---------------------------------------------
2370 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2371 ---------------------------------------------
2373 -- As for other generic types, we create a valid type representation with
2374 -- legal but arbitrary attributes, whose values are never considered
2375 -- static. For all scalar types we introduce an anonymous base type, with
2376 -- the same attributes. We choose the corresponding integer type to be
2377 -- Standard_Integer.
2378 -- Here and in other similar routines, the Sloc of the generated internal
2379 -- type must be the same as the sloc of the defining identifier of the
2380 -- formal type declaration, to provide proper source navigation.
2382 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2383 (T : Entity_Id;
2384 Def : Node_Id)
2386 Loc : constant Source_Ptr := Sloc (Def);
2388 Base : constant Entity_Id :=
2389 New_Internal_Entity
2390 (E_Decimal_Fixed_Point_Type,
2391 Current_Scope,
2392 Sloc (Defining_Identifier (Parent (Def))), 'G');
2394 Int_Base : constant Entity_Id := Standard_Integer;
2395 Delta_Val : constant Ureal := Ureal_1;
2396 Digs_Val : constant Uint := Uint_6;
2398 function Make_Dummy_Bound return Node_Id;
2399 -- Return a properly typed universal real literal to use as a bound
2401 ----------------------
2402 -- Make_Dummy_Bound --
2403 ----------------------
2405 function Make_Dummy_Bound return Node_Id is
2406 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2407 begin
2408 Set_Etype (Bound, Universal_Real);
2409 return Bound;
2410 end Make_Dummy_Bound;
2412 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2414 begin
2415 Enter_Name (T);
2417 Set_Etype (Base, Base);
2418 Set_Size_Info (Base, Int_Base);
2419 Set_RM_Size (Base, RM_Size (Int_Base));
2420 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2421 Set_Digits_Value (Base, Digs_Val);
2422 Set_Delta_Value (Base, Delta_Val);
2423 Set_Small_Value (Base, Delta_Val);
2424 Set_Scalar_Range (Base,
2425 Make_Range (Loc,
2426 Low_Bound => Make_Dummy_Bound,
2427 High_Bound => Make_Dummy_Bound));
2429 Set_Is_Generic_Type (Base);
2430 Set_Parent (Base, Parent (Def));
2432 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2433 Set_Etype (T, Base);
2434 Set_Size_Info (T, Int_Base);
2435 Set_RM_Size (T, RM_Size (Int_Base));
2436 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2437 Set_Digits_Value (T, Digs_Val);
2438 Set_Delta_Value (T, Delta_Val);
2439 Set_Small_Value (T, Delta_Val);
2440 Set_Scalar_Range (T, Scalar_Range (Base));
2441 Set_Is_Constrained (T);
2443 Check_Restriction (No_Fixed_Point, Def);
2444 end Analyze_Formal_Decimal_Fixed_Point_Type;
2446 -------------------------------------------
2447 -- Analyze_Formal_Derived_Interface_Type --
2448 -------------------------------------------
2450 procedure Analyze_Formal_Derived_Interface_Type
2451 (N : Node_Id;
2452 T : Entity_Id;
2453 Def : Node_Id)
2455 Loc : constant Source_Ptr := Sloc (Def);
2457 begin
2458 -- Rewrite as a type declaration of a derived type. This ensures that
2459 -- the interface list and primitive operations are properly captured.
2461 Rewrite (N,
2462 Make_Full_Type_Declaration (Loc,
2463 Defining_Identifier => T,
2464 Type_Definition => Def));
2466 -- Keep the aspects from the original node
2468 Move_Aspects (Original_Node (N), N);
2470 Analyze (N);
2471 Set_Is_Generic_Type (T);
2472 end Analyze_Formal_Derived_Interface_Type;
2474 ---------------------------------
2475 -- Analyze_Formal_Derived_Type --
2476 ---------------------------------
2478 procedure Analyze_Formal_Derived_Type
2479 (N : Node_Id;
2480 T : Entity_Id;
2481 Def : Node_Id)
2483 Loc : constant Source_Ptr := Sloc (Def);
2484 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2485 New_N : Node_Id;
2487 begin
2488 Set_Is_Generic_Type (T);
2490 if Private_Present (Def) then
2491 New_N :=
2492 Make_Private_Extension_Declaration (Loc,
2493 Defining_Identifier => T,
2494 Discriminant_Specifications => Discriminant_Specifications (N),
2495 Unknown_Discriminants_Present => Unk_Disc,
2496 Subtype_Indication => Subtype_Mark (Def),
2497 Interface_List => Interface_List (Def));
2499 Set_Abstract_Present (New_N, Abstract_Present (Def));
2500 Set_Limited_Present (New_N, Limited_Present (Def));
2501 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2503 else
2504 New_N :=
2505 Make_Full_Type_Declaration (Loc,
2506 Defining_Identifier => T,
2507 Discriminant_Specifications =>
2508 Discriminant_Specifications (Parent (T)),
2509 Type_Definition =>
2510 Make_Derived_Type_Definition (Loc,
2511 Subtype_Indication => Subtype_Mark (Def)));
2513 Set_Abstract_Present
2514 (Type_Definition (New_N), Abstract_Present (Def));
2515 Set_Limited_Present
2516 (Type_Definition (New_N), Limited_Present (Def));
2517 end if;
2519 Rewrite (N, New_N);
2521 -- Keep the aspects from the original node
2523 Move_Aspects (Original_Node (N), N);
2525 Analyze (N);
2527 if Unk_Disc then
2528 if not Is_Composite_Type (T) then
2529 Error_Msg_N
2530 ("unknown discriminants not allowed for elementary types", N);
2531 else
2532 Set_Has_Unknown_Discriminants (T);
2533 Set_Is_Constrained (T, False);
2534 end if;
2535 end if;
2537 if Subtype_Mark (Def) <= Empty_Or_Error then
2538 pragma Assert (Serious_Errors_Detected > 0);
2539 -- avoid passing bad argument to Entity
2540 return;
2541 end if;
2543 -- If the parent type has a known size, so does the formal, which makes
2544 -- legal representation clauses that involve the formal.
2546 Set_Size_Known_At_Compile_Time
2547 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2548 end Analyze_Formal_Derived_Type;
2550 ----------------------------------
2551 -- Analyze_Formal_Discrete_Type --
2552 ----------------------------------
2554 -- The operations defined for a discrete types are those of an enumeration
2555 -- type. The size is set to an arbitrary value, for use in analyzing the
2556 -- generic unit.
2558 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2559 Loc : constant Source_Ptr := Sloc (Def);
2560 Lo : Node_Id;
2561 Hi : Node_Id;
2563 Base : constant Entity_Id :=
2564 New_Internal_Entity
2565 (E_Floating_Point_Type, Current_Scope,
2566 Sloc (Defining_Identifier (Parent (Def))), 'G');
2568 begin
2569 Enter_Name (T);
2570 Mutate_Ekind (T, E_Enumeration_Subtype);
2571 Set_Etype (T, Base);
2572 Init_Size (T, 8);
2573 Reinit_Alignment (T);
2574 Set_Is_Generic_Type (T);
2575 Set_Is_Constrained (T);
2577 -- For semantic analysis, the bounds of the type must be set to some
2578 -- non-static value. The simplest is to create attribute nodes for those
2579 -- bounds, that refer to the type itself. These bounds are never
2580 -- analyzed but serve as place-holders.
2582 Lo :=
2583 Make_Attribute_Reference (Loc,
2584 Attribute_Name => Name_First,
2585 Prefix => New_Occurrence_Of (T, Loc));
2586 Set_Etype (Lo, T);
2588 Hi :=
2589 Make_Attribute_Reference (Loc,
2590 Attribute_Name => Name_Last,
2591 Prefix => New_Occurrence_Of (T, Loc));
2592 Set_Etype (Hi, T);
2594 Set_Scalar_Range (T,
2595 Make_Range (Loc,
2596 Low_Bound => Lo,
2597 High_Bound => Hi));
2599 Mutate_Ekind (Base, E_Enumeration_Type);
2600 Set_Etype (Base, Base);
2601 Init_Size (Base, 8);
2602 Reinit_Alignment (Base);
2603 Set_Is_Generic_Type (Base);
2604 Set_Scalar_Range (Base, Scalar_Range (T));
2605 Set_Parent (Base, Parent (Def));
2606 end Analyze_Formal_Discrete_Type;
2608 ----------------------------------
2609 -- Analyze_Formal_Floating_Type --
2610 ---------------------------------
2612 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2613 Base : constant Entity_Id :=
2614 New_Internal_Entity
2615 (E_Floating_Point_Type, Current_Scope,
2616 Sloc (Defining_Identifier (Parent (Def))), 'G');
2618 begin
2619 -- The various semantic attributes are taken from the predefined type
2620 -- Float, just so that all of them are initialized. Their values are
2621 -- never used because no constant folding or expansion takes place in
2622 -- the generic itself.
2624 Enter_Name (T);
2625 Mutate_Ekind (T, E_Floating_Point_Subtype);
2626 Set_Etype (T, Base);
2627 Set_Size_Info (T, (Standard_Float));
2628 Set_RM_Size (T, RM_Size (Standard_Float));
2629 Set_Digits_Value (T, Digits_Value (Standard_Float));
2630 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2631 Set_Is_Constrained (T);
2633 Set_Is_Generic_Type (Base);
2634 Set_Etype (Base, Base);
2635 Set_Size_Info (Base, (Standard_Float));
2636 Set_RM_Size (Base, RM_Size (Standard_Float));
2637 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2638 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2639 Set_Parent (Base, Parent (Def));
2641 Check_Restriction (No_Floating_Point, Def);
2642 end Analyze_Formal_Floating_Type;
2644 -----------------------------------
2645 -- Analyze_Formal_Interface_Type;--
2646 -----------------------------------
2648 procedure Analyze_Formal_Interface_Type
2649 (N : Node_Id;
2650 T : Entity_Id;
2651 Def : Node_Id)
2653 Loc : constant Source_Ptr := Sloc (N);
2654 New_N : Node_Id;
2656 begin
2657 New_N :=
2658 Make_Full_Type_Declaration (Loc,
2659 Defining_Identifier => T,
2660 Type_Definition => Def);
2662 Rewrite (N, New_N);
2664 -- Keep the aspects from the original node
2666 Move_Aspects (Original_Node (N), N);
2668 Analyze (N);
2669 Set_Is_Generic_Type (T);
2670 end Analyze_Formal_Interface_Type;
2672 ---------------------------------
2673 -- Analyze_Formal_Modular_Type --
2674 ---------------------------------
2676 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2677 begin
2678 -- Apart from their entity kind, generic modular types are treated like
2679 -- signed integer types, and have the same attributes.
2681 Analyze_Formal_Signed_Integer_Type (T, Def);
2682 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2683 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2685 end Analyze_Formal_Modular_Type;
2687 ---------------------------------------
2688 -- Analyze_Formal_Object_Declaration --
2689 ---------------------------------------
2691 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2692 E : constant Node_Id := Default_Expression (N);
2693 Id : constant Node_Id := Defining_Identifier (N);
2695 K : Entity_Kind;
2696 Parent_Installed : Boolean := False;
2697 T : Node_Id;
2699 begin
2700 Enter_Name (Id);
2702 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2704 -- Determine the mode of the formal object
2706 if Out_Present (N) then
2707 K := E_Generic_In_Out_Parameter;
2709 if not In_Present (N) then
2710 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2711 end if;
2713 else
2714 K := E_Generic_In_Parameter;
2715 end if;
2717 if Present (Subtype_Mark (N)) then
2718 Find_Type (Subtype_Mark (N));
2719 T := Entity (Subtype_Mark (N));
2721 -- Verify that there is no redundant null exclusion
2723 if Null_Exclusion_Present (N) then
2724 if not Is_Access_Type (T) then
2725 Error_Msg_N
2726 ("null exclusion can only apply to an access type", N);
2728 elsif Can_Never_Be_Null (T) then
2729 Error_Msg_NE
2730 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2731 end if;
2732 end if;
2734 -- Ada 2005 (AI-423): Formal object with an access definition
2736 else
2737 Check_Access_Definition (N);
2738 T := Access_Definition
2739 (Related_Nod => N,
2740 N => Access_Definition (N));
2741 end if;
2743 if Ekind (T) = E_Incomplete_Type then
2744 declare
2745 Error_Node : Node_Id;
2747 begin
2748 if Present (Subtype_Mark (N)) then
2749 Error_Node := Subtype_Mark (N);
2750 else
2751 Check_Access_Definition (N);
2752 Error_Node := Access_Definition (N);
2753 end if;
2755 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2756 end;
2757 end if;
2759 if K = E_Generic_In_Parameter then
2761 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2763 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2764 Error_Msg_N
2765 ("generic formal of mode IN must not be of limited type", N);
2766 Explain_Limited_Type (T, N);
2767 end if;
2769 if Is_Abstract_Type (T) then
2770 Error_Msg_N
2771 ("generic formal of mode IN must not be of abstract type", N);
2772 end if;
2774 if Present (E) then
2775 Preanalyze_Spec_Expression (E, T);
2777 -- The default for a ghost generic formal IN parameter of
2778 -- access-to-variable type should be a ghost object (SPARK
2779 -- RM 6.9(13)).
2781 if Is_Access_Variable (T) then
2782 Check_Ghost_Formal_Variable
2783 (Actual => E,
2784 Formal => Id,
2785 Is_Default => True);
2786 end if;
2788 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2789 Error_Msg_N
2790 ("initialization not allowed for limited types", E);
2791 Explain_Limited_Type (T, E);
2792 end if;
2793 end if;
2795 Mutate_Ekind (Id, K);
2796 Set_Etype (Id, T);
2798 -- Case of generic IN OUT parameter
2800 else
2801 -- If the formal has an unconstrained type, construct its actual
2802 -- subtype, as is done for subprogram formals. In this fashion, all
2803 -- its uses can refer to specific bounds.
2805 Mutate_Ekind (Id, K);
2806 Set_Etype (Id, T);
2808 if (Is_Array_Type (T) and then not Is_Constrained (T))
2809 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2810 then
2811 declare
2812 Non_Freezing_Ref : constant Node_Id :=
2813 New_Occurrence_Of (Id, Sloc (Id));
2814 Decl : Node_Id;
2816 begin
2817 -- Make sure the actual subtype doesn't generate bogus freezing
2819 Set_Must_Not_Freeze (Non_Freezing_Ref);
2820 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2821 Insert_Before_And_Analyze (N, Decl);
2822 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2823 end;
2824 else
2825 Set_Actual_Subtype (Id, T);
2826 end if;
2828 if Present (E) then
2829 Error_Msg_N
2830 ("initialization not allowed for `IN OUT` formals", N);
2831 end if;
2832 end if;
2834 Analyze_Aspect_Specifications (N, Id);
2836 if Parent_Installed then
2837 Remove_Parent;
2838 end if;
2839 end Analyze_Formal_Object_Declaration;
2841 ----------------------------------------------
2842 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2843 ----------------------------------------------
2845 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2846 (T : Entity_Id;
2847 Def : Node_Id)
2849 Loc : constant Source_Ptr := Sloc (Def);
2850 Base : constant Entity_Id :=
2851 New_Internal_Entity
2852 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2853 Sloc (Defining_Identifier (Parent (Def))), 'G');
2855 begin
2856 -- The semantic attributes are set for completeness only, their values
2857 -- will never be used, since all properties of the type are non-static.
2859 Enter_Name (T);
2860 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2861 Set_Etype (T, Base);
2862 Set_Size_Info (T, Standard_Integer);
2863 Set_RM_Size (T, RM_Size (Standard_Integer));
2864 Set_Small_Value (T, Ureal_1);
2865 Set_Delta_Value (T, Ureal_1);
2866 Set_Scalar_Range (T,
2867 Make_Range (Loc,
2868 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2869 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2870 Set_Is_Constrained (T);
2872 Set_Is_Generic_Type (Base);
2873 Set_Etype (Base, Base);
2874 Set_Size_Info (Base, Standard_Integer);
2875 Set_RM_Size (Base, RM_Size (Standard_Integer));
2876 Set_Small_Value (Base, Ureal_1);
2877 Set_Delta_Value (Base, Ureal_1);
2878 Set_Scalar_Range (Base, Scalar_Range (T));
2879 Set_Parent (Base, Parent (Def));
2881 Check_Restriction (No_Fixed_Point, Def);
2882 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2884 ----------------------------------------
2885 -- Analyze_Formal_Package_Declaration --
2886 ----------------------------------------
2888 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2889 Gen_Id : constant Node_Id := Name (N);
2890 Loc : constant Source_Ptr := Sloc (N);
2891 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2892 Formal : Entity_Id;
2893 Gen_Decl : Node_Id;
2894 Gen_Unit : Entity_Id;
2895 Renaming : Node_Id;
2897 Vis_Prims_List : Elist_Id := No_Elist;
2898 -- List of primitives made temporarily visible in the instantiation
2899 -- to match the visibility of the formal type.
2901 function Build_Local_Package return Node_Id;
2902 -- The formal package is rewritten so that its parameters are replaced
2903 -- with corresponding declarations. For parameters with bona fide
2904 -- associations these declarations are created by Analyze_Associations
2905 -- as for a regular instantiation. For boxed parameters, we preserve
2906 -- the formal declarations and analyze them, in order to introduce
2907 -- entities of the right kind in the environment of the formal.
2909 -------------------------
2910 -- Build_Local_Package --
2911 -------------------------
2913 function Build_Local_Package return Node_Id is
2914 Decls : List_Id;
2915 Pack_Decl : Node_Id;
2917 begin
2918 -- Within the formal, the name of the generic package is a renaming
2919 -- of the formal (as for a regular instantiation).
2921 Pack_Decl :=
2922 Make_Package_Declaration (Loc,
2923 Specification =>
2924 Copy_Generic_Node
2925 (Specification (Original_Node (Gen_Decl)),
2926 Empty, Instantiating => True));
2928 Renaming :=
2929 Make_Package_Renaming_Declaration (Loc,
2930 Defining_Unit_Name =>
2931 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2932 Name => New_Occurrence_Of (Formal, Loc));
2934 if Nkind (Gen_Id) = N_Identifier
2935 and then Chars (Gen_Id) = Chars (Pack_Id)
2936 then
2937 Error_Msg_NE
2938 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2939 end if;
2941 -- If the formal is declared with a box, or with an others choice,
2942 -- create corresponding declarations for all entities in the formal
2943 -- part, so that names with the proper types are available in the
2944 -- specification of the formal package.
2946 -- On the other hand, if there are no associations, then all the
2947 -- formals must have defaults, and this will be checked by the
2948 -- call to Analyze_Associations.
2950 if Box_Present (N)
2951 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2952 then
2953 declare
2954 Formal_Decl : Node_Id;
2956 begin
2957 -- TBA : for a formal package, need to recurse ???
2959 Decls := New_List;
2960 Formal_Decl :=
2961 First
2962 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2963 while Present (Formal_Decl) loop
2964 Append_To
2965 (Decls,
2966 Copy_Generic_Node
2967 (Formal_Decl, Empty, Instantiating => True));
2968 Next (Formal_Decl);
2969 end loop;
2970 end;
2972 -- If generic associations are present, use Analyze_Associations to
2973 -- create the proper renaming declarations.
2975 else
2976 declare
2977 Act_Tree : constant Node_Id :=
2978 Copy_Generic_Node
2979 (Original_Node (Gen_Decl), Empty,
2980 Instantiating => True);
2982 begin
2983 Generic_Renamings.Set_Last (0);
2984 Generic_Renamings_HTable.Reset;
2985 Instantiation_Node := N;
2987 Decls :=
2988 Analyze_Associations
2989 (I_Node => Original_Node (N),
2990 Formals => Generic_Formal_Declarations (Act_Tree),
2991 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2993 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2994 end;
2995 end if;
2997 Append (Renaming, To => Decls);
2999 -- Add generated declarations ahead of local declarations in
3000 -- the package.
3002 if No (Visible_Declarations (Specification (Pack_Decl))) then
3003 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3004 else
3005 Insert_List_Before
3006 (First (Visible_Declarations (Specification (Pack_Decl))),
3007 Decls);
3008 end if;
3010 return Pack_Decl;
3011 end Build_Local_Package;
3013 -- Local variables
3015 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3016 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3018 Associations : Boolean := True;
3019 New_N : Node_Id;
3020 Parent_Installed : Boolean := False;
3021 Parent_Instance : Entity_Id;
3022 Renaming_In_Par : Entity_Id;
3024 -- Start of processing for Analyze_Formal_Package_Declaration
3026 begin
3027 Check_Text_IO_Special_Unit (Gen_Id);
3029 Init_Env;
3030 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3031 Gen_Unit := Entity (Gen_Id);
3033 -- Check for a formal package that is a package renaming
3035 if Present (Renamed_Entity (Gen_Unit)) then
3037 -- Indicate that unit is used, before replacing it with renamed
3038 -- entity for use below.
3040 if In_Extended_Main_Source_Unit (N) then
3041 Set_Is_Instantiated (Gen_Unit);
3042 Generate_Reference (Gen_Unit, N);
3043 end if;
3045 Gen_Unit := Renamed_Entity (Gen_Unit);
3046 end if;
3048 if Ekind (Gen_Unit) /= E_Generic_Package then
3049 Error_Msg_N ("expect generic package name", Gen_Id);
3050 Restore_Env;
3051 goto Leave;
3053 elsif Gen_Unit = Current_Scope then
3054 Error_Msg_N
3055 ("generic package cannot be used as a formal package of itself",
3056 Gen_Id);
3057 Restore_Env;
3058 goto Leave;
3060 elsif In_Open_Scopes (Gen_Unit) then
3061 if Is_Compilation_Unit (Gen_Unit)
3062 and then Is_Child_Unit (Current_Scope)
3063 then
3064 -- Special-case the error when the formal is a parent, and
3065 -- continue analysis to minimize cascaded errors.
3067 Error_Msg_N
3068 ("generic parent cannot be used as formal package of a child "
3069 & "unit", Gen_Id);
3071 else
3072 Error_Msg_N
3073 ("generic package cannot be used as a formal package within "
3074 & "itself", Gen_Id);
3075 Restore_Env;
3076 goto Leave;
3077 end if;
3078 end if;
3080 -- Check that name of formal package does not hide name of generic,
3081 -- or its leading prefix. This check must be done separately because
3082 -- the name of the generic has already been analyzed.
3084 declare
3085 Gen_Name : Entity_Id;
3087 begin
3088 Gen_Name := Gen_Id;
3089 while Nkind (Gen_Name) = N_Expanded_Name loop
3090 Gen_Name := Prefix (Gen_Name);
3091 end loop;
3093 if Chars (Gen_Name) = Chars (Pack_Id) then
3094 Error_Msg_NE
3095 ("& is hidden within declaration of formal package",
3096 Gen_Id, Gen_Name);
3097 end if;
3098 end;
3100 if Box_Present (N)
3101 or else No (Generic_Associations (N))
3102 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3103 then
3104 Associations := False;
3105 end if;
3107 -- If there are no generic associations, the generic parameters appear
3108 -- as local entities and are instantiated like them. We copy the generic
3109 -- package declaration as if it were an instantiation, and analyze it
3110 -- like a regular package, except that we treat the formals as
3111 -- additional visible components.
3113 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3115 if In_Extended_Main_Source_Unit (N) then
3116 Set_Is_Instantiated (Gen_Unit);
3117 Generate_Reference (Gen_Unit, N);
3118 end if;
3120 Formal := New_Copy (Pack_Id);
3121 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3123 -- Make local generic without formals. The formals will be replaced with
3124 -- internal declarations.
3126 begin
3127 New_N := Build_Local_Package;
3129 -- If there are errors in the parameter list, Analyze_Associations
3130 -- raises Instantiation_Error. Patch the declaration to prevent further
3131 -- exception propagation.
3133 exception
3134 when Instantiation_Error =>
3135 Enter_Name (Formal);
3136 Mutate_Ekind (Formal, E_Variable);
3137 Set_Etype (Formal, Any_Type);
3138 Restore_Hidden_Primitives (Vis_Prims_List);
3140 if Parent_Installed then
3141 Remove_Parent;
3142 end if;
3144 goto Leave;
3145 end;
3147 Rewrite (N, New_N);
3148 Set_Defining_Unit_Name (Specification (New_N), Formal);
3149 Set_Generic_Parent (Specification (N), Gen_Unit);
3150 Set_Instance_Env (Gen_Unit, Formal);
3151 Set_Is_Generic_Instance (Formal);
3153 Enter_Name (Formal);
3154 Mutate_Ekind (Formal, E_Package);
3155 Set_Etype (Formal, Standard_Void_Type);
3156 Set_Inner_Instances (Formal, New_Elmt_List);
3158 -- It is unclear that any aspects can apply to a formal package
3159 -- declaration, given that they look like a hidden conformance
3160 -- requirement on the corresponding actual. However, Abstract_State
3161 -- must be treated specially because it generates declarations that
3162 -- must appear before other declarations in the specification and
3163 -- must be analyzed at once.
3165 if Present (Aspect_Specifications (Gen_Decl)) then
3166 if No (Aspect_Specifications (N)) then
3167 Set_Aspect_Specifications (N, New_List);
3168 end if;
3170 declare
3171 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3172 New_A : Node_Id;
3174 begin
3175 while Present (ASN) loop
3176 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3177 New_A :=
3178 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3179 Set_Entity (New_A, Formal);
3180 Set_Analyzed (New_A, False);
3181 Append (New_A, Aspect_Specifications (N));
3182 Analyze_Aspect_Specifications (N, Formal);
3183 exit;
3184 end if;
3186 Next (ASN);
3187 end loop;
3188 end;
3189 end if;
3191 Push_Scope (Formal);
3193 -- Manually set the SPARK_Mode from the context because the package
3194 -- declaration is never analyzed.
3196 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3197 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3198 Set_SPARK_Pragma_Inherited (Formal);
3199 Set_SPARK_Aux_Pragma_Inherited (Formal);
3201 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3203 -- Similarly, we have to make the name of the formal visible in the
3204 -- parent instance, to resolve properly fully qualified names that
3205 -- may appear in the generic unit. The parent instance has been
3206 -- placed on the scope stack ahead of the current scope.
3208 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3210 Renaming_In_Par :=
3211 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3212 Mutate_Ekind (Renaming_In_Par, E_Package);
3213 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3214 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3215 Set_Scope (Renaming_In_Par, Parent_Instance);
3216 Set_Parent (Renaming_In_Par, Parent (Formal));
3217 Set_Renamed_Entity (Renaming_In_Par, Formal);
3218 Append_Entity (Renaming_In_Par, Parent_Instance);
3219 end if;
3221 -- A formal package declaration behaves as a package instantiation with
3222 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3223 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3224 -- all SPARK_Mode pragmas within the generic_package_name.
3226 if SPARK_Mode /= On then
3227 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3229 -- Mark the formal spec in case the body is instantiated at a later
3230 -- pass. This preserves the original context in effect for the body.
3232 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3233 end if;
3235 Analyze (Specification (N));
3237 -- The formals for which associations are provided are not visible
3238 -- outside of the formal package. The others are still declared by a
3239 -- formal parameter declaration.
3241 -- If there are no associations, the only local entity to hide is the
3242 -- generated package renaming itself.
3244 declare
3245 E : Entity_Id;
3247 begin
3248 E := First_Entity (Formal);
3249 while Present (E) loop
3250 if Associations and then not Is_Generic_Formal (E) then
3251 Set_Is_Hidden (E);
3252 end if;
3254 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3255 Set_Is_Hidden (E);
3256 exit;
3257 end if;
3259 Next_Entity (E);
3260 end loop;
3261 end;
3263 End_Package_Scope (Formal);
3264 Restore_Hidden_Primitives (Vis_Prims_List);
3266 if Parent_Installed then
3267 Remove_Parent;
3268 end if;
3270 Restore_Env;
3272 -- Inside the generic unit, the formal package is a regular package, but
3273 -- no body is needed for it. Note that after instantiation, the defining
3274 -- unit name we need is in the new tree and not in the original (see
3275 -- Package_Instantiation). A generic formal package is an instance, and
3276 -- can be used as an actual for an inner instance.
3278 Set_Has_Completion (Formal, True);
3280 -- Add semantic information to the original defining identifier.
3282 Mutate_Ekind (Pack_Id, E_Package);
3283 Set_Etype (Pack_Id, Standard_Void_Type);
3284 Set_Scope (Pack_Id, Scope (Formal));
3285 Set_Has_Completion (Pack_Id, True);
3287 <<Leave>>
3288 -- Unclear that any other aspects may appear here, analyze them
3289 -- for completion, given that the grammar allows their appearance.
3291 Analyze_Aspect_Specifications (N, Pack_Id);
3293 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3294 end Analyze_Formal_Package_Declaration;
3296 ---------------------------------
3297 -- Analyze_Formal_Private_Type --
3298 ---------------------------------
3300 procedure Analyze_Formal_Private_Type
3301 (N : Node_Id;
3302 T : Entity_Id;
3303 Def : Node_Id)
3305 begin
3306 New_Private_Type (N, T, Def);
3308 -- Set the size to an arbitrary but legal value
3310 Set_Size_Info (T, Standard_Integer);
3311 Set_RM_Size (T, RM_Size (Standard_Integer));
3312 end Analyze_Formal_Private_Type;
3314 ------------------------------------
3315 -- Analyze_Formal_Incomplete_Type --
3316 ------------------------------------
3318 procedure Analyze_Formal_Incomplete_Type
3319 (T : Entity_Id;
3320 Def : Node_Id)
3322 begin
3323 Enter_Name (T);
3324 Mutate_Ekind (T, E_Incomplete_Type);
3325 Set_Etype (T, T);
3326 Set_Private_Dependents (T, New_Elmt_List);
3328 if Tagged_Present (Def) then
3329 Set_Is_Tagged_Type (T);
3330 Make_Class_Wide_Type (T);
3331 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3332 end if;
3333 end Analyze_Formal_Incomplete_Type;
3335 ----------------------------------------
3336 -- Analyze_Formal_Signed_Integer_Type --
3337 ----------------------------------------
3339 procedure Analyze_Formal_Signed_Integer_Type
3340 (T : Entity_Id;
3341 Def : Node_Id)
3343 Base : constant Entity_Id :=
3344 New_Internal_Entity
3345 (E_Signed_Integer_Type,
3346 Current_Scope,
3347 Sloc (Defining_Identifier (Parent (Def))), 'G');
3349 begin
3350 Enter_Name (T);
3352 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3353 Set_Etype (T, Base);
3354 Set_Size_Info (T, Standard_Integer);
3355 Set_RM_Size (T, RM_Size (Standard_Integer));
3356 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3357 Set_Is_Constrained (T);
3359 Set_Is_Generic_Type (Base);
3360 Set_Size_Info (Base, Standard_Integer);
3361 Set_RM_Size (Base, RM_Size (Standard_Integer));
3362 Set_Etype (Base, Base);
3363 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3364 Set_Parent (Base, Parent (Def));
3365 end Analyze_Formal_Signed_Integer_Type;
3367 -------------------------------------------
3368 -- Analyze_Formal_Subprogram_Declaration --
3369 -------------------------------------------
3371 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3372 Spec : constant Node_Id := Specification (N);
3373 Def : constant Node_Id := Default_Name (N);
3374 Expr : constant Node_Id := Expression (N);
3375 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3377 Parent_Installed : Boolean := False;
3378 Subp : Entity_Id;
3380 begin
3381 if Nam = Error then
3382 return;
3383 end if;
3385 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3386 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3387 goto Leave;
3388 end if;
3390 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3392 Analyze_Subprogram_Declaration (N);
3393 Set_Is_Formal_Subprogram (Nam);
3394 Set_Has_Completion (Nam);
3396 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3397 Set_Is_Abstract_Subprogram (Nam);
3399 Set_Is_Dispatching_Operation (Nam);
3401 -- A formal abstract procedure cannot have a null default
3402 -- (RM 12.6(4.1/2)).
3404 if Nkind (Spec) = N_Procedure_Specification
3405 and then Null_Present (Spec)
3406 then
3407 Error_Msg_N
3408 ("a formal abstract subprogram cannot default to null", Spec);
3409 end if;
3411 -- A formal abstract function cannot have an expression default
3412 -- (expression defaults are allowed for nonabstract formal functions
3413 -- when extensions are enabled).
3415 if Nkind (Spec) = N_Function_Specification
3416 and then Present (Expr)
3417 then
3418 Error_Msg_N
3419 ("a formal abstract subprogram cannot default to an expression",
3420 Spec);
3421 end if;
3423 declare
3424 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3425 begin
3426 if No (Ctrl_Type) then
3427 Error_Msg_N
3428 ("abstract formal subprogram must have a controlling type",
3431 elsif Ada_Version >= Ada_2012
3432 and then Is_Incomplete_Type (Ctrl_Type)
3433 then
3434 Error_Msg_NE
3435 ("controlling type of abstract formal subprogram cannot "
3436 & "be incomplete type", N, Ctrl_Type);
3438 else
3439 Check_Controlling_Formals (Ctrl_Type, Nam);
3440 end if;
3441 end;
3442 end if;
3444 -- Default name is resolved at the point of instantiation
3446 if Box_Present (N) then
3447 null;
3449 -- Default name is bound at the point of generic declaration
3451 elsif Present (Def) then
3452 if Nkind (Def) = N_Operator_Symbol then
3453 Find_Direct_Name (Def);
3455 elsif Nkind (Def) /= N_Attribute_Reference then
3456 Analyze (Def);
3458 else
3459 -- For an attribute reference, analyze the prefix and verify
3460 -- that it has the proper profile for the subprogram.
3462 Analyze (Prefix (Def));
3463 Valid_Default_Attribute (Nam, Def);
3464 goto Leave;
3465 end if;
3467 -- The default for a ghost generic formal procedure should be a ghost
3468 -- procedure (SPARK RM 6.9(13)).
3470 if Ekind (Nam) = E_Procedure then
3471 declare
3472 Def_E : Entity_Id := Empty;
3473 begin
3474 if Nkind (Def) in N_Has_Entity then
3475 Def_E := Entity (Def);
3476 end if;
3478 Check_Ghost_Formal_Procedure_Or_Package
3479 (N => Def,
3480 Actual => Def_E,
3481 Formal => Nam,
3482 Is_Default => True);
3483 end;
3484 end if;
3486 -- Default name may be overloaded, in which case the interpretation
3487 -- with the correct profile must be selected, as for a renaming.
3488 -- If the definition is an indexed component, it must denote a
3489 -- member of an entry family. If it is a selected component, it
3490 -- can be a protected operation.
3492 if Etype (Def) = Any_Type then
3493 goto Leave;
3495 elsif Nkind (Def) = N_Selected_Component then
3496 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3497 Error_Msg_N ("expect valid subprogram name as default", Def);
3498 end if;
3500 elsif Nkind (Def) = N_Indexed_Component then
3501 if Is_Entity_Name (Prefix (Def)) then
3502 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3503 Error_Msg_N ("expect valid subprogram name as default", Def);
3504 end if;
3506 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3507 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3508 E_Entry_Family
3509 then
3510 Error_Msg_N ("expect valid subprogram name as default", Def);
3511 end if;
3513 else
3514 Error_Msg_N ("expect valid subprogram name as default", Def);
3515 goto Leave;
3516 end if;
3518 elsif Nkind (Def) = N_Character_Literal then
3520 -- Needs some type checks: subprogram should be parameterless???
3522 Resolve (Def, (Etype (Nam)));
3524 elsif not Is_Entity_Name (Def)
3525 or else not Is_Overloadable (Entity (Def))
3526 then
3527 Error_Msg_N ("expect valid subprogram name as default", Def);
3528 goto Leave;
3530 elsif not Is_Overloaded (Def) then
3531 Subp := Entity (Def);
3533 if Subp = Nam then
3534 Error_Msg_N ("premature usage of formal subprogram", Def);
3536 elsif not Entity_Matches_Spec (Subp, Nam) then
3537 Error_Msg_N ("no visible entity matches specification", Def);
3538 end if;
3540 -- More than one interpretation, so disambiguate as for a renaming
3542 else
3543 declare
3544 I : Interp_Index;
3545 I1 : Interp_Index := 0;
3546 It : Interp;
3547 It1 : Interp;
3549 begin
3550 Subp := Any_Id;
3551 Get_First_Interp (Def, I, It);
3552 while Present (It.Nam) loop
3553 if Entity_Matches_Spec (It.Nam, Nam) then
3554 if Subp /= Any_Id then
3555 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3557 if It1 = No_Interp then
3558 Error_Msg_N ("ambiguous default subprogram", Def);
3559 else
3560 Subp := It1.Nam;
3561 end if;
3563 exit;
3565 else
3566 I1 := I;
3567 Subp := It.Nam;
3568 end if;
3569 end if;
3571 Get_Next_Interp (I, It);
3572 end loop;
3573 end;
3575 if Subp /= Any_Id then
3577 -- Subprogram found, generate reference to it
3579 Set_Entity (Def, Subp);
3580 Generate_Reference (Subp, Def);
3582 if Subp = Nam then
3583 Error_Msg_N ("premature usage of formal subprogram", Def);
3585 elsif Ekind (Subp) /= E_Operator then
3586 Check_Mode_Conformant (Subp, Nam);
3587 end if;
3589 else
3590 Error_Msg_N ("no visible subprogram matches specification", N);
3591 end if;
3592 end if;
3594 -- When extensions are enabled, an expression can be given as default
3595 -- for a formal function. The expression must be of the function result
3596 -- type and can reference formal parameters of the function.
3598 elsif Present (Expr) then
3599 Push_Scope (Nam);
3600 Install_Formals (Nam);
3601 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3602 End_Scope;
3603 end if;
3605 <<Leave>>
3606 Analyze_Aspect_Specifications (N, Nam);
3608 if Parent_Installed then
3609 Remove_Parent;
3610 end if;
3611 end Analyze_Formal_Subprogram_Declaration;
3613 -------------------------------------
3614 -- Analyze_Formal_Type_Declaration --
3615 -------------------------------------
3617 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3618 Def : constant Node_Id := Formal_Type_Definition (N);
3620 Parent_Installed : Boolean := False;
3621 T : Entity_Id;
3623 begin
3624 T := Defining_Identifier (N);
3626 if Present (Discriminant_Specifications (N))
3627 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3628 then
3629 Error_Msg_N
3630 ("discriminants not allowed for this formal type", T);
3631 end if;
3633 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3635 -- Enter the new name, and branch to specific routine
3637 case Nkind (Def) is
3638 when N_Formal_Private_Type_Definition =>
3639 Analyze_Formal_Private_Type (N, T, Def);
3641 when N_Formal_Derived_Type_Definition =>
3642 Analyze_Formal_Derived_Type (N, T, Def);
3644 when N_Formal_Incomplete_Type_Definition =>
3645 Analyze_Formal_Incomplete_Type (T, Def);
3647 when N_Formal_Discrete_Type_Definition =>
3648 Analyze_Formal_Discrete_Type (T, Def);
3650 when N_Formal_Signed_Integer_Type_Definition =>
3651 Analyze_Formal_Signed_Integer_Type (T, Def);
3653 when N_Formal_Modular_Type_Definition =>
3654 Analyze_Formal_Modular_Type (T, Def);
3656 when N_Formal_Floating_Point_Definition =>
3657 Analyze_Formal_Floating_Type (T, Def);
3659 when N_Formal_Ordinary_Fixed_Point_Definition =>
3660 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3662 when N_Formal_Decimal_Fixed_Point_Definition =>
3663 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3665 when N_Array_Type_Definition =>
3666 Analyze_Formal_Array_Type (T, Def);
3668 when N_Access_Function_Definition
3669 | N_Access_Procedure_Definition
3670 | N_Access_To_Object_Definition
3672 Analyze_Generic_Access_Type (T, Def);
3674 -- Ada 2005: a interface declaration is encoded as an abstract
3675 -- record declaration or a abstract type derivation.
3677 when N_Record_Definition =>
3678 Analyze_Formal_Interface_Type (N, T, Def);
3680 when N_Derived_Type_Definition =>
3681 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3683 when N_Error =>
3684 null;
3686 when others =>
3687 raise Program_Error;
3688 end case;
3690 -- A formal type declaration declares a type and its first
3691 -- subtype.
3693 Set_Is_Generic_Type (T);
3694 Set_Is_First_Subtype (T);
3696 if Present (Default_Subtype_Mark (Original_Node (N))) then
3697 Validate_Formal_Type_Default (N);
3698 end if;
3700 Analyze_Aspect_Specifications (N, T);
3702 if Parent_Installed then
3703 Remove_Parent;
3704 end if;
3705 end Analyze_Formal_Type_Declaration;
3707 ------------------------------------
3708 -- Analyze_Function_Instantiation --
3709 ------------------------------------
3711 procedure Analyze_Function_Instantiation (N : Node_Id) is
3712 begin
3713 Analyze_Subprogram_Instantiation (N, E_Function);
3714 end Analyze_Function_Instantiation;
3716 ---------------------------------
3717 -- Analyze_Generic_Access_Type --
3718 ---------------------------------
3720 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3721 begin
3722 Enter_Name (T);
3724 if Nkind (Def) = N_Access_To_Object_Definition then
3725 Access_Type_Declaration (T, Def);
3727 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3728 and then No (Full_View (Designated_Type (T)))
3729 and then not Is_Generic_Type (Designated_Type (T))
3730 then
3731 Error_Msg_N ("premature usage of incomplete type", Def);
3733 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3734 Error_Msg_N
3735 ("only a subtype mark is allowed in a formal", Def);
3736 end if;
3738 else
3739 Access_Subprogram_Declaration (T, Def);
3740 end if;
3741 end Analyze_Generic_Access_Type;
3743 ---------------------------------
3744 -- Analyze_Generic_Formal_Part --
3745 ---------------------------------
3747 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3748 Gen_Parm_Decl : Node_Id;
3750 begin
3751 -- The generic formals are processed in the scope of the generic unit,
3752 -- where they are immediately visible. The scope is installed by the
3753 -- caller.
3755 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3756 while Present (Gen_Parm_Decl) loop
3757 Analyze (Gen_Parm_Decl);
3758 Next (Gen_Parm_Decl);
3759 end loop;
3761 Generate_Reference_To_Generic_Formals (Current_Scope);
3763 -- For Ada 2022, some formal parameters can carry aspects, which must
3764 -- be name-resolved at the end of the list of formal parameters (which
3765 -- has the semantics of a declaration list).
3767 Analyze_Contracts (Generic_Formal_Declarations (N));
3768 end Analyze_Generic_Formal_Part;
3770 ------------------------------------------
3771 -- Analyze_Generic_Package_Declaration --
3772 ------------------------------------------
3774 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3775 Decls : constant List_Id := Visible_Declarations (Specification (N));
3776 Loc : constant Source_Ptr := Sloc (N);
3778 Decl : Node_Id;
3779 Id : Entity_Id;
3780 New_N : Node_Id;
3781 Renaming : Node_Id;
3782 Save_Parent : Node_Id;
3784 begin
3785 -- A generic may grant access to its private enclosing context depending
3786 -- on the placement of its corresponding body. From elaboration point of
3787 -- view, the flow of execution may enter this private context, and then
3788 -- reach an external unit, thus producing a dependency on that external
3789 -- unit. For such a path to be properly discovered and encoded in the
3790 -- ALI file of the main unit, let the ABE mechanism process the body of
3791 -- the main unit, and encode all relevant invocation constructs and the
3792 -- relations between them.
3794 Mark_Save_Invocation_Graph_Of_Body;
3796 -- We introduce a renaming of the enclosing package, to have a usable
3797 -- entity as the prefix of an expanded name for a local entity of the
3798 -- form Par.P.Q, where P is the generic package. This is because a local
3799 -- entity named P may hide it, so that the usual visibility rules in
3800 -- the instance will not resolve properly.
3802 Renaming :=
3803 Make_Package_Renaming_Declaration (Loc,
3804 Defining_Unit_Name =>
3805 Make_Defining_Identifier (Loc,
3806 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3807 Name =>
3808 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3810 -- The declaration is inserted before other declarations, but before
3811 -- pragmas that may be library-unit pragmas and must appear before other
3812 -- declarations. The pragma Compile_Time_Error is not in this class, and
3813 -- may contain an expression that includes such a qualified name, so the
3814 -- renaming declaration must appear before it.
3816 -- Are there other pragmas that require this special handling ???
3818 if Present (Decls) then
3819 Decl := First (Decls);
3820 while Present (Decl)
3821 and then Nkind (Decl) = N_Pragma
3822 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3823 loop
3824 Next (Decl);
3825 end loop;
3827 if Present (Decl) then
3828 Insert_Before (Decl, Renaming);
3829 else
3830 Append (Renaming, Visible_Declarations (Specification (N)));
3831 end if;
3833 else
3834 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3835 end if;
3837 -- Create copy of generic unit, and save for instantiation. If the unit
3838 -- is a child unit, do not copy the specifications for the parent, which
3839 -- are not part of the generic tree.
3841 Save_Parent := Parent_Spec (N);
3842 Set_Parent_Spec (N, Empty);
3844 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3845 Set_Parent_Spec (New_N, Save_Parent);
3846 Rewrite (N, New_N);
3848 -- Collect all contract-related source pragmas found within the template
3849 -- and attach them to the contract of the package spec. This contract is
3850 -- used in the capture of global references within annotations.
3852 Create_Generic_Contract (N);
3854 Id := Defining_Entity (N);
3855 Generate_Definition (Id);
3857 -- Expansion is not applied to generic units
3859 Start_Generic;
3861 Enter_Name (Id);
3862 Mutate_Ekind (Id, E_Generic_Package);
3863 Set_Is_Not_Self_Hidden (Id);
3864 Set_Etype (Id, Standard_Void_Type);
3866 -- Set SPARK_Mode from context
3868 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3869 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3870 Set_SPARK_Pragma_Inherited (Id);
3871 Set_SPARK_Aux_Pragma_Inherited (Id);
3873 -- Preserve relevant elaboration-related attributes of the context which
3874 -- are no longer available or very expensive to recompute once analysis,
3875 -- resolution, and expansion are over.
3877 Mark_Elaboration_Attributes
3878 (N_Id => Id,
3879 Checks => True,
3880 Warnings => True);
3882 -- Analyze aspects now, so that generated pragmas appear in the
3883 -- declarations before building and analyzing the generic copy.
3885 Analyze_Aspect_Specifications (N, Id);
3887 Push_Scope (Id);
3888 Enter_Generic_Scope (Id);
3889 Set_Inner_Instances (Id, New_Elmt_List);
3891 Set_Categorization_From_Pragmas (N);
3892 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3894 -- Link the declaration of the generic homonym in the generic copy to
3895 -- the package it renames, so that it is always resolved properly.
3897 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3898 Set_Entity (Associated_Node (Name (Renaming)), Id);
3900 -- For a library unit, we have reconstructed the entity for the unit,
3901 -- and must reset it in the library tables.
3903 if Nkind (Parent (N)) = N_Compilation_Unit then
3904 Set_Cunit_Entity (Current_Sem_Unit, Id);
3905 end if;
3907 Analyze_Generic_Formal_Part (N);
3909 -- After processing the generic formals, analysis proceeds as for a
3910 -- non-generic package.
3912 Analyze (Specification (N));
3914 Validate_Categorization_Dependency (N, Id);
3916 End_Generic;
3918 End_Package_Scope (Id);
3919 Exit_Generic_Scope (Id);
3921 -- If the generic appears within a package unit, the body of that unit
3922 -- has to be present for instantiation and inlining.
3924 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3925 Set_Body_Needed_For_Inlining
3926 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3927 end if;
3929 if Nkind (Parent (N)) /= N_Compilation_Unit then
3930 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3931 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3932 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3934 else
3935 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3936 Validate_RT_RAT_Component (N);
3938 -- If this is a spec without a body, check that generic parameters
3939 -- are referenced.
3941 if not Body_Required (Parent (N)) then
3942 Check_References (Id);
3943 end if;
3944 end if;
3946 -- If there is a specified storage pool in the context, create an
3947 -- aspect on the package declaration, so that it is used in any
3948 -- instance that does not override it.
3950 if Present (Default_Pool) then
3951 declare
3952 ASN : Node_Id;
3954 begin
3955 ASN :=
3956 Make_Aspect_Specification (Loc,
3957 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3958 Expression => New_Copy (Default_Pool));
3960 if No (Aspect_Specifications (Specification (N))) then
3961 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3962 else
3963 Append (ASN, Aspect_Specifications (Specification (N)));
3964 end if;
3965 end;
3966 end if;
3967 end Analyze_Generic_Package_Declaration;
3969 --------------------------------------------
3970 -- Analyze_Generic_Subprogram_Declaration --
3971 --------------------------------------------
3973 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3974 Formals : List_Id;
3975 Id : Entity_Id;
3976 New_N : Node_Id;
3977 Result_Type : Entity_Id;
3978 Save_Parent : Node_Id;
3979 Spec : Node_Id;
3980 Typ : Entity_Id;
3982 begin
3983 -- A generic may grant access to its private enclosing context depending
3984 -- on the placement of its corresponding body. From elaboration point of
3985 -- view, the flow of execution may enter this private context, and then
3986 -- reach an external unit, thus producing a dependency on that external
3987 -- unit. For such a path to be properly discovered and encoded in the
3988 -- ALI file of the main unit, let the ABE mechanism process the body of
3989 -- the main unit, and encode all relevant invocation constructs and the
3990 -- relations between them.
3992 Mark_Save_Invocation_Graph_Of_Body;
3994 -- Create copy of generic unit, and save for instantiation. If the unit
3995 -- is a child unit, do not copy the specifications for the parent, which
3996 -- are not part of the generic tree.
3998 Save_Parent := Parent_Spec (N);
3999 Set_Parent_Spec (N, Empty);
4001 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4002 Set_Parent_Spec (New_N, Save_Parent);
4003 Rewrite (N, New_N);
4005 -- Collect all contract-related source pragmas found within the template
4006 -- and attach them to the contract of the subprogram spec. This contract
4007 -- is used in the capture of global references within annotations.
4009 Create_Generic_Contract (N);
4011 Spec := Specification (N);
4012 Id := Defining_Entity (Spec);
4013 Generate_Definition (Id);
4015 if Nkind (Id) = N_Defining_Operator_Symbol then
4016 Error_Msg_N
4017 ("operator symbol not allowed for generic subprogram", Id);
4018 end if;
4020 Start_Generic;
4022 Enter_Name (Id);
4023 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4025 Push_Scope (Id);
4026 Enter_Generic_Scope (Id);
4027 Set_Inner_Instances (Id, New_Elmt_List);
4028 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4030 Analyze_Generic_Formal_Part (N);
4032 if Nkind (Spec) = N_Function_Specification then
4033 Mutate_Ekind (Id, E_Generic_Function);
4034 else
4035 Mutate_Ekind (Id, E_Generic_Procedure);
4036 end if;
4038 -- Set SPARK_Mode from context
4040 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4041 Set_SPARK_Pragma_Inherited (Id);
4043 -- Preserve relevant elaboration-related attributes of the context which
4044 -- are no longer available or very expensive to recompute once analysis,
4045 -- resolution, and expansion are over.
4047 Mark_Elaboration_Attributes
4048 (N_Id => Id,
4049 Checks => True,
4050 Warnings => True);
4052 Formals := Parameter_Specifications (Spec);
4054 if Present (Formals) then
4055 Process_Formals (Formals, Spec);
4056 end if;
4058 if Nkind (Spec) = N_Function_Specification then
4059 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4060 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4061 Set_Etype (Id, Result_Type);
4063 -- Check restriction imposed by AI05-073: a generic function
4064 -- cannot return an abstract type or an access to such.
4066 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4067 Error_Msg_N
4068 ("generic function cannot have an access result "
4069 & "that designates an abstract type", Spec);
4070 end if;
4072 else
4073 Find_Type (Result_Definition (Spec));
4074 Typ := Entity (Result_Definition (Spec));
4076 if Is_Abstract_Type (Typ)
4077 and then Ada_Version >= Ada_2012
4078 then
4079 Error_Msg_N
4080 ("generic function cannot have abstract result type", Spec);
4081 end if;
4083 -- If a null exclusion is imposed on the result type, then create
4084 -- a null-excluding itype (an access subtype) and use it as the
4085 -- function's Etype.
4087 if Is_Access_Type (Typ)
4088 and then Null_Exclusion_Present (Spec)
4089 then
4090 Set_Etype (Id,
4091 Create_Null_Excluding_Itype
4092 (T => Typ,
4093 Related_Nod => Spec,
4094 Scope_Id => Defining_Unit_Name (Spec)));
4095 else
4096 Set_Etype (Id, Typ);
4097 end if;
4098 end if;
4100 else
4101 Set_Etype (Id, Standard_Void_Type);
4102 end if;
4104 Set_Is_Not_Self_Hidden (Id);
4106 -- Analyze the aspects of the generic copy to ensure that all generated
4107 -- pragmas (if any) perform their semantic effects.
4109 Analyze_Aspect_Specifications (N, Id);
4111 -- For a library unit, we have reconstructed the entity for the unit,
4112 -- and must reset it in the library tables. We also make sure that
4113 -- Body_Required is set properly in the original compilation unit node.
4115 if Nkind (Parent (N)) = N_Compilation_Unit then
4116 Set_Cunit_Entity (Current_Sem_Unit, Id);
4117 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4118 end if;
4120 -- If the generic appears within a package unit, the body of that unit
4121 -- has to be present for instantiation and inlining.
4123 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4124 and then Unit_Requires_Body (Id)
4125 then
4126 Set_Body_Needed_For_Inlining
4127 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4128 end if;
4130 Set_Categorization_From_Pragmas (N);
4131 Validate_Categorization_Dependency (N, Id);
4133 -- Capture all global references that occur within the profile of the
4134 -- generic subprogram. Aspects are not part of this processing because
4135 -- they must be delayed. If processed now, Save_Global_References will
4136 -- destroy the Associated_Node links and prevent the capture of global
4137 -- references when the contract of the generic subprogram is analyzed.
4139 Save_Global_References (Original_Node (N));
4141 End_Generic;
4142 End_Scope;
4143 Exit_Generic_Scope (Id);
4144 Generate_Reference_To_Formals (Id);
4146 List_Inherited_Pre_Post_Aspects (Id);
4147 end Analyze_Generic_Subprogram_Declaration;
4149 -----------------------------------
4150 -- Analyze_Package_Instantiation --
4151 -----------------------------------
4153 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4154 -- must be replaced by gotos which jump to the end of the routine in order
4155 -- to restore the Ghost and SPARK modes.
4157 procedure Analyze_Package_Instantiation (N : Node_Id) is
4158 Has_Inline_Always : Boolean := False;
4159 -- Set if the generic unit contains any subprograms with Inline_Always.
4160 -- Only relevant when back-end inlining is not enabled.
4162 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4163 -- Return True if inlining is active and Gen_Unit contains inlined
4164 -- subprograms. In this case, we may either instantiate the body when
4165 -- front-end inlining is enabled, or add a pending instantiation when
4166 -- back-end inlining is enabled. In the former case, this may cause
4167 -- superfluous instantiations, but in either case we need to perform
4168 -- the instantiation of the body in the context of the instance and
4169 -- not in that of the point of inlining.
4171 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4172 -- Return True if Gen_Unit needs to have its body instantiated in the
4173 -- context of N. This in particular excludes generic contexts.
4175 -----------------------
4176 -- Might_Inline_Subp --
4177 -----------------------
4179 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4180 E : Entity_Id;
4182 begin
4183 if Inline_Processing_Required then
4184 -- No need to recompute the answer if we know it is positive
4185 -- and back-end inlining is enabled.
4187 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4188 return True;
4189 end if;
4191 E := First_Entity (Gen_Unit);
4192 while Present (E) loop
4193 if Is_Subprogram (E) and then Is_Inlined (E) then
4194 -- Remember if there are any subprograms with Inline_Always
4196 if Has_Pragma_Inline_Always (E) then
4197 Has_Inline_Always := True;
4198 end if;
4200 Set_Is_Inlined (Gen_Unit);
4201 return True;
4202 end if;
4204 Next_Entity (E);
4205 end loop;
4206 end if;
4208 return False;
4209 end Might_Inline_Subp;
4211 -------------------------------
4212 -- Needs_Body_Instantiated --
4213 -------------------------------
4215 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4216 begin
4217 -- No need to instantiate bodies in generic units
4219 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4220 return False;
4221 end if;
4223 -- If the instantiation is in the main unit, then the body is needed
4225 if Is_In_Main_Unit (N) then
4226 return True;
4227 end if;
4229 -- In GNATprove mode, never instantiate bodies outside of the main
4230 -- unit, as it does not use frontend/backend inlining in the way that
4231 -- GNAT does, so does not benefit from such instantiations. On the
4232 -- contrary, such instantiations may bring artificial constraints,
4233 -- as for example such bodies may require preprocessing.
4235 if GNATprove_Mode then
4236 return False;
4237 end if;
4239 -- If not, then again no need to instantiate bodies in generic units
4241 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4242 return False;
4243 end if;
4245 -- Here we have a special handling for back-end inlining: if inline
4246 -- processing is required, then we unconditionally want to have the
4247 -- body instantiated. The reason is that Might_Inline_Subp does not
4248 -- catch all the cases (as it does not recurse into nested packages)
4249 -- so this avoids the need to patch things up afterwards. Moreover,
4250 -- these instantiations are only performed on demand when back-end
4251 -- inlining is enabled, so this causes very little extra work.
4253 if Inline_Processing_Required and then Back_End_Inlining then
4254 return True;
4255 end if;
4257 -- We want to have the bodies instantiated in non-main units if
4258 -- they might contribute inlined subprograms.
4260 return Might_Inline_Subp (Gen_Unit);
4261 end Needs_Body_Instantiated;
4263 -- Local declarations
4265 Gen_Id : constant Node_Id := Name (N);
4266 Inst_Id : constant Entity_Id := Defining_Entity (N);
4267 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4268 Loc : constant Source_Ptr := Sloc (N);
4270 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4271 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4272 Saved_ISMP : constant Boolean :=
4273 Ignore_SPARK_Mode_Pragmas_In_Instance;
4274 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4275 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4276 -- Save the Ghost and SPARK mode-related data to restore on exit
4278 Saved_Style_Check : constant Boolean := Style_Check;
4279 -- Save style check mode for restore on exit
4281 Act_Decl : Node_Id;
4282 Act_Decl_Name : Node_Id;
4283 Act_Decl_Id : Entity_Id;
4284 Act_Spec : Node_Id;
4285 Act_Tree : Node_Id;
4286 Env_Installed : Boolean := False;
4287 Gen_Decl : Node_Id;
4288 Gen_Spec : Node_Id;
4289 Gen_Unit : Entity_Id;
4290 Inline_Now : Boolean := False;
4291 Needs_Body : Boolean;
4292 Parent_Installed : Boolean := False;
4293 Renaming_List : List_Id;
4294 Unit_Renaming : Node_Id;
4296 Vis_Prims_List : Elist_Id := No_Elist;
4297 -- List of primitives made temporarily visible in the instantiation
4298 -- to match the visibility of the formal type
4300 -- Start of processing for Analyze_Package_Instantiation
4302 begin
4303 -- Preserve relevant elaboration-related attributes of the context which
4304 -- are no longer available or very expensive to recompute once analysis,
4305 -- resolution, and expansion are over.
4307 Mark_Elaboration_Attributes
4308 (N_Id => N,
4309 Checks => True,
4310 Level => True,
4311 Modes => True,
4312 Warnings => True);
4314 -- Very first thing: check for Text_IO special unit in case we are
4315 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4317 Check_Text_IO_Special_Unit (Name (N));
4319 -- Make node global for error reporting
4321 Instantiation_Node := N;
4323 -- Case of instantiation of a generic package
4325 if Nkind (N) = N_Package_Instantiation then
4326 Act_Decl_Id := New_Copy (Defining_Entity (N));
4328 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4329 Act_Decl_Name :=
4330 Make_Defining_Program_Unit_Name (Loc,
4331 Name =>
4332 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4333 Defining_Identifier => Act_Decl_Id);
4334 else
4335 Act_Decl_Name := Act_Decl_Id;
4336 end if;
4338 -- Case of instantiation of a formal package
4340 else
4341 Act_Decl_Id := Defining_Identifier (N);
4342 Act_Decl_Name := Act_Decl_Id;
4343 end if;
4345 Generate_Definition (Act_Decl_Id);
4346 Mutate_Ekind (Act_Decl_Id, E_Package);
4347 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4349 -- Initialize list of incomplete actuals before analysis
4351 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4353 Preanalyze_Actuals (N, Act_Decl_Id);
4355 -- Turn off style checking in instances. If the check is enabled on the
4356 -- generic unit, a warning in an instance would just be noise. If not
4357 -- enabled on the generic, then a warning in an instance is just wrong.
4358 -- This must be done after analyzing the actuals, which do come from
4359 -- source and are subject to style checking.
4361 Style_Check := False;
4363 Init_Env;
4364 Env_Installed := True;
4366 -- Reset renaming map for formal types. The mapping is established
4367 -- when analyzing the generic associations, but some mappings are
4368 -- inherited from formal packages of parent units, and these are
4369 -- constructed when the parents are installed.
4371 Generic_Renamings.Set_Last (0);
4372 Generic_Renamings_HTable.Reset;
4374 -- Except for an abbreviated instance created to check a formal package,
4375 -- install the parent if this is a generic child unit.
4377 if not Is_Abbreviated_Instance (Inst_Id) then
4378 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4379 end if;
4381 Gen_Unit := Entity (Gen_Id);
4383 -- A package instantiation is Ghost when it is subject to pragma Ghost
4384 -- or the generic template is Ghost. Set the mode now to ensure that
4385 -- any nodes generated during analysis and expansion are marked as
4386 -- Ghost.
4388 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4390 -- Verify that it is the name of a generic package
4392 -- A visibility glitch: if the instance is a child unit and the generic
4393 -- is the generic unit of a parent instance (i.e. both the parent and
4394 -- the child units are instances of the same package) the name now
4395 -- denotes the renaming within the parent, not the intended generic
4396 -- unit. See if there is a homonym that is the desired generic. The
4397 -- renaming declaration must be visible inside the instance of the
4398 -- child, but not when analyzing the name in the instantiation itself.
4400 if Ekind (Gen_Unit) = E_Package
4401 and then Present (Renamed_Entity (Gen_Unit))
4402 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4403 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4404 and then Present (Homonym (Gen_Unit))
4405 then
4406 Gen_Unit := Homonym (Gen_Unit);
4407 end if;
4409 if Etype (Gen_Unit) = Any_Type then
4410 Restore_Env;
4411 goto Leave;
4413 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4415 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4417 if From_Limited_With (Gen_Unit) then
4418 Error_Msg_N
4419 ("cannot instantiate a limited withed package", Gen_Id);
4420 else
4421 Error_Msg_NE
4422 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4423 end if;
4425 Restore_Env;
4426 goto Leave;
4427 end if;
4429 if In_Extended_Main_Source_Unit (N) then
4430 Set_Is_Instantiated (Gen_Unit);
4431 Generate_Reference (Gen_Unit, N);
4433 if Present (Renamed_Entity (Gen_Unit)) then
4434 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4435 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4436 end if;
4437 end if;
4439 if Nkind (Gen_Id) = N_Identifier
4440 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4441 then
4442 Error_Msg_NE
4443 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4445 elsif Nkind (Gen_Id) = N_Expanded_Name
4446 and then Is_Child_Unit (Gen_Unit)
4447 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4448 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4449 then
4450 Error_Msg_N
4451 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4452 end if;
4454 Set_Entity (Gen_Id, Gen_Unit);
4456 -- If generic is a renaming, get original generic unit
4458 if Present (Renamed_Entity (Gen_Unit))
4459 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4460 then
4461 Gen_Unit := Renamed_Entity (Gen_Unit);
4462 end if;
4464 -- Verify that there are no circular instantiations
4466 if In_Open_Scopes (Gen_Unit) then
4467 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4468 Restore_Env;
4469 goto Leave;
4471 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4472 Error_Msg_Node_2 := Current_Scope;
4473 Error_Msg_NE
4474 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4475 Circularity_Detected := True;
4476 Restore_Env;
4477 goto Leave;
4479 else
4480 Mutate_Ekind (Inst_Id, E_Package);
4481 Set_Scope (Inst_Id, Current_Scope);
4483 -- If the context of the instance is subject to SPARK_Mode "off" or
4484 -- the annotation is altogether missing, set the global flag which
4485 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4486 -- the instance.
4488 if SPARK_Mode /= On then
4489 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4491 -- Mark the instance spec in case the body is instantiated at a
4492 -- later pass. This preserves the original context in effect for
4493 -- the body.
4495 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4496 end if;
4498 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4499 Gen_Spec := Specification (Gen_Decl);
4501 -- Initialize renamings map, for error checking, and the list that
4502 -- holds private entities whose views have changed between generic
4503 -- definition and instantiation. If this is the instance created to
4504 -- validate an actual package, the instantiation environment is that
4505 -- of the enclosing instance.
4507 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4509 -- Copy original generic tree, to produce text for instantiation
4511 Act_Tree :=
4512 Copy_Generic_Node
4513 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4515 Act_Spec := Specification (Act_Tree);
4517 -- If this is the instance created to validate an actual package,
4518 -- only the formals matter, do not examine the package spec itself.
4520 if Is_Actual_Pack then
4521 Set_Visible_Declarations (Act_Spec, New_List);
4522 Set_Private_Declarations (Act_Spec, New_List);
4523 end if;
4525 Renaming_List :=
4526 Analyze_Associations
4527 (I_Node => N,
4528 Formals => Generic_Formal_Declarations (Act_Tree),
4529 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4531 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4533 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4534 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4535 Set_Is_Generic_Instance (Act_Decl_Id);
4536 Set_Generic_Parent (Act_Spec, Gen_Unit);
4538 -- References to the generic in its own declaration or its body are
4539 -- references to the instance. Add a renaming declaration for the
4540 -- generic unit itself. This declaration, as well as the renaming
4541 -- declarations for the generic formals, must remain private to the
4542 -- unit: the formals, because this is the language semantics, and
4543 -- the unit because its use is an artifact of the implementation.
4545 Unit_Renaming :=
4546 Make_Package_Renaming_Declaration (Loc,
4547 Defining_Unit_Name =>
4548 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4549 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4551 Append (Unit_Renaming, Renaming_List);
4553 -- The renaming declarations are the first local declarations of the
4554 -- new unit.
4556 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4557 Insert_List_Before
4558 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4559 else
4560 Set_Visible_Declarations (Act_Spec, Renaming_List);
4561 end if;
4563 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4565 -- Propagate the aspect specifications from the package declaration
4566 -- template to the instantiated version of the package declaration.
4568 if Has_Aspects (Act_Tree) then
4569 Set_Aspect_Specifications (Act_Decl,
4570 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4571 end if;
4573 -- The generic may have a generated Default_Storage_Pool aspect,
4574 -- set at the point of generic declaration. If the instance has
4575 -- that aspect, it overrides the one inherited from the generic.
4577 if Has_Aspects (Gen_Spec) then
4578 if No (Aspect_Specifications (N)) then
4579 Set_Aspect_Specifications (N,
4580 (New_Copy_List_Tree
4581 (Aspect_Specifications (Gen_Spec))));
4583 else
4584 declare
4585 Inherited_Aspects : constant List_Id :=
4586 New_Copy_List_Tree
4587 (Aspect_Specifications (Gen_Spec));
4589 ASN1 : Node_Id;
4590 ASN2 : Node_Id;
4591 Pool_Present : Boolean := False;
4593 begin
4594 ASN1 := First (Aspect_Specifications (N));
4595 while Present (ASN1) loop
4596 if Chars (Identifier (ASN1)) =
4597 Name_Default_Storage_Pool
4598 then
4599 Pool_Present := True;
4600 exit;
4601 end if;
4603 Next (ASN1);
4604 end loop;
4606 if Pool_Present then
4608 -- If generic carries a default storage pool, remove it
4609 -- in favor of the instance one.
4611 ASN2 := First (Inherited_Aspects);
4612 while Present (ASN2) loop
4613 if Chars (Identifier (ASN2)) =
4614 Name_Default_Storage_Pool
4615 then
4616 Remove (ASN2);
4617 exit;
4618 end if;
4620 Next (ASN2);
4621 end loop;
4622 end if;
4624 Prepend_List_To
4625 (Aspect_Specifications (N), Inherited_Aspects);
4626 end;
4627 end if;
4628 end if;
4630 -- Save the instantiation node for a subsequent instantiation of the
4631 -- body if there is one and it needs to be instantiated here.
4633 -- We instantiate the body only if we are generating code, or if we
4634 -- are generating cross-reference information, or for GNATprove use.
4636 declare
4637 Enclosing_Body_Present : Boolean := False;
4638 -- If the generic unit is not a compilation unit, then a body may
4639 -- be present in its parent even if none is required. We create a
4640 -- tentative pending instantiation for the body, which will be
4641 -- discarded if none is actually present.
4643 Scop : Entity_Id;
4645 begin
4646 if Scope (Gen_Unit) /= Standard_Standard
4647 and then not Is_Child_Unit (Gen_Unit)
4648 then
4649 Scop := Scope (Gen_Unit);
4650 while Present (Scop) and then Scop /= Standard_Standard loop
4651 if Unit_Requires_Body (Scop) then
4652 Enclosing_Body_Present := True;
4653 exit;
4655 elsif In_Open_Scopes (Scop)
4656 and then In_Package_Body (Scop)
4657 then
4658 Enclosing_Body_Present := True;
4659 exit;
4660 end if;
4662 exit when Is_Compilation_Unit (Scop);
4663 Scop := Scope (Scop);
4664 end loop;
4665 end if;
4667 -- If front-end inlining is enabled or there are any subprograms
4668 -- marked with Inline_Always, and this is a unit for which code
4669 -- will be generated, we instantiate the body at once.
4671 -- This is done if the instance is not the main unit, and if the
4672 -- generic is not a child unit of another generic, to avoid scope
4673 -- problems and the reinstallation of parent instances.
4675 if Expander_Active
4676 and then (not Is_Child_Unit (Gen_Unit)
4677 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4678 and then Might_Inline_Subp (Gen_Unit)
4679 and then not Is_Actual_Pack
4680 then
4681 if not Back_End_Inlining
4682 and then (Front_End_Inlining or else Has_Inline_Always)
4683 and then (Is_In_Main_Unit (N)
4684 or else In_Main_Context (Current_Scope))
4685 and then Nkind (Parent (N)) /= N_Compilation_Unit
4686 then
4687 Inline_Now := True;
4689 -- In configurable_run_time mode we force the inlining of
4690 -- predefined subprograms marked Inline_Always, to minimize
4691 -- the use of the run-time library.
4693 elsif In_Predefined_Unit (Gen_Decl)
4694 and then Configurable_Run_Time_Mode
4695 and then Nkind (Parent (N)) /= N_Compilation_Unit
4696 then
4697 Inline_Now := True;
4698 end if;
4700 -- If the current scope is itself an instance within a child
4701 -- unit, there will be duplications in the scope stack, and the
4702 -- unstacking mechanism in Inline_Instance_Body will fail.
4703 -- This loses some rare cases of optimization.
4705 if Is_Generic_Instance (Current_Scope) then
4706 declare
4707 Curr_Unit : constant Entity_Id :=
4708 Cunit_Entity (Current_Sem_Unit);
4709 begin
4710 if Curr_Unit /= Current_Scope
4711 and then Is_Child_Unit (Curr_Unit)
4712 then
4713 Inline_Now := False;
4714 end if;
4715 end;
4716 end if;
4717 end if;
4719 Needs_Body :=
4720 (Unit_Requires_Body (Gen_Unit)
4721 or else Enclosing_Body_Present
4722 or else Present (Corresponding_Body (Gen_Decl)))
4723 and then Needs_Body_Instantiated (Gen_Unit)
4724 and then not Is_Actual_Pack
4725 and then not Inline_Now
4726 and then (Operating_Mode = Generate_Code
4727 or else (Operating_Mode = Check_Semantics
4728 and then GNATprove_Mode));
4730 -- If front-end inlining is enabled or there are any subprograms
4731 -- marked with Inline_Always, do not instantiate body when within
4732 -- a generic context.
4734 if not Back_End_Inlining
4735 and then (Front_End_Inlining or else Has_Inline_Always)
4736 and then not Expander_Active
4737 then
4738 Needs_Body := False;
4739 end if;
4741 -- If the current context is generic, and the package being
4742 -- instantiated is declared within a formal package, there is no
4743 -- body to instantiate until the enclosing generic is instantiated
4744 -- and there is an actual for the formal package. If the formal
4745 -- package has parameters, we build a regular package instance for
4746 -- it, that precedes the original formal package declaration.
4748 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4749 declare
4750 Decl : constant Node_Id :=
4751 Original_Node
4752 (Unit_Declaration_Node (Scope (Gen_Unit)));
4753 begin
4754 if Nkind (Decl) = N_Formal_Package_Declaration
4755 or else (Nkind (Decl) = N_Package_Declaration
4756 and then Is_List_Member (Decl)
4757 and then Present (Next (Decl))
4758 and then
4759 Nkind (Next (Decl)) =
4760 N_Formal_Package_Declaration)
4761 then
4762 Needs_Body := False;
4763 end if;
4764 end;
4765 end if;
4766 end;
4768 -- For RCI unit calling stubs, we omit the instance body if the
4769 -- instance is the RCI library unit itself.
4771 -- However there is a special case for nested instances: in this case
4772 -- we do generate the instance body, as it might be required, e.g.
4773 -- because it provides stream attributes for some type used in the
4774 -- profile of a remote subprogram. This is consistent with 12.3(12),
4775 -- which indicates that the instance body occurs at the place of the
4776 -- instantiation, and thus is part of the RCI declaration, which is
4777 -- present on all client partitions (this is E.2.3(18)).
4779 -- Note that AI12-0002 may make it illegal at some point to have
4780 -- stream attributes defined in an RCI unit, in which case this
4781 -- special case will become unnecessary. In the meantime, there
4782 -- is known application code in production that depends on this
4783 -- being possible, so we definitely cannot eliminate the body in
4784 -- the case of nested instances for the time being.
4786 -- When we generate a nested instance body, calling stubs for any
4787 -- relevant subprogram will be inserted immediately after the
4788 -- subprogram declarations, and will take precedence over the
4789 -- subsequent (original) body. (The stub and original body will be
4790 -- complete homographs, but this is permitted in an instance).
4791 -- (Could we do better and remove the original body???)
4793 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4794 and then Comes_From_Source (N)
4795 and then Nkind (Parent (N)) = N_Compilation_Unit
4796 then
4797 Needs_Body := False;
4798 end if;
4800 -- If the context requires a full instantiation, set things up for
4801 -- subsequent construction of the body.
4803 if Needs_Body then
4804 declare
4805 Fin_Scop, S : Entity_Id;
4807 begin
4808 Check_Forward_Instantiation (Gen_Decl);
4810 Fin_Scop := Empty;
4812 -- For a package instantiation that is not a compilation unit,
4813 -- indicate that cleanup actions of the innermost enclosing
4814 -- scope for which they are generated should be delayed until
4815 -- after the package body is instantiated.
4817 if Nkind (N) = N_Package_Instantiation
4818 and then not Is_Compilation_Unit (Act_Decl_Id)
4819 then
4820 S := Current_Scope;
4822 while S /= Standard_Standard loop
4823 -- Cleanup actions are not generated within generic units
4824 -- or in the formal part of generic units.
4826 if not Expander_Active then
4827 exit;
4829 -- For package scopes, cleanup actions are generated only
4830 -- for compilation units, for spec and body separately.
4832 elsif Ekind (S) = E_Package then
4833 if Is_Compilation_Unit (S) then
4834 if In_Package_Body (S) then
4835 Fin_Scop := Body_Entity (S);
4836 else
4837 Fin_Scop := S;
4838 end if;
4840 Set_Delay_Cleanups (Fin_Scop);
4841 exit;
4843 else
4844 S := Scope (S);
4845 end if;
4847 -- Cleanup actions are generated for all dynamic scopes
4849 else
4850 Fin_Scop := S;
4851 Set_Delay_Cleanups (Fin_Scop);
4852 exit;
4853 end if;
4854 end loop;
4855 end if;
4857 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
4858 end;
4859 end if;
4861 Set_Categorization_From_Pragmas (Act_Decl);
4863 if Parent_Installed then
4864 Hide_Current_Scope;
4865 end if;
4867 Set_Instance_Spec (N, Act_Decl);
4869 -- If not a compilation unit, insert the package declaration before
4870 -- the original instantiation node.
4872 if Nkind (Parent (N)) /= N_Compilation_Unit then
4873 Mark_Rewrite_Insertion (Act_Decl);
4874 Insert_Before (N, Act_Decl);
4876 if Has_Aspects (N) then
4877 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4879 -- The pragma created for a Default_Storage_Pool aspect must
4880 -- appear ahead of the declarations in the instance spec.
4881 -- Analysis has placed it after the instance node, so remove
4882 -- it and reinsert it properly now.
4884 declare
4885 ASN : constant Node_Id := First (Aspect_Specifications (N));
4886 A_Name : constant Name_Id := Chars (Identifier (ASN));
4887 Decl : Node_Id;
4889 begin
4890 if A_Name = Name_Default_Storage_Pool then
4891 if No (Visible_Declarations (Act_Spec)) then
4892 Set_Visible_Declarations (Act_Spec, New_List);
4893 end if;
4895 Decl := Next (N);
4896 while Present (Decl) loop
4897 if Nkind (Decl) = N_Pragma then
4898 Remove (Decl);
4899 Prepend (Decl, Visible_Declarations (Act_Spec));
4900 exit;
4901 end if;
4903 Next (Decl);
4904 end loop;
4905 end if;
4906 end;
4907 end if;
4909 Analyze (Act_Decl);
4911 -- For an instantiation that is a compilation unit, place
4912 -- declaration on current node so context is complete for analysis
4913 -- (including nested instantiations). If this is the main unit,
4914 -- the declaration eventually replaces the instantiation node.
4915 -- If the instance body is created later, it replaces the
4916 -- instance node, and the declaration is attached to it
4917 -- (see Build_Instance_Compilation_Unit_Nodes).
4919 else
4920 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4922 -- The entity for the current unit is the newly created one,
4923 -- and all semantic information is attached to it.
4925 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4927 -- If this is the main unit, replace the main entity as well
4929 if Current_Sem_Unit = Main_Unit then
4930 Main_Unit_Entity := Act_Decl_Id;
4931 end if;
4932 end if;
4934 Set_Unit (Parent (N), Act_Decl);
4935 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4936 Set_Package_Instantiation (Act_Decl_Id, N);
4938 -- Process aspect specifications of the instance node, if any, to
4939 -- take into account categorization pragmas before analyzing the
4940 -- instance.
4942 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4944 Analyze (Act_Decl);
4945 Set_Unit (Parent (N), N);
4946 Set_Body_Required (Parent (N), False);
4948 -- We never need elaboration checks on instantiations, since by
4949 -- definition, the body instantiation is elaborated at the same
4950 -- time as the spec instantiation.
4952 if Legacy_Elaboration_Checks then
4953 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4954 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4955 end if;
4956 end if;
4958 if Legacy_Elaboration_Checks then
4959 Check_Elab_Instantiation (N);
4960 end if;
4962 -- Save the scenario for later examination by the ABE Processing
4963 -- phase.
4965 Record_Elaboration_Scenario (N);
4967 -- The instantiation results in a guaranteed ABE
4969 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4970 -- Do not instantiate the corresponding body because gigi cannot
4971 -- handle certain types of premature instantiations.
4973 Remove_Dead_Instance (N);
4975 -- Create completing bodies for all subprogram declarations since
4976 -- their real bodies will not be instantiated.
4978 Provide_Completing_Bodies (Instance_Spec (N));
4979 end if;
4981 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4983 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4984 First_Private_Entity (Act_Decl_Id));
4986 -- If the instantiation needs a body, the unit will be turned into
4987 -- a package body and receive its own elaboration entity. Otherwise,
4988 -- the nature of the unit is now a package declaration.
4990 -- Note that the below rewriting means that Act_Decl, which has been
4991 -- analyzed and expanded, will be re-expanded as the rewritten N.
4993 if Nkind (Parent (N)) = N_Compilation_Unit
4994 and then not Needs_Body
4995 then
4996 Rewrite (N, Act_Decl);
4997 end if;
4999 if Present (Corresponding_Body (Gen_Decl))
5000 or else Unit_Requires_Body (Gen_Unit)
5001 then
5002 Set_Has_Completion (Act_Decl_Id);
5003 end if;
5005 Check_Formal_Packages (Act_Decl_Id);
5007 Restore_Hidden_Primitives (Vis_Prims_List);
5008 Restore_Private_Views (Act_Decl_Id);
5010 Inherit_Context (Gen_Decl, N);
5012 if Parent_Installed then
5013 Remove_Parent;
5014 end if;
5016 Restore_Env;
5017 Env_Installed := False;
5018 end if;
5020 Validate_Categorization_Dependency (N, Act_Decl_Id);
5022 -- There used to be a check here to prevent instantiations in local
5023 -- contexts if the No_Local_Allocators restriction was active. This
5024 -- check was removed by a binding interpretation in AI-95-00130/07,
5025 -- but we retain the code for documentation purposes.
5027 -- if Ekind (Act_Decl_Id) /= E_Void
5028 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5029 -- then
5030 -- Check_Restriction (No_Local_Allocators, N);
5031 -- end if;
5033 if Inline_Now then
5034 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5035 end if;
5037 -- Check that if N is an instantiation of System.Dim_Float_IO or
5038 -- System.Dim_Integer_IO, the formal type has a dimension system.
5040 if Nkind (N) = N_Package_Instantiation
5041 and then Is_Dim_IO_Package_Instantiation (N)
5042 then
5043 declare
5044 Assoc : constant Node_Id := First (Generic_Associations (N));
5045 begin
5046 if not Has_Dimension_System
5047 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5048 then
5049 Error_Msg_N ("type with a dimension system expected", Assoc);
5050 end if;
5051 end;
5052 end if;
5054 <<Leave>>
5055 if Nkind (Parent (N)) /= N_Compilation_Unit then
5056 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5057 end if;
5059 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5060 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5061 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5062 Style_Check := Saved_Style_Check;
5064 exception
5065 when Instantiation_Error =>
5066 if Parent_Installed then
5067 Remove_Parent;
5068 end if;
5070 if Env_Installed then
5071 Restore_Env;
5072 end if;
5074 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5075 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5076 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5077 Style_Check := Saved_Style_Check;
5078 end Analyze_Package_Instantiation;
5080 --------------------------
5081 -- Inline_Instance_Body --
5082 --------------------------
5084 -- WARNING: This routine manages SPARK regions. Return statements must be
5085 -- replaced by gotos which jump to the end of the routine and restore the
5086 -- SPARK mode.
5088 procedure Inline_Instance_Body
5089 (N : Node_Id;
5090 Gen_Unit : Entity_Id;
5091 Act_Decl : Node_Id)
5093 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5095 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5096 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5097 Gen_Comp : constant Entity_Id :=
5098 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5100 Scope_Stack_Depth : constant Pos :=
5101 Scope_Stack.Last - Scope_Stack.First + 1;
5103 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5104 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5105 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5107 Curr_Scope : Entity_Id := Empty;
5108 List : Elist_Id := No_Elist; -- init to avoid warning
5109 N_Instances : Nat := 0;
5110 Num_Inner : Nat := 0;
5111 Num_Scopes : Nat := 0;
5112 Removed : Boolean := False;
5113 S : Entity_Id;
5114 Vis : Boolean;
5116 begin
5117 -- Case of generic unit defined in another unit. We must remove the
5118 -- complete context of the current unit to install that of the generic.
5120 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5122 -- Loop through enclosing scopes until we reach a generic instance,
5123 -- package body, or subprogram.
5125 S := Current_Scope;
5126 while Present (S) and then S /= Standard_Standard loop
5128 -- Save use clauses from enclosing scopes into Use_Clauses
5130 loop
5131 Num_Scopes := Num_Scopes + 1;
5133 Use_Clauses (Num_Scopes) :=
5134 (Scope_Stack.Table
5135 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5136 End_Use_Clauses (Use_Clauses (Num_Scopes));
5138 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5139 or else Scope_Stack.Table
5140 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5141 end loop;
5143 exit when Is_Generic_Instance (S)
5144 and then (In_Package_Body (S)
5145 or else Ekind (S) = E_Procedure
5146 or else Ekind (S) = E_Function);
5147 S := Scope (S);
5148 end loop;
5150 Vis := Is_Immediately_Visible (Gen_Comp);
5152 -- Find and save all enclosing instances
5154 S := Current_Scope;
5156 while Present (S)
5157 and then S /= Standard_Standard
5158 loop
5159 if Is_Generic_Instance (S) then
5160 N_Instances := N_Instances + 1;
5161 Instances (N_Instances) := S;
5163 exit when In_Package_Body (S);
5164 end if;
5166 S := Scope (S);
5167 end loop;
5169 -- Remove context of current compilation unit, unless we are within a
5170 -- nested package instantiation, in which case the context has been
5171 -- removed previously.
5173 -- If current scope is the body of a child unit, remove context of
5174 -- spec as well. If an enclosing scope is an instance body, the
5175 -- context has already been removed, but the entities in the body
5176 -- must be made invisible as well.
5178 S := Current_Scope;
5179 while Present (S) and then S /= Standard_Standard loop
5180 if Is_Generic_Instance (S)
5181 and then (In_Package_Body (S)
5182 or else Ekind (S) in E_Procedure | E_Function)
5183 then
5184 -- We still have to remove the entities of the enclosing
5185 -- instance from direct visibility.
5187 declare
5188 E : Entity_Id;
5189 begin
5190 E := First_Entity (S);
5191 while Present (E) loop
5192 Set_Is_Immediately_Visible (E, False);
5193 Next_Entity (E);
5194 end loop;
5195 end;
5197 exit;
5198 end if;
5200 if S = Curr_Unit
5201 or else (Ekind (Curr_Unit) = E_Package_Body
5202 and then S = Spec_Entity (Curr_Unit))
5203 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5204 and then S = Corresponding_Spec
5205 (Unit_Declaration_Node (Curr_Unit)))
5206 then
5207 Removed := True;
5209 -- Remove entities in current scopes from visibility, so that
5210 -- instance body is compiled in a clean environment.
5212 List := Save_Scope_Stack (Handle_Use => False);
5214 if Is_Child_Unit (S) then
5216 -- Remove child unit from stack, as well as inner scopes.
5217 -- Removing the context of a child unit removes parent units
5218 -- as well.
5220 while Current_Scope /= S loop
5221 Num_Inner := Num_Inner + 1;
5222 Inner_Scopes (Num_Inner) := Current_Scope;
5223 Pop_Scope;
5224 end loop;
5226 Pop_Scope;
5227 Remove_Context (Curr_Comp);
5228 Curr_Scope := S;
5230 else
5231 Remove_Context (Curr_Comp);
5232 end if;
5234 if Ekind (Curr_Unit) = E_Package_Body then
5235 Remove_Context (Library_Unit (Curr_Comp));
5236 end if;
5237 end if;
5239 S := Scope (S);
5240 end loop;
5242 pragma Assert (Num_Inner < Num_Scopes);
5244 Push_Scope (Standard_Standard);
5245 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5247 -- The inlined package body is analyzed with the configuration state
5248 -- of the context prior to the scope manipulations performed above.
5250 -- ??? shouldn't this also use the warning state of the context prior
5251 -- to the scope manipulations?
5253 Instantiate_Package_Body
5254 (Body_Info =>
5255 ((Inst_Node => N,
5256 Act_Decl => Act_Decl,
5257 Fin_Scop => Empty,
5258 Config_Switches => Config_Attrs,
5259 Current_Sem_Unit => Current_Sem_Unit,
5260 Expander_Status => Expander_Active,
5261 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5262 Scope_Suppress => Scope_Suppress,
5263 Warnings => Save_Warnings)),
5264 Inlined_Body => True);
5266 Pop_Scope;
5268 -- Restore context
5270 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5272 -- Reset Generic_Instance flag so that use clauses can be installed
5273 -- in the proper order. (See Use_One_Package for effect of enclosing
5274 -- instances on processing of use clauses).
5276 for J in 1 .. N_Instances loop
5277 Set_Is_Generic_Instance (Instances (J), False);
5278 end loop;
5280 if Removed then
5281 Install_Context (Curr_Comp, Chain => False);
5283 if Present (Curr_Scope)
5284 and then Is_Child_Unit (Curr_Scope)
5285 then
5286 Push_Scope (Curr_Scope);
5287 Set_Is_Immediately_Visible (Curr_Scope);
5289 -- Finally, restore inner scopes as well
5291 for J in reverse 1 .. Num_Inner loop
5292 Push_Scope (Inner_Scopes (J));
5293 end loop;
5294 end if;
5296 Restore_Scope_Stack (List, Handle_Use => False);
5298 if Present (Curr_Scope)
5299 and then
5300 (In_Private_Part (Curr_Scope)
5301 or else In_Package_Body (Curr_Scope))
5302 then
5303 -- Install private declaration of ancestor units, which are
5304 -- currently available. Restore_Scope_Stack and Install_Context
5305 -- only install the visible part of parents.
5307 declare
5308 Par : Entity_Id;
5309 begin
5310 Par := Scope (Curr_Scope);
5311 while Present (Par) and then Par /= Standard_Standard loop
5312 Install_Private_Declarations (Par);
5313 Par := Scope (Par);
5314 end loop;
5315 end;
5316 end if;
5317 end if;
5319 -- Restore use clauses. For a child unit, use clauses in the parents
5320 -- are restored when installing the context, so only those in inner
5321 -- scopes (and those local to the child unit itself) need to be
5322 -- installed explicitly.
5324 if Is_Child_Unit (Curr_Unit) and then Removed then
5325 for J in reverse 1 .. Num_Inner + 1 loop
5326 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5327 Use_Clauses (J);
5328 Install_Use_Clauses (Use_Clauses (J));
5329 end loop;
5331 else
5332 for J in reverse 1 .. Num_Scopes loop
5333 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5334 Use_Clauses (J);
5335 Install_Use_Clauses (Use_Clauses (J));
5336 end loop;
5337 end if;
5339 -- Restore status of instances. If one of them is a body, make its
5340 -- local entities visible again.
5342 declare
5343 E : Entity_Id;
5344 Inst : Entity_Id;
5346 begin
5347 for J in 1 .. N_Instances loop
5348 Inst := Instances (J);
5349 Set_Is_Generic_Instance (Inst, True);
5351 if In_Package_Body (Inst)
5352 or else Ekind (S) in E_Procedure | E_Function
5353 then
5354 E := First_Entity (Instances (J));
5355 while Present (E) loop
5356 Set_Is_Immediately_Visible (E);
5357 Next_Entity (E);
5358 end loop;
5359 end if;
5360 end loop;
5361 end;
5363 -- If generic unit is in current unit, current context is correct. Note
5364 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5365 -- enclosing scopes were removed.
5367 else
5368 Instantiate_Package_Body
5369 (Body_Info =>
5370 ((Inst_Node => N,
5371 Act_Decl => Act_Decl,
5372 Fin_Scop => Empty,
5373 Config_Switches => Save_Config_Switches,
5374 Current_Sem_Unit => Current_Sem_Unit,
5375 Expander_Status => Expander_Active,
5376 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5377 Scope_Suppress => Scope_Suppress,
5378 Warnings => Save_Warnings)),
5379 Inlined_Body => True);
5380 end if;
5381 end Inline_Instance_Body;
5383 -------------------------------------
5384 -- Analyze_Procedure_Instantiation --
5385 -------------------------------------
5387 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5388 begin
5389 Analyze_Subprogram_Instantiation (N, E_Procedure);
5390 end Analyze_Procedure_Instantiation;
5392 -----------------------------------
5393 -- Need_Subprogram_Instance_Body --
5394 -----------------------------------
5396 function Need_Subprogram_Instance_Body
5397 (N : Node_Id;
5398 Subp : Entity_Id) return Boolean
5400 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5401 -- Return True if E is an inlined subprogram, an inlined renaming or a
5402 -- subprogram nested in an inlined subprogram. The inlining machinery
5403 -- totally disregards nested subprograms since it considers that they
5404 -- will always be compiled if the parent is (see Inline.Is_Nested).
5406 ------------------------------------
5407 -- Is_Inlined_Or_Child_Of_Inlined --
5408 ------------------------------------
5410 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5411 Scop : Entity_Id;
5413 begin
5414 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5415 return True;
5416 end if;
5418 Scop := Scope (E);
5419 while Scop /= Standard_Standard loop
5420 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5421 return True;
5422 end if;
5424 Scop := Scope (Scop);
5425 end loop;
5427 return False;
5428 end Is_Inlined_Or_Child_Of_Inlined;
5430 begin
5431 -- Must be in the main unit or inlined (or child of inlined)
5433 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5435 -- Must be generating code or analyzing code in GNATprove mode
5437 and then (Operating_Mode = Generate_Code
5438 or else (Operating_Mode = Check_Semantics
5439 and then GNATprove_Mode))
5441 -- The body is needed when generating code (full expansion) and in
5442 -- in GNATprove mode (special expansion) for formal verification of
5443 -- the body itself.
5445 and then (Expander_Active or GNATprove_Mode)
5447 -- No point in inlining if ABE is inevitable
5449 and then not Is_Known_Guaranteed_ABE (N)
5451 -- Or if subprogram is eliminated
5453 and then not Is_Eliminated (Subp)
5454 then
5455 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5456 return True;
5458 -- Here if not inlined, or we ignore the inlining
5460 else
5461 return False;
5462 end if;
5463 end Need_Subprogram_Instance_Body;
5465 --------------------------------------
5466 -- Analyze_Subprogram_Instantiation --
5467 --------------------------------------
5469 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5470 -- must be replaced by gotos which jump to the end of the routine in order
5471 -- to restore the Ghost and SPARK modes.
5473 procedure Analyze_Subprogram_Instantiation
5474 (N : Node_Id;
5475 K : Entity_Kind)
5477 Errs : constant Nat := Serious_Errors_Detected;
5478 Gen_Id : constant Node_Id := Name (N);
5479 Inst_Id : constant Entity_Id := Defining_Entity (N);
5480 Anon_Id : constant Entity_Id :=
5481 Make_Defining_Identifier (Sloc (Inst_Id),
5482 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5483 Loc : constant Source_Ptr := Sloc (N);
5485 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5486 Act_Decl : Node_Id;
5487 Act_Spec : Node_Id;
5488 Act_Tree : Node_Id;
5490 Env_Installed : Boolean := False;
5491 Gen_Unit : Entity_Id;
5492 Gen_Decl : Node_Id;
5493 Pack_Id : Entity_Id;
5494 Parent_Installed : Boolean := False;
5496 Renaming_List : List_Id;
5497 -- The list of declarations that link formals and actuals of the
5498 -- instance. These are subtype declarations for formal types, and
5499 -- renaming declarations for other formals. The subprogram declaration
5500 -- for the instance is then appended to the list, and the last item on
5501 -- the list is the renaming declaration for the instance.
5503 procedure Analyze_Instance_And_Renamings;
5504 -- The instance must be analyzed in a context that includes the mappings
5505 -- of generic parameters into actuals. We create a package declaration
5506 -- for this purpose, and a subprogram with an internal name within the
5507 -- package. The subprogram instance is simply an alias for the internal
5508 -- subprogram, declared in the current scope.
5510 procedure Build_Subprogram_Renaming;
5511 -- If the subprogram is recursive, there are occurrences of the name of
5512 -- the generic within the body, which must resolve to the current
5513 -- instance. We add a renaming declaration after the declaration, which
5514 -- is available in the instance body, as well as in the analysis of
5515 -- aspects that appear in the generic. This renaming declaration is
5516 -- inserted after the instance declaration which it renames.
5518 ------------------------------------
5519 -- Analyze_Instance_And_Renamings --
5520 ------------------------------------
5522 procedure Analyze_Instance_And_Renamings is
5523 Def_Ent : constant Entity_Id := Defining_Entity (N);
5524 Pack_Decl : Node_Id;
5526 begin
5527 if Nkind (Parent (N)) = N_Compilation_Unit then
5529 -- For the case of a compilation unit, the container package has
5530 -- the same name as the instantiation, to insure that the binder
5531 -- calls the elaboration procedure with the right name. Copy the
5532 -- entity of the instance, which may have compilation level flags
5533 -- (e.g. Is_Child_Unit) set.
5535 Pack_Id := New_Copy (Def_Ent);
5537 else
5538 -- Otherwise we use the name of the instantiation concatenated
5539 -- with its source position to ensure uniqueness if there are
5540 -- several instantiations with the same name.
5542 Pack_Id :=
5543 Make_Defining_Identifier (Loc,
5544 Chars => New_External_Name
5545 (Related_Id => Chars (Def_Ent),
5546 Suffix => "GP",
5547 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5548 end if;
5550 Pack_Decl :=
5551 Make_Package_Declaration (Loc,
5552 Specification => Make_Package_Specification (Loc,
5553 Defining_Unit_Name => Pack_Id,
5554 Visible_Declarations => Renaming_List,
5555 End_Label => Empty));
5557 Set_Instance_Spec (N, Pack_Decl);
5558 Set_Is_Generic_Instance (Pack_Id);
5559 Set_Debug_Info_Needed (Pack_Id);
5561 -- Case of not a compilation unit
5563 if Nkind (Parent (N)) /= N_Compilation_Unit then
5564 Mark_Rewrite_Insertion (Pack_Decl);
5565 Insert_Before (N, Pack_Decl);
5566 Set_Has_Completion (Pack_Id);
5568 -- Case of an instantiation that is a compilation unit
5570 -- Place declaration on current node so context is complete for
5571 -- analysis (including nested instantiations), and for use in a
5572 -- context_clause (see Analyze_With_Clause).
5574 else
5575 Set_Unit (Parent (N), Pack_Decl);
5576 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5577 end if;
5579 Analyze (Pack_Decl);
5580 Check_Formal_Packages (Pack_Id);
5582 -- Body of the enclosing package is supplied when instantiating the
5583 -- subprogram body, after semantic analysis is completed.
5585 if Nkind (Parent (N)) = N_Compilation_Unit then
5587 -- Remove package itself from visibility, so it does not
5588 -- conflict with subprogram.
5590 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5592 -- Set name and scope of internal subprogram so that the proper
5593 -- external name will be generated. The proper scope is the scope
5594 -- of the wrapper package. We need to generate debugging info for
5595 -- the internal subprogram, so set flag accordingly.
5597 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5598 Set_Scope (Anon_Id, Scope (Pack_Id));
5600 -- Mark wrapper package as referenced, to avoid spurious warnings
5601 -- if the instantiation appears in various with_ clauses of
5602 -- subunits of the main unit.
5604 Set_Referenced (Pack_Id);
5605 end if;
5607 Set_Is_Generic_Instance (Anon_Id);
5608 Set_Debug_Info_Needed (Anon_Id);
5609 Act_Decl_Id := New_Copy (Anon_Id);
5611 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5612 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5613 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5615 -- Subprogram instance comes from source only if generic does
5617 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5619 -- If the instance is a child unit, mark the Id accordingly. Mark
5620 -- the anonymous entity as well, which is the real subprogram and
5621 -- which is used when the instance appears in a context clause.
5622 -- Similarly, propagate the Is_Eliminated flag to handle properly
5623 -- nested eliminated subprograms.
5625 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5626 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5627 New_Overloaded_Entity (Act_Decl_Id);
5628 Check_Eliminated (Act_Decl_Id);
5629 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5631 if Nkind (Parent (N)) = N_Compilation_Unit then
5633 -- In compilation unit case, kill elaboration checks on the
5634 -- instantiation, since they are never needed - the body is
5635 -- instantiated at the same point as the spec.
5637 if Legacy_Elaboration_Checks then
5638 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5639 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5640 end if;
5642 Set_Is_Compilation_Unit (Anon_Id);
5643 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5644 end if;
5646 -- The instance is not a freezing point for the new subprogram.
5647 -- The anonymous subprogram may have a freeze node, created for
5648 -- some delayed aspects. This freeze node must not be inherited
5649 -- by the visible subprogram entity.
5651 Set_Is_Frozen (Act_Decl_Id, False);
5652 Set_Freeze_Node (Act_Decl_Id, Empty);
5654 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5655 Valid_Operator_Definition (Act_Decl_Id);
5656 end if;
5658 Set_Alias (Act_Decl_Id, Anon_Id);
5659 Set_Has_Completion (Act_Decl_Id);
5660 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5662 if Nkind (Parent (N)) = N_Compilation_Unit then
5663 Set_Body_Required (Parent (N), False);
5664 end if;
5665 end Analyze_Instance_And_Renamings;
5667 -------------------------------
5668 -- Build_Subprogram_Renaming --
5669 -------------------------------
5671 procedure Build_Subprogram_Renaming is
5672 Renaming_Decl : Node_Id;
5673 Unit_Renaming : Node_Id;
5675 begin
5676 Unit_Renaming :=
5677 Make_Subprogram_Renaming_Declaration (Loc,
5678 Specification =>
5679 Copy_Generic_Node
5680 (Specification (Original_Node (Gen_Decl)),
5681 Empty,
5682 Instantiating => True),
5683 Name => New_Occurrence_Of (Anon_Id, Loc));
5685 -- The generic may be a child unit. The renaming needs an identifier
5686 -- with the proper name.
5688 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5689 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5691 -- If there is a formal subprogram with the same name as the unit
5692 -- itself, do not add this renaming declaration, to prevent
5693 -- ambiguities when there is a call with that name in the body.
5695 Renaming_Decl := First (Renaming_List);
5696 while Present (Renaming_Decl) loop
5697 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5698 and then
5699 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5700 then
5701 exit;
5702 end if;
5704 Next (Renaming_Decl);
5705 end loop;
5707 if No (Renaming_Decl) then
5708 Append (Unit_Renaming, Renaming_List);
5709 end if;
5710 end Build_Subprogram_Renaming;
5712 -- Local variables
5714 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5715 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5716 Saved_ISMP : constant Boolean :=
5717 Ignore_SPARK_Mode_Pragmas_In_Instance;
5718 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5719 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5720 -- Save the Ghost and SPARK mode-related data to restore on exit
5722 Vis_Prims_List : Elist_Id := No_Elist;
5723 -- List of primitives made temporarily visible in the instantiation
5724 -- to match the visibility of the formal type
5726 -- Start of processing for Analyze_Subprogram_Instantiation
5728 begin
5729 -- Preserve relevant elaboration-related attributes of the context which
5730 -- are no longer available or very expensive to recompute once analysis,
5731 -- resolution, and expansion are over.
5733 Mark_Elaboration_Attributes
5734 (N_Id => N,
5735 Checks => True,
5736 Level => True,
5737 Modes => True,
5738 Warnings => True);
5740 -- Very first thing: check for special Text_IO unit in case we are
5741 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5742 -- such an instantiation is bogus (these are packages, not subprograms),
5743 -- but we get a better error message if we do this.
5745 Check_Text_IO_Special_Unit (Gen_Id);
5747 -- Make node global for error reporting
5749 Instantiation_Node := N;
5751 -- For package instantiations we turn off style checks, because they
5752 -- will have been emitted in the generic. For subprogram instantiations
5753 -- we want to apply at least the check on overriding indicators so we
5754 -- do not modify the style check status.
5756 -- The renaming declarations for the actuals do not come from source and
5757 -- will not generate spurious warnings.
5759 Preanalyze_Actuals (N);
5761 Init_Env;
5762 Env_Installed := True;
5763 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5764 Gen_Unit := Entity (Gen_Id);
5766 -- A subprogram instantiation is Ghost when it is subject to pragma
5767 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5768 -- that any nodes generated during analysis and expansion are marked as
5769 -- Ghost.
5771 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5773 Generate_Reference (Gen_Unit, Gen_Id);
5775 if Nkind (Gen_Id) = N_Identifier
5776 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5777 then
5778 Error_Msg_NE
5779 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5780 end if;
5782 if Etype (Gen_Unit) = Any_Type then
5783 Restore_Env;
5784 goto Leave;
5785 end if;
5787 -- Verify that it is a generic subprogram of the right kind, and that
5788 -- it does not lead to a circular instantiation.
5790 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5791 Error_Msg_NE
5792 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5794 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5795 Error_Msg_NE
5796 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5798 elsif In_Open_Scopes (Gen_Unit) then
5799 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5801 else
5802 Mutate_Ekind (Inst_Id, K);
5803 Set_Scope (Inst_Id, Current_Scope);
5805 Set_Entity (Gen_Id, Gen_Unit);
5807 if In_Extended_Main_Source_Unit (N) then
5808 Set_Is_Instantiated (Gen_Unit);
5809 Generate_Reference (Gen_Unit, N);
5810 end if;
5812 -- If renaming, get original unit
5814 if Present (Renamed_Entity (Gen_Unit))
5815 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5816 then
5817 Gen_Unit := Renamed_Entity (Gen_Unit);
5818 Set_Is_Instantiated (Gen_Unit);
5819 Generate_Reference (Gen_Unit, N);
5820 end if;
5822 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5823 Error_Msg_Node_2 := Current_Scope;
5824 Error_Msg_NE
5825 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5826 Circularity_Detected := True;
5827 Restore_Hidden_Primitives (Vis_Prims_List);
5828 goto Leave;
5829 end if;
5831 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5833 -- Initialize renamings map, for error checking
5835 Generic_Renamings.Set_Last (0);
5836 Generic_Renamings_HTable.Reset;
5838 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5840 -- Copy original generic tree, to produce text for instantiation
5842 Act_Tree :=
5843 Copy_Generic_Node
5844 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5846 -- Inherit overriding indicator from instance node
5848 Act_Spec := Specification (Act_Tree);
5849 Set_Must_Override (Act_Spec, Must_Override (N));
5850 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5852 Renaming_List :=
5853 Analyze_Associations
5854 (I_Node => N,
5855 Formals => Generic_Formal_Declarations (Act_Tree),
5856 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5858 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5860 -- The subprogram itself cannot contain a nested instance, so the
5861 -- current parent is left empty.
5863 Set_Instance_Env (Gen_Unit, Empty);
5865 -- Build the subprogram declaration, which does not appear in the
5866 -- generic template, and give it a sloc consistent with that of the
5867 -- template.
5869 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5870 Set_Generic_Parent (Act_Spec, Gen_Unit);
5871 Act_Decl :=
5872 Make_Subprogram_Declaration (Sloc (Act_Spec),
5873 Specification => Act_Spec);
5875 -- The aspects have been copied previously, but they have to be
5876 -- linked explicitly to the new subprogram declaration. Explicit
5877 -- pre/postconditions on the instance are analyzed below, in a
5878 -- separate step.
5880 Move_Aspects (Act_Tree, To => Act_Decl);
5881 Set_Categorization_From_Pragmas (Act_Decl);
5883 if Parent_Installed then
5884 Hide_Current_Scope;
5885 end if;
5887 Append (Act_Decl, Renaming_List);
5889 -- Contract-related source pragmas that follow a generic subprogram
5890 -- must be instantiated explicitly because they are not part of the
5891 -- subprogram template.
5893 Instantiate_Subprogram_Contract
5894 (Original_Node (Gen_Decl), Renaming_List);
5896 Build_Subprogram_Renaming;
5898 -- If the context of the instance is subject to SPARK_Mode "off" or
5899 -- the annotation is altogether missing, set the global flag which
5900 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5901 -- the instance. This should be done prior to analyzing the instance.
5903 if SPARK_Mode /= On then
5904 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5905 end if;
5907 -- If the context of an instance is not subject to SPARK_Mode "off",
5908 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5909 -- the latter should be the one applicable to the instance.
5911 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5912 and then Saved_SM /= Off
5913 and then Present (SPARK_Pragma (Gen_Unit))
5914 then
5915 Set_SPARK_Mode (Gen_Unit);
5916 end if;
5918 -- Need to mark Anon_Id intrinsic before calling
5919 -- Analyze_Instance_And_Renamings because this flag may be propagated
5920 -- to other nodes.
5922 if Is_Intrinsic_Subprogram (Gen_Unit) then
5923 Set_Is_Intrinsic_Subprogram (Anon_Id);
5924 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5925 end if;
5927 Analyze_Instance_And_Renamings;
5929 -- Restore SPARK_Mode from the context after analysis of the package
5930 -- declaration, so that the SPARK_Mode on the generic spec does not
5931 -- apply to the pending instance for the instance body.
5933 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5934 and then Saved_SM /= Off
5935 and then Present (SPARK_Pragma (Gen_Unit))
5936 then
5937 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5938 end if;
5940 -- If the generic is marked Import (Intrinsic), then so is the
5941 -- instance; this indicates that there is no body to instantiate.
5942 -- We also copy the interface name in case this is handled by the
5943 -- back-end and deal with an instance of unchecked conversion.
5945 if Is_Intrinsic_Subprogram (Gen_Unit) then
5946 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5947 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5949 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5950 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5951 end if;
5952 end if;
5954 -- Inherit convention from generic unit. Intrinsic convention, as for
5955 -- an instance of unchecked conversion, is not inherited because an
5956 -- explicit Ada instance has been created.
5958 if Has_Convention_Pragma (Gen_Unit)
5959 and then Convention (Gen_Unit) /= Convention_Intrinsic
5960 then
5961 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5962 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5963 end if;
5965 Generate_Definition (Act_Decl_Id);
5967 -- Inherit all inlining-related flags which apply to the generic in
5968 -- the subprogram and its declaration.
5970 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5971 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5973 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5974 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5976 Set_Has_Pragma_Inline_Always
5977 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5978 Set_Has_Pragma_Inline_Always
5979 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5981 Set_Has_Pragma_No_Inline
5982 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5983 Set_Has_Pragma_No_Inline
5984 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5986 -- Propagate No_Return if pragma applied to generic unit. This must
5987 -- be done explicitly because pragma does not appear in generic
5988 -- declaration (unlike the aspect case).
5990 if No_Return (Gen_Unit) then
5991 Set_No_Return (Act_Decl_Id);
5992 Set_No_Return (Anon_Id);
5993 end if;
5995 -- Mark both the instance spec and the anonymous package in case the
5996 -- body is instantiated at a later pass. This preserves the original
5997 -- context in effect for the body.
5999 if SPARK_Mode /= On then
6000 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6001 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6002 end if;
6004 if Legacy_Elaboration_Checks
6005 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6006 then
6007 Check_Elab_Instantiation (N);
6008 end if;
6010 -- Save the scenario for later examination by the ABE Processing
6011 -- phase.
6013 Record_Elaboration_Scenario (N);
6015 -- The instantiation results in a guaranteed ABE. Create a completing
6016 -- body for the subprogram declaration because the real body will not
6017 -- be instantiated.
6019 if Is_Known_Guaranteed_ABE (N) then
6020 Provide_Completing_Bodies (Instance_Spec (N));
6021 end if;
6023 if Is_Dispatching_Operation (Act_Decl_Id)
6024 and then Ada_Version >= Ada_2005
6025 then
6026 declare
6027 Formal : Entity_Id;
6029 begin
6030 Formal := First_Formal (Act_Decl_Id);
6031 while Present (Formal) loop
6032 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6033 and then Is_Controlling_Formal (Formal)
6034 and then not Can_Never_Be_Null (Formal)
6035 then
6036 Error_Msg_NE
6037 ("access parameter& is controlling,", N, Formal);
6038 Error_Msg_NE
6039 ("\corresponding parameter of & must be explicitly "
6040 & "null-excluding", N, Gen_Id);
6041 end if;
6043 Next_Formal (Formal);
6044 end loop;
6045 end;
6046 end if;
6048 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6050 Validate_Categorization_Dependency (N, Act_Decl_Id);
6052 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6053 Inherit_Context (Gen_Decl, N);
6055 Restore_Private_Views (Pack_Id, False);
6057 -- If the context requires a full instantiation, mark node for
6058 -- subsequent construction of the body.
6060 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6061 Check_Forward_Instantiation (Gen_Decl);
6063 -- The wrapper package is always delayed, because it does not
6064 -- constitute a freeze point, but to insure that the freeze node
6065 -- is placed properly, it is created directly when instantiating
6066 -- the body (otherwise the freeze node might appear to early for
6067 -- nested instantiations).
6069 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6070 Rewrite (N, Unit (Parent (N)));
6071 Set_Unit (Parent (N), N);
6072 end if;
6074 -- Replace instance node for library-level instantiations of
6075 -- intrinsic subprograms.
6077 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6078 Rewrite (N, Unit (Parent (N)));
6079 Set_Unit (Parent (N), N);
6080 end if;
6082 if Parent_Installed then
6083 Remove_Parent;
6084 end if;
6086 Restore_Hidden_Primitives (Vis_Prims_List);
6087 Restore_Env;
6088 Env_Installed := False;
6089 Generic_Renamings.Set_Last (0);
6090 Generic_Renamings_HTable.Reset;
6091 end if;
6093 <<Leave>>
6094 -- Analyze aspects in declaration if no errors appear in the instance.
6096 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6097 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6098 end if;
6100 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6101 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6102 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6104 exception
6105 when Instantiation_Error =>
6106 if Parent_Installed then
6107 Remove_Parent;
6108 end if;
6110 if Env_Installed then
6111 Restore_Env;
6112 end if;
6114 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6115 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6116 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6117 end Analyze_Subprogram_Instantiation;
6119 ---------------------------
6120 -- Get_Associated_Entity --
6121 ---------------------------
6123 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6124 Assoc : Entity_Id;
6126 begin
6127 Assoc := Associated_Entity (Id);
6129 if Present (Assoc) then
6130 while Present (Associated_Entity (Assoc)) loop
6131 Assoc := Associated_Entity (Assoc);
6132 end loop;
6133 end if;
6135 return Assoc;
6136 end Get_Associated_Entity;
6138 -------------------------
6139 -- Get_Associated_Node --
6140 -------------------------
6142 function Get_Associated_Node (N : Node_Id) return Node_Id is
6143 Assoc : Node_Id;
6145 begin
6146 Assoc := Associated_Node (N);
6148 if Nkind (Assoc) /= Nkind (N) then
6149 return Assoc;
6151 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6152 return Assoc;
6154 else
6155 -- If the node is part of an inner generic, it may itself have been
6156 -- remapped into a further generic copy. Associated_Node is otherwise
6157 -- used for the entity of the node, and will be of a different node
6158 -- kind, or else N has been rewritten as a literal or function call.
6160 while Present (Associated_Node (Assoc))
6161 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6162 loop
6163 Assoc := Associated_Node (Assoc);
6164 end loop;
6166 -- Follow an additional link in case the final node was rewritten.
6167 -- This can only happen with nested generic units.
6169 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6170 and then Present (Associated_Node (Assoc))
6171 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6172 | N_Explicit_Dereference
6173 | N_Integer_Literal
6174 | N_Real_Literal
6175 | N_String_Literal
6176 then
6177 Assoc := Associated_Node (Assoc);
6178 end if;
6180 -- An additional special case: an unconstrained type in an object
6181 -- declaration may have been rewritten as a local subtype constrained
6182 -- by the expression in the declaration. We need to recover the
6183 -- original entity, which may be global.
6185 if Present (Original_Node (Assoc))
6186 and then Nkind (Parent (N)) = N_Object_Declaration
6187 then
6188 Assoc := Original_Node (Assoc);
6189 end if;
6191 return Assoc;
6192 end if;
6193 end Get_Associated_Node;
6195 -----------------------------------
6196 -- Build_Subprogram_Decl_Wrapper --
6197 -----------------------------------
6199 function Build_Subprogram_Decl_Wrapper
6200 (Formal_Subp : Entity_Id) return Node_Id
6202 Loc : constant Source_Ptr := Sloc (Current_Scope);
6203 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6204 Decl : Node_Id;
6205 Subp : Entity_Id;
6206 Parm_Spec : Node_Id;
6207 Profile : List_Id := New_List;
6208 Spec : Node_Id;
6209 Form_F : Entity_Id;
6210 New_F : Entity_Id;
6212 begin
6214 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6215 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6216 Set_Is_Generic_Actual_Subprogram (Subp);
6218 Profile := Parameter_Specifications (
6219 New_Copy_Tree
6220 (Specification (Unit_Declaration_Node (Formal_Subp))));
6222 Form_F := First_Formal (Formal_Subp);
6223 Parm_Spec := First (Profile);
6225 -- Create new entities for the formals. Reset entities so that
6226 -- parameter types are properly resolved when wrapper declaration
6227 -- is analyzed.
6229 while Present (Parm_Spec) loop
6230 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6231 Set_Defining_Identifier (Parm_Spec, New_F);
6232 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6233 Next (Parm_Spec);
6234 Next_Formal (Form_F);
6235 end loop;
6237 if Ret_Type = Standard_Void_Type then
6238 Spec :=
6239 Make_Procedure_Specification (Loc,
6240 Defining_Unit_Name => Subp,
6241 Parameter_Specifications => Profile);
6242 else
6243 Spec :=
6244 Make_Function_Specification (Loc,
6245 Defining_Unit_Name => Subp,
6246 Parameter_Specifications => Profile,
6247 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6248 end if;
6250 Decl :=
6251 Make_Subprogram_Declaration (Loc, Specification => Spec);
6253 return Decl;
6254 end Build_Subprogram_Decl_Wrapper;
6256 -----------------------------------
6257 -- Build_Subprogram_Body_Wrapper --
6258 -----------------------------------
6260 function Build_Subprogram_Body_Wrapper
6261 (Formal_Subp : Entity_Id;
6262 Actual_Name : Node_Id) return Node_Id
6264 Loc : constant Source_Ptr := Sloc (Current_Scope);
6265 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6266 Spec_Node : constant Node_Id :=
6267 Specification
6268 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6269 Act : Node_Id;
6270 Actuals : List_Id;
6271 Body_Node : Node_Id;
6272 Stmt : Node_Id;
6273 begin
6274 Actuals := New_List;
6275 Act := First (Parameter_Specifications (Spec_Node));
6277 while Present (Act) loop
6278 Append_To (Actuals,
6279 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6280 Next (Act);
6281 end loop;
6283 if Ret_Type = Standard_Void_Type then
6284 Stmt := Make_Procedure_Call_Statement (Loc,
6285 Name => Actual_Name,
6286 Parameter_Associations => Actuals);
6288 else
6289 Stmt := Make_Simple_Return_Statement (Loc,
6290 Expression =>
6291 Make_Function_Call (Loc,
6292 Name => Actual_Name,
6293 Parameter_Associations => Actuals));
6294 end if;
6296 Body_Node := Make_Subprogram_Body (Loc,
6297 Specification => Spec_Node,
6298 Declarations => New_List,
6299 Handled_Statement_Sequence =>
6300 Make_Handled_Sequence_Of_Statements (Loc,
6301 Statements => New_List (Stmt)));
6303 return Body_Node;
6304 end Build_Subprogram_Body_Wrapper;
6306 -------------------------------------------
6307 -- Build_Instance_Compilation_Unit_Nodes --
6308 -------------------------------------------
6310 procedure Build_Instance_Compilation_Unit_Nodes
6311 (N : Node_Id;
6312 Act_Body : Node_Id;
6313 Act_Decl : Node_Id)
6315 Decl_Cunit : Node_Id;
6316 Body_Cunit : Node_Id;
6317 Citem : Node_Id;
6318 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6319 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6321 begin
6322 -- A new compilation unit node is built for the instance declaration.
6323 -- It relocates the auxiliary declaration node from the compilation unit
6324 -- where the instance appeared, so that declarations that originally
6325 -- followed the instance will be attached to the spec compilation unit.
6327 Decl_Cunit :=
6328 Make_Compilation_Unit (Sloc (N),
6329 Context_Items => Empty_List,
6330 Unit => Act_Decl,
6331 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6333 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6335 -- The new compilation unit is linked to its body, but both share the
6336 -- same file, so we do not set Body_Required on the new unit so as not
6337 -- to create a spurious dependency on a non-existent body in the ali.
6338 -- This simplifies CodePeer unit traversal.
6340 -- We use the original instantiation compilation unit as the resulting
6341 -- compilation unit of the instance, since this is the main unit.
6343 Rewrite (N, Act_Body);
6345 Body_Cunit := Parent (N);
6347 -- The two compilation unit nodes are linked by the Library_Unit field
6349 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6350 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6352 -- Preserve the private nature of the package if needed
6354 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6356 -- If the instance is not the main unit, its context, categorization
6357 -- and elaboration entity are not relevant to the compilation.
6359 if Body_Cunit /= Cunit (Main_Unit) then
6360 Make_Instance_Unit (Body_Cunit, In_Main => False);
6361 return;
6362 end if;
6364 -- The context clause items on the instantiation, which are now attached
6365 -- to the body compilation unit (since the body overwrote the original
6366 -- instantiation node), semantically belong on the spec, so copy them
6367 -- there. It's harmless to leave them on the body as well. In fact one
6368 -- could argue that they belong in both places.
6370 Citem := First (Context_Items (Body_Cunit));
6371 while Present (Citem) loop
6372 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6373 Next (Citem);
6374 end loop;
6376 -- Propagate categorization flags on packages, so that they appear in
6377 -- the ali file for the spec of the unit.
6379 if Ekind (New_Main) = E_Package then
6380 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6381 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6382 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6383 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6384 Set_Is_Remote_Call_Interface
6385 (Old_Main, Is_Remote_Call_Interface (New_Main));
6386 end if;
6388 -- Make entry in Units table, so that binder can generate call to
6389 -- elaboration procedure for body, if any.
6391 Make_Instance_Unit (Body_Cunit, In_Main => True);
6392 Main_Unit_Entity := New_Main;
6393 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6395 -- Build elaboration entity, since the instance may certainly generate
6396 -- elaboration code requiring a flag for protection.
6398 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6399 end Build_Instance_Compilation_Unit_Nodes;
6401 --------------------------------
6402 -- Check_Abbreviated_Instance --
6403 --------------------------------
6405 procedure Check_Abbreviated_Instance
6406 (N : Node_Id;
6407 Parent_Installed : in out Boolean)
6409 Inst_Node : Node_Id;
6411 begin
6412 if Nkind (N) = N_Package_Specification
6413 and then Is_Abbreviated_Instance (Defining_Entity (N))
6414 then
6415 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6416 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6417 end if;
6418 end Check_Abbreviated_Instance;
6420 -----------------------------
6421 -- Check_Access_Definition --
6422 -----------------------------
6424 procedure Check_Access_Definition (N : Node_Id) is
6425 begin
6426 pragma Assert
6427 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6428 null;
6429 end Check_Access_Definition;
6431 -----------------------------------
6432 -- Check_Formal_Package_Instance --
6433 -----------------------------------
6435 -- If the formal has specific parameters, they must match those of the
6436 -- actual. Both of them are instances, and the renaming declarations for
6437 -- their formal parameters appear in the same order in both. The analyzed
6438 -- formal has been analyzed in the context of the current instance.
6440 procedure Check_Formal_Package_Instance
6441 (Formal_Pack : Entity_Id;
6442 Actual_Pack : Entity_Id)
6444 E1 : Entity_Id := First_Entity (Actual_Pack);
6445 E2 : Entity_Id := First_Entity (Formal_Pack);
6446 Prev_E1 : Entity_Id;
6448 Expr1 : Node_Id;
6449 Expr2 : Node_Id;
6451 procedure Check_Mismatch (B : Boolean);
6452 -- Common error routine for mismatch between the parameters of the
6453 -- actual instance and those of the formal package.
6455 function Is_Defaulted (Param : Entity_Id) return Boolean;
6456 -- If the formal package has partly box-initialized formals, skip
6457 -- conformance check for these formals. Previously the code assumed
6458 -- that box initialization for a formal package applied to all its
6459 -- formal parameters.
6461 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6462 -- The formal may come from a nested formal package, and the actual may
6463 -- have been constant-folded. To determine whether the two denote the
6464 -- same entity we may have to traverse several definitions to recover
6465 -- the ultimate entity that they refer to.
6467 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6468 -- The formal and the actual must be identical, but if both are
6469 -- given by attributes they end up renaming different generated bodies,
6470 -- and we must verify that the attributes themselves match.
6472 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6473 -- Similarly, if the formal comes from a nested formal package, the
6474 -- actual may designate the formal through multiple renamings, which
6475 -- have to be followed to determine the original variable in question.
6477 --------------------
6478 -- Check_Mismatch --
6479 --------------------
6481 procedure Check_Mismatch (B : Boolean) is
6482 -- A Formal_Type_Declaration for a derived private type is rewritten
6483 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6484 -- which is why we examine the original node.
6486 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6488 begin
6489 if Kind = N_Formal_Type_Declaration then
6490 return;
6492 elsif Kind in N_Formal_Object_Declaration
6493 | N_Formal_Package_Declaration
6494 | N_Formal_Subprogram_Declaration
6495 then
6496 null;
6498 -- Ada 2012: If both formal and actual are incomplete types they
6499 -- are conformant.
6501 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6502 null;
6504 elsif B then
6505 Error_Msg_NE
6506 ("actual for & in actual instance does not match formal",
6507 Parent (Actual_Pack), E1);
6508 end if;
6509 end Check_Mismatch;
6511 ------------------
6512 -- Is_Defaulted --
6513 ------------------
6515 function Is_Defaulted (Param : Entity_Id) return Boolean is
6516 Assoc : Node_Id;
6518 begin
6519 Assoc :=
6520 First (Generic_Associations (Parent
6521 (Associated_Formal_Package (Actual_Pack))));
6523 while Present (Assoc) loop
6524 if Nkind (Assoc) = N_Others_Choice then
6525 return True;
6527 elsif Nkind (Assoc) = N_Generic_Association
6528 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6529 then
6530 return Box_Present (Assoc);
6531 end if;
6533 Next (Assoc);
6534 end loop;
6536 return False;
6537 end Is_Defaulted;
6539 --------------------------------
6540 -- Same_Instantiated_Constant --
6541 --------------------------------
6543 function Same_Instantiated_Constant
6544 (E1, E2 : Entity_Id) return Boolean
6546 Ent : Entity_Id;
6548 begin
6549 Ent := E2;
6550 while Present (Ent) loop
6551 if E1 = Ent then
6552 return True;
6554 elsif Ekind (Ent) /= E_Constant then
6555 return False;
6557 elsif Is_Entity_Name (Constant_Value (Ent)) then
6558 if Entity (Constant_Value (Ent)) = E1 then
6559 return True;
6560 else
6561 Ent := Entity (Constant_Value (Ent));
6562 end if;
6564 -- The actual may be a constant that has been folded. Recover
6565 -- original name.
6567 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6568 Ent := Entity (Original_Node (Constant_Value (Ent)));
6570 else
6571 return False;
6572 end if;
6573 end loop;
6575 return False;
6576 end Same_Instantiated_Constant;
6578 --------------------------------
6579 -- Same_Instantiated_Function --
6580 --------------------------------
6582 function Same_Instantiated_Function
6583 (E1, E2 : Entity_Id) return Boolean
6585 U1, U2 : Node_Id;
6586 begin
6587 if Alias (E1) = Alias (E2) then
6588 return True;
6590 elsif Present (Alias (E2)) then
6591 U1 := Original_Node (Unit_Declaration_Node (E1));
6592 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6594 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6595 and then Nkind (Name (U1)) = N_Attribute_Reference
6597 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6598 and then Nkind (Name (U2)) = N_Attribute_Reference
6600 and then
6601 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6602 else
6603 return False;
6604 end if;
6605 end Same_Instantiated_Function;
6607 --------------------------------
6608 -- Same_Instantiated_Variable --
6609 --------------------------------
6611 function Same_Instantiated_Variable
6612 (E1, E2 : Entity_Id) return Boolean
6614 function Original_Entity (E : Entity_Id) return Entity_Id;
6615 -- Follow chain of renamings to the ultimate ancestor
6617 ---------------------
6618 -- Original_Entity --
6619 ---------------------
6621 function Original_Entity (E : Entity_Id) return Entity_Id is
6622 Orig : Entity_Id;
6624 begin
6625 Orig := E;
6626 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6627 and then Present (Renamed_Object (Orig))
6628 and then Is_Entity_Name (Renamed_Object (Orig))
6629 loop
6630 Orig := Entity (Renamed_Object (Orig));
6631 end loop;
6633 return Orig;
6634 end Original_Entity;
6636 -- Start of processing for Same_Instantiated_Variable
6638 begin
6639 return Ekind (E1) = Ekind (E2)
6640 and then Original_Entity (E1) = Original_Entity (E2);
6641 end Same_Instantiated_Variable;
6643 -- Start of processing for Check_Formal_Package_Instance
6645 begin
6646 Prev_E1 := E1;
6647 while Present (E1) and then Present (E2) loop
6648 exit when Ekind (E1) = E_Package
6649 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6651 -- If the formal is the renaming of the formal package, this
6652 -- is the end of its formal part, which may occur before the
6653 -- end of the formal part in the actual in the presence of
6654 -- defaulted parameters in the formal package.
6656 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6657 and then Renamed_Entity (E2) = Scope (E2);
6659 -- The analysis of the actual may generate additional internal
6660 -- entities. If the formal is defaulted, there is no corresponding
6661 -- analysis and the internal entities must be skipped, until we
6662 -- find corresponding entities again.
6664 if Comes_From_Source (E2)
6665 and then not Comes_From_Source (E1)
6666 and then Chars (E1) /= Chars (E2)
6667 then
6668 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6669 Next_Entity (E1);
6670 end loop;
6671 end if;
6673 if No (E1) then
6674 return;
6676 -- Entities may be declared without full declaration, such as
6677 -- itypes and predefined operators (concatenation for arrays, eg).
6678 -- Skip it and keep the formal entity to find a later match for it.
6680 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6681 E1 := Prev_E1;
6682 goto Next_E;
6684 -- If the formal entity comes from a formal declaration, it was
6685 -- defaulted in the formal package, and no check is needed on it.
6687 elsif Nkind (Original_Node (Parent (E2))) in
6688 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6689 then
6690 -- If the formal is a tagged type the corresponding class-wide
6691 -- type has been generated as well, and it must be skipped.
6693 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6694 Next_Entity (E2);
6695 end if;
6697 goto Next_E;
6699 -- Ditto for defaulted formal subprograms.
6701 elsif Is_Overloadable (E1)
6702 and then Nkind (Unit_Declaration_Node (E2)) in
6703 N_Formal_Subprogram_Declaration
6704 then
6705 goto Next_E;
6707 elsif Is_Defaulted (E1) then
6708 goto Next_E;
6710 elsif Is_Type (E1) then
6712 -- Subtypes must statically match. E1, E2 are the local entities
6713 -- that are subtypes of the actuals. Itypes generated for other
6714 -- parameters need not be checked, the check will be performed
6715 -- on the parameters themselves.
6717 -- If E2 is a formal type declaration, it is a defaulted parameter
6718 -- and needs no checking.
6720 if not Is_Itype (E1) and then not Is_Itype (E2) then
6721 Check_Mismatch
6722 (not Is_Type (E2)
6723 or else Etype (E1) /= Etype (E2)
6724 or else not Subtypes_Statically_Match (E1, E2));
6725 end if;
6727 elsif Ekind (E1) = E_Constant then
6729 -- IN parameters must denote the same static value, or the same
6730 -- constant, or the literal null.
6732 Expr1 := Expression (Parent (E1));
6734 if Ekind (E2) /= E_Constant then
6735 Check_Mismatch (True);
6736 goto Next_E;
6737 else
6738 Expr2 := Expression (Parent (E2));
6739 end if;
6741 if Is_OK_Static_Expression (Expr1) then
6742 if not Is_OK_Static_Expression (Expr2) then
6743 Check_Mismatch (True);
6745 elsif Is_Discrete_Type (Etype (E1)) then
6746 declare
6747 V1 : constant Uint := Expr_Value (Expr1);
6748 V2 : constant Uint := Expr_Value (Expr2);
6749 begin
6750 Check_Mismatch (V1 /= V2);
6751 end;
6753 elsif Is_Real_Type (Etype (E1)) then
6754 declare
6755 V1 : constant Ureal := Expr_Value_R (Expr1);
6756 V2 : constant Ureal := Expr_Value_R (Expr2);
6757 begin
6758 Check_Mismatch (V1 /= V2);
6759 end;
6761 elsif Is_String_Type (Etype (E1))
6762 and then Nkind (Expr1) = N_String_Literal
6763 then
6764 if Nkind (Expr2) /= N_String_Literal then
6765 Check_Mismatch (True);
6766 else
6767 Check_Mismatch
6768 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6769 end if;
6770 end if;
6772 elsif Is_Entity_Name (Expr1) then
6773 if Is_Entity_Name (Expr2) then
6774 if Entity (Expr1) = Entity (Expr2) then
6775 null;
6776 else
6777 Check_Mismatch
6778 (not Same_Instantiated_Constant
6779 (Entity (Expr1), Entity (Expr2)));
6780 end if;
6782 else
6783 Check_Mismatch (True);
6784 end if;
6786 elsif Is_Entity_Name (Original_Node (Expr1))
6787 and then Is_Entity_Name (Expr2)
6788 and then Same_Instantiated_Constant
6789 (Entity (Original_Node (Expr1)), Entity (Expr2))
6790 then
6791 null;
6793 elsif Nkind (Expr1) = N_Null then
6794 Check_Mismatch (Nkind (Expr1) /= N_Null);
6796 else
6797 Check_Mismatch (True);
6798 end if;
6800 elsif Ekind (E1) = E_Variable then
6801 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6803 elsif Ekind (E1) = E_Package then
6804 Check_Mismatch
6805 (Ekind (E1) /= Ekind (E2)
6806 or else (Present (Renamed_Entity (E2))
6807 and then Renamed_Entity (E1) /=
6808 Renamed_Entity (E2)));
6810 elsif Is_Overloadable (E1) then
6811 -- Verify that the actual subprograms match. Note that actuals
6812 -- that are attributes are rewritten as subprograms. If the
6813 -- subprogram in the formal package is defaulted, no check is
6814 -- needed. Note that this can only happen in Ada 2005 when the
6815 -- formal package can be partially parameterized.
6817 if Nkind (Unit_Declaration_Node (E1)) =
6818 N_Subprogram_Renaming_Declaration
6819 and then From_Default (Unit_Declaration_Node (E1))
6820 then
6821 null;
6823 -- If the formal package has an "others" box association that
6824 -- covers this formal, there is no need for a check either.
6826 elsif Nkind (Unit_Declaration_Node (E2)) in
6827 N_Formal_Subprogram_Declaration
6828 and then Box_Present (Unit_Declaration_Node (E2))
6829 then
6830 null;
6832 -- No check needed if subprogram is a defaulted null procedure
6834 elsif No (Alias (E2))
6835 and then Ekind (E2) = E_Procedure
6836 and then
6837 Null_Present (Specification (Unit_Declaration_Node (E2)))
6838 then
6839 null;
6841 -- Otherwise the actual in the formal and the actual in the
6842 -- instantiation of the formal must match, up to renamings.
6844 else
6845 Check_Mismatch
6846 (Ekind (E2) /= Ekind (E1)
6847 or else not Same_Instantiated_Function (E1, E2));
6848 end if;
6850 else
6851 raise Program_Error;
6852 end if;
6854 <<Next_E>>
6855 Prev_E1 := E1;
6856 Next_Entity (E1);
6857 Next_Entity (E2);
6858 end loop;
6859 end Check_Formal_Package_Instance;
6861 ---------------------------
6862 -- Check_Formal_Packages --
6863 ---------------------------
6865 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6866 E : Entity_Id;
6867 Formal_P : Entity_Id;
6868 Formal_Decl : Node_Id;
6870 begin
6871 -- Iterate through the declarations in the instance, looking for package
6872 -- renaming declarations that denote instances of formal packages, until
6873 -- we find the renaming of the current package itself. The declaration
6874 -- of a formal package that requires conformance checking is followed by
6875 -- an internal entity that is the abbreviated instance.
6877 E := First_Entity (P_Id);
6878 while Present (E) loop
6879 if Ekind (E) = E_Package then
6880 exit when Renamed_Entity (E) = P_Id;
6882 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6883 Formal_Decl := Parent (Associated_Formal_Package (E));
6885 if Requires_Conformance_Checking (Formal_Decl) then
6886 Formal_P := Next_Entity (E);
6888 -- If the instance is within an enclosing instance body
6889 -- there is no need to verify the legality of current formal
6890 -- packages because they were legal in the generic body.
6891 -- This optimization may be applicable elsewhere, and it
6892 -- also removes spurious errors that may arise with
6893 -- on-the-fly inlining and confusion between private and
6894 -- full views.
6896 if not In_Instance_Body then
6897 Check_Formal_Package_Instance (Formal_P, E);
6898 end if;
6900 -- Restore the visibility of formals of the formal instance
6901 -- that are not defaulted, and are hidden within the current
6902 -- generic. These formals may be visible within an enclosing
6903 -- generic.
6905 declare
6906 Elmt : Elmt_Id;
6907 begin
6908 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6909 while Present (Elmt) loop
6910 Set_Is_Hidden (Node (Elmt), False);
6911 Next_Elmt (Elmt);
6912 end loop;
6913 end;
6915 -- After checking, remove the internal validating package.
6916 -- It is only needed for semantic checks, and as it may
6917 -- contain generic formal declarations it should not reach
6918 -- gigi.
6920 Remove (Unit_Declaration_Node (Formal_P));
6921 end if;
6922 end if;
6923 end if;
6925 Next_Entity (E);
6926 end loop;
6927 end Check_Formal_Packages;
6929 ---------------------------------
6930 -- Check_Forward_Instantiation --
6931 ---------------------------------
6933 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6934 S : Entity_Id;
6935 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6937 begin
6938 -- The instantiation appears before the generic body if we are in the
6939 -- scope of the unit containing the generic, either in its spec or in
6940 -- the package body, and before the generic body.
6942 if Ekind (Gen_Comp) = E_Package_Body then
6943 Gen_Comp := Spec_Entity (Gen_Comp);
6944 end if;
6946 if In_Open_Scopes (Gen_Comp)
6947 and then No (Corresponding_Body (Decl))
6948 then
6949 S := Current_Scope;
6951 while Present (S)
6952 and then not Is_Compilation_Unit (S)
6953 and then not Is_Child_Unit (S)
6954 loop
6955 if Ekind (S) = E_Package then
6956 Set_Has_Forward_Instantiation (S);
6957 end if;
6959 S := Scope (S);
6960 end loop;
6961 end if;
6962 end Check_Forward_Instantiation;
6964 ---------------------------
6965 -- Check_Generic_Actuals --
6966 ---------------------------
6968 -- The visibility of the actuals may be different between the point of
6969 -- generic instantiation and the instantiation of the body.
6971 procedure Check_Generic_Actuals
6972 (Instance : Entity_Id;
6973 Is_Formal_Box : Boolean)
6975 Gen_Id : constant Entity_Id
6976 := (if Is_Generic_Unit (Instance) then
6977 Instance
6978 elsif Is_Wrapper_Package (Instance) then
6979 Generic_Parent
6980 (Specification
6981 (Unit_Declaration_Node (Related_Instance (Instance))))
6982 else
6983 Generic_Parent (Package_Specification (Instance)));
6984 -- The generic unit
6986 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
6987 -- The enclosing scope of the generic unit
6989 procedure Check_Actual_Type (Typ : Entity_Id);
6990 -- If the type of the actual is a private type declared in the enclosing
6991 -- scope of the generic, either directly or through packages nested in
6992 -- bodies, but not a derived type of a private type declared elsewhere,
6993 -- then the body of the generic sees the full view of the type because
6994 -- it has to appear in the package body. If the type is private now then
6995 -- exchange views to restore the proper visibility in the instance.
6997 -----------------------
6998 -- Check_Actual_Type --
6999 -----------------------
7001 procedure Check_Actual_Type (Typ : Entity_Id) is
7002 Btyp : constant Entity_Id := Base_Type (Typ);
7004 function Scope_Within_Body_Or_Same
7005 (Inner : Entity_Id;
7006 Outer : Entity_Id) return Boolean;
7007 -- Determine whether scope Inner is within the body of scope Outer
7008 -- or is Outer itself.
7010 -------------------------------
7011 -- Scope_Within_Body_Or_Same --
7012 -------------------------------
7014 function Scope_Within_Body_Or_Same
7015 (Inner : Entity_Id;
7016 Outer : Entity_Id) return Boolean
7018 Curr : Entity_Id := Inner;
7020 begin
7021 while Curr /= Standard_Standard loop
7022 if Curr = Outer then
7023 return True;
7025 elsif Is_Package_Body_Entity (Curr) then
7026 Curr := Scope (Curr);
7028 else
7029 exit;
7030 end if;
7031 end loop;
7033 return False;
7034 end Scope_Within_Body_Or_Same;
7036 begin
7037 -- The exchange is only needed if the generic is defined
7038 -- within a package which is not a common ancestor of the
7039 -- scope of the instance, and is not already in scope.
7041 if Is_Private_Type (Btyp)
7042 and then not Has_Private_Ancestor (Btyp)
7043 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7044 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7045 and then Parent_Scope /= Scope (Instance)
7046 and then not Is_Child_Unit (Gen_Id)
7047 then
7048 Switch_View (Btyp);
7050 -- If the type of the entity is a subtype, it may also have
7051 -- to be made visible, together with the base type of its
7052 -- full view, after exchange.
7054 if Is_Private_Type (Typ) then
7055 Switch_View (Typ);
7056 Switch_View (Base_Type (Typ));
7057 end if;
7058 end if;
7059 end Check_Actual_Type;
7061 -- Local variables
7063 Astype : Entity_Id;
7064 E : Entity_Id;
7065 Formal : Node_Id;
7067 -- Start of processing for Check_Generic_Actuals
7069 begin
7070 E := First_Entity (Instance);
7071 while Present (E) loop
7072 if Is_Type (E)
7073 and then Nkind (Parent (E)) = N_Subtype_Declaration
7074 and then Scope (Etype (E)) /= Instance
7075 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7076 then
7077 declare
7078 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7080 begin
7081 -- Restore the proper view of the actual from the information
7082 -- saved earlier by Instantiate_Type.
7084 Check_Private_View (Indic);
7086 -- If this view is an array type, check its component type.
7087 -- This handles the case of an array type whose component
7088 -- type is private, used as the actual in an instantiation
7089 -- of a generic construct declared in the same package as
7090 -- the component type and taking an array type with this
7091 -- component type as formal type parameter.
7093 if Is_Array_Type (Etype (Indic)) then
7094 Check_Actual_Type
7095 (Component_Type_For_Private_View (Etype (Indic)));
7096 end if;
7097 end;
7099 -- If the actual is itself the formal of a parent instance,
7100 -- then also restore the proper view of its actual and so on.
7101 -- That's necessary for nested instantiations of the form
7103 -- generic
7104 -- type Component is private;
7105 -- type Array_Type is array (Positive range <>) of Component;
7106 -- procedure Proc;
7108 -- when the outermost actuals have inconsistent views, because
7109 -- the Component_Type of Array_Type of the inner instantiations
7110 -- is the actual of Component of the outermost one and not that
7111 -- of the corresponding inner instantiations.
7113 Astype := Ancestor_Subtype (E);
7114 while Present (Astype)
7115 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7116 and then Present (Generic_Parent_Type (Parent (Astype)))
7117 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7118 loop
7119 Check_Private_View (Subtype_Indication (Parent (Astype)));
7120 Astype := Ancestor_Subtype (Astype);
7121 end loop;
7123 Set_Is_Generic_Actual_Type (E);
7125 if Is_Private_Type (E) and then Present (Full_View (E)) then
7126 Set_Is_Generic_Actual_Type (Full_View (E));
7127 end if;
7129 Set_Is_Hidden (E, False);
7130 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7132 -- We constructed the generic actual type as a subtype of the
7133 -- supplied type. This means that it normally would not inherit
7134 -- subtype specific attributes of the actual, which is wrong for
7135 -- the generic case.
7137 Astype := Ancestor_Subtype (E);
7139 if No (Astype) then
7141 -- This can happen when E is an itype that is the full view of
7142 -- a private type completed, e.g. with a constrained array. In
7143 -- that case, use the first subtype, which will carry size
7144 -- information. The base type itself is unconstrained and will
7145 -- not carry it.
7147 Astype := First_Subtype (E);
7148 end if;
7150 Set_Size_Info (E, Astype);
7151 Copy_RM_Size (To => E, From => Astype);
7152 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7154 if Is_Discrete_Or_Fixed_Point_Type (E) then
7155 Set_RM_Size (E, RM_Size (Astype));
7156 end if;
7158 elsif Ekind (E) = E_Package then
7160 -- If this is the renaming for the current instance, we're done.
7161 -- Otherwise it is a formal package. If the corresponding formal
7162 -- was declared with a box, the (instantiations of the) generic
7163 -- formal part are also visible. Otherwise, ignore the entity
7164 -- created to validate the actuals.
7166 if Renamed_Entity (E) = Instance then
7167 exit;
7169 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7170 null;
7172 -- The visibility of a formal of an enclosing generic is already
7173 -- correct.
7175 elsif Denotes_Formal_Package (E) then
7176 null;
7178 elsif Present (Associated_Formal_Package (E))
7179 and then not Is_Generic_Formal (E)
7180 then
7181 Check_Generic_Actuals
7182 (Renamed_Entity (E),
7183 Is_Formal_Box =>
7184 Box_Present (Parent (Associated_Formal_Package (E))));
7186 Set_Is_Hidden (E, False);
7187 end if;
7189 -- If this is a subprogram instance (in a wrapper package) the
7190 -- actual is fully visible.
7192 elsif Is_Wrapper_Package (Instance) then
7193 Set_Is_Hidden (E, False);
7195 -- If the formal package is declared with a box, or if the formal
7196 -- parameter is defaulted, it is visible in the body.
7198 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7199 Set_Is_Hidden (E, False);
7200 end if;
7202 -- Check directly the type of the actual objects, including the
7203 -- component type for array types.
7205 if Ekind (E) in E_Constant | E_Variable then
7206 Check_Actual_Type (Etype (E));
7208 if Is_Array_Type (Etype (E)) then
7209 Check_Actual_Type (Component_Type (Etype (E)));
7210 end if;
7212 -- As well as the type of formal parameters of actual subprograms
7214 elsif Ekind (E) in E_Function | E_Procedure
7215 and then Is_Generic_Actual_Subprogram (E)
7216 and then Present (Alias (E))
7217 then
7218 Formal := First_Formal (Alias (E));
7219 while Present (Formal) loop
7220 Check_Actual_Type (Etype (Formal));
7221 Next_Formal (Formal);
7222 end loop;
7223 end if;
7225 Next_Entity (E);
7226 end loop;
7227 end Check_Generic_Actuals;
7229 ------------------------------
7230 -- Check_Generic_Child_Unit --
7231 ------------------------------
7233 procedure Check_Generic_Child_Unit
7234 (Gen_Id : Node_Id;
7235 Parent_Installed : in out Boolean)
7237 Loc : constant Source_Ptr := Sloc (Gen_Id);
7238 Gen_Par : Entity_Id := Empty;
7239 E : Entity_Id;
7240 Inst_Par : Entity_Id := Empty;
7241 S : Node_Id;
7243 function Find_Generic_Child
7244 (Scop : Entity_Id;
7245 Id : Node_Id) return Entity_Id;
7246 -- Search generic parent for possible child unit with the given name
7248 function In_Enclosing_Instance return Boolean;
7249 -- Within an instance of the parent, the child unit may be denoted by
7250 -- a simple name, or an abbreviated expanded name. Examine enclosing
7251 -- scopes to locate a possible parent instantiation.
7253 ------------------------
7254 -- Find_Generic_Child --
7255 ------------------------
7257 function Find_Generic_Child
7258 (Scop : Entity_Id;
7259 Id : Node_Id) return Entity_Id
7261 E : Entity_Id;
7263 begin
7264 -- If entity of name is already set, instance has already been
7265 -- resolved, e.g. in an enclosing instantiation.
7267 if Present (Entity (Id)) then
7268 if Scope (Entity (Id)) = Scop then
7269 return Entity (Id);
7270 else
7271 return Empty;
7272 end if;
7274 else
7275 E := First_Entity (Scop);
7276 while Present (E) loop
7277 if Chars (E) = Chars (Id)
7278 and then Is_Child_Unit (E)
7279 then
7280 if Is_Child_Unit (E)
7281 and then not Is_Visible_Lib_Unit (E)
7282 then
7283 Error_Msg_NE
7284 ("generic child unit& is not visible", Gen_Id, E);
7285 end if;
7287 Set_Entity (Id, E);
7288 return E;
7289 end if;
7291 Next_Entity (E);
7292 end loop;
7294 return Empty;
7295 end if;
7296 end Find_Generic_Child;
7298 ---------------------------
7299 -- In_Enclosing_Instance --
7300 ---------------------------
7302 function In_Enclosing_Instance return Boolean is
7303 Enclosing_Instance : Node_Id;
7304 Instance_Decl : Node_Id;
7306 begin
7307 -- We do not inline any call that contains instantiations, except
7308 -- for instantiations of Unchecked_Conversion, so if we are within
7309 -- an inlined body the current instance does not require parents.
7311 if In_Inlined_Body then
7312 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7313 return False;
7314 end if;
7316 -- Loop to check enclosing scopes
7318 Enclosing_Instance := Current_Scope;
7319 while Present (Enclosing_Instance) loop
7320 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7322 if Ekind (Enclosing_Instance) = E_Package
7323 and then Is_Generic_Instance (Enclosing_Instance)
7324 and then Present
7325 (Generic_Parent (Specification (Instance_Decl)))
7326 then
7327 -- Check whether the generic we are looking for is a child of
7328 -- this instance.
7330 E := Find_Generic_Child
7331 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7332 exit when Present (E);
7334 else
7335 E := Empty;
7336 end if;
7338 Enclosing_Instance := Scope (Enclosing_Instance);
7339 end loop;
7341 if No (E) then
7343 -- Not a child unit
7345 Analyze (Gen_Id);
7346 return False;
7348 else
7349 Rewrite (Gen_Id,
7350 Make_Expanded_Name (Loc,
7351 Chars => Chars (E),
7352 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7353 Selector_Name => New_Occurrence_Of (E, Loc)));
7355 Set_Entity (Gen_Id, E);
7356 Set_Etype (Gen_Id, Etype (E));
7357 Parent_Installed := False; -- Already in scope.
7358 return True;
7359 end if;
7360 end In_Enclosing_Instance;
7362 -- Start of processing for Check_Generic_Child_Unit
7364 begin
7365 -- If the name of the generic is given by a selected component, it may
7366 -- be the name of a generic child unit, and the prefix is the name of an
7367 -- instance of the parent, in which case the child unit must be visible.
7368 -- If this instance is not in scope, it must be placed there and removed
7369 -- after instantiation, because what is being instantiated is not the
7370 -- original child, but the corresponding child present in the instance
7371 -- of the parent.
7373 -- If the child is instantiated within the parent, it can be given by
7374 -- a simple name. In this case the instance is already in scope, but
7375 -- the child generic must be recovered from the generic parent as well.
7377 if Nkind (Gen_Id) = N_Selected_Component then
7378 S := Selector_Name (Gen_Id);
7379 Analyze (Prefix (Gen_Id));
7380 Inst_Par := Entity (Prefix (Gen_Id));
7382 if Ekind (Inst_Par) = E_Package
7383 and then Present (Renamed_Entity (Inst_Par))
7384 then
7385 Inst_Par := Renamed_Entity (Inst_Par);
7386 end if;
7388 if Ekind (Inst_Par) = E_Package then
7389 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7390 Gen_Par := Generic_Parent (Parent (Inst_Par));
7392 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7393 and then
7394 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7395 then
7396 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7397 end if;
7399 elsif Ekind (Inst_Par) = E_Generic_Package
7400 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7401 then
7402 -- A formal package may be a real child package, and not the
7403 -- implicit instance within a parent. In this case the child is
7404 -- not visible and has to be retrieved explicitly as well.
7406 Gen_Par := Inst_Par;
7407 end if;
7409 if Present (Gen_Par) then
7411 -- The prefix denotes an instantiation. The entity itself may be a
7412 -- nested generic, or a child unit.
7414 E := Find_Generic_Child (Gen_Par, S);
7416 if Present (E) then
7417 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7418 Set_Entity (Gen_Id, E);
7419 Set_Etype (Gen_Id, Etype (E));
7420 Set_Entity (S, E);
7421 Set_Etype (S, Etype (E));
7423 -- Indicate that this is a reference to the parent
7425 if In_Extended_Main_Source_Unit (Gen_Id) then
7426 Set_Is_Instantiated (Inst_Par);
7427 end if;
7429 -- A common mistake is to replicate the naming scheme of a
7430 -- hierarchy by instantiating a generic child directly, rather
7431 -- than the implicit child in a parent instance:
7433 -- generic .. package Gpar is ..
7434 -- generic .. package Gpar.Child is ..
7435 -- package Par is new Gpar ();
7437 -- with Gpar.Child;
7438 -- package Par.Child is new Gpar.Child ();
7439 -- rather than Par.Child
7441 -- In this case the instantiation is within Par, which is an
7442 -- instance, but Gpar does not denote Par because we are not IN
7443 -- the instance of Gpar, so this is illegal. The test below
7444 -- recognizes this particular case.
7446 declare
7447 -- We want to reject the final instantiation in
7448 -- generic package G1 is end G1;
7449 -- generic package G1.G2 is end G1.G2;
7450 -- with G1; package I1 is new G1;
7451 -- with G1.G2; package I1.I2 is new G1.G2;
7452 -- because the use of G1.G2 should instead be either
7453 -- I1.G2 or simply G2. However, the tree that is built
7454 -- in this case is wrong. In the expanded copy
7455 -- of G2, we need (and therefore generate) a renaming
7456 -- package G1 renames I1;
7457 -- but this renaming should not participate in resolving
7458 -- this occurrence of the name "G1.G2"; unfortunately,
7459 -- it does. Rather than correct this error, we compensate
7460 -- for it in this function.
7462 -- We also perform another adjustment here. If we are
7463 -- currently inside a generic package, then that
7464 -- generic package needs to be treated as a package.
7465 -- For example, if a generic Aaa declares a nested generic
7466 -- Bbb (perhaps as a child unit) then Aaa can also legally
7467 -- declare an instance of Aaa.Bbb.
7469 function Adjusted_Inst_Par_Ekind return Entity_Kind;
7471 -----------------------------
7472 -- Adjusted_Inst_Par_Ekind --
7473 -----------------------------
7475 function Adjusted_Inst_Par_Ekind return Entity_Kind is
7476 Prefix_Entity : Entity_Id;
7477 Inst_Par_GP : Node_Id;
7478 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
7479 begin
7480 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
7481 then
7482 Inst_Par_Parent := Parent (Inst_Par_Parent);
7483 end if;
7485 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
7487 if Nkind (Gen_Id) = N_Expanded_Name
7488 and then Present (Inst_Par_GP)
7489 and then Ekind (Inst_Par_GP) = E_Generic_Package
7490 then
7491 Prefix_Entity := Entity (Prefix (Gen_Id));
7493 if Present (Prefix_Entity)
7494 and then not Comes_From_Source (Prefix_Entity)
7495 and then Nkind (Parent (Prefix_Entity)) =
7496 N_Package_Renaming_Declaration
7497 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
7498 then
7499 return E_Generic_Package;
7500 end if;
7501 end if;
7503 if Ekind (Inst_Par) = E_Generic_Package
7504 and then In_Open_Scopes (Inst_Par)
7505 then
7506 -- If we are inside a generic package then
7507 -- treat it as a package.
7508 return E_Package;
7509 end if;
7511 -- The usual path
7512 return Ekind (Inst_Par);
7513 end Adjusted_Inst_Par_Ekind;
7515 begin
7516 if Is_Child_Unit (E)
7517 and then (No (Inst_Par)
7518 or else Adjusted_Inst_Par_Ekind =
7519 E_Generic_Package)
7520 and then (not In_Instance
7521 or else Nkind (Parent (Parent (Gen_Id))) =
7522 N_Compilation_Unit)
7523 then
7524 Error_Msg_N
7525 ("prefix of generic child unit must be " &
7526 "instance of parent",
7527 Gen_Id);
7528 end if;
7529 end;
7531 if not In_Open_Scopes (Inst_Par)
7532 and then Nkind (Parent (Gen_Id)) not in
7533 N_Generic_Renaming_Declaration
7534 then
7535 Install_Parent (Inst_Par);
7536 Parent_Installed := True;
7538 elsif In_Open_Scopes (Inst_Par) then
7540 -- If the parent is already installed, install the actuals
7541 -- for its formal packages. This is necessary when the child
7542 -- instance is a child of the parent instance: in this case,
7543 -- the parent is placed on the scope stack but the formal
7544 -- packages are not made visible.
7546 Install_Formal_Packages (Inst_Par);
7547 end if;
7549 else
7550 -- If the generic parent does not contain an entity that
7551 -- corresponds to the selector, the instance doesn't either.
7552 -- Analyzing the node will yield the appropriate error message.
7553 -- If the entity is not a child unit, then it is an inner
7554 -- generic in the parent.
7556 Analyze (Gen_Id);
7557 end if;
7559 else
7560 Analyze (Gen_Id);
7562 if Is_Child_Unit (Entity (Gen_Id))
7563 and then
7564 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7565 and then not In_Open_Scopes (Inst_Par)
7566 then
7567 Install_Parent (Inst_Par);
7568 Parent_Installed := True;
7570 -- The generic unit may be the renaming of the implicit child
7571 -- present in an instance. In that case the parent instance is
7572 -- obtained from the name of the renamed entity.
7574 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7575 and then Present (Renamed_Entity (Entity (Gen_Id)))
7576 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7577 then
7578 declare
7579 Renamed_Package : constant Node_Id :=
7580 Name (Parent (Entity (Gen_Id)));
7581 begin
7582 if Nkind (Renamed_Package) = N_Expanded_Name then
7583 Inst_Par := Entity (Prefix (Renamed_Package));
7584 Install_Parent (Inst_Par);
7585 Parent_Installed := True;
7586 end if;
7587 end;
7588 end if;
7589 end if;
7591 elsif Nkind (Gen_Id) = N_Expanded_Name then
7593 -- Entity already present, analyze prefix, whose meaning may be an
7594 -- instance in the current context. If it is an instance of a
7595 -- relative within another, the proper parent may still have to be
7596 -- installed, if they are not of the same generation.
7598 Analyze (Prefix (Gen_Id));
7600 -- Prevent cascaded errors
7602 if Etype (Prefix (Gen_Id)) = Any_Type then
7603 return;
7604 end if;
7606 -- In the unlikely case that a local declaration hides the name of
7607 -- the parent package, locate it on the homonym chain. If the context
7608 -- is an instance of the parent, the renaming entity is flagged as
7609 -- such.
7611 Inst_Par := Entity (Prefix (Gen_Id));
7612 while Present (Inst_Par)
7613 and then not Is_Package_Or_Generic_Package (Inst_Par)
7614 loop
7615 Inst_Par := Homonym (Inst_Par);
7616 end loop;
7618 pragma Assert (Present (Inst_Par));
7619 Set_Entity (Prefix (Gen_Id), Inst_Par);
7621 if In_Enclosing_Instance then
7622 null;
7624 elsif Present (Entity (Gen_Id))
7625 and then No (Renamed_Entity (Entity (Gen_Id)))
7626 and then Is_Child_Unit (Entity (Gen_Id))
7627 and then not In_Open_Scopes (Inst_Par)
7628 then
7629 Install_Parent (Inst_Par);
7630 Parent_Installed := True;
7632 -- Handle renaming of generic child unit
7634 elsif Present (Entity (Gen_Id))
7635 and then Present (Renamed_Entity (Entity (Gen_Id)))
7636 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7637 then
7638 declare
7639 E : Entity_Id;
7640 Ren_Decl : Node_Id;
7642 begin
7643 -- The entity of the renamed generic child unit does not
7644 -- have any reference to the instantiated parent. In order to
7645 -- locate it we traverse the scope containing the renaming
7646 -- declaration; the instance of the parent is available in
7647 -- the prefix of the renaming declaration. For example:
7649 -- package A is
7650 -- package Inst_Par is new ...
7651 -- generic package Ren_Child renames Ins_Par.Child;
7652 -- end;
7654 -- with A;
7655 -- package B is
7656 -- package Inst_Child is new A.Ren_Child;
7657 -- end;
7659 E := First_Entity (Entity (Prefix (Gen_Id)));
7660 while Present (E) loop
7661 if not Is_Object (E)
7662 and then Present (Renamed_Entity (E))
7663 and then
7664 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7665 then
7666 Ren_Decl := Parent (E);
7667 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7669 if not In_Open_Scopes (Inst_Par) then
7670 Install_Parent (Inst_Par);
7671 Parent_Installed := True;
7672 end if;
7674 exit;
7675 end if;
7677 E := Next_Entity (E);
7678 end loop;
7679 end;
7680 end if;
7682 elsif In_Enclosing_Instance then
7684 -- The child unit is found in some enclosing scope
7686 null;
7688 else
7689 Analyze (Gen_Id);
7691 -- If this is the renaming of the implicit child in a parent
7692 -- instance, recover the parent name and install it.
7694 if Is_Entity_Name (Gen_Id) then
7695 E := Entity (Gen_Id);
7697 if Is_Generic_Unit (E)
7698 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7699 and then Is_Child_Unit (Renamed_Entity (E))
7700 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7701 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7702 then
7703 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7704 Inst_Par := Entity (Prefix (Gen_Id));
7706 if not In_Open_Scopes (Inst_Par) then
7707 Install_Parent (Inst_Par);
7708 Parent_Installed := True;
7709 end if;
7711 -- If it is a child unit of a non-generic parent, it may be
7712 -- use-visible and given by a direct name. Install parent as
7713 -- for other cases.
7715 elsif Is_Generic_Unit (E)
7716 and then Is_Child_Unit (E)
7717 and then
7718 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7719 and then not Is_Generic_Unit (Scope (E))
7720 then
7721 if not In_Open_Scopes (Scope (E)) then
7722 Install_Parent (Scope (E));
7723 Parent_Installed := True;
7724 end if;
7725 end if;
7726 end if;
7727 end if;
7728 end Check_Generic_Child_Unit;
7730 -----------------------------
7731 -- Check_Hidden_Child_Unit --
7732 -----------------------------
7734 procedure Check_Hidden_Child_Unit
7735 (N : Node_Id;
7736 Gen_Unit : Entity_Id;
7737 Act_Decl_Id : Entity_Id)
7739 Gen_Id : constant Node_Id := Name (N);
7741 begin
7742 if Is_Child_Unit (Gen_Unit)
7743 and then Is_Child_Unit (Act_Decl_Id)
7744 and then Nkind (Gen_Id) = N_Expanded_Name
7745 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7746 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7747 then
7748 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7749 Error_Msg_NE
7750 ("generic unit & is implicitly declared in &",
7751 Defining_Unit_Name (N), Gen_Unit);
7752 Error_Msg_N ("\instance must have different name",
7753 Defining_Unit_Name (N));
7754 end if;
7755 end Check_Hidden_Child_Unit;
7757 ------------------------
7758 -- Check_Private_View --
7759 ------------------------
7761 procedure Check_Private_View (N : Node_Id) is
7762 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
7763 Typ : constant Entity_Id :=
7764 (if Comparison then Compare_Type (N) else Etype (N));
7766 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
7767 -- Check that the available view of T matches Private_View and, if not,
7768 -- switch the view of T or of its base type.
7770 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
7771 BT : constant Entity_Id := Base_Type (T);
7773 begin
7774 -- If the full declaration was not visible in the generic, stop here
7776 if Private_View then
7777 return;
7778 end if;
7780 -- Exchange views if the type was not private in the generic but is
7781 -- private at the point of instantiation. Do not exchange views if
7782 -- the scope of the type is in scope. This can happen if both generic
7783 -- and instance are sibling units, or if type is defined in a parent.
7784 -- In this case the visibility of the type will be correct for all
7785 -- semantic checks.
7787 if Is_Private_Type (T)
7788 and then Present (Full_View (T))
7789 and then not In_Open_Scopes (Scope (T))
7790 then
7791 Switch_View (T);
7793 -- Finally, a nonprivate subtype may have a private base type, which
7794 -- must be exchanged for consistency. This can happen when a package
7795 -- body is instantiated, when the scope stack is empty but in fact
7796 -- the subtype and the base type are declared in an enclosing scope.
7798 -- Note that in this case we introduce an inconsistency in the view
7799 -- set, because we switch the base type BT, but there could be some
7800 -- private dependent subtypes of BT which remain unswitched. Such
7801 -- subtypes might need to be switched at a later point (see specific
7802 -- provision for that case in Switch_View).
7804 elsif not Is_Private_Type (T)
7805 and then Is_Private_Type (BT)
7806 and then Present (Full_View (BT))
7807 and then not In_Open_Scopes (BT)
7808 then
7809 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7810 Exchange_Declarations (BT);
7811 end if;
7812 end Check_Private_Type;
7814 begin
7815 if Present (Typ) then
7816 -- If the type appears in a subtype declaration, the subtype in
7817 -- instance must have a view compatible with that of its parent,
7818 -- which must be exchanged (see corresponding code in Restore_
7819 -- Private_Views) so we make an exception to the open scope rule
7820 -- implemented by Check_Private_Type above.
7822 if Has_Private_View (N)
7823 and then not Is_Private_Type (Typ)
7824 and then not Has_Been_Exchanged (Typ)
7825 and then (not In_Open_Scopes (Scope (Typ))
7826 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7827 then
7828 declare
7829 Assoc : constant Node_Id := Get_Associated_Node (N);
7831 begin
7832 -- In the generic, only the private declaration was visible
7834 Prepend_Elmt (Typ, Exchanged_Views);
7835 Exchange_Declarations
7836 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
7837 end;
7839 -- Check that the available views of Typ match their respective flag.
7840 -- Note that the type of a visible discriminant is never private.
7842 else
7843 Check_Private_Type (Typ, Has_Private_View (N));
7845 if Is_Access_Type (Typ) then
7846 Check_Private_Type
7847 (Designated_Type (Typ), Has_Secondary_Private_View (N));
7849 elsif Is_Array_Type (Typ) then
7850 Check_Private_Type
7851 (Component_Type_For_Private_View (Typ),
7852 Has_Secondary_Private_View (N));
7854 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
7855 and then Has_Discriminants (Typ)
7856 then
7857 declare
7858 Disc : Entity_Id;
7860 begin
7861 Disc := First_Discriminant (Typ);
7862 while Present (Disc) loop
7863 Check_Private_Type (Etype (Disc), False);
7864 Next_Discriminant (Disc);
7865 end loop;
7866 end;
7867 end if;
7868 end if;
7869 end if;
7870 end Check_Private_View;
7872 -----------------------------
7873 -- Check_Hidden_Primitives --
7874 -----------------------------
7876 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7877 Actual : Node_Id;
7878 Gen_T : Entity_Id;
7879 Result : Elist_Id := No_Elist;
7881 begin
7882 if No (Assoc_List) then
7883 return No_Elist;
7884 end if;
7886 -- Traverse the list of associations between formals and actuals
7887 -- searching for renamings of tagged types
7889 Actual := First (Assoc_List);
7890 while Present (Actual) loop
7891 if Nkind (Actual) = N_Subtype_Declaration then
7892 Gen_T := Generic_Parent_Type (Actual);
7894 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7896 -- Traverse the list of primitives of the actual types
7897 -- searching for hidden primitives that are visible in the
7898 -- corresponding generic formal; leave them visible and
7899 -- append them to Result to restore their decoration later.
7901 Install_Hidden_Primitives
7902 (Prims_List => Result,
7903 Gen_T => Gen_T,
7904 Act_T => Entity (Subtype_Indication (Actual)));
7905 end if;
7906 end if;
7908 Next (Actual);
7909 end loop;
7911 return Result;
7912 end Check_Hidden_Primitives;
7914 -------------------------------------
7915 -- Component_Type_For_Private_View --
7916 -------------------------------------
7918 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
7919 Typ : constant Entity_Id := Component_Type (T);
7921 begin
7922 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
7923 return Component_Type_For_Private_View (Typ);
7924 else
7925 return Typ;
7926 end if;
7927 end Component_Type_For_Private_View;
7929 --------------------------
7930 -- Contains_Instance_Of --
7931 --------------------------
7933 function Contains_Instance_Of
7934 (Inner : Entity_Id;
7935 Outer : Entity_Id;
7936 N : Node_Id) return Boolean
7938 Elmt : Elmt_Id;
7939 Scop : Entity_Id;
7941 begin
7942 Scop := Outer;
7944 -- Verify that there are no circular instantiations. We check whether
7945 -- the unit contains an instance of the current scope or some enclosing
7946 -- scope (in case one of the instances appears in a subunit). Longer
7947 -- circularities involving subunits might seem too pathological to
7948 -- consider, but they were not too pathological for the authors of
7949 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7950 -- enclosing generic scopes as containing an instance.
7952 loop
7953 -- Within a generic subprogram body, the scope is not generic, to
7954 -- allow for recursive subprograms. Use the declaration to determine
7955 -- whether this is a generic unit.
7957 if Ekind (Scop) = E_Generic_Package
7958 or else (Is_Subprogram (Scop)
7959 and then Nkind (Unit_Declaration_Node (Scop)) =
7960 N_Generic_Subprogram_Declaration)
7961 then
7962 Elmt := First_Elmt (Inner_Instances (Inner));
7964 while Present (Elmt) loop
7965 if Node (Elmt) = Scop then
7966 Error_Msg_Node_2 := Inner;
7967 Error_Msg_NE
7968 ("circular instantiation: & instantiated within &!",
7969 N, Scop);
7970 return True;
7972 elsif Node (Elmt) = Inner then
7973 return True;
7975 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7976 Error_Msg_Node_2 := Inner;
7977 Error_Msg_NE
7978 ("circular instantiation: & instantiated within &!",
7979 N, Node (Elmt));
7980 return True;
7981 end if;
7983 Next_Elmt (Elmt);
7984 end loop;
7986 -- Indicate that Inner is being instantiated within Scop
7988 Append_Elmt (Inner, Inner_Instances (Scop));
7989 end if;
7991 if Scop = Standard_Standard then
7992 exit;
7993 else
7994 Scop := Scope (Scop);
7995 end if;
7996 end loop;
7998 return False;
7999 end Contains_Instance_Of;
8001 -----------------------
8002 -- Copy_Generic_Node --
8003 -----------------------
8005 function Copy_Generic_Node
8006 (N : Node_Id;
8007 Parent_Id : Node_Id;
8008 Instantiating : Boolean) return Node_Id
8010 Ent : Entity_Id;
8011 New_N : Node_Id;
8013 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8014 -- Check the given value of one of the Fields referenced by the current
8015 -- node to determine whether to copy it recursively. The field may hold
8016 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8017 -- Char) in which case it need not be copied.
8019 procedure Copy_Descendants;
8020 -- Common utility for various nodes
8022 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8023 -- Make copy of element list
8025 function Copy_Generic_List
8026 (L : List_Id;
8027 Parent_Id : Node_Id) return List_Id;
8028 -- Apply Copy_Generic_Node recursively to the members of a node list
8030 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8031 -- True if an identifier is part of the defining program unit name of
8032 -- a child unit.
8033 -- Consider removing this subprogram now that ASIS no longer uses it.
8035 ----------------------
8036 -- Copy_Descendants --
8037 ----------------------
8039 procedure Copy_Descendants is
8040 procedure Walk is new
8041 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8042 begin
8043 Walk (New_N, N);
8044 end Copy_Descendants;
8046 -----------------------------
8047 -- Copy_Generic_Descendant --
8048 -----------------------------
8050 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8051 begin
8052 if D = Union_Id (Empty) then
8053 return D;
8055 elsif D in Node_Range then
8056 return Union_Id
8057 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8059 elsif D in List_Range then
8060 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8062 elsif D in Elist_Range then
8063 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8065 -- Nothing else is copyable (e.g. Uint values), return as is
8067 else
8068 return D;
8069 end if;
8070 end Copy_Generic_Descendant;
8072 ------------------------
8073 -- Copy_Generic_Elist --
8074 ------------------------
8076 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8077 M : Elmt_Id;
8078 L : Elist_Id;
8080 begin
8081 if Present (E) then
8082 L := New_Elmt_List;
8083 M := First_Elmt (E);
8084 while Present (M) loop
8085 Append_Elmt
8086 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8087 Next_Elmt (M);
8088 end loop;
8090 return L;
8092 else
8093 return No_Elist;
8094 end if;
8095 end Copy_Generic_Elist;
8097 -----------------------
8098 -- Copy_Generic_List --
8099 -----------------------
8101 function Copy_Generic_List
8102 (L : List_Id;
8103 Parent_Id : Node_Id) return List_Id
8105 N : Node_Id;
8106 New_L : List_Id;
8108 begin
8109 if Present (L) then
8110 New_L := New_List;
8111 Set_Parent (New_L, Parent_Id);
8113 N := First (L);
8114 while Present (N) loop
8115 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8116 Next (N);
8117 end loop;
8119 return New_L;
8121 else
8122 return No_List;
8123 end if;
8124 end Copy_Generic_List;
8126 ---------------------------
8127 -- In_Defining_Unit_Name --
8128 ---------------------------
8130 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8131 begin
8132 return
8133 Present (Parent (Nam))
8134 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8135 or else
8136 (Nkind (Parent (Nam)) = N_Expanded_Name
8137 and then In_Defining_Unit_Name (Parent (Nam))));
8138 end In_Defining_Unit_Name;
8140 -- Start of processing for Copy_Generic_Node
8142 begin
8143 if N = Empty then
8144 return N;
8145 end if;
8147 New_N := New_Copy (N);
8149 -- If we are instantiating, we want to adjust the sloc based on the
8150 -- current S_Adjustment. However, if this is the root node of a subunit,
8151 -- we need to defer that adjustment to below (see "elsif Instantiating
8152 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8153 -- computed the adjustment.
8155 if Instantiating
8156 and then not (Nkind (N) in N_Proper_Body
8157 and then Was_Originally_Stub (N))
8158 then
8159 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8160 end if;
8162 if not Is_List_Member (N) then
8163 Set_Parent (New_N, Parent_Id);
8164 end if;
8166 -- Special casing for identifiers and other entity names and operators
8168 if Nkind (N) in N_Character_Literal
8169 | N_Expanded_Name
8170 | N_Identifier
8171 | N_Operator_Symbol
8172 | N_Op
8173 then
8174 if not Instantiating then
8176 -- Link both nodes in order to assign subsequently the entity of
8177 -- the copy to the original node, in case this is a global
8178 -- reference.
8180 Set_Associated_Node (N, New_N);
8182 -- If we are within an instantiation, this is a nested generic
8183 -- that has already been analyzed at the point of definition.
8184 -- We must preserve references that were global to the enclosing
8185 -- parent at that point. Other occurrences, whether global or
8186 -- local to the current generic, must be resolved anew, so we
8187 -- reset the entity in the generic copy. A global reference has a
8188 -- smaller depth than the parent, or else the same depth in case
8189 -- both are distinct compilation units.
8191 -- A child unit is implicitly declared within the enclosing parent
8192 -- but is in fact global to it, and must be preserved.
8194 -- It is also possible for Current_Instantiated_Parent to be
8195 -- defined, and for this not to be a nested generic, namely if
8196 -- the unit is loaded through Rtsfind. In that case, the entity of
8197 -- New_N is only a link to the associated node, and not a defining
8198 -- occurrence.
8200 -- The entities for parent units in the defining_program_unit of a
8201 -- generic child unit are established when the context of the unit
8202 -- is first analyzed, before the generic copy is made. They are
8203 -- preserved in the copy for use in e.g. ASIS queries.
8205 Ent := Entity (New_N);
8207 if No (Current_Instantiated_Parent.Gen_Id) then
8208 if No (Ent)
8209 or else Nkind (Ent) /= N_Defining_Identifier
8210 or else not In_Defining_Unit_Name (N)
8211 then
8212 Set_Associated_Node (New_N, Empty);
8213 end if;
8215 elsif No (Ent)
8216 or else Nkind (Ent) not in N_Entity
8217 or else No (Scope (Ent))
8218 or else
8219 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8220 and then not Is_Child_Unit (Ent))
8221 or else
8222 (Scope_Depth_Set (Scope (Ent))
8223 and then
8224 Scope_Depth (Scope (Ent)) >
8225 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8226 and then
8227 Get_Source_Unit (Ent) =
8228 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8229 then
8230 Set_Associated_Node (New_N, Empty);
8231 end if;
8233 -- Case of instantiating identifier or some other name or operator
8235 else
8236 -- If the associated node is still defined, the entity in it
8237 -- is global, and must be copied to the instance. If this copy
8238 -- is being made for a body to inline, it is applied to an
8239 -- instantiated tree, and the entity is already present and
8240 -- must be also preserved.
8242 declare
8243 Assoc : constant Node_Id := Get_Associated_Node (N);
8245 begin
8246 if Present (Assoc) then
8247 if Nkind (Assoc) = Nkind (N) then
8248 Set_Entity (New_N, Entity (Assoc));
8249 Check_Private_View (N);
8251 -- The node is a reference to a global type and acts as the
8252 -- subtype mark of a qualified expression created in order
8253 -- to aid resolution of accidental overloading in instances.
8254 -- Since N is a reference to a type, the Associated_Node of
8255 -- N denotes an entity rather than another identifier. See
8256 -- Qualify_Universal_Operands for details.
8258 elsif Nkind (N) = N_Identifier
8259 and then Nkind (Parent (N)) = N_Qualified_Expression
8260 and then Subtype_Mark (Parent (N)) = N
8261 and then Is_Qualified_Universal_Literal (Parent (N))
8262 then
8263 Set_Entity (New_N, Assoc);
8265 -- Cope with the rewriting into expanded name that may have
8266 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8267 -- generic renaming declarations.
8269 elsif Nkind (Assoc) = N_Expanded_Name then
8270 Rewrite (N, New_Copy_Tree (Assoc));
8271 Set_Associated_Node (N, Assoc);
8272 return Copy_Generic_Node (N, Parent_Id, Instantiating);
8274 -- The name in the call may be a selected component if the
8275 -- call has not been analyzed yet, as may be the case for
8276 -- pre/post conditions in a generic unit.
8278 elsif Nkind (Assoc) = N_Function_Call
8279 and then Is_Entity_Name (Name (Assoc))
8280 then
8281 Set_Entity (New_N, Entity (Name (Assoc)));
8282 Check_Private_View (N);
8284 elsif Nkind (Assoc) in N_Entity
8285 and then (Expander_Active
8286 or else (GNATprove_Mode
8287 and then not In_Spec_Expression
8288 and then not Inside_A_Generic))
8289 then
8290 -- Inlining case: we are copying a tree that contains
8291 -- global entities, which are preserved in the copy to be
8292 -- used for subsequent inlining.
8294 null;
8296 else
8297 Set_Entity (New_N, Empty);
8298 end if;
8299 end if;
8300 end;
8301 end if;
8303 -- For expanded name, we must copy the Prefix and Selector_Name
8305 if Nkind (N) = N_Expanded_Name then
8306 Set_Prefix
8307 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8309 Set_Selector_Name (New_N,
8310 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8312 -- For operators, copy the operands
8314 elsif Nkind (N) in N_Op then
8315 if Nkind (N) in N_Binary_Op then
8316 Set_Left_Opnd (New_N,
8317 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8318 end if;
8320 Set_Right_Opnd (New_N,
8321 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8322 end if;
8324 -- Establish a link between an entity from the generic template and the
8325 -- corresponding entity in the generic copy to be analyzed.
8327 elsif Nkind (N) in N_Entity then
8328 if not Instantiating then
8329 Set_Associated_Entity (N, New_N);
8330 end if;
8332 -- Clear any existing link the copy may inherit from the replicated
8333 -- generic template entity.
8335 Set_Associated_Entity (New_N, Empty);
8337 -- Special casing for stubs
8339 elsif Nkind (N) in N_Body_Stub then
8341 -- In any case, we must copy the specification or defining
8342 -- identifier as appropriate.
8344 if Nkind (N) = N_Subprogram_Body_Stub then
8345 Set_Specification (New_N,
8346 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8348 else
8349 Set_Defining_Identifier (New_N,
8350 Copy_Generic_Node
8351 (Defining_Identifier (N), New_N, Instantiating));
8352 end if;
8354 -- If we are not instantiating, then this is where we load and
8355 -- analyze subunits, i.e. at the point where the stub occurs. A
8356 -- more permissive system might defer this analysis to the point
8357 -- of instantiation, but this seems too complicated for now.
8359 if not Instantiating then
8360 declare
8361 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8362 Subunit : Node_Id;
8363 Unum : Unit_Number_Type;
8364 New_Body : Node_Id;
8366 begin
8367 -- Make sure that, if it is a subunit of the main unit that is
8368 -- preprocessed and if -gnateG is specified, the preprocessed
8369 -- file will be written.
8371 Lib.Analysing_Subunit_Of_Main :=
8372 Lib.In_Extended_Main_Source_Unit (N);
8373 Unum :=
8374 Load_Unit
8375 (Load_Name => Subunit_Name,
8376 Required => False,
8377 Subunit => True,
8378 Error_Node => N);
8379 Lib.Analysing_Subunit_Of_Main := False;
8381 -- If the proper body is not found, a warning message will be
8382 -- emitted when analyzing the stub, or later at the point of
8383 -- instantiation. Here we just leave the stub as is.
8385 if Unum = No_Unit then
8386 Subunits_Missing := True;
8387 goto Subunit_Not_Found;
8388 end if;
8390 Subunit := Cunit (Unum);
8392 if Nkind (Unit (Subunit)) /= N_Subunit then
8393 Error_Msg_N
8394 ("found child unit instead of expected SEPARATE subunit",
8395 Subunit);
8396 Error_Msg_Sloc := Sloc (N);
8397 Error_Msg_N ("\to complete stub #", Subunit);
8398 goto Subunit_Not_Found;
8399 end if;
8401 -- We must create a generic copy of the subunit, in order to
8402 -- perform semantic analysis on it, and we must replace the
8403 -- stub in the original generic unit with the subunit, in order
8404 -- to preserve non-local references within.
8406 -- Only the proper body needs to be copied. Library_Unit and
8407 -- context clause are simply inherited by the generic copy.
8408 -- Note that the copy (which may be recursive if there are
8409 -- nested subunits) must be done first, before attaching it to
8410 -- the enclosing generic.
8412 New_Body :=
8413 Copy_Generic_Node
8414 (Proper_Body (Unit (Subunit)),
8415 Empty, Instantiating => False);
8417 -- Now place the original proper body in the original generic
8418 -- unit. This is a body, not a compilation unit.
8420 Rewrite (N, Proper_Body (Unit (Subunit)));
8421 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8422 Set_Was_Originally_Stub (N);
8424 -- Finally replace the body of the subunit with its copy, and
8425 -- make this new subunit into the library unit of the generic
8426 -- copy, which does not have stubs any longer.
8428 Set_Proper_Body (Unit (Subunit), New_Body);
8429 Set_Library_Unit (New_N, Subunit);
8430 Inherit_Context (Unit (Subunit), N);
8431 end;
8433 -- If we are instantiating, this must be an error case, since
8434 -- otherwise we would have replaced the stub node by the proper body
8435 -- that corresponds. So just ignore it in the copy (i.e. we have
8436 -- copied it, and that is good enough).
8438 else
8439 null;
8440 end if;
8442 <<Subunit_Not_Found>> null;
8444 -- If the node is a compilation unit, it is the subunit of a stub, which
8445 -- has been loaded already (see code below). In this case, the library
8446 -- unit field of N points to the parent unit (which is a compilation
8447 -- unit) and need not (and cannot) be copied.
8449 -- When the proper body of the stub is analyzed, the library_unit link
8450 -- is used to establish the proper context (see sem_ch10).
8452 -- The other fields of a compilation unit are copied as usual
8454 elsif Nkind (N) = N_Compilation_Unit then
8456 -- This code can only be executed when not instantiating, because in
8457 -- the copy made for an instantiation, the compilation unit node has
8458 -- disappeared at the point that a stub is replaced by its proper
8459 -- body.
8461 pragma Assert (not Instantiating);
8463 Set_Context_Items (New_N,
8464 Copy_Generic_List (Context_Items (N), New_N));
8466 Set_Unit (New_N,
8467 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8469 Set_First_Inlined_Subprogram (New_N,
8470 Copy_Generic_Node
8471 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8473 Set_Aux_Decls_Node
8474 (New_N,
8475 Copy_Generic_Node
8476 (Aux_Decls_Node (N), New_N, Instantiating => False));
8478 -- For an assignment node, the assignment is known to be semantically
8479 -- legal if we are instantiating the template. This avoids incorrect
8480 -- diagnostics in generated code.
8482 elsif Nkind (N) = N_Assignment_Statement then
8484 -- Copy name and expression fields in usual manner
8486 Set_Name (New_N,
8487 Copy_Generic_Node (Name (N), New_N, Instantiating));
8489 Set_Expression (New_N,
8490 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8492 if Instantiating then
8493 Set_Assignment_OK (Name (New_N), True);
8494 end if;
8496 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8497 if not Instantiating then
8498 Set_Associated_Node (N, New_N);
8500 else
8501 -- If, in the generic, the aggregate has a global composite type
8502 -- and, at the point of instantiation, the type has a private view
8503 -- then install the full view.
8505 declare
8506 Assoc : constant Node_Id := Get_Associated_Node (N);
8508 begin
8509 if Present (Assoc)
8510 and then Nkind (Assoc) = Nkind (N)
8511 and then Present (Etype (Assoc))
8512 and then Is_Private_Type (Etype (Assoc))
8513 then
8514 Switch_View (Etype (Assoc));
8515 end if;
8516 end;
8518 -- Moreover, for a full aggregate, if the type is a derived tagged
8519 -- type and has a global ancestor, then also restore the full view
8520 -- of this ancestor and do so up to the root type. Beware that the
8521 -- Ancestor_Type field is overloaded, so test that it's an entity.
8523 if Nkind (N) = N_Aggregate
8524 and then Present (Ancestor_Type (N))
8525 and then Nkind (Ancestor_Type (N)) in N_Entity
8526 then
8527 declare
8528 Root_Typ : constant Entity_Id :=
8529 Root_Type (Ancestor_Type (N));
8531 Typ : Entity_Id := Ancestor_Type (N);
8533 begin
8534 loop
8535 if Is_Private_Type (Typ) then
8536 Switch_View (Typ);
8537 end if;
8539 exit when Typ = Root_Typ;
8541 Typ := Etype (Typ);
8542 end loop;
8543 end;
8544 end if;
8545 end if;
8547 -- Do not copy the associated node, which points to the generic copy
8548 -- of the aggregate.
8550 if Nkind (N) = N_Aggregate then
8551 Set_Aggregate_Bounds
8552 (New_N,
8553 Node_Id (Copy_Generic_Descendant
8554 (Union_Id (Aggregate_Bounds (N)))));
8556 elsif Nkind (N) = N_Extension_Aggregate then
8557 Set_Ancestor_Part
8558 (New_N,
8559 Node_Id (Copy_Generic_Descendant
8560 (Union_Id (Ancestor_Part (N)))));
8562 else
8563 pragma Assert (False);
8564 end if;
8566 Set_Expressions
8567 (New_N,
8568 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8569 Set_Component_Associations
8570 (New_N,
8571 List_Id (Copy_Generic_Descendant
8572 (Union_Id (Component_Associations (N)))));
8573 Set_Etype
8574 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8576 -- Allocators do not have an identifier denoting the access type, so we
8577 -- must locate it through the expression to check whether the views are
8578 -- consistent.
8580 elsif Nkind (N) = N_Allocator
8581 and then Nkind (Expression (N)) = N_Qualified_Expression
8582 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8583 and then Instantiating
8584 then
8585 declare
8586 T : constant Node_Id :=
8587 Get_Associated_Node (Subtype_Mark (Expression (N)));
8588 Acc_T : Entity_Id;
8590 begin
8591 if Present (T) then
8593 -- Retrieve the allocator node in the generic copy
8595 Acc_T := Etype (Parent (Parent (T)));
8597 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8598 Switch_View (Acc_T);
8599 end if;
8600 end if;
8602 Copy_Descendants;
8603 end;
8605 -- Loop parameter specifications do not have an identifier denoting the
8606 -- index type, so we must locate it through the defining identifier to
8607 -- check whether the views are consistent.
8609 elsif Nkind (N) = N_Loop_Parameter_Specification
8610 and then Instantiating
8611 then
8612 declare
8613 Id : constant Entity_Id :=
8614 Get_Associated_Entity (Defining_Identifier (N));
8616 Index_T : Entity_Id;
8618 begin
8619 if Present (Id) and then Present (Etype (Id)) then
8620 Index_T := First_Subtype (Etype (Id));
8622 if Present (Index_T) and then Is_Private_Type (Index_T) then
8623 Switch_View (Index_T);
8624 end if;
8625 end if;
8627 Copy_Descendants;
8628 end;
8630 -- For a proper body, we must catch the case of a proper body that
8631 -- replaces a stub. This represents the point at which a separate
8632 -- compilation unit, and hence template file, may be referenced, so we
8633 -- must make a new source instantiation entry for the template of the
8634 -- subunit, and ensure that all nodes in the subunit are adjusted using
8635 -- this new source instantiation entry.
8637 elsif Nkind (N) in N_Proper_Body then
8638 declare
8639 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8640 begin
8641 if Instantiating and then Was_Originally_Stub (N) then
8642 Create_Instantiation_Source
8643 (Instantiation_Node,
8644 Defining_Entity (N),
8645 S_Adjustment);
8647 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8648 end if;
8650 -- Now copy the fields of the proper body, using the new
8651 -- adjustment factor if one was needed as per test above.
8653 Copy_Descendants;
8655 -- Restore the original adjustment factor
8657 S_Adjustment := Save_Adjustment;
8658 end;
8660 elsif Nkind (N) = N_Pragma and then Instantiating then
8662 -- Do not copy Comment or Ident pragmas their content is relevant to
8663 -- the generic unit, not to the instantiating unit.
8665 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8666 New_N := Make_Null_Statement (Sloc (N));
8668 -- Do not copy pragmas generated from aspects because the pragmas do
8669 -- not carry any semantic information, plus they will be regenerated
8670 -- in the instance.
8672 -- However, generating C we need to copy them since postconditions
8673 -- are inlined by the front end, and the front-end inlining machinery
8674 -- relies on this routine to perform inlining.
8676 elsif From_Aspect_Specification (N)
8677 and then not Modify_Tree_For_C
8678 then
8679 New_N := Make_Null_Statement (Sloc (N));
8681 else
8682 Copy_Descendants;
8683 end if;
8685 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8687 -- No descendant fields need traversing
8689 null;
8691 elsif Nkind (N) = N_String_Literal
8692 and then Present (Etype (N))
8693 and then Instantiating
8694 then
8695 -- If the string is declared in an outer scope, the string_literal
8696 -- subtype created for it may have the wrong scope. Force reanalysis
8697 -- of the constant to generate a new itype in the proper context.
8699 Set_Etype (New_N, Empty);
8700 Set_Analyzed (New_N, False);
8702 -- For the remaining nodes, copy their descendants recursively
8704 else
8705 Copy_Descendants;
8707 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8708 Set_Generic_Parent (Specification (New_N), N);
8710 -- Should preserve Corresponding_Spec??? (12.3(14))
8711 end if;
8712 end if;
8714 -- Propagate dimensions if present, so that they are reflected in the
8715 -- instance.
8717 if Nkind (N) in N_Has_Etype
8718 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8719 and then Present (Etype (N))
8720 and then Is_Floating_Point_Type (Etype (N))
8721 and then Has_Dimension_System (Etype (N))
8722 then
8723 Copy_Dimensions (N, New_N);
8724 end if;
8726 return New_N;
8727 end Copy_Generic_Node;
8729 ----------------------------
8730 -- Denotes_Formal_Package --
8731 ----------------------------
8733 function Denotes_Formal_Package
8734 (Pack : Entity_Id;
8735 On_Exit : Boolean := False;
8736 Instance : Entity_Id := Empty) return Boolean
8738 Par : Entity_Id;
8739 Scop : constant Entity_Id := Scope (Pack);
8740 E : Entity_Id;
8742 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8743 -- The package in question may be an actual for a previous formal
8744 -- package P of the current instance, so examine its actuals as well.
8745 -- This must be recursive over other formal packages.
8747 ----------------------------------
8748 -- Is_Actual_Of_Previous_Formal --
8749 ----------------------------------
8751 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8752 E1 : Entity_Id;
8754 begin
8755 E1 := First_Entity (P);
8756 while Present (E1) and then E1 /= Instance loop
8757 if Ekind (E1) = E_Package
8758 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8759 then
8760 if Renamed_Entity (E1) = Pack then
8761 return True;
8763 elsif E1 = P or else Renamed_Entity (E1) = P then
8764 return False;
8766 elsif Is_Actual_Of_Previous_Formal (E1) then
8767 return True;
8768 end if;
8769 end if;
8771 Next_Entity (E1);
8772 end loop;
8774 return False;
8775 end Is_Actual_Of_Previous_Formal;
8777 -- Start of processing for Denotes_Formal_Package
8779 begin
8780 if On_Exit then
8781 Par :=
8782 Instance_Envs.Table
8783 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8784 else
8785 Par := Current_Instantiated_Parent.Act_Id;
8786 end if;
8788 if Ekind (Scop) = E_Generic_Package
8789 or else Nkind (Unit_Declaration_Node (Scop)) =
8790 N_Generic_Subprogram_Declaration
8791 then
8792 return True;
8794 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8795 N_Formal_Package_Declaration
8796 then
8797 return True;
8799 elsif No (Par) then
8800 return False;
8802 else
8803 -- Check whether this package is associated with a formal package of
8804 -- the enclosing instantiation. Iterate over the list of renamings.
8806 E := First_Entity (Par);
8807 while Present (E) loop
8808 if Ekind (E) /= E_Package
8809 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8810 then
8811 null;
8813 elsif Renamed_Entity (E) = Par then
8814 return False;
8816 elsif Renamed_Entity (E) = Pack then
8817 return True;
8819 elsif Is_Actual_Of_Previous_Formal (E) then
8820 return True;
8822 end if;
8824 Next_Entity (E);
8825 end loop;
8827 return False;
8828 end if;
8829 end Denotes_Formal_Package;
8831 -----------------
8832 -- End_Generic --
8833 -----------------
8835 procedure End_Generic is
8836 begin
8837 -- ??? More things could be factored out in this routine. Should
8838 -- probably be done at a later stage.
8840 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8841 Generic_Flags.Decrement_Last;
8843 Expander_Mode_Restore;
8844 end End_Generic;
8846 -------------
8847 -- Earlier --
8848 -------------
8850 function Earlier (N1, N2 : Node_Id) return Boolean is
8851 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8852 -- Find distance from given node to enclosing compilation unit
8854 ----------------
8855 -- Find_Depth --
8856 ----------------
8858 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8859 begin
8860 while Present (P)
8861 and then Nkind (P) /= N_Compilation_Unit
8862 loop
8863 P := True_Parent (P);
8864 D := D + 1;
8865 end loop;
8866 end Find_Depth;
8868 -- Local declarations
8870 D1 : Integer := 0;
8871 D2 : Integer := 0;
8872 P1 : Node_Id := N1;
8873 P2 : Node_Id := N2;
8874 T1 : Source_Ptr;
8875 T2 : Source_Ptr;
8877 -- Start of processing for Earlier
8879 begin
8880 Find_Depth (P1, D1);
8881 Find_Depth (P2, D2);
8883 if P1 /= P2 then
8884 return False;
8885 else
8886 P1 := N1;
8887 P2 := N2;
8888 end if;
8890 while D1 > D2 loop
8891 P1 := True_Parent (P1);
8892 D1 := D1 - 1;
8893 end loop;
8895 while D2 > D1 loop
8896 P2 := True_Parent (P2);
8897 D2 := D2 - 1;
8898 end loop;
8900 -- At this point P1 and P2 are at the same distance from the root.
8901 -- We examine their parents until we find a common declarative list.
8902 -- If we reach the root, N1 and N2 do not descend from the same
8903 -- declarative list (e.g. one is nested in the declarative part and
8904 -- the other is in a block in the statement part) and the earlier
8905 -- one is already frozen.
8907 while not Is_List_Member (P1)
8908 or else not Is_List_Member (P2)
8909 or else not In_Same_List (P1, P2)
8910 loop
8911 P1 := True_Parent (P1);
8912 P2 := True_Parent (P2);
8914 if Nkind (Parent (P1)) = N_Subunit then
8915 P1 := Corresponding_Stub (Parent (P1));
8916 end if;
8918 if Nkind (Parent (P2)) = N_Subunit then
8919 P2 := Corresponding_Stub (Parent (P2));
8920 end if;
8922 if P1 = P2 then
8923 return False;
8924 end if;
8925 end loop;
8927 -- Expanded code usually shares the source location of the original
8928 -- construct it was generated for. This however may not necessarily
8929 -- reflect the true location of the code within the tree.
8931 -- Before comparing the slocs of the two nodes, make sure that we are
8932 -- working with correct source locations. Assume that P1 is to the left
8933 -- of P2. If either one does not come from source, traverse the common
8934 -- list heading towards the other node and locate the first source
8935 -- statement.
8937 -- P1 P2
8938 -- ----+===+===+--------------+===+===+----
8939 -- expanded code expanded code
8941 if not Comes_From_Source (P1) then
8942 while Present (P1) loop
8944 -- Neither P2 nor a source statement were located during the
8945 -- search. If we reach the end of the list, then P1 does not
8946 -- occur earlier than P2.
8948 -- ---->
8949 -- start --- P2 ----- P1 --- end
8951 if No (Next (P1)) then
8952 return False;
8954 -- We encounter P2 while going to the right of the list. This
8955 -- means that P1 does indeed appear earlier.
8957 -- ---->
8958 -- start --- P1 ===== P2 --- end
8959 -- expanded code in between
8961 elsif P1 = P2 then
8962 return True;
8964 -- No need to look any further since we have located a source
8965 -- statement.
8967 elsif Comes_From_Source (P1) then
8968 exit;
8969 end if;
8971 -- Keep going right
8973 Next (P1);
8974 end loop;
8975 end if;
8977 if not Comes_From_Source (P2) then
8978 while Present (P2) loop
8980 -- Neither P1 nor a source statement were located during the
8981 -- search. If we reach the start of the list, then P1 does not
8982 -- occur earlier than P2.
8984 -- <----
8985 -- start --- P2 --- P1 --- end
8987 if No (Prev (P2)) then
8988 return False;
8990 -- We encounter P1 while going to the left of the list. This
8991 -- means that P1 does indeed appear earlier.
8993 -- <----
8994 -- start --- P1 ===== P2 --- end
8995 -- expanded code in between
8997 elsif P2 = P1 then
8998 return True;
9000 -- No need to look any further since we have located a source
9001 -- statement.
9003 elsif Comes_From_Source (P2) then
9004 exit;
9005 end if;
9007 -- Keep going left
9009 Prev (P2);
9010 end loop;
9011 end if;
9013 -- At this point either both nodes came from source or we approximated
9014 -- their source locations through neighboring source statements.
9016 T1 := Top_Level_Location (Sloc (P1));
9017 T2 := Top_Level_Location (Sloc (P2));
9019 -- When two nodes come from the same instance, they have identical top
9020 -- level locations. To determine proper relation within the tree, check
9021 -- their locations within the template.
9023 if T1 = T2 then
9024 return Sloc (P1) < Sloc (P2);
9026 -- The two nodes either come from unrelated instances or do not come
9027 -- from instantiated code at all.
9029 else
9030 return T1 < T2;
9031 end if;
9032 end Earlier;
9034 ----------------------
9035 -- Find_Actual_Type --
9036 ----------------------
9038 function Find_Actual_Type
9039 (Typ : Entity_Id;
9040 Gen_Type : Entity_Id) return Entity_Id
9042 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9043 T : Entity_Id;
9045 begin
9046 -- Special processing only applies to child units
9048 if not Is_Child_Unit (Gen_Scope) then
9049 return Get_Instance_Of (Typ);
9051 -- If designated or component type is itself a formal of the child unit,
9052 -- its instance is available.
9054 elsif Scope (Typ) = Gen_Scope then
9055 return Get_Instance_Of (Typ);
9057 -- If the array or access type is not declared in the parent unit,
9058 -- no special processing needed.
9060 elsif not Is_Generic_Type (Typ)
9061 and then Scope (Gen_Scope) /= Scope (Typ)
9062 then
9063 return Get_Instance_Of (Typ);
9065 -- Otherwise, retrieve designated or component type by visibility
9067 else
9068 T := Current_Entity (Typ);
9069 while Present (T) loop
9070 if In_Open_Scopes (Scope (T)) then
9071 return T;
9072 elsif Is_Generic_Actual_Type (T) then
9073 return T;
9074 end if;
9076 T := Homonym (T);
9077 end loop;
9079 return Typ;
9080 end if;
9081 end Find_Actual_Type;
9083 -----------------------------
9084 -- Freeze_Package_Instance --
9085 -----------------------------
9087 procedure Freeze_Package_Instance
9088 (N : Node_Id;
9089 Gen_Body : Node_Id;
9090 Gen_Decl : Node_Id;
9091 Act_Id : Entity_Id)
9093 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9094 -- Check if the generic definition and the instantiation come from
9095 -- a common scope, in which case the instance must be frozen after
9096 -- the generic body.
9098 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9099 -- If the instance is nested inside a generic unit, the Sloc of the
9100 -- instance indicates the place of the original definition, not the
9101 -- point of the current enclosing instance. Pending a better usage of
9102 -- Slocs to indicate instantiation places, we determine the place of
9103 -- origin of a node by finding the maximum sloc of any ancestor node.
9105 -- Why is this not equivalent to Top_Level_Location ???
9107 -------------------
9108 -- In_Same_Scope --
9109 -------------------
9111 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9112 Act_Scop : Entity_Id := Scope (Act_Id);
9113 Gen_Scop : Entity_Id := Scope (Gen_Id);
9115 begin
9116 while Act_Scop /= Standard_Standard
9117 and then Gen_Scop /= Standard_Standard
9118 loop
9119 if Act_Scop = Gen_Scop then
9120 return True;
9121 end if;
9123 Act_Scop := Scope (Act_Scop);
9124 Gen_Scop := Scope (Gen_Scop);
9125 end loop;
9127 return False;
9128 end In_Same_Scope;
9130 ---------------
9131 -- True_Sloc --
9132 ---------------
9134 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9135 N1 : Node_Id;
9136 Res : Source_Ptr;
9138 begin
9139 Res := Sloc (N);
9140 N1 := N;
9141 while Present (N1) and then N1 /= Act_Unit loop
9142 if Sloc (N1) > Res then
9143 Res := Sloc (N1);
9144 end if;
9146 N1 := Parent (N1);
9147 end loop;
9149 return Res;
9150 end True_Sloc;
9152 -- Local variables
9154 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9155 Par_Id : constant Entity_Id := Scope (Gen_Id);
9156 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9157 Gen_Unit : constant Node_Id :=
9158 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9160 Body_Unit : Node_Id;
9161 F_Node : Node_Id;
9162 Must_Delay : Boolean;
9163 Orig_Body : Node_Id;
9165 -- Start of processing for Freeze_Package_Instance
9167 begin
9168 -- If the body is a subunit, the freeze point is the corresponding stub
9169 -- in the current compilation, not the subunit itself.
9171 if Nkind (Parent (Gen_Body)) = N_Subunit then
9172 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9173 else
9174 Orig_Body := Gen_Body;
9175 end if;
9177 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9179 -- If the instantiation and the generic definition appear in the same
9180 -- package declaration, this is an early instantiation. If they appear
9181 -- in the same declarative part, it is an early instantiation only if
9182 -- the generic body appears textually later, and the generic body is
9183 -- also in the main unit.
9185 -- If instance is nested within a subprogram, and the generic body
9186 -- is not, the instance is delayed because the enclosing body is. If
9187 -- instance and body are within the same scope, or the same subprogram
9188 -- body, indicate explicitly that the instance is delayed.
9190 Must_Delay :=
9191 (Gen_Unit = Act_Unit
9192 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9193 | N_Package_Declaration
9194 or else (Gen_Unit = Body_Unit
9195 and then
9196 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9197 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9198 and then In_Same_Scope (Gen_Id, Act_Id));
9200 -- If this is an early instantiation, the freeze node is placed after
9201 -- the generic body. Otherwise, if the generic appears in an instance,
9202 -- we cannot freeze the current instance until the outer one is frozen.
9203 -- This is only relevant if the current instance is nested within some
9204 -- inner scope not itself within the outer instance. If this scope is
9205 -- a package body in the same declarative part as the outer instance,
9206 -- then that body needs to be frozen after the outer instance. Finally,
9207 -- if no delay is needed, we place the freeze node at the end of the
9208 -- current declarative part.
9210 if No (Freeze_Node (Act_Id))
9211 or else not Is_List_Member (Freeze_Node (Act_Id))
9212 then
9213 Ensure_Freeze_Node (Act_Id);
9214 F_Node := Freeze_Node (Act_Id);
9216 if Must_Delay then
9217 Insert_After (Orig_Body, F_Node);
9219 elsif Is_Generic_Instance (Par_Id)
9220 and then Present (Freeze_Node (Par_Id))
9221 and then Scope (Act_Id) /= Par_Id
9222 then
9223 -- Freeze instance of inner generic after instance of enclosing
9224 -- generic.
9226 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9228 -- Handle the following case:
9230 -- package Parent_Inst is new ...
9231 -- freeze Parent_Inst []
9233 -- procedure P ... -- this body freezes Parent_Inst
9235 -- package Inst is new ...
9237 -- In this particular scenario, the freeze node for Inst must
9238 -- be inserted in the same manner as that of Parent_Inst,
9239 -- before the next source body or at the end of the declarative
9240 -- list (body not available). If body P did not exist and
9241 -- Parent_Inst was frozen after Inst, either by a body
9242 -- following Inst or at the end of the declarative region,
9243 -- the freeze node for Inst must be inserted after that of
9244 -- Parent_Inst. This relation is established by comparing
9245 -- the Slocs of Parent_Inst freeze node and Inst.
9246 -- We examine the parents of the enclosing lists to handle
9247 -- the case where the parent instance is in the visible part
9248 -- of a package declaration, and the inner instance is in
9249 -- the corresponding private part.
9251 if Parent (List_Containing (Freeze_Node (Par_Id)))
9252 = Parent (List_Containing (N))
9253 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9254 then
9255 Insert_Freeze_Node_For_Instance (N, F_Node);
9256 else
9257 Insert_After (Freeze_Node (Par_Id), F_Node);
9258 end if;
9260 -- Freeze package enclosing instance of inner generic after
9261 -- instance of enclosing generic.
9263 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9264 and then In_Same_Declarative_Part
9265 (Parent (Freeze_Node (Par_Id)), Parent (N))
9266 then
9267 declare
9268 Enclosing : Entity_Id;
9270 begin
9271 Enclosing := Corresponding_Spec (Parent (N));
9273 if No (Enclosing) then
9274 Enclosing := Defining_Entity (Parent (N));
9275 end if;
9277 Insert_Freeze_Node_For_Instance (N, F_Node);
9278 Ensure_Freeze_Node (Enclosing);
9280 if not Is_List_Member (Freeze_Node (Enclosing)) then
9282 -- The enclosing context is a subunit, insert the freeze
9283 -- node after the stub.
9285 if Nkind (Parent (Parent (N))) = N_Subunit then
9286 Insert_Freeze_Node_For_Instance
9287 (Corresponding_Stub (Parent (Parent (N))),
9288 Freeze_Node (Enclosing));
9290 -- The enclosing context is a package with a stub body
9291 -- which has already been replaced by the real body.
9292 -- Insert the freeze node after the actual body.
9294 elsif Ekind (Enclosing) = E_Package
9295 and then Present (Body_Entity (Enclosing))
9296 and then Was_Originally_Stub
9297 (Parent (Body_Entity (Enclosing)))
9298 then
9299 Insert_Freeze_Node_For_Instance
9300 (Parent (Body_Entity (Enclosing)),
9301 Freeze_Node (Enclosing));
9303 -- The parent instance has been frozen before the body of
9304 -- the enclosing package, insert the freeze node after
9305 -- the body.
9307 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9308 and then
9309 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9310 then
9311 Insert_Freeze_Node_For_Instance
9312 (Parent (N), Freeze_Node (Enclosing));
9314 else
9315 Insert_After
9316 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9317 end if;
9318 end if;
9319 end;
9321 else
9322 Insert_Freeze_Node_For_Instance (N, F_Node);
9323 end if;
9325 else
9326 Insert_Freeze_Node_For_Instance (N, F_Node);
9327 end if;
9328 end if;
9329 end Freeze_Package_Instance;
9331 --------------------------------
9332 -- Freeze_Subprogram_Instance --
9333 --------------------------------
9335 procedure Freeze_Subprogram_Instance
9336 (N : Node_Id;
9337 Gen_Body : Node_Id;
9338 Pack_Id : Entity_Id)
9340 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9341 -- Find innermost package body that encloses the given node, and which
9342 -- is not a compilation unit. Freeze nodes for the instance, or for its
9343 -- enclosing body, may be inserted after the enclosing_body of the
9344 -- generic unit. Used to determine proper placement of freeze node for
9345 -- both package and subprogram instances.
9347 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9348 -- Find entity for given package body, and locate or create a freeze
9349 -- node for it.
9351 ----------------------------
9352 -- Enclosing_Package_Body --
9353 ----------------------------
9355 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9356 P : Node_Id;
9358 begin
9359 P := Parent (N);
9360 while Present (P)
9361 and then Nkind (Parent (P)) /= N_Compilation_Unit
9362 loop
9363 if Nkind (P) = N_Package_Body then
9364 if Nkind (Parent (P)) = N_Subunit then
9365 return Corresponding_Stub (Parent (P));
9366 else
9367 return P;
9368 end if;
9369 end if;
9371 P := True_Parent (P);
9372 end loop;
9374 return Empty;
9375 end Enclosing_Package_Body;
9377 -------------------------
9378 -- Package_Freeze_Node --
9379 -------------------------
9381 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9382 Id : Entity_Id;
9384 begin
9385 if Nkind (B) = N_Package_Body then
9386 Id := Corresponding_Spec (B);
9387 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9388 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9389 end if;
9391 Ensure_Freeze_Node (Id);
9392 return Freeze_Node (Id);
9393 end Package_Freeze_Node;
9395 -- Local variables
9397 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9398 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9399 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9401 Enc_G_F : Node_Id;
9402 F_Node : Node_Id;
9404 -- Start of processing for Freeze_Subprogram_Instance
9406 begin
9407 -- If the instance and the generic body appear within the same unit, and
9408 -- the instance precedes the generic, the freeze node for the instance
9409 -- must appear after that of the generic. If the generic is nested
9410 -- within another instance I2, then current instance must be frozen
9411 -- after I2. In both cases, the freeze nodes are those of enclosing
9412 -- packages. Otherwise, the freeze node is placed at the end of the
9413 -- current declarative part.
9415 Ensure_Freeze_Node (Pack_Id);
9416 F_Node := Freeze_Node (Pack_Id);
9418 if Is_Generic_Instance (Par_Id)
9419 and then Present (Freeze_Node (Par_Id))
9420 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9421 then
9422 -- The parent was a premature instantiation. Insert freeze node at
9423 -- the end the current declarative part.
9425 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9426 Insert_Freeze_Node_For_Instance (N, F_Node);
9428 -- Handle the following case:
9430 -- package Parent_Inst is new ...
9431 -- freeze Parent_Inst []
9433 -- procedure P ... -- this body freezes Parent_Inst
9435 -- procedure Inst is new ...
9437 -- In this particular scenario, the freeze node for Inst must be
9438 -- inserted in the same manner as that of Parent_Inst - before the
9439 -- next source body or at the end of the declarative list (body not
9440 -- available). If body P did not exist and Parent_Inst was frozen
9441 -- after Inst, either by a body following Inst or at the end of the
9442 -- declarative region, the freeze node for Inst must be inserted
9443 -- after that of Parent_Inst. This relation is established by
9444 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9446 elsif In_Same_List (Freeze_Node (Par_Id), N)
9447 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9448 then
9449 Insert_Freeze_Node_For_Instance (N, F_Node);
9451 else
9452 Insert_After (Freeze_Node (Par_Id), F_Node);
9453 end if;
9455 -- The body enclosing the instance should be frozen after the body that
9456 -- includes the generic, because the body of the instance may make
9457 -- references to entities therein. If the two are not in the same
9458 -- declarative part, or if the one enclosing the instance is frozen
9459 -- already, freeze the instance at the end of the current declarative
9460 -- part.
9462 elsif Is_Generic_Instance (Par_Id)
9463 and then Present (Freeze_Node (Par_Id))
9464 and then Present (Enc_N)
9465 then
9466 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9467 then
9468 -- The enclosing package may contain several instances. Rather
9469 -- than computing the earliest point at which to insert its freeze
9470 -- node, we place it at the end of the declarative part of the
9471 -- parent of the generic.
9473 Insert_Freeze_Node_For_Instance
9474 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9475 end if;
9477 Insert_Freeze_Node_For_Instance (N, F_Node);
9479 elsif Present (Enc_G)
9480 and then Present (Enc_N)
9481 and then Enc_G /= Enc_N
9482 and then Earlier (N, Gen_Body)
9483 then
9484 -- Freeze package that encloses instance, and place node after the
9485 -- package that encloses generic. If enclosing package is already
9486 -- frozen we have to assume it is at the proper place. This may be a
9487 -- potential ABE that requires dynamic checking. Do not add a freeze
9488 -- node if the package that encloses the generic is inside the body
9489 -- that encloses the instance, because the freeze node would be in
9490 -- the wrong scope. Additional contortions needed if the bodies are
9491 -- within a subunit.
9493 declare
9494 Enclosing_Body : Node_Id;
9496 begin
9497 if Nkind (Enc_N) = N_Package_Body_Stub then
9498 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9499 else
9500 Enclosing_Body := Enc_N;
9501 end if;
9503 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9504 Insert_Freeze_Node_For_Instance
9505 (Enc_G, Package_Freeze_Node (Enc_N));
9506 end if;
9507 end;
9509 -- Freeze enclosing subunit before instance
9511 Enc_G_F := Package_Freeze_Node (Enc_G);
9513 if not Is_List_Member (Enc_G_F) then
9514 Insert_After (Enc_G, Enc_G_F);
9515 end if;
9517 Insert_Freeze_Node_For_Instance (N, F_Node);
9519 else
9520 -- If none of the above, insert freeze node at the end of the current
9521 -- declarative part.
9523 Insert_Freeze_Node_For_Instance (N, F_Node);
9524 end if;
9525 end Freeze_Subprogram_Instance;
9527 ----------------
9528 -- Get_Gen_Id --
9529 ----------------
9531 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9532 begin
9533 return Generic_Renamings.Table (E).Gen_Id;
9534 end Get_Gen_Id;
9536 ---------------------
9537 -- Get_Instance_Of --
9538 ---------------------
9540 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9541 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9543 begin
9544 if Res /= Assoc_Null then
9545 return Generic_Renamings.Table (Res).Act_Id;
9547 else
9548 -- On exit, entity is not instantiated: not a generic parameter, or
9549 -- else parameter of an inner generic unit.
9551 return A;
9552 end if;
9553 end Get_Instance_Of;
9555 ---------------------------------
9556 -- Get_Unit_Instantiation_Node --
9557 ---------------------------------
9559 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9560 Decl : Node_Id := Unit_Declaration_Node (A);
9561 Inst : Node_Id;
9563 begin
9564 -- If the Package_Instantiation attribute has been set on the package
9565 -- entity, then use it directly when it (or its Original_Node) refers
9566 -- to an N_Package_Instantiation node. In principle it should be
9567 -- possible to have this field set in all cases, which should be
9568 -- investigated, and would allow this function to be significantly
9569 -- simplified. ???
9571 Inst := Package_Instantiation (A);
9573 if Present (Inst) then
9574 if Nkind (Inst) = N_Package_Instantiation then
9575 return Inst;
9577 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9578 return Original_Node (Inst);
9579 end if;
9580 end if;
9582 -- If the instantiation is a compilation unit that does not need body
9583 -- then the instantiation node has been rewritten as a package
9584 -- declaration for the instance, and we return the original node.
9586 -- If it is a compilation unit and the instance node has not been
9587 -- rewritten, then it is still the unit of the compilation. Finally, if
9588 -- a body is present, this is a parent of the main unit whose body has
9589 -- been compiled for inlining purposes, and the instantiation node has
9590 -- been rewritten with the instance body.
9592 -- Otherwise the instantiation node appears after the declaration. If
9593 -- the entity is a formal package, the declaration may have been
9594 -- rewritten as a generic declaration (in the case of a formal with box)
9595 -- or left as a formal package declaration if it has actuals, and is
9596 -- found with a forward search.
9598 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9599 if Nkind (Decl) = N_Package_Declaration
9600 and then Present (Corresponding_Body (Decl))
9601 then
9602 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9603 end if;
9605 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9606 return Original_Node (Decl);
9607 else
9608 return Unit (Parent (Decl));
9609 end if;
9611 elsif Nkind (Decl) = N_Package_Declaration
9612 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9613 then
9614 return Original_Node (Decl);
9616 else
9617 Inst := Next (Decl);
9618 while Nkind (Inst) not in N_Formal_Package_Declaration
9619 | N_Function_Instantiation
9620 | N_Package_Instantiation
9621 | N_Procedure_Instantiation
9622 loop
9623 Next (Inst);
9624 end loop;
9626 return Inst;
9627 end if;
9628 end Get_Unit_Instantiation_Node;
9630 ------------------------
9631 -- Has_Been_Exchanged --
9632 ------------------------
9634 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9635 Next : Elmt_Id;
9637 begin
9638 Next := First_Elmt (Exchanged_Views);
9639 while Present (Next) loop
9640 if Full_View (Node (Next)) = E then
9641 return True;
9642 end if;
9644 Next_Elmt (Next);
9645 end loop;
9647 return False;
9648 end Has_Been_Exchanged;
9650 -------------------
9651 -- Has_Contracts --
9652 -------------------
9654 function Has_Contracts (Decl : Node_Id) return Boolean is
9655 A_List : constant List_Id := Aspect_Specifications (Decl);
9656 A_Spec : Node_Id;
9657 A_Id : Aspect_Id;
9658 begin
9659 A_Spec := First (A_List);
9660 while Present (A_Spec) loop
9661 A_Id := Get_Aspect_Id (A_Spec);
9662 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9663 return True;
9664 end if;
9666 Next (A_Spec);
9667 end loop;
9669 return False;
9670 end Has_Contracts;
9672 ----------
9673 -- Hash --
9674 ----------
9676 function Hash (F : Entity_Id) return HTable_Range is
9677 begin
9678 return HTable_Range (F mod HTable_Size);
9679 end Hash;
9681 ------------------------
9682 -- Hide_Current_Scope --
9683 ------------------------
9685 procedure Hide_Current_Scope is
9686 C : constant Entity_Id := Current_Scope;
9687 E : Entity_Id;
9689 begin
9690 Set_Is_Hidden_Open_Scope (C);
9692 E := First_Entity (C);
9693 while Present (E) loop
9694 if Is_Immediately_Visible (E) then
9695 Set_Is_Immediately_Visible (E, False);
9696 Append_Elmt (E, Hidden_Entities);
9697 end if;
9699 Next_Entity (E);
9700 end loop;
9702 -- Make the scope name invisible as well. This is necessary, but might
9703 -- conflict with calls to Rtsfind later on, in case the scope is a
9704 -- predefined one. There is no clean solution to this problem, so for
9705 -- now we depend on the user not redefining Standard itself in one of
9706 -- the parent units.
9708 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9709 Set_Is_Immediately_Visible (C, False);
9710 Append_Elmt (C, Hidden_Entities);
9711 end if;
9713 end Hide_Current_Scope;
9715 --------------
9716 -- Init_Env --
9717 --------------
9719 procedure Init_Env is
9720 Saved : Instance_Env;
9722 begin
9723 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9724 Saved.Exchanged_Views := Exchanged_Views;
9725 Saved.Hidden_Entities := Hidden_Entities;
9726 Saved.Current_Sem_Unit := Current_Sem_Unit;
9727 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9728 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9730 -- Save configuration switches. These may be reset if the unit is a
9731 -- predefined unit, and the current mode is not Ada 2005.
9733 Saved.Switches := Save_Config_Switches;
9735 Instance_Envs.Append (Saved);
9737 Exchanged_Views := New_Elmt_List;
9738 Hidden_Entities := New_Elmt_List;
9740 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9741 -- this is set properly in Set_Instance_Env.
9743 Current_Instantiated_Parent :=
9744 (Current_Scope, Current_Scope, Assoc_Null);
9745 end Init_Env;
9747 ---------------------
9748 -- In_Main_Context --
9749 ---------------------
9751 function In_Main_Context (E : Entity_Id) return Boolean is
9752 Context : List_Id;
9753 Clause : Node_Id;
9754 Nam : Node_Id;
9756 begin
9757 if not Is_Compilation_Unit (E)
9758 or else Ekind (E) /= E_Package
9759 or else In_Private_Part (E)
9760 then
9761 return False;
9762 end if;
9764 Context := Context_Items (Cunit (Main_Unit));
9766 Clause := First (Context);
9767 while Present (Clause) loop
9768 if Nkind (Clause) = N_With_Clause then
9769 Nam := Name (Clause);
9771 -- If the current scope is part of the context of the main unit,
9772 -- analysis of the corresponding with_clause is not complete, and
9773 -- the entity is not set. We use the Chars field directly, which
9774 -- might produce false positives in rare cases, but guarantees
9775 -- that we produce all the instance bodies we will need.
9777 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9778 or else (Nkind (Nam) = N_Selected_Component
9779 and then Chars (Selector_Name (Nam)) = Chars (E))
9780 then
9781 return True;
9782 end if;
9783 end if;
9785 Next (Clause);
9786 end loop;
9788 return False;
9789 end In_Main_Context;
9791 ---------------------
9792 -- Inherit_Context --
9793 ---------------------
9795 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9796 Current_Context : List_Id;
9797 Current_Unit : Node_Id;
9798 Item : Node_Id;
9799 New_I : Node_Id;
9801 Clause : Node_Id;
9802 OK : Boolean;
9803 Lib_Unit : Node_Id;
9805 begin
9806 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9808 -- The inherited context is attached to the enclosing compilation
9809 -- unit. This is either the main unit, or the declaration for the
9810 -- main unit (in case the instantiation appears within the package
9811 -- declaration and the main unit is its body).
9813 Current_Unit := Parent (Inst);
9814 while Present (Current_Unit)
9815 and then Nkind (Current_Unit) /= N_Compilation_Unit
9816 loop
9817 Current_Unit := Parent (Current_Unit);
9818 end loop;
9820 Current_Context := Context_Items (Current_Unit);
9822 Item := First (Context_Items (Parent (Gen_Decl)));
9823 while Present (Item) loop
9824 if Nkind (Item) = N_With_Clause then
9825 Lib_Unit := Library_Unit (Item);
9827 -- Take care to prevent direct cyclic with's
9829 if Lib_Unit /= Current_Unit then
9831 -- Do not add a unit if it is already in the context
9833 Clause := First (Current_Context);
9834 OK := True;
9835 while Present (Clause) loop
9836 if Nkind (Clause) = N_With_Clause
9837 and then Library_Unit (Clause) = Lib_Unit
9838 then
9839 OK := False;
9840 exit;
9841 end if;
9843 Next (Clause);
9844 end loop;
9846 if OK then
9847 New_I := New_Copy (Item);
9848 Set_Implicit_With (New_I);
9850 Append (New_I, Current_Context);
9851 end if;
9852 end if;
9853 end if;
9855 Next (Item);
9856 end loop;
9857 end if;
9858 end Inherit_Context;
9860 ----------------
9861 -- Initialize --
9862 ----------------
9864 procedure Initialize is
9865 begin
9866 Generic_Renamings.Init;
9867 Instance_Envs.Init;
9868 Generic_Flags.Init;
9869 Generic_Renamings_HTable.Reset;
9870 Circularity_Detected := False;
9871 Exchanged_Views := No_Elist;
9872 Hidden_Entities := No_Elist;
9873 end Initialize;
9875 -------------------------------------
9876 -- Insert_Freeze_Node_For_Instance --
9877 -------------------------------------
9879 procedure Insert_Freeze_Node_For_Instance
9880 (N : Node_Id;
9881 F_Node : Node_Id)
9883 function Enclosing_Body (N : Node_Id) return Node_Id;
9884 -- Find enclosing package or subprogram body, if any. Freeze node may
9885 -- be placed at end of current declarative list if previous instance
9886 -- and current one have different enclosing bodies.
9888 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9889 -- Find the local instance, if any, that declares the generic that is
9890 -- being instantiated. If present, the freeze node for this instance
9891 -- must follow the freeze node for the previous instance.
9893 --------------------
9894 -- Enclosing_Body --
9895 --------------------
9897 function Enclosing_Body (N : Node_Id) return Node_Id is
9898 P : Node_Id;
9900 begin
9901 P := Parent (N);
9902 while Present (P)
9903 and then Nkind (Parent (P)) /= N_Compilation_Unit
9904 loop
9905 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9906 if Nkind (Parent (P)) = N_Subunit then
9907 return Corresponding_Stub (Parent (P));
9908 else
9909 return P;
9910 end if;
9911 end if;
9913 P := True_Parent (P);
9914 end loop;
9916 return Empty;
9917 end Enclosing_Body;
9919 -----------------------
9920 -- Previous_Instance --
9921 -----------------------
9923 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9924 S : Entity_Id;
9926 begin
9927 S := Scope (Gen);
9928 while Present (S) and then S /= Standard_Standard loop
9929 if Is_Generic_Instance (S)
9930 and then In_Same_Source_Unit (S, N)
9931 then
9932 return S;
9933 end if;
9935 S := Scope (S);
9936 end loop;
9938 return Empty;
9939 end Previous_Instance;
9941 -- Local variables
9943 Decl : Node_Id;
9944 Decls : List_Id;
9945 Inst : Entity_Id;
9946 Origin : Entity_Id;
9947 Par_Inst : Node_Id;
9948 Par_N : Node_Id;
9950 -- Start of processing for Insert_Freeze_Node_For_Instance
9952 begin
9953 -- Nothing to do if the freeze node has already been inserted
9955 if Is_List_Member (F_Node) then
9956 return;
9957 end if;
9959 Inst := Entity (F_Node);
9961 -- When processing a subprogram instantiation, utilize the actual
9962 -- subprogram instantiation rather than its package wrapper as it
9963 -- carries all the context information.
9965 if Is_Wrapper_Package (Inst) then
9966 Inst := Related_Instance (Inst);
9967 end if;
9969 Par_Inst := Parent (Inst);
9971 -- If this is a package instance, check whether the generic is declared
9972 -- in a previous instance and the current instance is not within the
9973 -- previous one.
9975 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9976 declare
9977 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9978 Par_I : constant Entity_Id :=
9979 Previous_Instance (Generic_Parent (Par_Inst));
9980 Scop : Entity_Id;
9982 begin
9983 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9984 Scop := Scope (Inst);
9986 -- If the current instance is within the one that contains
9987 -- the generic, the freeze node for the current one must
9988 -- appear in the current declarative part. Ditto, if the
9989 -- current instance is within another package instance or
9990 -- within a body that does not enclose the current instance.
9991 -- In these three cases the freeze node of the previous
9992 -- instance is not relevant.
9994 while Present (Scop) and then Scop /= Standard_Standard loop
9995 exit when Scop = Par_I
9996 or else
9997 (Is_Generic_Instance (Scop)
9998 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9999 Scop := Scope (Scop);
10000 end loop;
10002 -- Previous instance encloses current instance
10004 if Scop = Par_I then
10005 null;
10007 -- If the next node is a source body we must freeze in the
10008 -- current scope as well.
10010 elsif Present (Next (N))
10011 and then Nkind (Next (N)) in N_Subprogram_Body
10012 | N_Package_Body
10013 and then Comes_From_Source (Next (N))
10014 then
10015 null;
10017 -- Current instance is within an unrelated instance
10019 elsif Is_Generic_Instance (Scop) then
10020 null;
10022 -- Current instance is within an unrelated body
10024 elsif Present (Enclosing_N)
10025 and then Enclosing_N /= Enclosing_Body (Par_I)
10026 then
10027 null;
10029 else
10030 Insert_After (Freeze_Node (Par_I), F_Node);
10031 return;
10032 end if;
10033 end if;
10034 end;
10035 end if;
10037 Decl := N;
10038 Decls := List_Containing (N);
10039 Par_N := Parent (Decls);
10040 Origin := Empty;
10042 -- Determine the proper freeze point of an instantiation
10044 if Is_Generic_Instance (Inst) then
10045 loop
10046 -- When the instantiation occurs in a package spec, append the
10047 -- freeze node to the private declarations (if any).
10049 if Nkind (Par_N) = N_Package_Specification
10050 and then Decls = Visible_Declarations (Par_N)
10051 and then not Is_Empty_List (Private_Declarations (Par_N))
10052 then
10053 Decls := Private_Declarations (Par_N);
10054 Decl := First (Decls);
10055 end if;
10057 -- We adhere to the general rule of a package or subprogram body
10058 -- causing freezing of anything before it in the same declarative
10059 -- region. In this respect, the proper freeze point of a package
10060 -- instantiation is before the first source body which follows, or
10061 -- before a stub. This ensures that entities from the instance are
10062 -- already frozen and therefore usable in source bodies.
10064 if Nkind (Par_N) /= N_Package_Declaration
10065 and then
10066 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10067 then
10068 while Present (Decl) loop
10069 if ((Nkind (Decl) in N_Unit_Body
10070 or else
10071 Nkind (Decl) in N_Body_Stub)
10072 and then Comes_From_Source (Decl))
10073 or else (Present (Origin)
10074 and then Nkind (Decl) in N_Generic_Instantiation
10075 and then Instance_Spec (Decl) /= Origin)
10076 then
10077 Set_Sloc (F_Node, Sloc (Decl));
10078 Insert_Before (Decl, F_Node);
10079 return;
10080 end if;
10082 Next (Decl);
10083 end loop;
10084 end if;
10086 -- When the instantiation occurs in a package spec and there is
10087 -- no source body which follows, and the package has a body but
10088 -- is delayed, then insert immediately before its freeze node.
10090 if Nkind (Par_N) = N_Package_Specification
10091 and then Present (Corresponding_Body (Parent (Par_N)))
10092 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10093 then
10094 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10095 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10096 return;
10098 -- When the instantiation occurs in a package spec and there is
10099 -- no source body which follows, not even of the package itself,
10100 -- then insert into the declaration list of the outer level, but
10101 -- do not jump over following instantiations in this list because
10102 -- they may have a body that has not materialized yet, see above.
10104 elsif Nkind (Par_N) = N_Package_Specification
10105 and then No (Corresponding_Body (Parent (Par_N)))
10106 and then Is_List_Member (Parent (Par_N))
10107 then
10108 Decl := Parent (Par_N);
10109 Decls := List_Containing (Decl);
10110 Par_N := Parent (Decls);
10111 Origin := Decl;
10113 -- In a package declaration, or if no source body which follows
10114 -- and at library level, then insert at end of list.
10116 else
10117 exit;
10118 end if;
10119 end loop;
10120 end if;
10122 -- Insert and adjust the Sloc of the freeze node
10124 Set_Sloc (F_Node, Sloc (Last (Decls)));
10125 Insert_After (Last (Decls), F_Node);
10126 end Insert_Freeze_Node_For_Instance;
10128 -----------------------------
10129 -- Install_Formal_Packages --
10130 -----------------------------
10132 procedure Install_Formal_Packages (Par : Entity_Id) is
10133 E : Entity_Id;
10134 Gen : Entity_Id;
10135 Gen_E : Entity_Id := Empty;
10137 begin
10138 E := First_Entity (Par);
10140 -- If we are installing an instance parent, locate the formal packages
10141 -- of its generic parent.
10143 if Is_Generic_Instance (Par) then
10144 Gen := Generic_Parent (Package_Specification (Par));
10145 Gen_E := First_Entity (Gen);
10146 end if;
10148 while Present (E) loop
10149 if Ekind (E) = E_Package
10150 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10151 then
10152 -- If this is the renaming for the parent instance, done
10154 if Renamed_Entity (E) = Par then
10155 exit;
10157 -- The visibility of a formal of an enclosing generic is already
10158 -- correct.
10160 elsif Denotes_Formal_Package (E) then
10161 null;
10163 elsif Present (Associated_Formal_Package (E)) then
10164 Check_Generic_Actuals (Renamed_Entity (E), True);
10165 Set_Is_Hidden (E, False);
10167 -- Find formal package in generic unit that corresponds to
10168 -- (instance of) formal package in instance.
10170 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10171 Next_Entity (Gen_E);
10172 end loop;
10174 if Present (Gen_E) then
10175 Map_Formal_Package_Entities (Gen_E, E);
10176 end if;
10177 end if;
10178 end if;
10180 Next_Entity (E);
10182 if Present (Gen_E) then
10183 Next_Entity (Gen_E);
10184 end if;
10185 end loop;
10186 end Install_Formal_Packages;
10188 --------------------
10189 -- Install_Parent --
10190 --------------------
10192 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10193 Ancestors : constant Elist_Id := New_Elmt_List;
10194 S : constant Entity_Id := Current_Scope;
10195 Inst_Par : Entity_Id;
10196 First_Par : Entity_Id;
10197 Inst_Node : Node_Id;
10198 Gen_Par : Entity_Id;
10199 First_Gen : Entity_Id;
10200 Elmt : Elmt_Id;
10202 procedure Install_Noninstance_Specs (Par : Entity_Id);
10203 -- Install the scopes of noninstance parent units ending with Par
10205 procedure Install_Spec (Par : Entity_Id);
10206 -- The child unit is within the declarative part of the parent, so the
10207 -- declarations within the parent are immediately visible.
10209 -------------------------------
10210 -- Install_Noninstance_Specs --
10211 -------------------------------
10213 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10214 begin
10215 if Present (Par)
10216 and then Par /= Standard_Standard
10217 and then not In_Open_Scopes (Par)
10218 then
10219 Install_Noninstance_Specs (Scope (Par));
10220 Install_Spec (Par);
10221 end if;
10222 end Install_Noninstance_Specs;
10224 ------------------
10225 -- Install_Spec --
10226 ------------------
10228 procedure Install_Spec (Par : Entity_Id) is
10229 Spec : constant Node_Id := Package_Specification (Par);
10231 begin
10232 -- If this parent of the child instance is a top-level unit,
10233 -- then record the unit and its visibility for later resetting in
10234 -- Remove_Parent. We exclude units that are generic instances, as we
10235 -- only want to record this information for the ultimate top-level
10236 -- noninstance parent (is that always correct???).
10238 if Scope (Par) = Standard_Standard
10239 and then not Is_Generic_Instance (Par)
10240 then
10241 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10242 Instance_Parent_Unit := Par;
10243 end if;
10245 -- Open the parent scope and make it and its declarations visible.
10246 -- If this point is not within a body, then only the visible
10247 -- declarations should be made visible, and installation of the
10248 -- private declarations is deferred until the appropriate point
10249 -- within analysis of the spec being instantiated (see the handling
10250 -- of parent visibility in Analyze_Package_Specification). This is
10251 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10252 -- private view problems that occur when compiling instantiations of
10253 -- a generic child of that package (Generic_Dispatching_Constructor).
10254 -- If the instance freezes a tagged type, inlinings of operations
10255 -- from Ada.Tags may need the full view of type Tag. If inlining took
10256 -- proper account of establishing visibility of inlined subprograms'
10257 -- parents then it should be possible to remove this
10258 -- special check. ???
10260 Push_Scope (Par);
10261 Set_Is_Immediately_Visible (Par);
10262 Install_Visible_Declarations (Par);
10263 Set_Use (Visible_Declarations (Spec));
10265 if In_Body or else Is_RTU (Par, Ada_Tags) then
10266 Install_Private_Declarations (Par);
10267 Set_Use (Private_Declarations (Spec));
10268 end if;
10269 end Install_Spec;
10271 -- Start of processing for Install_Parent
10273 begin
10274 -- We need to install the parent instance to compile the instantiation
10275 -- of the child, but the child instance must appear in the current
10276 -- scope. Given that we cannot place the parent above the current scope
10277 -- in the scope stack, we duplicate the current scope and unstack both
10278 -- after the instantiation is complete.
10280 -- If the parent is itself the instantiation of a child unit, we must
10281 -- also stack the instantiation of its parent, and so on. Each such
10282 -- ancestor is the prefix of the name in a prior instantiation.
10284 -- If this is a nested instance, the parent unit itself resolves to
10285 -- a renaming of the parent instance, whose declaration we need.
10287 -- Finally, the parent may be a generic (not an instance) when the
10288 -- child unit appears as a formal package.
10290 Inst_Par := P;
10292 if Present (Renamed_Entity (Inst_Par)) then
10293 Inst_Par := Renamed_Entity (Inst_Par);
10294 end if;
10296 First_Par := Inst_Par;
10298 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10300 First_Gen := Gen_Par;
10302 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10304 -- Load grandparent instance as well
10306 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10308 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10309 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10311 if Present (Renamed_Entity (Inst_Par)) then
10312 Inst_Par := Renamed_Entity (Inst_Par);
10313 end if;
10315 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10317 if Present (Gen_Par) then
10318 Prepend_Elmt (Inst_Par, Ancestors);
10320 else
10321 -- Parent is not the name of an instantiation
10323 Install_Noninstance_Specs (Inst_Par);
10324 exit;
10325 end if;
10327 else
10328 -- Previous error
10330 exit;
10331 end if;
10332 end loop;
10334 if Present (First_Gen) then
10335 Append_Elmt (First_Par, Ancestors);
10336 else
10337 Install_Noninstance_Specs (First_Par);
10338 end if;
10340 if not Is_Empty_Elmt_List (Ancestors) then
10341 Elmt := First_Elmt (Ancestors);
10342 while Present (Elmt) loop
10343 Install_Spec (Node (Elmt));
10344 Install_Formal_Packages (Node (Elmt));
10345 Next_Elmt (Elmt);
10346 end loop;
10347 end if;
10349 if not In_Body then
10350 Push_Scope (S);
10351 end if;
10352 end Install_Parent;
10354 -------------------------------
10355 -- Install_Hidden_Primitives --
10356 -------------------------------
10358 procedure Install_Hidden_Primitives
10359 (Prims_List : in out Elist_Id;
10360 Gen_T : Entity_Id;
10361 Act_T : Entity_Id)
10363 Elmt : Elmt_Id;
10364 List : Elist_Id := No_Elist;
10365 Prim_G_Elmt : Elmt_Id;
10366 Prim_A_Elmt : Elmt_Id;
10367 Prim_G : Node_Id;
10368 Prim_A : Node_Id;
10370 begin
10371 -- No action needed in case of serious errors because we cannot trust
10372 -- in the order of primitives
10374 if Serious_Errors_Detected > 0 then
10375 return;
10377 -- No action possible if we don't have available the list of primitive
10378 -- operations
10380 elsif No (Gen_T)
10381 or else not Is_Record_Type (Gen_T)
10382 or else not Is_Tagged_Type (Gen_T)
10383 or else not Is_Record_Type (Act_T)
10384 or else not Is_Tagged_Type (Act_T)
10385 then
10386 return;
10388 -- There is no need to handle interface types since their primitives
10389 -- cannot be hidden
10391 elsif Is_Interface (Gen_T) then
10392 return;
10393 end if;
10395 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10397 if not Is_Class_Wide_Type (Act_T) then
10398 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10399 else
10400 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10401 end if;
10403 loop
10404 -- Skip predefined primitives in the generic formal
10406 while Present (Prim_G_Elmt)
10407 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10408 loop
10409 Next_Elmt (Prim_G_Elmt);
10410 end loop;
10412 -- Skip predefined primitives in the generic actual
10414 while Present (Prim_A_Elmt)
10415 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10416 loop
10417 Next_Elmt (Prim_A_Elmt);
10418 end loop;
10420 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10422 Prim_G := Node (Prim_G_Elmt);
10423 Prim_A := Node (Prim_A_Elmt);
10425 -- There is no need to handle interface primitives because their
10426 -- primitives are not hidden
10428 exit when Present (Interface_Alias (Prim_G));
10430 -- Here we install one hidden primitive
10432 if Chars (Prim_G) /= Chars (Prim_A)
10433 and then Has_Suffix (Prim_A, 'P')
10434 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10435 then
10436 Set_Chars (Prim_A, Chars (Prim_G));
10437 Append_New_Elmt (Prim_A, To => List);
10438 end if;
10440 Next_Elmt (Prim_A_Elmt);
10441 Next_Elmt (Prim_G_Elmt);
10442 end loop;
10444 -- Append the elements to the list of temporarily visible primitives
10445 -- avoiding duplicates.
10447 if Present (List) then
10448 if No (Prims_List) then
10449 Prims_List := New_Elmt_List;
10450 end if;
10452 Elmt := First_Elmt (List);
10453 while Present (Elmt) loop
10454 Append_Unique_Elmt (Node (Elmt), Prims_List);
10455 Next_Elmt (Elmt);
10456 end loop;
10457 end if;
10458 end Install_Hidden_Primitives;
10460 -------------------------------
10461 -- Restore_Hidden_Primitives --
10462 -------------------------------
10464 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10465 Prim_Elmt : Elmt_Id;
10466 Prim : Node_Id;
10468 begin
10469 if Present (Prims_List) then
10470 Prim_Elmt := First_Elmt (Prims_List);
10471 while Present (Prim_Elmt) loop
10472 Prim := Node (Prim_Elmt);
10473 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10474 Next_Elmt (Prim_Elmt);
10475 end loop;
10477 Prims_List := No_Elist;
10478 end if;
10479 end Restore_Hidden_Primitives;
10481 --------------------------------
10482 -- Instantiate_Formal_Package --
10483 --------------------------------
10485 function Instantiate_Formal_Package
10486 (Formal : Node_Id;
10487 Actual : Node_Id;
10488 Analyzed_Formal : Node_Id) return List_Id
10490 Loc : constant Source_Ptr := Sloc (Actual);
10491 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10493 Actual_Pack : Entity_Id;
10494 Formal_Pack : Entity_Id;
10495 Gen_Parent : Entity_Id;
10496 Decls : List_Id;
10497 Nod : Node_Id;
10498 Parent_Spec : Node_Id;
10500 procedure Find_Matching_Actual
10501 (F : Node_Id;
10502 Act : in out Entity_Id);
10503 -- We need to associate each formal entity in the formal package with
10504 -- the corresponding entity in the actual package. The actual package
10505 -- has been analyzed and possibly expanded, and as a result there is
10506 -- no one-to-one correspondence between the two lists (for example,
10507 -- the actual may include subtypes, itypes, and inherited primitive
10508 -- operations, interspersed among the renaming declarations for the
10509 -- actuals). We retrieve the corresponding actual by name because each
10510 -- actual has the same name as the formal, and they do appear in the
10511 -- same order.
10513 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10514 -- Retrieve entity of defining entity of generic formal parameter.
10515 -- Only the declarations of formals need to be considered when
10516 -- linking them to actuals, but the declarative list may include
10517 -- internal entities generated during analysis, and those are ignored.
10519 procedure Match_Formal_Entity
10520 (Formal_Node : Node_Id;
10521 Formal_Ent : Entity_Id;
10522 Actual_Ent : Entity_Id);
10523 -- Associates the formal entity with the actual. In the case where
10524 -- Formal_Ent is a formal package, this procedure iterates through all
10525 -- of its formals and enters associations between the actuals occurring
10526 -- in the formal package's corresponding actual package (given by
10527 -- Actual_Ent) and the formal package's formal parameters. This
10528 -- procedure recurses if any of the parameters is itself a package.
10530 function Is_Instance_Of
10531 (Act_Spec : Entity_Id;
10532 Gen_Anc : Entity_Id) return Boolean;
10533 -- The actual can be an instantiation of a generic within another
10534 -- instance, in which case there is no direct link from it to the
10535 -- original generic ancestor. In that case, we recognize that the
10536 -- ultimate ancestor is the same by examining names and scopes.
10538 procedure Process_Nested_Formal (Formal : Entity_Id);
10539 -- If the current formal is declared with a box, its own formals are
10540 -- visible in the instance, as they were in the generic, and their
10541 -- Hidden flag must be reset. If some of these formals are themselves
10542 -- packages declared with a box, the processing must be recursive.
10544 --------------------------
10545 -- Find_Matching_Actual --
10546 --------------------------
10548 procedure Find_Matching_Actual
10549 (F : Node_Id;
10550 Act : in out Entity_Id)
10552 Formal_Ent : Entity_Id;
10554 begin
10555 case Nkind (Original_Node (F)) is
10556 when N_Formal_Object_Declaration
10557 | N_Formal_Type_Declaration
10559 Formal_Ent := Defining_Identifier (F);
10561 while Present (Act)
10562 and then Chars (Act) /= Chars (Formal_Ent)
10563 loop
10564 Next_Entity (Act);
10565 end loop;
10567 when N_Formal_Package_Declaration
10568 | N_Formal_Subprogram_Declaration
10569 | N_Generic_Package_Declaration
10570 | N_Package_Declaration
10572 Formal_Ent := Defining_Entity (F);
10574 while Present (Act)
10575 and then Chars (Act) /= Chars (Formal_Ent)
10576 loop
10577 Next_Entity (Act);
10578 end loop;
10580 when others =>
10581 raise Program_Error;
10582 end case;
10583 end Find_Matching_Actual;
10585 -------------------------
10586 -- Match_Formal_Entity --
10587 -------------------------
10589 procedure Match_Formal_Entity
10590 (Formal_Node : Node_Id;
10591 Formal_Ent : Entity_Id;
10592 Actual_Ent : Entity_Id)
10594 Act_Pkg : Entity_Id;
10596 begin
10597 Set_Instance_Of (Formal_Ent, Actual_Ent);
10599 if Ekind (Actual_Ent) = E_Package then
10601 -- Record associations for each parameter
10603 Act_Pkg := Actual_Ent;
10605 declare
10606 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10607 F_Ent : Entity_Id;
10608 F_Node : Node_Id;
10610 Gen_Decl : Node_Id;
10611 Formals : List_Id;
10612 Actual : Entity_Id;
10614 begin
10615 -- Retrieve the actual given in the formal package declaration
10617 Actual := Entity (Name (Original_Node (Formal_Node)));
10619 -- The actual in the formal package declaration may be a
10620 -- renamed generic package, in which case we want to retrieve
10621 -- the original generic in order to traverse its formal part.
10623 if Present (Renamed_Entity (Actual)) then
10624 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10625 else
10626 Gen_Decl := Unit_Declaration_Node (Actual);
10627 end if;
10629 Formals := Generic_Formal_Declarations (Gen_Decl);
10631 if Present (Formals) then
10632 F_Node := First_Non_Pragma (Formals);
10633 else
10634 F_Node := Empty;
10635 end if;
10637 while Present (A_Ent)
10638 and then Present (F_Node)
10639 and then A_Ent /= First_Private_Entity (Act_Pkg)
10640 loop
10641 F_Ent := Get_Formal_Entity (F_Node);
10643 if Present (F_Ent) then
10645 -- This is a formal of the original package. Record
10646 -- association and recurse.
10648 Find_Matching_Actual (F_Node, A_Ent);
10649 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10650 Next_Entity (A_Ent);
10651 end if;
10653 Next_Non_Pragma (F_Node);
10654 end loop;
10655 end;
10656 end if;
10657 end Match_Formal_Entity;
10659 -----------------------
10660 -- Get_Formal_Entity --
10661 -----------------------
10663 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10664 Kind : constant Node_Kind := Nkind (Original_Node (N));
10665 begin
10666 case Kind is
10667 when N_Formal_Object_Declaration =>
10668 return Defining_Identifier (N);
10670 when N_Formal_Type_Declaration =>
10671 return Defining_Identifier (N);
10673 when N_Formal_Subprogram_Declaration =>
10674 return Defining_Unit_Name (Specification (N));
10676 when N_Formal_Package_Declaration =>
10677 return Defining_Identifier (Original_Node (N));
10679 when N_Generic_Package_Declaration =>
10680 return Defining_Identifier (Original_Node (N));
10682 -- All other declarations are introduced by semantic analysis and
10683 -- have no match in the actual.
10685 when others =>
10686 return Empty;
10687 end case;
10688 end Get_Formal_Entity;
10690 --------------------
10691 -- Is_Instance_Of --
10692 --------------------
10694 function Is_Instance_Of
10695 (Act_Spec : Entity_Id;
10696 Gen_Anc : Entity_Id) return Boolean
10698 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10700 begin
10701 if No (Gen_Par) then
10702 return False;
10704 -- Simplest case: the generic parent of the actual is the formal
10706 elsif Gen_Par = Gen_Anc then
10707 return True;
10709 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10710 return False;
10712 -- The actual may be obtained through several instantiations. Its
10713 -- scope must itself be an instance of a generic declared in the
10714 -- same scope as the formal. Any other case is detected above.
10716 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10717 return False;
10719 else
10720 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10721 end if;
10722 end Is_Instance_Of;
10724 ---------------------------
10725 -- Process_Nested_Formal --
10726 ---------------------------
10728 procedure Process_Nested_Formal (Formal : Entity_Id) is
10729 Ent : Entity_Id;
10731 begin
10732 if Present (Associated_Formal_Package (Formal))
10733 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10734 then
10735 Ent := First_Entity (Formal);
10736 while Present (Ent) loop
10737 Set_Is_Hidden (Ent, False);
10738 Set_Is_Visible_Formal (Ent);
10739 Set_Is_Potentially_Use_Visible
10740 (Ent, Is_Potentially_Use_Visible (Formal));
10742 if Ekind (Ent) = E_Package then
10743 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10744 Process_Nested_Formal (Ent);
10745 end if;
10747 Next_Entity (Ent);
10748 end loop;
10749 end if;
10750 end Process_Nested_Formal;
10752 -- Start of processing for Instantiate_Formal_Package
10754 begin
10755 Analyze (Actual);
10757 -- The actual must be a package instance, or else a current instance
10758 -- such as a parent generic within the body of a generic child.
10760 if not Is_Entity_Name (Actual)
10761 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10762 then
10763 Error_Msg_N
10764 ("expect package instance to instantiate formal", Actual);
10765 Abandon_Instantiation (Actual);
10767 else
10768 Actual_Pack := Entity (Actual);
10769 Set_Is_Instantiated (Actual_Pack);
10771 -- The actual may be a renamed package, or an outer generic formal
10772 -- package whose instantiation is converted into a renaming.
10774 if Present (Renamed_Entity (Actual_Pack)) then
10775 Actual_Pack := Renamed_Entity (Actual_Pack);
10776 end if;
10778 -- The analyzed formal is expected to be the result of the rewriting
10779 -- of the formal package into a regular package by analysis.
10781 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10782 and then Nkind (Original_Node (Analyzed_Formal)) =
10783 N_Formal_Package_Declaration);
10785 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10786 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10788 -- The actual for a ghost generic formal package should be a ghost
10789 -- package (SPARK RM 6.9(14)).
10791 Check_Ghost_Formal_Procedure_Or_Package
10792 (N => Actual,
10793 Actual => Actual_Pack,
10794 Formal => Formal_Pack);
10796 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10797 Parent_Spec := Package_Specification (Actual_Pack);
10798 else
10799 Parent_Spec := Parent (Actual_Pack);
10800 end if;
10802 if Gen_Parent = Any_Id then
10803 Error_Msg_N
10804 ("previous error in declaration of formal package", Actual);
10805 Abandon_Instantiation (Actual);
10807 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10808 null;
10810 -- If this is the current instance of an enclosing generic, that unit
10811 -- is the generic package we need.
10813 elsif In_Open_Scopes (Actual_Pack)
10814 and then Ekind (Actual_Pack) = E_Generic_Package
10815 then
10816 null;
10818 else
10819 Error_Msg_NE
10820 ("actual parameter must be instance of&", Actual, Gen_Parent);
10821 Abandon_Instantiation (Actual);
10822 end if;
10824 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10825 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10827 Nod :=
10828 Make_Package_Renaming_Declaration (Loc,
10829 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10830 Name => New_Occurrence_Of (Actual_Pack, Loc));
10832 Set_Associated_Formal_Package
10833 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10834 Decls := New_List (Nod);
10836 -- If the formal F has a box, then the generic declarations are
10837 -- visible in the generic G. In an instance of G, the corresponding
10838 -- entities in the actual for F (which are the actuals for the
10839 -- instantiation of the generic that F denotes) must also be made
10840 -- visible for analysis of the current instance. On exit from the
10841 -- current instance, those entities are made private again. If the
10842 -- actual is currently in use, these entities are also use-visible.
10844 -- The loop through the actual entities also steps through the formal
10845 -- entities and enters associations from formals to actuals into the
10846 -- renaming map. This is necessary to properly handle checking of
10847 -- actual parameter associations for later formals that depend on
10848 -- actuals declared in the formal package.
10850 -- In Ada 2005, partial parameterization requires that we make
10851 -- visible the actuals corresponding to formals that were defaulted
10852 -- in the formal package. There formals are identified because they
10853 -- remain formal generics within the formal package, rather than
10854 -- being renamings of the actuals supplied.
10856 declare
10857 Gen_Decl : constant Node_Id :=
10858 Unit_Declaration_Node (Gen_Parent);
10859 Formals : constant List_Id :=
10860 Generic_Formal_Declarations (Gen_Decl);
10862 Actual_Ent : Entity_Id;
10863 Actual_Of_Formal : Node_Id;
10864 Formal_Node : Node_Id;
10865 Formal_Ent : Entity_Id;
10867 begin
10868 if Present (Formals) then
10869 Formal_Node := First_Non_Pragma (Formals);
10870 else
10871 Formal_Node := Empty;
10872 end if;
10874 Actual_Ent := First_Entity (Actual_Pack);
10875 Actual_Of_Formal :=
10876 First (Visible_Declarations (Specification (Analyzed_Formal)));
10877 while Present (Actual_Ent)
10878 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10879 loop
10880 if Present (Formal_Node) then
10881 Formal_Ent := Get_Formal_Entity (Formal_Node);
10883 if Present (Formal_Ent) then
10884 Find_Matching_Actual (Formal_Node, Actual_Ent);
10885 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10887 -- We iterate at the same time over the actuals of the
10888 -- local package created for the formal, to determine
10889 -- which one of the formals of the original generic were
10890 -- defaulted in the formal. The corresponding actual
10891 -- entities are visible in the enclosing instance.
10893 if Box_Present (Formal)
10894 or else
10895 (Present (Actual_Of_Formal)
10896 and then
10897 Is_Generic_Formal
10898 (Get_Formal_Entity (Actual_Of_Formal)))
10899 then
10900 Set_Is_Hidden (Actual_Ent, False);
10901 Set_Is_Visible_Formal (Actual_Ent);
10902 Set_Is_Potentially_Use_Visible
10903 (Actual_Ent, In_Use (Actual_Pack));
10905 if Ekind (Actual_Ent) = E_Package then
10906 Process_Nested_Formal (Actual_Ent);
10907 end if;
10909 else
10910 if not Is_Hidden (Actual_Ent) then
10911 Append_Elmt (Actual_Ent, Hidden_Formals);
10912 end if;
10914 Set_Is_Hidden (Actual_Ent);
10915 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10916 end if;
10917 end if;
10919 Next_Non_Pragma (Formal_Node);
10920 Next (Actual_Of_Formal);
10922 -- A formal subprogram may be overloaded, so advance in
10923 -- the list of actuals to make sure we do not match two
10924 -- successive formals to the same actual. This is only
10925 -- relevant for overloadable entities, others have
10926 -- distinct names.
10928 if Is_Overloadable (Actual_Ent) then
10929 Next_Entity (Actual_Ent);
10930 end if;
10932 else
10933 -- No further formals to match, but the generic part may
10934 -- contain inherited operation that are not hidden in the
10935 -- enclosing instance.
10937 Next_Entity (Actual_Ent);
10938 end if;
10939 end loop;
10941 -- Inherited subprograms generated by formal derived types are
10942 -- also visible if the types are.
10944 Actual_Ent := First_Entity (Actual_Pack);
10945 while Present (Actual_Ent)
10946 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10947 loop
10948 if Is_Overloadable (Actual_Ent)
10949 and then
10950 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10951 and then
10952 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10953 then
10954 Set_Is_Hidden (Actual_Ent, False);
10955 Set_Is_Potentially_Use_Visible
10956 (Actual_Ent, In_Use (Actual_Pack));
10957 end if;
10959 Next_Entity (Actual_Ent);
10960 end loop;
10961 end;
10963 -- If the formal requires conformance checking, reanalyze it as an
10964 -- abbreviated instantiation, to verify the matching rules of 12.7.
10965 -- The actual checks are performed after the generic associations
10966 -- have been analyzed, to guarantee the same visibility for this
10967 -- instantiation and for the actuals.
10969 -- In Ada 2005, the generic associations for the formal can include
10970 -- defaulted parameters. These are ignored during check. This
10971 -- internal instantiation is removed from the tree after conformance
10972 -- checking, because it contains formal declarations for those
10973 -- defaulted parameters, and those should not reach the back-end.
10975 if Requires_Conformance_Checking (Formal) then
10976 declare
10977 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10979 I_Nam : Node_Id;
10981 begin
10982 Set_Is_Internal (I_Pack);
10983 Mutate_Ekind (I_Pack, E_Package);
10985 -- Insert the package into the list of its hidden entities so
10986 -- that the list is not empty for Is_Abbreviated_Instance.
10988 Append_Elmt (I_Pack, Hidden_Formals);
10990 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10992 -- If the generic is a child unit, Check_Generic_Child_Unit
10993 -- needs its original name in case it is qualified.
10995 if Is_Child_Unit (Gen_Parent) then
10996 I_Nam :=
10997 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
10998 pragma Assert (Entity (I_Nam) = Gen_Parent);
11000 else
11001 I_Nam :=
11002 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11003 end if;
11005 Append_To (Decls,
11006 Make_Package_Instantiation (Loc,
11007 Defining_Unit_Name => I_Pack,
11008 Name => I_Nam,
11009 Generic_Associations => Generic_Associations (Formal)));
11010 end;
11011 end if;
11013 return Decls;
11014 end if;
11015 end Instantiate_Formal_Package;
11017 -----------------------------------
11018 -- Instantiate_Formal_Subprogram --
11019 -----------------------------------
11021 function Instantiate_Formal_Subprogram
11022 (Formal : Node_Id;
11023 Actual : Node_Id;
11024 Analyzed_Formal : Node_Id) return Node_Id
11026 Analyzed_S : constant Entity_Id :=
11027 Defining_Unit_Name (Specification (Analyzed_Formal));
11028 Formal_Sub : constant Entity_Id :=
11029 Defining_Unit_Name (Specification (Formal));
11031 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11032 -- If the generic is a child unit, the parent has been installed on the
11033 -- scope stack, but a default subprogram cannot resolve to something
11034 -- on the parent because that parent is not really part of the visible
11035 -- context (it is there to resolve explicit local entities). If the
11036 -- default has resolved in this way, we remove the entity from immediate
11037 -- visibility and analyze the node again to emit an error message or
11038 -- find another visible candidate.
11040 procedure Valid_Actual_Subprogram (Act : Node_Id);
11041 -- Perform legality check and raise exception on failure
11043 -----------------------
11044 -- From_Parent_Scope --
11045 -----------------------
11047 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11048 Gen_Scope : Node_Id;
11050 begin
11051 Gen_Scope := Scope (Analyzed_S);
11052 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11053 if Scope (Subp) = Scope (Gen_Scope) then
11054 return True;
11055 end if;
11057 Gen_Scope := Scope (Gen_Scope);
11058 end loop;
11060 return False;
11061 end From_Parent_Scope;
11063 -----------------------------
11064 -- Valid_Actual_Subprogram --
11065 -----------------------------
11067 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11068 Act_E : Entity_Id;
11070 begin
11071 if Is_Entity_Name (Act) then
11072 Act_E := Entity (Act);
11074 elsif Nkind (Act) = N_Selected_Component
11075 and then Is_Entity_Name (Selector_Name (Act))
11076 then
11077 Act_E := Entity (Selector_Name (Act));
11079 else
11080 Act_E := Empty;
11081 end if;
11083 -- The actual for a ghost generic formal procedure should be a ghost
11084 -- procedure (SPARK RM 6.9(14)).
11086 if Present (Act_E)
11087 and then Ekind (Act_E) = E_Procedure
11088 then
11089 Check_Ghost_Formal_Procedure_Or_Package
11090 (N => Act,
11091 Actual => Act_E,
11092 Formal => Analyzed_S);
11093 end if;
11095 if (Present (Act_E) and then Is_Overloadable (Act_E))
11096 or else Nkind (Act) in N_Attribute_Reference
11097 | N_Indexed_Component
11098 | N_Character_Literal
11099 | N_Explicit_Dereference
11100 then
11101 return;
11102 end if;
11104 Error_Msg_NE
11105 ("expect subprogram or entry name in instantiation of &",
11106 Instantiation_Node, Formal_Sub);
11107 Abandon_Instantiation (Instantiation_Node);
11108 end Valid_Actual_Subprogram;
11110 -- Local variables
11112 Decl_Node : Node_Id;
11113 Loc : Source_Ptr;
11114 Nam : Node_Id;
11115 New_Spec : Node_Id;
11116 New_Subp : Entity_Id;
11118 -- Start of processing for Instantiate_Formal_Subprogram
11120 begin
11121 New_Spec := New_Copy_Tree (Specification (Formal));
11123 -- The tree copy has created the proper instantiation sloc for the
11124 -- new specification. Use this location for all other constructed
11125 -- declarations.
11127 Loc := Sloc (Defining_Unit_Name (New_Spec));
11129 -- Create new entity for the actual (New_Copy_Tree does not), and
11130 -- indicate that it is an actual.
11132 -- If the actual is not an entity (i.e. an attribute reference)
11133 -- and the formal includes aspect specifications for contracts,
11134 -- we create an internal name for the renaming declaration. The
11135 -- constructed wrapper contains a call to the entity in the renaming.
11136 -- This is an expansion activity, as is the wrapper creation.
11138 if Ada_Version >= Ada_2022
11139 and then Has_Contracts (Analyzed_Formal)
11140 and then not Is_Entity_Name (Actual)
11141 and then Expander_Active
11142 then
11143 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11144 else
11145 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11146 end if;
11148 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11149 Set_Is_Generic_Actual_Subprogram (New_Subp);
11150 Set_Defining_Unit_Name (New_Spec, New_Subp);
11152 -- Create new entities for the each of the formals in the specification
11153 -- of the renaming declaration built for the actual.
11155 if Present (Parameter_Specifications (New_Spec)) then
11156 declare
11157 F : Node_Id;
11158 F_Id : Entity_Id;
11160 begin
11161 F := First (Parameter_Specifications (New_Spec));
11162 while Present (F) loop
11163 F_Id := Defining_Identifier (F);
11165 Set_Defining_Identifier (F,
11166 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11167 Next (F);
11168 end loop;
11169 end;
11170 end if;
11172 -- Find entity of actual. If the actual is an attribute reference, it
11173 -- cannot be resolved here (its formal is missing) but is handled
11174 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11175 -- fully resolved subsequently, when the renaming declaration for the
11176 -- formal is analyzed. If it is an explicit dereference, resolve the
11177 -- prefix but not the actual itself, to prevent interpretation as call.
11179 if Present (Actual) then
11180 Loc := Sloc (Actual);
11181 Set_Sloc (New_Spec, Loc);
11183 if Nkind (Actual) = N_Operator_Symbol then
11184 Find_Direct_Name (Actual);
11186 elsif Nkind (Actual) = N_Explicit_Dereference then
11187 Analyze (Prefix (Actual));
11189 elsif Nkind (Actual) /= N_Attribute_Reference then
11190 Analyze (Actual);
11191 end if;
11193 Valid_Actual_Subprogram (Actual);
11194 Nam := Actual;
11196 elsif Present (Default_Name (Formal)) then
11197 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11198 | N_Selected_Component
11199 | N_Indexed_Component
11200 | N_Character_Literal
11201 and then Present (Entity (Default_Name (Formal)))
11202 then
11203 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11204 else
11205 Nam := New_Copy (Default_Name (Formal));
11206 Set_Sloc (Nam, Loc);
11207 end if;
11209 elsif Box_Present (Formal) then
11211 -- Actual is resolved at the point of instantiation. Create an
11212 -- identifier or operator with the same name as the formal.
11214 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11215 Nam :=
11216 Make_Operator_Symbol (Loc,
11217 Chars => Chars (Formal_Sub),
11218 Strval => No_String);
11219 else
11220 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11221 end if;
11223 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11224 and then Null_Present (Specification (Formal))
11225 then
11226 -- Generate null body for procedure, for use in the instance
11228 Decl_Node :=
11229 Make_Subprogram_Body (Loc,
11230 Specification => New_Spec,
11231 Declarations => New_List,
11232 Handled_Statement_Sequence =>
11233 Make_Handled_Sequence_Of_Statements (Loc,
11234 Statements => New_List (Make_Null_Statement (Loc))));
11236 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11238 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11240 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11242 -- Eliminate the calls to it when optimization is enabled
11244 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11245 return Decl_Node;
11247 -- Handle case of a formal function with an expression default (allowed
11248 -- when extensions are enabled).
11250 elsif Nkind (Specification (Formal)) = N_Function_Specification
11251 and then Present (Expression (Formal))
11252 then
11253 -- Generate body for function, for use in the instance
11255 declare
11256 Expr : constant Node_Id := New_Copy (Expression (Formal));
11257 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11258 begin
11259 Set_Sloc (Expr, Loc);
11260 Set_Expression (Stmt, Expr);
11262 Decl_Node :=
11263 Make_Subprogram_Body (Loc,
11264 Specification => New_Spec,
11265 Declarations => New_List,
11266 Handled_Statement_Sequence =>
11267 Make_Handled_Sequence_Of_Statements (Loc,
11268 Statements => New_List (Stmt)));
11269 end;
11271 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11272 -- has convention Intrinsic.
11274 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11276 -- Inline calls to it when optimization is enabled
11278 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11279 return Decl_Node;
11281 else
11282 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11283 Error_Msg_NE
11284 ("missing actual&", Instantiation_Node, Formal_Sub);
11285 Error_Msg_NE
11286 ("\in instantiation of & declared#",
11287 Instantiation_Node, Scope (Analyzed_S));
11288 Abandon_Instantiation (Instantiation_Node);
11289 end if;
11291 Decl_Node :=
11292 Make_Subprogram_Renaming_Declaration (Loc,
11293 Specification => New_Spec,
11294 Name => Nam);
11296 -- If we do not have an actual and the formal specified <> then set to
11297 -- get proper default.
11299 if No (Actual) and then Box_Present (Formal) then
11300 Set_From_Default (Decl_Node);
11301 end if;
11303 -- Gather possible interpretations for the actual before analyzing the
11304 -- instance. If overloaded, it will be resolved when analyzing the
11305 -- renaming declaration.
11307 if Box_Present (Formal) and then No (Actual) then
11308 Analyze (Nam);
11310 if Is_Child_Unit (Scope (Analyzed_S))
11311 and then Present (Entity (Nam))
11312 then
11313 if not Is_Overloaded (Nam) then
11314 if From_Parent_Scope (Entity (Nam)) then
11315 Set_Is_Immediately_Visible (Entity (Nam), False);
11316 Set_Entity (Nam, Empty);
11317 Set_Etype (Nam, Empty);
11319 Analyze (Nam);
11320 Set_Is_Immediately_Visible (Entity (Nam));
11321 end if;
11323 else
11324 declare
11325 I : Interp_Index;
11326 It : Interp;
11328 begin
11329 Get_First_Interp (Nam, I, It);
11330 while Present (It.Nam) loop
11331 if From_Parent_Scope (It.Nam) then
11332 Remove_Interp (I);
11333 end if;
11335 Get_Next_Interp (I, It);
11336 end loop;
11337 end;
11338 end if;
11339 end if;
11340 end if;
11342 -- The generic instantiation freezes the actual. This can only be done
11343 -- once the actual is resolved, in the analysis of the renaming
11344 -- declaration. To make the formal subprogram entity available, we set
11345 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11346 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11347 -- of formal abstract subprograms.
11349 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11351 -- We cannot analyze the renaming declaration, and thus find the actual,
11352 -- until all the actuals are assembled in the instance. For subsequent
11353 -- checks of other actuals, indicate the node that will hold the
11354 -- instance of this formal.
11356 Set_Instance_Of (Analyzed_S, Nam);
11358 if Nkind (Actual) = N_Selected_Component
11359 and then Is_Task_Type (Etype (Prefix (Actual)))
11360 and then not Is_Frozen (Etype (Prefix (Actual)))
11361 then
11362 -- The renaming declaration will create a body, which must appear
11363 -- outside of the instantiation, We move the renaming declaration
11364 -- out of the instance, and create an additional renaming inside,
11365 -- to prevent freezing anomalies.
11367 declare
11368 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11370 begin
11371 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11372 Insert_Before (Instantiation_Node, Decl_Node);
11373 Analyze (Decl_Node);
11375 -- Now create renaming within the instance
11377 Decl_Node :=
11378 Make_Subprogram_Renaming_Declaration (Loc,
11379 Specification => New_Copy_Tree (New_Spec),
11380 Name => New_Occurrence_Of (Anon_Id, Loc));
11382 Set_Defining_Unit_Name (Specification (Decl_Node),
11383 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11384 end;
11385 end if;
11387 return Decl_Node;
11388 end Instantiate_Formal_Subprogram;
11390 ------------------------
11391 -- Instantiate_Object --
11392 ------------------------
11394 function Instantiate_Object
11395 (Formal : Node_Id;
11396 Actual : Node_Id;
11397 Analyzed_Formal : Node_Id) return List_Id
11399 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11400 A_Gen_Obj : constant Entity_Id :=
11401 Defining_Identifier (Analyzed_Formal);
11402 Acc_Def : Node_Id := Empty;
11403 Act_Assoc : constant Node_Id :=
11404 (if No (Actual) then Empty else Parent (Actual));
11405 Actual_Decl : Node_Id := Empty;
11406 Decl_Node : Node_Id;
11407 Def : Node_Id;
11408 Ftyp : Entity_Id;
11409 List : constant List_Id := New_List;
11410 Loc : constant Source_Ptr := Sloc (Actual);
11411 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11412 Subt_Decl : Node_Id := Empty;
11413 Subt_Mark : Node_Id := Empty;
11415 -- Start of processing for Instantiate_Object
11417 begin
11418 -- Formal may be an anonymous access
11420 if Present (Subtype_Mark (Formal)) then
11421 Subt_Mark := Subtype_Mark (Formal);
11422 else
11423 Check_Access_Definition (Formal);
11424 Acc_Def := Access_Definition (Formal);
11425 end if;
11427 -- Sloc for error message on missing actual
11429 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11431 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11432 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11433 end if;
11435 Set_Parent (List, Act_Assoc);
11437 -- OUT present
11439 if Out_Present (Formal) then
11441 -- An IN OUT generic actual must be a name. The instantiation is a
11442 -- renaming declaration. The actual is the name being renamed. We
11443 -- use the actual directly, rather than a copy, because it is not
11444 -- used further in the list of actuals, and because a copy or a use
11445 -- of relocate_node is incorrect if the instance is nested within a
11446 -- generic. In order to simplify e.g. ASIS queries, the
11447 -- Generic_Parent field links the declaration to the generic
11448 -- association.
11450 if No (Actual) then
11451 Error_Msg_NE
11452 ("missing actual &",
11453 Instantiation_Node, Gen_Obj);
11454 Error_Msg_NE
11455 ("\in instantiation of & declared#",
11456 Instantiation_Node, Scope (A_Gen_Obj));
11457 Abandon_Instantiation (Instantiation_Node);
11458 end if;
11460 if Present (Subt_Mark) then
11461 Decl_Node :=
11462 Make_Object_Renaming_Declaration (Loc,
11463 Defining_Identifier => New_Copy (Gen_Obj),
11464 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11465 Name => Actual);
11467 else pragma Assert (Present (Acc_Def));
11468 Decl_Node :=
11469 Make_Object_Renaming_Declaration (Loc,
11470 Defining_Identifier => New_Copy (Gen_Obj),
11471 Access_Definition => New_Copy_Tree (Acc_Def),
11472 Name => Actual);
11473 end if;
11475 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11477 -- The analysis of the actual may produce Insert_Action nodes, so
11478 -- the declaration must have a context in which to attach them.
11480 Append (Decl_Node, List);
11481 Analyze (Actual);
11483 -- Return if the analysis of the actual reported some error
11485 if Etype (Actual) = Any_Type then
11486 return List;
11487 end if;
11489 -- This check is performed here because Analyze_Object_Renaming will
11490 -- not check it when Comes_From_Source is False. Note though that the
11491 -- check for the actual being the name of an object will be performed
11492 -- in Analyze_Object_Renaming.
11494 if Is_Object_Reference (Actual)
11495 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11496 then
11497 Error_Msg_N
11498 ("illegal discriminant-dependent component for in out parameter",
11499 Actual);
11500 end if;
11502 -- The actual has to be resolved in order to check that it is a
11503 -- variable (due to cases such as F (1), where F returns access to
11504 -- an array, and for overloaded prefixes).
11506 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11508 -- If the type of the formal is not itself a formal, and the current
11509 -- unit is a child unit, the formal type must be declared in a
11510 -- parent, and must be retrieved by visibility.
11512 if Ftyp = Orig_Ftyp
11513 and then Is_Generic_Unit (Scope (Ftyp))
11514 and then Is_Child_Unit (Scope (A_Gen_Obj))
11515 then
11516 declare
11517 Temp : constant Node_Id :=
11518 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11519 begin
11520 Set_Entity (Temp, Empty);
11521 Find_Type (Temp);
11522 Ftyp := Entity (Temp);
11523 end;
11524 end if;
11526 if Is_Private_Type (Ftyp)
11527 and then not Is_Private_Type (Etype (Actual))
11528 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11529 or else Base_Type (Etype (Actual)) = Ftyp)
11530 then
11531 -- If the actual has the type of the full view of the formal, or
11532 -- else a non-private subtype of the formal, then the visibility
11533 -- of the formal type has changed. Add to the actuals a subtype
11534 -- declaration that will force the exchange of views in the body
11535 -- of the instance as well.
11537 Subt_Decl :=
11538 Make_Subtype_Declaration (Loc,
11539 Defining_Identifier => Make_Temporary (Loc, 'P'),
11540 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11542 Prepend (Subt_Decl, List);
11544 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11545 Exchange_Declarations (Ftyp);
11546 end if;
11548 Resolve (Actual, Ftyp);
11550 if not Denotes_Variable (Actual) then
11551 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11553 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11555 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11556 -- the type of the actual shall resolve to a specific anonymous
11557 -- access type.
11559 if Ada_Version < Ada_2005
11560 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11561 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11562 then
11563 Error_Msg_NE
11564 ("type of actual does not match type of&", Actual, Gen_Obj);
11565 end if;
11566 end if;
11568 Note_Possible_Modification (Actual, Sure => True);
11570 -- Check for instantiation with atomic/volatile/VFA object actual for
11571 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11573 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11574 Error_Msg_NE
11575 ("cannot instantiate nonatomic formal & of mode in out",
11576 Actual, Gen_Obj);
11577 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11579 elsif Is_Volatile_Object_Ref (Actual)
11580 and then not Is_Volatile (Orig_Ftyp)
11581 then
11582 Error_Msg_NE
11583 ("cannot instantiate nonvolatile formal & of mode in out",
11584 Actual, Gen_Obj);
11585 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11587 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11588 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11589 then
11590 Error_Msg_NE
11591 ("cannot instantiate nonfull access formal & of mode in out",
11592 Actual, Gen_Obj);
11593 Error_Msg_N
11594 ("\with full access object actual (RM C.6(12))", Actual);
11595 end if;
11597 -- Check for instantiation on nonatomic subcomponent of a full access
11598 -- object in Ada 2022 (RM C.6 (12)).
11600 if Ada_Version >= Ada_2022
11601 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11602 and then not Is_Atomic_Object (Actual)
11603 then
11604 Error_Msg_NE
11605 ("cannot instantiate formal & of mode in out with actual",
11606 Actual, Gen_Obj);
11607 Error_Msg_N
11608 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11609 Actual);
11610 end if;
11612 -- The actual for a ghost generic formal IN OUT parameter should be a
11613 -- ghost object (SPARK RM 6.9(14)).
11615 Check_Ghost_Formal_Variable
11616 (Actual => Actual,
11617 Formal => A_Gen_Obj);
11619 -- Formal in-parameter
11621 else
11622 -- The instantiation of a generic formal in-parameter is constant
11623 -- declaration. The actual is the expression for that declaration.
11624 -- Its type is a full copy of the type of the formal. This may be
11625 -- an access to subprogram, for which we need to generate entities
11626 -- for the formals in the new signature.
11628 if Present (Actual) then
11629 if Present (Subt_Mark) then
11630 Def := New_Copy_Tree (Subt_Mark);
11631 else
11632 pragma Assert (Present (Acc_Def));
11633 Def := New_Copy_Tree (Acc_Def);
11634 end if;
11636 Decl_Node :=
11637 Make_Object_Declaration (Loc,
11638 Defining_Identifier => New_Copy (Gen_Obj),
11639 Constant_Present => True,
11640 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11641 Object_Definition => Def,
11642 Expression => Actual);
11644 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11645 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11647 -- A generic formal object of a tagged type is defined to be
11648 -- aliased so the new constant must also be treated as aliased.
11650 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11651 Set_Aliased_Present (Decl_Node);
11652 end if;
11654 Append (Decl_Node, List);
11656 -- The actual for a ghost generic formal IN parameter of
11657 -- access-to-variable type should be a ghost object (SPARK
11658 -- RM 6.9(14)).
11660 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11661 Check_Ghost_Formal_Variable
11662 (Actual => Actual,
11663 Formal => A_Gen_Obj);
11664 end if;
11666 -- No need to repeat (pre-)analysis of some expression nodes
11667 -- already handled in Preanalyze_Actuals.
11669 if Nkind (Actual) /= N_Allocator then
11670 Analyze (Actual);
11672 -- Return if the analysis of the actual reported some error
11674 if Etype (Actual) = Any_Type then
11675 return List;
11676 end if;
11677 end if;
11679 declare
11680 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11681 Typ : Entity_Id;
11683 begin
11684 Typ := Get_Instance_Of (Formal_Type);
11686 -- If the actual appears in the current or an enclosing scope,
11687 -- use its type directly. This is relevant if it has an actual
11688 -- subtype that is distinct from its nominal one. This cannot
11689 -- be done in general because the type of the actual may
11690 -- depend on other actuals, and only be fully determined when
11691 -- the enclosing instance is analyzed.
11693 if Present (Etype (Actual))
11694 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11695 then
11696 Freeze_Before (Instantiation_Node, Etype (Actual));
11697 else
11698 Freeze_Before (Instantiation_Node, Typ);
11699 end if;
11701 -- If the actual is an aggregate, perform name resolution on
11702 -- its components (the analysis of an aggregate does not do it)
11703 -- to capture local names that may be hidden if the generic is
11704 -- a child unit.
11706 if Nkind (Actual) = N_Aggregate then
11707 Preanalyze_And_Resolve (Actual, Typ);
11708 end if;
11710 if Is_Limited_Type (Typ)
11711 and then not OK_For_Limited_Init (Typ, Actual)
11712 then
11713 Error_Msg_N
11714 ("initialization not allowed for limited types", Actual);
11715 Explain_Limited_Type (Typ, Actual);
11716 end if;
11717 end;
11719 elsif Present (Default_Expression (Formal)) then
11721 -- Use default to construct declaration
11723 if Present (Subt_Mark) then
11724 Def := New_Copy_Tree (Subt_Mark);
11725 else
11726 pragma Assert (Present (Acc_Def));
11727 Def := New_Copy_Tree (Acc_Def);
11728 end if;
11730 Decl_Node :=
11731 Make_Object_Declaration (Sloc (Formal),
11732 Defining_Identifier => New_Copy (Gen_Obj),
11733 Constant_Present => True,
11734 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11735 Object_Definition => Def,
11736 Expression => New_Copy_Tree
11737 (Default_Expression (Formal)));
11739 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11740 Set_Corresponding_Generic_Association
11741 (Decl_Node, Expression (Decl_Node));
11743 Append (Decl_Node, List);
11744 Set_Analyzed (Expression (Decl_Node), False);
11746 else
11747 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11748 Error_Msg_NE ("\in instantiation of & declared#",
11749 Instantiation_Node, Scope (A_Gen_Obj));
11751 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11753 -- Create dummy constant declaration so that instance can be
11754 -- analyzed, to minimize cascaded visibility errors.
11756 if Present (Subt_Mark) then
11757 Def := Subt_Mark;
11758 else pragma Assert (Present (Acc_Def));
11759 Def := Acc_Def;
11760 end if;
11762 Decl_Node :=
11763 Make_Object_Declaration (Loc,
11764 Defining_Identifier => New_Copy (Gen_Obj),
11765 Constant_Present => True,
11766 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11767 Object_Definition => New_Copy (Def),
11768 Expression =>
11769 Make_Attribute_Reference (Sloc (Gen_Obj),
11770 Attribute_Name => Name_First,
11771 Prefix => New_Copy (Def)));
11773 Append (Decl_Node, List);
11775 else
11776 Abandon_Instantiation (Instantiation_Node);
11777 end if;
11778 end if;
11779 end if;
11781 if Nkind (Actual) in N_Has_Entity
11782 and then Present (Entity (Actual))
11783 then
11784 Actual_Decl := Parent (Entity (Actual));
11785 end if;
11787 -- Ada 2005 (AI-423) refined by AI12-0287:
11788 -- For an object_renaming_declaration with a null_exclusion or an
11789 -- access_definition that has a null_exclusion, the subtype of the
11790 -- object_name shall exclude null. In addition, if the
11791 -- object_renaming_declaration occurs within the body of a generic unit
11792 -- G or within the body of a generic unit declared within the
11793 -- declarative region of generic unit G, then:
11794 -- * if the object_name statically denotes a generic formal object of
11795 -- mode in out of G, then the declaration of that object shall have a
11796 -- null_exclusion;
11797 -- * if the object_name statically denotes a call of a generic formal
11798 -- function of G, then the declaration of the result of that function
11799 -- shall have a null_exclusion.
11801 if Ada_Version >= Ada_2005
11802 and then Present (Actual_Decl)
11803 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11804 | N_Object_Declaration
11805 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11806 and then not Has_Null_Exclusion (Actual_Decl)
11807 and then Has_Null_Exclusion (Analyzed_Formal)
11808 and then Ekind (Defining_Identifier (Analyzed_Formal))
11809 = E_Generic_In_Out_Parameter
11810 and then ((In_Generic_Scope (Entity (Actual))
11811 and then In_Package_Body (Scope (Entity (Actual))))
11812 or else not Can_Never_Be_Null (Etype (Actual)))
11813 then
11814 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11815 Error_Msg_N
11816 ("actual must exclude null to match generic formal#", Actual);
11817 end if;
11819 return List;
11820 end Instantiate_Object;
11822 ------------------------------
11823 -- Instantiate_Package_Body --
11824 ------------------------------
11826 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11827 -- must be replaced by gotos which jump to the end of the routine in order
11828 -- to restore the Ghost and SPARK modes.
11830 procedure Instantiate_Package_Body
11831 (Body_Info : Pending_Body_Info;
11832 Inlined_Body : Boolean := False;
11833 Body_Optional : Boolean := False)
11835 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11836 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11837 Act_Spec : constant Node_Id := Specification (Act_Decl);
11838 Ctx_Parents : Elist_Id := No_Elist;
11839 Ctx_Top : Int := 0;
11840 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11841 Gen_Id : constant Node_Id := Name (Inst_Node);
11842 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11843 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11844 Loc : constant Source_Ptr := Sloc (Inst_Node);
11846 procedure Check_Initialized_Types;
11847 -- In a generic package body, an entity of a generic private type may
11848 -- appear uninitialized. This is suspicious, unless the actual is a
11849 -- fully initialized type.
11851 procedure Install_Parents_Of_Generic_Context
11852 (Inst_Scope : Entity_Id;
11853 Ctx_Parents : out Elist_Id);
11854 -- Inst_Scope is the scope where the instance appears within; when it
11855 -- appears within a generic child package G, this routine collects and
11856 -- installs the enclosing packages of G in the scopes stack; installed
11857 -- packages are returned in Ctx_Parents.
11859 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11860 -- Reverse effect after instantiation is complete
11862 -----------------------------
11863 -- Check_Initialized_Types --
11864 -----------------------------
11866 procedure Check_Initialized_Types is
11867 Decl : Node_Id;
11868 Formal : Entity_Id;
11869 Actual : Entity_Id;
11870 Uninit_Var : Entity_Id;
11872 begin
11873 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11874 while Present (Decl) loop
11875 Uninit_Var := Empty;
11877 if Nkind (Decl) = N_Private_Extension_Declaration then
11878 Uninit_Var := Uninitialized_Variable (Decl);
11880 elsif Nkind (Decl) = N_Formal_Type_Declaration
11881 and then Nkind (Formal_Type_Definition (Decl)) =
11882 N_Formal_Private_Type_Definition
11883 then
11884 Uninit_Var :=
11885 Uninitialized_Variable (Formal_Type_Definition (Decl));
11886 end if;
11888 if Present (Uninit_Var) then
11889 Formal := Defining_Identifier (Decl);
11890 Actual := First_Entity (Act_Decl_Id);
11892 -- For each formal there is a subtype declaration that renames
11893 -- the actual and has the same name as the formal. Locate the
11894 -- formal for warning message about uninitialized variables
11895 -- in the generic, for which the actual type should be a fully
11896 -- initialized type.
11898 while Present (Actual) loop
11899 exit when Ekind (Actual) = E_Package
11900 and then Present (Renamed_Entity (Actual));
11902 if Chars (Actual) = Chars (Formal)
11903 and then not Is_Scalar_Type (Actual)
11904 and then not Is_Fully_Initialized_Type (Actual)
11905 and then Warn_On_No_Value_Assigned
11906 then
11907 Error_Msg_Node_2 := Formal;
11908 Error_Msg_NE
11909 ("generic unit has uninitialized variable& of "
11910 & "formal private type &?v?", Actual, Uninit_Var);
11911 Error_Msg_NE
11912 ("actual type for& should be fully initialized type?v?",
11913 Actual, Formal);
11914 exit;
11915 end if;
11917 Next_Entity (Actual);
11918 end loop;
11919 end if;
11921 Next (Decl);
11922 end loop;
11923 end Check_Initialized_Types;
11925 ----------------------------------------
11926 -- Install_Parents_Of_Generic_Context --
11927 ----------------------------------------
11929 procedure Install_Parents_Of_Generic_Context
11930 (Inst_Scope : Entity_Id;
11931 Ctx_Parents : out Elist_Id)
11933 Elmt : Elmt_Id;
11934 S : Entity_Id;
11936 begin
11937 Ctx_Parents := New_Elmt_List;
11939 -- Collect context parents (ie. parents where the instantiation
11940 -- appears within).
11942 S := Inst_Scope;
11943 while S /= Standard_Standard loop
11944 Prepend_Elmt (S, Ctx_Parents);
11945 S := Scope (S);
11946 end loop;
11948 -- Install enclosing parents
11950 Elmt := First_Elmt (Ctx_Parents);
11951 while Present (Elmt) loop
11952 Push_Scope (Node (Elmt));
11953 Set_Is_Immediately_Visible (Node (Elmt));
11954 Next_Elmt (Elmt);
11955 end loop;
11956 end Install_Parents_Of_Generic_Context;
11958 ---------------------------------------
11959 -- Remove_Parents_Of_Generic_Context --
11960 ---------------------------------------
11962 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11963 Elmt : Elmt_Id;
11965 begin
11966 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11968 Elmt := Last_Elmt (Ctx_Parents);
11969 while Present (Elmt) loop
11970 pragma Assert (Current_Scope = Node (Elmt));
11971 Set_Is_Immediately_Visible (Current_Scope, False);
11972 Pop_Scope;
11974 Remove_Last_Elmt (Ctx_Parents);
11975 Elmt := Last_Elmt (Ctx_Parents);
11976 end loop;
11977 end Remove_Parents_Of_Generic_Context;
11979 -- Local variables
11981 -- The following constants capture the context prior to instantiating
11982 -- the package body.
11984 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11985 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11986 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11987 Saved_ISMP : constant Boolean :=
11988 Ignore_SPARK_Mode_Pragmas_In_Instance;
11989 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11990 Local_Suppress_Stack_Top;
11991 Saved_SC : constant Boolean := Style_Check;
11992 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11993 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11994 Saved_SS : constant Suppress_Record := Scope_Suppress;
11995 Saved_Warn : constant Warnings_State := Save_Warnings;
11997 Act_Body : Node_Id;
11998 Act_Body_Id : Entity_Id;
11999 Act_Body_Name : Node_Id;
12000 Gen_Body : Node_Id;
12001 Gen_Body_Id : Node_Id;
12002 Par_Ent : Entity_Id := Empty;
12003 Par_Installed : Boolean := False;
12004 Par_Vis : Boolean := False;
12006 Scope_Check_Id : Entity_Id;
12007 Scope_Check_Last : Nat;
12008 -- Value of Current_Scope before calls to Install_Parents; used to check
12009 -- that scopes are correctly removed after instantiation.
12011 Vis_Prims_List : Elist_Id := No_Elist;
12012 -- List of primitives made temporarily visible in the instantiation
12013 -- to match the visibility of the formal type.
12015 -- Start of processing for Instantiate_Package_Body
12017 begin
12018 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12020 -- The instance body may already have been processed, as the parent of
12021 -- another instance that is inlined (Load_Parent_Of_Generic).
12023 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12024 return;
12025 end if;
12027 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12029 -- Re-establish the state of information on which checks are suppressed.
12030 -- This information was set in Body_Info at the point of instantiation,
12031 -- and now we restore it so that the instance is compiled using the
12032 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12034 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12035 Scope_Suppress := Body_Info.Scope_Suppress;
12037 Restore_Config_Switches (Body_Info.Config_Switches);
12038 Restore_Warnings (Body_Info.Warnings);
12040 if No (Gen_Body_Id) then
12042 -- Do not look for parent of generic body if none is required.
12043 -- This may happen when the routine is called as part of the
12044 -- Pending_Instantiations processing, when nested instances
12045 -- may precede the one generated from the main unit.
12047 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12048 and then Body_Optional
12049 then
12050 goto Leave;
12051 else
12052 Load_Parent_Of_Generic
12053 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12055 -- Surprisingly enough, loading the body of the parent can cause
12056 -- the body to be instantiated and the double instantiation needs
12057 -- to be prevented in order to avoid giving bogus semantic errors.
12059 -- This case can occur because of the Collect_Previous_Instances
12060 -- machinery of Load_Parent_Of_Generic, which will instantiate
12061 -- bodies that are deemed to be ahead of the body of the parent
12062 -- in the compilation unit. But the relative position of these
12063 -- bodies is computed using the mere comparison of their Sloc.
12065 -- Now suppose that you have two generic packages G and H, with
12066 -- G containing a mere instantiation of H:
12068 -- generic
12069 -- package H is
12071 -- generic
12072 -- package Nested_G is
12073 -- ...
12074 -- end Nested_G;
12076 -- end H;
12078 -- with H;
12080 -- generic
12081 -- package G is
12083 -- package My_H is new H;
12085 -- end G;
12087 -- and a third package Q instantiating G and Nested_G:
12089 -- with G;
12091 -- package Q is
12093 -- package My_G is new G;
12095 -- package My_Nested_G is new My_G.My_H.Nested_G;
12097 -- end Q;
12099 -- The body to be instantiated is that of My_Nested_G and its
12100 -- parent is the instance My_G.My_H. This latter instantiation
12101 -- is done when My_G is analyzed, i.e. after the declarations
12102 -- of My_G and My_Nested_G have been parsed; as a result, the
12103 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12105 -- Therefore loading the body of My_G.My_H will cause the body
12106 -- of My_Nested_G to be instantiated because it is deemed to be
12107 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12108 -- will again be invoked on My_G.My_H, but this time with the
12109 -- Collect_Previous_Instances machinery disabled, so there is
12110 -- no endless mutual recursion and things are done in order.
12112 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12113 goto Leave;
12114 end if;
12116 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12117 end if;
12118 end if;
12120 -- Establish global variable for sloc adjustment and for error recovery
12121 -- In the case of an instance body for an instantiation with actuals
12122 -- from a limited view, the instance body is placed at the beginning
12123 -- of the enclosing package body: use the body entity as the source
12124 -- location for nodes of the instance body.
12126 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12127 declare
12128 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12129 Body_Id : constant Node_Id :=
12130 Corresponding_Body (Unit_Declaration_Node (Scop));
12132 begin
12133 Instantiation_Node := Body_Id;
12134 end;
12135 else
12136 Instantiation_Node := Inst_Node;
12137 end if;
12139 -- The package being instantiated may be subject to pragma Ghost. Set
12140 -- the mode now to ensure that any nodes generated during instantiation
12141 -- are properly marked as Ghost.
12143 Set_Ghost_Mode (Act_Decl_Id);
12145 if Present (Gen_Body_Id) then
12146 Save_Env (Gen_Unit, Act_Decl_Id);
12147 Style_Check := False;
12149 -- If the context of the instance is subject to SPARK_Mode "off", the
12150 -- annotation is missing, or the body is instantiated at a later pass
12151 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12152 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12153 -- instance.
12155 if SPARK_Mode /= On
12156 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12157 then
12158 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12159 end if;
12161 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12162 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12164 Create_Instantiation_Source
12165 (Inst_Node, Gen_Body_Id, S_Adjustment);
12167 Act_Body :=
12168 Copy_Generic_Node
12169 (Original_Node (Gen_Body), Empty, Instantiating => True);
12171 -- Create proper (possibly qualified) defining name for the body, to
12172 -- correspond to the one in the spec.
12174 Act_Body_Id :=
12175 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12176 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12178 -- Some attributes of spec entity are not inherited by body entity
12180 Set_Handler_Records (Act_Body_Id, No_List);
12182 if Nkind (Defining_Unit_Name (Act_Spec)) =
12183 N_Defining_Program_Unit_Name
12184 then
12185 Act_Body_Name :=
12186 Make_Defining_Program_Unit_Name (Loc,
12187 Name =>
12188 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12189 Defining_Identifier => Act_Body_Id);
12190 else
12191 Act_Body_Name := Act_Body_Id;
12192 end if;
12194 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12196 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12197 Check_Generic_Actuals (Act_Decl_Id, False);
12198 Check_Initialized_Types;
12200 -- Install primitives hidden at the point of the instantiation but
12201 -- visible when processing the generic formals
12203 declare
12204 E : Entity_Id;
12206 begin
12207 E := First_Entity (Act_Decl_Id);
12208 while Present (E) loop
12209 if Is_Type (E)
12210 and then not Is_Itype (E)
12211 and then Is_Generic_Actual_Type (E)
12212 and then Is_Tagged_Type (E)
12213 then
12214 Install_Hidden_Primitives
12215 (Prims_List => Vis_Prims_List,
12216 Gen_T => Generic_Parent_Type (Parent (E)),
12217 Act_T => E);
12218 end if;
12220 Next_Entity (E);
12221 end loop;
12222 end;
12224 Scope_Check_Id := Current_Scope;
12225 Scope_Check_Last := Scope_Stack.Last;
12227 -- If the instantiation appears within a generic child some actual
12228 -- parameter may be the current instance of the enclosing generic
12229 -- parent.
12231 declare
12232 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12234 begin
12235 if Is_Child_Unit (Inst_Scope)
12236 and then Ekind (Inst_Scope) = E_Generic_Package
12237 and then Present (Generic_Associations (Inst_Node))
12238 then
12239 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12241 -- Hide them from visibility; required to avoid conflicts
12242 -- installing the parent instance.
12244 if Present (Ctx_Parents) then
12245 Push_Scope (Standard_Standard);
12246 Ctx_Top := Scope_Stack.Last;
12247 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12248 end if;
12249 end if;
12250 end;
12252 -- If it is a child unit, make the parent instance (which is an
12253 -- instance of the parent of the generic) visible.
12255 -- 1) The child unit's parent is an explicit parent instance (the
12256 -- prefix of the name of the generic unit):
12258 -- package Child_Package is new Parent_Instance.Child_Unit;
12260 -- 2) The child unit's parent is an implicit parent instance (e.g.
12261 -- when instantiating a sibling package):
12263 -- generic
12264 -- package Parent.Second_Child is
12265 -- ...
12267 -- generic
12268 -- package Parent.First_Child is
12269 -- package Sibling_Package is new Second_Child;
12271 -- 3) The child unit's parent is not an instance, so the scope is
12272 -- simply the one of the unit.
12274 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12275 and then Nkind (Gen_Id) = N_Expanded_Name
12276 then
12277 Par_Ent := Entity (Prefix (Gen_Id));
12279 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12280 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12281 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12282 and then Nkind
12283 (Name (Get_Unit_Instantiation_Node
12284 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12285 then
12286 Par_Ent := Entity
12287 (Prefix (Name (Get_Unit_Instantiation_Node
12288 (Scope (Act_Decl_Id)))));
12290 elsif Is_Child_Unit (Gen_Unit) then
12291 Par_Ent := Scope (Gen_Unit);
12292 end if;
12294 if Present (Par_Ent) then
12295 Par_Vis := Is_Immediately_Visible (Par_Ent);
12296 Install_Parent (Par_Ent, In_Body => True);
12297 Par_Installed := True;
12298 end if;
12300 -- If the instantiation is a library unit, and this is the main unit,
12301 -- then build the resulting compilation unit nodes for the instance.
12302 -- If this is a compilation unit but it is not the main unit, then it
12303 -- is the body of a unit in the context, that is being compiled
12304 -- because it is encloses some inlined unit or another generic unit
12305 -- being instantiated. In that case, this body is not part of the
12306 -- current compilation, and is not attached to the tree, but its
12307 -- parent must be set for analysis.
12309 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12311 -- Replace instance node with body of instance, and create new
12312 -- node for corresponding instance declaration.
12314 Build_Instance_Compilation_Unit_Nodes
12315 (Inst_Node, Act_Body, Act_Decl);
12317 -- If the instantiation appears within a generic child package
12318 -- enable visibility of current instance of enclosing generic
12319 -- parents.
12321 if Present (Ctx_Parents) then
12322 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12323 Analyze (Inst_Node);
12324 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12325 else
12326 Analyze (Inst_Node);
12327 end if;
12329 if Parent (Inst_Node) = Cunit (Main_Unit) then
12331 -- If the instance is a child unit itself, then set the scope
12332 -- of the expanded body to be the parent of the instantiation
12333 -- (ensuring that the fully qualified name will be generated
12334 -- for the elaboration subprogram).
12336 if Nkind (Defining_Unit_Name (Act_Spec)) =
12337 N_Defining_Program_Unit_Name
12338 then
12339 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12340 end if;
12341 end if;
12343 -- Case where instantiation is not a library unit
12345 else
12346 -- Handle the case of an instance with incomplete actual types.
12347 -- The instance body cannot be placed just after the declaration
12348 -- because full views have not been seen yet. Any use of the non-
12349 -- limited views in the instance body requires the presence of a
12350 -- regular with_clause in the enclosing unit. Therefore we place
12351 -- the instance body at the beginning of the enclosing body, and
12352 -- the freeze node for the instance is then placed after the body.
12354 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12355 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12356 then
12357 declare
12358 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12359 Body_Id : constant Node_Id :=
12360 Corresponding_Body (Unit_Declaration_Node (Scop));
12362 F_Node : Node_Id;
12364 begin
12365 pragma Assert (Present (Body_Id));
12367 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12369 if Expander_Active then
12370 Ensure_Freeze_Node (Act_Decl_Id);
12371 F_Node := Freeze_Node (Act_Decl_Id);
12372 Set_Is_Frozen (Act_Decl_Id, False);
12373 if Is_List_Member (F_Node) then
12374 Remove (F_Node);
12375 end if;
12377 Insert_After (Act_Body, F_Node);
12378 end if;
12379 end;
12381 else
12382 Insert_Before (Inst_Node, Act_Body);
12383 Mark_Rewrite_Insertion (Act_Body);
12385 -- Insert the freeze node for the instance if need be
12387 if Expander_Active then
12388 Freeze_Package_Instance
12389 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12390 Set_Is_Frozen (Act_Decl_Id);
12391 end if;
12392 end if;
12394 -- If the instantiation appears within a generic child package
12395 -- enable visibility of current instance of enclosing generic
12396 -- parents.
12398 if Present (Ctx_Parents) then
12399 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12400 Analyze (Act_Body);
12401 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12402 else
12403 Analyze (Act_Body);
12404 end if;
12405 end if;
12407 Inherit_Context (Gen_Body, Inst_Node);
12409 if Par_Installed then
12410 Remove_Parent (In_Body => True);
12412 -- Restore the previous visibility of the parent
12414 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12415 end if;
12417 -- Remove the parent instances if they have been placed on the scope
12418 -- stack to compile the body.
12420 if Present (Ctx_Parents) then
12421 pragma Assert (Scope_Stack.Last = Ctx_Top
12422 and then Current_Scope = Standard_Standard);
12423 Pop_Scope;
12425 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12426 end if;
12428 pragma Assert (Current_Scope = Scope_Check_Id);
12429 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12431 Restore_Hidden_Primitives (Vis_Prims_List);
12433 -- Restore the private views that were made visible when the body of
12434 -- the instantiation was created. Note that, in the case where one of
12435 -- these private views is declared in the parent, there is a nesting
12436 -- issue with the calls to Install_Parent and Remove_Parent made in
12437 -- between above with In_Body set to True, because these calls also
12438 -- want to swap and restore this private view respectively. In this
12439 -- case, the call to Install_Parent does nothing, but the call to
12440 -- Remove_Parent does restore the private view, thus undercutting the
12441 -- call to Restore_Private_Views. That's OK under the condition that
12442 -- the two mechanisms swap exactly the same entities, in particular
12443 -- the private entities dependent on the primary private entities.
12445 Restore_Private_Views (Act_Decl_Id);
12447 -- Remove the current unit from visibility if this is an instance
12448 -- that is not elaborated on the fly for inlining purposes.
12450 if not Inlined_Body then
12451 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12452 end if;
12454 Restore_Env;
12456 -- If we have no body, and the unit requires a body, then complain. This
12457 -- complaint is suppressed if we have detected other errors (since a
12458 -- common reason for missing the body is that it had errors).
12459 -- In CodePeer mode, a warning has been emitted already, no need for
12460 -- further messages.
12462 elsif Unit_Requires_Body (Gen_Unit)
12463 and then not Body_Optional
12464 then
12465 if CodePeer_Mode then
12466 null;
12468 elsif Serious_Errors_Detected = 0 then
12469 Error_Msg_NE
12470 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12472 -- Don't attempt to perform any cleanup actions if some other error
12473 -- was already detected, since this can cause blowups.
12475 else
12476 goto Leave;
12477 end if;
12479 -- Case of package that does not need a body
12481 else
12482 -- If the instantiation of the declaration is a library unit, rewrite
12483 -- the original package instantiation as a package declaration in the
12484 -- compilation unit node.
12486 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12487 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12488 Rewrite (Inst_Node, Act_Decl);
12490 -- Generate elaboration entity, in case spec has elaboration code.
12491 -- This cannot be done when the instance is analyzed, because it
12492 -- is not known yet whether the body exists.
12494 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12495 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12497 -- If the instantiation is not a library unit, then append the
12498 -- declaration to the list of implicitly generated entities, unless
12499 -- it is already a list member which means that it was already
12500 -- processed
12502 elsif not Is_List_Member (Act_Decl) then
12503 Mark_Rewrite_Insertion (Act_Decl);
12504 Insert_Before (Inst_Node, Act_Decl);
12505 end if;
12506 end if;
12508 <<Leave>>
12510 -- Restore the context that was in effect prior to instantiating the
12511 -- package body.
12513 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12514 Local_Suppress_Stack_Top := Saved_LSST;
12515 Scope_Suppress := Saved_SS;
12516 Style_Check := Saved_SC;
12518 Expander_Mode_Restore;
12519 Restore_Config_Switches (Saved_CS);
12520 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12521 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12522 Restore_Warnings (Saved_Warn);
12523 end Instantiate_Package_Body;
12525 ---------------------------------
12526 -- Instantiate_Subprogram_Body --
12527 ---------------------------------
12529 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12530 -- must be replaced by gotos which jump to the end of the routine in order
12531 -- to restore the Ghost and SPARK modes.
12533 procedure Instantiate_Subprogram_Body
12534 (Body_Info : Pending_Body_Info;
12535 Body_Optional : Boolean := False)
12537 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12538 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12539 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12540 Gen_Id : constant Node_Id := Name (Inst_Node);
12541 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12542 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12543 Loc : constant Source_Ptr := Sloc (Inst_Node);
12544 Pack_Id : constant Entity_Id :=
12545 Defining_Unit_Name (Parent (Act_Decl));
12547 -- The following constants capture the context prior to instantiating
12548 -- the subprogram body.
12550 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12551 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12552 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12553 Saved_ISMP : constant Boolean :=
12554 Ignore_SPARK_Mode_Pragmas_In_Instance;
12555 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12556 Local_Suppress_Stack_Top;
12557 Saved_SC : constant Boolean := Style_Check;
12558 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12559 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12560 Saved_SS : constant Suppress_Record := Scope_Suppress;
12561 Saved_Warn : constant Warnings_State := Save_Warnings;
12563 Act_Body : Node_Id;
12564 Act_Body_Id : Entity_Id;
12565 Gen_Body : Node_Id;
12566 Gen_Body_Id : Node_Id;
12567 Pack_Body : Node_Id;
12568 Par_Ent : Entity_Id := Empty;
12569 Par_Installed : Boolean := False;
12570 Par_Vis : Boolean := False;
12571 Ret_Expr : Node_Id;
12573 begin
12574 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12576 -- Subprogram body may have been created already because of an inline
12577 -- pragma, or because of multiple elaborations of the enclosing package
12578 -- when several instances of the subprogram appear in the main unit.
12580 if Present (Corresponding_Body (Act_Decl)) then
12581 return;
12582 end if;
12584 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12586 -- Re-establish the state of information on which checks are suppressed.
12587 -- This information was set in Body_Info at the point of instantiation,
12588 -- and now we restore it so that the instance is compiled using the
12589 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12591 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12592 Scope_Suppress := Body_Info.Scope_Suppress;
12594 Restore_Config_Switches (Body_Info.Config_Switches);
12595 Restore_Warnings (Body_Info.Warnings);
12597 if No (Gen_Body_Id) then
12599 -- For imported generic subprogram, no body to compile, complete
12600 -- the spec entity appropriately.
12602 if Is_Imported (Gen_Unit) then
12603 Set_Is_Imported (Act_Decl_Id);
12604 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12605 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12606 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12607 Set_Has_Completion (Act_Decl_Id);
12608 goto Leave;
12610 -- For other cases, compile the body
12612 else
12613 Load_Parent_Of_Generic
12614 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12615 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12616 end if;
12617 end if;
12619 Instantiation_Node := Inst_Node;
12621 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12622 -- the mode now to ensure that any nodes generated during instantiation
12623 -- are properly marked as Ghost.
12625 Set_Ghost_Mode (Act_Decl_Id);
12627 if Present (Gen_Body_Id) then
12628 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12630 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12632 -- Either body is not present, or context is non-expanding, as
12633 -- when compiling a subunit. Mark the instance as completed, and
12634 -- diagnose a missing body when needed.
12636 if Expander_Active
12637 and then Operating_Mode = Generate_Code
12638 then
12639 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12640 end if;
12642 Set_Has_Completion (Act_Decl_Id);
12643 goto Leave;
12644 end if;
12646 Save_Env (Gen_Unit, Act_Decl_Id);
12647 Style_Check := False;
12649 -- If the context of the instance is subject to SPARK_Mode "off", the
12650 -- annotation is missing, or the body is instantiated at a later pass
12651 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12652 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12653 -- instance.
12655 if SPARK_Mode /= On
12656 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12657 then
12658 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12659 end if;
12661 -- If the context of an instance is not subject to SPARK_Mode "off",
12662 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12663 -- the latter should be the one applicable to the instance.
12665 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12666 and then SPARK_Mode /= Off
12667 and then Present (SPARK_Pragma (Gen_Body_Id))
12668 then
12669 Set_SPARK_Mode (Gen_Body_Id);
12670 end if;
12672 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12673 Create_Instantiation_Source
12674 (Inst_Node,
12675 Gen_Body_Id,
12676 S_Adjustment);
12678 Act_Body :=
12679 Copy_Generic_Node
12680 (Original_Node (Gen_Body), Empty, Instantiating => True);
12682 -- Create proper defining name for the body, to correspond to the one
12683 -- in the spec.
12685 Act_Body_Id :=
12686 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12688 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12689 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12691 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12692 Set_Has_Completion (Act_Decl_Id);
12693 Check_Generic_Actuals (Pack_Id, False);
12695 -- Generate a reference to link the visible subprogram instance to
12696 -- the generic body, which for navigation purposes is the only
12697 -- available source for the instance.
12699 Generate_Reference
12700 (Related_Instance (Pack_Id),
12701 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12703 -- If it is a child unit, make the parent instance (which is an
12704 -- instance of the parent of the generic) visible. The parent
12705 -- instance is the prefix of the name of the generic unit.
12707 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12708 and then Nkind (Gen_Id) = N_Expanded_Name
12709 then
12710 Par_Ent := Entity (Prefix (Gen_Id));
12711 elsif Is_Child_Unit (Gen_Unit) then
12712 Par_Ent := Scope (Gen_Unit);
12713 end if;
12715 if Present (Par_Ent) then
12716 Par_Vis := Is_Immediately_Visible (Par_Ent);
12717 Install_Parent (Par_Ent, In_Body => True);
12718 Par_Installed := True;
12719 end if;
12721 -- Subprogram body is placed in the body of wrapper package,
12722 -- whose spec contains the subprogram declaration as well as
12723 -- the renaming declarations for the generic parameters.
12725 Pack_Body :=
12726 Make_Package_Body (Loc,
12727 Defining_Unit_Name => New_Copy (Pack_Id),
12728 Declarations => New_List (Act_Body));
12730 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12732 -- If the instantiation is a library unit, then build resulting
12733 -- compilation unit nodes for the instance. The declaration of
12734 -- the enclosing package is the grandparent of the subprogram
12735 -- declaration. First replace the instantiation node as the unit
12736 -- of the corresponding compilation.
12738 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12739 if Parent (Inst_Node) = Cunit (Main_Unit) then
12740 Set_Unit (Parent (Inst_Node), Inst_Node);
12741 Build_Instance_Compilation_Unit_Nodes
12742 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12743 Analyze (Inst_Node);
12744 else
12745 Set_Parent (Pack_Body, Parent (Inst_Node));
12746 Analyze (Pack_Body);
12747 end if;
12749 else
12750 Insert_Before (Inst_Node, Pack_Body);
12751 Mark_Rewrite_Insertion (Pack_Body);
12753 -- Insert the freeze node for the instance if need be
12755 if Expander_Active then
12756 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12757 end if;
12759 Analyze (Pack_Body);
12760 end if;
12762 Inherit_Context (Gen_Body, Inst_Node);
12764 Restore_Private_Views (Pack_Id, False);
12766 if Par_Installed then
12767 Remove_Parent (In_Body => True);
12769 -- Restore the previous visibility of the parent
12771 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12772 end if;
12774 Restore_Env;
12776 -- Body not found. Error was emitted already. If there were no previous
12777 -- errors, this may be an instance whose scope is a premature instance.
12778 -- In that case we must insure that the (legal) program does raise
12779 -- program error if executed. We generate a subprogram body for this
12780 -- purpose.
12782 elsif Serious_Errors_Detected = 0
12783 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12784 then
12785 if Body_Optional then
12786 goto Leave;
12788 elsif Ekind (Act_Decl_Id) = E_Procedure then
12789 Act_Body :=
12790 Make_Subprogram_Body (Loc,
12791 Specification =>
12792 Make_Procedure_Specification (Loc,
12793 Defining_Unit_Name =>
12794 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12795 Parameter_Specifications =>
12796 New_Copy_List
12797 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12799 Declarations => Empty_List,
12800 Handled_Statement_Sequence =>
12801 Make_Handled_Sequence_Of_Statements (Loc,
12802 Statements => New_List (
12803 Make_Raise_Program_Error (Loc,
12804 Reason => PE_Access_Before_Elaboration))));
12806 else
12807 Ret_Expr :=
12808 Make_Raise_Program_Error (Loc,
12809 Reason => PE_Access_Before_Elaboration);
12811 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12812 Set_Analyzed (Ret_Expr);
12814 Act_Body :=
12815 Make_Subprogram_Body (Loc,
12816 Specification =>
12817 Make_Function_Specification (Loc,
12818 Defining_Unit_Name =>
12819 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12820 Parameter_Specifications =>
12821 New_Copy_List
12822 (Parameter_Specifications (Parent (Act_Decl_Id))),
12823 Result_Definition =>
12824 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12826 Declarations => Empty_List,
12827 Handled_Statement_Sequence =>
12828 Make_Handled_Sequence_Of_Statements (Loc,
12829 Statements => New_List (
12830 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12831 end if;
12833 Pack_Body :=
12834 Make_Package_Body (Loc,
12835 Defining_Unit_Name => New_Copy (Pack_Id),
12836 Declarations => New_List (Act_Body));
12838 Insert_After (Inst_Node, Pack_Body);
12839 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12840 Analyze (Pack_Body);
12841 end if;
12843 <<Leave>>
12845 -- Restore the context that was in effect prior to instantiating the
12846 -- subprogram body.
12848 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12849 Local_Suppress_Stack_Top := Saved_LSST;
12850 Scope_Suppress := Saved_SS;
12851 Style_Check := Saved_SC;
12853 Expander_Mode_Restore;
12854 Restore_Config_Switches (Saved_CS);
12855 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12856 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12857 Restore_Warnings (Saved_Warn);
12858 end Instantiate_Subprogram_Body;
12860 ----------------------
12861 -- Instantiate_Type --
12862 ----------------------
12864 function Instantiate_Type
12865 (Formal : Node_Id;
12866 Actual : Node_Id;
12867 Analyzed_Formal : Node_Id;
12868 Actual_Decls : List_Id) return List_Id
12870 A_Gen_T : constant Entity_Id :=
12871 Defining_Identifier (Analyzed_Formal);
12872 Def : constant Node_Id := Formal_Type_Definition (Formal);
12873 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12874 Act_T : Entity_Id;
12875 Ancestor : Entity_Id := Empty;
12876 Decl_Node : Node_Id;
12877 Decl_Nodes : List_Id;
12878 Loc : Source_Ptr;
12879 Subt : Entity_Id;
12881 procedure Check_Shared_Variable_Control_Aspects;
12882 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12883 -- that may be specified for a formal type are obeyed by the actual.
12885 procedure Diagnose_Predicated_Actual;
12886 -- There are a number of constructs in which a discrete type with
12887 -- predicates is illegal, e.g. as an index in an array type declaration.
12888 -- If a generic type is used is such a construct in a generic package
12889 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12890 -- of the generic contract that the actual cannot have predicates.
12892 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12893 -- Check that base types are the same and that the subtypes match
12894 -- statically. Used in several of the validation subprograms for
12895 -- actuals in instantiations.
12897 procedure Validate_Array_Type_Instance;
12898 procedure Validate_Access_Subprogram_Instance;
12899 procedure Validate_Access_Type_Instance;
12900 procedure Validate_Derived_Type_Instance;
12901 procedure Validate_Derived_Interface_Type_Instance;
12902 procedure Validate_Discriminated_Formal_Type;
12903 procedure Validate_Interface_Type_Instance;
12904 procedure Validate_Private_Type_Instance;
12905 procedure Validate_Incomplete_Type_Instance;
12906 -- These procedures perform validation tests for the named case.
12907 -- Validate_Discriminated_Formal_Type is shared by formal private
12908 -- types and Ada 2012 formal incomplete types.
12910 --------------------------------------------
12911 -- Check_Shared_Variable_Control_Aspects --
12912 --------------------------------------------
12914 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12915 -- that may be specified for the formal are obeyed by the actual.
12916 -- If the formal is a derived type the aspect specifications must match.
12917 -- NOTE: AI12-0282 implies that matching of aspects is required between
12918 -- formal and actual in all cases, but this is too restrictive.
12919 -- In particular it violates a language design rule: a limited private
12920 -- indefinite formal can be matched by any actual. The current code
12921 -- reflects an older and more permissive version of RM C.6 (12/5).
12923 procedure Check_Shared_Variable_Control_Aspects is
12924 begin
12925 if Ada_Version >= Ada_2022 then
12926 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12927 Error_Msg_NE
12928 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12930 elsif Is_Derived_Type (A_Gen_T)
12931 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12932 then
12933 Error_Msg_NE
12934 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12935 end if;
12937 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12938 Error_Msg_NE
12939 ("actual for& must have Volatile aspect",
12940 Actual, A_Gen_T);
12942 elsif Is_Derived_Type (A_Gen_T)
12943 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12944 then
12945 Error_Msg_NE
12946 ("actual for& has different Volatile aspect",
12947 Actual, A_Gen_T);
12948 end if;
12950 -- We assume that an array type whose atomic component type
12951 -- is Atomic is equivalent to an array type with the explicit
12952 -- aspect Has_Atomic_Components. This is a reasonable inference
12953 -- from the intent of AI12-0282, and makes it legal to use an
12954 -- actual that does not have the identical aspect as the formal.
12955 -- Ditto for volatile components.
12957 declare
12958 Actual_Atomic_Comp : constant Boolean :=
12959 Has_Atomic_Components (Act_T)
12960 or else (Is_Array_Type (Act_T)
12961 and then Is_Atomic (Component_Type (Act_T)));
12962 begin
12963 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12964 Error_Msg_NE
12965 ("formal and actual for& must agree on atomic components",
12966 Actual, A_Gen_T);
12967 end if;
12968 end;
12970 declare
12971 Actual_Volatile_Comp : constant Boolean :=
12972 Has_Volatile_Components (Act_T)
12973 or else (Is_Array_Type (Act_T)
12974 and then Is_Volatile (Component_Type (Act_T)));
12975 begin
12976 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12977 then
12978 Error_Msg_NE
12979 ("actual for& must have volatile components",
12980 Actual, A_Gen_T);
12981 end if;
12982 end;
12984 -- The following two aspects do not require exact matching,
12985 -- but only one-way agreement. See RM C.6.
12987 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12988 then
12989 Error_Msg_NE
12990 ("actual for& must have Independent aspect specified",
12991 Actual, A_Gen_T);
12992 end if;
12994 if Has_Independent_Components (A_Gen_T)
12995 and then not Has_Independent_Components (Act_T)
12996 then
12997 Error_Msg_NE
12998 ("actual for& must have Independent_Components specified",
12999 Actual, A_Gen_T);
13000 end if;
13001 end if;
13002 end Check_Shared_Variable_Control_Aspects;
13004 ---------------------------------
13005 -- Diagnose_Predicated_Actual --
13006 ---------------------------------
13008 procedure Diagnose_Predicated_Actual is
13009 begin
13010 if No_Predicate_On_Actual (A_Gen_T)
13011 and then Has_Predicates (Act_T)
13012 then
13013 Error_Msg_NE
13014 ("actual for& cannot be a type with predicate",
13015 Instantiation_Node, A_Gen_T);
13017 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13018 and then Has_Predicates (Act_T)
13019 and then not Has_Static_Predicate_Aspect (Act_T)
13020 then
13021 Error_Msg_NE
13022 ("actual for& cannot be a type with a dynamic predicate",
13023 Instantiation_Node, A_Gen_T);
13024 end if;
13025 end Diagnose_Predicated_Actual;
13027 --------------------
13028 -- Subtypes_Match --
13029 --------------------
13031 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13032 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13034 begin
13035 -- Check that the base types, root types (when dealing with class
13036 -- wide types), or designated types (when dealing with anonymous
13037 -- access types) of Gen_T and Act_T are statically matching subtypes.
13039 return ((Base_Type (T) = Act_T
13040 or else Base_Type (T) = Base_Type (Act_T))
13041 and then Subtypes_Statically_Match (T, Act_T))
13043 or else (Is_Class_Wide_Type (Gen_T)
13044 and then Is_Class_Wide_Type (Act_T)
13045 and then Subtypes_Match
13046 (Get_Instance_Of (Root_Type (Gen_T)),
13047 Root_Type (Act_T)))
13049 or else (Is_Anonymous_Access_Type (Gen_T)
13050 and then Ekind (Act_T) = Ekind (Gen_T)
13051 and then Subtypes_Statically_Match
13052 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13053 end Subtypes_Match;
13055 -----------------------------------------
13056 -- Validate_Access_Subprogram_Instance --
13057 -----------------------------------------
13059 procedure Validate_Access_Subprogram_Instance is
13060 begin
13061 if not Is_Access_Type (Act_T)
13062 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13063 then
13064 Error_Msg_NE
13065 ("expect access type in instantiation of &", Actual, Gen_T);
13066 Abandon_Instantiation (Actual);
13067 end if;
13069 -- According to AI05-288, actuals for access_to_subprograms must be
13070 -- subtype conformant with the generic formal. Previous to AI05-288
13071 -- only mode conformance was required.
13073 -- This is a binding interpretation that applies to previous versions
13074 -- of the language, no need to maintain previous weaker checks.
13076 Check_Subtype_Conformant
13077 (Designated_Type (Act_T),
13078 Designated_Type (A_Gen_T),
13079 Actual,
13080 Get_Inst => True);
13082 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13083 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13084 Error_Msg_NE
13085 ("protected access type not allowed for formal &",
13086 Actual, Gen_T);
13087 end if;
13089 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13090 Error_Msg_NE
13091 ("expect protected access type for formal &",
13092 Actual, Gen_T);
13093 end if;
13095 -- If the formal has a specified convention (which in most cases
13096 -- will be StdCall) verify that the actual has the same convention.
13098 if Has_Convention_Pragma (A_Gen_T)
13099 and then Convention (A_Gen_T) /= Convention (Act_T)
13100 then
13101 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13102 Error_Msg_NE
13103 ("actual for formal & must have convention %", Actual, Gen_T);
13104 end if;
13106 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13107 Error_Msg_NE
13108 ("non null exclusion of actual and formal & do not match",
13109 Actual, Gen_T);
13110 end if;
13111 end Validate_Access_Subprogram_Instance;
13113 -----------------------------------
13114 -- Validate_Access_Type_Instance --
13115 -----------------------------------
13117 procedure Validate_Access_Type_Instance is
13118 Desig_Type : constant Entity_Id :=
13119 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13120 Desig_Act : Entity_Id;
13122 begin
13123 if not Is_Access_Type (Act_T) then
13124 Error_Msg_NE
13125 ("expect access type in instantiation of &", Actual, Gen_T);
13126 Abandon_Instantiation (Actual);
13127 end if;
13129 if Is_Access_Constant (A_Gen_T) then
13130 if not Is_Access_Constant (Act_T) then
13131 Error_Msg_N
13132 ("actual type must be access-to-constant type", Actual);
13133 Abandon_Instantiation (Actual);
13134 end if;
13135 else
13136 if Is_Access_Constant (Act_T) then
13137 Error_Msg_N
13138 ("actual type must be access-to-variable type", Actual);
13139 Abandon_Instantiation (Actual);
13141 elsif Ekind (A_Gen_T) = E_General_Access_Type
13142 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13143 then
13144 Error_Msg_N
13145 ("actual must be general access type!", Actual);
13146 Error_Msg_NE -- CODEFIX
13147 ("\add ALL to }!", Actual, Act_T);
13148 Abandon_Instantiation (Actual);
13149 end if;
13150 end if;
13152 -- The designated subtypes, that is to say the subtypes introduced
13153 -- by an access type declaration (and not by a subtype declaration)
13154 -- must match.
13156 Desig_Act := Designated_Type (Base_Type (Act_T));
13158 -- The designated type may have been introduced through a limited_
13159 -- with clause, in which case retrieve the non-limited view. This
13160 -- applies to incomplete types as well as to class-wide types.
13162 if From_Limited_With (Desig_Act) then
13163 Desig_Act := Available_View (Desig_Act);
13164 end if;
13166 if not Subtypes_Match (Desig_Type, Desig_Act) then
13167 Error_Msg_NE
13168 ("designated type of actual does not match that of formal &",
13169 Actual, Gen_T);
13171 if not Predicates_Match (Desig_Type, Desig_Act) then
13172 Error_Msg_N ("\predicates do not match", Actual);
13173 end if;
13175 Abandon_Instantiation (Actual);
13176 end if;
13178 -- Ada 2005: null-exclusion indicators of the two types must agree
13180 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13181 Error_Msg_NE
13182 ("non null exclusion of actual and formal & do not match",
13183 Actual, Gen_T);
13184 end if;
13185 end Validate_Access_Type_Instance;
13187 ----------------------------------
13188 -- Validate_Array_Type_Instance --
13189 ----------------------------------
13191 procedure Validate_Array_Type_Instance is
13192 I1 : Node_Id;
13193 I2 : Node_Id;
13194 T2 : Entity_Id;
13196 function Formal_Dimensions return Nat;
13197 -- Count number of dimensions in array type formal
13199 -----------------------
13200 -- Formal_Dimensions --
13201 -----------------------
13203 function Formal_Dimensions return Nat is
13204 Num : Nat := 0;
13205 Index : Node_Id;
13207 begin
13208 if Nkind (Def) = N_Constrained_Array_Definition then
13209 Index := First (Discrete_Subtype_Definitions (Def));
13210 else
13211 Index := First (Subtype_Marks (Def));
13212 end if;
13214 while Present (Index) loop
13215 Num := Num + 1;
13216 Next (Index);
13217 end loop;
13219 return Num;
13220 end Formal_Dimensions;
13222 -- Start of processing for Validate_Array_Type_Instance
13224 begin
13225 if not Is_Array_Type (Act_T) then
13226 Error_Msg_NE
13227 ("expect array type in instantiation of &", Actual, Gen_T);
13228 Abandon_Instantiation (Actual);
13230 elsif Nkind (Def) = N_Constrained_Array_Definition then
13231 if not (Is_Constrained (Act_T)) then
13232 Error_Msg_NE
13233 ("expect constrained array in instantiation of &",
13234 Actual, Gen_T);
13235 Abandon_Instantiation (Actual);
13236 end if;
13238 else
13239 if Is_Constrained (Act_T) then
13240 Error_Msg_NE
13241 ("expect unconstrained array in instantiation of &",
13242 Actual, Gen_T);
13243 Abandon_Instantiation (Actual);
13244 end if;
13245 end if;
13247 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13248 Error_Msg_NE
13249 ("dimensions of actual do not match formal &", Actual, Gen_T);
13250 Abandon_Instantiation (Actual);
13251 end if;
13253 I1 := First_Index (A_Gen_T);
13254 I2 := First_Index (Act_T);
13255 for J in 1 .. Formal_Dimensions loop
13257 -- If the indexes of the actual were given by a subtype_mark,
13258 -- the index was transformed into a range attribute. Retrieve
13259 -- the original type mark for checking.
13261 if Is_Entity_Name (Original_Node (I2)) then
13262 T2 := Entity (Original_Node (I2));
13263 else
13264 T2 := Etype (I2);
13265 end if;
13267 if not Subtypes_Match
13268 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13269 then
13270 Error_Msg_NE
13271 ("index types of actual do not match those of formal &",
13272 Actual, Gen_T);
13273 Abandon_Instantiation (Actual);
13274 end if;
13276 Next_Index (I1);
13277 Next_Index (I2);
13278 end loop;
13280 -- Check matching subtypes. Note that there are complex visibility
13281 -- issues when the generic is a child unit and some aspect of the
13282 -- generic type is declared in a parent unit of the generic. We do
13283 -- the test to handle this special case only after a direct check
13284 -- for static matching has failed. The case where both the component
13285 -- type and the array type are separate formals, and the component
13286 -- type is a private view may also require special checking in
13287 -- Subtypes_Match. Finally, we assume that a child instance where
13288 -- the component type comes from a formal of a parent instance is
13289 -- correct because the generic was correct. A more precise check
13290 -- seems too complex to install???
13292 if Subtypes_Match
13293 (Component_Type (A_Gen_T), Component_Type (Act_T))
13294 or else
13295 Subtypes_Match
13296 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13297 Component_Type (Act_T))
13298 or else
13299 (not Inside_A_Generic
13300 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13301 then
13302 null;
13303 else
13304 Error_Msg_NE
13305 ("component subtype of actual does not match that of formal &",
13306 Actual, Gen_T);
13307 Abandon_Instantiation (Actual);
13308 end if;
13310 if Has_Aliased_Components (A_Gen_T)
13311 and then not Has_Aliased_Components (Act_T)
13312 then
13313 Error_Msg_NE
13314 ("actual must have aliased components to match formal type &",
13315 Actual, Gen_T);
13316 end if;
13317 end Validate_Array_Type_Instance;
13319 -----------------------------------------------
13320 -- Validate_Derived_Interface_Type_Instance --
13321 -----------------------------------------------
13323 procedure Validate_Derived_Interface_Type_Instance is
13324 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13325 Elmt : Elmt_Id;
13327 begin
13328 -- First apply interface instance checks
13330 Validate_Interface_Type_Instance;
13332 -- Verify that immediate parent interface is an ancestor of
13333 -- the actual.
13335 if Present (Par)
13336 and then not Interface_Present_In_Ancestor (Act_T, Par)
13337 then
13338 Error_Msg_NE
13339 ("interface actual must include progenitor&", Actual, Par);
13340 end if;
13342 -- Now verify that the actual includes all other ancestors of
13343 -- the formal.
13345 Elmt := First_Elmt (Interfaces (A_Gen_T));
13346 while Present (Elmt) loop
13347 if not Interface_Present_In_Ancestor
13348 (Act_T, Get_Instance_Of (Node (Elmt)))
13349 then
13350 Error_Msg_NE
13351 ("interface actual must include progenitor&",
13352 Actual, Node (Elmt));
13353 end if;
13355 Next_Elmt (Elmt);
13356 end loop;
13357 end Validate_Derived_Interface_Type_Instance;
13359 ------------------------------------
13360 -- Validate_Derived_Type_Instance --
13361 ------------------------------------
13363 procedure Validate_Derived_Type_Instance is
13364 Actual_Discr : Entity_Id;
13365 Ancestor_Discr : Entity_Id;
13367 begin
13368 -- Verify that the actual includes the progenitors of the formal,
13369 -- if any. The formal may depend on previous formals and their
13370 -- instance, so we must examine instance of interfaces if present.
13371 -- The actual may be an extension of an interface, in which case
13372 -- it does not appear in the interface list, so this must be
13373 -- checked separately.
13375 if Present (Interface_List (Def)) then
13376 if not Has_Interfaces (Act_T) then
13377 Error_Msg_NE
13378 ("actual must implement all interfaces of formal&",
13379 Actual, A_Gen_T);
13381 else
13382 declare
13383 Act_Iface_List : Elist_Id;
13384 Iface : Node_Id;
13385 Iface_Ent : Entity_Id;
13387 function Instance_Exists (I : Entity_Id) return Boolean;
13388 -- If the interface entity is declared in a generic unit,
13389 -- this can only be legal if we are within an instantiation
13390 -- of a child of that generic. There is currently no
13391 -- mechanism to relate an interface declared within a
13392 -- generic to the corresponding interface in an instance,
13393 -- so we traverse the list of interfaces of the actual,
13394 -- looking for a name match.
13396 ---------------------
13397 -- Instance_Exists --
13398 ---------------------
13400 function Instance_Exists (I : Entity_Id) return Boolean is
13401 Iface_Elmt : Elmt_Id;
13403 begin
13404 Iface_Elmt := First_Elmt (Act_Iface_List);
13405 while Present (Iface_Elmt) loop
13406 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13407 and then Chars (Node (Iface_Elmt)) = Chars (I)
13408 then
13409 return True;
13410 end if;
13412 Next_Elmt (Iface_Elmt);
13413 end loop;
13415 return False;
13416 end Instance_Exists;
13418 begin
13419 Iface := First (Abstract_Interface_List (A_Gen_T));
13420 Collect_Interfaces (Act_T, Act_Iface_List);
13422 while Present (Iface) loop
13423 Iface_Ent := Get_Instance_Of (Entity (Iface));
13425 if Is_Ancestor (Iface_Ent, Act_T)
13426 or else Is_Progenitor (Iface_Ent, Act_T)
13427 then
13428 null;
13430 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13431 and then Instance_Exists (Iface_Ent)
13432 then
13433 null;
13435 else
13436 Error_Msg_Name_1 := Chars (Act_T);
13437 Error_Msg_NE
13438 ("actual% must implement interface&",
13439 Actual, Etype (Iface));
13440 end if;
13442 Next (Iface);
13443 end loop;
13444 end;
13445 end if;
13446 end if;
13448 -- If the parent type in the generic declaration is itself a previous
13449 -- formal type, then it is local to the generic and absent from the
13450 -- analyzed generic definition. In that case the ancestor is the
13451 -- instance of the formal (which must have been instantiated
13452 -- previously), unless the ancestor is itself a formal derived type.
13453 -- In this latter case (which is the subject of Corrigendum 8652/0038
13454 -- (AI-202) the ancestor of the formals is the ancestor of its
13455 -- parent. Otherwise, the analyzed generic carries the parent type.
13456 -- If the parent type is defined in a previous formal package, then
13457 -- the scope of that formal package is that of the generic type
13458 -- itself, and it has already been mapped into the corresponding type
13459 -- in the actual package.
13461 -- Common case: parent type defined outside of the generic
13463 if Is_Entity_Name (Subtype_Mark (Def))
13464 and then Present (Entity (Subtype_Mark (Def)))
13465 then
13466 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13468 -- Check whether parent is defined in a previous formal package
13470 elsif
13471 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13472 then
13473 Ancestor :=
13474 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13476 -- The type may be a local derivation, or a type extension of a
13477 -- previous formal, or of a formal of a parent package.
13479 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13480 or else
13481 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13482 then
13483 -- Check whether the parent is another derived formal type in the
13484 -- same generic unit.
13486 if Etype (A_Gen_T) /= A_Gen_T
13487 and then Is_Generic_Type (Etype (A_Gen_T))
13488 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13489 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13490 then
13491 -- Locate ancestor of parent from the subtype declaration
13492 -- created for the actual.
13494 declare
13495 Decl : Node_Id;
13497 begin
13498 Decl := First (Actual_Decls);
13499 while Present (Decl) loop
13500 if Nkind (Decl) = N_Subtype_Declaration
13501 and then Chars (Defining_Identifier (Decl)) =
13502 Chars (Etype (A_Gen_T))
13503 then
13504 Ancestor := Generic_Parent_Type (Decl);
13505 exit;
13506 else
13507 Next (Decl);
13508 end if;
13509 end loop;
13510 end;
13512 pragma Assert (Present (Ancestor));
13514 -- The ancestor itself may be a previous formal that has been
13515 -- instantiated.
13517 Ancestor := Get_Instance_Of (Ancestor);
13519 else
13520 Ancestor := Get_Instance_Of (Etype (Get_Instance_Of (A_Gen_T)));
13521 end if;
13523 -- Check whether parent is a previous formal of the current generic
13525 elsif Is_Derived_Type (A_Gen_T)
13526 and then Is_Generic_Type (Etype (A_Gen_T))
13527 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13528 then
13529 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13531 -- An unusual case: the actual is a type declared in a parent unit,
13532 -- but is not a formal type so there is no instance_of for it.
13533 -- Retrieve it by analyzing the record extension.
13535 elsif Is_Child_Unit (Scope (A_Gen_T))
13536 and then In_Open_Scopes (Scope (Act_T))
13537 and then Is_Generic_Instance (Scope (Act_T))
13538 then
13539 Analyze (Subtype_Mark (Def));
13540 Ancestor := Entity (Subtype_Mark (Def));
13542 else
13543 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13544 end if;
13546 -- If the formal derived type has pragma Preelaborable_Initialization
13547 -- then the actual type must have preelaborable initialization.
13549 if Known_To_Have_Preelab_Init (A_Gen_T)
13550 and then not Has_Preelaborable_Initialization (Act_T)
13551 then
13552 Error_Msg_NE
13553 ("actual for & must have preelaborable initialization",
13554 Actual, Gen_T);
13555 end if;
13557 -- Ada 2005 (AI-251)
13559 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13560 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13561 Error_Msg_NE
13562 ("(Ada 2005) expected type implementing & in instantiation",
13563 Actual, Ancestor);
13564 end if;
13566 -- Finally verify that the (instance of) the ancestor is an ancestor
13567 -- of the actual.
13569 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13570 Error_Msg_NE
13571 ("expect type derived from & in instantiation",
13572 Actual, First_Subtype (Ancestor));
13573 Abandon_Instantiation (Actual);
13574 end if;
13576 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13577 -- that the formal type declaration has been rewritten as a private
13578 -- extension.
13580 if Ada_Version >= Ada_2005
13581 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13582 and then Synchronized_Present (Parent (A_Gen_T))
13583 then
13584 -- The actual must be a synchronized tagged type
13586 if not Is_Tagged_Type (Act_T) then
13587 Error_Msg_N
13588 ("actual of synchronized type must be tagged", Actual);
13589 Abandon_Instantiation (Actual);
13591 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13592 and then Nkind (Type_Definition (Parent (Act_T))) =
13593 N_Derived_Type_Definition
13594 and then not Synchronized_Present
13595 (Type_Definition (Parent (Act_T)))
13596 then
13597 Error_Msg_N
13598 ("actual of synchronized type must be synchronized", Actual);
13599 Abandon_Instantiation (Actual);
13600 end if;
13601 end if;
13603 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13604 -- removes the second instance of the phrase "or allow pass by copy".
13606 -- For Ada 2022, the aspect may be specified explicitly for the
13607 -- formal regardless of whether an ancestor obeys it.
13609 if Is_Atomic (Act_T)
13610 and then not Is_Atomic (Ancestor)
13611 and then not Is_Atomic (A_Gen_T)
13612 then
13613 Error_Msg_N
13614 ("cannot have atomic actual type for non-atomic formal type",
13615 Actual);
13617 elsif Is_Volatile (Act_T)
13618 and then not Is_Volatile (Ancestor)
13619 and then not Is_Volatile (A_Gen_T)
13620 then
13621 Error_Msg_N
13622 ("cannot have volatile actual type for non-volatile formal type",
13623 Actual);
13624 end if;
13626 -- It should not be necessary to check for unknown discriminants on
13627 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13628 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13629 -- needs fixing. ???
13631 if Is_Definite_Subtype (A_Gen_T)
13632 and then not Unknown_Discriminants_Present (Formal)
13633 and then not Is_Definite_Subtype (Act_T)
13634 then
13635 Error_Msg_N ("actual subtype must be constrained", Actual);
13636 Abandon_Instantiation (Actual);
13637 end if;
13639 if not Unknown_Discriminants_Present (Formal) then
13640 if Is_Constrained (Ancestor) then
13641 if not Is_Constrained (Act_T) then
13642 Error_Msg_N ("actual subtype must be constrained", Actual);
13643 Abandon_Instantiation (Actual);
13644 end if;
13646 -- Ancestor is unconstrained, Check if generic formal and actual
13647 -- agree on constrainedness. The check only applies to array types
13648 -- and discriminated types.
13650 elsif Is_Constrained (Act_T) then
13651 if Ekind (Ancestor) = E_Access_Type
13652 or else (not Is_Constrained (A_Gen_T)
13653 and then Is_Composite_Type (A_Gen_T))
13654 then
13655 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13656 Abandon_Instantiation (Actual);
13657 end if;
13659 -- A class-wide type is only allowed if the formal has unknown
13660 -- discriminants.
13662 elsif Is_Class_Wide_Type (Act_T)
13663 and then not Has_Unknown_Discriminants (Ancestor)
13664 then
13665 Error_Msg_NE
13666 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13667 Abandon_Instantiation (Actual);
13669 -- Otherwise, the formal and actual must have the same number
13670 -- of discriminants and each discriminant of the actual must
13671 -- correspond to a discriminant of the formal.
13673 elsif Has_Discriminants (Act_T)
13674 and then not Has_Unknown_Discriminants (Act_T)
13675 and then Has_Discriminants (Ancestor)
13676 then
13677 Actual_Discr := First_Discriminant (Act_T);
13678 Ancestor_Discr := First_Discriminant (Ancestor);
13679 while Present (Actual_Discr)
13680 and then Present (Ancestor_Discr)
13681 loop
13682 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13683 No (Corresponding_Discriminant (Actual_Discr))
13684 then
13685 Error_Msg_NE
13686 ("discriminant & does not correspond "
13687 & "to ancestor discriminant", Actual, Actual_Discr);
13688 Abandon_Instantiation (Actual);
13689 end if;
13691 Next_Discriminant (Actual_Discr);
13692 Next_Discriminant (Ancestor_Discr);
13693 end loop;
13695 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13696 Error_Msg_NE
13697 ("actual for & must have same number of discriminants",
13698 Actual, Gen_T);
13699 Abandon_Instantiation (Actual);
13700 end if;
13702 -- This case should be caught by the earlier check for
13703 -- constrainedness, but the check here is added for completeness.
13705 elsif Has_Discriminants (Act_T)
13706 and then not Has_Unknown_Discriminants (Act_T)
13707 then
13708 Error_Msg_NE
13709 ("actual for & must not have discriminants", Actual, Gen_T);
13710 Abandon_Instantiation (Actual);
13712 elsif Has_Discriminants (Ancestor) then
13713 Error_Msg_NE
13714 ("actual for & must have known discriminants", Actual, Gen_T);
13715 Abandon_Instantiation (Actual);
13716 end if;
13718 if not Subtypes_Statically_Compatible
13719 (Act_T, Ancestor, Formal_Derived_Matching => True)
13720 then
13721 Error_Msg_NE
13722 ("actual for & must be statically compatible with ancestor",
13723 Actual, Gen_T);
13725 if not Predicates_Compatible (Act_T, Ancestor) then
13726 Error_Msg_N
13727 ("\predicate on actual is not compatible with ancestor",
13728 Actual);
13729 end if;
13731 Abandon_Instantiation (Actual);
13732 end if;
13733 end if;
13735 -- If the formal and actual types are abstract, check that there
13736 -- are no abstract primitives of the actual type that correspond to
13737 -- nonabstract primitives of the formal type (second sentence of
13738 -- RM95 3.9.3(9)).
13740 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13741 Check_Abstract_Primitives : declare
13742 Gen_Prims : constant Elist_Id :=
13743 Primitive_Operations (A_Gen_T);
13744 Gen_Elmt : Elmt_Id;
13745 Gen_Subp : Entity_Id;
13746 Anc_Subp : Entity_Id;
13747 Anc_Formal : Entity_Id;
13748 Anc_F_Type : Entity_Id;
13750 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13751 Act_Elmt : Elmt_Id;
13752 Act_Subp : Entity_Id;
13753 Act_Formal : Entity_Id;
13754 Act_F_Type : Entity_Id;
13756 Subprograms_Correspond : Boolean;
13758 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13759 -- Returns true if T2 is derived directly or indirectly from
13760 -- T1, including derivations from interfaces. T1 and T2 are
13761 -- required to be specific tagged base types.
13763 ------------------------
13764 -- Is_Tagged_Ancestor --
13765 ------------------------
13767 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13769 Intfc_Elmt : Elmt_Id;
13771 begin
13772 -- The predicate is satisfied if the types are the same
13774 if T1 = T2 then
13775 return True;
13777 -- If we've reached the top of the derivation chain then
13778 -- we know that T1 is not an ancestor of T2.
13780 elsif Etype (T2) = T2 then
13781 return False;
13783 -- Proceed to check T2's immediate parent
13785 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13786 return True;
13788 -- Finally, check to see if T1 is an ancestor of any of T2's
13789 -- progenitors.
13791 else
13792 Intfc_Elmt := First_Elmt (Interfaces (T2));
13793 while Present (Intfc_Elmt) loop
13794 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13795 return True;
13796 end if;
13798 Next_Elmt (Intfc_Elmt);
13799 end loop;
13800 end if;
13802 return False;
13803 end Is_Tagged_Ancestor;
13805 -- Start of processing for Check_Abstract_Primitives
13807 begin
13808 -- Loop over all of the formal derived type's primitives
13810 Gen_Elmt := First_Elmt (Gen_Prims);
13811 while Present (Gen_Elmt) loop
13812 Gen_Subp := Node (Gen_Elmt);
13814 -- If the primitive of the formal is not abstract, then
13815 -- determine whether there is a corresponding primitive of
13816 -- the actual type that's abstract.
13818 if not Is_Abstract_Subprogram (Gen_Subp) then
13819 Act_Elmt := First_Elmt (Act_Prims);
13820 while Present (Act_Elmt) loop
13821 Act_Subp := Node (Act_Elmt);
13823 -- If we find an abstract primitive of the actual,
13824 -- then we need to test whether it corresponds to the
13825 -- subprogram from which the generic formal primitive
13826 -- is inherited.
13828 if Is_Abstract_Subprogram (Act_Subp) then
13829 Anc_Subp := Alias (Gen_Subp);
13831 -- Test whether we have a corresponding primitive
13832 -- by comparing names, kinds, formal types, and
13833 -- result types.
13835 if Chars (Anc_Subp) = Chars (Act_Subp)
13836 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13837 then
13838 Anc_Formal := First_Formal (Anc_Subp);
13839 Act_Formal := First_Formal (Act_Subp);
13840 while Present (Anc_Formal)
13841 and then Present (Act_Formal)
13842 loop
13843 Anc_F_Type := Etype (Anc_Formal);
13844 Act_F_Type := Etype (Act_Formal);
13846 if Ekind (Anc_F_Type) =
13847 E_Anonymous_Access_Type
13848 then
13849 Anc_F_Type := Designated_Type (Anc_F_Type);
13851 if Ekind (Act_F_Type) =
13852 E_Anonymous_Access_Type
13853 then
13854 Act_F_Type :=
13855 Designated_Type (Act_F_Type);
13856 else
13857 exit;
13858 end if;
13860 elsif
13861 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13862 then
13863 exit;
13864 end if;
13866 Anc_F_Type := Base_Type (Anc_F_Type);
13867 Act_F_Type := Base_Type (Act_F_Type);
13869 -- If the formal is controlling, then the
13870 -- the type of the actual primitive's formal
13871 -- must be derived directly or indirectly
13872 -- from the type of the ancestor primitive's
13873 -- formal.
13875 if Is_Controlling_Formal (Anc_Formal) then
13876 if not Is_Tagged_Ancestor
13877 (Anc_F_Type, Act_F_Type)
13878 then
13879 exit;
13880 end if;
13882 -- Otherwise the types of the formals must
13883 -- be the same.
13885 elsif Anc_F_Type /= Act_F_Type then
13886 exit;
13887 end if;
13889 Next_Formal (Anc_Formal);
13890 Next_Formal (Act_Formal);
13891 end loop;
13893 -- If we traversed through all of the formals
13894 -- then so far the subprograms correspond, so
13895 -- now check that any result types correspond.
13897 if No (Anc_Formal) and then No (Act_Formal) then
13898 Subprograms_Correspond := True;
13900 if Ekind (Act_Subp) = E_Function then
13901 Anc_F_Type := Etype (Anc_Subp);
13902 Act_F_Type := Etype (Act_Subp);
13904 if Ekind (Anc_F_Type) =
13905 E_Anonymous_Access_Type
13906 then
13907 Anc_F_Type :=
13908 Designated_Type (Anc_F_Type);
13910 if Ekind (Act_F_Type) =
13911 E_Anonymous_Access_Type
13912 then
13913 Act_F_Type :=
13914 Designated_Type (Act_F_Type);
13915 else
13916 Subprograms_Correspond := False;
13917 end if;
13919 elsif
13920 Ekind (Act_F_Type)
13921 = E_Anonymous_Access_Type
13922 then
13923 Subprograms_Correspond := False;
13924 end if;
13926 Anc_F_Type := Base_Type (Anc_F_Type);
13927 Act_F_Type := Base_Type (Act_F_Type);
13929 -- Now either the result types must be
13930 -- the same or, if the result type is
13931 -- controlling, the result type of the
13932 -- actual primitive must descend from the
13933 -- result type of the ancestor primitive.
13935 if Subprograms_Correspond
13936 and then Anc_F_Type /= Act_F_Type
13937 and then
13938 Has_Controlling_Result (Anc_Subp)
13939 and then not Is_Tagged_Ancestor
13940 (Anc_F_Type, Act_F_Type)
13941 then
13942 Subprograms_Correspond := False;
13943 end if;
13944 end if;
13946 -- Found a matching subprogram belonging to
13947 -- formal ancestor type, so actual subprogram
13948 -- corresponds and this violates 3.9.3(9).
13950 if Subprograms_Correspond then
13951 Error_Msg_NE
13952 ("abstract subprogram & overrides "
13953 & "nonabstract subprogram of ancestor",
13954 Actual, Act_Subp);
13955 end if;
13956 end if;
13957 end if;
13958 end if;
13960 Next_Elmt (Act_Elmt);
13961 end loop;
13962 end if;
13964 Next_Elmt (Gen_Elmt);
13965 end loop;
13966 end Check_Abstract_Primitives;
13967 end if;
13969 -- Verify that limitedness matches. If parent is a limited
13970 -- interface then the generic formal is not unless declared
13971 -- explicitly so. If not declared limited, the actual cannot be
13972 -- limited (see AI05-0087).
13974 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13975 if not In_Instance then
13976 Error_Msg_NE
13977 ("actual for non-limited & cannot be a limited type",
13978 Actual, Gen_T);
13979 Explain_Limited_Type (Act_T, Actual);
13980 Abandon_Instantiation (Actual);
13981 end if;
13982 end if;
13984 -- Check for AI12-0036
13986 declare
13987 Formal_Is_Private_Extension : constant Boolean :=
13988 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13990 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13992 begin
13993 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13994 if not In_Instance then
13995 if Actual_Is_Tagged then
13996 Error_Msg_NE
13997 ("actual for & cannot be a tagged type", Actual, Gen_T);
13998 else
13999 Error_Msg_NE
14000 ("actual for & must be a tagged type", Actual, Gen_T);
14001 end if;
14003 Abandon_Instantiation (Actual);
14004 end if;
14005 end if;
14006 end;
14007 end Validate_Derived_Type_Instance;
14009 ----------------------------------------
14010 -- Validate_Discriminated_Formal_Type --
14011 ----------------------------------------
14013 procedure Validate_Discriminated_Formal_Type is
14014 Formal_Discr : Entity_Id;
14015 Actual_Discr : Entity_Id;
14016 Formal_Subt : Entity_Id;
14018 begin
14019 if Has_Discriminants (A_Gen_T) then
14020 if not Has_Discriminants (Act_T) then
14021 Error_Msg_NE
14022 ("actual for & must have discriminants", Actual, Gen_T);
14023 Abandon_Instantiation (Actual);
14025 elsif Is_Constrained (Act_T) then
14026 Error_Msg_NE
14027 ("actual for & must be unconstrained", Actual, Gen_T);
14028 Abandon_Instantiation (Actual);
14030 else
14031 Formal_Discr := First_Discriminant (A_Gen_T);
14032 Actual_Discr := First_Discriminant (Act_T);
14033 while Formal_Discr /= Empty loop
14034 if Actual_Discr = Empty then
14035 Error_Msg_N
14036 ("discriminants on actual do not match formal",
14037 Actual);
14038 Abandon_Instantiation (Actual);
14039 end if;
14041 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14043 -- Access discriminants match if designated types do
14045 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14046 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14047 E_Anonymous_Access_Type
14048 and then
14049 Subtypes_Match
14050 (Get_Instance_Of
14051 (Designated_Type (Base_Type (Formal_Subt))),
14052 Designated_Type (Base_Type (Etype (Actual_Discr))))
14053 then
14054 null;
14056 elsif Base_Type (Formal_Subt) /=
14057 Base_Type (Etype (Actual_Discr))
14058 then
14059 Error_Msg_N
14060 ("types of actual discriminants must match formal",
14061 Actual);
14062 Abandon_Instantiation (Actual);
14064 elsif not Subtypes_Statically_Match
14065 (Formal_Subt, Etype (Actual_Discr))
14066 and then Ada_Version >= Ada_95
14067 then
14068 Error_Msg_N
14069 ("subtypes of actual discriminants must match formal",
14070 Actual);
14071 Abandon_Instantiation (Actual);
14072 end if;
14074 Next_Discriminant (Formal_Discr);
14075 Next_Discriminant (Actual_Discr);
14076 end loop;
14078 if Actual_Discr /= Empty then
14079 Error_Msg_NE
14080 ("discriminants on actual do not match formal",
14081 Actual, Gen_T);
14082 Abandon_Instantiation (Actual);
14083 end if;
14084 end if;
14085 end if;
14086 end Validate_Discriminated_Formal_Type;
14088 ---------------------------------------
14089 -- Validate_Incomplete_Type_Instance --
14090 ---------------------------------------
14092 procedure Validate_Incomplete_Type_Instance is
14093 begin
14094 if not Is_Tagged_Type (Act_T)
14095 and then Is_Tagged_Type (A_Gen_T)
14096 then
14097 Error_Msg_NE
14098 ("actual for & must be a tagged type", Actual, Gen_T);
14099 end if;
14101 Validate_Discriminated_Formal_Type;
14102 end Validate_Incomplete_Type_Instance;
14104 --------------------------------------
14105 -- Validate_Interface_Type_Instance --
14106 --------------------------------------
14108 procedure Validate_Interface_Type_Instance is
14109 begin
14110 if not Is_Interface (Act_T) then
14111 Error_Msg_NE
14112 ("actual for formal interface type must be an interface",
14113 Actual, Gen_T);
14115 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14116 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14117 or else Is_Protected_Interface (A_Gen_T) /=
14118 Is_Protected_Interface (Act_T)
14119 or else Is_Synchronized_Interface (A_Gen_T) /=
14120 Is_Synchronized_Interface (Act_T)
14121 then
14122 Error_Msg_NE
14123 ("actual for interface& does not match (RM 12.5.5(4))",
14124 Actual, Gen_T);
14125 end if;
14126 end Validate_Interface_Type_Instance;
14128 ------------------------------------
14129 -- Validate_Private_Type_Instance --
14130 ------------------------------------
14132 procedure Validate_Private_Type_Instance is
14133 begin
14134 if Is_Limited_Type (Act_T)
14135 and then not Is_Limited_Type (A_Gen_T)
14136 then
14137 if In_Instance then
14138 null;
14139 else
14140 Error_Msg_NE
14141 ("actual for non-limited & cannot be a limited type", Actual,
14142 Gen_T);
14143 Explain_Limited_Type (Act_T, Actual);
14144 Abandon_Instantiation (Actual);
14145 end if;
14147 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14148 and then not Has_Preelaborable_Initialization (Act_T)
14149 then
14150 Error_Msg_NE
14151 ("actual for & must have preelaborable initialization", Actual,
14152 Gen_T);
14154 elsif not Is_Definite_Subtype (Act_T)
14155 and then Is_Definite_Subtype (A_Gen_T)
14156 and then Ada_Version >= Ada_95
14157 then
14158 Error_Msg_NE
14159 ("actual for & must be a definite subtype", Actual, Gen_T);
14161 elsif not Is_Tagged_Type (Act_T)
14162 and then Is_Tagged_Type (A_Gen_T)
14163 then
14164 Error_Msg_NE
14165 ("actual for & must be a tagged type", Actual, Gen_T);
14166 end if;
14168 Validate_Discriminated_Formal_Type;
14169 Ancestor := Gen_T;
14170 end Validate_Private_Type_Instance;
14172 -- Start of processing for Instantiate_Type
14174 begin
14175 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14176 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14177 return New_List (Error);
14178 end if;
14180 if not Is_Entity_Name (Actual)
14181 or else not Is_Type (Entity (Actual))
14182 then
14183 Error_Msg_NE
14184 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14185 Abandon_Instantiation (Actual);
14186 end if;
14188 Act_T := Entity (Actual);
14190 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14191 -- as a generic actual parameter if the corresponding formal type
14192 -- does not have a known_discriminant_part, or is a formal derived
14193 -- type that is an Unchecked_Union type.
14195 if Is_Unchecked_Union (Base_Type (Act_T)) then
14196 if not Has_Discriminants (A_Gen_T)
14197 or else (Is_Derived_Type (A_Gen_T)
14198 and then Is_Unchecked_Union (A_Gen_T))
14199 then
14200 null;
14201 else
14202 Error_Msg_N ("unchecked union cannot be the actual for a "
14203 & "discriminated formal type", Act_T);
14205 end if;
14206 end if;
14208 -- Deal with fixed/floating restrictions
14210 if Is_Floating_Point_Type (Act_T) then
14211 Check_Restriction (No_Floating_Point, Actual);
14212 elsif Is_Fixed_Point_Type (Act_T) then
14213 Check_Restriction (No_Fixed_Point, Actual);
14214 end if;
14216 -- Deal with error of using incomplete type as generic actual.
14217 -- This includes limited views of a type, even if the non-limited
14218 -- view may be available.
14220 if Ekind (Act_T) = E_Incomplete_Type
14221 or else (Is_Class_Wide_Type (Act_T)
14222 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14223 then
14224 -- If the formal is an incomplete type, the actual can be
14225 -- incomplete as well, but if an actual incomplete type has
14226 -- a full view, then we'll retrieve that.
14228 if Ekind (A_Gen_T) = E_Incomplete_Type
14229 and then No (Full_View (Act_T))
14230 then
14231 null;
14233 elsif Is_Class_Wide_Type (Act_T)
14234 or else No (Full_View (Act_T))
14235 then
14236 Error_Msg_N ("premature use of incomplete type", Actual);
14237 Abandon_Instantiation (Actual);
14239 else
14240 Act_T := Full_View (Act_T);
14241 Set_Entity (Actual, Act_T);
14243 if Has_Private_Component (Act_T) then
14244 Error_Msg_N
14245 ("premature use of type with private component", Actual);
14246 end if;
14247 end if;
14249 -- Deal with error of premature use of private type as generic actual,
14250 -- which is allowed for incomplete formals.
14252 elsif Ekind (A_Gen_T) /= E_Incomplete_Type then
14253 if Is_Private_Type (Act_T)
14254 and then Is_Private_Type (Base_Type (Act_T))
14255 and then not Is_Generic_Type (Act_T)
14256 and then not Is_Derived_Type (Act_T)
14257 and then No (Full_View (Root_Type (Act_T)))
14258 then
14259 Error_Msg_N ("premature use of private type", Actual);
14261 elsif Has_Private_Component (Act_T) then
14262 Error_Msg_N
14263 ("premature use of type with private component", Actual);
14264 end if;
14265 end if;
14267 Set_Instance_Of (A_Gen_T, Act_T);
14269 -- If the type is generic, the class-wide type may also be used
14271 if Is_Tagged_Type (A_Gen_T)
14272 and then Is_Tagged_Type (Act_T)
14273 and then not Is_Class_Wide_Type (A_Gen_T)
14274 then
14275 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14276 Class_Wide_Type (Act_T));
14277 end if;
14279 if not Is_Abstract_Type (A_Gen_T)
14280 and then Is_Abstract_Type (Act_T)
14281 then
14282 Error_Msg_N
14283 ("actual of non-abstract formal cannot be abstract", Actual);
14284 end if;
14286 -- A generic scalar type is a first subtype for which we generate
14287 -- an anonymous base type. Indicate that the instance of this base
14288 -- is the base type of the actual.
14290 if Is_Scalar_Type (A_Gen_T) then
14291 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14292 end if;
14294 Check_Shared_Variable_Control_Aspects;
14296 if Error_Posted (Act_T) then
14297 null;
14298 else
14299 case Nkind (Def) is
14300 when N_Formal_Private_Type_Definition =>
14301 Validate_Private_Type_Instance;
14303 when N_Formal_Incomplete_Type_Definition =>
14304 Validate_Incomplete_Type_Instance;
14306 when N_Formal_Derived_Type_Definition =>
14307 Validate_Derived_Type_Instance;
14309 when N_Formal_Discrete_Type_Definition =>
14310 if not Is_Discrete_Type (Act_T) then
14311 Error_Msg_NE
14312 ("expect discrete type in instantiation of&",
14313 Actual, Gen_T);
14314 Abandon_Instantiation (Actual);
14315 end if;
14317 Diagnose_Predicated_Actual;
14319 when N_Formal_Signed_Integer_Type_Definition =>
14320 if not Is_Signed_Integer_Type (Act_T) then
14321 Error_Msg_NE
14322 ("expect signed integer type in instantiation of&",
14323 Actual, Gen_T);
14324 Abandon_Instantiation (Actual);
14325 end if;
14327 Diagnose_Predicated_Actual;
14329 when N_Formal_Modular_Type_Definition =>
14330 if not Is_Modular_Integer_Type (Act_T) then
14331 Error_Msg_NE
14332 ("expect modular type in instantiation of &",
14333 Actual, Gen_T);
14334 Abandon_Instantiation (Actual);
14335 end if;
14337 Diagnose_Predicated_Actual;
14339 when N_Formal_Floating_Point_Definition =>
14340 if not Is_Floating_Point_Type (Act_T) then
14341 Error_Msg_NE
14342 ("expect float type in instantiation of &", Actual, Gen_T);
14343 Abandon_Instantiation (Actual);
14344 end if;
14346 when N_Formal_Ordinary_Fixed_Point_Definition =>
14347 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14348 Error_Msg_NE
14349 ("expect ordinary fixed point type in instantiation of &",
14350 Actual, Gen_T);
14351 Abandon_Instantiation (Actual);
14352 end if;
14354 when N_Formal_Decimal_Fixed_Point_Definition =>
14355 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14356 Error_Msg_NE
14357 ("expect decimal type in instantiation of &",
14358 Actual, Gen_T);
14359 Abandon_Instantiation (Actual);
14360 end if;
14362 when N_Array_Type_Definition =>
14363 Validate_Array_Type_Instance;
14365 when N_Access_To_Object_Definition =>
14366 Validate_Access_Type_Instance;
14368 when N_Access_Function_Definition
14369 | N_Access_Procedure_Definition
14371 Validate_Access_Subprogram_Instance;
14373 when N_Record_Definition =>
14374 Validate_Interface_Type_Instance;
14376 when N_Derived_Type_Definition =>
14377 Validate_Derived_Interface_Type_Instance;
14379 when others =>
14380 raise Program_Error;
14381 end case;
14382 end if;
14384 Subt := New_Copy (Gen_T);
14386 -- Use adjusted sloc of subtype name as the location for other nodes in
14387 -- the subtype declaration.
14389 Loc := Sloc (Subt);
14391 Decl_Node :=
14392 Make_Subtype_Declaration (Loc,
14393 Defining_Identifier => Subt,
14394 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14396 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14398 -- Record whether the actual is private at this point, so that
14399 -- Check_Generic_Actuals can restore its proper view before the
14400 -- semantic analysis of the instance.
14402 if Is_Private_Type (Act_T) then
14403 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14405 elsif (Is_Access_Type (Act_T)
14406 and then Is_Private_Type (Designated_Type (Act_T)))
14407 or else (Is_Array_Type (Act_T)
14408 and then
14409 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
14410 then
14411 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
14412 end if;
14414 -- In Ada 2012 the actual may be a limited view. Indicate that
14415 -- the local subtype must be treated as such.
14417 if From_Limited_With (Act_T) then
14418 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14419 Set_From_Limited_With (Subt);
14420 end if;
14422 Decl_Nodes := New_List (Decl_Node);
14424 -- Flag actual derived types so their elaboration produces the
14425 -- appropriate renamings for the primitive operations of the ancestor.
14426 -- Flag actual for formal private types as well, to determine whether
14427 -- operations in the private part may override inherited operations.
14428 -- If the formal has an interface list, the ancestor is not the
14429 -- parent, but the analyzed formal that includes the interface
14430 -- operations of all its progenitors.
14432 -- Same treatment for formal private types, so we can check whether the
14433 -- type is tagged limited when validating derivations in the private
14434 -- part. (See AI05-096).
14436 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14437 if Present (Interface_List (Def)) then
14438 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14439 else
14440 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14441 end if;
14443 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14444 | N_Formal_Incomplete_Type_Definition
14445 then
14446 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14447 end if;
14449 -- If the actual is a synchronized type that implements an interface,
14450 -- the primitive operations are attached to the corresponding record,
14451 -- and we have to treat it as an additional generic actual, so that its
14452 -- primitive operations become visible in the instance. The task or
14453 -- protected type itself does not carry primitive operations.
14455 if Is_Concurrent_Type (Act_T)
14456 and then Is_Tagged_Type (Act_T)
14457 and then Present (Corresponding_Record_Type (Act_T))
14458 and then Present (Ancestor)
14459 and then Is_Interface (Ancestor)
14460 then
14461 declare
14462 Corr_Rec : constant Entity_Id :=
14463 Corresponding_Record_Type (Act_T);
14464 New_Corr : Entity_Id;
14465 Corr_Decl : Node_Id;
14467 begin
14468 New_Corr := Make_Temporary (Loc, 'S');
14469 Corr_Decl :=
14470 Make_Subtype_Declaration (Loc,
14471 Defining_Identifier => New_Corr,
14472 Subtype_Indication =>
14473 New_Occurrence_Of (Corr_Rec, Loc));
14474 Append_To (Decl_Nodes, Corr_Decl);
14476 if Ekind (Act_T) = E_Task_Type then
14477 Mutate_Ekind (Subt, E_Task_Subtype);
14478 else
14479 Mutate_Ekind (Subt, E_Protected_Subtype);
14480 end if;
14482 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14483 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14484 Set_Generic_Parent_Type (Decl_Node, Empty);
14485 end;
14486 end if;
14488 -- For a floating-point type, capture dimension info if any, because
14489 -- the generated subtype declaration does not come from source and
14490 -- will not process dimensions.
14492 if Is_Floating_Point_Type (Act_T) then
14493 Copy_Dimensions (Act_T, Subt);
14494 end if;
14496 return Decl_Nodes;
14497 end Instantiate_Type;
14499 -----------------------------
14500 -- Is_Abbreviated_Instance --
14501 -----------------------------
14503 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14504 begin
14505 return Ekind (E) = E_Package
14506 and then Present (Hidden_In_Formal_Instance (E));
14507 end Is_Abbreviated_Instance;
14509 ---------------------
14510 -- Is_In_Main_Unit --
14511 ---------------------
14513 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14514 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14515 Current_Unit : Node_Id;
14517 begin
14518 if Unum = Main_Unit then
14519 return True;
14521 -- If the current unit is a subunit then it is either the main unit or
14522 -- is being compiled as part of the main unit.
14524 elsif Nkind (N) = N_Compilation_Unit then
14525 return Nkind (Unit (N)) = N_Subunit;
14526 end if;
14528 Current_Unit := Parent (N);
14529 while Present (Current_Unit)
14530 and then Nkind (Current_Unit) /= N_Compilation_Unit
14531 loop
14532 Current_Unit := Parent (Current_Unit);
14533 end loop;
14535 -- The instantiation node is in the main unit, or else the current node
14536 -- (perhaps as the result of nested instantiations) is in the main unit,
14537 -- or in the declaration of the main unit, which in this last case must
14538 -- be a body.
14540 return
14541 Current_Unit = Cunit (Main_Unit)
14542 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14543 or else (Present (Current_Unit)
14544 and then Present (Library_Unit (Current_Unit))
14545 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14546 end Is_In_Main_Unit;
14548 ----------------------------
14549 -- Load_Parent_Of_Generic --
14550 ----------------------------
14552 procedure Load_Parent_Of_Generic
14553 (N : Node_Id;
14554 Spec : Node_Id;
14555 Body_Optional : Boolean := False)
14557 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14558 Saved_Style_Check : constant Boolean := Style_Check;
14559 Saved_Warn : constant Warnings_State := Save_Warnings;
14560 True_Parent : Node_Id;
14561 Inst_Node : Node_Id;
14562 OK : Boolean;
14563 Previous_Instances : constant Elist_Id := New_Elmt_List;
14565 procedure Collect_Previous_Instances (Decls : List_Id);
14566 -- Collect all instantiations in the given list of declarations, that
14567 -- precede the generic that we need to load. If the bodies of these
14568 -- instantiations are available, we must analyze them, to ensure that
14569 -- the public symbols generated are the same when the unit is compiled
14570 -- to generate code, and when it is compiled in the context of a unit
14571 -- that needs a particular nested instance. This process is applied to
14572 -- both package and subprogram instances.
14574 --------------------------------
14575 -- Collect_Previous_Instances --
14576 --------------------------------
14578 procedure Collect_Previous_Instances (Decls : List_Id) is
14579 Decl : Node_Id;
14581 begin
14582 Decl := First (Decls);
14583 while Present (Decl) loop
14584 if Sloc (Decl) >= Sloc (Inst_Node) then
14585 return;
14587 -- If Decl is an instantiation, then record it as requiring
14588 -- instantiation of the corresponding body, except if it is an
14589 -- abbreviated instantiation generated internally for conformance
14590 -- checking purposes only for the case of a formal package
14591 -- declared without a box (see Instantiate_Formal_Package). Such
14592 -- an instantiation does not generate any code (the actual code
14593 -- comes from actual) and thus does not need to be analyzed here.
14594 -- If the instantiation appears with a generic package body it is
14595 -- not analyzed here either.
14597 elsif Nkind (Decl) = N_Package_Instantiation
14598 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14599 then
14600 Append_Elmt (Decl, Previous_Instances);
14602 -- For a subprogram instantiation, omit instantiations intrinsic
14603 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14605 elsif Nkind (Decl) in N_Function_Instantiation
14606 | N_Procedure_Instantiation
14607 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14608 then
14609 Append_Elmt (Decl, Previous_Instances);
14611 elsif Nkind (Decl) = N_Package_Declaration then
14612 Collect_Previous_Instances
14613 (Visible_Declarations (Specification (Decl)));
14614 Collect_Previous_Instances
14615 (Private_Declarations (Specification (Decl)));
14617 -- Previous non-generic bodies may contain instances as well
14619 elsif Nkind (Decl) = N_Package_Body
14620 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14621 then
14622 Collect_Previous_Instances (Declarations (Decl));
14624 elsif Nkind (Decl) = N_Subprogram_Body
14625 and then not Acts_As_Spec (Decl)
14626 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14627 then
14628 Collect_Previous_Instances (Declarations (Decl));
14629 end if;
14631 Next (Decl);
14632 end loop;
14633 end Collect_Previous_Instances;
14635 -- Start of processing for Load_Parent_Of_Generic
14637 begin
14638 if not In_Same_Source_Unit (N, Spec)
14639 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14640 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14641 and then not Is_In_Main_Unit (Spec))
14642 then
14643 -- Find body of parent of spec, and analyze it. A special case arises
14644 -- when the parent is an instantiation, that is to say when we are
14645 -- currently instantiating a nested generic. In that case, there is
14646 -- no separate file for the body of the enclosing instance. Instead,
14647 -- the enclosing body must be instantiated as if it were a pending
14648 -- instantiation, in order to produce the body for the nested generic
14649 -- we require now. Note that in that case the generic may be defined
14650 -- in a package body, the instance defined in the same package body,
14651 -- and the original enclosing body may not be in the main unit.
14653 Inst_Node := Empty;
14655 True_Parent := Parent (Spec);
14656 while Present (True_Parent)
14657 and then Nkind (True_Parent) /= N_Compilation_Unit
14658 loop
14659 if Nkind (True_Parent) = N_Package_Declaration
14660 and then
14661 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14662 then
14663 -- Parent is a compilation unit that is an instantiation, and
14664 -- instantiation node has been replaced with package decl.
14666 Inst_Node := Original_Node (True_Parent);
14667 exit;
14669 elsif Nkind (True_Parent) = N_Package_Declaration
14670 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14671 and then
14672 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14673 then
14674 -- Parent is a compilation unit that is an instantiation, but
14675 -- instantiation node has not been replaced with package decl.
14677 Inst_Node := Unit (Parent (True_Parent));
14678 exit;
14680 elsif Nkind (True_Parent) = N_Package_Declaration
14681 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14682 and then Present (Generic_Parent (Specification (True_Parent)))
14683 then
14684 -- Parent is an instantiation within another specification.
14685 -- Declaration for instance has been inserted before original
14686 -- instantiation node. A direct link would be preferable?
14688 Inst_Node := Next (True_Parent);
14689 while Present (Inst_Node)
14690 and then Nkind (Inst_Node) /= N_Package_Instantiation
14691 loop
14692 Next (Inst_Node);
14693 end loop;
14695 -- If the instance appears within a generic, and the generic
14696 -- unit is defined within a formal package of the enclosing
14697 -- generic, there is no generic body available, and none
14698 -- needed. A more precise test should be used ???
14700 if No (Inst_Node) then
14701 return;
14702 end if;
14704 exit;
14706 -- If an ancestor of the generic comes from a formal package
14707 -- there is no source for the ancestor body. This is detected
14708 -- by examining the scope of the ancestor and its declaration.
14709 -- The body, if any is needed, will be available when the
14710 -- current unit (containing a formal package) is instantiated.
14712 elsif Nkind (True_Parent) = N_Package_Specification
14713 and then Present (Generic_Parent (True_Parent))
14714 and then Nkind
14715 (Original_Node (Unit_Declaration_Node
14716 (Scope (Generic_Parent (True_Parent)))))
14717 = N_Formal_Package_Declaration
14718 then
14719 return;
14721 else
14722 True_Parent := Parent (True_Parent);
14723 end if;
14724 end loop;
14726 -- Case where we are currently instantiating a nested generic
14728 if Present (Inst_Node) then
14729 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14731 -- Instantiation node and declaration of instantiated package
14732 -- were exchanged when only the declaration was needed.
14733 -- Restore instantiation node before proceeding with body.
14735 Set_Unit (Parent (True_Parent), Inst_Node);
14736 end if;
14738 -- Now complete instantiation of enclosing body, if it appears in
14739 -- some other unit. If it appears in the current unit, the body
14740 -- will have been instantiated already.
14742 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14744 -- We need to determine the expander mode to instantiate the
14745 -- enclosing body. Because the generic body we need may use
14746 -- global entities declared in the enclosing package (including
14747 -- aggregates) it is in general necessary to compile this body
14748 -- with expansion enabled, except if we are within a generic
14749 -- package, in which case the usual generic rule applies.
14751 declare
14752 Exp_Status : Boolean := True;
14753 Scop : Entity_Id;
14755 begin
14756 -- Loop through scopes looking for generic package
14758 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14759 while Present (Scop)
14760 and then Scop /= Standard_Standard
14761 loop
14762 if Ekind (Scop) = E_Generic_Package then
14763 Exp_Status := False;
14764 exit;
14765 end if;
14767 Scop := Scope (Scop);
14768 end loop;
14770 -- Collect previous instantiations in the unit that contains
14771 -- the desired generic.
14773 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14774 and then not Body_Optional
14775 then
14776 declare
14777 Decl : Elmt_Id;
14778 Info : Pending_Body_Info;
14779 Par : Node_Id;
14781 begin
14782 Par := Parent (Inst_Node);
14783 while Present (Par) loop
14784 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14785 Par := Parent (Par);
14786 end loop;
14788 pragma Assert (Present (Par));
14790 if Nkind (Par) = N_Package_Body then
14791 Collect_Previous_Instances (Declarations (Par));
14793 elsif Nkind (Par) = N_Package_Declaration then
14794 Collect_Previous_Instances
14795 (Visible_Declarations (Specification (Par)));
14796 Collect_Previous_Instances
14797 (Private_Declarations (Specification (Par)));
14799 else
14800 -- Enclosing unit is a subprogram body. In this
14801 -- case all instance bodies are processed in order
14802 -- and there is no need to collect them separately.
14804 null;
14805 end if;
14807 Decl := First_Elmt (Previous_Instances);
14808 while Present (Decl) loop
14809 Info :=
14810 (Inst_Node => Node (Decl),
14811 Act_Decl =>
14812 Instance_Spec (Node (Decl)),
14813 Fin_Scop => Empty,
14814 Config_Switches => Save_Config_Switches,
14815 Current_Sem_Unit =>
14816 Get_Code_Unit (Sloc (Node (Decl))),
14817 Expander_Status => Exp_Status,
14818 Local_Suppress_Stack_Top =>
14819 Local_Suppress_Stack_Top,
14820 Scope_Suppress => Scope_Suppress,
14821 Warnings => Save_Warnings);
14823 -- Package instance
14825 if Nkind (Node (Decl)) = N_Package_Instantiation
14826 then
14827 Instantiate_Package_Body
14828 (Info, Body_Optional => True);
14830 -- Subprogram instance
14832 else
14833 -- The instance_spec is in the wrapper package,
14834 -- usually followed by its local renaming
14835 -- declaration. See Build_Subprogram_Renaming
14836 -- for details. If the instance carries aspects,
14837 -- these result in the corresponding pragmas,
14838 -- inserted after the subprogram declaration.
14839 -- They must be skipped as well when retrieving
14840 -- the desired spec. Some of them may have been
14841 -- rewritten as null statements.
14842 -- A direct link would be more robust ???
14844 declare
14845 Decl : Node_Id :=
14846 (Last (Visible_Declarations
14847 (Specification (Info.Act_Decl))));
14848 begin
14849 while Nkind (Decl) in
14850 N_Null_Statement |
14851 N_Pragma |
14852 N_Subprogram_Renaming_Declaration
14853 loop
14854 Decl := Prev (Decl);
14855 end loop;
14857 Info.Act_Decl := Decl;
14858 end;
14860 Instantiate_Subprogram_Body
14861 (Info, Body_Optional => True);
14862 end if;
14864 Next_Elmt (Decl);
14865 end loop;
14866 end;
14867 end if;
14869 Instantiate_Package_Body
14870 (Body_Info =>
14871 ((Inst_Node => Inst_Node,
14872 Act_Decl => True_Parent,
14873 Fin_Scop => Empty,
14874 Config_Switches => Save_Config_Switches,
14875 Current_Sem_Unit =>
14876 Get_Code_Unit (Sloc (Inst_Node)),
14877 Expander_Status => Exp_Status,
14878 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14879 Scope_Suppress => Scope_Suppress,
14880 Warnings => Save_Warnings)),
14881 Body_Optional => Body_Optional);
14882 end;
14883 end if;
14885 -- Case where we are not instantiating a nested generic
14887 else
14888 Opt.Style_Check := False;
14889 Expander_Mode_Save_And_Set (True);
14890 Load_Needed_Body (Comp_Unit, OK);
14891 Opt.Style_Check := Saved_Style_Check;
14892 Restore_Warnings (Saved_Warn);
14893 Expander_Mode_Restore;
14895 if not OK
14896 and then Unit_Requires_Body (Defining_Entity (Spec))
14897 and then not Body_Optional
14898 then
14899 declare
14900 Bname : constant Unit_Name_Type :=
14901 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14903 begin
14904 -- In CodePeer mode, the missing body may make the analysis
14905 -- incomplete, but we do not treat it as fatal.
14907 if CodePeer_Mode then
14908 return;
14910 else
14911 Error_Msg_Unit_1 := Bname;
14912 Error_Msg_N ("this instantiation requires$!", N);
14913 Error_Msg_File_1 :=
14914 Get_File_Name (Bname, Subunit => False);
14915 Error_Msg_N ("\but file{ was not found!", N);
14916 raise Unrecoverable_Error;
14917 end if;
14918 end;
14919 end if;
14920 end if;
14921 end if;
14923 -- If loading parent of the generic caused an instantiation circularity,
14924 -- we abandon compilation at this point, because otherwise in some cases
14925 -- we get into trouble with infinite recursions after this point.
14927 if Circularity_Detected then
14928 raise Unrecoverable_Error;
14929 end if;
14930 end Load_Parent_Of_Generic;
14932 ---------------------------------
14933 -- Map_Formal_Package_Entities --
14934 ---------------------------------
14936 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14937 E1 : Entity_Id;
14938 E2 : Entity_Id;
14940 begin
14941 Set_Instance_Of (Form, Act);
14943 -- Traverse formal and actual package to map the corresponding entities.
14944 -- We skip over internal entities that may be generated during semantic
14945 -- analysis, and find the matching entities by name, given that they
14946 -- must appear in the same order.
14948 E1 := First_Entity (Form);
14949 E2 := First_Entity (Act);
14950 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14951 -- Could this test be a single condition??? Seems like it could, and
14952 -- isn't FPE (Form) a constant anyway???
14954 if not Is_Internal (E1)
14955 and then Present (Parent (E1))
14956 and then not Is_Class_Wide_Type (E1)
14957 and then not Is_Internal_Name (Chars (E1))
14958 then
14959 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14960 Next_Entity (E2);
14961 end loop;
14963 if No (E2) then
14964 exit;
14965 else
14966 Set_Instance_Of (E1, E2);
14968 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14969 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14970 end if;
14972 if Is_Constrained (E1) then
14973 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14974 end if;
14976 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14977 Map_Formal_Package_Entities (E1, E2);
14978 end if;
14979 end if;
14980 end if;
14982 Next_Entity (E1);
14983 end loop;
14984 end Map_Formal_Package_Entities;
14986 -----------------------
14987 -- Move_Freeze_Nodes --
14988 -----------------------
14990 procedure Move_Freeze_Nodes
14991 (Out_Of : Entity_Id;
14992 After : Node_Id;
14993 L : List_Id)
14995 Decl : Node_Id;
14996 Next_Decl : Node_Id;
14997 Next_Node : Node_Id := After;
14998 Spec : Node_Id;
15000 function Is_Outer_Type (T : Entity_Id) return Boolean;
15001 -- Check whether entity is declared in a scope external to that of the
15002 -- generic unit.
15004 -------------------
15005 -- Is_Outer_Type --
15006 -------------------
15008 function Is_Outer_Type (T : Entity_Id) return Boolean is
15009 Scop : Entity_Id := Scope (T);
15011 begin
15012 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15013 return True;
15015 else
15016 while Scop /= Standard_Standard loop
15017 if Scop = Out_Of then
15018 return False;
15019 else
15020 Scop := Scope (Scop);
15021 end if;
15022 end loop;
15024 return True;
15025 end if;
15026 end Is_Outer_Type;
15028 -- Start of processing for Move_Freeze_Nodes
15030 begin
15031 if No (L) then
15032 return;
15033 end if;
15035 -- First remove the freeze nodes that may appear before all other
15036 -- declarations.
15038 Decl := First (L);
15039 while Present (Decl)
15040 and then Nkind (Decl) = N_Freeze_Entity
15041 and then Is_Outer_Type (Entity (Decl))
15042 loop
15043 Decl := Remove_Head (L);
15044 Insert_After (Next_Node, Decl);
15045 Set_Analyzed (Decl, False);
15046 Next_Node := Decl;
15047 Decl := First (L);
15048 end loop;
15050 -- Next scan the list of declarations and remove each freeze node that
15051 -- appears ahead of the current node.
15053 while Present (Decl) loop
15054 while Present (Next (Decl))
15055 and then Nkind (Next (Decl)) = N_Freeze_Entity
15056 and then Is_Outer_Type (Entity (Next (Decl)))
15057 loop
15058 Next_Decl := Remove_Next (Decl);
15059 Insert_After (Next_Node, Next_Decl);
15060 Set_Analyzed (Next_Decl, False);
15061 Next_Node := Next_Decl;
15062 end loop;
15064 -- If the declaration is a nested package or concurrent type, then
15065 -- recurse. Nested generic packages will have been processed from the
15066 -- inside out.
15068 case Nkind (Decl) is
15069 when N_Package_Declaration =>
15070 Spec := Specification (Decl);
15072 when N_Task_Type_Declaration =>
15073 Spec := Task_Definition (Decl);
15075 when N_Protected_Type_Declaration =>
15076 Spec := Protected_Definition (Decl);
15078 when others =>
15079 Spec := Empty;
15080 end case;
15082 if Present (Spec) then
15083 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15084 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15085 end if;
15087 Next (Decl);
15088 end loop;
15089 end Move_Freeze_Nodes;
15091 ----------------
15092 -- Next_Assoc --
15093 ----------------
15095 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15096 begin
15097 return Generic_Renamings.Table (E).Next_In_HTable;
15098 end Next_Assoc;
15100 ------------------------
15101 -- Preanalyze_Actuals --
15102 ------------------------
15104 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15105 procedure Perform_Appropriate_Analysis (N : Node_Id);
15106 -- Determine if the actuals we are analyzing come from a generic
15107 -- instantiation that is a library unit and dispatch accordingly.
15109 ----------------------------------
15110 -- Perform_Appropriate_Analysis --
15111 ----------------------------------
15113 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15114 begin
15115 -- When we have a library instantiation we cannot allow any expansion
15116 -- to occur, since there may be no place to put it. Instead, in that
15117 -- case we perform a preanalysis of the actual.
15119 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15120 Preanalyze (N);
15121 else
15122 Analyze (N);
15123 end if;
15124 end Perform_Appropriate_Analysis;
15126 -- Local variables
15128 Errs : constant Nat := Serious_Errors_Detected;
15130 Assoc : Node_Id;
15131 Act : Node_Id;
15133 Cur : Entity_Id := Empty;
15134 -- Current homograph of the instance name
15136 Vis : Boolean := False;
15137 -- Saved visibility status of the current homograph
15139 -- Start of processing for Preanalyze_Actuals
15141 begin
15142 Assoc := First (Generic_Associations (N));
15144 -- If the instance is a child unit, its name may hide an outer homonym,
15145 -- so make it invisible to perform name resolution on the actuals.
15147 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15148 and then Present
15149 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15150 then
15151 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15153 if Is_Compilation_Unit (Cur) then
15154 Vis := Is_Immediately_Visible (Cur);
15155 Set_Is_Immediately_Visible (Cur, False);
15156 else
15157 Cur := Empty;
15158 end if;
15159 end if;
15161 while Present (Assoc) loop
15162 if Nkind (Assoc) /= N_Others_Choice then
15163 Act := Explicit_Generic_Actual_Parameter (Assoc);
15165 -- Within a nested instantiation, a defaulted actual is an empty
15166 -- association, so nothing to analyze. If the subprogram actual
15167 -- is an attribute, analyze prefix only, because actual is not a
15168 -- complete attribute reference.
15170 -- If actual is an allocator, analyze expression only. The full
15171 -- analysis can generate code, and if instance is a compilation
15172 -- unit we have to wait until the package instance is installed
15173 -- to have a proper place to insert this code.
15175 -- String literals may be operators, but at this point we do not
15176 -- know whether the actual is a formal subprogram or a string.
15178 if No (Act) then
15179 null;
15181 elsif Nkind (Act) = N_Attribute_Reference then
15182 Perform_Appropriate_Analysis (Prefix (Act));
15184 elsif Nkind (Act) = N_Explicit_Dereference then
15185 Perform_Appropriate_Analysis (Prefix (Act));
15187 elsif Nkind (Act) = N_Allocator then
15188 declare
15189 Expr : constant Node_Id := Expression (Act);
15191 begin
15192 if Nkind (Expr) = N_Subtype_Indication then
15193 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15195 -- Analyze separately each discriminant constraint, when
15196 -- given with a named association.
15198 declare
15199 Constr : Node_Id;
15201 begin
15202 Constr := First (Constraints (Constraint (Expr)));
15203 while Present (Constr) loop
15204 if Nkind (Constr) = N_Discriminant_Association then
15205 Perform_Appropriate_Analysis
15206 (Expression (Constr));
15207 else
15208 Perform_Appropriate_Analysis (Constr);
15209 end if;
15211 Next (Constr);
15212 end loop;
15213 end;
15215 else
15216 Perform_Appropriate_Analysis (Expr);
15217 end if;
15218 end;
15220 elsif Nkind (Act) /= N_Operator_Symbol then
15221 Perform_Appropriate_Analysis (Act);
15223 -- Within a package instance, mark actuals that are limited
15224 -- views, so their use can be moved to the body of the
15225 -- enclosing unit.
15227 if Is_Entity_Name (Act)
15228 and then Is_Type (Entity (Act))
15229 and then From_Limited_With (Entity (Act))
15230 and then Present (Inst)
15231 then
15232 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15233 end if;
15234 end if;
15236 if Errs /= Serious_Errors_Detected then
15238 -- Do a minimal analysis of the generic, to prevent spurious
15239 -- warnings complaining about the generic being unreferenced,
15240 -- before abandoning the instantiation.
15242 Perform_Appropriate_Analysis (Name (N));
15244 if Is_Entity_Name (Name (N))
15245 and then Etype (Name (N)) /= Any_Type
15246 then
15247 Generate_Reference (Entity (Name (N)), Name (N));
15248 Set_Is_Instantiated (Entity (Name (N)));
15249 end if;
15251 if Present (Cur) then
15253 -- For the case of a child instance hiding an outer homonym,
15254 -- provide additional warning which might explain the error.
15256 Set_Is_Immediately_Visible (Cur, Vis);
15257 Error_Msg_NE
15258 ("& hides outer unit with the same name??",
15259 N, Defining_Unit_Name (N));
15260 end if;
15262 Abandon_Instantiation (Act);
15263 end if;
15264 end if;
15266 Next (Assoc);
15267 end loop;
15269 if Present (Cur) then
15270 Set_Is_Immediately_Visible (Cur, Vis);
15271 end if;
15272 end Preanalyze_Actuals;
15274 -------------------------------
15275 -- Provide_Completing_Bodies --
15276 -------------------------------
15278 procedure Provide_Completing_Bodies (N : Node_Id) is
15279 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15280 -- Generate the completing body for subprogram declaration Subp_Decl
15282 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15283 -- Generating completing bodies for all subprograms found in declarative
15284 -- list Decls.
15286 ---------------------------
15287 -- Build_Completing_Body --
15288 ---------------------------
15290 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15291 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15292 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15293 Spec : Node_Id;
15295 begin
15296 -- Nothing to do if the subprogram already has a completing body
15298 if Present (Corresponding_Body (Subp_Decl)) then
15299 return;
15301 -- Mark the function as having a valid return statement even though
15302 -- the body contains a single raise statement.
15304 elsif Ekind (Subp_Id) = E_Function then
15305 Set_Return_Present (Subp_Id);
15306 end if;
15308 -- Clone the specification to obtain new entities and reset the only
15309 -- semantic field.
15311 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15312 Set_Generic_Parent (Spec, Empty);
15314 -- Generate:
15315 -- function Func ... return ... is
15316 -- <or>
15317 -- procedure Proc ... is
15318 -- begin
15319 -- raise Program_Error with "access before elaboration";
15320 -- edn Proc;
15322 Insert_After_And_Analyze (Subp_Decl,
15323 Make_Subprogram_Body (Loc,
15324 Specification => Spec,
15325 Declarations => New_List,
15326 Handled_Statement_Sequence =>
15327 Make_Handled_Sequence_Of_Statements (Loc,
15328 Statements => New_List (
15329 Make_Raise_Program_Error (Loc,
15330 Reason => PE_Access_Before_Elaboration)))));
15331 end Build_Completing_Body;
15333 ----------------------------------
15334 -- Provide_Completing_Bodies_In --
15335 ----------------------------------
15337 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15338 Decl : Node_Id;
15340 begin
15341 if Present (Decls) then
15342 Decl := First (Decls);
15343 while Present (Decl) loop
15344 Provide_Completing_Bodies (Decl);
15345 Next (Decl);
15346 end loop;
15347 end if;
15348 end Provide_Completing_Bodies_In;
15350 -- Local variables
15352 Spec : Node_Id;
15354 -- Start of processing for Provide_Completing_Bodies
15356 begin
15357 if Nkind (N) = N_Package_Declaration then
15358 Spec := Specification (N);
15360 Push_Scope (Defining_Entity (N));
15361 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15362 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15363 Pop_Scope;
15365 elsif Nkind (N) = N_Subprogram_Declaration then
15366 Build_Completing_Body (N);
15367 end if;
15368 end Provide_Completing_Bodies;
15370 -------------------
15371 -- Remove_Parent --
15372 -------------------
15374 procedure Remove_Parent (In_Body : Boolean := False) is
15375 S : Entity_Id := Current_Scope;
15376 -- S is the scope containing the instantiation just completed. The scope
15377 -- stack contains the parent instances of the instantiation, followed by
15378 -- the original S.
15380 Cur_P : Entity_Id;
15381 E : Entity_Id;
15382 P : Entity_Id;
15383 Hidden : Elmt_Id;
15385 begin
15386 -- After child instantiation is complete, remove from scope stack the
15387 -- extra copy of the current scope, and then remove parent instances.
15389 if not In_Body then
15390 Pop_Scope;
15392 while Current_Scope /= S loop
15393 P := Current_Scope;
15394 End_Package_Scope (Current_Scope);
15396 if In_Open_Scopes (P) then
15397 E := First_Entity (P);
15398 while Present (E) loop
15399 Set_Is_Immediately_Visible (E, True);
15400 Next_Entity (E);
15401 end loop;
15403 -- If instantiation is declared in a block, it is the enclosing
15404 -- scope that might be a parent instance. Note that only one
15405 -- block can be involved, because the parent instances have
15406 -- been installed within it.
15408 if Ekind (P) = E_Block then
15409 Cur_P := Scope (P);
15410 else
15411 Cur_P := P;
15412 end if;
15414 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15415 -- We are within an instance of some sibling. Retain
15416 -- visibility of parent, for proper subsequent cleanup, and
15417 -- reinstall private declarations as well.
15419 Set_In_Private_Part (P);
15420 Install_Private_Declarations (P);
15421 end if;
15423 -- If the ultimate parent is a top-level unit recorded in
15424 -- Instance_Parent_Unit, then reset its visibility to what it was
15425 -- before instantiation. (It's not clear what the purpose is of
15426 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15427 -- present before the ultimate parent test was added.???)
15429 elsif not In_Open_Scopes (Scope (P))
15430 or else (P = Instance_Parent_Unit
15431 and then not Parent_Unit_Visible)
15432 then
15433 Set_Is_Immediately_Visible (P, False);
15435 -- If the current scope is itself an instantiation of a generic
15436 -- nested within P, and we are in the private part of body of this
15437 -- instantiation, restore the full views of P, that were removed
15438 -- in End_Package_Scope above. This obscure case can occur when a
15439 -- subunit of a generic contains an instance of a child unit of
15440 -- its generic parent unit.
15442 elsif S = Current_Scope and then Is_Generic_Instance (S)
15443 and then (In_Package_Body (S) or else In_Private_Part (S))
15444 then
15445 declare
15446 Par : constant Entity_Id :=
15447 Generic_Parent (Package_Specification (S));
15448 begin
15449 if Present (Par)
15450 and then P = Scope (Par)
15451 then
15452 Set_In_Private_Part (P);
15453 Install_Private_Declarations (P);
15454 end if;
15455 end;
15456 end if;
15457 end loop;
15459 -- Reset visibility of entities in the enclosing scope
15461 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15463 Hidden := First_Elmt (Hidden_Entities);
15464 while Present (Hidden) loop
15465 Set_Is_Immediately_Visible (Node (Hidden), True);
15466 Next_Elmt (Hidden);
15467 end loop;
15469 else
15470 -- Each body is analyzed separately, and there is no context that
15471 -- needs preserving from one body instance to the next, so remove all
15472 -- parent scopes that have been installed.
15474 while Present (S) loop
15475 End_Package_Scope (S);
15476 Set_Is_Immediately_Visible (S, False);
15477 S := Current_Scope;
15478 exit when S = Standard_Standard;
15479 end loop;
15480 end if;
15481 end Remove_Parent;
15483 -----------------------------------
15484 -- Requires_Conformance_Checking --
15485 -----------------------------------
15487 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15488 begin
15489 -- No conformance checking required if the generic actual part is empty,
15490 -- or is a box or an others_clause (necessarily with a box).
15492 return Present (Generic_Associations (N))
15493 and then not Box_Present (N)
15494 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15495 end Requires_Conformance_Checking;
15497 -----------------
15498 -- Restore_Env --
15499 -----------------
15501 procedure Restore_Env is
15502 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15504 begin
15505 if No (Current_Instantiated_Parent.Act_Id) then
15506 -- Restore environment after subprogram inlining
15508 Restore_Private_Views (Empty);
15509 end if;
15511 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15512 Exchanged_Views := Saved.Exchanged_Views;
15513 Hidden_Entities := Saved.Hidden_Entities;
15514 Current_Sem_Unit := Saved.Current_Sem_Unit;
15515 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15516 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15518 Restore_Config_Switches (Saved.Switches);
15520 Instance_Envs.Decrement_Last;
15521 end Restore_Env;
15523 ---------------------------
15524 -- Restore_Private_Views --
15525 ---------------------------
15527 procedure Restore_Private_Views
15528 (Pack_Id : Entity_Id;
15529 Is_Package : Boolean := True)
15531 M : Elmt_Id;
15532 E : Entity_Id;
15533 Typ : Entity_Id;
15534 Dep_Elmt : Elmt_Id;
15535 Dep_Typ : Node_Id;
15537 procedure Restore_Nested_Formal (Formal : Entity_Id);
15538 -- Hide the generic formals of formal packages declared with box which
15539 -- were reachable in the current instantiation.
15541 ---------------------------
15542 -- Restore_Nested_Formal --
15543 ---------------------------
15545 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15546 pragma Assert (Ekind (Formal) = E_Package);
15547 Ent : Entity_Id;
15548 begin
15549 if Present (Renamed_Entity (Formal))
15550 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15551 then
15552 return;
15554 elsif Present (Associated_Formal_Package (Formal)) then
15555 Ent := First_Entity (Formal);
15556 while Present (Ent) loop
15557 exit when Ekind (Ent) = E_Package
15558 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15560 Set_Is_Hidden (Ent);
15561 Set_Is_Potentially_Use_Visible (Ent, False);
15563 -- If package, then recurse
15565 if Ekind (Ent) = E_Package then
15566 Restore_Nested_Formal (Ent);
15567 end if;
15569 Next_Entity (Ent);
15570 end loop;
15571 end if;
15572 end Restore_Nested_Formal;
15574 -- Start of processing for Restore_Private_Views
15576 begin
15577 M := First_Elmt (Exchanged_Views);
15578 while Present (M) loop
15579 Typ := Node (M);
15581 -- Subtypes of types whose views have been exchanged, and that are
15582 -- defined within the instance, were not on the Private_Dependents
15583 -- list on entry to the instance, so they have to be exchanged
15584 -- explicitly now, in order to remain consistent with the view of the
15585 -- parent type.
15587 if Ekind (Typ) in E_Private_Type
15588 | E_Limited_Private_Type
15589 | E_Record_Type_With_Private
15590 then
15591 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15592 while Present (Dep_Elmt) loop
15593 Dep_Typ := Node (Dep_Elmt);
15595 if Scope (Dep_Typ) = Pack_Id
15596 and then Present (Full_View (Dep_Typ))
15597 then
15598 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15599 Exchange_Declarations (Dep_Typ);
15600 end if;
15602 Next_Elmt (Dep_Elmt);
15603 end loop;
15604 end if;
15606 Exchange_Declarations (Typ);
15607 Next_Elmt (M);
15608 end loop;
15610 if No (Pack_Id) then
15611 return;
15612 end if;
15614 -- Make the generic formal parameters private, and make the formal types
15615 -- into subtypes of the actuals again.
15617 E := First_Entity (Pack_Id);
15618 while Present (E) loop
15619 Set_Is_Hidden (E, True);
15621 if Is_Type (E)
15622 and then Nkind (Parent (E)) = N_Subtype_Declaration
15623 then
15624 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15625 -- as it is needed to identify the subtype with the type it
15626 -- renames, when there are conversions between access types
15627 -- to these.
15629 if GNATprove_Mode then
15630 null;
15632 -- If the actual for E is itself a generic actual type from
15633 -- an enclosing instance, E is still a generic actual type
15634 -- outside of the current instance. This matter when resolving
15635 -- an overloaded call that may be ambiguous in the enclosing
15636 -- instance, when two of its actuals coincide.
15638 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15639 and then Is_Generic_Actual_Type
15640 (Entity (Subtype_Indication (Parent (E))))
15641 then
15642 null;
15643 else
15644 Set_Is_Generic_Actual_Type (E, False);
15646 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15647 -- flag also on the Full_View if the type is private, since it
15648 -- was set also on this Full_View. However, this flag is relied
15649 -- upon by Covers to spot "types exported from instantiations"
15650 -- which are implicit Full_Views built for instantiations made
15651 -- on private types and we get type mismatches if we do it when
15652 -- the block exchanging the declarations below triggers ???
15654 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15655 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15656 -- end if;
15657 end if;
15659 -- An unusual case of aliasing: the actual may also be directly
15660 -- visible in the generic, and be private there, while it is fully
15661 -- visible in the context of the instance. The internal subtype
15662 -- is private in the instance but has full visibility like its
15663 -- parent in the enclosing scope. This enforces the invariant that
15664 -- the privacy status of all private dependents of a type coincide
15665 -- with that of the parent type. This can only happen when a
15666 -- generic child unit is instantiated within a sibling.
15668 if Is_Private_Type (E)
15669 and then not Is_Private_Type (Etype (E))
15670 then
15671 Exchange_Declarations (E);
15672 end if;
15674 elsif Ekind (E) = E_Package then
15676 -- The end of the renaming list is the renaming of the generic
15677 -- package itself. If the instance is a subprogram, all entities
15678 -- in the corresponding package are renamings. If this entity is
15679 -- a formal package, make its own formals private as well. The
15680 -- actual in this case is itself the renaming of an instantiation.
15681 -- If the entity is not a package renaming, it is the entity
15682 -- created to validate formal package actuals: ignore it.
15684 -- If the actual is itself a formal package for the enclosing
15685 -- generic, or the actual for such a formal package, it remains
15686 -- visible on exit from the instance, and therefore nothing needs
15687 -- to be done either, except to keep it accessible.
15689 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15690 exit;
15692 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15693 null;
15695 elsif
15696 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15697 then
15698 Set_Is_Hidden (E, False);
15700 else
15701 declare
15702 Act_P : constant Entity_Id := Renamed_Entity (E);
15703 Id : Entity_Id;
15705 begin
15706 Id := First_Entity (Act_P);
15707 while Present (Id)
15708 and then Id /= First_Private_Entity (Act_P)
15709 loop
15710 exit when Ekind (Id) = E_Package
15711 and then Renamed_Entity (Id) = Act_P;
15713 Set_Is_Hidden (Id, True);
15714 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15716 if Ekind (Id) = E_Package then
15717 Restore_Nested_Formal (Id);
15718 end if;
15720 Next_Entity (Id);
15721 end loop;
15722 end;
15723 end if;
15724 end if;
15726 Next_Entity (E);
15727 end loop;
15728 end Restore_Private_Views;
15730 --------------
15731 -- Save_Env --
15732 --------------
15734 procedure Save_Env
15735 (Gen_Unit : Entity_Id;
15736 Act_Unit : Entity_Id)
15738 begin
15739 Init_Env;
15740 Set_Instance_Env (Gen_Unit, Act_Unit);
15741 end Save_Env;
15743 ----------------------------
15744 -- Save_Global_References --
15745 ----------------------------
15747 procedure Save_Global_References (Templ : Node_Id) is
15749 -- ??? it is horrible to use global variables in highly recursive code
15751 E : Entity_Id;
15752 -- The entity of the current associated node
15754 Gen_Scope : Entity_Id;
15755 -- The scope of the generic for which references are being saved
15757 N2 : Node_Id;
15758 -- The current associated node
15760 function Is_Global (E : Entity_Id) return Boolean;
15761 -- Check whether entity is defined outside of generic unit. Examine the
15762 -- scope of an entity, and the scope of the scope, etc, until we find
15763 -- either Standard, in which case the entity is global, or the generic
15764 -- unit itself, which indicates that the entity is local. If the entity
15765 -- is the generic unit itself, as in the case of a recursive call, or
15766 -- the enclosing generic unit, if different from the current scope, then
15767 -- it is local as well, because it will be replaced at the point of
15768 -- instantiation. On the other hand, if it is a reference to a child
15769 -- unit of a common ancestor, which appears in an instantiation, it is
15770 -- global because it is used to denote a specific compilation unit at
15771 -- the time the instantiations will be analyzed.
15773 procedure Qualify_Universal_Operands
15774 (Op : Node_Id;
15775 Func_Call : Node_Id);
15776 -- Op denotes a binary or unary operator in generic template Templ. Node
15777 -- Func_Call is the function call alternative of the operator within the
15778 -- the analyzed copy of the template. Change each operand which yields a
15779 -- universal type by wrapping it into a qualified expression
15781 -- Actual_Typ'(Operand)
15783 -- where Actual_Typ is the type of corresponding actual parameter of
15784 -- Operand in Func_Call.
15786 procedure Reset_Entity (N : Node_Id);
15787 -- Save semantic information on global entity so that it is not resolved
15788 -- again at instantiation time.
15790 procedure Save_Entity_Descendants (N : Node_Id);
15791 -- Apply Save_Global_References to the two syntactic descendants of
15792 -- non-terminal nodes that carry an Associated_Node and are processed
15793 -- through Reset_Entity. Once the global entity (if any) has been
15794 -- captured together with its type, only two syntactic descendants need
15795 -- to be traversed to complete the processing of the tree rooted at N.
15796 -- This applies to Selected_Components, Expanded_Names, and to Operator
15797 -- nodes. N can also be a character literal, identifier, or operator
15798 -- symbol node, but the call has no effect in these cases.
15800 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15801 -- Default actuals in nested instances must be handled specially
15802 -- because there is no link to them from the original tree. When an
15803 -- actual subprogram is given by a default, we add an explicit generic
15804 -- association for it in the instantiation node. When we save the
15805 -- global references on the name of the instance, we recover the list
15806 -- of generic associations, and add an explicit one to the original
15807 -- generic tree, through which a global actual can be preserved.
15808 -- Similarly, if a child unit is instantiated within a sibling, in the
15809 -- context of the parent, we must preserve the identifier of the parent
15810 -- so that it can be properly resolved in a subsequent instantiation.
15812 procedure Save_Global_Descendant (D : Union_Id);
15813 -- Apply Save_References recursively to the descendants of node D
15815 procedure Save_References (N : Node_Id);
15816 -- This is the recursive procedure that does the work, once the
15817 -- enclosing generic scope has been established.
15819 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15820 -- If the type of N2 is global to the generic unit, save the type in
15821 -- the generic node. Just as we perform name capture for explicit
15822 -- references within the generic, we must capture the global types
15823 -- of local entities because they may participate in resolution in
15824 -- the instance.
15826 ---------------
15827 -- Is_Global --
15828 ---------------
15830 function Is_Global (E : Entity_Id) return Boolean is
15831 Se : Entity_Id;
15833 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15834 -- Determine whether the parent node of a reference to a child unit
15835 -- denotes an instantiation or a formal package, in which case the
15836 -- reference to the child unit is global, even if it appears within
15837 -- the current scope (e.g. when the instance appears within the body
15838 -- of an ancestor).
15840 ----------------------
15841 -- Is_Instance_Node --
15842 ----------------------
15844 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15845 begin
15846 return Nkind (Decl) in N_Generic_Instantiation
15847 or else
15848 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15849 end Is_Instance_Node;
15851 -- Start of processing for Is_Global
15853 begin
15854 if E = Gen_Scope then
15855 return False;
15857 elsif E = Standard_Standard then
15858 return True;
15860 -- E should be an entity, but it is not always
15862 elsif Nkind (E) not in N_Entity then
15863 return False;
15865 elsif Nkind (E) /= N_Expanded_Name
15866 and then Is_Child_Unit (E)
15867 and then (Is_Instance_Node (Parent (N2))
15868 or else (Nkind (Parent (N2)) = N_Expanded_Name
15869 and then N2 = Selector_Name (Parent (N2))
15870 and then
15871 Is_Instance_Node (Parent (Parent (N2)))))
15872 then
15873 return True;
15875 else
15876 -- E may be an expanded name - typically an operator - in which
15877 -- case we must find its enclosing scope since expanded names
15878 -- don't have corresponding scopes.
15880 if Nkind (E) = N_Expanded_Name then
15881 Se := Find_Enclosing_Scope (E);
15883 -- Otherwise, E is an entity and will have Scope set
15885 else
15886 Se := Scope (E);
15887 end if;
15889 while Se /= Gen_Scope loop
15890 if Se = Standard_Standard then
15891 return True;
15892 else
15893 Se := Scope (Se);
15894 end if;
15895 end loop;
15897 return False;
15898 end if;
15899 end Is_Global;
15901 --------------------------------
15902 -- Qualify_Universal_Operands --
15903 --------------------------------
15905 procedure Qualify_Universal_Operands
15906 (Op : Node_Id;
15907 Func_Call : Node_Id)
15909 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15910 -- Rewrite operand Opnd as a qualified expression of the form
15912 -- Actual_Typ'(Opnd)
15914 -- where Actual is the corresponding actual parameter of Opnd in
15915 -- function call Func_Call.
15917 function Qualify_Type
15918 (Loc : Source_Ptr;
15919 Typ : Entity_Id) return Node_Id;
15920 -- Qualify type Typ by creating a selected component of the form
15922 -- Scope_Of_Typ.Typ
15924 ---------------------
15925 -- Qualify_Operand --
15926 ---------------------
15928 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15929 Loc : constant Source_Ptr := Sloc (Opnd);
15930 Typ : constant Entity_Id := Etype (Actual);
15931 Mark : Node_Id;
15932 Qual : Node_Id;
15934 begin
15935 -- Qualify the operand when it is of a universal type. Note that
15936 -- the template is unanalyzed and it is not possible to directly
15937 -- query the type. This transformation is not done when the type
15938 -- of the actual is internally generated because the type will be
15939 -- regenerated in the instance.
15941 if Yields_Universal_Type (Opnd)
15942 and then Comes_From_Source (Typ)
15943 and then not Is_Hidden (Typ)
15944 then
15945 -- The type of the actual may be a global reference. Save this
15946 -- information by creating a reference to it.
15948 if Is_Global (Typ) then
15949 Mark := New_Occurrence_Of (Typ, Loc);
15951 -- Otherwise rely on resolution to find the proper type within
15952 -- the instance.
15954 else
15955 Mark := Qualify_Type (Loc, Typ);
15956 end if;
15958 Qual :=
15959 Make_Qualified_Expression (Loc,
15960 Subtype_Mark => Mark,
15961 Expression => Relocate_Node (Opnd));
15963 -- Mark the qualification to distinguish it from other source
15964 -- constructs and signal the instantiation mechanism that this
15965 -- node requires special processing. See Copy_Generic_Node for
15966 -- details.
15968 Set_Is_Qualified_Universal_Literal (Qual);
15970 Rewrite (Opnd, Qual);
15971 end if;
15972 end Qualify_Operand;
15974 ------------------
15975 -- Qualify_Type --
15976 ------------------
15978 function Qualify_Type
15979 (Loc : Source_Ptr;
15980 Typ : Entity_Id) return Node_Id
15982 Scop : constant Entity_Id := Scope (Typ);
15983 Result : Node_Id;
15985 begin
15986 Result := Make_Identifier (Loc, Chars (Typ));
15988 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15989 Result :=
15990 Make_Selected_Component (Loc,
15991 Prefix => Make_Identifier (Loc, Chars (Scop)),
15992 Selector_Name => Result);
15993 end if;
15995 return Result;
15996 end Qualify_Type;
15998 -- Local variables
16000 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16002 -- Start of processing for Qualify_Universal_Operands
16004 begin
16005 if Nkind (Op) in N_Binary_Op then
16006 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16007 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16009 elsif Nkind (Op) in N_Unary_Op then
16010 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16011 end if;
16012 end Qualify_Universal_Operands;
16014 ------------------
16015 -- Reset_Entity --
16016 ------------------
16018 procedure Reset_Entity (N : Node_Id) is
16019 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16020 -- Find the ultimate ancestor of the current unit. If it is not a
16021 -- generic unit, then the name of the current unit in the prefix of
16022 -- an expanded name must be replaced with its generic homonym to
16023 -- ensure that it will be properly resolved in an instance.
16025 ------------------
16026 -- Top_Ancestor --
16027 ------------------
16029 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16030 Par : Entity_Id;
16032 begin
16033 Par := E;
16034 while Is_Child_Unit (Par) loop
16035 Par := Scope (Par);
16036 end loop;
16038 return Par;
16039 end Top_Ancestor;
16041 -- Start of processing for Reset_Entity
16043 begin
16044 N2 := Get_Associated_Node (N);
16045 E := Entity (N2);
16047 if Present (E) then
16049 -- If the node is an entry call to an entry in an enclosing task,
16050 -- it is rewritten as a selected component. No global entity to
16051 -- preserve in this case, since the expansion will be redone in
16052 -- the instance.
16054 if Nkind (E) not in N_Entity then
16055 Set_Associated_Node (N, Empty);
16056 Set_Etype (N, Empty);
16057 return;
16058 end if;
16060 -- If the entity is an itype created as a subtype of an access
16061 -- type with a null exclusion restore source entity for proper
16062 -- visibility. The itype will be created anew in the instance.
16064 if Is_Itype (E)
16065 and then Ekind (E) = E_Access_Subtype
16066 and then Is_Entity_Name (N)
16067 and then Chars (Etype (E)) = Chars (N)
16068 then
16069 E := Etype (E);
16070 Set_Entity (N2, E);
16071 Set_Etype (N2, E);
16072 end if;
16074 if Is_Global (E) then
16075 Set_Global_Type (N, N2);
16077 elsif Nkind (N) = N_Op_Concat
16078 and then Is_Generic_Type (Etype (N2))
16079 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16080 or else
16081 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16082 and then Is_Intrinsic_Subprogram (E)
16083 then
16084 null;
16086 -- Entity is local. Mark generic node as unresolved. Note that now
16087 -- it does not have an entity.
16089 else
16090 Set_Associated_Node (N, Empty);
16091 Set_Etype (N, Empty);
16092 end if;
16094 if Nkind (Parent (N)) in N_Generic_Instantiation
16095 and then N = Name (Parent (N))
16096 then
16097 Save_Global_Defaults (Parent (N), Parent (N2));
16098 end if;
16100 elsif Nkind (Parent (N)) = N_Selected_Component
16101 and then Nkind (Parent (N2)) = N_Expanded_Name
16102 then
16103 -- In case of previous errors, the tree might be malformed
16105 if No (Entity (Parent (N2))) then
16106 null;
16108 elsif Is_Global (Entity (Parent (N2))) then
16109 Change_Selected_Component_To_Expanded_Name (Parent (N));
16110 Set_Associated_Node (Parent (N), Parent (N2));
16111 Set_Global_Type (Parent (N), Parent (N2));
16112 Save_Entity_Descendants (N);
16114 -- If this is a reference to the current generic entity, replace
16115 -- by the name of the generic homonym of the current package. This
16116 -- is because in an instantiation Par.P.Q will not resolve to the
16117 -- name of the instance, whose enclosing scope is not necessarily
16118 -- Par. We use the generic homonym rather that the name of the
16119 -- generic itself because it may be hidden by a local declaration.
16121 elsif In_Open_Scopes (Entity (Parent (N2)))
16122 and then not
16123 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16124 then
16125 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16126 Rewrite (Parent (N),
16127 Make_Identifier (Sloc (N),
16128 Chars =>
16129 Chars (Generic_Homonym (Entity (Parent (N2))))));
16130 else
16131 Rewrite (Parent (N),
16132 Make_Identifier (Sloc (N),
16133 Chars => Chars (Selector_Name (Parent (N2)))));
16134 end if;
16135 end if;
16137 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16138 and then Parent (N) = Name (Parent (Parent (N)))
16139 then
16140 Save_Global_Defaults
16141 (Parent (Parent (N)), Parent (Parent (N2)));
16142 end if;
16144 -- A selected component may denote a static constant that has been
16145 -- folded. If the static constant is global to the generic, capture
16146 -- its value. Otherwise the folding will happen in any instantiation.
16148 elsif Nkind (Parent (N)) = N_Selected_Component
16149 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16150 then
16151 if Present (Entity (Original_Node (Parent (N2))))
16152 and then Is_Global (Entity (Original_Node (Parent (N2))))
16153 then
16154 Rewrite (Parent (N), New_Copy (Parent (N2)));
16155 Set_Analyzed (Parent (N), False);
16156 end if;
16158 -- A selected component may be transformed into a parameterless
16159 -- function call. If the called entity is global, rewrite the node
16160 -- appropriately, i.e. as an extended name for the global entity.
16162 elsif Nkind (Parent (N)) = N_Selected_Component
16163 and then Nkind (Parent (N2)) = N_Function_Call
16164 and then N = Selector_Name (Parent (N))
16165 then
16166 if No (Parameter_Associations (Parent (N2))) then
16167 if Is_Global (Entity (Name (Parent (N2)))) then
16168 Change_Selected_Component_To_Expanded_Name (Parent (N));
16169 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16170 Set_Global_Type (Parent (N), Name (Parent (N2)));
16171 Save_Entity_Descendants (N);
16173 else
16174 Set_Is_Prefixed_Call (Parent (N));
16175 Set_Associated_Node (N, Empty);
16176 Set_Etype (N, Empty);
16177 end if;
16179 -- In Ada 2005, X.F may be a call to a primitive operation,
16180 -- rewritten as F (X). This rewriting will be done again in an
16181 -- instance, so keep the original node. Global entities will be
16182 -- captured as for other constructs. Indicate that this must
16183 -- resolve as a call, to prevent accidental overloading in the
16184 -- instance, if both a component and a primitive operation appear
16185 -- as candidates.
16187 else
16188 Set_Is_Prefixed_Call (Parent (N));
16189 end if;
16191 -- Entity is local. Reset in generic unit, so that node is resolved
16192 -- anew at the point of instantiation.
16194 else
16195 Set_Associated_Node (N, Empty);
16196 Set_Etype (N, Empty);
16197 end if;
16198 end Reset_Entity;
16200 -----------------------------
16201 -- Save_Entity_Descendants --
16202 -----------------------------
16204 procedure Save_Entity_Descendants (N : Node_Id) is
16205 begin
16206 case Nkind (N) is
16207 when N_Binary_Op =>
16208 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16209 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16211 when N_Unary_Op =>
16212 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16214 when N_Expanded_Name
16215 | N_Selected_Component
16217 Save_Global_Descendant (Union_Id (Prefix (N)));
16218 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16220 when N_Character_Literal
16221 | N_Identifier
16222 | N_Operator_Symbol
16224 null;
16226 when others =>
16227 raise Program_Error;
16228 end case;
16229 end Save_Entity_Descendants;
16231 --------------------------
16232 -- Save_Global_Defaults --
16233 --------------------------
16235 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16236 Loc : constant Source_Ptr := Sloc (N1);
16237 Assoc2 : constant List_Id := Generic_Associations (N2);
16238 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16239 Assoc1 : List_Id;
16240 Act1 : Node_Id;
16241 Act2 : Node_Id;
16242 Def : Node_Id;
16243 Ndec : Node_Id;
16244 Subp : Entity_Id;
16245 Actual : Entity_Id;
16247 begin
16248 Assoc1 := Generic_Associations (N1);
16250 if Present (Assoc1) then
16251 Act1 := First (Assoc1);
16252 else
16253 Act1 := Empty;
16254 Set_Generic_Associations (N1, New_List);
16255 Assoc1 := Generic_Associations (N1);
16256 end if;
16258 if Present (Assoc2) then
16259 Act2 := First (Assoc2);
16260 else
16261 return;
16262 end if;
16264 while Present (Act1) and then Present (Act2) loop
16265 Next (Act1);
16266 Next (Act2);
16267 end loop;
16269 -- Find the associations added for default subprograms
16271 if Present (Act2) then
16272 while Nkind (Act2) /= N_Generic_Association
16273 or else No (Entity (Selector_Name (Act2)))
16274 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16275 loop
16276 Next (Act2);
16277 end loop;
16279 -- Add a similar association if the default is global. The
16280 -- renaming declaration for the actual has been analyzed, and
16281 -- its alias is the program it renames. Link the actual in the
16282 -- original generic tree with the node in the analyzed tree.
16284 while Present (Act2) loop
16285 Subp := Entity (Selector_Name (Act2));
16286 Def := Explicit_Generic_Actual_Parameter (Act2);
16288 -- Following test is defence against rubbish errors
16290 if No (Alias (Subp)) then
16291 return;
16292 end if;
16294 -- Retrieve the resolved actual from the renaming declaration
16295 -- created for the instantiated formal.
16297 Actual := Entity (Name (Parent (Parent (Subp))));
16298 Set_Entity (Def, Actual);
16299 Set_Etype (Def, Etype (Actual));
16301 if Is_Global (Actual) then
16302 Ndec :=
16303 Make_Generic_Association (Loc,
16304 Selector_Name =>
16305 New_Occurrence_Of (Subp, Loc),
16306 Explicit_Generic_Actual_Parameter =>
16307 New_Occurrence_Of (Actual, Loc));
16309 Set_Associated_Node
16310 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16312 Append (Ndec, Assoc1);
16314 -- If there are other defaults, add a dummy association in case
16315 -- there are other defaulted formals with the same name.
16317 elsif Present (Next (Act2)) then
16318 Ndec :=
16319 Make_Generic_Association (Loc,
16320 Selector_Name =>
16321 New_Occurrence_Of (Subp, Loc),
16322 Explicit_Generic_Actual_Parameter => Empty);
16324 Append (Ndec, Assoc1);
16325 end if;
16327 Next (Act2);
16328 end loop;
16329 end if;
16331 if Nkind (Name (N1)) = N_Identifier
16332 and then Is_Child_Unit (Gen_Id)
16333 and then Is_Global (Gen_Id)
16334 and then Is_Generic_Unit (Scope (Gen_Id))
16335 and then In_Open_Scopes (Scope (Gen_Id))
16336 then
16337 -- This is an instantiation of a child unit within a sibling, so
16338 -- that the generic parent is in scope. An eventual instance must
16339 -- occur within the scope of an instance of the parent. Make name
16340 -- in instance into an expanded name, to preserve the identifier
16341 -- of the parent, so it can be resolved subsequently.
16343 Rewrite (Name (N2),
16344 Make_Expanded_Name (Loc,
16345 Chars => Chars (Gen_Id),
16346 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16347 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16348 Set_Entity (Name (N2), Gen_Id);
16350 Rewrite (Name (N1),
16351 Make_Expanded_Name (Loc,
16352 Chars => Chars (Gen_Id),
16353 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16354 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16356 Set_Associated_Node (Name (N1), Name (N2));
16357 Set_Associated_Node (Prefix (Name (N1)), Empty);
16358 Set_Associated_Node
16359 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16360 Set_Etype (Name (N1), Etype (Gen_Id));
16361 end if;
16362 end Save_Global_Defaults;
16364 ----------------------------
16365 -- Save_Global_Descendant --
16366 ----------------------------
16368 procedure Save_Global_Descendant (D : Union_Id) is
16369 N1 : Node_Id;
16371 begin
16372 if D in Node_Range then
16373 if D = Union_Id (Empty) then
16374 null;
16376 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16377 Save_References (Node_Id (D));
16378 end if;
16380 elsif D in List_Range then
16381 pragma Assert (D /= Union_Id (No_List));
16382 -- Because No_List = Empty, which is in Node_Range above
16384 N1 := First (List_Id (D));
16385 while Present (N1) loop
16386 Save_References (N1);
16387 Next (N1);
16388 end loop;
16390 -- Element list or other non-node field, nothing to do
16392 else
16393 null;
16394 end if;
16395 end Save_Global_Descendant;
16397 ---------------------
16398 -- Save_References --
16399 ---------------------
16401 -- This is the recursive procedure that does the work once the enclosing
16402 -- generic scope has been established. We have to treat specially a
16403 -- number of node rewritings that are required by semantic processing
16404 -- and which change the kind of nodes in the generic copy: typically
16405 -- constant-folding, replacing an operator node by a string literal, or
16406 -- a selected component by an expanded name. In each of those cases, the
16407 -- transformation is propagated to the generic unit.
16409 procedure Save_References (N : Node_Id) is
16410 Loc : constant Source_Ptr := Sloc (N);
16412 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16413 -- Determine whether arbitrary node Nod requires delayed capture of
16414 -- global references within its aspect specifications.
16416 procedure Save_References_In_Aggregate (N : Node_Id);
16417 -- Save all global references in [extension] aggregate node N
16419 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16420 -- Save all global references in a character literal or operator
16421 -- symbol denoted by N.
16423 procedure Save_References_In_Descendants (N : Node_Id);
16424 -- Save all global references in all descendants of node N
16426 procedure Save_References_In_Identifier (N : Node_Id);
16427 -- Save all global references in identifier node N
16429 procedure Save_References_In_Operator (N : Node_Id);
16430 -- Save all global references in operator node N
16432 procedure Save_References_In_Pragma (Prag : Node_Id);
16433 -- Save all global references found within the expression of pragma
16434 -- Prag.
16436 ---------------------------
16437 -- Requires_Delayed_Save --
16438 ---------------------------
16440 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16441 begin
16442 -- Generic packages and subprograms require delayed capture of
16443 -- global references within their aspects due to the timing of
16444 -- annotation analysis.
16446 if Nkind (Nod) in N_Generic_Package_Declaration
16447 | N_Generic_Subprogram_Declaration
16448 | N_Package_Body
16449 | N_Package_Body_Stub
16450 | N_Subprogram_Body
16451 | N_Subprogram_Body_Stub
16452 then
16453 -- Since the capture of global references is done on the
16454 -- unanalyzed generic template, there is no information around
16455 -- to infer the context. Use the Associated_Entity linkages to
16456 -- peek into the analyzed generic copy and determine what the
16457 -- template corresponds to.
16459 if Nod = Templ then
16460 return
16461 Is_Generic_Declaration_Or_Body
16462 (Unit_Declaration_Node
16463 (Get_Associated_Entity (Defining_Entity (Nod))));
16465 -- Otherwise the generic unit being processed is not the top
16466 -- level template. It is safe to capture of global references
16467 -- within the generic unit because at this point the top level
16468 -- copy is fully analyzed.
16470 else
16471 return False;
16472 end if;
16474 -- Otherwise capture the global references without interference
16476 else
16477 return False;
16478 end if;
16479 end Requires_Delayed_Save;
16481 ----------------------------------
16482 -- Save_References_In_Aggregate --
16483 ----------------------------------
16485 procedure Save_References_In_Aggregate (N : Node_Id) is
16486 Nam : Node_Id;
16487 Qual : Node_Id := Empty;
16488 Typ : Entity_Id := Empty;
16490 begin
16491 N2 := Get_Associated_Node (N);
16493 if Present (N2) then
16494 Typ := Etype (N2);
16496 -- In an instance within a generic, use the name of the actual
16497 -- and not the original generic parameter. If the actual is
16498 -- global in the current generic it must be preserved for its
16499 -- instantiation.
16501 if Parent_Kind (Typ) = N_Subtype_Declaration
16502 and then Present (Generic_Parent_Type (Parent (Typ)))
16503 then
16504 Typ := Base_Type (Typ);
16505 Set_Etype (N2, Typ);
16506 end if;
16507 end if;
16509 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16510 Set_Associated_Node (N, Empty);
16512 -- For a full aggregate, if the type is local but is a derived
16513 -- tagged type of a global ancestor, we will need to have the
16514 -- full view of this global ancestor available in the instance
16515 -- in order to analyze the full aggregate.
16517 if Present (N2)
16518 and then Nkind (N2) = N_Aggregate
16519 and then Present (Typ)
16520 and then Is_Tagged_Type (Typ)
16521 and then Is_Derived_Type (Typ)
16522 then
16523 declare
16524 Root_Typ : constant Entity_Id := Root_Type (Typ);
16526 Parent_Typ : Entity_Id := Typ;
16528 begin
16529 loop
16530 Parent_Typ := Etype (Parent_Typ);
16532 if Is_Global (Parent_Typ) then
16533 Set_Ancestor_Type (N, Parent_Typ);
16534 exit;
16535 end if;
16537 exit when Parent_Typ = Root_Typ;
16538 end loop;
16539 end;
16540 end if;
16542 -- If the aggregate is an actual in a call, it has been
16543 -- resolved in the current context, to some local type. The
16544 -- enclosing call may have been disambiguated by the aggregate,
16545 -- and this disambiguation might fail at instantiation time
16546 -- because the type to which the aggregate did resolve is not
16547 -- preserved. In order to preserve some of this information,
16548 -- wrap the aggregate in a qualified expression, using the id
16549 -- of its type. For further disambiguation we qualify the type
16550 -- name with its scope (if visible and not hidden by a local
16551 -- homograph) because both id's will have corresponding
16552 -- entities in an instance. This resolves most of the problems
16553 -- with missing type information on aggregates in instances.
16555 if Present (N2)
16556 and then Nkind (N2) = Nkind (N)
16557 and then Nkind (Parent (N2)) in N_Subprogram_Call
16558 and then Present (Typ)
16559 and then Comes_From_Source (Typ)
16560 then
16561 Nam := Make_Identifier (Loc, Chars (Typ));
16563 if Is_Immediately_Visible (Scope (Typ))
16564 and then
16565 (not In_Open_Scopes (Scope (Typ))
16566 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16567 then
16568 Nam :=
16569 Make_Selected_Component (Loc,
16570 Prefix =>
16571 Make_Identifier (Loc, Chars (Scope (Typ))),
16572 Selector_Name => Nam);
16573 end if;
16575 Qual :=
16576 Make_Qualified_Expression (Loc,
16577 Subtype_Mark => Nam,
16578 Expression => Relocate_Node (N));
16579 end if;
16581 -- For a full aggregate, if the type is global and a derived
16582 -- tagged type, we will also need to have the full view of its
16583 -- ancestor available in the instance in order to analyze the
16584 -- full aggregate.
16586 elsif Present (N2)
16587 and then Nkind (N2) = N_Aggregate
16588 and then Present (Typ)
16589 and then Is_Tagged_Type (Typ)
16590 and then Is_Derived_Type (Typ)
16591 then
16592 Set_Ancestor_Type (N, Etype (Typ));
16593 end if;
16595 if Nkind (N) = N_Aggregate then
16596 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16598 elsif Nkind (N) = N_Extension_Aggregate then
16599 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16601 else
16602 pragma Assert (False);
16603 end if;
16605 Save_Global_Descendant (Union_Id (Expressions (N)));
16606 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16607 Save_Global_Descendant (Union_Id (Etype (N)));
16609 if Present (Qual) then
16610 Rewrite (N, Qual);
16611 end if;
16612 end Save_References_In_Aggregate;
16614 ----------------------------------------------
16615 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16616 ----------------------------------------------
16618 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16619 begin
16620 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16621 Reset_Entity (N);
16623 elsif Nkind (N) = N_Operator_Symbol
16624 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16625 then
16626 Change_Operator_Symbol_To_String_Literal (N);
16627 end if;
16628 end Save_References_In_Char_Lit_Or_Op_Symbol;
16630 ------------------------------------
16631 -- Save_References_In_Descendants --
16632 ------------------------------------
16634 procedure Save_References_In_Descendants (N : Node_Id) is
16635 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16636 begin
16637 Walk (N);
16638 end Save_References_In_Descendants;
16640 -----------------------------------
16641 -- Save_References_In_Identifier --
16642 -----------------------------------
16644 procedure Save_References_In_Identifier (N : Node_Id) is
16645 begin
16646 -- The node did not undergo a transformation
16648 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16649 -- If this is a discriminant reference, always save it.
16650 -- It is used in the instance to find the corresponding
16651 -- discriminant positionally rather than by name.
16653 Set_Original_Discriminant
16654 (N, Original_Discriminant (Get_Associated_Node (N)));
16656 Reset_Entity (N);
16658 -- The analysis of the generic copy transformed the identifier
16659 -- into another construct. Propagate the changes to the template.
16661 else
16662 N2 := Get_Associated_Node (N);
16664 -- The identifier denotes a call to a parameterless function.
16665 -- Mark the node as resolved when the function is external.
16667 if Nkind (N2) = N_Function_Call then
16668 E := Entity (Name (N2));
16670 if Present (E) and then Is_Global (E) then
16671 Set_Global_Type (N, N2);
16672 else
16673 Set_Associated_Node (N, Empty);
16674 Set_Etype (N, Empty);
16675 end if;
16677 -- The identifier denotes a named number that was constant
16678 -- folded. Preserve the original name for ASIS and undo the
16679 -- constant folding which will be repeated in the instance.
16680 -- Is this still needed???
16682 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16683 and then Is_Entity_Name (Original_Node (N2))
16684 then
16685 Set_Associated_Node (N, Original_Node (N2));
16686 Reset_Entity (N);
16688 -- The identifier resolved to a string literal. Propagate this
16689 -- information to the generic template.
16691 elsif Nkind (N2) = N_String_Literal then
16692 Rewrite (N, New_Copy (N2));
16694 -- The identifier is rewritten as a dereference if it is the
16695 -- prefix of an implicit dereference. Preserve the original
16696 -- tree as the analysis of the instance will expand the node
16697 -- again, but preserve the resolved entity if it is global.
16699 elsif Nkind (N2) = N_Explicit_Dereference then
16700 if Is_Entity_Name (Prefix (N2))
16701 and then Present (Entity (Prefix (N2)))
16702 and then Is_Global (Entity (Prefix (N2)))
16703 then
16704 Set_Associated_Node (N, Prefix (N2));
16705 Set_Global_Type (N, Prefix (N2));
16707 elsif Nkind (Prefix (N2)) = N_Function_Call
16708 and then Is_Entity_Name (Name (Prefix (N2)))
16709 and then Present (Entity (Name (Prefix (N2))))
16710 and then Is_Global (Entity (Name (Prefix (N2))))
16711 then
16712 Rewrite (N,
16713 Make_Explicit_Dereference (Loc,
16714 Prefix =>
16715 Make_Function_Call (Loc,
16716 Name =>
16717 New_Occurrence_Of
16718 (Entity (Name (Prefix (N2))), Loc))));
16719 Set_Associated_Node
16720 (Name (Prefix (N)), Name (Prefix (N2)));
16721 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
16723 else
16724 Set_Associated_Node (N, Empty);
16725 Set_Etype (N, Empty);
16726 end if;
16728 -- The subtype mark of a nominally unconstrained object is
16729 -- rewritten as a subtype indication using the bounds of the
16730 -- expression. Recover the original subtype mark.
16732 elsif Nkind (N2) = N_Subtype_Indication
16733 and then Is_Entity_Name (Original_Node (N2))
16734 then
16735 Set_Associated_Node (N, Original_Node (N2));
16736 Reset_Entity (N);
16737 end if;
16738 end if;
16739 end Save_References_In_Identifier;
16741 ---------------------------------
16742 -- Save_References_In_Operator --
16743 ---------------------------------
16745 procedure Save_References_In_Operator (N : Node_Id) is
16746 begin
16747 N2 := Get_Associated_Node (N);
16749 -- The node did not undergo a transformation
16751 if Nkind (N) = Nkind (N2) then
16752 if Nkind (N) = N_Op_Concat then
16753 Set_Is_Component_Left_Opnd
16754 (N, Is_Component_Left_Opnd (N2));
16755 Set_Is_Component_Right_Opnd
16756 (N, Is_Component_Right_Opnd (N2));
16757 end if;
16759 Reset_Entity (N);
16761 -- The analysis of the generic copy transformed the operator into
16762 -- some other construct. Propagate the changes to the template if
16763 -- applicable.
16765 else
16766 -- The operator resoved to a function call
16768 if Nkind (N2) = N_Function_Call then
16770 -- Add explicit qualifications in the generic template for
16771 -- all operands of universal type. This aids resolution by
16772 -- preserving the actual type of a literal or an attribute
16773 -- that yields a universal result.
16775 Qualify_Universal_Operands (N, N2);
16777 E := Entity (Name (N2));
16779 if Present (E) and then Is_Global (E) then
16780 Set_Global_Type (N, N2);
16781 else
16782 Set_Associated_Node (N, Empty);
16783 Set_Etype (N, Empty);
16784 end if;
16786 -- The operator was folded into a literal
16788 elsif Nkind (N2) in N_Integer_Literal
16789 | N_Real_Literal
16790 | N_String_Literal
16791 then
16792 if Present (Original_Node (N2))
16793 and then Nkind (Original_Node (N2)) = Nkind (N)
16794 then
16795 -- Operation was constant-folded. Whenever possible,
16796 -- recover semantic information from unfolded node.
16797 -- This was initially done for ASIS but is apparently
16798 -- needed also for e.g. compiling a-nbnbin.adb.
16800 Set_Associated_Node (N, Original_Node (N2));
16802 if Nkind (N) = N_Op_Concat then
16803 Set_Is_Component_Left_Opnd (N,
16804 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16805 Set_Is_Component_Right_Opnd (N,
16806 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16807 end if;
16809 Reset_Entity (N);
16811 -- Propagate the constant folding back to the template
16813 else
16814 Rewrite (N, New_Copy (N2));
16815 Set_Analyzed (N, False);
16816 end if;
16818 -- The operator was folded into an enumeration literal. Retain
16819 -- the entity to avoid spurious ambiguities if it is overloaded
16820 -- at the point of instantiation or inlining.
16822 elsif Nkind (N2) = N_Identifier
16823 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16824 then
16825 Rewrite (N, New_Copy (N2));
16826 Set_Analyzed (N, False);
16827 end if;
16828 end if;
16830 -- Complete the operands check if node has not been constant
16831 -- folded.
16833 if Nkind (N) in N_Op then
16834 Save_Entity_Descendants (N);
16835 end if;
16836 end Save_References_In_Operator;
16838 -------------------------------
16839 -- Save_References_In_Pragma --
16840 -------------------------------
16842 procedure Save_References_In_Pragma (Prag : Node_Id) is
16843 Context : Node_Id;
16844 Do_Save : Boolean := True;
16846 begin
16847 -- Do not save global references in pragmas generated from aspects
16848 -- because the pragmas will be regenerated at instantiation time.
16850 if From_Aspect_Specification (Prag) then
16851 Do_Save := False;
16853 -- The capture of global references within contract-related source
16854 -- pragmas associated with generic packages, subprograms or their
16855 -- respective bodies must be delayed due to timing of annotation
16856 -- analysis. Global references are still captured in routine
16857 -- Save_Global_References_In_Contract.
16859 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16860 if Is_Package_Contract_Annotation (Prag) then
16861 Context := Find_Related_Package_Or_Body (Prag);
16862 else
16863 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16864 Context := Find_Related_Declaration_Or_Body (Prag);
16865 end if;
16867 -- The use of Original_Node accounts for the case when the
16868 -- related context is generic template.
16870 if Requires_Delayed_Save (Original_Node (Context)) then
16871 Do_Save := False;
16872 end if;
16873 end if;
16875 -- For all other cases, save all global references within the
16876 -- descendants, but skip the following semantic fields:
16877 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16879 if Do_Save then
16880 Save_Global_Descendant
16881 (Union_Id (Pragma_Argument_Associations (N)));
16882 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16883 end if;
16884 end Save_References_In_Pragma;
16886 -- Start of processing for Save_References
16888 begin
16889 if N = Empty then
16890 null;
16892 -- Aggregates
16894 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16895 Save_References_In_Aggregate (N);
16897 -- Character literals, operator symbols
16899 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16900 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16902 -- Defining identifiers
16904 elsif Nkind (N) in N_Entity then
16905 null;
16907 -- Identifiers
16909 elsif Nkind (N) = N_Identifier then
16910 Save_References_In_Identifier (N);
16912 -- Operators
16914 elsif Nkind (N) in N_Op then
16915 Save_References_In_Operator (N);
16917 -- Pragmas
16919 elsif Nkind (N) = N_Pragma then
16920 Save_References_In_Pragma (N);
16922 elsif Nkind (N) = N_Aspect_Specification then
16923 declare
16924 P : constant Node_Id := Parent (N);
16925 Expr : Node_Id;
16926 begin
16928 if Permits_Aspect_Specifications (P) then
16930 -- The capture of global references within aspects
16931 -- associated with generic packages, subprograms or
16932 -- their bodies must be delayed due to timing of
16933 -- annotation analysis. Global references are still
16934 -- captured in routine Save_Global_References_In_Contract.
16936 if Requires_Delayed_Save (Original_Node (P)) then
16937 null;
16939 -- Otherwise save all global references within the
16940 -- aspects
16942 else
16943 Expr := Expression (N);
16945 if Present (Expr) then
16946 Save_Global_References (Expr);
16947 end if;
16948 end if;
16949 end if;
16950 end;
16952 -- Do not walk the node pointed to by Label_Construct twice
16954 elsif Nkind (N) = N_Implicit_Label_Declaration then
16955 null;
16957 else
16958 Save_References_In_Descendants (N);
16959 end if;
16961 end Save_References;
16963 ---------------------
16964 -- Set_Global_Type --
16965 ---------------------
16967 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
16968 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
16969 Typ : constant Entity_Id :=
16970 (if Comparison then Compare_Type (N2) else Etype (N2));
16972 begin
16973 -- For a comparison (or equality) operator, the Etype is Boolean, so
16974 -- it is always global. But the type subject to the Has_Private_View
16975 -- processing is the Compare_Type, so we must specifically check it.
16977 if Comparison then
16978 Set_Etype (N, Etype (N2));
16980 if not Is_Global (Typ) then
16981 return;
16982 end if;
16984 Set_Compare_Type (N, Typ);
16986 else
16987 Set_Etype (N, Typ);
16988 end if;
16990 -- If the entity of N is not the associated node, this is a
16991 -- nested generic and it has an associated node as well, whose
16992 -- type is already the full view (see below). Indicate that the
16993 -- original node has a private view.
16995 if Entity (N) /= N2 then
16996 if Has_Private_View (Entity (N)) then
16997 Set_Has_Private_View (N);
16998 end if;
17000 if Has_Secondary_Private_View (Entity (N)) then
17001 Set_Has_Secondary_Private_View (N);
17002 end if;
17003 end if;
17005 -- If not a private type, deal with a secondary private view
17007 if not Is_Private_Type (Typ) then
17008 if (Is_Access_Type (Typ)
17009 and then Is_Private_Type (Designated_Type (Typ)))
17010 or else (Is_Array_Type (Typ)
17011 and then
17012 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17013 then
17014 Set_Has_Secondary_Private_View (N);
17015 end if;
17017 -- If it is a derivation of a private type in a context where no
17018 -- full view is needed, nothing to do either.
17020 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17021 null;
17023 -- Otherwise mark the type for flipping and set the full view on N2
17024 -- when available, which is necessary for Check_Private_View to swap
17025 -- back the views in case the full declaration of Typ is visible in
17026 -- the instantiation context. Note that this will be problematic if
17027 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17029 else
17030 Set_Has_Private_View (N);
17032 if Present (Full_View (Typ)) then
17033 if Comparison then
17034 Set_Compare_Type (N2, Full_View (Typ));
17035 else
17036 Set_Etype (N2, Full_View (Typ));
17037 end if;
17038 end if;
17039 end if;
17041 if Is_Floating_Point_Type (Typ)
17042 and then Has_Dimension_System (Typ)
17043 then
17044 Copy_Dimensions (N2, N);
17045 end if;
17046 end Set_Global_Type;
17048 -- Start of processing for Save_Global_References
17050 begin
17051 Gen_Scope := Current_Scope;
17053 -- If the generic unit is a child unit, references to entities in the
17054 -- parent are treated as local, because they will be resolved anew in
17055 -- the context of the instance of the parent.
17057 while Is_Child_Unit (Gen_Scope)
17058 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17059 loop
17060 Gen_Scope := Scope (Gen_Scope);
17061 end loop;
17063 Save_References (Templ);
17064 end Save_Global_References;
17066 ---------------------------------------
17067 -- Save_Global_References_In_Aspects --
17068 ---------------------------------------
17070 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17071 Asp : Node_Id;
17072 Expr : Node_Id;
17074 begin
17075 Asp := First (Aspect_Specifications (N));
17076 while Present (Asp) loop
17077 Expr := Expression (Asp);
17079 if Present (Expr) then
17080 Save_Global_References (Expr);
17081 end if;
17083 Next (Asp);
17084 end loop;
17085 end Save_Global_References_In_Aspects;
17087 ------------------------------------------
17088 -- Set_Copied_Sloc_For_Inherited_Pragma --
17089 ------------------------------------------
17091 procedure Set_Copied_Sloc_For_Inherited_Pragma
17092 (N : Node_Id;
17093 E : Entity_Id)
17095 begin
17096 Create_Instantiation_Source (N, E,
17097 Inlined_Body => False,
17098 Inherited_Pragma => True,
17099 Factor => S_Adjustment);
17100 end Set_Copied_Sloc_For_Inherited_Pragma;
17102 --------------------------------------
17103 -- Set_Copied_Sloc_For_Inlined_Body --
17104 --------------------------------------
17106 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
17107 begin
17108 Create_Instantiation_Source (N, E,
17109 Inlined_Body => True,
17110 Inherited_Pragma => False,
17111 Factor => S_Adjustment);
17112 end Set_Copied_Sloc_For_Inlined_Body;
17114 ---------------------
17115 -- Set_Instance_Of --
17116 ---------------------
17118 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
17119 begin
17120 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
17121 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
17122 Generic_Renamings.Increment_Last;
17123 end Set_Instance_Of;
17125 --------------------
17126 -- Set_Next_Assoc --
17127 --------------------
17129 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
17130 begin
17131 Generic_Renamings.Table (E).Next_In_HTable := Next;
17132 end Set_Next_Assoc;
17134 -------------------
17135 -- Start_Generic --
17136 -------------------
17138 procedure Start_Generic is
17139 begin
17140 -- ??? More things could be factored out in this routine.
17141 -- Should probably be done at a later stage.
17143 Generic_Flags.Append (Inside_A_Generic);
17144 Inside_A_Generic := True;
17146 Expander_Mode_Save_And_Set (False);
17147 end Start_Generic;
17149 ----------------------
17150 -- Set_Instance_Env --
17151 ----------------------
17153 -- WARNING: This routine manages SPARK regions
17155 procedure Set_Instance_Env
17156 (Gen_Unit : Entity_Id;
17157 Act_Unit : Entity_Id)
17159 Saved_AE : constant Boolean := Assertions_Enabled;
17160 Saved_CPL : constant Node_Id := Check_Policy_List;
17161 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
17162 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
17163 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
17165 begin
17166 -- Regardless of the current mode, predefined units are analyzed in the
17167 -- most current Ada mode, and earlier version Ada checks do not apply
17168 -- to predefined units. Nothing needs to be done for non-internal units.
17169 -- These are always analyzed in the current mode.
17171 if In_Internal_Unit (Gen_Unit) then
17173 -- The following call resets all configuration attributes to default
17174 -- or the xxx_Config versions of the attributes when the current sem
17175 -- unit is the main unit. At the same time, internal units must also
17176 -- inherit certain configuration attributes from their context. It
17177 -- is unclear what these two sets are.
17179 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17181 -- Reinstall relevant configuration attributes of the context
17183 Assertions_Enabled := Saved_AE;
17184 Check_Policy_List := Saved_CPL;
17185 Dynamic_Elaboration_Checks := Saved_DEC;
17187 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17188 end if;
17190 Current_Instantiated_Parent :=
17191 (Gen_Id => Gen_Unit,
17192 Act_Id => Act_Unit,
17193 Next_In_HTable => Assoc_Null);
17194 end Set_Instance_Env;
17196 -----------------
17197 -- Switch_View --
17198 -----------------
17200 procedure Switch_View (T : Entity_Id) is
17201 BT : constant Entity_Id := Base_Type (T);
17202 Priv_Elmt : Elmt_Id := No_Elmt;
17203 Priv_Sub : Entity_Id;
17205 begin
17206 -- T may be private but its base type may have been exchanged through
17207 -- some other occurrence, in which case there is nothing to switch
17208 -- besides T itself. Note that a private dependent subtype of a private
17209 -- type might not have been switched even if the base type has been,
17210 -- because of the last branch of Check_Private_View (see comment there).
17212 if not Is_Private_Type (BT) then
17213 Prepend_Elmt (Full_View (T), Exchanged_Views);
17214 Exchange_Declarations (T);
17215 return;
17216 end if;
17218 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17220 if Present (Full_View (BT)) then
17221 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17222 Exchange_Declarations (BT);
17223 end if;
17225 while Present (Priv_Elmt) loop
17226 Priv_Sub := Node (Priv_Elmt);
17228 if Present (Full_View (Priv_Sub)) then
17229 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17230 Exchange_Declarations (Priv_Sub);
17231 end if;
17233 Next_Elmt (Priv_Elmt);
17234 end loop;
17235 end Switch_View;
17237 -----------------
17238 -- True_Parent --
17239 -----------------
17241 function True_Parent (N : Node_Id) return Node_Id is
17242 begin
17243 if Nkind (Parent (N)) = N_Subunit then
17244 return Parent (Corresponding_Stub (Parent (N)));
17245 else
17246 return Parent (N);
17247 end if;
17248 end True_Parent;
17250 -----------------------------
17251 -- Valid_Default_Attribute --
17252 -----------------------------
17254 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17255 Attr_Id : constant Attribute_Id :=
17256 Get_Attribute_Id (Attribute_Name (Def));
17257 T : constant Entity_Id := Entity (Prefix (Def));
17258 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17259 F : Entity_Id;
17260 Num_F : Nat;
17261 OK : Boolean;
17263 begin
17264 if No (T) or else T = Any_Id then
17265 return;
17266 end if;
17268 Num_F := 0;
17269 F := First_Formal (Nam);
17270 while Present (F) loop
17271 Num_F := Num_F + 1;
17272 Next_Formal (F);
17273 end loop;
17275 case Attr_Id is
17276 when Attribute_Adjacent
17277 | Attribute_Ceiling
17278 | Attribute_Copy_Sign
17279 | Attribute_Floor
17280 | Attribute_Fraction
17281 | Attribute_Machine
17282 | Attribute_Model
17283 | Attribute_Remainder
17284 | Attribute_Rounding
17285 | Attribute_Unbiased_Rounding
17287 OK := Is_Fun
17288 and then Num_F = 1
17289 and then Is_Floating_Point_Type (T);
17291 when Attribute_Image
17292 | Attribute_Pred
17293 | Attribute_Succ
17294 | Attribute_Value
17295 | Attribute_Wide_Image
17296 | Attribute_Wide_Value
17298 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17300 when Attribute_Max
17301 | Attribute_Min
17303 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17305 when Attribute_Input =>
17306 OK := (Is_Fun and then Num_F = 1);
17308 when Attribute_Output
17309 | Attribute_Put_Image
17310 | Attribute_Read
17311 | Attribute_Write
17313 OK := not Is_Fun and then Num_F = 2;
17315 when others =>
17316 OK := False;
17317 end case;
17319 if not OK then
17320 Error_Msg_N
17321 ("attribute reference has wrong profile for subprogram", Def);
17322 end if;
17323 end Valid_Default_Attribute;
17325 ----------------------------------
17326 -- Validate_Formal_Type_Default --
17327 ----------------------------------
17329 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17330 Default : constant Node_Id :=
17331 Default_Subtype_Mark (Original_Node (Decl));
17332 Formal : constant Entity_Id := Defining_Identifier (Decl);
17334 Def_Sub : Entity_Id; -- Default subtype mark
17335 Type_Def : Node_Id;
17337 procedure Check_Discriminated_Formal;
17338 -- Check that discriminants of default for private or incomplete
17339 -- type match those of formal type.
17341 function Reference_Formal (N : Node_Id) return Traverse_Result;
17342 -- Check whether formal type definition mentions a previous formal
17343 -- type of the same generic.
17345 ----------------------
17346 -- Reference_Formal --
17347 ----------------------
17349 function Reference_Formal (N : Node_Id) return Traverse_Result is
17350 begin
17351 if Is_Entity_Name (N)
17352 and then Scope (Entity (N)) = Current_Scope
17353 then
17354 return Abandon;
17355 else
17356 return OK;
17357 end if;
17358 end Reference_Formal;
17360 function Depends_On_Other_Formals is
17361 new Traverse_Func (Reference_Formal);
17363 function Default_Subtype_Matches
17364 (Gen_T, Def_T : Entity_Id) return Boolean;
17366 procedure Validate_Array_Type_Default;
17367 -- Verify that dimension, indices, and component types of default
17368 -- are compatible with formal array type definition.
17370 procedure Validate_Derived_Type_Default;
17371 -- Verify that ancestor and progenitor types match.
17373 ---------------------------------
17374 -- Check_Discriminated_Formal --
17375 ---------------------------------
17377 procedure Check_Discriminated_Formal is
17378 Formal_Discr : Entity_Id;
17379 Actual_Discr : Entity_Id;
17380 Formal_Subt : Entity_Id;
17382 begin
17383 if Has_Discriminants (Formal) then
17384 if not Has_Discriminants (Def_Sub) then
17385 Error_Msg_NE
17386 ("default for & must have discriminants", Default, Formal);
17388 elsif Is_Constrained (Def_Sub) then
17389 Error_Msg_NE
17390 ("default for & must be unconstrained", Default, Formal);
17392 else
17393 Formal_Discr := First_Discriminant (Formal);
17394 Actual_Discr := First_Discriminant (Def_Sub);
17395 while Formal_Discr /= Empty loop
17396 if Actual_Discr = Empty then
17397 Error_Msg_N
17398 ("discriminants on Formal do not match formal",
17399 Default);
17400 end if;
17402 Formal_Subt := Etype (Formal_Discr);
17404 -- Access discriminants match if designated types do
17406 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17407 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17408 E_Anonymous_Access_Type
17409 and then
17410 Base_Type
17411 (Designated_Type (Base_Type (Formal_Subt))) =
17412 Base_Type
17413 (Designated_Type (Base_Type (Etype (Actual_Discr))))
17414 and then
17415 Subtypes_Statically_Match
17416 (Designated_Type (Base_Type (Formal_Subt)),
17417 Designated_Type (Base_Type (Etype (Actual_Discr))))
17418 then
17419 null;
17421 elsif Base_Type (Formal_Subt) /=
17422 Base_Type (Etype (Actual_Discr))
17423 then
17424 Error_Msg_N
17425 ("types of discriminants of default must match formal",
17426 Default);
17428 elsif not Subtypes_Statically_Match
17429 (Formal_Subt, Etype (Actual_Discr))
17430 and then Ada_Version >= Ada_95
17431 then
17432 Error_Msg_N
17433 ("subtypes of discriminants of default "
17434 & "must match formal",
17435 Default);
17436 end if;
17438 Next_Discriminant (Formal_Discr);
17439 Next_Discriminant (Actual_Discr);
17440 end loop;
17442 if Actual_Discr /= Empty then
17443 Error_Msg_NE
17444 ("discriminants on default do not match formal",
17445 Default, Formal);
17446 end if;
17447 end if;
17448 end if;
17449 end Check_Discriminated_Formal;
17451 ---------------------------
17452 -- Default_Subtype_Matches --
17453 ---------------------------
17455 function Default_Subtype_Matches
17456 (Gen_T, Def_T : Entity_Id) return Boolean
17458 begin
17459 -- Check that the base types, root types (when dealing with class
17460 -- wide types), or designated types (when dealing with anonymous
17461 -- access types) of Gen_T and Def_T are statically matching subtypes.
17463 return (Base_Type (Gen_T) = Base_Type (Def_T)
17464 and then Subtypes_Statically_Match (Gen_T, Def_T))
17466 or else (Is_Class_Wide_Type (Gen_T)
17467 and then Is_Class_Wide_Type (Def_T)
17468 and then Default_Subtype_Matches
17469 (Root_Type (Gen_T), Root_Type (Def_T)))
17471 or else (Is_Anonymous_Access_Type (Gen_T)
17472 and then Ekind (Def_T) = Ekind (Gen_T)
17473 and then Subtypes_Statically_Match
17474 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17476 end Default_Subtype_Matches;
17478 ----------------------------------
17479 -- Validate_Array_Type_Default --
17480 ----------------------------------
17482 procedure Validate_Array_Type_Default is
17483 I1, I2 : Node_Id;
17484 T2 : Entity_Id;
17485 begin
17486 if not Is_Array_Type (Def_Sub) then
17487 Error_Msg_NE ("default for& must be an array type ",
17488 Default, Formal);
17489 return;
17491 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17492 or else Is_Constrained (Def_Sub) /=
17493 Is_Constrained (Formal)
17494 then
17495 Error_Msg_NE ("default array type does not match&",
17496 Default, Formal);
17497 return;
17498 end if;
17500 I1 := First_Index (Formal);
17501 I2 := First_Index (Def_Sub);
17502 for J in 1 .. Number_Dimensions (Formal) loop
17504 -- If the indexes of the actual were given by a subtype_mark,
17505 -- the index was transformed into a range attribute. Retrieve
17506 -- the original type mark for checking.
17508 if Is_Entity_Name (Original_Node (I2)) then
17509 T2 := Entity (Original_Node (I2));
17510 else
17511 T2 := Etype (I2);
17512 end if;
17514 if not Subtypes_Statically_Match (Etype (I1), T2) then
17515 Error_Msg_NE
17516 ("index types of default do not match those of formal &",
17517 Default, Formal);
17518 end if;
17520 Next_Index (I1);
17521 Next_Index (I2);
17522 end loop;
17524 if not Default_Subtype_Matches
17525 (Component_Type (Formal), Component_Type (Def_Sub))
17526 then
17527 Error_Msg_NE
17528 ("component subtype of default does not match that of formal &",
17529 Default, Formal);
17530 end if;
17532 if Has_Aliased_Components (Formal)
17533 and then not Has_Aliased_Components (Default)
17534 then
17535 Error_Msg_NE
17536 ("default must have aliased components to match formal type &",
17537 Default, Formal);
17538 end if;
17539 end Validate_Array_Type_Default;
17541 -----------------------------------
17542 -- Validate_Derived_Type_Default --
17543 -----------------------------------
17545 procedure Validate_Derived_Type_Default is
17546 begin
17547 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17548 Error_Msg_NE ("default must be a descendent of&",
17549 Default, Etype (Formal));
17550 end if;
17552 if Has_Interfaces (Formal) then
17553 if not Has_Interfaces (Def_Sub) then
17554 Error_Msg_NE
17555 ("default must implement all interfaces of formal&",
17556 Default, Formal);
17558 else
17559 declare
17560 Iface : Node_Id;
17561 Iface_Ent : Entity_Id;
17563 begin
17564 Iface := First (Abstract_Interface_List (Formal));
17566 while Present (Iface) loop
17567 Iface_Ent := Entity (Iface);
17569 if Is_Ancestor (Iface_Ent, Def_Sub)
17570 or else Is_Progenitor (Iface_Ent, Def_Sub)
17571 then
17572 null;
17574 else
17575 Error_Msg_NE
17576 ("Default must implement interface&",
17577 Default, Etype (Iface));
17578 end if;
17580 Next (Iface);
17581 end loop;
17582 end;
17583 end if;
17584 end if;
17585 end Validate_Derived_Type_Default;
17587 -- Start of processing for Validate_Formal_Type_Default
17589 begin
17590 Analyze (Default);
17591 if not Is_Entity_Name (Default)
17592 or else not Is_Type (Entity (Default))
17593 then
17594 Error_Msg_N
17595 ("Expect type name for default of formal type", Default);
17596 return;
17597 else
17598 Def_Sub := Entity (Default);
17599 end if;
17601 -- Formal derived_type declarations are transformed into full
17602 -- type declarations or Private_Type_Extensions for ease of processing.
17604 if Nkind (Decl) = N_Full_Type_Declaration then
17605 Type_Def := Type_Definition (Decl);
17607 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17608 Type_Def := Subtype_Indication (Decl);
17610 else
17611 Type_Def := Formal_Type_Definition (Decl);
17612 end if;
17614 if Depends_On_Other_Formals (Type_Def) = Abandon
17615 and then Scope (Def_Sub) /= Current_Scope
17616 then
17617 Error_Msg_N ("default of formal type that depends on "
17618 & "other formals must be a previous formal type", Default);
17619 return;
17621 elsif Def_Sub = Formal then
17622 Error_Msg_N
17623 ("default for formal type cannot be formal itsef", Default);
17624 return;
17625 end if;
17627 case Nkind (Type_Def) is
17629 when N_Formal_Private_Type_Definition =>
17630 if (Is_Abstract_Type (Formal)
17631 and then not Is_Abstract_Type (Def_Sub))
17632 or else (Is_Limited_Type (Formal)
17633 and then not Is_Limited_Type (Def_Sub))
17634 then
17635 Error_Msg_NE
17636 ("default for private type$ does not match",
17637 Default, Formal);
17638 end if;
17640 Check_Discriminated_Formal;
17642 when N_Formal_Derived_Type_Definition =>
17643 Check_Discriminated_Formal;
17644 Validate_Derived_Type_Default;
17646 when N_Formal_Incomplete_Type_Definition =>
17647 if Is_Tagged_Type (Formal)
17648 and then not Is_Tagged_Type (Def_Sub)
17649 then
17650 Error_Msg_NE
17651 ("default for & must be a tagged type", Default, Formal);
17652 end if;
17654 Check_Discriminated_Formal;
17656 when N_Formal_Discrete_Type_Definition =>
17657 if not Is_Discrete_Type (Def_Sub) then
17658 Error_Msg_NE ("default for& must be a discrete type",
17659 Default, Formal);
17660 end if;
17662 when N_Formal_Signed_Integer_Type_Definition =>
17663 if not Is_Integer_Type (Def_Sub) then
17664 Error_Msg_NE ("default for& must be a discrete type",
17665 Default, Formal);
17666 end if;
17668 when N_Formal_Modular_Type_Definition =>
17669 if not Is_Modular_Integer_Type (Def_Sub) then
17670 Error_Msg_NE ("default for& must be a modular_integer Type",
17671 Default, Formal);
17672 end if;
17674 when N_Formal_Floating_Point_Definition =>
17675 if not Is_Floating_Point_Type (Def_Sub) then
17676 Error_Msg_NE ("default for& must be a floating_point type",
17677 Default, Formal);
17678 end if;
17680 when N_Formal_Ordinary_Fixed_Point_Definition =>
17681 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17682 Error_Msg_NE ("default for& must be "
17683 & "an ordinary_fixed_point type ",
17684 Default, Formal);
17685 end if;
17687 when N_Formal_Decimal_Fixed_Point_Definition =>
17688 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17689 Error_Msg_NE ("default for& must be "
17690 & "an Decimal_fixed_point type ",
17691 Default, Formal);
17692 end if;
17694 when N_Array_Type_Definition =>
17695 Validate_Array_Type_Default;
17697 when N_Access_Function_Definition |
17698 N_Access_Procedure_Definition =>
17699 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17700 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17701 Default, Formal);
17702 end if;
17703 Check_Subtype_Conformant
17704 (Designated_Type (Formal), Designated_Type (Def_Sub));
17706 when N_Access_To_Object_Definition =>
17707 if not Is_Access_Object_Type (Def_Sub) then
17708 Error_Msg_NE ("default for& must be an Access_To_Object",
17709 Default, Formal);
17711 elsif not Default_Subtype_Matches
17712 (Designated_Type (Formal), Designated_Type (Def_Sub))
17713 then
17714 Error_Msg_NE ("designated type of defaul does not match "
17715 & "designated type of formal type",
17716 Default, Formal);
17717 end if;
17719 when N_Record_Definition => -- Formal interface type
17720 if not Is_Interface (Def_Sub) then
17721 Error_Msg_NE
17722 ("default for formal interface type must be an interface",
17723 Default, Formal);
17725 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17726 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17727 or else Is_Protected_Interface (Formal) /=
17728 Is_Protected_Interface (Def_Sub)
17729 or else Is_Synchronized_Interface (Formal) /=
17730 Is_Synchronized_Interface (Def_Sub)
17731 then
17732 Error_Msg_NE
17733 ("default for interface& does not match", Def_Sub, Formal);
17734 end if;
17736 when N_Derived_Type_Definition =>
17737 Validate_Derived_Type_Default;
17739 when N_Identifier => -- case of a private extension
17740 Validate_Derived_Type_Default;
17742 when N_Error =>
17743 null;
17745 when others =>
17746 raise Program_Error;
17747 end case;
17748 end Validate_Formal_Type_Default;
17750 package body Instance_Context is
17752 --------------------
17753 -- Save_And_Reset --
17754 --------------------
17756 function Save_And_Reset return Context is
17757 begin
17758 return Result : Context (0 .. Integer (Generic_Renamings.Last)) do
17759 for Index in Result'Range loop
17760 declare
17761 Indexed_Assoc : Assoc renames Generic_Renamings.Table
17762 (Assoc_Ptr (Index));
17763 Result_Pair : Binding_Pair renames Result (Index);
17764 begin
17765 -- If we have called Increment_Last but have not yet
17766 -- initialized the new last element of the table, then
17767 -- that last element might be invalid. Saving and
17768 -- restoring (especially restoring, it turns out) invalid
17769 -- values can result in exceptions if predicate checking
17770 -- is enabled, so replace invalid values with Empty.
17772 if Indexed_Assoc.Gen_Id'Valid then
17773 Result_Pair.Formal_Id := Indexed_Assoc.Gen_Id;
17774 else
17775 pragma Assert (Index = Result'Last);
17776 Result_Pair.Formal_Id := Empty;
17777 end if;
17779 if Indexed_Assoc.Act_Id'Valid then
17780 Result_Pair.Actual_Id := Indexed_Assoc.Act_Id;
17781 else
17782 pragma Assert (Index = Result'Last);
17783 Result_Pair.Actual_Id := Empty;
17784 end if;
17785 end;
17786 end loop;
17788 Generic_Renamings.Init;
17789 Generic_Renamings.Set_Last (0);
17790 Generic_Renamings_HTable.Reset;
17791 end return;
17792 end Save_And_Reset;
17794 -------------
17795 -- Restore --
17796 -------------
17798 procedure Restore (Saved : Context) is
17799 begin
17800 Generic_Renamings.Init;
17801 Generic_Renamings.Set_Last (0);
17802 Generic_Renamings_HTable.Reset;
17803 Generic_Renamings.Increment_Last;
17804 for Pair of Saved loop
17805 Set_Instance_Of (Pair.Formal_Id, Pair.Actual_Id);
17806 end loop;
17807 Generic_Renamings.Decrement_Last;
17808 end Restore;
17810 end Instance_Context;
17811 end Sem_Ch12;