ada: Rename Is_Constr_Subt_For_UN_Aliased flag
[official-gcc.git] / gcc / ada / sem_ch12.adb
blobbfb400f5642290d1f3b2c9e6bd8ea88fba62e6f6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
79 with Table;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
85 with GNAT.HTable;
87 package body Sem_Ch12 is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
109 -- | copy | | unit |
110 -- | |==============>| |
111 -- |___________| global |__________|
112 -- references | | |
113 -- | | |
114 -- .-----|--|.
115 -- | .-----|---.
116 -- | | .----------.
117 -- | | | generic |
118 -- |__| | |
119 -- |__| instance |
120 -- |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
135 -- actuals.
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
142 -- package.
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
169 -- generic ...
170 -- package Outer is
171 -- ...
172 -- type Semi_Global is ... -- global to inner.
174 -- generic ... -- 1
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
178 -- end Outer;
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
220 -- with B; with A;
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
261 -- as annotations:
263 -- package subprogram [body]
264 -- Abstract_State Always_Terminates
265 -- Initial_Condition Contract_Cases
266 -- Initializes Depends
267 -- Exceptional_Cases
268 -- Extensions_Visible
269 -- Global
270 -- package body Post
271 -- Refined_State Post_Class
272 -- Postcondition
273 -- Pre
274 -- Pre_Class
275 -- Precondition
276 -- Refined_Depends
277 -- Refined_Global
278 -- Refined_Post
279 -- Subprogram_Variant
280 -- Test_Case
282 -- Most package contract annotations utilize forward references to classify
283 -- data declared within the package [body]. Subprogram annotations then use
284 -- the classifications to further refine them. These inter dependencies are
285 -- problematic with respect to the implementation of generics because their
286 -- analysis, capture of global references and instantiation does not mesh
287 -- well with the existing mechanism.
289 -- 1) Analysis of generic contracts is carried out the same way non-generic
290 -- contracts are analyzed:
292 -- 1.1) General rule - a contract is analyzed after all related aspects
293 -- and pragmas are analyzed. This is done by routines
295 -- Analyze_Package_Body_Contract
296 -- Analyze_Package_Contract
297 -- Analyze_Subprogram_Body_Contract
298 -- Analyze_Subprogram_Contract
300 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
301 -- are processed.
303 -- 1.3) Compilation unit body - the contract is analyzed at the end of
304 -- the body declaration list.
306 -- 1.4) Package - the contract is analyzed at the end of the private or
307 -- visible declarations, prior to analyzing the contracts of any nested
308 -- packages or subprograms.
310 -- 1.5) Package body - the contract is analyzed at the end of the body
311 -- declaration list, prior to analyzing the contracts of any nested
312 -- packages or subprograms.
314 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
315 -- package or a subprogram, then its contract is analyzed at the end of
316 -- the enclosing declarations, otherwise the subprogram is a compilation
317 -- unit 1.2).
319 -- 1.7) Subprogram body - if the subprogram body is declared inside a
320 -- block, a package body or a subprogram body, then its contract is
321 -- analyzed at the end of the enclosing declarations, otherwise the
322 -- subprogram is a compilation unit 1.3).
324 -- 2) Capture of global references within contracts is done after capturing
325 -- global references within the generic template. There are two reasons for
326 -- this delay - pragma annotations are not part of the generic template in
327 -- the case of a generic subprogram declaration, and analysis of contracts
328 -- is delayed.
330 -- Contract-related source pragmas within generic templates are prepared
331 -- for delayed capture of global references by routine
333 -- Create_Generic_Contract
335 -- The routine associates these pragmas with the contract of the template.
336 -- In the case of a generic subprogram declaration, the routine creates
337 -- generic templates for the pragmas declared after the subprogram because
338 -- they are not part of the template.
340 -- generic -- template starts
341 -- procedure Gen_Proc (Input : Integer); -- template ends
342 -- pragma Precondition (Input > 0); -- requires own template
344 -- 2.1) The capture of global references with aspect specifications and
345 -- source pragmas that apply to a generic unit must be suppressed when
346 -- the generic template is being processed because the contracts have not
347 -- been analyzed yet. Any attempts to capture global references at that
348 -- point will destroy the Associated_Node linkages and leave the template
349 -- undecorated. This delay is controlled by routine
351 -- Requires_Delayed_Save
353 -- 2.2) The real capture of global references within a contract is done
354 -- after the contract has been analyzed, by routine
356 -- Save_Global_References_In_Contract
358 -- 3) The instantiation of a generic contract occurs as part of the
359 -- instantiation of the contract owner. Generic subprogram declarations
360 -- require additional processing when the contract is specified by pragmas
361 -- because the pragmas are not part of the generic template. This is done
362 -- by routine
364 -- Instantiate_Subprogram_Contract
366 --------------------------------------------------
367 -- Formal packages and partial parameterization --
368 --------------------------------------------------
370 -- When compiling a generic, a formal package is a local instantiation. If
371 -- declared with a box, its generic formals are visible in the enclosing
372 -- generic. If declared with a partial list of actuals, those actuals that
373 -- are defaulted (covered by an Others clause, or given an explicit box
374 -- initialization) are also visible in the enclosing generic, while those
375 -- that have a corresponding actual are not.
377 -- In our source model of instantiation, the same visibility must be
378 -- present in the spec and body of an instance: the names of the formals
379 -- that are defaulted must be made visible within the instance, and made
380 -- invisible (hidden) after the instantiation is complete, so that they
381 -- are not accessible outside of the instance.
383 -- In a generic, a formal package is treated like a special instantiation.
384 -- Our Ada 95 compiler handled formals with and without box in different
385 -- ways. With partial parameterization, we use a single model for both.
386 -- We create a package declaration that consists of the specification of
387 -- the generic package, and a set of declarations that map the actuals
388 -- into local renamings, just as we do for bona fide instantiations. For
389 -- defaulted parameters and formals with a box, we copy directly the
390 -- declarations of the formals into this local package. The result is a
391 -- package whose visible declarations may include generic formals. This
392 -- package is only used for type checking and visibility analysis, and
393 -- never reaches the back end, so it can freely violate the placement
394 -- rules for generic formal declarations.
396 -- The list of declarations (renamings and copies of formals) is built
397 -- by Analyze_Associations, just as for regular instantiations.
399 -- At the point of instantiation, conformance checking must be applied only
400 -- to those parameters that were specified in the formals. We perform this
401 -- checking by creating another internal instantiation, this one including
402 -- only the renamings and the formals (the rest of the package spec is not
403 -- relevant to conformance checking). We can then traverse two lists: the
404 -- list of actuals in the instance that corresponds to the formal package,
405 -- and the list of actuals produced for this bogus instantiation. We apply
406 -- the conformance rules to those actuals that are not defaulted, i.e.
407 -- which still appear as generic formals.
409 -- When we compile an instance body we must make the right parameters
410 -- visible again. The predicate Is_Generic_Formal indicates which of the
411 -- formals should have its Is_Hidden flag reset.
413 -----------------------
414 -- Local subprograms --
415 -----------------------
417 procedure Abandon_Instantiation (N : Node_Id);
418 pragma No_Return (Abandon_Instantiation);
419 -- Posts an error message "instantiation abandoned" at the indicated node
420 -- and then raises the exception Instantiation_Error to do it.
422 procedure Analyze_Formal_Array_Type
423 (T : in out Entity_Id;
424 Def : Node_Id);
425 -- A formal array type is treated like an array type declaration, and
426 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
427 -- in-out, because in the case of an anonymous type the entity is
428 -- actually created in the procedure.
430 -- The following procedures treat other kinds of formal parameters
432 procedure Analyze_Formal_Derived_Interface_Type
433 (N : Node_Id;
434 T : Entity_Id;
435 Def : Node_Id);
437 procedure Analyze_Formal_Derived_Type
438 (N : Node_Id;
439 T : Entity_Id;
440 Def : Node_Id);
442 procedure Analyze_Formal_Interface_Type
443 (N : Node_Id;
444 T : Entity_Id;
445 Def : Node_Id);
447 -- The following subprograms create abbreviated declarations for formal
448 -- scalar types. We introduce an anonymous base of the proper class for
449 -- each of them, and define the formals as constrained first subtypes of
450 -- their bases. The bounds are expressions that are non-static in the
451 -- generic.
453 procedure Analyze_Formal_Decimal_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
460 (T : Entity_Id; Def : Node_Id);
462 procedure Analyze_Formal_Private_Type
463 (N : Node_Id;
464 T : Entity_Id;
465 Def : Node_Id);
466 -- Creates a new private type, which does not require completion
468 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
469 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
471 procedure Analyze_Generic_Formal_Part (N : Node_Id);
472 -- Analyze generic formal part
474 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
475 -- Create a new access type with the given designated type
477 function Analyze_Associations
478 (I_Node : Node_Id;
479 Formals : List_Id;
480 F_Copy : List_Id) return List_Id;
481 -- At instantiation time, build the list of associations between formals
482 -- and actuals. Each association becomes a renaming declaration for the
483 -- formal entity. F_Copy is the analyzed list of formals in the generic
484 -- copy. It is used to apply legality checks to the actuals. I_Node is the
485 -- instantiation node itself.
487 procedure Analyze_Subprogram_Instantiation
488 (N : Node_Id;
489 K : Entity_Kind);
491 procedure Build_Instance_Compilation_Unit_Nodes
492 (N : Node_Id;
493 Act_Body : Node_Id;
494 Act_Decl : Node_Id);
495 -- This procedure is used in the case where the generic instance of a
496 -- subprogram body or package body is a library unit. In this case, the
497 -- original library unit node for the generic instantiation must be
498 -- replaced by the resulting generic body, and a link made to a new
499 -- compilation unit node for the generic declaration. The argument N is
500 -- the original generic instantiation. Act_Body and Act_Decl are the body
501 -- and declaration of the instance (either package body and declaration
502 -- nodes or subprogram body and declaration nodes depending on the case).
503 -- On return, the node N has been rewritten with the actual body.
505 function Build_Subprogram_Decl_Wrapper
506 (Formal_Subp : Entity_Id) return Node_Id;
507 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
508 -- At the point of instantiation these contracts apply to uses of
509 -- the actual subprogram. This is implemented by creating wrapper
510 -- subprograms instead of the renamings previously used to link
511 -- formal subprograms and the corresponding actuals. If the actual
512 -- is not an entity (e.g. an attribute reference) a renaming is
513 -- created to handle the expansion of the attribute.
515 function Build_Subprogram_Body_Wrapper
516 (Formal_Subp : Entity_Id;
517 Actual_Name : Node_Id) return Node_Id;
518 -- The body of the wrapper is a call to the actual, with the generated
519 -- pre/postconditon checks added.
521 procedure Check_Abbreviated_Instance
522 (N : Node_Id;
523 Parent_Installed : in out Boolean);
524 -- If the name of the generic unit in an abbreviated instantiation is an
525 -- expanded name, then the prefix may be an instance and the selector may
526 -- designate a child unit. If the parent is installed as a result of this
527 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
528 -- unchanged by the call.
530 -- This routine needs to be called for declaration nodes of formal objects,
531 -- types and subprograms to check whether they are the copy, present in the
532 -- visible part of the abbreviated instantiation of formal packages, of the
533 -- declaration node of their corresponding formal parameter in the template
534 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
535 -- the proper context for their analysis.
537 procedure Check_Access_Definition (N : Node_Id);
538 -- Subsidiary routine to null exclusion processing. Perform an assertion
539 -- check on Ada version and the presence of an access definition in N.
541 procedure Check_Formal_Packages (P_Id : Entity_Id);
542 -- Apply the following to all formal packages in generic associations.
543 -- Restore the visibility of the formals of the instance that are not
544 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
545 -- created for formal instances that are not defaulted.
547 procedure Check_Formal_Package_Instance
548 (Formal_Pack : Entity_Id;
549 Actual_Pack : Entity_Id);
550 -- Verify that the actuals of the actual instance match the actuals of
551 -- the template for a formal package that is not declared with a box.
553 procedure Check_Forward_Instantiation (Decl : Node_Id);
554 -- If the generic is a local entity and the corresponding body has not
555 -- been seen yet, flag enclosing packages to indicate that it will be
556 -- elaborated after the generic body. Subprograms declared in the same
557 -- package cannot be inlined by the front end because front-end inlining
558 -- requires a strict linear order of elaboration.
560 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
561 -- Check if some association between formals and actuals requires to make
562 -- visible primitives of a tagged type, and make those primitives visible.
563 -- Return the list of primitives whose visibility is modified (to restore
564 -- their visibility later through Restore_Hidden_Primitives). If no
565 -- candidate is found then return No_Elist.
567 procedure Check_Hidden_Child_Unit
568 (N : Node_Id;
569 Gen_Unit : Entity_Id;
570 Act_Decl_Id : Entity_Id);
571 -- If the generic unit is an implicit child instance within a parent
572 -- instance, we need to make an explicit test that it is not hidden by
573 -- a child instance of the same name and parent.
575 procedure Check_Generic_Actuals
576 (Instance : Entity_Id;
577 Is_Formal_Box : Boolean);
578 -- Similar to previous one. Check the actuals in the instantiation,
579 -- whose views can change between the point of instantiation and the point
580 -- of instantiation of the body. In addition, mark the generic renamings
581 -- as generic actuals, so that they are not compatible with other actuals.
582 -- Recurse on an actual that is a formal package whose declaration has
583 -- a box.
585 function 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 Others_Choice is present, some of the formals may be defaulted.
1134 -- To simplify the treatment of visibility in an instance, we introduce
1135 -- individual defaults for each such formal. These defaults are
1136 -- appended to the list of associations and replace the Others_Choice.
1138 Found_Assoc : Node_Id;
1139 -- Association for the current formal being match. Empty if there are
1140 -- no remaining actuals, or if there is no named association with the
1141 -- name of the formal.
1143 Is_Named_Assoc : Boolean;
1144 Num_Matched : Nat := 0;
1145 Num_Actuals : Nat := 0;
1147 Others_Present : Boolean := False;
1148 Others_Choice : Node_Id := Empty;
1149 -- In Ada 2005, indicates partial parameterization of a formal
1150 -- package. As usual an other 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 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);
1318 else
1319 Error_Msg_N
1320 ("named association not allowed for overloaded formal",
1321 Others_Choice);
1322 end if;
1324 Abandon_Instantiation (Instantiation_Node);
1325 end if;
1327 Next (Temp_Formal);
1328 end loop;
1329 end Check_Overloaded_Formal_Subprogram;
1331 -------------------------------
1332 -- Check_Fixed_Point_Actual --
1333 -------------------------------
1335 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1336 Typ : constant Entity_Id := Entity (Actual);
1337 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1338 Elem : Elmt_Id;
1339 Formal : Node_Id;
1340 Op : Entity_Id;
1342 begin
1343 -- Locate primitive operations of the type that are arithmetic
1344 -- operations.
1346 Elem := First_Elmt (Prims);
1347 while Present (Elem) loop
1348 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1350 -- Check whether the generic unit has a formal subprogram of
1351 -- the same name. This does not check types but is good enough
1352 -- to justify a warning.
1354 Formal := First_Non_Pragma (Formals);
1355 Op := Alias (Node (Elem));
1357 while Present (Formal) loop
1358 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1359 and then Chars (Defining_Entity (Formal)) =
1360 Chars (Node (Elem))
1361 then
1362 exit;
1364 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1365 declare
1366 Assoc : Node_Id;
1367 Ent : Entity_Id;
1369 begin
1370 -- Locate corresponding actual, and check whether it
1371 -- includes a fixed-point type.
1373 Assoc := First (Assoc_List);
1374 while Present (Assoc) loop
1375 exit when
1376 Nkind (Assoc) = N_Package_Renaming_Declaration
1377 and then Chars (Defining_Unit_Name (Assoc)) =
1378 Chars (Defining_Identifier (Formal));
1380 Next (Assoc);
1381 end loop;
1383 if Present (Assoc) then
1385 -- If formal package declares a fixed-point type,
1386 -- and the user-defined operator is derived from
1387 -- a generic instance package, the fixed-point type
1388 -- does not use the corresponding predefined op.
1390 Ent := First_Entity (Entity (Name (Assoc)));
1391 while Present (Ent) loop
1392 if Is_Fixed_Point_Type (Ent)
1393 and then Present (Op)
1394 and then Is_Generic_Instance (Scope (Op))
1395 then
1396 return;
1397 end if;
1399 Next_Entity (Ent);
1400 end loop;
1401 end if;
1402 end;
1403 end if;
1405 Next (Formal);
1406 end loop;
1408 if No (Formal) then
1409 Error_Msg_Sloc := Sloc (Node (Elem));
1410 Error_Msg_NE
1411 ("?instance uses predefined operation, not primitive "
1412 & "operation&#", Actual, Node (Elem));
1413 end if;
1414 end if;
1416 Next_Elmt (Elem);
1417 end loop;
1418 end Check_Fixed_Point_Actual;
1420 -------------------------------
1421 -- Has_Fully_Defined_Profile --
1422 -------------------------------
1424 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1425 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1426 -- Determine whethet type Typ is fully defined
1428 ---------------------------
1429 -- Is_Fully_Defined_Type --
1430 ---------------------------
1432 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1433 begin
1434 -- A private type without a full view is not fully defined
1436 if Is_Private_Type (Typ)
1437 and then No (Full_View (Typ))
1438 then
1439 return False;
1441 -- An incomplete type is never fully defined
1443 elsif Is_Incomplete_Type (Typ) then
1444 return False;
1446 -- All other types are fully defined
1448 else
1449 return True;
1450 end if;
1451 end Is_Fully_Defined_Type;
1453 -- Local declarations
1455 Param : Entity_Id;
1457 -- Start of processing for Has_Fully_Defined_Profile
1459 begin
1460 -- Check the parameters
1462 Param := First_Formal (Subp);
1463 while Present (Param) loop
1464 if not Is_Fully_Defined_Type (Etype (Param)) then
1465 return False;
1466 end if;
1468 Next_Formal (Param);
1469 end loop;
1471 -- Check the return type
1473 return Is_Fully_Defined_Type (Etype (Subp));
1474 end Has_Fully_Defined_Profile;
1476 ---------------------
1477 -- Matching_Actual --
1478 ---------------------
1480 function Matching_Actual
1481 (F : Entity_Id;
1482 A_F : Entity_Id) return Node_Id
1484 Prev : Node_Id;
1485 Act : Node_Id;
1487 begin
1488 Is_Named_Assoc := False;
1490 -- End of list of purely positional parameters
1492 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1493 Found_Assoc := Empty;
1494 Act := Empty;
1496 -- Case of positional parameter corresponding to current formal
1498 elsif No (Selector_Name (Actual)) then
1499 Found_Assoc := Actual;
1500 Act := Explicit_Generic_Actual_Parameter (Actual);
1501 Num_Matched := Num_Matched + 1;
1502 Next (Actual);
1504 -- Otherwise scan list of named actuals to find the one with the
1505 -- desired name. All remaining actuals have explicit names.
1507 else
1508 Is_Named_Assoc := True;
1509 Found_Assoc := Empty;
1510 Act := Empty;
1511 Prev := Empty;
1513 while Present (Actual) loop
1514 if Nkind (Actual) = N_Others_Choice then
1515 Found_Assoc := Empty;
1516 Act := Empty;
1518 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1519 Set_Entity (Selector_Name (Actual), A_F);
1520 Set_Etype (Selector_Name (Actual), Etype (A_F));
1521 Generate_Reference (A_F, Selector_Name (Actual));
1523 Found_Assoc := Actual;
1524 Act := Explicit_Generic_Actual_Parameter (Actual);
1525 Num_Matched := Num_Matched + 1;
1526 exit;
1527 end if;
1529 Prev := Actual;
1530 Next (Actual);
1531 end loop;
1533 -- Reset for subsequent searches. In most cases the named
1534 -- associations are in order. If they are not, we reorder them
1535 -- to avoid scanning twice the same actual. This is not just a
1536 -- question of efficiency: there may be multiple defaults with
1537 -- boxes that have the same name. In a nested instantiation we
1538 -- insert actuals for those defaults, and cannot rely on their
1539 -- names to disambiguate them.
1541 if Actual = First_Named then
1542 Next (First_Named);
1544 elsif Present (Actual) then
1545 Insert_Before (First_Named, Remove_Next (Prev));
1546 end if;
1548 Actual := First_Named;
1549 end if;
1551 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1552 Set_Used_As_Generic_Actual (Entity (Act));
1553 end if;
1555 return Act;
1556 end Matching_Actual;
1558 ------------------------------
1559 -- Partial_Parameterization --
1560 ------------------------------
1562 function Partial_Parameterization return Boolean is
1563 begin
1564 return Others_Present
1565 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1566 end Partial_Parameterization;
1568 ---------------------
1569 -- Process_Default --
1570 ---------------------
1572 procedure Process_Default (Formal : Node_Id) is
1573 Loc : constant Source_Ptr := Sloc (I_Node);
1574 F_Id : constant Entity_Id := Defining_Entity (Formal);
1575 Decl : Node_Id;
1576 Default : Node_Id;
1577 Id : Entity_Id;
1579 begin
1580 -- Append copy of formal declaration to associations, and create new
1581 -- defining identifier for it.
1583 Decl := New_Copy_Tree (Formal);
1584 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1586 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1587 Set_Defining_Unit_Name (Specification (Decl), Id);
1589 else
1590 Set_Defining_Identifier (Decl, Id);
1591 end if;
1593 Append (Decl, Assoc_List);
1595 if No (Found_Assoc) then
1596 Default :=
1597 Make_Generic_Association (Loc,
1598 Selector_Name =>
1599 New_Occurrence_Of (Id, Loc),
1600 Explicit_Generic_Actual_Parameter => Empty);
1601 Set_Box_Present (Default);
1602 Append (Default, Default_Formals);
1603 end if;
1604 end Process_Default;
1606 ---------------------------------
1607 -- Renames_Standard_Subprogram --
1608 ---------------------------------
1610 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1611 Id : Entity_Id;
1613 begin
1614 Id := Alias (Subp);
1615 while Present (Id) loop
1616 if Scope (Id) = Standard_Standard then
1617 return True;
1618 end if;
1620 Id := Alias (Id);
1621 end loop;
1623 return False;
1624 end Renames_Standard_Subprogram;
1626 -------------------------
1627 -- Set_Analyzed_Formal --
1628 -------------------------
1630 procedure Set_Analyzed_Formal is
1631 Kind : Node_Kind;
1633 begin
1634 while Present (Analyzed_Formal) loop
1635 Kind := Nkind (Analyzed_Formal);
1637 case Nkind (Formal) is
1638 when N_Formal_Subprogram_Declaration =>
1639 exit when Kind in N_Formal_Subprogram_Declaration
1640 and then
1641 Chars
1642 (Defining_Unit_Name (Specification (Formal))) =
1643 Chars
1644 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1646 when N_Formal_Package_Declaration =>
1647 exit when Kind in N_Formal_Package_Declaration
1648 | N_Generic_Package_Declaration
1649 | N_Package_Declaration;
1651 when N_Use_Package_Clause
1652 | N_Use_Type_Clause
1654 exit;
1656 when others =>
1658 -- Skip freeze nodes, and nodes inserted to replace
1659 -- unrecognized pragmas.
1661 exit when
1662 Kind not in N_Formal_Subprogram_Declaration
1663 and then Kind not in N_Subprogram_Declaration
1664 | N_Freeze_Entity
1665 | N_Null_Statement
1666 | N_Itype_Reference
1667 and then Chars (Defining_Identifier (Formal)) =
1668 Chars (Defining_Identifier (Analyzed_Formal));
1669 end case;
1671 Next (Analyzed_Formal);
1672 end loop;
1673 end Set_Analyzed_Formal;
1675 -- Start of processing for Analyze_Associations
1677 begin
1678 Actuals := Generic_Associations (I_Node);
1680 if Present (Actuals) then
1682 -- Check for an Others choice, indicating a partial parameterization
1683 -- for a formal package.
1685 Actual := First (Actuals);
1686 while Present (Actual) loop
1687 if Nkind (Actual) = N_Others_Choice then
1688 Others_Present := True;
1689 Others_Choice := Actual;
1691 if Present (Next (Actual)) then
1692 Error_Msg_N ("OTHERS must be last association", Actual);
1693 end if;
1695 -- This subprogram is used both for formal packages and for
1696 -- instantiations. For the latter, associations must all be
1697 -- explicit.
1699 if Nkind (I_Node) /= N_Formal_Package_Declaration
1700 and then Comes_From_Source (I_Node)
1701 then
1702 Error_Msg_N
1703 ("OTHERS association not allowed in an instance",
1704 Actual);
1705 end if;
1707 -- In any case, nothing to do after the others association
1709 exit;
1711 elsif Box_Present (Actual)
1712 and then Comes_From_Source (I_Node)
1713 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1714 then
1715 Error_Msg_N
1716 ("box association not allowed in an instance", Actual);
1717 end if;
1719 Next (Actual);
1720 end loop;
1722 -- If named associations are present, save first named association
1723 -- (it may of course be Empty) to facilitate subsequent name search.
1725 First_Named := First (Actuals);
1726 while Present (First_Named)
1727 and then Nkind (First_Named) /= N_Others_Choice
1728 and then No (Selector_Name (First_Named))
1729 loop
1730 Num_Actuals := Num_Actuals + 1;
1731 Next (First_Named);
1732 end loop;
1733 end if;
1735 Named := First_Named;
1736 while Present (Named) loop
1737 if Nkind (Named) /= N_Others_Choice
1738 and then No (Selector_Name (Named))
1739 then
1740 Error_Msg_N ("invalid positional actual after named one", Named);
1741 Abandon_Instantiation (Named);
1742 end if;
1744 -- A named association may lack an actual parameter, if it was
1745 -- introduced for a default subprogram that turns out to be local
1746 -- to the outer instantiation. If it has a box association it must
1747 -- correspond to some formal in the generic.
1749 if Nkind (Named) /= N_Others_Choice
1750 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1751 or else Box_Present (Named))
1752 then
1753 Num_Actuals := Num_Actuals + 1;
1754 end if;
1756 Next (Named);
1757 end loop;
1759 if Present (Formals) then
1760 Formal := First_Non_Pragma (Formals);
1761 Analyzed_Formal := First_Non_Pragma (F_Copy);
1763 if Present (Actuals) then
1764 Actual := First (Actuals);
1766 -- All formals should have default values
1768 else
1769 Actual := Empty;
1770 end if;
1772 while Present (Formal) loop
1773 Set_Analyzed_Formal;
1774 Saved_Formal := Next_Non_Pragma (Formal);
1776 case Nkind (Formal) is
1777 when N_Formal_Object_Declaration =>
1778 Match :=
1779 Matching_Actual
1780 (Defining_Identifier (Formal),
1781 Defining_Identifier (Analyzed_Formal));
1783 if No (Match) and then Partial_Parameterization then
1784 Process_Default (Formal);
1786 else
1787 Append_List
1788 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1789 Assoc_List);
1791 -- For a defaulted in_parameter, create an entry in the
1792 -- the list of defaulted actuals, for GNATprove use. Do
1793 -- not included these defaults for an instance nested
1794 -- within a generic, because the defaults are also used
1795 -- in the analysis of the enclosing generic, and only
1796 -- defaulted subprograms are relevant there.
1798 if No (Match) and then not Inside_A_Generic then
1799 Append_To (Default_Actuals,
1800 Make_Generic_Association (Sloc (I_Node),
1801 Selector_Name =>
1802 New_Occurrence_Of
1803 (Defining_Identifier (Formal), Sloc (I_Node)),
1804 Explicit_Generic_Actual_Parameter =>
1805 New_Copy_Tree (Default_Expression (Formal))));
1806 end if;
1807 end if;
1809 -- If the object is a call to an expression function, this
1810 -- is a freezing point for it.
1812 if Is_Entity_Name (Match)
1813 and then Present (Entity (Match))
1814 and then Nkind
1815 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1816 = N_Expression_Function
1817 then
1818 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1819 end if;
1821 when N_Formal_Type_Declaration =>
1822 Match :=
1823 Matching_Actual
1824 (Defining_Identifier (Formal),
1825 Defining_Identifier (Analyzed_Formal));
1827 if No (Match) then
1828 if Partial_Parameterization then
1829 Process_Default (Formal);
1831 elsif Present (Default_Subtype_Mark (Formal)) then
1832 Match := New_Copy (Default_Subtype_Mark (Formal));
1833 Append_List
1834 (Instantiate_Type
1835 (Formal, Match, Analyzed_Formal, Assoc_List),
1836 Assoc_List);
1837 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1839 else
1840 Error_Msg_Sloc := Sloc (Gen_Unit);
1841 Error_Msg_NE
1842 ("missing actual&",
1843 Instantiation_Node, Defining_Identifier (Formal));
1844 Error_Msg_NE
1845 ("\in instantiation of & declared#",
1846 Instantiation_Node, Gen_Unit);
1847 Abandon_Instantiation (Instantiation_Node);
1848 end if;
1850 else
1851 Analyze (Match);
1852 Append_List
1853 (Instantiate_Type
1854 (Formal, Match, Analyzed_Formal, Assoc_List),
1855 Assoc_List);
1857 -- Warn when an actual is a fixed-point with user-
1858 -- defined promitives. The warning is superfluous
1859 -- if the formal is private, because there can be
1860 -- no arithmetic operations in the generic so there
1861 -- no danger of confusion.
1863 if Is_Fixed_Point_Type (Entity (Match))
1864 and then not Is_Private_Type
1865 (Defining_Identifier (Analyzed_Formal))
1866 then
1867 Check_Fixed_Point_Actual (Match);
1868 end if;
1870 -- An instantiation is a freeze point for the actuals,
1871 -- unless this is a rewritten formal package, or the
1872 -- formal is an Ada 2012 formal incomplete type.
1874 if Nkind (I_Node) = N_Formal_Package_Declaration
1875 or else
1876 (Ada_Version >= Ada_2012
1877 and then
1878 Ekind (Defining_Identifier (Analyzed_Formal)) =
1879 E_Incomplete_Type)
1880 then
1881 null;
1883 else
1884 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1885 end if;
1886 end if;
1888 -- A remote access-to-class-wide type is not a legal actual
1889 -- for a generic formal of an access type (E.2.2(17/2)).
1890 -- In GNAT an exception to this rule is introduced when
1891 -- the formal is marked as remote using implementation
1892 -- defined aspect/pragma Remote_Access_Type. In that case
1893 -- the actual must be remote as well.
1895 -- If the current instantiation is the construction of a
1896 -- local copy for a formal package the actuals may be
1897 -- defaulted, and there is no matching actual to check.
1899 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1900 and then
1901 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1902 N_Access_To_Object_Definition
1903 and then Present (Match)
1904 then
1905 declare
1906 Formal_Ent : constant Entity_Id :=
1907 Defining_Identifier (Analyzed_Formal);
1908 begin
1909 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1910 = Is_Remote_Types (Formal_Ent)
1911 then
1912 -- Remoteness of formal and actual match
1914 null;
1916 elsif Is_Remote_Types (Formal_Ent) then
1918 -- Remote formal, non-remote actual
1920 Error_Msg_NE
1921 ("actual for& must be remote", Match, Formal_Ent);
1923 else
1924 -- Non-remote formal, remote actual
1926 Error_Msg_NE
1927 ("actual for& may not be remote",
1928 Match, Formal_Ent);
1929 end if;
1930 end;
1931 end if;
1933 when N_Formal_Subprogram_Declaration =>
1934 Match :=
1935 Matching_Actual
1936 (Defining_Unit_Name (Specification (Formal)),
1937 Defining_Unit_Name (Specification (Analyzed_Formal)));
1939 -- If the formal subprogram has the same name as another
1940 -- formal subprogram of the generic, then a named
1941 -- association is illegal (12.3(9)). Exclude named
1942 -- associations that are generated for a nested instance.
1944 if Present (Match)
1945 and then Is_Named_Assoc
1946 and then Comes_From_Source (Found_Assoc)
1947 then
1948 Check_Overloaded_Formal_Subprogram (Formal);
1949 end if;
1951 -- If there is no corresponding actual, this may be case
1952 -- of partial parameterization, or else the formal has a
1953 -- default or a box.
1955 if No (Match) and then Partial_Parameterization then
1956 Process_Default (Formal);
1958 if Nkind (I_Node) = N_Formal_Package_Declaration then
1959 Check_Overloaded_Formal_Subprogram (Formal);
1960 end if;
1962 else
1963 Append_To (Assoc_List,
1964 Instantiate_Formal_Subprogram
1965 (Formal, Match, Analyzed_Formal));
1967 -- If formal subprogram has contracts, create wrappers
1968 -- for it. This is an expansion activity that cannot
1969 -- take place e.g. within an enclosing generic unit.
1971 if Has_Contracts (Analyzed_Formal)
1972 and then (Expander_Active or GNATprove_Mode)
1973 then
1974 Build_Subprogram_Wrappers;
1975 end if;
1977 -- An instantiation is a freeze point for the actuals,
1978 -- unless this is a rewritten formal package.
1980 if Nkind (I_Node) /= N_Formal_Package_Declaration
1981 and then Nkind (Match) = N_Identifier
1982 and then Is_Subprogram (Entity (Match))
1984 -- The actual subprogram may rename a routine defined
1985 -- in Standard. Avoid freezing such renamings because
1986 -- subprograms coming from Standard cannot be frozen.
1988 and then
1989 not Renames_Standard_Subprogram (Entity (Match))
1991 -- If the actual subprogram comes from a different
1992 -- unit, it is already frozen, either by a body in
1993 -- that unit or by the end of the declarative part
1994 -- of the unit. This check avoids the freezing of
1995 -- subprograms defined in Standard which are used
1996 -- as generic actuals.
1998 and then In_Same_Code_Unit (Entity (Match), I_Node)
1999 and then Has_Fully_Defined_Profile (Entity (Match))
2000 then
2001 -- Mark the subprogram as having a delayed freeze
2002 -- since this may be an out-of-order action.
2004 Set_Has_Delayed_Freeze (Entity (Match));
2005 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2006 end if;
2007 end if;
2009 -- If this is a nested generic, preserve default for later
2010 -- instantiations. We do this as well for GNATprove use,
2011 -- so that the list of generic associations is complete.
2013 if No (Match) and then Box_Present (Formal) then
2014 declare
2015 Subp : constant Entity_Id :=
2016 Defining_Unit_Name
2017 (Specification (Last (Assoc_List)));
2019 begin
2020 Append_To (Default_Actuals,
2021 Make_Generic_Association (Sloc (I_Node),
2022 Selector_Name =>
2023 New_Occurrence_Of (Subp, Sloc (I_Node)),
2024 Explicit_Generic_Actual_Parameter =>
2025 New_Occurrence_Of (Subp, Sloc (I_Node))));
2026 end;
2027 end if;
2029 when N_Formal_Package_Declaration =>
2030 -- The name of the formal package may be hidden by the
2031 -- formal parameter itself.
2033 if Error_Posted (Analyzed_Formal) then
2034 Abandon_Instantiation (Instantiation_Node);
2036 else
2037 Match :=
2038 Matching_Actual
2039 (Defining_Identifier (Formal),
2040 Defining_Identifier
2041 (Original_Node (Analyzed_Formal)));
2042 end if;
2044 if No (Match) then
2045 if Partial_Parameterization then
2046 Process_Default (Formal);
2048 else
2049 Error_Msg_Sloc := Sloc (Gen_Unit);
2050 Error_Msg_NE
2051 ("missing actual&",
2052 Instantiation_Node, Defining_Identifier (Formal));
2053 Error_Msg_NE
2054 ("\in instantiation of & declared#",
2055 Instantiation_Node, Gen_Unit);
2057 Abandon_Instantiation (Instantiation_Node);
2058 end if;
2060 else
2061 Analyze (Match);
2062 Append_List
2063 (Instantiate_Formal_Package
2064 (Formal, Match, Analyzed_Formal),
2065 Assoc_List);
2067 -- Determine whether the actual package needs an explicit
2068 -- freeze node. This is only the case if the actual is
2069 -- declared in the same unit and has a body. Normally
2070 -- packages do not have explicit freeze nodes, and gigi
2071 -- only uses them to elaborate entities in a package
2072 -- body.
2074 Explicit_Freeze_Check : declare
2075 Actual : constant Entity_Id := Entity (Match);
2076 Gen_Par : Entity_Id;
2078 Needs_Freezing : Boolean;
2079 P : Node_Id;
2081 procedure Check_Generic_Parent;
2082 -- The actual may be an instantiation of a unit
2083 -- declared in a previous instantiation. If that
2084 -- one is also in the current compilation, it must
2085 -- itself be frozen before the actual. The actual
2086 -- may be an instantiation of a generic child unit,
2087 -- in which case the same applies to the instance
2088 -- of the parent which must be frozen before the
2089 -- actual.
2090 -- Should this itself be recursive ???
2092 --------------------------
2093 -- Check_Generic_Parent --
2094 --------------------------
2096 procedure Check_Generic_Parent is
2097 Inst : constant Node_Id :=
2098 Get_Unit_Instantiation_Node (Actual);
2099 Par : Entity_Id;
2101 begin
2102 Par := Empty;
2104 if Nkind (Parent (Actual)) = N_Package_Specification
2105 then
2106 Par := Scope (Generic_Parent (Parent (Actual)));
2108 if Is_Generic_Instance (Par) then
2109 null;
2111 -- If the actual is a child generic unit, check
2112 -- whether the instantiation of the parent is
2113 -- also local and must also be frozen now. We
2114 -- must retrieve the instance node to locate the
2115 -- parent instance if any.
2117 elsif Ekind (Par) = E_Generic_Package
2118 and then Is_Child_Unit (Gen_Par)
2119 and then Ekind (Scope (Gen_Par)) =
2120 E_Generic_Package
2121 then
2122 if Nkind (Inst) = N_Package_Instantiation
2123 and then Nkind (Name (Inst)) =
2124 N_Expanded_Name
2125 then
2126 -- Retrieve entity of parent instance
2128 Par := Entity (Prefix (Name (Inst)));
2129 end if;
2131 else
2132 Par := Empty;
2133 end if;
2134 end if;
2136 if Present (Par)
2137 and then Is_Generic_Instance (Par)
2138 and then Scope (Par) = Current_Scope
2139 and then
2140 (No (Freeze_Node (Par))
2141 or else
2142 not Is_List_Member (Freeze_Node (Par)))
2143 then
2144 Set_Has_Delayed_Freeze (Par);
2145 Append_Elmt (Par, Actuals_To_Freeze);
2146 end if;
2147 end Check_Generic_Parent;
2149 -- Start of processing for Explicit_Freeze_Check
2151 begin
2152 if Present (Renamed_Entity (Actual)) then
2153 Gen_Par :=
2154 Generic_Parent (Specification
2155 (Unit_Declaration_Node
2156 (Renamed_Entity (Actual))));
2157 else
2158 Gen_Par :=
2159 Generic_Parent (Specification
2160 (Unit_Declaration_Node (Actual)));
2161 end if;
2163 if not Expander_Active
2164 or else not Has_Completion (Actual)
2165 or else not In_Same_Source_Unit (I_Node, Actual)
2166 or else Is_Frozen (Actual)
2167 or else
2168 (Present (Renamed_Entity (Actual))
2169 and then
2170 not In_Same_Source_Unit
2171 (I_Node, (Renamed_Entity (Actual))))
2172 then
2173 null;
2175 else
2176 -- Finally we want to exclude such freeze nodes
2177 -- from statement sequences, which freeze
2178 -- everything before them.
2179 -- Is this strictly necessary ???
2181 Needs_Freezing := True;
2183 P := Parent (I_Node);
2184 while Nkind (P) /= N_Compilation_Unit loop
2185 if Nkind (P) = N_Handled_Sequence_Of_Statements
2186 then
2187 Needs_Freezing := False;
2188 exit;
2189 end if;
2191 P := Parent (P);
2192 end loop;
2194 if Needs_Freezing then
2195 Check_Generic_Parent;
2197 -- If the actual is a renaming of a proper
2198 -- instance of the formal package, indicate
2199 -- that it is the instance that must be frozen.
2201 if Nkind (Parent (Actual)) =
2202 N_Package_Renaming_Declaration
2203 then
2204 Set_Has_Delayed_Freeze
2205 (Renamed_Entity (Actual));
2206 Append_Elmt
2207 (Renamed_Entity (Actual),
2208 Actuals_To_Freeze);
2209 else
2210 Set_Has_Delayed_Freeze (Actual);
2211 Append_Elmt (Actual, Actuals_To_Freeze);
2212 end if;
2213 end if;
2214 end if;
2215 end Explicit_Freeze_Check;
2216 end if;
2218 -- For use type and use package appearing in the generic part,
2219 -- we have already copied them, so we can just move them where
2220 -- they belong (we mustn't recopy them since this would mess up
2221 -- the Sloc values).
2223 when N_Use_Package_Clause
2224 | N_Use_Type_Clause
2226 if Nkind (Original_Node (I_Node)) =
2227 N_Formal_Package_Declaration
2228 then
2229 Append (New_Copy_Tree (Formal), Assoc_List);
2230 else
2231 Remove (Formal);
2232 Append (Formal, Assoc_List);
2233 end if;
2235 when others =>
2236 raise Program_Error;
2237 end case;
2239 -- Check here the correct use of Ghost entities in generic
2240 -- instantiations, as now the generic has been resolved and
2241 -- we know which formal generic parameters are ghost (SPARK
2242 -- RM 6.9(10)).
2244 if Nkind (Formal) not in N_Use_Package_Clause
2245 | N_Use_Type_Clause
2246 then
2247 Check_Ghost_Context_In_Generic_Association
2248 (Actual => Match,
2249 Formal => Defining_Entity (Analyzed_Formal));
2250 end if;
2252 Formal := Saved_Formal;
2253 Next_Non_Pragma (Analyzed_Formal);
2254 end loop;
2256 if Num_Actuals > Num_Matched then
2257 Error_Msg_Sloc := Sloc (Gen_Unit);
2259 if Present (Selector_Name (Actual)) then
2260 Error_Msg_NE
2261 ("unmatched actual &", Actual, Selector_Name (Actual));
2262 Error_Msg_NE
2263 ("\in instantiation of & declared#", Actual, Gen_Unit);
2264 else
2265 Error_Msg_NE
2266 ("unmatched actual in instantiation of & declared#",
2267 Actual, Gen_Unit);
2268 end if;
2269 end if;
2271 elsif Present (Actuals) then
2272 Error_Msg_N
2273 ("too many actuals in generic instantiation", Instantiation_Node);
2274 end if;
2276 -- An instantiation freezes all generic actuals. The only exceptions
2277 -- to this are incomplete types and subprograms which are not fully
2278 -- defined at the point of instantiation.
2280 declare
2281 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2282 begin
2283 while Present (Elmt) loop
2284 Freeze_Before (I_Node, Node (Elmt));
2285 Next_Elmt (Elmt);
2286 end loop;
2287 end;
2289 -- If there are default subprograms, normalize the tree by adding
2290 -- explicit associations for them. This is required if the instance
2291 -- appears within a generic.
2293 if not Is_Empty_List (Default_Actuals) then
2294 declare
2295 Default : Node_Id;
2297 begin
2298 Default := First (Default_Actuals);
2299 while Present (Default) loop
2300 Mark_Rewrite_Insertion (Default);
2301 Next (Default);
2302 end loop;
2304 if No (Actuals) then
2305 Set_Generic_Associations (I_Node, Default_Actuals);
2306 else
2307 Append_List_To (Actuals, Default_Actuals);
2308 end if;
2309 end;
2310 end if;
2312 -- If this is a formal package, normalize the parameter list by adding
2313 -- explicit box associations for the formals that are covered by an
2314 -- Others_Choice.
2316 Append_List (Default_Formals, Formals);
2318 return Assoc_List;
2319 end Analyze_Associations;
2321 -------------------------------
2322 -- Analyze_Formal_Array_Type --
2323 -------------------------------
2325 procedure Analyze_Formal_Array_Type
2326 (T : in out Entity_Id;
2327 Def : Node_Id)
2329 DSS : Node_Id;
2331 begin
2332 -- Treated like a non-generic array declaration, with additional
2333 -- semantic checks.
2335 Enter_Name (T);
2337 if Nkind (Def) = N_Constrained_Array_Definition then
2338 DSS := First (Discrete_Subtype_Definitions (Def));
2339 while Present (DSS) loop
2340 if Nkind (DSS) in N_Subtype_Indication
2341 | N_Range
2342 | N_Attribute_Reference
2343 then
2344 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2345 end if;
2347 Next (DSS);
2348 end loop;
2349 end if;
2351 Array_Type_Declaration (T, Def);
2352 Set_Is_Generic_Type (Base_Type (T));
2354 if Ekind (Component_Type (T)) = E_Incomplete_Type
2355 and then No (Full_View (Component_Type (T)))
2356 then
2357 Error_Msg_N ("premature usage of incomplete type", Def);
2359 -- Check that range constraint is not allowed on the component type
2360 -- of a generic formal array type (AARM 12.5.3(3))
2362 elsif Is_Internal (Component_Type (T))
2363 and then Present (Subtype_Indication (Component_Definition (Def)))
2364 and then Nkind (Original_Node
2365 (Subtype_Indication (Component_Definition (Def)))) =
2366 N_Subtype_Indication
2367 then
2368 Error_Msg_N
2369 ("in a formal, a subtype indication can only be "
2370 & "a subtype mark (RM 12.5.3(3))",
2371 Subtype_Indication (Component_Definition (Def)));
2372 end if;
2374 end Analyze_Formal_Array_Type;
2376 ---------------------------------------------
2377 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2378 ---------------------------------------------
2380 -- As for other generic types, we create a valid type representation with
2381 -- legal but arbitrary attributes, whose values are never considered
2382 -- static. For all scalar types we introduce an anonymous base type, with
2383 -- the same attributes. We choose the corresponding integer type to be
2384 -- Standard_Integer.
2385 -- Here and in other similar routines, the Sloc of the generated internal
2386 -- type must be the same as the sloc of the defining identifier of the
2387 -- formal type declaration, to provide proper source navigation.
2389 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2390 (T : Entity_Id;
2391 Def : Node_Id)
2393 Loc : constant Source_Ptr := Sloc (Def);
2395 Base : constant Entity_Id :=
2396 New_Internal_Entity
2397 (E_Decimal_Fixed_Point_Type,
2398 Current_Scope,
2399 Sloc (Defining_Identifier (Parent (Def))), 'G');
2401 Int_Base : constant Entity_Id := Standard_Integer;
2402 Delta_Val : constant Ureal := Ureal_1;
2403 Digs_Val : constant Uint := Uint_6;
2405 function Make_Dummy_Bound return Node_Id;
2406 -- Return a properly typed universal real literal to use as a bound
2408 ----------------------
2409 -- Make_Dummy_Bound --
2410 ----------------------
2412 function Make_Dummy_Bound return Node_Id is
2413 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2414 begin
2415 Set_Etype (Bound, Universal_Real);
2416 return Bound;
2417 end Make_Dummy_Bound;
2419 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2421 begin
2422 Enter_Name (T);
2424 Set_Etype (Base, Base);
2425 Set_Size_Info (Base, Int_Base);
2426 Set_RM_Size (Base, RM_Size (Int_Base));
2427 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2428 Set_Digits_Value (Base, Digs_Val);
2429 Set_Delta_Value (Base, Delta_Val);
2430 Set_Small_Value (Base, Delta_Val);
2431 Set_Scalar_Range (Base,
2432 Make_Range (Loc,
2433 Low_Bound => Make_Dummy_Bound,
2434 High_Bound => Make_Dummy_Bound));
2436 Set_Is_Generic_Type (Base);
2437 Set_Parent (Base, Parent (Def));
2439 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2440 Set_Etype (T, Base);
2441 Set_Size_Info (T, Int_Base);
2442 Set_RM_Size (T, RM_Size (Int_Base));
2443 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2444 Set_Digits_Value (T, Digs_Val);
2445 Set_Delta_Value (T, Delta_Val);
2446 Set_Small_Value (T, Delta_Val);
2447 Set_Scalar_Range (T, Scalar_Range (Base));
2448 Set_Is_Constrained (T);
2450 Check_Restriction (No_Fixed_Point, Def);
2451 end Analyze_Formal_Decimal_Fixed_Point_Type;
2453 -------------------------------------------
2454 -- Analyze_Formal_Derived_Interface_Type --
2455 -------------------------------------------
2457 procedure Analyze_Formal_Derived_Interface_Type
2458 (N : Node_Id;
2459 T : Entity_Id;
2460 Def : Node_Id)
2462 Loc : constant Source_Ptr := Sloc (Def);
2464 begin
2465 -- Rewrite as a type declaration of a derived type. This ensures that
2466 -- the interface list and primitive operations are properly captured.
2468 Rewrite (N,
2469 Make_Full_Type_Declaration (Loc,
2470 Defining_Identifier => T,
2471 Type_Definition => Def));
2473 -- Keep the aspects from the original node
2475 Move_Aspects (Original_Node (N), N);
2477 Analyze (N);
2478 Set_Is_Generic_Type (T);
2479 end Analyze_Formal_Derived_Interface_Type;
2481 ---------------------------------
2482 -- Analyze_Formal_Derived_Type --
2483 ---------------------------------
2485 procedure Analyze_Formal_Derived_Type
2486 (N : Node_Id;
2487 T : Entity_Id;
2488 Def : Node_Id)
2490 Loc : constant Source_Ptr := Sloc (Def);
2491 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2492 New_N : Node_Id;
2494 begin
2495 Set_Is_Generic_Type (T);
2497 if Private_Present (Def) then
2498 New_N :=
2499 Make_Private_Extension_Declaration (Loc,
2500 Defining_Identifier => T,
2501 Discriminant_Specifications => Discriminant_Specifications (N),
2502 Unknown_Discriminants_Present => Unk_Disc,
2503 Subtype_Indication => Subtype_Mark (Def),
2504 Interface_List => Interface_List (Def));
2506 Set_Abstract_Present (New_N, Abstract_Present (Def));
2507 Set_Limited_Present (New_N, Limited_Present (Def));
2508 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2510 else
2511 New_N :=
2512 Make_Full_Type_Declaration (Loc,
2513 Defining_Identifier => T,
2514 Discriminant_Specifications =>
2515 Discriminant_Specifications (Parent (T)),
2516 Type_Definition =>
2517 Make_Derived_Type_Definition (Loc,
2518 Subtype_Indication => Subtype_Mark (Def)));
2520 Set_Abstract_Present
2521 (Type_Definition (New_N), Abstract_Present (Def));
2522 Set_Limited_Present
2523 (Type_Definition (New_N), Limited_Present (Def));
2524 end if;
2526 Rewrite (N, New_N);
2528 -- Keep the aspects from the original node
2530 Move_Aspects (Original_Node (N), N);
2532 Analyze (N);
2534 if Unk_Disc then
2535 if not Is_Composite_Type (T) then
2536 Error_Msg_N
2537 ("unknown discriminants not allowed for elementary types", N);
2538 else
2539 Set_Has_Unknown_Discriminants (T);
2540 Set_Is_Constrained (T, False);
2541 end if;
2542 end if;
2544 -- If the parent type has a known size, so does the formal, which makes
2545 -- legal representation clauses that involve the formal.
2547 Set_Size_Known_At_Compile_Time
2548 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2549 end Analyze_Formal_Derived_Type;
2551 ----------------------------------
2552 -- Analyze_Formal_Discrete_Type --
2553 ----------------------------------
2555 -- The operations defined for a discrete types are those of an enumeration
2556 -- type. The size is set to an arbitrary value, for use in analyzing the
2557 -- generic unit.
2559 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2560 Loc : constant Source_Ptr := Sloc (Def);
2561 Lo : Node_Id;
2562 Hi : Node_Id;
2564 Base : constant Entity_Id :=
2565 New_Internal_Entity
2566 (E_Floating_Point_Type, Current_Scope,
2567 Sloc (Defining_Identifier (Parent (Def))), 'G');
2569 begin
2570 Enter_Name (T);
2571 Mutate_Ekind (T, E_Enumeration_Subtype);
2572 Set_Etype (T, Base);
2573 Init_Size (T, 8);
2574 Reinit_Alignment (T);
2575 Set_Is_Generic_Type (T);
2576 Set_Is_Constrained (T);
2578 -- For semantic analysis, the bounds of the type must be set to some
2579 -- non-static value. The simplest is to create attribute nodes for those
2580 -- bounds, that refer to the type itself. These bounds are never
2581 -- analyzed but serve as place-holders.
2583 Lo :=
2584 Make_Attribute_Reference (Loc,
2585 Attribute_Name => Name_First,
2586 Prefix => New_Occurrence_Of (T, Loc));
2587 Set_Etype (Lo, T);
2589 Hi :=
2590 Make_Attribute_Reference (Loc,
2591 Attribute_Name => Name_Last,
2592 Prefix => New_Occurrence_Of (T, Loc));
2593 Set_Etype (Hi, T);
2595 Set_Scalar_Range (T,
2596 Make_Range (Loc,
2597 Low_Bound => Lo,
2598 High_Bound => Hi));
2600 Mutate_Ekind (Base, E_Enumeration_Type);
2601 Set_Etype (Base, Base);
2602 Init_Size (Base, 8);
2603 Reinit_Alignment (Base);
2604 Set_Is_Generic_Type (Base);
2605 Set_Scalar_Range (Base, Scalar_Range (T));
2606 Set_Parent (Base, Parent (Def));
2607 end Analyze_Formal_Discrete_Type;
2609 ----------------------------------
2610 -- Analyze_Formal_Floating_Type --
2611 ---------------------------------
2613 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2614 Base : constant Entity_Id :=
2615 New_Internal_Entity
2616 (E_Floating_Point_Type, Current_Scope,
2617 Sloc (Defining_Identifier (Parent (Def))), 'G');
2619 begin
2620 -- The various semantic attributes are taken from the predefined type
2621 -- Float, just so that all of them are initialized. Their values are
2622 -- never used because no constant folding or expansion takes place in
2623 -- the generic itself.
2625 Enter_Name (T);
2626 Mutate_Ekind (T, E_Floating_Point_Subtype);
2627 Set_Etype (T, Base);
2628 Set_Size_Info (T, (Standard_Float));
2629 Set_RM_Size (T, RM_Size (Standard_Float));
2630 Set_Digits_Value (T, Digits_Value (Standard_Float));
2631 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2632 Set_Is_Constrained (T);
2634 Set_Is_Generic_Type (Base);
2635 Set_Etype (Base, Base);
2636 Set_Size_Info (Base, (Standard_Float));
2637 Set_RM_Size (Base, RM_Size (Standard_Float));
2638 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2639 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2640 Set_Parent (Base, Parent (Def));
2642 Check_Restriction (No_Floating_Point, Def);
2643 end Analyze_Formal_Floating_Type;
2645 -----------------------------------
2646 -- Analyze_Formal_Interface_Type;--
2647 -----------------------------------
2649 procedure Analyze_Formal_Interface_Type
2650 (N : Node_Id;
2651 T : Entity_Id;
2652 Def : Node_Id)
2654 Loc : constant Source_Ptr := Sloc (N);
2655 New_N : Node_Id;
2657 begin
2658 New_N :=
2659 Make_Full_Type_Declaration (Loc,
2660 Defining_Identifier => T,
2661 Type_Definition => Def);
2663 Rewrite (N, New_N);
2665 -- Keep the aspects from the original node
2667 Move_Aspects (Original_Node (N), N);
2669 Analyze (N);
2670 Set_Is_Generic_Type (T);
2671 end Analyze_Formal_Interface_Type;
2673 ---------------------------------
2674 -- Analyze_Formal_Modular_Type --
2675 ---------------------------------
2677 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2678 begin
2679 -- Apart from their entity kind, generic modular types are treated like
2680 -- signed integer types, and have the same attributes.
2682 Analyze_Formal_Signed_Integer_Type (T, Def);
2683 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2684 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2686 end Analyze_Formal_Modular_Type;
2688 ---------------------------------------
2689 -- Analyze_Formal_Object_Declaration --
2690 ---------------------------------------
2692 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2693 E : constant Node_Id := Default_Expression (N);
2694 Id : constant Node_Id := Defining_Identifier (N);
2696 K : Entity_Kind;
2697 Parent_Installed : Boolean := False;
2698 T : Node_Id;
2700 begin
2701 Enter_Name (Id);
2703 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2705 -- Determine the mode of the formal object
2707 if Out_Present (N) then
2708 K := E_Generic_In_Out_Parameter;
2710 if not In_Present (N) then
2711 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2712 end if;
2714 else
2715 K := E_Generic_In_Parameter;
2716 end if;
2718 if Present (Subtype_Mark (N)) then
2719 Find_Type (Subtype_Mark (N));
2720 T := Entity (Subtype_Mark (N));
2722 -- Verify that there is no redundant null exclusion
2724 if Null_Exclusion_Present (N) then
2725 if not Is_Access_Type (T) then
2726 Error_Msg_N
2727 ("null exclusion can only apply to an access type", N);
2729 elsif Can_Never_Be_Null (T) then
2730 Error_Msg_NE
2731 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2732 end if;
2733 end if;
2735 -- Ada 2005 (AI-423): Formal object with an access definition
2737 else
2738 Check_Access_Definition (N);
2739 T := Access_Definition
2740 (Related_Nod => N,
2741 N => Access_Definition (N));
2742 end if;
2744 if Ekind (T) = E_Incomplete_Type then
2745 declare
2746 Error_Node : Node_Id;
2748 begin
2749 if Present (Subtype_Mark (N)) then
2750 Error_Node := Subtype_Mark (N);
2751 else
2752 Check_Access_Definition (N);
2753 Error_Node := Access_Definition (N);
2754 end if;
2756 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2757 end;
2758 end if;
2760 if K = E_Generic_In_Parameter then
2762 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2764 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2765 Error_Msg_N
2766 ("generic formal of mode IN must not be of limited type", N);
2767 Explain_Limited_Type (T, N);
2768 end if;
2770 if Is_Abstract_Type (T) then
2771 Error_Msg_N
2772 ("generic formal of mode IN must not be of abstract type", N);
2773 end if;
2775 if Present (E) then
2776 Preanalyze_Spec_Expression (E, T);
2778 -- The default for a ghost generic formal IN parameter of
2779 -- access-to-variable type should be a ghost object (SPARK
2780 -- RM 6.9(13)).
2782 if Is_Access_Variable (T) then
2783 Check_Ghost_Formal_Variable
2784 (Actual => E,
2785 Formal => Id,
2786 Is_Default => True);
2787 end if;
2789 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2790 Error_Msg_N
2791 ("initialization not allowed for limited types", E);
2792 Explain_Limited_Type (T, E);
2793 end if;
2794 end if;
2796 Mutate_Ekind (Id, K);
2797 Set_Etype (Id, T);
2799 -- Case of generic IN OUT parameter
2801 else
2802 -- If the formal has an unconstrained type, construct its actual
2803 -- subtype, as is done for subprogram formals. In this fashion, all
2804 -- its uses can refer to specific bounds.
2806 Mutate_Ekind (Id, K);
2807 Set_Etype (Id, T);
2809 if (Is_Array_Type (T) and then not Is_Constrained (T))
2810 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2811 then
2812 declare
2813 Non_Freezing_Ref : constant Node_Id :=
2814 New_Occurrence_Of (Id, Sloc (Id));
2815 Decl : Node_Id;
2817 begin
2818 -- Make sure the actual subtype doesn't generate bogus freezing
2820 Set_Must_Not_Freeze (Non_Freezing_Ref);
2821 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2822 Insert_Before_And_Analyze (N, Decl);
2823 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2824 end;
2825 else
2826 Set_Actual_Subtype (Id, T);
2827 end if;
2829 if Present (E) then
2830 Error_Msg_N
2831 ("initialization not allowed for `IN OUT` formals", N);
2832 end if;
2833 end if;
2835 Analyze_Aspect_Specifications (N, Id);
2837 if Parent_Installed then
2838 Remove_Parent;
2839 end if;
2840 end Analyze_Formal_Object_Declaration;
2842 ----------------------------------------------
2843 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2844 ----------------------------------------------
2846 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2847 (T : Entity_Id;
2848 Def : Node_Id)
2850 Loc : constant Source_Ptr := Sloc (Def);
2851 Base : constant Entity_Id :=
2852 New_Internal_Entity
2853 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2854 Sloc (Defining_Identifier (Parent (Def))), 'G');
2856 begin
2857 -- The semantic attributes are set for completeness only, their values
2858 -- will never be used, since all properties of the type are non-static.
2860 Enter_Name (T);
2861 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2862 Set_Etype (T, Base);
2863 Set_Size_Info (T, Standard_Integer);
2864 Set_RM_Size (T, RM_Size (Standard_Integer));
2865 Set_Small_Value (T, Ureal_1);
2866 Set_Delta_Value (T, Ureal_1);
2867 Set_Scalar_Range (T,
2868 Make_Range (Loc,
2869 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2870 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2871 Set_Is_Constrained (T);
2873 Set_Is_Generic_Type (Base);
2874 Set_Etype (Base, Base);
2875 Set_Size_Info (Base, Standard_Integer);
2876 Set_RM_Size (Base, RM_Size (Standard_Integer));
2877 Set_Small_Value (Base, Ureal_1);
2878 Set_Delta_Value (Base, Ureal_1);
2879 Set_Scalar_Range (Base, Scalar_Range (T));
2880 Set_Parent (Base, Parent (Def));
2882 Check_Restriction (No_Fixed_Point, Def);
2883 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2885 ----------------------------------------
2886 -- Analyze_Formal_Package_Declaration --
2887 ----------------------------------------
2889 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2890 Gen_Id : constant Node_Id := Name (N);
2891 Loc : constant Source_Ptr := Sloc (N);
2892 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2893 Formal : Entity_Id;
2894 Gen_Decl : Node_Id;
2895 Gen_Unit : Entity_Id;
2896 Renaming : Node_Id;
2898 Vis_Prims_List : Elist_Id := No_Elist;
2899 -- List of primitives made temporarily visible in the instantiation
2900 -- to match the visibility of the formal type.
2902 function Build_Local_Package return Node_Id;
2903 -- The formal package is rewritten so that its parameters are replaced
2904 -- with corresponding declarations. For parameters with bona fide
2905 -- associations these declarations are created by Analyze_Associations
2906 -- as for a regular instantiation. For boxed parameters, we preserve
2907 -- the formal declarations and analyze them, in order to introduce
2908 -- entities of the right kind in the environment of the formal.
2910 -------------------------
2911 -- Build_Local_Package --
2912 -------------------------
2914 function Build_Local_Package return Node_Id is
2915 Decls : List_Id;
2916 Pack_Decl : Node_Id;
2918 begin
2919 -- Within the formal, the name of the generic package is a renaming
2920 -- of the formal (as for a regular instantiation).
2922 Pack_Decl :=
2923 Make_Package_Declaration (Loc,
2924 Specification =>
2925 Copy_Generic_Node
2926 (Specification (Original_Node (Gen_Decl)),
2927 Empty, Instantiating => True));
2929 Renaming :=
2930 Make_Package_Renaming_Declaration (Loc,
2931 Defining_Unit_Name =>
2932 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2933 Name => New_Occurrence_Of (Formal, Loc));
2935 if Nkind (Gen_Id) = N_Identifier
2936 and then Chars (Gen_Id) = Chars (Pack_Id)
2937 then
2938 Error_Msg_NE
2939 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2940 end if;
2942 -- If the formal is declared with a box, or with an others choice,
2943 -- create corresponding declarations for all entities in the formal
2944 -- part, so that names with the proper types are available in the
2945 -- specification of the formal package.
2947 -- On the other hand, if there are no associations, then all the
2948 -- formals must have defaults, and this will be checked by the
2949 -- call to Analyze_Associations.
2951 if Box_Present (N)
2952 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2953 then
2954 declare
2955 Formal_Decl : Node_Id;
2957 begin
2958 -- TBA : for a formal package, need to recurse ???
2960 Decls := New_List;
2961 Formal_Decl :=
2962 First
2963 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2964 while Present (Formal_Decl) loop
2965 Append_To
2966 (Decls,
2967 Copy_Generic_Node
2968 (Formal_Decl, Empty, Instantiating => True));
2969 Next (Formal_Decl);
2970 end loop;
2971 end;
2973 -- If generic associations are present, use Analyze_Associations to
2974 -- create the proper renaming declarations.
2976 else
2977 declare
2978 Act_Tree : constant Node_Id :=
2979 Copy_Generic_Node
2980 (Original_Node (Gen_Decl), Empty,
2981 Instantiating => True);
2983 begin
2984 Generic_Renamings.Set_Last (0);
2985 Generic_Renamings_HTable.Reset;
2986 Instantiation_Node := N;
2988 Decls :=
2989 Analyze_Associations
2990 (I_Node => Original_Node (N),
2991 Formals => Generic_Formal_Declarations (Act_Tree),
2992 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2994 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2995 end;
2996 end if;
2998 Append (Renaming, To => Decls);
3000 -- Add generated declarations ahead of local declarations in
3001 -- the package.
3003 if No (Visible_Declarations (Specification (Pack_Decl))) then
3004 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3005 else
3006 Insert_List_Before
3007 (First (Visible_Declarations (Specification (Pack_Decl))),
3008 Decls);
3009 end if;
3011 return Pack_Decl;
3012 end Build_Local_Package;
3014 -- Local variables
3016 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3017 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3019 Associations : Boolean := True;
3020 New_N : Node_Id;
3021 Parent_Installed : Boolean := False;
3022 Parent_Instance : Entity_Id;
3023 Renaming_In_Par : Entity_Id;
3025 -- Start of processing for Analyze_Formal_Package_Declaration
3027 begin
3028 Check_Text_IO_Special_Unit (Gen_Id);
3030 Init_Env;
3031 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3032 Gen_Unit := Entity (Gen_Id);
3034 -- Check for a formal package that is a package renaming
3036 if Present (Renamed_Entity (Gen_Unit)) then
3038 -- Indicate that unit is used, before replacing it with renamed
3039 -- entity for use below.
3041 if In_Extended_Main_Source_Unit (N) then
3042 Set_Is_Instantiated (Gen_Unit);
3043 Generate_Reference (Gen_Unit, N);
3044 end if;
3046 Gen_Unit := Renamed_Entity (Gen_Unit);
3047 end if;
3049 if Ekind (Gen_Unit) /= E_Generic_Package then
3050 Error_Msg_N ("expect generic package name", Gen_Id);
3051 Restore_Env;
3052 goto Leave;
3054 elsif Gen_Unit = Current_Scope then
3055 Error_Msg_N
3056 ("generic package cannot be used as a formal package of itself",
3057 Gen_Id);
3058 Restore_Env;
3059 goto Leave;
3061 elsif In_Open_Scopes (Gen_Unit) then
3062 if Is_Compilation_Unit (Gen_Unit)
3063 and then Is_Child_Unit (Current_Scope)
3064 then
3065 -- Special-case the error when the formal is a parent, and
3066 -- continue analysis to minimize cascaded errors.
3068 Error_Msg_N
3069 ("generic parent cannot be used as formal package of a child "
3070 & "unit", Gen_Id);
3072 else
3073 Error_Msg_N
3074 ("generic package cannot be used as a formal package within "
3075 & "itself", Gen_Id);
3076 Restore_Env;
3077 goto Leave;
3078 end if;
3079 end if;
3081 -- Check that name of formal package does not hide name of generic,
3082 -- or its leading prefix. This check must be done separately because
3083 -- the name of the generic has already been analyzed.
3085 declare
3086 Gen_Name : Entity_Id;
3088 begin
3089 Gen_Name := Gen_Id;
3090 while Nkind (Gen_Name) = N_Expanded_Name loop
3091 Gen_Name := Prefix (Gen_Name);
3092 end loop;
3094 if Chars (Gen_Name) = Chars (Pack_Id) then
3095 Error_Msg_NE
3096 ("& is hidden within declaration of formal package",
3097 Gen_Id, Gen_Name);
3098 end if;
3099 end;
3101 if Box_Present (N)
3102 or else No (Generic_Associations (N))
3103 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3104 then
3105 Associations := False;
3106 end if;
3108 -- If there are no generic associations, the generic parameters appear
3109 -- as local entities and are instantiated like them. We copy the generic
3110 -- package declaration as if it were an instantiation, and analyze it
3111 -- like a regular package, except that we treat the formals as
3112 -- additional visible components.
3114 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3116 if In_Extended_Main_Source_Unit (N) then
3117 Set_Is_Instantiated (Gen_Unit);
3118 Generate_Reference (Gen_Unit, N);
3119 end if;
3121 Formal := New_Copy (Pack_Id);
3122 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3124 -- Make local generic without formals. The formals will be replaced with
3125 -- internal declarations.
3127 begin
3128 New_N := Build_Local_Package;
3130 -- If there are errors in the parameter list, Analyze_Associations
3131 -- raises Instantiation_Error. Patch the declaration to prevent further
3132 -- exception propagation.
3134 exception
3135 when Instantiation_Error =>
3136 Enter_Name (Formal);
3137 Mutate_Ekind (Formal, E_Variable);
3138 Set_Etype (Formal, Any_Type);
3139 Restore_Hidden_Primitives (Vis_Prims_List);
3141 if Parent_Installed then
3142 Remove_Parent;
3143 end if;
3145 goto Leave;
3146 end;
3148 Rewrite (N, New_N);
3149 Set_Defining_Unit_Name (Specification (New_N), Formal);
3150 Set_Generic_Parent (Specification (N), Gen_Unit);
3151 Set_Instance_Env (Gen_Unit, Formal);
3152 Set_Is_Generic_Instance (Formal);
3154 Enter_Name (Formal);
3155 Mutate_Ekind (Formal, E_Package);
3156 Set_Etype (Formal, Standard_Void_Type);
3157 Set_Inner_Instances (Formal, New_Elmt_List);
3159 -- It is unclear that any aspects can apply to a formal package
3160 -- declaration, given that they look like a hidden conformance
3161 -- requirement on the corresponding actual. However, Abstract_State
3162 -- must be treated specially because it generates declarations that
3163 -- must appear before other declarations in the specification and
3164 -- must be analyzed at once.
3166 if Present (Aspect_Specifications (Gen_Decl)) then
3167 if No (Aspect_Specifications (N)) then
3168 Set_Aspect_Specifications (N, New_List);
3169 end if;
3171 declare
3172 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3173 New_A : Node_Id;
3175 begin
3176 while Present (ASN) loop
3177 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3178 New_A :=
3179 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3180 Set_Entity (New_A, Formal);
3181 Set_Analyzed (New_A, False);
3182 Append (New_A, Aspect_Specifications (N));
3183 Analyze_Aspect_Specifications (N, Formal);
3184 exit;
3185 end if;
3187 Next (ASN);
3188 end loop;
3189 end;
3190 end if;
3192 Push_Scope (Formal);
3194 -- Manually set the SPARK_Mode from the context because the package
3195 -- declaration is never analyzed.
3197 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3198 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3199 Set_SPARK_Pragma_Inherited (Formal);
3200 Set_SPARK_Aux_Pragma_Inherited (Formal);
3202 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3204 -- Similarly, we have to make the name of the formal visible in the
3205 -- parent instance, to resolve properly fully qualified names that
3206 -- may appear in the generic unit. The parent instance has been
3207 -- placed on the scope stack ahead of the current scope.
3209 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3211 Renaming_In_Par :=
3212 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3213 Mutate_Ekind (Renaming_In_Par, E_Package);
3214 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3215 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3216 Set_Scope (Renaming_In_Par, Parent_Instance);
3217 Set_Parent (Renaming_In_Par, Parent (Formal));
3218 Set_Renamed_Entity (Renaming_In_Par, Formal);
3219 Append_Entity (Renaming_In_Par, Parent_Instance);
3220 end if;
3222 -- A formal package declaration behaves as a package instantiation with
3223 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3224 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3225 -- all SPARK_Mode pragmas within the generic_package_name.
3227 if SPARK_Mode /= On then
3228 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3230 -- Mark the formal spec in case the body is instantiated at a later
3231 -- pass. This preserves the original context in effect for the body.
3233 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3234 end if;
3236 Analyze (Specification (N));
3238 -- The formals for which associations are provided are not visible
3239 -- outside of the formal package. The others are still declared by a
3240 -- formal parameter declaration.
3242 -- If there are no associations, the only local entity to hide is the
3243 -- generated package renaming itself.
3245 declare
3246 E : Entity_Id;
3248 begin
3249 E := First_Entity (Formal);
3250 while Present (E) loop
3251 if Associations and then not Is_Generic_Formal (E) then
3252 Set_Is_Hidden (E);
3253 end if;
3255 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3256 Set_Is_Hidden (E);
3257 exit;
3258 end if;
3260 Next_Entity (E);
3261 end loop;
3262 end;
3264 End_Package_Scope (Formal);
3265 Restore_Hidden_Primitives (Vis_Prims_List);
3267 if Parent_Installed then
3268 Remove_Parent;
3269 end if;
3271 Restore_Env;
3273 -- Inside the generic unit, the formal package is a regular package, but
3274 -- no body is needed for it. Note that after instantiation, the defining
3275 -- unit name we need is in the new tree and not in the original (see
3276 -- Package_Instantiation). A generic formal package is an instance, and
3277 -- can be used as an actual for an inner instance.
3279 Set_Has_Completion (Formal, True);
3281 -- Add semantic information to the original defining identifier.
3283 Mutate_Ekind (Pack_Id, E_Package);
3284 Set_Etype (Pack_Id, Standard_Void_Type);
3285 Set_Scope (Pack_Id, Scope (Formal));
3286 Set_Has_Completion (Pack_Id, True);
3288 <<Leave>>
3289 -- Unclear that any other aspects may appear here, analyze them
3290 -- for completion, given that the grammar allows their appearance.
3292 Analyze_Aspect_Specifications (N, Pack_Id);
3294 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3295 end Analyze_Formal_Package_Declaration;
3297 ---------------------------------
3298 -- Analyze_Formal_Private_Type --
3299 ---------------------------------
3301 procedure Analyze_Formal_Private_Type
3302 (N : Node_Id;
3303 T : Entity_Id;
3304 Def : Node_Id)
3306 begin
3307 New_Private_Type (N, T, Def);
3309 -- Set the size to an arbitrary but legal value
3311 Set_Size_Info (T, Standard_Integer);
3312 Set_RM_Size (T, RM_Size (Standard_Integer));
3313 end Analyze_Formal_Private_Type;
3315 ------------------------------------
3316 -- Analyze_Formal_Incomplete_Type --
3317 ------------------------------------
3319 procedure Analyze_Formal_Incomplete_Type
3320 (T : Entity_Id;
3321 Def : Node_Id)
3323 begin
3324 Enter_Name (T);
3325 Mutate_Ekind (T, E_Incomplete_Type);
3326 Set_Etype (T, T);
3327 Set_Private_Dependents (T, New_Elmt_List);
3329 if Tagged_Present (Def) then
3330 Set_Is_Tagged_Type (T);
3331 Make_Class_Wide_Type (T);
3332 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3333 end if;
3334 end Analyze_Formal_Incomplete_Type;
3336 ----------------------------------------
3337 -- Analyze_Formal_Signed_Integer_Type --
3338 ----------------------------------------
3340 procedure Analyze_Formal_Signed_Integer_Type
3341 (T : Entity_Id;
3342 Def : Node_Id)
3344 Base : constant Entity_Id :=
3345 New_Internal_Entity
3346 (E_Signed_Integer_Type,
3347 Current_Scope,
3348 Sloc (Defining_Identifier (Parent (Def))), 'G');
3350 begin
3351 Enter_Name (T);
3353 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3354 Set_Etype (T, Base);
3355 Set_Size_Info (T, Standard_Integer);
3356 Set_RM_Size (T, RM_Size (Standard_Integer));
3357 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3358 Set_Is_Constrained (T);
3360 Set_Is_Generic_Type (Base);
3361 Set_Size_Info (Base, Standard_Integer);
3362 Set_RM_Size (Base, RM_Size (Standard_Integer));
3363 Set_Etype (Base, Base);
3364 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3365 Set_Parent (Base, Parent (Def));
3366 end Analyze_Formal_Signed_Integer_Type;
3368 -------------------------------------------
3369 -- Analyze_Formal_Subprogram_Declaration --
3370 -------------------------------------------
3372 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3373 Spec : constant Node_Id := Specification (N);
3374 Def : constant Node_Id := Default_Name (N);
3375 Expr : constant Node_Id := Expression (N);
3376 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3378 Parent_Installed : Boolean := False;
3379 Subp : Entity_Id;
3381 begin
3382 if Nam = Error then
3383 return;
3384 end if;
3386 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3387 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3388 goto Leave;
3389 end if;
3391 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3393 Analyze_Subprogram_Declaration (N);
3394 Set_Is_Formal_Subprogram (Nam);
3395 Set_Has_Completion (Nam);
3397 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3398 Set_Is_Abstract_Subprogram (Nam);
3400 Set_Is_Dispatching_Operation (Nam);
3402 -- A formal abstract procedure cannot have a null default
3403 -- (RM 12.6(4.1/2)).
3405 if Nkind (Spec) = N_Procedure_Specification
3406 and then Null_Present (Spec)
3407 then
3408 Error_Msg_N
3409 ("a formal abstract subprogram cannot default to null", Spec);
3410 end if;
3412 -- A formal abstract function cannot have an expression default
3413 -- (expression defaults are allowed for nonabstract formal functions
3414 -- when extensions are enabled).
3416 if Nkind (Spec) = N_Function_Specification
3417 and then Present (Expr)
3418 then
3419 Error_Msg_N
3420 ("a formal abstract subprogram cannot default to an expression",
3421 Spec);
3422 end if;
3424 declare
3425 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3426 begin
3427 if No (Ctrl_Type) then
3428 Error_Msg_N
3429 ("abstract formal subprogram must have a controlling type",
3432 elsif Ada_Version >= Ada_2012
3433 and then Is_Incomplete_Type (Ctrl_Type)
3434 then
3435 Error_Msg_NE
3436 ("controlling type of abstract formal subprogram cannot "
3437 & "be incomplete type", N, Ctrl_Type);
3439 else
3440 Check_Controlling_Formals (Ctrl_Type, Nam);
3441 end if;
3442 end;
3443 end if;
3445 -- Default name is resolved at the point of instantiation
3447 if Box_Present (N) then
3448 null;
3450 -- Default name is bound at the point of generic declaration
3452 elsif Present (Def) then
3453 if Nkind (Def) = N_Operator_Symbol then
3454 Find_Direct_Name (Def);
3456 elsif Nkind (Def) /= N_Attribute_Reference then
3457 Analyze (Def);
3459 else
3460 -- For an attribute reference, analyze the prefix and verify
3461 -- that it has the proper profile for the subprogram.
3463 Analyze (Prefix (Def));
3464 Valid_Default_Attribute (Nam, Def);
3465 goto Leave;
3466 end if;
3468 -- The default for a ghost generic formal procedure should be a ghost
3469 -- procedure (SPARK RM 6.9(13)).
3471 if Ekind (Nam) = E_Procedure then
3472 declare
3473 Def_E : Entity_Id := Empty;
3474 begin
3475 if Nkind (Def) in N_Has_Entity then
3476 Def_E := Entity (Def);
3477 end if;
3479 Check_Ghost_Formal_Procedure_Or_Package
3480 (N => Def,
3481 Actual => Def_E,
3482 Formal => Nam,
3483 Is_Default => True);
3484 end;
3485 end if;
3487 -- Default name may be overloaded, in which case the interpretation
3488 -- with the correct profile must be selected, as for a renaming.
3489 -- If the definition is an indexed component, it must denote a
3490 -- member of an entry family. If it is a selected component, it
3491 -- can be a protected operation.
3493 if Etype (Def) = Any_Type then
3494 goto Leave;
3496 elsif Nkind (Def) = N_Selected_Component then
3497 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3498 Error_Msg_N ("expect valid subprogram name as default", Def);
3499 end if;
3501 elsif Nkind (Def) = N_Indexed_Component then
3502 if Is_Entity_Name (Prefix (Def)) then
3503 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3504 Error_Msg_N ("expect valid subprogram name as default", Def);
3505 end if;
3507 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3508 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3509 E_Entry_Family
3510 then
3511 Error_Msg_N ("expect valid subprogram name as default", Def);
3512 end if;
3514 else
3515 Error_Msg_N ("expect valid subprogram name as default", Def);
3516 goto Leave;
3517 end if;
3519 elsif Nkind (Def) = N_Character_Literal then
3521 -- Needs some type checks: subprogram should be parameterless???
3523 Resolve (Def, (Etype (Nam)));
3525 elsif not Is_Entity_Name (Def)
3526 or else not Is_Overloadable (Entity (Def))
3527 then
3528 Error_Msg_N ("expect valid subprogram name as default", Def);
3529 goto Leave;
3531 elsif not Is_Overloaded (Def) then
3532 Subp := Entity (Def);
3534 if Subp = Nam then
3535 Error_Msg_N ("premature usage of formal subprogram", Def);
3537 elsif not Entity_Matches_Spec (Subp, Nam) then
3538 Error_Msg_N ("no visible entity matches specification", Def);
3539 end if;
3541 -- More than one interpretation, so disambiguate as for a renaming
3543 else
3544 declare
3545 I : Interp_Index;
3546 I1 : Interp_Index := 0;
3547 It : Interp;
3548 It1 : Interp;
3550 begin
3551 Subp := Any_Id;
3552 Get_First_Interp (Def, I, It);
3553 while Present (It.Nam) loop
3554 if Entity_Matches_Spec (It.Nam, Nam) then
3555 if Subp /= Any_Id then
3556 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3558 if It1 = No_Interp then
3559 Error_Msg_N ("ambiguous default subprogram", Def);
3560 else
3561 Subp := It1.Nam;
3562 end if;
3564 exit;
3566 else
3567 I1 := I;
3568 Subp := It.Nam;
3569 end if;
3570 end if;
3572 Get_Next_Interp (I, It);
3573 end loop;
3574 end;
3576 if Subp /= Any_Id then
3578 -- Subprogram found, generate reference to it
3580 Set_Entity (Def, Subp);
3581 Generate_Reference (Subp, Def);
3583 if Subp = Nam then
3584 Error_Msg_N ("premature usage of formal subprogram", Def);
3586 elsif Ekind (Subp) /= E_Operator then
3587 Check_Mode_Conformant (Subp, Nam);
3588 end if;
3590 else
3591 Error_Msg_N ("no visible subprogram matches specification", N);
3592 end if;
3593 end if;
3595 -- When extensions are enabled, an expression can be given as default
3596 -- for a formal function. The expression must be of the function result
3597 -- type and can reference formal parameters of the function.
3599 elsif Present (Expr) then
3600 Push_Scope (Nam);
3601 Install_Formals (Nam);
3602 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3603 End_Scope;
3604 end if;
3606 <<Leave>>
3607 Analyze_Aspect_Specifications (N, Nam);
3609 if Parent_Installed then
3610 Remove_Parent;
3611 end if;
3612 end Analyze_Formal_Subprogram_Declaration;
3614 -------------------------------------
3615 -- Analyze_Formal_Type_Declaration --
3616 -------------------------------------
3618 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3619 Def : constant Node_Id := Formal_Type_Definition (N);
3621 Parent_Installed : Boolean := False;
3622 T : Entity_Id;
3624 begin
3625 T := Defining_Identifier (N);
3627 if Present (Discriminant_Specifications (N))
3628 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3629 then
3630 Error_Msg_N
3631 ("discriminants not allowed for this formal type", T);
3632 end if;
3634 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3636 -- Enter the new name, and branch to specific routine
3638 case Nkind (Def) is
3639 when N_Formal_Private_Type_Definition =>
3640 Analyze_Formal_Private_Type (N, T, Def);
3642 when N_Formal_Derived_Type_Definition =>
3643 Analyze_Formal_Derived_Type (N, T, Def);
3645 when N_Formal_Incomplete_Type_Definition =>
3646 Analyze_Formal_Incomplete_Type (T, Def);
3648 when N_Formal_Discrete_Type_Definition =>
3649 Analyze_Formal_Discrete_Type (T, Def);
3651 when N_Formal_Signed_Integer_Type_Definition =>
3652 Analyze_Formal_Signed_Integer_Type (T, Def);
3654 when N_Formal_Modular_Type_Definition =>
3655 Analyze_Formal_Modular_Type (T, Def);
3657 when N_Formal_Floating_Point_Definition =>
3658 Analyze_Formal_Floating_Type (T, Def);
3660 when N_Formal_Ordinary_Fixed_Point_Definition =>
3661 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3663 when N_Formal_Decimal_Fixed_Point_Definition =>
3664 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3666 when N_Array_Type_Definition =>
3667 Analyze_Formal_Array_Type (T, Def);
3669 when N_Access_Function_Definition
3670 | N_Access_Procedure_Definition
3671 | N_Access_To_Object_Definition
3673 Analyze_Generic_Access_Type (T, Def);
3675 -- Ada 2005: a interface declaration is encoded as an abstract
3676 -- record declaration or a abstract type derivation.
3678 when N_Record_Definition =>
3679 Analyze_Formal_Interface_Type (N, T, Def);
3681 when N_Derived_Type_Definition =>
3682 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3684 when N_Error =>
3685 null;
3687 when others =>
3688 raise Program_Error;
3689 end case;
3691 -- A formal type declaration declares a type and its first
3692 -- subtype.
3694 Set_Is_Generic_Type (T);
3695 Set_Is_First_Subtype (T);
3697 if Present (Default_Subtype_Mark (Original_Node (N))) then
3698 Validate_Formal_Type_Default (N);
3699 end if;
3701 Analyze_Aspect_Specifications (N, T);
3703 if Parent_Installed then
3704 Remove_Parent;
3705 end if;
3706 end Analyze_Formal_Type_Declaration;
3708 ------------------------------------
3709 -- Analyze_Function_Instantiation --
3710 ------------------------------------
3712 procedure Analyze_Function_Instantiation (N : Node_Id) is
3713 begin
3714 Analyze_Subprogram_Instantiation (N, E_Function);
3715 end Analyze_Function_Instantiation;
3717 ---------------------------------
3718 -- Analyze_Generic_Access_Type --
3719 ---------------------------------
3721 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3722 begin
3723 Enter_Name (T);
3725 if Nkind (Def) = N_Access_To_Object_Definition then
3726 Access_Type_Declaration (T, Def);
3728 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3729 and then No (Full_View (Designated_Type (T)))
3730 and then not Is_Generic_Type (Designated_Type (T))
3731 then
3732 Error_Msg_N ("premature usage of incomplete type", Def);
3734 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3735 Error_Msg_N
3736 ("only a subtype mark is allowed in a formal", Def);
3737 end if;
3739 else
3740 Access_Subprogram_Declaration (T, Def);
3741 end if;
3742 end Analyze_Generic_Access_Type;
3744 ---------------------------------
3745 -- Analyze_Generic_Formal_Part --
3746 ---------------------------------
3748 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3749 Gen_Parm_Decl : Node_Id;
3751 begin
3752 -- The generic formals are processed in the scope of the generic unit,
3753 -- where they are immediately visible. The scope is installed by the
3754 -- caller.
3756 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3757 while Present (Gen_Parm_Decl) loop
3758 Analyze (Gen_Parm_Decl);
3759 Next (Gen_Parm_Decl);
3760 end loop;
3762 Generate_Reference_To_Generic_Formals (Current_Scope);
3764 -- For Ada 2022, some formal parameters can carry aspects, which must
3765 -- be name-resolved at the end of the list of formal parameters (which
3766 -- has the semantics of a declaration list).
3768 Analyze_Contracts (Generic_Formal_Declarations (N));
3769 end Analyze_Generic_Formal_Part;
3771 ------------------------------------------
3772 -- Analyze_Generic_Package_Declaration --
3773 ------------------------------------------
3775 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3776 Decls : constant List_Id := Visible_Declarations (Specification (N));
3777 Loc : constant Source_Ptr := Sloc (N);
3779 Decl : Node_Id;
3780 Id : Entity_Id;
3781 New_N : Node_Id;
3782 Renaming : Node_Id;
3783 Save_Parent : Node_Id;
3785 begin
3786 -- A generic may grant access to its private enclosing context depending
3787 -- on the placement of its corresponding body. From elaboration point of
3788 -- view, the flow of execution may enter this private context, and then
3789 -- reach an external unit, thus producing a dependency on that external
3790 -- unit. For such a path to be properly discovered and encoded in the
3791 -- ALI file of the main unit, let the ABE mechanism process the body of
3792 -- the main unit, and encode all relevant invocation constructs and the
3793 -- relations between them.
3795 Mark_Save_Invocation_Graph_Of_Body;
3797 -- We introduce a renaming of the enclosing package, to have a usable
3798 -- entity as the prefix of an expanded name for a local entity of the
3799 -- form Par.P.Q, where P is the generic package. This is because a local
3800 -- entity named P may hide it, so that the usual visibility rules in
3801 -- the instance will not resolve properly.
3803 Renaming :=
3804 Make_Package_Renaming_Declaration (Loc,
3805 Defining_Unit_Name =>
3806 Make_Defining_Identifier (Loc,
3807 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3808 Name =>
3809 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3811 -- The declaration is inserted before other declarations, but before
3812 -- pragmas that may be library-unit pragmas and must appear before other
3813 -- declarations. The pragma Compile_Time_Error is not in this class, and
3814 -- may contain an expression that includes such a qualified name, so the
3815 -- renaming declaration must appear before it.
3817 -- Are there other pragmas that require this special handling ???
3819 if Present (Decls) then
3820 Decl := First (Decls);
3821 while Present (Decl)
3822 and then Nkind (Decl) = N_Pragma
3823 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3824 loop
3825 Next (Decl);
3826 end loop;
3828 if Present (Decl) then
3829 Insert_Before (Decl, Renaming);
3830 else
3831 Append (Renaming, Visible_Declarations (Specification (N)));
3832 end if;
3834 else
3835 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3836 end if;
3838 -- Create copy of generic unit, and save for instantiation. If the unit
3839 -- is a child unit, do not copy the specifications for the parent, which
3840 -- are not part of the generic tree.
3842 Save_Parent := Parent_Spec (N);
3843 Set_Parent_Spec (N, Empty);
3845 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3846 Set_Parent_Spec (New_N, Save_Parent);
3847 Rewrite (N, New_N);
3849 -- Collect all contract-related source pragmas found within the template
3850 -- and attach them to the contract of the package spec. This contract is
3851 -- used in the capture of global references within annotations.
3853 Create_Generic_Contract (N);
3855 Id := Defining_Entity (N);
3856 Generate_Definition (Id);
3858 -- Expansion is not applied to generic units
3860 Start_Generic;
3862 Enter_Name (Id);
3863 Mutate_Ekind (Id, E_Generic_Package);
3864 Set_Is_Not_Self_Hidden (Id);
3865 Set_Etype (Id, Standard_Void_Type);
3867 -- Set SPARK_Mode from context
3869 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3870 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3871 Set_SPARK_Pragma_Inherited (Id);
3872 Set_SPARK_Aux_Pragma_Inherited (Id);
3874 -- Preserve relevant elaboration-related attributes of the context which
3875 -- are no longer available or very expensive to recompute once analysis,
3876 -- resolution, and expansion are over.
3878 Mark_Elaboration_Attributes
3879 (N_Id => Id,
3880 Checks => True,
3881 Warnings => True);
3883 -- Analyze aspects now, so that generated pragmas appear in the
3884 -- declarations before building and analyzing the generic copy.
3886 Analyze_Aspect_Specifications (N, Id);
3888 Push_Scope (Id);
3889 Enter_Generic_Scope (Id);
3890 Set_Inner_Instances (Id, New_Elmt_List);
3892 Set_Categorization_From_Pragmas (N);
3893 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3895 -- Link the declaration of the generic homonym in the generic copy to
3896 -- the package it renames, so that it is always resolved properly.
3898 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3899 Set_Entity (Associated_Node (Name (Renaming)), Id);
3901 -- For a library unit, we have reconstructed the entity for the unit,
3902 -- and must reset it in the library tables.
3904 if Nkind (Parent (N)) = N_Compilation_Unit then
3905 Set_Cunit_Entity (Current_Sem_Unit, Id);
3906 end if;
3908 Analyze_Generic_Formal_Part (N);
3910 -- After processing the generic formals, analysis proceeds as for a
3911 -- non-generic package.
3913 Analyze (Specification (N));
3915 Validate_Categorization_Dependency (N, Id);
3917 End_Generic;
3919 End_Package_Scope (Id);
3920 Exit_Generic_Scope (Id);
3922 -- If the generic appears within a package unit, the body of that unit
3923 -- has to be present for instantiation and inlining.
3925 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3926 Set_Body_Needed_For_Inlining
3927 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3928 end if;
3930 if Nkind (Parent (N)) /= N_Compilation_Unit then
3931 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3932 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3933 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3935 else
3936 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3937 Validate_RT_RAT_Component (N);
3939 -- If this is a spec without a body, check that generic parameters
3940 -- are referenced.
3942 if not Body_Required (Parent (N)) then
3943 Check_References (Id);
3944 end if;
3945 end if;
3947 -- If there is a specified storage pool in the context, create an
3948 -- aspect on the package declaration, so that it is used in any
3949 -- instance that does not override it.
3951 if Present (Default_Pool) then
3952 declare
3953 ASN : Node_Id;
3955 begin
3956 ASN :=
3957 Make_Aspect_Specification (Loc,
3958 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3959 Expression => New_Copy (Default_Pool));
3961 if No (Aspect_Specifications (Specification (N))) then
3962 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3963 else
3964 Append (ASN, Aspect_Specifications (Specification (N)));
3965 end if;
3966 end;
3967 end if;
3968 end Analyze_Generic_Package_Declaration;
3970 --------------------------------------------
3971 -- Analyze_Generic_Subprogram_Declaration --
3972 --------------------------------------------
3974 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3975 Formals : List_Id;
3976 Id : Entity_Id;
3977 New_N : Node_Id;
3978 Result_Type : Entity_Id;
3979 Save_Parent : Node_Id;
3980 Spec : Node_Id;
3981 Typ : Entity_Id;
3983 begin
3984 -- A generic may grant access to its private enclosing context depending
3985 -- on the placement of its corresponding body. From elaboration point of
3986 -- view, the flow of execution may enter this private context, and then
3987 -- reach an external unit, thus producing a dependency on that external
3988 -- unit. For such a path to be properly discovered and encoded in the
3989 -- ALI file of the main unit, let the ABE mechanism process the body of
3990 -- the main unit, and encode all relevant invocation constructs and the
3991 -- relations between them.
3993 Mark_Save_Invocation_Graph_Of_Body;
3995 -- Create copy of generic unit, and save for instantiation. If the unit
3996 -- is a child unit, do not copy the specifications for the parent, which
3997 -- are not part of the generic tree.
3999 Save_Parent := Parent_Spec (N);
4000 Set_Parent_Spec (N, Empty);
4002 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4003 Set_Parent_Spec (New_N, Save_Parent);
4004 Rewrite (N, New_N);
4006 -- Collect all contract-related source pragmas found within the template
4007 -- and attach them to the contract of the subprogram spec. This contract
4008 -- is used in the capture of global references within annotations.
4010 Create_Generic_Contract (N);
4012 Spec := Specification (N);
4013 Id := Defining_Entity (Spec);
4014 Generate_Definition (Id);
4016 if Nkind (Id) = N_Defining_Operator_Symbol then
4017 Error_Msg_N
4018 ("operator symbol not allowed for generic subprogram", Id);
4019 end if;
4021 Start_Generic;
4023 Enter_Name (Id);
4024 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4026 Push_Scope (Id);
4027 Enter_Generic_Scope (Id);
4028 Set_Inner_Instances (Id, New_Elmt_List);
4029 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4031 Analyze_Generic_Formal_Part (N);
4033 if Nkind (Spec) = N_Function_Specification then
4034 Mutate_Ekind (Id, E_Generic_Function);
4035 else
4036 Mutate_Ekind (Id, E_Generic_Procedure);
4037 end if;
4039 -- Set SPARK_Mode from context
4041 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4042 Set_SPARK_Pragma_Inherited (Id);
4044 -- Preserve relevant elaboration-related attributes of the context which
4045 -- are no longer available or very expensive to recompute once analysis,
4046 -- resolution, and expansion are over.
4048 Mark_Elaboration_Attributes
4049 (N_Id => Id,
4050 Checks => True,
4051 Warnings => True);
4053 Formals := Parameter_Specifications (Spec);
4055 if Present (Formals) then
4056 Process_Formals (Formals, Spec);
4057 end if;
4059 if Nkind (Spec) = N_Function_Specification then
4060 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4061 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4062 Set_Etype (Id, Result_Type);
4064 -- Check restriction imposed by AI05-073: a generic function
4065 -- cannot return an abstract type or an access to such.
4067 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4068 Error_Msg_N
4069 ("generic function cannot have an access result "
4070 & "that designates an abstract type", Spec);
4071 end if;
4073 else
4074 Find_Type (Result_Definition (Spec));
4075 Typ := Entity (Result_Definition (Spec));
4077 if Is_Abstract_Type (Typ)
4078 and then Ada_Version >= Ada_2012
4079 then
4080 Error_Msg_N
4081 ("generic function cannot have abstract result type", Spec);
4082 end if;
4084 -- If a null exclusion is imposed on the result type, then create
4085 -- a null-excluding itype (an access subtype) and use it as the
4086 -- function's Etype.
4088 if Is_Access_Type (Typ)
4089 and then Null_Exclusion_Present (Spec)
4090 then
4091 Set_Etype (Id,
4092 Create_Null_Excluding_Itype
4093 (T => Typ,
4094 Related_Nod => Spec,
4095 Scope_Id => Defining_Unit_Name (Spec)));
4096 else
4097 Set_Etype (Id, Typ);
4098 end if;
4099 end if;
4101 else
4102 Set_Etype (Id, Standard_Void_Type);
4103 end if;
4105 Set_Is_Not_Self_Hidden (Id);
4107 -- Analyze the aspects of the generic copy to ensure that all generated
4108 -- pragmas (if any) perform their semantic effects.
4110 Analyze_Aspect_Specifications (N, Id);
4112 -- For a library unit, we have reconstructed the entity for the unit,
4113 -- and must reset it in the library tables. We also make sure that
4114 -- Body_Required is set properly in the original compilation unit node.
4116 if Nkind (Parent (N)) = N_Compilation_Unit then
4117 Set_Cunit_Entity (Current_Sem_Unit, Id);
4118 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4119 end if;
4121 -- If the generic appears within a package unit, the body of that unit
4122 -- has to be present for instantiation and inlining.
4124 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4125 and then Unit_Requires_Body (Id)
4126 then
4127 Set_Body_Needed_For_Inlining
4128 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4129 end if;
4131 Set_Categorization_From_Pragmas (N);
4132 Validate_Categorization_Dependency (N, Id);
4134 -- Capture all global references that occur within the profile of the
4135 -- generic subprogram. Aspects are not part of this processing because
4136 -- they must be delayed. If processed now, Save_Global_References will
4137 -- destroy the Associated_Node links and prevent the capture of global
4138 -- references when the contract of the generic subprogram is analyzed.
4140 Save_Global_References (Original_Node (N));
4142 End_Generic;
4143 End_Scope;
4144 Exit_Generic_Scope (Id);
4145 Generate_Reference_To_Formals (Id);
4147 List_Inherited_Pre_Post_Aspects (Id);
4148 end Analyze_Generic_Subprogram_Declaration;
4150 -----------------------------------
4151 -- Analyze_Package_Instantiation --
4152 -----------------------------------
4154 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4155 -- must be replaced by gotos which jump to the end of the routine in order
4156 -- to restore the Ghost and SPARK modes.
4158 procedure Analyze_Package_Instantiation (N : Node_Id) is
4159 Has_Inline_Always : Boolean := False;
4160 -- Set if the generic unit contains any subprograms with Inline_Always.
4161 -- Only relevant when back-end inlining is not enabled.
4163 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4164 -- Return True if inlining is active and Gen_Unit contains inlined
4165 -- subprograms. In this case, we may either instantiate the body when
4166 -- front-end inlining is enabled, or add a pending instantiation when
4167 -- back-end inlining is enabled. In the former case, this may cause
4168 -- superfluous instantiations, but in either case we need to perform
4169 -- the instantiation of the body in the context of the instance and
4170 -- not in that of the point of inlining.
4172 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4173 -- Return True if Gen_Unit needs to have its body instantiated in the
4174 -- context of N. This in particular excludes generic contexts.
4176 -----------------------
4177 -- Might_Inline_Subp --
4178 -----------------------
4180 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4181 E : Entity_Id;
4183 begin
4184 if Inline_Processing_Required then
4185 -- No need to recompute the answer if we know it is positive
4186 -- and back-end inlining is enabled.
4188 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4189 return True;
4190 end if;
4192 E := First_Entity (Gen_Unit);
4193 while Present (E) loop
4194 if Is_Subprogram (E) and then Is_Inlined (E) then
4195 -- Remember if there are any subprograms with Inline_Always
4197 if Has_Pragma_Inline_Always (E) then
4198 Has_Inline_Always := True;
4199 end if;
4201 Set_Is_Inlined (Gen_Unit);
4202 return True;
4203 end if;
4205 Next_Entity (E);
4206 end loop;
4207 end if;
4209 return False;
4210 end Might_Inline_Subp;
4212 -------------------------------
4213 -- Needs_Body_Instantiated --
4214 -------------------------------
4216 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4217 begin
4218 -- No need to instantiate bodies in generic units
4220 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4221 return False;
4222 end if;
4224 -- If the instantiation is in the main unit, then the body is needed
4226 if Is_In_Main_Unit (N) then
4227 return True;
4228 end if;
4230 -- In GNATprove mode, never instantiate bodies outside of the main
4231 -- unit, as it does not use frontend/backend inlining in the way that
4232 -- GNAT does, so does not benefit from such instantiations. On the
4233 -- contrary, such instantiations may bring artificial constraints,
4234 -- as for example such bodies may require preprocessing.
4236 if GNATprove_Mode then
4237 return False;
4238 end if;
4240 -- If not, then again no need to instantiate bodies in generic units
4242 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4243 return False;
4244 end if;
4246 -- Here we have a special handling for back-end inlining: if inline
4247 -- processing is required, then we unconditionally want to have the
4248 -- body instantiated. The reason is that Might_Inline_Subp does not
4249 -- catch all the cases (as it does not recurse into nested packages)
4250 -- so this avoids the need to patch things up afterwards. Moreover,
4251 -- these instantiations are only performed on demand when back-end
4252 -- inlining is enabled, so this causes very little extra work.
4254 if Inline_Processing_Required and then Back_End_Inlining then
4255 return True;
4256 end if;
4258 -- We want to have the bodies instantiated in non-main units if
4259 -- they might contribute inlined subprograms.
4261 return Might_Inline_Subp (Gen_Unit);
4262 end Needs_Body_Instantiated;
4264 -- Local declarations
4266 Gen_Id : constant Node_Id := Name (N);
4267 Inst_Id : constant Entity_Id := Defining_Entity (N);
4268 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4269 Loc : constant Source_Ptr := Sloc (N);
4271 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4272 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4273 Saved_ISMP : constant Boolean :=
4274 Ignore_SPARK_Mode_Pragmas_In_Instance;
4275 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4276 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4277 -- Save the Ghost and SPARK mode-related data to restore on exit
4279 Saved_Style_Check : constant Boolean := Style_Check;
4280 -- Save style check mode for restore on exit
4282 Act_Decl : Node_Id;
4283 Act_Decl_Name : Node_Id;
4284 Act_Decl_Id : Entity_Id;
4285 Act_Spec : Node_Id;
4286 Act_Tree : Node_Id;
4287 Env_Installed : Boolean := False;
4288 Gen_Decl : Node_Id;
4289 Gen_Spec : Node_Id;
4290 Gen_Unit : Entity_Id;
4291 Inline_Now : Boolean := False;
4292 Needs_Body : Boolean;
4293 Parent_Installed : Boolean := False;
4294 Renaming_List : List_Id;
4295 Unit_Renaming : Node_Id;
4297 Vis_Prims_List : Elist_Id := No_Elist;
4298 -- List of primitives made temporarily visible in the instantiation
4299 -- to match the visibility of the formal type
4301 -- Start of processing for Analyze_Package_Instantiation
4303 begin
4304 -- Preserve relevant elaboration-related attributes of the context which
4305 -- are no longer available or very expensive to recompute once analysis,
4306 -- resolution, and expansion are over.
4308 Mark_Elaboration_Attributes
4309 (N_Id => N,
4310 Checks => True,
4311 Level => True,
4312 Modes => True,
4313 Warnings => True);
4315 -- Very first thing: check for Text_IO special unit in case we are
4316 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4318 Check_Text_IO_Special_Unit (Name (N));
4320 -- Make node global for error reporting
4322 Instantiation_Node := N;
4324 -- Case of instantiation of a generic package
4326 if Nkind (N) = N_Package_Instantiation then
4327 Act_Decl_Id := New_Copy (Defining_Entity (N));
4329 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4330 Act_Decl_Name :=
4331 Make_Defining_Program_Unit_Name (Loc,
4332 Name =>
4333 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4334 Defining_Identifier => Act_Decl_Id);
4335 else
4336 Act_Decl_Name := Act_Decl_Id;
4337 end if;
4339 -- Case of instantiation of a formal package
4341 else
4342 Act_Decl_Id := Defining_Identifier (N);
4343 Act_Decl_Name := Act_Decl_Id;
4344 end if;
4346 Generate_Definition (Act_Decl_Id);
4347 Mutate_Ekind (Act_Decl_Id, E_Package);
4348 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4350 -- Initialize list of incomplete actuals before analysis
4352 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4354 Preanalyze_Actuals (N, Act_Decl_Id);
4356 -- Turn off style checking in instances. If the check is enabled on the
4357 -- generic unit, a warning in an instance would just be noise. If not
4358 -- enabled on the generic, then a warning in an instance is just wrong.
4359 -- This must be done after analyzing the actuals, which do come from
4360 -- source and are subject to style checking.
4362 Style_Check := False;
4364 Init_Env;
4365 Env_Installed := True;
4367 -- Reset renaming map for formal types. The mapping is established
4368 -- when analyzing the generic associations, but some mappings are
4369 -- inherited from formal packages of parent units, and these are
4370 -- constructed when the parents are installed.
4372 Generic_Renamings.Set_Last (0);
4373 Generic_Renamings_HTable.Reset;
4375 -- Except for an abbreviated instance created to check a formal package,
4376 -- install the parent if this is a generic child unit.
4378 if not Is_Abbreviated_Instance (Inst_Id) then
4379 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4380 end if;
4382 Gen_Unit := Entity (Gen_Id);
4384 -- A package instantiation is Ghost when it is subject to pragma Ghost
4385 -- or the generic template is Ghost. Set the mode now to ensure that
4386 -- any nodes generated during analysis and expansion are marked as
4387 -- Ghost.
4389 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4391 -- Verify that it is the name of a generic package
4393 -- A visibility glitch: if the instance is a child unit and the generic
4394 -- is the generic unit of a parent instance (i.e. both the parent and
4395 -- the child units are instances of the same package) the name now
4396 -- denotes the renaming within the parent, not the intended generic
4397 -- unit. See if there is a homonym that is the desired generic. The
4398 -- renaming declaration must be visible inside the instance of the
4399 -- child, but not when analyzing the name in the instantiation itself.
4401 if Ekind (Gen_Unit) = E_Package
4402 and then Present (Renamed_Entity (Gen_Unit))
4403 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4404 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4405 and then Present (Homonym (Gen_Unit))
4406 then
4407 Gen_Unit := Homonym (Gen_Unit);
4408 end if;
4410 if Etype (Gen_Unit) = Any_Type then
4411 Restore_Env;
4412 goto Leave;
4414 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4416 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4418 if From_Limited_With (Gen_Unit) then
4419 Error_Msg_N
4420 ("cannot instantiate a limited withed package", Gen_Id);
4421 else
4422 Error_Msg_NE
4423 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4424 end if;
4426 Restore_Env;
4427 goto Leave;
4428 end if;
4430 if In_Extended_Main_Source_Unit (N) then
4431 Set_Is_Instantiated (Gen_Unit);
4432 Generate_Reference (Gen_Unit, N);
4434 if Present (Renamed_Entity (Gen_Unit)) then
4435 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4436 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4437 end if;
4438 end if;
4440 if Nkind (Gen_Id) = N_Identifier
4441 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4442 then
4443 Error_Msg_NE
4444 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4446 elsif Nkind (Gen_Id) = N_Expanded_Name
4447 and then Is_Child_Unit (Gen_Unit)
4448 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4449 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4450 then
4451 Error_Msg_N
4452 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4453 end if;
4455 Set_Entity (Gen_Id, Gen_Unit);
4457 -- If generic is a renaming, get original generic unit
4459 if Present (Renamed_Entity (Gen_Unit))
4460 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4461 then
4462 Gen_Unit := Renamed_Entity (Gen_Unit);
4463 end if;
4465 -- Verify that there are no circular instantiations
4467 if In_Open_Scopes (Gen_Unit) then
4468 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4469 Restore_Env;
4470 goto Leave;
4472 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4473 Error_Msg_Node_2 := Current_Scope;
4474 Error_Msg_NE
4475 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4476 Circularity_Detected := True;
4477 Restore_Env;
4478 goto Leave;
4480 else
4481 Mutate_Ekind (Inst_Id, E_Package);
4482 Set_Scope (Inst_Id, Current_Scope);
4484 -- If the context of the instance is subject to SPARK_Mode "off" or
4485 -- the annotation is altogether missing, set the global flag which
4486 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4487 -- the instance.
4489 if SPARK_Mode /= On then
4490 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4492 -- Mark the instance spec in case the body is instantiated at a
4493 -- later pass. This preserves the original context in effect for
4494 -- the body.
4496 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4497 end if;
4499 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4500 Gen_Spec := Specification (Gen_Decl);
4502 -- Initialize renamings map, for error checking, and the list that
4503 -- holds private entities whose views have changed between generic
4504 -- definition and instantiation. If this is the instance created to
4505 -- validate an actual package, the instantiation environment is that
4506 -- of the enclosing instance.
4508 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4510 -- Copy original generic tree, to produce text for instantiation
4512 Act_Tree :=
4513 Copy_Generic_Node
4514 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4516 Act_Spec := Specification (Act_Tree);
4518 -- If this is the instance created to validate an actual package,
4519 -- only the formals matter, do not examine the package spec itself.
4521 if Is_Actual_Pack then
4522 Set_Visible_Declarations (Act_Spec, New_List);
4523 Set_Private_Declarations (Act_Spec, New_List);
4524 end if;
4526 Renaming_List :=
4527 Analyze_Associations
4528 (I_Node => N,
4529 Formals => Generic_Formal_Declarations (Act_Tree),
4530 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4532 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4534 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4535 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4536 Set_Is_Generic_Instance (Act_Decl_Id);
4537 Set_Generic_Parent (Act_Spec, Gen_Unit);
4539 -- References to the generic in its own declaration or its body are
4540 -- references to the instance. Add a renaming declaration for the
4541 -- generic unit itself. This declaration, as well as the renaming
4542 -- declarations for the generic formals, must remain private to the
4543 -- unit: the formals, because this is the language semantics, and
4544 -- the unit because its use is an artifact of the implementation.
4546 Unit_Renaming :=
4547 Make_Package_Renaming_Declaration (Loc,
4548 Defining_Unit_Name =>
4549 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4550 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4552 Append (Unit_Renaming, Renaming_List);
4554 -- The renaming declarations are the first local declarations of the
4555 -- new unit.
4557 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4558 Insert_List_Before
4559 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4560 else
4561 Set_Visible_Declarations (Act_Spec, Renaming_List);
4562 end if;
4564 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4566 -- Propagate the aspect specifications from the package declaration
4567 -- template to the instantiated version of the package declaration.
4569 if Has_Aspects (Act_Tree) then
4570 Set_Aspect_Specifications (Act_Decl,
4571 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4572 end if;
4574 -- The generic may have a generated Default_Storage_Pool aspect,
4575 -- set at the point of generic declaration. If the instance has
4576 -- that aspect, it overrides the one inherited from the generic.
4578 if Has_Aspects (Gen_Spec) then
4579 if No (Aspect_Specifications (N)) then
4580 Set_Aspect_Specifications (N,
4581 (New_Copy_List_Tree
4582 (Aspect_Specifications (Gen_Spec))));
4584 else
4585 declare
4586 Inherited_Aspects : constant List_Id :=
4587 New_Copy_List_Tree
4588 (Aspect_Specifications (Gen_Spec));
4590 ASN1 : Node_Id;
4591 ASN2 : Node_Id;
4592 Pool_Present : Boolean := False;
4594 begin
4595 ASN1 := First (Aspect_Specifications (N));
4596 while Present (ASN1) loop
4597 if Chars (Identifier (ASN1)) =
4598 Name_Default_Storage_Pool
4599 then
4600 Pool_Present := True;
4601 exit;
4602 end if;
4604 Next (ASN1);
4605 end loop;
4607 if Pool_Present then
4609 -- If generic carries a default storage pool, remove it
4610 -- in favor of the instance one.
4612 ASN2 := First (Inherited_Aspects);
4613 while Present (ASN2) loop
4614 if Chars (Identifier (ASN2)) =
4615 Name_Default_Storage_Pool
4616 then
4617 Remove (ASN2);
4618 exit;
4619 end if;
4621 Next (ASN2);
4622 end loop;
4623 end if;
4625 Prepend_List_To
4626 (Aspect_Specifications (N), Inherited_Aspects);
4627 end;
4628 end if;
4629 end if;
4631 -- Save the instantiation node for a subsequent instantiation of the
4632 -- body if there is one and it needs to be instantiated here.
4634 -- We instantiate the body only if we are generating code, or if we
4635 -- are generating cross-reference information, or for GNATprove use.
4637 declare
4638 Enclosing_Body_Present : Boolean := False;
4639 -- If the generic unit is not a compilation unit, then a body may
4640 -- be present in its parent even if none is required. We create a
4641 -- tentative pending instantiation for the body, which will be
4642 -- discarded if none is actually present.
4644 Scop : Entity_Id;
4646 begin
4647 if Scope (Gen_Unit) /= Standard_Standard
4648 and then not Is_Child_Unit (Gen_Unit)
4649 then
4650 Scop := Scope (Gen_Unit);
4651 while Present (Scop) and then Scop /= Standard_Standard loop
4652 if Unit_Requires_Body (Scop) then
4653 Enclosing_Body_Present := True;
4654 exit;
4656 elsif In_Open_Scopes (Scop)
4657 and then In_Package_Body (Scop)
4658 then
4659 Enclosing_Body_Present := True;
4660 exit;
4661 end if;
4663 exit when Is_Compilation_Unit (Scop);
4664 Scop := Scope (Scop);
4665 end loop;
4666 end if;
4668 -- If front-end inlining is enabled or there are any subprograms
4669 -- marked with Inline_Always, and this is a unit for which code
4670 -- will be generated, we instantiate the body at once.
4672 -- This is done if the instance is not the main unit, and if the
4673 -- generic is not a child unit of another generic, to avoid scope
4674 -- problems and the reinstallation of parent instances.
4676 if Expander_Active
4677 and then (not Is_Child_Unit (Gen_Unit)
4678 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4679 and then Might_Inline_Subp (Gen_Unit)
4680 and then not Is_Actual_Pack
4681 then
4682 if not Back_End_Inlining
4683 and then (Front_End_Inlining or else Has_Inline_Always)
4684 and then (Is_In_Main_Unit (N)
4685 or else In_Main_Context (Current_Scope))
4686 and then Nkind (Parent (N)) /= N_Compilation_Unit
4687 then
4688 Inline_Now := True;
4690 -- In configurable_run_time mode we force the inlining of
4691 -- predefined subprograms marked Inline_Always, to minimize
4692 -- the use of the run-time library.
4694 elsif In_Predefined_Unit (Gen_Decl)
4695 and then Configurable_Run_Time_Mode
4696 and then Nkind (Parent (N)) /= N_Compilation_Unit
4697 then
4698 Inline_Now := True;
4699 end if;
4701 -- If the current scope is itself an instance within a child
4702 -- unit, there will be duplications in the scope stack, and the
4703 -- unstacking mechanism in Inline_Instance_Body will fail.
4704 -- This loses some rare cases of optimization.
4706 if Is_Generic_Instance (Current_Scope) then
4707 declare
4708 Curr_Unit : constant Entity_Id :=
4709 Cunit_Entity (Current_Sem_Unit);
4710 begin
4711 if Curr_Unit /= Current_Scope
4712 and then Is_Child_Unit (Curr_Unit)
4713 then
4714 Inline_Now := False;
4715 end if;
4716 end;
4717 end if;
4718 end if;
4720 Needs_Body :=
4721 (Unit_Requires_Body (Gen_Unit)
4722 or else Enclosing_Body_Present
4723 or else Present (Corresponding_Body (Gen_Decl)))
4724 and then Needs_Body_Instantiated (Gen_Unit)
4725 and then not Is_Actual_Pack
4726 and then not Inline_Now
4727 and then (Operating_Mode = Generate_Code
4728 or else (Operating_Mode = Check_Semantics
4729 and then GNATprove_Mode));
4731 -- If front-end inlining is enabled or there are any subprograms
4732 -- marked with Inline_Always, do not instantiate body when within
4733 -- a generic context.
4735 if not Back_End_Inlining
4736 and then (Front_End_Inlining or else Has_Inline_Always)
4737 and then not Expander_Active
4738 then
4739 Needs_Body := False;
4740 end if;
4742 -- If the current context is generic, and the package being
4743 -- instantiated is declared within a formal package, there is no
4744 -- body to instantiate until the enclosing generic is instantiated
4745 -- and there is an actual for the formal package. If the formal
4746 -- package has parameters, we build a regular package instance for
4747 -- it, that precedes the original formal package declaration.
4749 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4750 declare
4751 Decl : constant Node_Id :=
4752 Original_Node
4753 (Unit_Declaration_Node (Scope (Gen_Unit)));
4754 begin
4755 if Nkind (Decl) = N_Formal_Package_Declaration
4756 or else (Nkind (Decl) = N_Package_Declaration
4757 and then Is_List_Member (Decl)
4758 and then Present (Next (Decl))
4759 and then
4760 Nkind (Next (Decl)) =
4761 N_Formal_Package_Declaration)
4762 then
4763 Needs_Body := False;
4764 end if;
4765 end;
4766 end if;
4767 end;
4769 -- For RCI unit calling stubs, we omit the instance body if the
4770 -- instance is the RCI library unit itself.
4772 -- However there is a special case for nested instances: in this case
4773 -- we do generate the instance body, as it might be required, e.g.
4774 -- because it provides stream attributes for some type used in the
4775 -- profile of a remote subprogram. This is consistent with 12.3(12),
4776 -- which indicates that the instance body occurs at the place of the
4777 -- instantiation, and thus is part of the RCI declaration, which is
4778 -- present on all client partitions (this is E.2.3(18)).
4780 -- Note that AI12-0002 may make it illegal at some point to have
4781 -- stream attributes defined in an RCI unit, in which case this
4782 -- special case will become unnecessary. In the meantime, there
4783 -- is known application code in production that depends on this
4784 -- being possible, so we definitely cannot eliminate the body in
4785 -- the case of nested instances for the time being.
4787 -- When we generate a nested instance body, calling stubs for any
4788 -- relevant subprogram will be inserted immediately after the
4789 -- subprogram declarations, and will take precedence over the
4790 -- subsequent (original) body. (The stub and original body will be
4791 -- complete homographs, but this is permitted in an instance).
4792 -- (Could we do better and remove the original body???)
4794 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4795 and then Comes_From_Source (N)
4796 and then Nkind (Parent (N)) = N_Compilation_Unit
4797 then
4798 Needs_Body := False;
4799 end if;
4801 -- If the context requires a full instantiation, set things up for
4802 -- subsequent construction of the body.
4804 if Needs_Body then
4805 declare
4806 Fin_Scop, S : Entity_Id;
4808 begin
4809 Check_Forward_Instantiation (Gen_Decl);
4811 Fin_Scop := Empty;
4813 -- For a package instantiation that is not a compilation unit,
4814 -- indicate that cleanup actions of the innermost enclosing
4815 -- scope for which they are generated should be delayed until
4816 -- after the package body is instantiated.
4818 if Nkind (N) = N_Package_Instantiation
4819 and then not Is_Compilation_Unit (Act_Decl_Id)
4820 then
4821 S := Current_Scope;
4823 while S /= Standard_Standard loop
4824 -- Cleanup actions are not generated within generic units
4825 -- or in the formal part of generic units.
4827 if not Expander_Active then
4828 exit;
4830 -- For package scopes, cleanup actions are generated only
4831 -- for compilation units, for spec and body separately.
4833 elsif Ekind (S) = E_Package then
4834 if Is_Compilation_Unit (S) then
4835 if In_Package_Body (S) then
4836 Fin_Scop := Body_Entity (S);
4837 else
4838 Fin_Scop := S;
4839 end if;
4841 Set_Delay_Cleanups (Fin_Scop);
4842 exit;
4844 else
4845 S := Scope (S);
4846 end if;
4848 -- Cleanup actions are generated for all dynamic scopes
4850 else
4851 Fin_Scop := S;
4852 Set_Delay_Cleanups (Fin_Scop);
4853 exit;
4854 end if;
4855 end loop;
4856 end if;
4858 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
4859 end;
4860 end if;
4862 Set_Categorization_From_Pragmas (Act_Decl);
4864 if Parent_Installed then
4865 Hide_Current_Scope;
4866 end if;
4868 Set_Instance_Spec (N, Act_Decl);
4870 -- If not a compilation unit, insert the package declaration before
4871 -- the original instantiation node.
4873 if Nkind (Parent (N)) /= N_Compilation_Unit then
4874 Mark_Rewrite_Insertion (Act_Decl);
4875 Insert_Before (N, Act_Decl);
4877 if Has_Aspects (N) then
4878 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4880 -- The pragma created for a Default_Storage_Pool aspect must
4881 -- appear ahead of the declarations in the instance spec.
4882 -- Analysis has placed it after the instance node, so remove
4883 -- it and reinsert it properly now.
4885 declare
4886 ASN : constant Node_Id := First (Aspect_Specifications (N));
4887 A_Name : constant Name_Id := Chars (Identifier (ASN));
4888 Decl : Node_Id;
4890 begin
4891 if A_Name = Name_Default_Storage_Pool then
4892 if No (Visible_Declarations (Act_Spec)) then
4893 Set_Visible_Declarations (Act_Spec, New_List);
4894 end if;
4896 Decl := Next (N);
4897 while Present (Decl) loop
4898 if Nkind (Decl) = N_Pragma then
4899 Remove (Decl);
4900 Prepend (Decl, Visible_Declarations (Act_Spec));
4901 exit;
4902 end if;
4904 Next (Decl);
4905 end loop;
4906 end if;
4907 end;
4908 end if;
4910 Analyze (Act_Decl);
4912 -- For an instantiation that is a compilation unit, place
4913 -- declaration on current node so context is complete for analysis
4914 -- (including nested instantiations). If this is the main unit,
4915 -- the declaration eventually replaces the instantiation node.
4916 -- If the instance body is created later, it replaces the
4917 -- instance node, and the declaration is attached to it
4918 -- (see Build_Instance_Compilation_Unit_Nodes).
4920 else
4921 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4923 -- The entity for the current unit is the newly created one,
4924 -- and all semantic information is attached to it.
4926 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4928 -- If this is the main unit, replace the main entity as well
4930 if Current_Sem_Unit = Main_Unit then
4931 Main_Unit_Entity := Act_Decl_Id;
4932 end if;
4933 end if;
4935 Set_Unit (Parent (N), Act_Decl);
4936 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4937 Set_Package_Instantiation (Act_Decl_Id, N);
4939 -- Process aspect specifications of the instance node, if any, to
4940 -- take into account categorization pragmas before analyzing the
4941 -- instance.
4943 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4945 Analyze (Act_Decl);
4946 Set_Unit (Parent (N), N);
4947 Set_Body_Required (Parent (N), False);
4949 -- We never need elaboration checks on instantiations, since by
4950 -- definition, the body instantiation is elaborated at the same
4951 -- time as the spec instantiation.
4953 if Legacy_Elaboration_Checks then
4954 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4955 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4956 end if;
4957 end if;
4959 if Legacy_Elaboration_Checks then
4960 Check_Elab_Instantiation (N);
4961 end if;
4963 -- Save the scenario for later examination by the ABE Processing
4964 -- phase.
4966 Record_Elaboration_Scenario (N);
4968 -- The instantiation results in a guaranteed ABE
4970 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4971 -- Do not instantiate the corresponding body because gigi cannot
4972 -- handle certain types of premature instantiations.
4974 Remove_Dead_Instance (N);
4976 -- Create completing bodies for all subprogram declarations since
4977 -- their real bodies will not be instantiated.
4979 Provide_Completing_Bodies (Instance_Spec (N));
4980 end if;
4982 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4984 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4985 First_Private_Entity (Act_Decl_Id));
4987 -- If the instantiation needs a body, the unit will be turned into
4988 -- a package body and receive its own elaboration entity. Otherwise,
4989 -- the nature of the unit is now a package declaration.
4991 -- Note that the below rewriting means that Act_Decl, which has been
4992 -- analyzed and expanded, will be re-expanded as the rewritten N.
4994 if Nkind (Parent (N)) = N_Compilation_Unit
4995 and then not Needs_Body
4996 then
4997 Rewrite (N, Act_Decl);
4998 end if;
5000 if Present (Corresponding_Body (Gen_Decl))
5001 or else Unit_Requires_Body (Gen_Unit)
5002 then
5003 Set_Has_Completion (Act_Decl_Id);
5004 end if;
5006 Check_Formal_Packages (Act_Decl_Id);
5008 Restore_Hidden_Primitives (Vis_Prims_List);
5009 Restore_Private_Views (Act_Decl_Id);
5011 Inherit_Context (Gen_Decl, N);
5013 if Parent_Installed then
5014 Remove_Parent;
5015 end if;
5017 Restore_Env;
5018 Env_Installed := False;
5019 end if;
5021 Validate_Categorization_Dependency (N, Act_Decl_Id);
5023 -- There used to be a check here to prevent instantiations in local
5024 -- contexts if the No_Local_Allocators restriction was active. This
5025 -- check was removed by a binding interpretation in AI-95-00130/07,
5026 -- but we retain the code for documentation purposes.
5028 -- if Ekind (Act_Decl_Id) /= E_Void
5029 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5030 -- then
5031 -- Check_Restriction (No_Local_Allocators, N);
5032 -- end if;
5034 if Inline_Now then
5035 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5036 end if;
5038 -- Check that if N is an instantiation of System.Dim_Float_IO or
5039 -- System.Dim_Integer_IO, the formal type has a dimension system.
5041 if Nkind (N) = N_Package_Instantiation
5042 and then Is_Dim_IO_Package_Instantiation (N)
5043 then
5044 declare
5045 Assoc : constant Node_Id := First (Generic_Associations (N));
5046 begin
5047 if not Has_Dimension_System
5048 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5049 then
5050 Error_Msg_N ("type with a dimension system expected", Assoc);
5051 end if;
5052 end;
5053 end if;
5055 <<Leave>>
5056 if Nkind (Parent (N)) /= N_Compilation_Unit then
5057 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5058 end if;
5060 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5061 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5062 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5063 Style_Check := Saved_Style_Check;
5065 exception
5066 when Instantiation_Error =>
5067 if Parent_Installed then
5068 Remove_Parent;
5069 end if;
5071 if Env_Installed then
5072 Restore_Env;
5073 end if;
5075 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5076 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5077 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5078 Style_Check := Saved_Style_Check;
5079 end Analyze_Package_Instantiation;
5081 --------------------------
5082 -- Inline_Instance_Body --
5083 --------------------------
5085 -- WARNING: This routine manages SPARK regions. Return statements must be
5086 -- replaced by gotos which jump to the end of the routine and restore the
5087 -- SPARK mode.
5089 procedure Inline_Instance_Body
5090 (N : Node_Id;
5091 Gen_Unit : Entity_Id;
5092 Act_Decl : Node_Id)
5094 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5096 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5097 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5098 Gen_Comp : constant Entity_Id :=
5099 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5101 Scope_Stack_Depth : constant Pos :=
5102 Scope_Stack.Last - Scope_Stack.First + 1;
5104 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5105 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5106 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5108 Curr_Scope : Entity_Id := Empty;
5109 List : Elist_Id := No_Elist; -- init to avoid warning
5110 N_Instances : Nat := 0;
5111 Num_Inner : Nat := 0;
5112 Num_Scopes : Nat := 0;
5113 Removed : Boolean := False;
5114 S : Entity_Id;
5115 Vis : Boolean;
5117 begin
5118 -- Case of generic unit defined in another unit. We must remove the
5119 -- complete context of the current unit to install that of the generic.
5121 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5123 -- Loop through enclosing scopes until we reach a generic instance,
5124 -- package body, or subprogram.
5126 S := Current_Scope;
5127 while Present (S) and then S /= Standard_Standard loop
5129 -- Save use clauses from enclosing scopes into Use_Clauses
5131 loop
5132 Num_Scopes := Num_Scopes + 1;
5134 Use_Clauses (Num_Scopes) :=
5135 (Scope_Stack.Table
5136 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5137 End_Use_Clauses (Use_Clauses (Num_Scopes));
5139 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5140 or else Scope_Stack.Table
5141 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5142 end loop;
5144 exit when Is_Generic_Instance (S)
5145 and then (In_Package_Body (S)
5146 or else Ekind (S) = E_Procedure
5147 or else Ekind (S) = E_Function);
5148 S := Scope (S);
5149 end loop;
5151 Vis := Is_Immediately_Visible (Gen_Comp);
5153 -- Find and save all enclosing instances
5155 S := Current_Scope;
5157 while Present (S)
5158 and then S /= Standard_Standard
5159 loop
5160 if Is_Generic_Instance (S) then
5161 N_Instances := N_Instances + 1;
5162 Instances (N_Instances) := S;
5164 exit when In_Package_Body (S);
5165 end if;
5167 S := Scope (S);
5168 end loop;
5170 -- Remove context of current compilation unit, unless we are within a
5171 -- nested package instantiation, in which case the context has been
5172 -- removed previously.
5174 -- If current scope is the body of a child unit, remove context of
5175 -- spec as well. If an enclosing scope is an instance body, the
5176 -- context has already been removed, but the entities in the body
5177 -- must be made invisible as well.
5179 S := Current_Scope;
5180 while Present (S) and then S /= Standard_Standard loop
5181 if Is_Generic_Instance (S)
5182 and then (In_Package_Body (S)
5183 or else Ekind (S) in E_Procedure | E_Function)
5184 then
5185 -- We still have to remove the entities of the enclosing
5186 -- instance from direct visibility.
5188 declare
5189 E : Entity_Id;
5190 begin
5191 E := First_Entity (S);
5192 while Present (E) loop
5193 Set_Is_Immediately_Visible (E, False);
5194 Next_Entity (E);
5195 end loop;
5196 end;
5198 exit;
5199 end if;
5201 if S = Curr_Unit
5202 or else (Ekind (Curr_Unit) = E_Package_Body
5203 and then S = Spec_Entity (Curr_Unit))
5204 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5205 and then S = Corresponding_Spec
5206 (Unit_Declaration_Node (Curr_Unit)))
5207 then
5208 Removed := True;
5210 -- Remove entities in current scopes from visibility, so that
5211 -- instance body is compiled in a clean environment.
5213 List := Save_Scope_Stack (Handle_Use => False);
5215 if Is_Child_Unit (S) then
5217 -- Remove child unit from stack, as well as inner scopes.
5218 -- Removing the context of a child unit removes parent units
5219 -- as well.
5221 while Current_Scope /= S loop
5222 Num_Inner := Num_Inner + 1;
5223 Inner_Scopes (Num_Inner) := Current_Scope;
5224 Pop_Scope;
5225 end loop;
5227 Pop_Scope;
5228 Remove_Context (Curr_Comp);
5229 Curr_Scope := S;
5231 else
5232 Remove_Context (Curr_Comp);
5233 end if;
5235 if Ekind (Curr_Unit) = E_Package_Body then
5236 Remove_Context (Library_Unit (Curr_Comp));
5237 end if;
5238 end if;
5240 S := Scope (S);
5241 end loop;
5243 pragma Assert (Num_Inner < Num_Scopes);
5245 Push_Scope (Standard_Standard);
5246 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5248 -- The inlined package body is analyzed with the configuration state
5249 -- of the context prior to the scope manipulations performed above.
5251 -- ??? shouldn't this also use the warning state of the context prior
5252 -- to the scope manipulations?
5254 Instantiate_Package_Body
5255 (Body_Info =>
5256 ((Inst_Node => N,
5257 Act_Decl => Act_Decl,
5258 Fin_Scop => Empty,
5259 Config_Switches => Config_Attrs,
5260 Current_Sem_Unit => Current_Sem_Unit,
5261 Expander_Status => Expander_Active,
5262 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5263 Scope_Suppress => Scope_Suppress,
5264 Warnings => Save_Warnings)),
5265 Inlined_Body => True);
5267 Pop_Scope;
5269 -- Restore context
5271 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5273 -- Reset Generic_Instance flag so that use clauses can be installed
5274 -- in the proper order. (See Use_One_Package for effect of enclosing
5275 -- instances on processing of use clauses).
5277 for J in 1 .. N_Instances loop
5278 Set_Is_Generic_Instance (Instances (J), False);
5279 end loop;
5281 if Removed then
5282 Install_Context (Curr_Comp, Chain => False);
5284 if Present (Curr_Scope)
5285 and then Is_Child_Unit (Curr_Scope)
5286 then
5287 Push_Scope (Curr_Scope);
5288 Set_Is_Immediately_Visible (Curr_Scope);
5290 -- Finally, restore inner scopes as well
5292 for J in reverse 1 .. Num_Inner loop
5293 Push_Scope (Inner_Scopes (J));
5294 end loop;
5295 end if;
5297 Restore_Scope_Stack (List, Handle_Use => False);
5299 if Present (Curr_Scope)
5300 and then
5301 (In_Private_Part (Curr_Scope)
5302 or else In_Package_Body (Curr_Scope))
5303 then
5304 -- Install private declaration of ancestor units, which are
5305 -- currently available. Restore_Scope_Stack and Install_Context
5306 -- only install the visible part of parents.
5308 declare
5309 Par : Entity_Id;
5310 begin
5311 Par := Scope (Curr_Scope);
5312 while Present (Par) and then Par /= Standard_Standard loop
5313 Install_Private_Declarations (Par);
5314 Par := Scope (Par);
5315 end loop;
5316 end;
5317 end if;
5318 end if;
5320 -- Restore use clauses. For a child unit, use clauses in the parents
5321 -- are restored when installing the context, so only those in inner
5322 -- scopes (and those local to the child unit itself) need to be
5323 -- installed explicitly.
5325 if Is_Child_Unit (Curr_Unit) and then Removed then
5326 for J in reverse 1 .. Num_Inner + 1 loop
5327 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5328 Use_Clauses (J);
5329 Install_Use_Clauses (Use_Clauses (J));
5330 end loop;
5332 else
5333 for J in reverse 1 .. Num_Scopes loop
5334 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5335 Use_Clauses (J);
5336 Install_Use_Clauses (Use_Clauses (J));
5337 end loop;
5338 end if;
5340 -- Restore status of instances. If one of them is a body, make its
5341 -- local entities visible again.
5343 declare
5344 E : Entity_Id;
5345 Inst : Entity_Id;
5347 begin
5348 for J in 1 .. N_Instances loop
5349 Inst := Instances (J);
5350 Set_Is_Generic_Instance (Inst, True);
5352 if In_Package_Body (Inst)
5353 or else Ekind (S) in E_Procedure | E_Function
5354 then
5355 E := First_Entity (Instances (J));
5356 while Present (E) loop
5357 Set_Is_Immediately_Visible (E);
5358 Next_Entity (E);
5359 end loop;
5360 end if;
5361 end loop;
5362 end;
5364 -- If generic unit is in current unit, current context is correct. Note
5365 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5366 -- enclosing scopes were removed.
5368 else
5369 Instantiate_Package_Body
5370 (Body_Info =>
5371 ((Inst_Node => N,
5372 Act_Decl => Act_Decl,
5373 Fin_Scop => Empty,
5374 Config_Switches => Save_Config_Switches,
5375 Current_Sem_Unit => Current_Sem_Unit,
5376 Expander_Status => Expander_Active,
5377 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5378 Scope_Suppress => Scope_Suppress,
5379 Warnings => Save_Warnings)),
5380 Inlined_Body => True);
5381 end if;
5382 end Inline_Instance_Body;
5384 -------------------------------------
5385 -- Analyze_Procedure_Instantiation --
5386 -------------------------------------
5388 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5389 begin
5390 Analyze_Subprogram_Instantiation (N, E_Procedure);
5391 end Analyze_Procedure_Instantiation;
5393 -----------------------------------
5394 -- Need_Subprogram_Instance_Body --
5395 -----------------------------------
5397 function Need_Subprogram_Instance_Body
5398 (N : Node_Id;
5399 Subp : Entity_Id) return Boolean
5401 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5402 -- Return True if E is an inlined subprogram, an inlined renaming or a
5403 -- subprogram nested in an inlined subprogram. The inlining machinery
5404 -- totally disregards nested subprograms since it considers that they
5405 -- will always be compiled if the parent is (see Inline.Is_Nested).
5407 ------------------------------------
5408 -- Is_Inlined_Or_Child_Of_Inlined --
5409 ------------------------------------
5411 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5412 Scop : Entity_Id;
5414 begin
5415 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5416 return True;
5417 end if;
5419 Scop := Scope (E);
5420 while Scop /= Standard_Standard loop
5421 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5422 return True;
5423 end if;
5425 Scop := Scope (Scop);
5426 end loop;
5428 return False;
5429 end Is_Inlined_Or_Child_Of_Inlined;
5431 begin
5432 -- Must be in the main unit or inlined (or child of inlined)
5434 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5436 -- Must be generating code or analyzing code in GNATprove mode
5438 and then (Operating_Mode = Generate_Code
5439 or else (Operating_Mode = Check_Semantics
5440 and then GNATprove_Mode))
5442 -- The body is needed when generating code (full expansion) and in
5443 -- in GNATprove mode (special expansion) for formal verification of
5444 -- the body itself.
5446 and then (Expander_Active or GNATprove_Mode)
5448 -- No point in inlining if ABE is inevitable
5450 and then not Is_Known_Guaranteed_ABE (N)
5452 -- Or if subprogram is eliminated
5454 and then not Is_Eliminated (Subp)
5455 then
5456 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5457 return True;
5459 -- Here if not inlined, or we ignore the inlining
5461 else
5462 return False;
5463 end if;
5464 end Need_Subprogram_Instance_Body;
5466 --------------------------------------
5467 -- Analyze_Subprogram_Instantiation --
5468 --------------------------------------
5470 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5471 -- must be replaced by gotos which jump to the end of the routine in order
5472 -- to restore the Ghost and SPARK modes.
5474 procedure Analyze_Subprogram_Instantiation
5475 (N : Node_Id;
5476 K : Entity_Kind)
5478 Errs : constant Nat := Serious_Errors_Detected;
5479 Gen_Id : constant Node_Id := Name (N);
5480 Inst_Id : constant Entity_Id := Defining_Entity (N);
5481 Anon_Id : constant Entity_Id :=
5482 Make_Defining_Identifier (Sloc (Inst_Id),
5483 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5484 Loc : constant Source_Ptr := Sloc (N);
5486 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5487 Act_Decl : Node_Id;
5488 Act_Spec : Node_Id;
5489 Act_Tree : Node_Id;
5491 Env_Installed : Boolean := False;
5492 Gen_Unit : Entity_Id;
5493 Gen_Decl : Node_Id;
5494 Pack_Id : Entity_Id;
5495 Parent_Installed : Boolean := False;
5497 Renaming_List : List_Id;
5498 -- The list of declarations that link formals and actuals of the
5499 -- instance. These are subtype declarations for formal types, and
5500 -- renaming declarations for other formals. The subprogram declaration
5501 -- for the instance is then appended to the list, and the last item on
5502 -- the list is the renaming declaration for the instance.
5504 procedure Analyze_Instance_And_Renamings;
5505 -- The instance must be analyzed in a context that includes the mappings
5506 -- of generic parameters into actuals. We create a package declaration
5507 -- for this purpose, and a subprogram with an internal name within the
5508 -- package. The subprogram instance is simply an alias for the internal
5509 -- subprogram, declared in the current scope.
5511 procedure Build_Subprogram_Renaming;
5512 -- If the subprogram is recursive, there are occurrences of the name of
5513 -- the generic within the body, which must resolve to the current
5514 -- instance. We add a renaming declaration after the declaration, which
5515 -- is available in the instance body, as well as in the analysis of
5516 -- aspects that appear in the generic. This renaming declaration is
5517 -- inserted after the instance declaration which it renames.
5519 ------------------------------------
5520 -- Analyze_Instance_And_Renamings --
5521 ------------------------------------
5523 procedure Analyze_Instance_And_Renamings is
5524 Def_Ent : constant Entity_Id := Defining_Entity (N);
5525 Pack_Decl : Node_Id;
5527 begin
5528 if Nkind (Parent (N)) = N_Compilation_Unit then
5530 -- For the case of a compilation unit, the container package has
5531 -- the same name as the instantiation, to insure that the binder
5532 -- calls the elaboration procedure with the right name. Copy the
5533 -- entity of the instance, which may have compilation level flags
5534 -- (e.g. Is_Child_Unit) set.
5536 Pack_Id := New_Copy (Def_Ent);
5538 else
5539 -- Otherwise we use the name of the instantiation concatenated
5540 -- with its source position to ensure uniqueness if there are
5541 -- several instantiations with the same name.
5543 Pack_Id :=
5544 Make_Defining_Identifier (Loc,
5545 Chars => New_External_Name
5546 (Related_Id => Chars (Def_Ent),
5547 Suffix => "GP",
5548 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5549 end if;
5551 Pack_Decl :=
5552 Make_Package_Declaration (Loc,
5553 Specification => Make_Package_Specification (Loc,
5554 Defining_Unit_Name => Pack_Id,
5555 Visible_Declarations => Renaming_List,
5556 End_Label => Empty));
5558 Set_Instance_Spec (N, Pack_Decl);
5559 Set_Is_Generic_Instance (Pack_Id);
5560 Set_Debug_Info_Needed (Pack_Id);
5562 -- Case of not a compilation unit
5564 if Nkind (Parent (N)) /= N_Compilation_Unit then
5565 Mark_Rewrite_Insertion (Pack_Decl);
5566 Insert_Before (N, Pack_Decl);
5567 Set_Has_Completion (Pack_Id);
5569 -- Case of an instantiation that is a compilation unit
5571 -- Place declaration on current node so context is complete for
5572 -- analysis (including nested instantiations), and for use in a
5573 -- context_clause (see Analyze_With_Clause).
5575 else
5576 Set_Unit (Parent (N), Pack_Decl);
5577 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5578 end if;
5580 Analyze (Pack_Decl);
5581 Check_Formal_Packages (Pack_Id);
5583 -- Body of the enclosing package is supplied when instantiating the
5584 -- subprogram body, after semantic analysis is completed.
5586 if Nkind (Parent (N)) = N_Compilation_Unit then
5588 -- Remove package itself from visibility, so it does not
5589 -- conflict with subprogram.
5591 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5593 -- Set name and scope of internal subprogram so that the proper
5594 -- external name will be generated. The proper scope is the scope
5595 -- of the wrapper package. We need to generate debugging info for
5596 -- the internal subprogram, so set flag accordingly.
5598 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5599 Set_Scope (Anon_Id, Scope (Pack_Id));
5601 -- Mark wrapper package as referenced, to avoid spurious warnings
5602 -- if the instantiation appears in various with_ clauses of
5603 -- subunits of the main unit.
5605 Set_Referenced (Pack_Id);
5606 end if;
5608 Set_Is_Generic_Instance (Anon_Id);
5609 Set_Debug_Info_Needed (Anon_Id);
5610 Act_Decl_Id := New_Copy (Anon_Id);
5612 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5613 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5614 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5616 -- Subprogram instance comes from source only if generic does
5618 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5620 -- If the instance is a child unit, mark the Id accordingly. Mark
5621 -- the anonymous entity as well, which is the real subprogram and
5622 -- which is used when the instance appears in a context clause.
5623 -- Similarly, propagate the Is_Eliminated flag to handle properly
5624 -- nested eliminated subprograms.
5626 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5627 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5628 New_Overloaded_Entity (Act_Decl_Id);
5629 Check_Eliminated (Act_Decl_Id);
5630 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5632 if Nkind (Parent (N)) = N_Compilation_Unit then
5634 -- In compilation unit case, kill elaboration checks on the
5635 -- instantiation, since they are never needed - the body is
5636 -- instantiated at the same point as the spec.
5638 if Legacy_Elaboration_Checks then
5639 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5640 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5641 end if;
5643 Set_Is_Compilation_Unit (Anon_Id);
5644 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5645 end if;
5647 -- The instance is not a freezing point for the new subprogram.
5648 -- The anonymous subprogram may have a freeze node, created for
5649 -- some delayed aspects. This freeze node must not be inherited
5650 -- by the visible subprogram entity.
5652 Set_Is_Frozen (Act_Decl_Id, False);
5653 Set_Freeze_Node (Act_Decl_Id, Empty);
5655 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5656 Valid_Operator_Definition (Act_Decl_Id);
5657 end if;
5659 Set_Alias (Act_Decl_Id, Anon_Id);
5660 Set_Has_Completion (Act_Decl_Id);
5661 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5663 if Nkind (Parent (N)) = N_Compilation_Unit then
5664 Set_Body_Required (Parent (N), False);
5665 end if;
5666 end Analyze_Instance_And_Renamings;
5668 -------------------------------
5669 -- Build_Subprogram_Renaming --
5670 -------------------------------
5672 procedure Build_Subprogram_Renaming is
5673 Renaming_Decl : Node_Id;
5674 Unit_Renaming : Node_Id;
5676 begin
5677 Unit_Renaming :=
5678 Make_Subprogram_Renaming_Declaration (Loc,
5679 Specification =>
5680 Copy_Generic_Node
5681 (Specification (Original_Node (Gen_Decl)),
5682 Empty,
5683 Instantiating => True),
5684 Name => New_Occurrence_Of (Anon_Id, Loc));
5686 -- The generic may be a child unit. The renaming needs an identifier
5687 -- with the proper name.
5689 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5690 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5692 -- If there is a formal subprogram with the same name as the unit
5693 -- itself, do not add this renaming declaration, to prevent
5694 -- ambiguities when there is a call with that name in the body.
5696 Renaming_Decl := First (Renaming_List);
5697 while Present (Renaming_Decl) loop
5698 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5699 and then
5700 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5701 then
5702 exit;
5703 end if;
5705 Next (Renaming_Decl);
5706 end loop;
5708 if No (Renaming_Decl) then
5709 Append (Unit_Renaming, Renaming_List);
5710 end if;
5711 end Build_Subprogram_Renaming;
5713 -- Local variables
5715 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5716 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5717 Saved_ISMP : constant Boolean :=
5718 Ignore_SPARK_Mode_Pragmas_In_Instance;
5719 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5720 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5721 -- Save the Ghost and SPARK mode-related data to restore on exit
5723 Vis_Prims_List : Elist_Id := No_Elist;
5724 -- List of primitives made temporarily visible in the instantiation
5725 -- to match the visibility of the formal type
5727 -- Start of processing for Analyze_Subprogram_Instantiation
5729 begin
5730 -- Preserve relevant elaboration-related attributes of the context which
5731 -- are no longer available or very expensive to recompute once analysis,
5732 -- resolution, and expansion are over.
5734 Mark_Elaboration_Attributes
5735 (N_Id => N,
5736 Checks => True,
5737 Level => True,
5738 Modes => True,
5739 Warnings => True);
5741 -- Very first thing: check for special Text_IO unit in case we are
5742 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5743 -- such an instantiation is bogus (these are packages, not subprograms),
5744 -- but we get a better error message if we do this.
5746 Check_Text_IO_Special_Unit (Gen_Id);
5748 -- Make node global for error reporting
5750 Instantiation_Node := N;
5752 -- For package instantiations we turn off style checks, because they
5753 -- will have been emitted in the generic. For subprogram instantiations
5754 -- we want to apply at least the check on overriding indicators so we
5755 -- do not modify the style check status.
5757 -- The renaming declarations for the actuals do not come from source and
5758 -- will not generate spurious warnings.
5760 Preanalyze_Actuals (N);
5762 Init_Env;
5763 Env_Installed := True;
5764 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5765 Gen_Unit := Entity (Gen_Id);
5767 -- A subprogram instantiation is Ghost when it is subject to pragma
5768 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5769 -- that any nodes generated during analysis and expansion are marked as
5770 -- Ghost.
5772 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5774 Generate_Reference (Gen_Unit, Gen_Id);
5776 if Nkind (Gen_Id) = N_Identifier
5777 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5778 then
5779 Error_Msg_NE
5780 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5781 end if;
5783 if Etype (Gen_Unit) = Any_Type then
5784 Restore_Env;
5785 goto Leave;
5786 end if;
5788 -- Verify that it is a generic subprogram of the right kind, and that
5789 -- it does not lead to a circular instantiation.
5791 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5792 Error_Msg_NE
5793 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5795 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5796 Error_Msg_NE
5797 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5799 elsif In_Open_Scopes (Gen_Unit) then
5800 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5802 else
5803 Mutate_Ekind (Inst_Id, K);
5804 Set_Scope (Inst_Id, Current_Scope);
5806 Set_Entity (Gen_Id, Gen_Unit);
5808 if In_Extended_Main_Source_Unit (N) then
5809 Set_Is_Instantiated (Gen_Unit);
5810 Generate_Reference (Gen_Unit, N);
5811 end if;
5813 -- If renaming, get original unit
5815 if Present (Renamed_Entity (Gen_Unit))
5816 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5817 then
5818 Gen_Unit := Renamed_Entity (Gen_Unit);
5819 Set_Is_Instantiated (Gen_Unit);
5820 Generate_Reference (Gen_Unit, N);
5821 end if;
5823 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5824 Error_Msg_Node_2 := Current_Scope;
5825 Error_Msg_NE
5826 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5827 Circularity_Detected := True;
5828 Restore_Hidden_Primitives (Vis_Prims_List);
5829 goto Leave;
5830 end if;
5832 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5834 -- Initialize renamings map, for error checking
5836 Generic_Renamings.Set_Last (0);
5837 Generic_Renamings_HTable.Reset;
5839 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5841 -- Copy original generic tree, to produce text for instantiation
5843 Act_Tree :=
5844 Copy_Generic_Node
5845 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5847 -- Inherit overriding indicator from instance node
5849 Act_Spec := Specification (Act_Tree);
5850 Set_Must_Override (Act_Spec, Must_Override (N));
5851 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5853 Renaming_List :=
5854 Analyze_Associations
5855 (I_Node => N,
5856 Formals => Generic_Formal_Declarations (Act_Tree),
5857 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5859 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5861 -- The subprogram itself cannot contain a nested instance, so the
5862 -- current parent is left empty.
5864 Set_Instance_Env (Gen_Unit, Empty);
5866 -- Build the subprogram declaration, which does not appear in the
5867 -- generic template, and give it a sloc consistent with that of the
5868 -- template.
5870 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5871 Set_Generic_Parent (Act_Spec, Gen_Unit);
5872 Act_Decl :=
5873 Make_Subprogram_Declaration (Sloc (Act_Spec),
5874 Specification => Act_Spec);
5876 -- The aspects have been copied previously, but they have to be
5877 -- linked explicitly to the new subprogram declaration. Explicit
5878 -- pre/postconditions on the instance are analyzed below, in a
5879 -- separate step.
5881 Move_Aspects (Act_Tree, To => Act_Decl);
5882 Set_Categorization_From_Pragmas (Act_Decl);
5884 if Parent_Installed then
5885 Hide_Current_Scope;
5886 end if;
5888 Append (Act_Decl, Renaming_List);
5890 -- Contract-related source pragmas that follow a generic subprogram
5891 -- must be instantiated explicitly because they are not part of the
5892 -- subprogram template.
5894 Instantiate_Subprogram_Contract
5895 (Original_Node (Gen_Decl), Renaming_List);
5897 Build_Subprogram_Renaming;
5899 -- If the context of the instance is subject to SPARK_Mode "off" or
5900 -- the annotation is altogether missing, set the global flag which
5901 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5902 -- the instance. This should be done prior to analyzing the instance.
5904 if SPARK_Mode /= On then
5905 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5906 end if;
5908 -- If the context of an instance is not subject to SPARK_Mode "off",
5909 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5910 -- the latter should be the one applicable to the instance.
5912 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5913 and then Saved_SM /= Off
5914 and then Present (SPARK_Pragma (Gen_Unit))
5915 then
5916 Set_SPARK_Mode (Gen_Unit);
5917 end if;
5919 -- Need to mark Anon_Id intrinsic before calling
5920 -- Analyze_Instance_And_Renamings because this flag may be propagated
5921 -- to other nodes.
5923 if Is_Intrinsic_Subprogram (Gen_Unit) then
5924 Set_Is_Intrinsic_Subprogram (Anon_Id);
5925 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5926 end if;
5928 Analyze_Instance_And_Renamings;
5930 -- Restore SPARK_Mode from the context after analysis of the package
5931 -- declaration, so that the SPARK_Mode on the generic spec does not
5932 -- apply to the pending instance for the instance body.
5934 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5935 and then Saved_SM /= Off
5936 and then Present (SPARK_Pragma (Gen_Unit))
5937 then
5938 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5939 end if;
5941 -- If the generic is marked Import (Intrinsic), then so is the
5942 -- instance; this indicates that there is no body to instantiate.
5943 -- We also copy the interface name in case this is handled by the
5944 -- back-end and deal with an instance of unchecked conversion.
5946 if Is_Intrinsic_Subprogram (Gen_Unit) then
5947 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5948 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5950 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5951 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5952 end if;
5953 end if;
5955 -- Inherit convention from generic unit. Intrinsic convention, as for
5956 -- an instance of unchecked conversion, is not inherited because an
5957 -- explicit Ada instance has been created.
5959 if Has_Convention_Pragma (Gen_Unit)
5960 and then Convention (Gen_Unit) /= Convention_Intrinsic
5961 then
5962 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5963 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5964 end if;
5966 Generate_Definition (Act_Decl_Id);
5968 -- Inherit all inlining-related flags which apply to the generic in
5969 -- the subprogram and its declaration.
5971 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5972 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5974 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5975 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5977 Set_Has_Pragma_Inline_Always
5978 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5979 Set_Has_Pragma_Inline_Always
5980 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5982 Set_Has_Pragma_No_Inline
5983 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5984 Set_Has_Pragma_No_Inline
5985 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5987 -- Propagate No_Return if pragma applied to generic unit. This must
5988 -- be done explicitly because pragma does not appear in generic
5989 -- declaration (unlike the aspect case).
5991 if No_Return (Gen_Unit) then
5992 Set_No_Return (Act_Decl_Id);
5993 Set_No_Return (Anon_Id);
5994 end if;
5996 -- Mark both the instance spec and the anonymous package in case the
5997 -- body is instantiated at a later pass. This preserves the original
5998 -- context in effect for the body.
6000 if SPARK_Mode /= On then
6001 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6002 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6003 end if;
6005 if Legacy_Elaboration_Checks
6006 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6007 then
6008 Check_Elab_Instantiation (N);
6009 end if;
6011 -- Save the scenario for later examination by the ABE Processing
6012 -- phase.
6014 Record_Elaboration_Scenario (N);
6016 -- The instantiation results in a guaranteed ABE. Create a completing
6017 -- body for the subprogram declaration because the real body will not
6018 -- be instantiated.
6020 if Is_Known_Guaranteed_ABE (N) then
6021 Provide_Completing_Bodies (Instance_Spec (N));
6022 end if;
6024 if Is_Dispatching_Operation (Act_Decl_Id)
6025 and then Ada_Version >= Ada_2005
6026 then
6027 declare
6028 Formal : Entity_Id;
6030 begin
6031 Formal := First_Formal (Act_Decl_Id);
6032 while Present (Formal) loop
6033 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6034 and then Is_Controlling_Formal (Formal)
6035 and then not Can_Never_Be_Null (Formal)
6036 then
6037 Error_Msg_NE
6038 ("access parameter& is controlling,", N, Formal);
6039 Error_Msg_NE
6040 ("\corresponding parameter of & must be explicitly "
6041 & "null-excluding", N, Gen_Id);
6042 end if;
6044 Next_Formal (Formal);
6045 end loop;
6046 end;
6047 end if;
6049 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6051 Validate_Categorization_Dependency (N, Act_Decl_Id);
6053 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6054 Inherit_Context (Gen_Decl, N);
6056 Restore_Private_Views (Pack_Id, False);
6058 -- If the context requires a full instantiation, mark node for
6059 -- subsequent construction of the body.
6061 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6062 Check_Forward_Instantiation (Gen_Decl);
6064 -- The wrapper package is always delayed, because it does not
6065 -- constitute a freeze point, but to insure that the freeze node
6066 -- is placed properly, it is created directly when instantiating
6067 -- the body (otherwise the freeze node might appear to early for
6068 -- nested instantiations).
6070 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6071 Rewrite (N, Unit (Parent (N)));
6072 Set_Unit (Parent (N), N);
6073 end if;
6075 -- Replace instance node for library-level instantiations of
6076 -- intrinsic subprograms.
6078 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6079 Rewrite (N, Unit (Parent (N)));
6080 Set_Unit (Parent (N), N);
6081 end if;
6083 if Parent_Installed then
6084 Remove_Parent;
6085 end if;
6087 Restore_Hidden_Primitives (Vis_Prims_List);
6088 Restore_Env;
6089 Env_Installed := False;
6090 Generic_Renamings.Set_Last (0);
6091 Generic_Renamings_HTable.Reset;
6092 end if;
6094 <<Leave>>
6095 -- Analyze aspects in declaration if no errors appear in the instance.
6097 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6098 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6099 end if;
6101 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6102 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6103 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6105 exception
6106 when Instantiation_Error =>
6107 if Parent_Installed then
6108 Remove_Parent;
6109 end if;
6111 if Env_Installed then
6112 Restore_Env;
6113 end if;
6115 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6116 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6117 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6118 end Analyze_Subprogram_Instantiation;
6120 ---------------------------
6121 -- Get_Associated_Entity --
6122 ---------------------------
6124 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6125 Assoc : Entity_Id;
6127 begin
6128 Assoc := Associated_Entity (Id);
6130 if Present (Assoc) then
6131 while Present (Associated_Entity (Assoc)) loop
6132 Assoc := Associated_Entity (Assoc);
6133 end loop;
6134 end if;
6136 return Assoc;
6137 end Get_Associated_Entity;
6139 -------------------------
6140 -- Get_Associated_Node --
6141 -------------------------
6143 function Get_Associated_Node (N : Node_Id) return Node_Id is
6144 Assoc : Node_Id;
6146 begin
6147 Assoc := Associated_Node (N);
6149 if Nkind (Assoc) /= Nkind (N) then
6150 return Assoc;
6152 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6153 return Assoc;
6155 else
6156 -- If the node is part of an inner generic, it may itself have been
6157 -- remapped into a further generic copy. Associated_Node is otherwise
6158 -- used for the entity of the node, and will be of a different node
6159 -- kind, or else N has been rewritten as a literal or function call.
6161 while Present (Associated_Node (Assoc))
6162 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6163 loop
6164 Assoc := Associated_Node (Assoc);
6165 end loop;
6167 -- Follow an additional link in case the final node was rewritten.
6168 -- This can only happen with nested generic units.
6170 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6171 and then Present (Associated_Node (Assoc))
6172 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6173 | N_Explicit_Dereference
6174 | N_Integer_Literal
6175 | N_Real_Literal
6176 | N_String_Literal
6177 then
6178 Assoc := Associated_Node (Assoc);
6179 end if;
6181 -- An additional special case: an unconstrained type in an object
6182 -- declaration may have been rewritten as a local subtype constrained
6183 -- by the expression in the declaration. We need to recover the
6184 -- original entity, which may be global.
6186 if Present (Original_Node (Assoc))
6187 and then Nkind (Parent (N)) = N_Object_Declaration
6188 then
6189 Assoc := Original_Node (Assoc);
6190 end if;
6192 return Assoc;
6193 end if;
6194 end Get_Associated_Node;
6196 -----------------------------------
6197 -- Build_Subprogram_Decl_Wrapper --
6198 -----------------------------------
6200 function Build_Subprogram_Decl_Wrapper
6201 (Formal_Subp : Entity_Id) return Node_Id
6203 Loc : constant Source_Ptr := Sloc (Current_Scope);
6204 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6205 Decl : Node_Id;
6206 Subp : Entity_Id;
6207 Parm_Spec : Node_Id;
6208 Profile : List_Id := New_List;
6209 Spec : Node_Id;
6210 Form_F : Entity_Id;
6211 New_F : Entity_Id;
6213 begin
6215 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6216 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6217 Set_Is_Generic_Actual_Subprogram (Subp);
6219 Profile := Parameter_Specifications (
6220 New_Copy_Tree
6221 (Specification (Unit_Declaration_Node (Formal_Subp))));
6223 Form_F := First_Formal (Formal_Subp);
6224 Parm_Spec := First (Profile);
6226 -- Create new entities for the formals. Reset entities so that
6227 -- parameter types are properly resolved when wrapper declaration
6228 -- is analyzed.
6230 while Present (Parm_Spec) loop
6231 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6232 Set_Defining_Identifier (Parm_Spec, New_F);
6233 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6234 Next (Parm_Spec);
6235 Next_Formal (Form_F);
6236 end loop;
6238 if Ret_Type = Standard_Void_Type then
6239 Spec :=
6240 Make_Procedure_Specification (Loc,
6241 Defining_Unit_Name => Subp,
6242 Parameter_Specifications => Profile);
6243 else
6244 Spec :=
6245 Make_Function_Specification (Loc,
6246 Defining_Unit_Name => Subp,
6247 Parameter_Specifications => Profile,
6248 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6249 end if;
6251 Decl :=
6252 Make_Subprogram_Declaration (Loc, Specification => Spec);
6254 return Decl;
6255 end Build_Subprogram_Decl_Wrapper;
6257 -----------------------------------
6258 -- Build_Subprogram_Body_Wrapper --
6259 -----------------------------------
6261 function Build_Subprogram_Body_Wrapper
6262 (Formal_Subp : Entity_Id;
6263 Actual_Name : Node_Id) return Node_Id
6265 Loc : constant Source_Ptr := Sloc (Current_Scope);
6266 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6267 Spec_Node : constant Node_Id :=
6268 Specification
6269 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6270 Act : Node_Id;
6271 Actuals : List_Id;
6272 Body_Node : Node_Id;
6273 Stmt : Node_Id;
6274 begin
6275 Actuals := New_List;
6276 Act := First (Parameter_Specifications (Spec_Node));
6278 while Present (Act) loop
6279 Append_To (Actuals,
6280 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6281 Next (Act);
6282 end loop;
6284 if Ret_Type = Standard_Void_Type then
6285 Stmt := Make_Procedure_Call_Statement (Loc,
6286 Name => Actual_Name,
6287 Parameter_Associations => Actuals);
6289 else
6290 Stmt := Make_Simple_Return_Statement (Loc,
6291 Expression =>
6292 Make_Function_Call (Loc,
6293 Name => Actual_Name,
6294 Parameter_Associations => Actuals));
6295 end if;
6297 Body_Node := Make_Subprogram_Body (Loc,
6298 Specification => Spec_Node,
6299 Declarations => New_List,
6300 Handled_Statement_Sequence =>
6301 Make_Handled_Sequence_Of_Statements (Loc,
6302 Statements => New_List (Stmt)));
6304 return Body_Node;
6305 end Build_Subprogram_Body_Wrapper;
6307 -------------------------------------------
6308 -- Build_Instance_Compilation_Unit_Nodes --
6309 -------------------------------------------
6311 procedure Build_Instance_Compilation_Unit_Nodes
6312 (N : Node_Id;
6313 Act_Body : Node_Id;
6314 Act_Decl : Node_Id)
6316 Decl_Cunit : Node_Id;
6317 Body_Cunit : Node_Id;
6318 Citem : Node_Id;
6319 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6320 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6322 begin
6323 -- A new compilation unit node is built for the instance declaration.
6324 -- It relocates the auxiliary declaration node from the compilation unit
6325 -- where the instance appeared, so that declarations that originally
6326 -- followed the instance will be attached to the spec compilation unit.
6328 Decl_Cunit :=
6329 Make_Compilation_Unit (Sloc (N),
6330 Context_Items => Empty_List,
6331 Unit => Act_Decl,
6332 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6334 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6336 -- The new compilation unit is linked to its body, but both share the
6337 -- same file, so we do not set Body_Required on the new unit so as not
6338 -- to create a spurious dependency on a non-existent body in the ali.
6339 -- This simplifies CodePeer unit traversal.
6341 -- We use the original instantiation compilation unit as the resulting
6342 -- compilation unit of the instance, since this is the main unit.
6344 Rewrite (N, Act_Body);
6346 Body_Cunit := Parent (N);
6348 -- The two compilation unit nodes are linked by the Library_Unit field
6350 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6351 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6353 -- Preserve the private nature of the package if needed
6355 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6357 -- If the instance is not the main unit, its context, categorization
6358 -- and elaboration entity are not relevant to the compilation.
6360 if Body_Cunit /= Cunit (Main_Unit) then
6361 Make_Instance_Unit (Body_Cunit, In_Main => False);
6362 return;
6363 end if;
6365 -- The context clause items on the instantiation, which are now attached
6366 -- to the body compilation unit (since the body overwrote the original
6367 -- instantiation node), semantically belong on the spec, so copy them
6368 -- there. It's harmless to leave them on the body as well. In fact one
6369 -- could argue that they belong in both places.
6371 Citem := First (Context_Items (Body_Cunit));
6372 while Present (Citem) loop
6373 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6374 Next (Citem);
6375 end loop;
6377 -- Propagate categorization flags on packages, so that they appear in
6378 -- the ali file for the spec of the unit.
6380 if Ekind (New_Main) = E_Package then
6381 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6382 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6383 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6384 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6385 Set_Is_Remote_Call_Interface
6386 (Old_Main, Is_Remote_Call_Interface (New_Main));
6387 end if;
6389 -- Make entry in Units table, so that binder can generate call to
6390 -- elaboration procedure for body, if any.
6392 Make_Instance_Unit (Body_Cunit, In_Main => True);
6393 Main_Unit_Entity := New_Main;
6394 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6396 -- Build elaboration entity, since the instance may certainly generate
6397 -- elaboration code requiring a flag for protection.
6399 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6400 end Build_Instance_Compilation_Unit_Nodes;
6402 --------------------------------
6403 -- Check_Abbreviated_Instance --
6404 --------------------------------
6406 procedure Check_Abbreviated_Instance
6407 (N : Node_Id;
6408 Parent_Installed : in out Boolean)
6410 Inst_Node : Node_Id;
6412 begin
6413 if Nkind (N) = N_Package_Specification
6414 and then Is_Abbreviated_Instance (Defining_Entity (N))
6415 then
6416 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6417 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6418 end if;
6419 end Check_Abbreviated_Instance;
6421 -----------------------------
6422 -- Check_Access_Definition --
6423 -----------------------------
6425 procedure Check_Access_Definition (N : Node_Id) is
6426 begin
6427 pragma Assert
6428 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6429 null;
6430 end Check_Access_Definition;
6432 -----------------------------------
6433 -- Check_Formal_Package_Instance --
6434 -----------------------------------
6436 -- If the formal has specific parameters, they must match those of the
6437 -- actual. Both of them are instances, and the renaming declarations for
6438 -- their formal parameters appear in the same order in both. The analyzed
6439 -- formal has been analyzed in the context of the current instance.
6441 procedure Check_Formal_Package_Instance
6442 (Formal_Pack : Entity_Id;
6443 Actual_Pack : Entity_Id)
6445 E1 : Entity_Id := First_Entity (Actual_Pack);
6446 E2 : Entity_Id := First_Entity (Formal_Pack);
6447 Prev_E1 : Entity_Id;
6449 Expr1 : Node_Id;
6450 Expr2 : Node_Id;
6452 procedure Check_Mismatch (B : Boolean);
6453 -- Common error routine for mismatch between the parameters of the
6454 -- actual instance and those of the formal package.
6456 function Is_Defaulted (Param : Entity_Id) return Boolean;
6457 -- If the formal package has partly box-initialized formals, skip
6458 -- conformance check for these formals. Previously the code assumed
6459 -- that box initialization for a formal package applied to all its
6460 -- formal parameters.
6462 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6463 -- The formal may come from a nested formal package, and the actual may
6464 -- have been constant-folded. To determine whether the two denote the
6465 -- same entity we may have to traverse several definitions to recover
6466 -- the ultimate entity that they refer to.
6468 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6469 -- The formal and the actual must be identical, but if both are
6470 -- given by attributes they end up renaming different generated bodies,
6471 -- and we must verify that the attributes themselves match.
6473 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6474 -- Similarly, if the formal comes from a nested formal package, the
6475 -- actual may designate the formal through multiple renamings, which
6476 -- have to be followed to determine the original variable in question.
6478 --------------------
6479 -- Check_Mismatch --
6480 --------------------
6482 procedure Check_Mismatch (B : Boolean) is
6483 -- A Formal_Type_Declaration for a derived private type is rewritten
6484 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6485 -- which is why we examine the original node.
6487 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6489 begin
6490 if Kind = N_Formal_Type_Declaration then
6491 return;
6493 elsif Kind in N_Formal_Object_Declaration
6494 | N_Formal_Package_Declaration
6495 | N_Formal_Subprogram_Declaration
6496 then
6497 null;
6499 -- Ada 2012: If both formal and actual are incomplete types they
6500 -- are conformant.
6502 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6503 null;
6505 elsif B then
6506 Error_Msg_NE
6507 ("actual for & in actual instance does not match formal",
6508 Parent (Actual_Pack), E1);
6509 end if;
6510 end Check_Mismatch;
6512 ------------------
6513 -- Is_Defaulted --
6514 ------------------
6516 function Is_Defaulted (Param : Entity_Id) return Boolean is
6517 Assoc : Node_Id;
6519 begin
6520 Assoc :=
6521 First (Generic_Associations (Parent
6522 (Associated_Formal_Package (Actual_Pack))));
6524 while Present (Assoc) loop
6525 if Nkind (Assoc) = N_Others_Choice then
6526 return True;
6528 elsif Nkind (Assoc) = N_Generic_Association
6529 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6530 then
6531 return Box_Present (Assoc);
6532 end if;
6534 Next (Assoc);
6535 end loop;
6537 return False;
6538 end Is_Defaulted;
6540 --------------------------------
6541 -- Same_Instantiated_Constant --
6542 --------------------------------
6544 function Same_Instantiated_Constant
6545 (E1, E2 : Entity_Id) return Boolean
6547 Ent : Entity_Id;
6549 begin
6550 Ent := E2;
6551 while Present (Ent) loop
6552 if E1 = Ent then
6553 return True;
6555 elsif Ekind (Ent) /= E_Constant then
6556 return False;
6558 elsif Is_Entity_Name (Constant_Value (Ent)) then
6559 if Entity (Constant_Value (Ent)) = E1 then
6560 return True;
6561 else
6562 Ent := Entity (Constant_Value (Ent));
6563 end if;
6565 -- The actual may be a constant that has been folded. Recover
6566 -- original name.
6568 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6569 Ent := Entity (Original_Node (Constant_Value (Ent)));
6571 else
6572 return False;
6573 end if;
6574 end loop;
6576 return False;
6577 end Same_Instantiated_Constant;
6579 --------------------------------
6580 -- Same_Instantiated_Function --
6581 --------------------------------
6583 function Same_Instantiated_Function
6584 (E1, E2 : Entity_Id) return Boolean
6586 U1, U2 : Node_Id;
6587 begin
6588 if Alias (E1) = Alias (E2) then
6589 return True;
6591 elsif Present (Alias (E2)) then
6592 U1 := Original_Node (Unit_Declaration_Node (E1));
6593 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6595 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6596 and then Nkind (Name (U1)) = N_Attribute_Reference
6598 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6599 and then Nkind (Name (U2)) = N_Attribute_Reference
6601 and then
6602 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6603 else
6604 return False;
6605 end if;
6606 end Same_Instantiated_Function;
6608 --------------------------------
6609 -- Same_Instantiated_Variable --
6610 --------------------------------
6612 function Same_Instantiated_Variable
6613 (E1, E2 : Entity_Id) return Boolean
6615 function Original_Entity (E : Entity_Id) return Entity_Id;
6616 -- Follow chain of renamings to the ultimate ancestor
6618 ---------------------
6619 -- Original_Entity --
6620 ---------------------
6622 function Original_Entity (E : Entity_Id) return Entity_Id is
6623 Orig : Entity_Id;
6625 begin
6626 Orig := E;
6627 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6628 and then Present (Renamed_Object (Orig))
6629 and then Is_Entity_Name (Renamed_Object (Orig))
6630 loop
6631 Orig := Entity (Renamed_Object (Orig));
6632 end loop;
6634 return Orig;
6635 end Original_Entity;
6637 -- Start of processing for Same_Instantiated_Variable
6639 begin
6640 return Ekind (E1) = Ekind (E2)
6641 and then Original_Entity (E1) = Original_Entity (E2);
6642 end Same_Instantiated_Variable;
6644 -- Start of processing for Check_Formal_Package_Instance
6646 begin
6647 Prev_E1 := E1;
6648 while Present (E1) and then Present (E2) loop
6649 exit when Ekind (E1) = E_Package
6650 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6652 -- If the formal is the renaming of the formal package, this
6653 -- is the end of its formal part, which may occur before the
6654 -- end of the formal part in the actual in the presence of
6655 -- defaulted parameters in the formal package.
6657 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6658 and then Renamed_Entity (E2) = Scope (E2);
6660 -- The analysis of the actual may generate additional internal
6661 -- entities. If the formal is defaulted, there is no corresponding
6662 -- analysis and the internal entities must be skipped, until we
6663 -- find corresponding entities again.
6665 if Comes_From_Source (E2)
6666 and then not Comes_From_Source (E1)
6667 and then Chars (E1) /= Chars (E2)
6668 then
6669 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6670 Next_Entity (E1);
6671 end loop;
6672 end if;
6674 if No (E1) then
6675 return;
6677 -- Entities may be declared without full declaration, such as
6678 -- itypes and predefined operators (concatenation for arrays, eg).
6679 -- Skip it and keep the formal entity to find a later match for it.
6681 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6682 E1 := Prev_E1;
6683 goto Next_E;
6685 -- If the formal entity comes from a formal declaration, it was
6686 -- defaulted in the formal package, and no check is needed on it.
6688 elsif Nkind (Original_Node (Parent (E2))) in
6689 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6690 then
6691 -- If the formal is a tagged type the corresponding class-wide
6692 -- type has been generated as well, and it must be skipped.
6694 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6695 Next_Entity (E2);
6696 end if;
6698 goto Next_E;
6700 -- Ditto for defaulted formal subprograms.
6702 elsif Is_Overloadable (E1)
6703 and then Nkind (Unit_Declaration_Node (E2)) in
6704 N_Formal_Subprogram_Declaration
6705 then
6706 goto Next_E;
6708 elsif Is_Defaulted (E1) then
6709 goto Next_E;
6711 elsif Is_Type (E1) then
6713 -- Subtypes must statically match. E1, E2 are the local entities
6714 -- that are subtypes of the actuals. Itypes generated for other
6715 -- parameters need not be checked, the check will be performed
6716 -- on the parameters themselves.
6718 -- If E2 is a formal type declaration, it is a defaulted parameter
6719 -- and needs no checking.
6721 if not Is_Itype (E1) and then not Is_Itype (E2) then
6722 Check_Mismatch
6723 (not Is_Type (E2)
6724 or else Etype (E1) /= Etype (E2)
6725 or else not Subtypes_Statically_Match (E1, E2));
6726 end if;
6728 elsif Ekind (E1) = E_Constant then
6730 -- IN parameters must denote the same static value, or the same
6731 -- constant, or the literal null.
6733 Expr1 := Expression (Parent (E1));
6735 if Ekind (E2) /= E_Constant then
6736 Check_Mismatch (True);
6737 goto Next_E;
6738 else
6739 Expr2 := Expression (Parent (E2));
6740 end if;
6742 if Is_OK_Static_Expression (Expr1) then
6743 if not Is_OK_Static_Expression (Expr2) then
6744 Check_Mismatch (True);
6746 elsif Is_Discrete_Type (Etype (E1)) then
6747 declare
6748 V1 : constant Uint := Expr_Value (Expr1);
6749 V2 : constant Uint := Expr_Value (Expr2);
6750 begin
6751 Check_Mismatch (V1 /= V2);
6752 end;
6754 elsif Is_Real_Type (Etype (E1)) then
6755 declare
6756 V1 : constant Ureal := Expr_Value_R (Expr1);
6757 V2 : constant Ureal := Expr_Value_R (Expr2);
6758 begin
6759 Check_Mismatch (V1 /= V2);
6760 end;
6762 elsif Is_String_Type (Etype (E1))
6763 and then Nkind (Expr1) = N_String_Literal
6764 then
6765 if Nkind (Expr2) /= N_String_Literal then
6766 Check_Mismatch (True);
6767 else
6768 Check_Mismatch
6769 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6770 end if;
6771 end if;
6773 elsif Is_Entity_Name (Expr1) then
6774 if Is_Entity_Name (Expr2) then
6775 if Entity (Expr1) = Entity (Expr2) then
6776 null;
6777 else
6778 Check_Mismatch
6779 (not Same_Instantiated_Constant
6780 (Entity (Expr1), Entity (Expr2)));
6781 end if;
6783 else
6784 Check_Mismatch (True);
6785 end if;
6787 elsif Is_Entity_Name (Original_Node (Expr1))
6788 and then Is_Entity_Name (Expr2)
6789 and then Same_Instantiated_Constant
6790 (Entity (Original_Node (Expr1)), Entity (Expr2))
6791 then
6792 null;
6794 elsif Nkind (Expr1) = N_Null then
6795 Check_Mismatch (Nkind (Expr1) /= N_Null);
6797 else
6798 Check_Mismatch (True);
6799 end if;
6801 elsif Ekind (E1) = E_Variable then
6802 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6804 elsif Ekind (E1) = E_Package then
6805 Check_Mismatch
6806 (Ekind (E1) /= Ekind (E2)
6807 or else (Present (Renamed_Entity (E2))
6808 and then Renamed_Entity (E1) /=
6809 Renamed_Entity (E2)));
6811 elsif Is_Overloadable (E1) then
6812 -- Verify that the actual subprograms match. Note that actuals
6813 -- that are attributes are rewritten as subprograms. If the
6814 -- subprogram in the formal package is defaulted, no check is
6815 -- needed. Note that this can only happen in Ada 2005 when the
6816 -- formal package can be partially parameterized.
6818 if Nkind (Unit_Declaration_Node (E1)) =
6819 N_Subprogram_Renaming_Declaration
6820 and then From_Default (Unit_Declaration_Node (E1))
6821 then
6822 null;
6824 -- If the formal package has an "others" box association that
6825 -- covers this formal, there is no need for a check either.
6827 elsif Nkind (Unit_Declaration_Node (E2)) in
6828 N_Formal_Subprogram_Declaration
6829 and then Box_Present (Unit_Declaration_Node (E2))
6830 then
6831 null;
6833 -- No check needed if subprogram is a defaulted null procedure
6835 elsif No (Alias (E2))
6836 and then Ekind (E2) = E_Procedure
6837 and then
6838 Null_Present (Specification (Unit_Declaration_Node (E2)))
6839 then
6840 null;
6842 -- Otherwise the actual in the formal and the actual in the
6843 -- instantiation of the formal must match, up to renamings.
6845 else
6846 Check_Mismatch
6847 (Ekind (E2) /= Ekind (E1)
6848 or else not Same_Instantiated_Function (E1, E2));
6849 end if;
6851 else
6852 raise Program_Error;
6853 end if;
6855 <<Next_E>>
6856 Prev_E1 := E1;
6857 Next_Entity (E1);
6858 Next_Entity (E2);
6859 end loop;
6860 end Check_Formal_Package_Instance;
6862 ---------------------------
6863 -- Check_Formal_Packages --
6864 ---------------------------
6866 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6867 E : Entity_Id;
6868 Formal_P : Entity_Id;
6869 Formal_Decl : Node_Id;
6871 begin
6872 -- Iterate through the declarations in the instance, looking for package
6873 -- renaming declarations that denote instances of formal packages, until
6874 -- we find the renaming of the current package itself. The declaration
6875 -- of a formal package that requires conformance checking is followed by
6876 -- an internal entity that is the abbreviated instance.
6878 E := First_Entity (P_Id);
6879 while Present (E) loop
6880 if Ekind (E) = E_Package then
6881 exit when Renamed_Entity (E) = P_Id;
6883 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6884 Formal_Decl := Parent (Associated_Formal_Package (E));
6886 if Requires_Conformance_Checking (Formal_Decl) then
6887 Formal_P := Next_Entity (E);
6889 -- If the instance is within an enclosing instance body
6890 -- there is no need to verify the legality of current formal
6891 -- packages because they were legal in the generic body.
6892 -- This optimization may be applicable elsewhere, and it
6893 -- also removes spurious errors that may arise with
6894 -- on-the-fly inlining and confusion between private and
6895 -- full views.
6897 if not In_Instance_Body then
6898 Check_Formal_Package_Instance (Formal_P, E);
6899 end if;
6901 -- Restore the visibility of formals of the formal instance
6902 -- that are not defaulted, and are hidden within the current
6903 -- generic. These formals may be visible within an enclosing
6904 -- generic.
6906 declare
6907 Elmt : Elmt_Id;
6908 begin
6909 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6910 while Present (Elmt) loop
6911 Set_Is_Hidden (Node (Elmt), False);
6912 Next_Elmt (Elmt);
6913 end loop;
6914 end;
6916 -- After checking, remove the internal validating package.
6917 -- It is only needed for semantic checks, and as it may
6918 -- contain generic formal declarations it should not reach
6919 -- gigi.
6921 Remove (Unit_Declaration_Node (Formal_P));
6922 end if;
6923 end if;
6924 end if;
6926 Next_Entity (E);
6927 end loop;
6928 end Check_Formal_Packages;
6930 ---------------------------------
6931 -- Check_Forward_Instantiation --
6932 ---------------------------------
6934 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6935 S : Entity_Id;
6936 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6938 begin
6939 -- The instantiation appears before the generic body if we are in the
6940 -- scope of the unit containing the generic, either in its spec or in
6941 -- the package body, and before the generic body.
6943 if Ekind (Gen_Comp) = E_Package_Body then
6944 Gen_Comp := Spec_Entity (Gen_Comp);
6945 end if;
6947 if In_Open_Scopes (Gen_Comp)
6948 and then No (Corresponding_Body (Decl))
6949 then
6950 S := Current_Scope;
6952 while Present (S)
6953 and then not Is_Compilation_Unit (S)
6954 and then not Is_Child_Unit (S)
6955 loop
6956 if Ekind (S) = E_Package then
6957 Set_Has_Forward_Instantiation (S);
6958 end if;
6960 S := Scope (S);
6961 end loop;
6962 end if;
6963 end Check_Forward_Instantiation;
6965 ---------------------------
6966 -- Check_Generic_Actuals --
6967 ---------------------------
6969 -- The visibility of the actuals may be different between the point of
6970 -- generic instantiation and the instantiation of the body.
6972 procedure Check_Generic_Actuals
6973 (Instance : Entity_Id;
6974 Is_Formal_Box : Boolean)
6976 Gen_Id : constant Entity_Id
6977 := (if Is_Generic_Unit (Instance) then
6978 Instance
6979 elsif Is_Wrapper_Package (Instance) then
6980 Generic_Parent
6981 (Specification
6982 (Unit_Declaration_Node (Related_Instance (Instance))))
6983 else
6984 Generic_Parent (Package_Specification (Instance)));
6985 -- The generic unit
6987 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
6988 -- The enclosing scope of the generic unit
6990 procedure Check_Actual_Type (Typ : Entity_Id);
6991 -- If the type of the actual is a private type declared in the enclosing
6992 -- scope of the generic, either directly or through packages nested in
6993 -- bodies, but not a derived type of a private type declared elsewhere,
6994 -- then the body of the generic sees the full view of the type because
6995 -- it has to appear in the package body. If the type is private now then
6996 -- exchange views to restore the proper visibility in the instance.
6998 -----------------------
6999 -- Check_Actual_Type --
7000 -----------------------
7002 procedure Check_Actual_Type (Typ : Entity_Id) is
7003 Btyp : constant Entity_Id := Base_Type (Typ);
7005 function Scope_Within_Body_Or_Same
7006 (Inner : Entity_Id;
7007 Outer : Entity_Id) return Boolean;
7008 -- Determine whether scope Inner is within the body of scope Outer
7009 -- or is Outer itself.
7011 -------------------------------
7012 -- Scope_Within_Body_Or_Same --
7013 -------------------------------
7015 function Scope_Within_Body_Or_Same
7016 (Inner : Entity_Id;
7017 Outer : Entity_Id) return Boolean
7019 Curr : Entity_Id := Inner;
7021 begin
7022 while Curr /= Standard_Standard loop
7023 if Curr = Outer then
7024 return True;
7026 elsif Is_Package_Body_Entity (Curr) then
7027 Curr := Scope (Curr);
7029 else
7030 exit;
7031 end if;
7032 end loop;
7034 return False;
7035 end Scope_Within_Body_Or_Same;
7037 begin
7038 -- The exchange is only needed if the generic is defined
7039 -- within a package which is not a common ancestor of the
7040 -- scope of the instance, and is not already in scope.
7042 if Is_Private_Type (Btyp)
7043 and then not Has_Private_Ancestor (Btyp)
7044 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7045 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7046 and then Parent_Scope /= Scope (Instance)
7047 and then not Is_Child_Unit (Gen_Id)
7048 then
7049 Switch_View (Btyp);
7051 -- If the type of the entity is a subtype, it may also have
7052 -- to be made visible, together with the base type of its
7053 -- full view, after exchange.
7055 if Is_Private_Type (Typ) then
7056 Switch_View (Typ);
7057 Switch_View (Base_Type (Typ));
7058 end if;
7059 end if;
7060 end Check_Actual_Type;
7062 -- Local variables
7064 Astype : Entity_Id;
7065 E : Entity_Id;
7066 Formal : Node_Id;
7068 -- Start of processing for Check_Generic_Actuals
7070 begin
7071 E := First_Entity (Instance);
7072 while Present (E) loop
7073 if Is_Type (E)
7074 and then Nkind (Parent (E)) = N_Subtype_Declaration
7075 and then Scope (Etype (E)) /= Instance
7076 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7077 then
7078 declare
7079 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7081 begin
7082 -- Restore the proper view of the actual from the information
7083 -- saved earlier by Instantiate_Type.
7085 Check_Private_View (Indic);
7087 -- If this view is an array type, check its component type.
7088 -- This handles the case of an array type whose component
7089 -- type is private, used as the actual in an instantiation
7090 -- of a generic construct declared in the same package as
7091 -- the component type and taking an array type with this
7092 -- component type as formal type parameter.
7094 if Is_Array_Type (Etype (Indic)) then
7095 Check_Actual_Type
7096 (Component_Type_For_Private_View (Etype (Indic)));
7097 end if;
7098 end;
7100 -- If the actual is itself the formal of a parent instance,
7101 -- then also restore the proper view of its actual and so on.
7102 -- That's necessary for nested instantiations of the form
7104 -- generic
7105 -- type Component is private;
7106 -- type Array_Type is array (Positive range <>) of Component;
7107 -- procedure Proc;
7109 -- when the outermost actuals have inconsistent views, because
7110 -- the Component_Type of Array_Type of the inner instantiations
7111 -- is the actual of Component of the outermost one and not that
7112 -- of the corresponding inner instantiations.
7114 Astype := Ancestor_Subtype (E);
7115 while Present (Astype)
7116 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7117 and then Present (Generic_Parent_Type (Parent (Astype)))
7118 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7119 loop
7120 Check_Private_View (Subtype_Indication (Parent (Astype)));
7121 Astype := Ancestor_Subtype (Astype);
7122 end loop;
7124 Set_Is_Generic_Actual_Type (E);
7126 if Is_Private_Type (E) and then Present (Full_View (E)) then
7127 Set_Is_Generic_Actual_Type (Full_View (E));
7128 end if;
7130 Set_Is_Hidden (E, False);
7131 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7133 -- We constructed the generic actual type as a subtype of the
7134 -- supplied type. This means that it normally would not inherit
7135 -- subtype specific attributes of the actual, which is wrong for
7136 -- the generic case.
7138 Astype := Ancestor_Subtype (E);
7140 if No (Astype) then
7142 -- This can happen when E is an itype that is the full view of
7143 -- a private type completed, e.g. with a constrained array. In
7144 -- that case, use the first subtype, which will carry size
7145 -- information. The base type itself is unconstrained and will
7146 -- not carry it.
7148 Astype := First_Subtype (E);
7149 end if;
7151 Set_Size_Info (E, Astype);
7152 Copy_RM_Size (To => E, From => Astype);
7153 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7155 if Is_Discrete_Or_Fixed_Point_Type (E) then
7156 Set_RM_Size (E, RM_Size (Astype));
7157 end if;
7159 elsif Ekind (E) = E_Package then
7161 -- If this is the renaming for the current instance, we're done.
7162 -- Otherwise it is a formal package. If the corresponding formal
7163 -- was declared with a box, the (instantiations of the) generic
7164 -- formal part are also visible. Otherwise, ignore the entity
7165 -- created to validate the actuals.
7167 if Renamed_Entity (E) = Instance then
7168 exit;
7170 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7171 null;
7173 -- The visibility of a formal of an enclosing generic is already
7174 -- correct.
7176 elsif Denotes_Formal_Package (E) then
7177 null;
7179 elsif Present (Associated_Formal_Package (E))
7180 and then not Is_Generic_Formal (E)
7181 then
7182 Check_Generic_Actuals
7183 (Renamed_Entity (E),
7184 Is_Formal_Box =>
7185 Box_Present (Parent (Associated_Formal_Package (E))));
7187 Set_Is_Hidden (E, False);
7188 end if;
7190 -- If this is a subprogram instance (in a wrapper package) the
7191 -- actual is fully visible.
7193 elsif Is_Wrapper_Package (Instance) then
7194 Set_Is_Hidden (E, False);
7196 -- If the formal package is declared with a box, or if the formal
7197 -- parameter is defaulted, it is visible in the body.
7199 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7200 Set_Is_Hidden (E, False);
7201 end if;
7203 -- Check directly the type of the actual objects, including the
7204 -- component type for array types.
7206 if Ekind (E) in E_Constant | E_Variable then
7207 Check_Actual_Type (Etype (E));
7209 if Is_Array_Type (Etype (E)) then
7210 Check_Actual_Type (Component_Type (Etype (E)));
7211 end if;
7213 -- As well as the type of formal parameters of actual subprograms
7215 elsif Ekind (E) in E_Function | E_Procedure
7216 and then Is_Generic_Actual_Subprogram (E)
7217 and then Present (Alias (E))
7218 then
7219 Formal := First_Formal (Alias (E));
7220 while Present (Formal) loop
7221 Check_Actual_Type (Etype (Formal));
7222 Next_Formal (Formal);
7223 end loop;
7224 end if;
7226 Next_Entity (E);
7227 end loop;
7228 end Check_Generic_Actuals;
7230 ------------------------------
7231 -- Check_Generic_Child_Unit --
7232 ------------------------------
7234 procedure Check_Generic_Child_Unit
7235 (Gen_Id : Node_Id;
7236 Parent_Installed : in out Boolean)
7238 Loc : constant Source_Ptr := Sloc (Gen_Id);
7239 Gen_Par : Entity_Id := Empty;
7240 E : Entity_Id;
7241 Inst_Par : Entity_Id := Empty;
7242 S : Node_Id;
7244 function Find_Generic_Child
7245 (Scop : Entity_Id;
7246 Id : Node_Id) return Entity_Id;
7247 -- Search generic parent for possible child unit with the given name
7249 function In_Enclosing_Instance return Boolean;
7250 -- Within an instance of the parent, the child unit may be denoted by
7251 -- a simple name, or an abbreviated expanded name. Examine enclosing
7252 -- scopes to locate a possible parent instantiation.
7254 ------------------------
7255 -- Find_Generic_Child --
7256 ------------------------
7258 function Find_Generic_Child
7259 (Scop : Entity_Id;
7260 Id : Node_Id) return Entity_Id
7262 E : Entity_Id;
7264 begin
7265 -- If entity of name is already set, instance has already been
7266 -- resolved, e.g. in an enclosing instantiation.
7268 if Present (Entity (Id)) then
7269 if Scope (Entity (Id)) = Scop then
7270 return Entity (Id);
7271 else
7272 return Empty;
7273 end if;
7275 else
7276 E := First_Entity (Scop);
7277 while Present (E) loop
7278 if Chars (E) = Chars (Id)
7279 and then Is_Child_Unit (E)
7280 then
7281 if Is_Child_Unit (E)
7282 and then not Is_Visible_Lib_Unit (E)
7283 then
7284 Error_Msg_NE
7285 ("generic child unit& is not visible", Gen_Id, E);
7286 end if;
7288 Set_Entity (Id, E);
7289 return E;
7290 end if;
7292 Next_Entity (E);
7293 end loop;
7295 return Empty;
7296 end if;
7297 end Find_Generic_Child;
7299 ---------------------------
7300 -- In_Enclosing_Instance --
7301 ---------------------------
7303 function In_Enclosing_Instance return Boolean is
7304 Enclosing_Instance : Node_Id;
7305 Instance_Decl : Node_Id;
7307 begin
7308 -- We do not inline any call that contains instantiations, except
7309 -- for instantiations of Unchecked_Conversion, so if we are within
7310 -- an inlined body the current instance does not require parents.
7312 if In_Inlined_Body then
7313 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7314 return False;
7315 end if;
7317 -- Loop to check enclosing scopes
7319 Enclosing_Instance := Current_Scope;
7320 while Present (Enclosing_Instance) loop
7321 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7323 if Ekind (Enclosing_Instance) = E_Package
7324 and then Is_Generic_Instance (Enclosing_Instance)
7325 and then Present
7326 (Generic_Parent (Specification (Instance_Decl)))
7327 then
7328 -- Check whether the generic we are looking for is a child of
7329 -- this instance.
7331 E := Find_Generic_Child
7332 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7333 exit when Present (E);
7335 else
7336 E := Empty;
7337 end if;
7339 Enclosing_Instance := Scope (Enclosing_Instance);
7340 end loop;
7342 if No (E) then
7344 -- Not a child unit
7346 Analyze (Gen_Id);
7347 return False;
7349 else
7350 Rewrite (Gen_Id,
7351 Make_Expanded_Name (Loc,
7352 Chars => Chars (E),
7353 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7354 Selector_Name => New_Occurrence_Of (E, Loc)));
7356 Set_Entity (Gen_Id, E);
7357 Set_Etype (Gen_Id, Etype (E));
7358 Parent_Installed := False; -- Already in scope.
7359 return True;
7360 end if;
7361 end In_Enclosing_Instance;
7363 -- Start of processing for Check_Generic_Child_Unit
7365 begin
7366 -- If the name of the generic is given by a selected component, it may
7367 -- be the name of a generic child unit, and the prefix is the name of an
7368 -- instance of the parent, in which case the child unit must be visible.
7369 -- If this instance is not in scope, it must be placed there and removed
7370 -- after instantiation, because what is being instantiated is not the
7371 -- original child, but the corresponding child present in the instance
7372 -- of the parent.
7374 -- If the child is instantiated within the parent, it can be given by
7375 -- a simple name. In this case the instance is already in scope, but
7376 -- the child generic must be recovered from the generic parent as well.
7378 if Nkind (Gen_Id) = N_Selected_Component then
7379 S := Selector_Name (Gen_Id);
7380 Analyze (Prefix (Gen_Id));
7381 Inst_Par := Entity (Prefix (Gen_Id));
7383 if Ekind (Inst_Par) = E_Package
7384 and then Present (Renamed_Entity (Inst_Par))
7385 then
7386 Inst_Par := Renamed_Entity (Inst_Par);
7387 end if;
7389 if Ekind (Inst_Par) = E_Package then
7390 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7391 Gen_Par := Generic_Parent (Parent (Inst_Par));
7393 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7394 and then
7395 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7396 then
7397 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7398 end if;
7400 elsif Ekind (Inst_Par) = E_Generic_Package
7401 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7402 then
7403 -- A formal package may be a real child package, and not the
7404 -- implicit instance within a parent. In this case the child is
7405 -- not visible and has to be retrieved explicitly as well.
7407 Gen_Par := Inst_Par;
7408 end if;
7410 if Present (Gen_Par) then
7412 -- The prefix denotes an instantiation. The entity itself may be a
7413 -- nested generic, or a child unit.
7415 E := Find_Generic_Child (Gen_Par, S);
7417 if Present (E) then
7418 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7419 Set_Entity (Gen_Id, E);
7420 Set_Etype (Gen_Id, Etype (E));
7421 Set_Entity (S, E);
7422 Set_Etype (S, Etype (E));
7424 -- Indicate that this is a reference to the parent
7426 if In_Extended_Main_Source_Unit (Gen_Id) then
7427 Set_Is_Instantiated (Inst_Par);
7428 end if;
7430 -- A common mistake is to replicate the naming scheme of a
7431 -- hierarchy by instantiating a generic child directly, rather
7432 -- than the implicit child in a parent instance:
7434 -- generic .. package Gpar is ..
7435 -- generic .. package Gpar.Child is ..
7436 -- package Par is new Gpar ();
7438 -- with Gpar.Child;
7439 -- package Par.Child is new Gpar.Child ();
7440 -- rather than Par.Child
7442 -- In this case the instantiation is within Par, which is an
7443 -- instance, but Gpar does not denote Par because we are not IN
7444 -- the instance of Gpar, so this is illegal. The test below
7445 -- recognizes this particular case.
7447 declare
7448 -- We want to reject the final instantiation in
7449 -- generic package G1 is end G1;
7450 -- generic package G1.G2 is end G1.G2;
7451 -- with G1; package I1 is new G1;
7452 -- with G1.G2; package I1.I2 is new G1.G2;
7453 -- because the use of G1.G2 should instead be either
7454 -- I1.G2 or simply G2. However, the tree that is built
7455 -- in this case is wrong. In the expanded copy
7456 -- of G2, we need (and therefore generate) a renaming
7457 -- package G1 renames I1;
7458 -- but this renaming should not participate in resolving
7459 -- this occurrence of the name "G1.G2"; unfortunately,
7460 -- it does. Rather than correct this error, we compensate
7461 -- for it in this function.
7463 -- We also perform another adjustment here. If we are
7464 -- currently inside a generic package, then that
7465 -- generic package needs to be treated as a package.
7466 -- For example, if a generic Aaa declares a nested generic
7467 -- Bbb (perhaps as a child unit) then Aaa can also legally
7468 -- declare an instance of Aaa.Bbb.
7470 function Adjusted_Inst_Par_Ekind return Entity_Kind;
7472 -----------------------------
7473 -- Adjusted_Inst_Par_Ekind --
7474 -----------------------------
7476 function Adjusted_Inst_Par_Ekind return Entity_Kind is
7477 Prefix_Entity : Entity_Id;
7478 Inst_Par_GP : Node_Id;
7479 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
7480 begin
7481 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
7482 then
7483 Inst_Par_Parent := Parent (Inst_Par_Parent);
7484 end if;
7486 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
7488 if Nkind (Gen_Id) = N_Expanded_Name
7489 and then Present (Inst_Par_GP)
7490 and then Ekind (Inst_Par_GP) = E_Generic_Package
7491 then
7492 Prefix_Entity := Entity (Prefix (Gen_Id));
7494 if Present (Prefix_Entity)
7495 and then not Comes_From_Source (Prefix_Entity)
7496 and then Nkind (Parent (Prefix_Entity)) =
7497 N_Package_Renaming_Declaration
7498 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
7499 then
7500 return E_Generic_Package;
7501 end if;
7502 end if;
7504 if Ekind (Inst_Par) = E_Generic_Package
7505 and then In_Open_Scopes (Inst_Par)
7506 then
7507 -- If we are inside a generic package then
7508 -- treat it as a package.
7509 return E_Package;
7510 end if;
7512 -- The usual path
7513 return Ekind (Inst_Par);
7514 end Adjusted_Inst_Par_Ekind;
7516 begin
7517 if Is_Child_Unit (E)
7518 and then (No (Inst_Par)
7519 or else Adjusted_Inst_Par_Ekind =
7520 E_Generic_Package)
7521 and then (not In_Instance
7522 or else Nkind (Parent (Parent (Gen_Id))) =
7523 N_Compilation_Unit)
7524 then
7525 Error_Msg_N
7526 ("prefix of generic child unit must be " &
7527 "instance of parent",
7528 Gen_Id);
7529 end if;
7530 end;
7532 if not In_Open_Scopes (Inst_Par)
7533 and then Nkind (Parent (Gen_Id)) not in
7534 N_Generic_Renaming_Declaration
7535 then
7536 Install_Parent (Inst_Par);
7537 Parent_Installed := True;
7539 elsif In_Open_Scopes (Inst_Par) then
7541 -- If the parent is already installed, install the actuals
7542 -- for its formal packages. This is necessary when the child
7543 -- instance is a child of the parent instance: in this case,
7544 -- the parent is placed on the scope stack but the formal
7545 -- packages are not made visible.
7547 Install_Formal_Packages (Inst_Par);
7548 end if;
7550 else
7551 -- If the generic parent does not contain an entity that
7552 -- corresponds to the selector, the instance doesn't either.
7553 -- Analyzing the node will yield the appropriate error message.
7554 -- If the entity is not a child unit, then it is an inner
7555 -- generic in the parent.
7557 Analyze (Gen_Id);
7558 end if;
7560 else
7561 Analyze (Gen_Id);
7563 if Is_Child_Unit (Entity (Gen_Id))
7564 and then
7565 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7566 and then not In_Open_Scopes (Inst_Par)
7567 then
7568 Install_Parent (Inst_Par);
7569 Parent_Installed := True;
7571 -- The generic unit may be the renaming of the implicit child
7572 -- present in an instance. In that case the parent instance is
7573 -- obtained from the name of the renamed entity.
7575 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7576 and then Present (Renamed_Entity (Entity (Gen_Id)))
7577 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7578 then
7579 declare
7580 Renamed_Package : constant Node_Id :=
7581 Name (Parent (Entity (Gen_Id)));
7582 begin
7583 if Nkind (Renamed_Package) = N_Expanded_Name then
7584 Inst_Par := Entity (Prefix (Renamed_Package));
7585 Install_Parent (Inst_Par);
7586 Parent_Installed := True;
7587 end if;
7588 end;
7589 end if;
7590 end if;
7592 elsif Nkind (Gen_Id) = N_Expanded_Name then
7594 -- Entity already present, analyze prefix, whose meaning may be an
7595 -- instance in the current context. If it is an instance of a
7596 -- relative within another, the proper parent may still have to be
7597 -- installed, if they are not of the same generation.
7599 Analyze (Prefix (Gen_Id));
7601 -- Prevent cascaded errors
7603 if Etype (Prefix (Gen_Id)) = Any_Type then
7604 return;
7605 end if;
7607 -- In the unlikely case that a local declaration hides the name of
7608 -- the parent package, locate it on the homonym chain. If the context
7609 -- is an instance of the parent, the renaming entity is flagged as
7610 -- such.
7612 Inst_Par := Entity (Prefix (Gen_Id));
7613 while Present (Inst_Par)
7614 and then not Is_Package_Or_Generic_Package (Inst_Par)
7615 loop
7616 Inst_Par := Homonym (Inst_Par);
7617 end loop;
7619 pragma Assert (Present (Inst_Par));
7620 Set_Entity (Prefix (Gen_Id), Inst_Par);
7622 if In_Enclosing_Instance then
7623 null;
7625 elsif Present (Entity (Gen_Id))
7626 and then No (Renamed_Entity (Entity (Gen_Id)))
7627 and then Is_Child_Unit (Entity (Gen_Id))
7628 and then not In_Open_Scopes (Inst_Par)
7629 then
7630 Install_Parent (Inst_Par);
7631 Parent_Installed := True;
7633 -- Handle renaming of generic child unit
7635 elsif Present (Entity (Gen_Id))
7636 and then Present (Renamed_Entity (Entity (Gen_Id)))
7637 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7638 then
7639 declare
7640 E : Entity_Id;
7641 Ren_Decl : Node_Id;
7643 begin
7644 -- The entity of the renamed generic child unit does not
7645 -- have any reference to the instantiated parent. In order to
7646 -- locate it we traverse the scope containing the renaming
7647 -- declaration; the instance of the parent is available in
7648 -- the prefix of the renaming declaration. For example:
7650 -- package A is
7651 -- package Inst_Par is new ...
7652 -- generic package Ren_Child renames Ins_Par.Child;
7653 -- end;
7655 -- with A;
7656 -- package B is
7657 -- package Inst_Child is new A.Ren_Child;
7658 -- end;
7660 E := First_Entity (Entity (Prefix (Gen_Id)));
7661 while Present (E) loop
7662 if not Is_Object (E)
7663 and then Present (Renamed_Entity (E))
7664 and then
7665 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7666 then
7667 Ren_Decl := Parent (E);
7668 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7670 if not In_Open_Scopes (Inst_Par) then
7671 Install_Parent (Inst_Par);
7672 Parent_Installed := True;
7673 end if;
7675 exit;
7676 end if;
7678 E := Next_Entity (E);
7679 end loop;
7680 end;
7681 end if;
7683 elsif In_Enclosing_Instance then
7685 -- The child unit is found in some enclosing scope
7687 null;
7689 else
7690 Analyze (Gen_Id);
7692 -- If this is the renaming of the implicit child in a parent
7693 -- instance, recover the parent name and install it.
7695 if Is_Entity_Name (Gen_Id) then
7696 E := Entity (Gen_Id);
7698 if Is_Generic_Unit (E)
7699 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7700 and then Is_Child_Unit (Renamed_Entity (E))
7701 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7702 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7703 then
7704 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7705 Inst_Par := Entity (Prefix (Gen_Id));
7707 if not In_Open_Scopes (Inst_Par) then
7708 Install_Parent (Inst_Par);
7709 Parent_Installed := True;
7710 end if;
7712 -- If it is a child unit of a non-generic parent, it may be
7713 -- use-visible and given by a direct name. Install parent as
7714 -- for other cases.
7716 elsif Is_Generic_Unit (E)
7717 and then Is_Child_Unit (E)
7718 and then
7719 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7720 and then not Is_Generic_Unit (Scope (E))
7721 then
7722 if not In_Open_Scopes (Scope (E)) then
7723 Install_Parent (Scope (E));
7724 Parent_Installed := True;
7725 end if;
7726 end if;
7727 end if;
7728 end if;
7729 end Check_Generic_Child_Unit;
7731 -----------------------------
7732 -- Check_Hidden_Child_Unit --
7733 -----------------------------
7735 procedure Check_Hidden_Child_Unit
7736 (N : Node_Id;
7737 Gen_Unit : Entity_Id;
7738 Act_Decl_Id : Entity_Id)
7740 Gen_Id : constant Node_Id := Name (N);
7742 begin
7743 if Is_Child_Unit (Gen_Unit)
7744 and then Is_Child_Unit (Act_Decl_Id)
7745 and then Nkind (Gen_Id) = N_Expanded_Name
7746 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7747 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7748 then
7749 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7750 Error_Msg_NE
7751 ("generic unit & is implicitly declared in &",
7752 Defining_Unit_Name (N), Gen_Unit);
7753 Error_Msg_N ("\instance must have different name",
7754 Defining_Unit_Name (N));
7755 end if;
7756 end Check_Hidden_Child_Unit;
7758 ------------------------
7759 -- Check_Private_View --
7760 ------------------------
7762 procedure Check_Private_View (N : Node_Id) is
7763 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
7764 Typ : constant Entity_Id :=
7765 (if Comparison then Compare_Type (N) else Etype (N));
7767 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
7768 -- Check that the available view of T matches Private_View and, if not,
7769 -- switch the view of T or of its base type.
7771 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
7772 BT : constant Entity_Id := Base_Type (T);
7774 begin
7775 -- If the full declaration was not visible in the generic, stop here
7777 if Private_View then
7778 return;
7779 end if;
7781 -- Exchange views if the type was not private in the generic but is
7782 -- private at the point of instantiation. Do not exchange views if
7783 -- the scope of the type is in scope. This can happen if both generic
7784 -- and instance are sibling units, or if type is defined in a parent.
7785 -- In this case the visibility of the type will be correct for all
7786 -- semantic checks.
7788 if Is_Private_Type (T)
7789 and then Present (Full_View (T))
7790 and then not In_Open_Scopes (Scope (T))
7791 then
7792 Switch_View (T);
7794 -- Finally, a nonprivate subtype may have a private base type, which
7795 -- must be exchanged for consistency. This can happen when a package
7796 -- body is instantiated, when the scope stack is empty but in fact
7797 -- the subtype and the base type are declared in an enclosing scope.
7799 -- Note that in this case we introduce an inconsistency in the view
7800 -- set, because we switch the base type BT, but there could be some
7801 -- private dependent subtypes of BT which remain unswitched. Such
7802 -- subtypes might need to be switched at a later point (see specific
7803 -- provision for that case in Switch_View).
7805 elsif not Is_Private_Type (T)
7806 and then Is_Private_Type (BT)
7807 and then Present (Full_View (BT))
7808 and then not In_Open_Scopes (BT)
7809 then
7810 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7811 Exchange_Declarations (BT);
7812 end if;
7813 end Check_Private_Type;
7815 begin
7816 if Present (Typ) then
7817 -- If the type appears in a subtype declaration, the subtype in
7818 -- instance must have a view compatible with that of its parent,
7819 -- which must be exchanged (see corresponding code in Restore_
7820 -- Private_Views) so we make an exception to the open scope rule
7821 -- implemented by Check_Private_Type above.
7823 if Has_Private_View (N)
7824 and then not Is_Private_Type (Typ)
7825 and then not Has_Been_Exchanged (Typ)
7826 and then (not In_Open_Scopes (Scope (Typ))
7827 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7828 then
7829 declare
7830 Assoc : constant Node_Id := Get_Associated_Node (N);
7832 begin
7833 -- In the generic, only the private declaration was visible
7835 Prepend_Elmt (Typ, Exchanged_Views);
7836 Exchange_Declarations
7837 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
7838 end;
7840 -- Check that the available views of Typ match their respective flag.
7841 -- Note that the type of a visible discriminant is never private.
7843 else
7844 Check_Private_Type (Typ, Has_Private_View (N));
7846 if Is_Access_Type (Typ) then
7847 Check_Private_Type
7848 (Designated_Type (Typ), Has_Secondary_Private_View (N));
7850 elsif Is_Array_Type (Typ) then
7851 Check_Private_Type
7852 (Component_Type_For_Private_View (Typ),
7853 Has_Secondary_Private_View (N));
7855 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
7856 and then Has_Discriminants (Typ)
7857 then
7858 declare
7859 Disc : Entity_Id;
7861 begin
7862 Disc := First_Discriminant (Typ);
7863 while Present (Disc) loop
7864 Check_Private_Type (Etype (Disc), False);
7865 Next_Discriminant (Disc);
7866 end loop;
7867 end;
7868 end if;
7869 end if;
7870 end if;
7871 end Check_Private_View;
7873 -----------------------------
7874 -- Check_Hidden_Primitives --
7875 -----------------------------
7877 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7878 Actual : Node_Id;
7879 Gen_T : Entity_Id;
7880 Result : Elist_Id := No_Elist;
7882 begin
7883 if No (Assoc_List) then
7884 return No_Elist;
7885 end if;
7887 -- Traverse the list of associations between formals and actuals
7888 -- searching for renamings of tagged types
7890 Actual := First (Assoc_List);
7891 while Present (Actual) loop
7892 if Nkind (Actual) = N_Subtype_Declaration then
7893 Gen_T := Generic_Parent_Type (Actual);
7895 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7897 -- Traverse the list of primitives of the actual types
7898 -- searching for hidden primitives that are visible in the
7899 -- corresponding generic formal; leave them visible and
7900 -- append them to Result to restore their decoration later.
7902 Install_Hidden_Primitives
7903 (Prims_List => Result,
7904 Gen_T => Gen_T,
7905 Act_T => Entity (Subtype_Indication (Actual)));
7906 end if;
7907 end if;
7909 Next (Actual);
7910 end loop;
7912 return Result;
7913 end Check_Hidden_Primitives;
7915 -------------------------------------
7916 -- Component_Type_For_Private_View --
7917 -------------------------------------
7919 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
7920 Typ : constant Entity_Id := Component_Type (T);
7922 begin
7923 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
7924 return Component_Type_For_Private_View (Typ);
7925 else
7926 return Typ;
7927 end if;
7928 end Component_Type_For_Private_View;
7930 --------------------------
7931 -- Contains_Instance_Of --
7932 --------------------------
7934 function Contains_Instance_Of
7935 (Inner : Entity_Id;
7936 Outer : Entity_Id;
7937 N : Node_Id) return Boolean
7939 Elmt : Elmt_Id;
7940 Scop : Entity_Id;
7942 begin
7943 Scop := Outer;
7945 -- Verify that there are no circular instantiations. We check whether
7946 -- the unit contains an instance of the current scope or some enclosing
7947 -- scope (in case one of the instances appears in a subunit). Longer
7948 -- circularities involving subunits might seem too pathological to
7949 -- consider, but they were not too pathological for the authors of
7950 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7951 -- enclosing generic scopes as containing an instance.
7953 loop
7954 -- Within a generic subprogram body, the scope is not generic, to
7955 -- allow for recursive subprograms. Use the declaration to determine
7956 -- whether this is a generic unit.
7958 if Ekind (Scop) = E_Generic_Package
7959 or else (Is_Subprogram (Scop)
7960 and then Nkind (Unit_Declaration_Node (Scop)) =
7961 N_Generic_Subprogram_Declaration)
7962 then
7963 Elmt := First_Elmt (Inner_Instances (Inner));
7965 while Present (Elmt) loop
7966 if Node (Elmt) = Scop then
7967 Error_Msg_Node_2 := Inner;
7968 Error_Msg_NE
7969 ("circular instantiation: & instantiated within &!",
7970 N, Scop);
7971 return True;
7973 elsif Node (Elmt) = Inner then
7974 return True;
7976 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7977 Error_Msg_Node_2 := Inner;
7978 Error_Msg_NE
7979 ("circular instantiation: & instantiated within &!",
7980 N, Node (Elmt));
7981 return True;
7982 end if;
7984 Next_Elmt (Elmt);
7985 end loop;
7987 -- Indicate that Inner is being instantiated within Scop
7989 Append_Elmt (Inner, Inner_Instances (Scop));
7990 end if;
7992 if Scop = Standard_Standard then
7993 exit;
7994 else
7995 Scop := Scope (Scop);
7996 end if;
7997 end loop;
7999 return False;
8000 end Contains_Instance_Of;
8002 -----------------------
8003 -- Copy_Generic_Node --
8004 -----------------------
8006 function Copy_Generic_Node
8007 (N : Node_Id;
8008 Parent_Id : Node_Id;
8009 Instantiating : Boolean) return Node_Id
8011 Ent : Entity_Id;
8012 New_N : Node_Id;
8014 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8015 -- Check the given value of one of the Fields referenced by the current
8016 -- node to determine whether to copy it recursively. The field may hold
8017 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8018 -- Char) in which case it need not be copied.
8020 procedure Copy_Descendants;
8021 -- Common utility for various nodes
8023 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8024 -- Make copy of element list
8026 function Copy_Generic_List
8027 (L : List_Id;
8028 Parent_Id : Node_Id) return List_Id;
8029 -- Apply Copy_Generic_Node recursively to the members of a node list
8031 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8032 -- True if an identifier is part of the defining program unit name of
8033 -- a child unit.
8034 -- Consider removing this subprogram now that ASIS no longer uses it.
8036 ----------------------
8037 -- Copy_Descendants --
8038 ----------------------
8040 procedure Copy_Descendants is
8041 procedure Walk is new
8042 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8043 begin
8044 Walk (New_N, N);
8045 end Copy_Descendants;
8047 -----------------------------
8048 -- Copy_Generic_Descendant --
8049 -----------------------------
8051 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8052 begin
8053 if D = Union_Id (Empty) then
8054 return D;
8056 elsif D in Node_Range then
8057 return Union_Id
8058 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8060 elsif D in List_Range then
8061 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8063 elsif D in Elist_Range then
8064 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8066 -- Nothing else is copyable (e.g. Uint values), return as is
8068 else
8069 return D;
8070 end if;
8071 end Copy_Generic_Descendant;
8073 ------------------------
8074 -- Copy_Generic_Elist --
8075 ------------------------
8077 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8078 M : Elmt_Id;
8079 L : Elist_Id;
8081 begin
8082 if Present (E) then
8083 L := New_Elmt_List;
8084 M := First_Elmt (E);
8085 while Present (M) loop
8086 Append_Elmt
8087 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8088 Next_Elmt (M);
8089 end loop;
8091 return L;
8093 else
8094 return No_Elist;
8095 end if;
8096 end Copy_Generic_Elist;
8098 -----------------------
8099 -- Copy_Generic_List --
8100 -----------------------
8102 function Copy_Generic_List
8103 (L : List_Id;
8104 Parent_Id : Node_Id) return List_Id
8106 N : Node_Id;
8107 New_L : List_Id;
8109 begin
8110 if Present (L) then
8111 New_L := New_List;
8112 Set_Parent (New_L, Parent_Id);
8114 N := First (L);
8115 while Present (N) loop
8116 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8117 Next (N);
8118 end loop;
8120 return New_L;
8122 else
8123 return No_List;
8124 end if;
8125 end Copy_Generic_List;
8127 ---------------------------
8128 -- In_Defining_Unit_Name --
8129 ---------------------------
8131 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8132 begin
8133 return
8134 Present (Parent (Nam))
8135 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8136 or else
8137 (Nkind (Parent (Nam)) = N_Expanded_Name
8138 and then In_Defining_Unit_Name (Parent (Nam))));
8139 end In_Defining_Unit_Name;
8141 -- Start of processing for Copy_Generic_Node
8143 begin
8144 if N = Empty then
8145 return N;
8146 end if;
8148 New_N := New_Copy (N);
8150 -- If we are instantiating, we want to adjust the sloc based on the
8151 -- current S_Adjustment. However, if this is the root node of a subunit,
8152 -- we need to defer that adjustment to below (see "elsif Instantiating
8153 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8154 -- computed the adjustment.
8156 if Instantiating
8157 and then not (Nkind (N) in N_Proper_Body
8158 and then Was_Originally_Stub (N))
8159 then
8160 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8161 end if;
8163 if not Is_List_Member (N) then
8164 Set_Parent (New_N, Parent_Id);
8165 end if;
8167 -- Special casing for identifiers and other entity names and operators
8169 if Nkind (N) in N_Character_Literal
8170 | N_Expanded_Name
8171 | N_Identifier
8172 | N_Operator_Symbol
8173 | N_Op
8174 then
8175 if not Instantiating then
8177 -- Link both nodes in order to assign subsequently the entity of
8178 -- the copy to the original node, in case this is a global
8179 -- reference.
8181 Set_Associated_Node (N, New_N);
8183 -- If we are within an instantiation, this is a nested generic
8184 -- that has already been analyzed at the point of definition.
8185 -- We must preserve references that were global to the enclosing
8186 -- parent at that point. Other occurrences, whether global or
8187 -- local to the current generic, must be resolved anew, so we
8188 -- reset the entity in the generic copy. A global reference has a
8189 -- smaller depth than the parent, or else the same depth in case
8190 -- both are distinct compilation units.
8192 -- A child unit is implicitly declared within the enclosing parent
8193 -- but is in fact global to it, and must be preserved.
8195 -- It is also possible for Current_Instantiated_Parent to be
8196 -- defined, and for this not to be a nested generic, namely if
8197 -- the unit is loaded through Rtsfind. In that case, the entity of
8198 -- New_N is only a link to the associated node, and not a defining
8199 -- occurrence.
8201 -- The entities for parent units in the defining_program_unit of a
8202 -- generic child unit are established when the context of the unit
8203 -- is first analyzed, before the generic copy is made. They are
8204 -- preserved in the copy for use in e.g. ASIS queries.
8206 Ent := Entity (New_N);
8208 if No (Current_Instantiated_Parent.Gen_Id) then
8209 if No (Ent)
8210 or else Nkind (Ent) /= N_Defining_Identifier
8211 or else not In_Defining_Unit_Name (N)
8212 then
8213 Set_Associated_Node (New_N, Empty);
8214 end if;
8216 elsif No (Ent)
8217 or else Nkind (Ent) not in N_Entity
8218 or else No (Scope (Ent))
8219 or else
8220 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8221 and then not Is_Child_Unit (Ent))
8222 or else
8223 (Scope_Depth_Set (Scope (Ent))
8224 and then
8225 Scope_Depth (Scope (Ent)) >
8226 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8227 and then
8228 Get_Source_Unit (Ent) =
8229 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8230 then
8231 Set_Associated_Node (New_N, Empty);
8232 end if;
8234 -- Case of instantiating identifier or some other name or operator
8236 else
8237 -- If the associated node is still defined, the entity in it
8238 -- is global, and must be copied to the instance. If this copy
8239 -- is being made for a body to inline, it is applied to an
8240 -- instantiated tree, and the entity is already present and
8241 -- must be also preserved.
8243 declare
8244 Assoc : constant Node_Id := Get_Associated_Node (N);
8246 begin
8247 if Present (Assoc) then
8248 if Nkind (Assoc) = Nkind (N) then
8249 Set_Entity (New_N, Entity (Assoc));
8250 Check_Private_View (N);
8252 -- The node is a reference to a global type and acts as the
8253 -- subtype mark of a qualified expression created in order
8254 -- to aid resolution of accidental overloading in instances.
8255 -- Since N is a reference to a type, the Associated_Node of
8256 -- N denotes an entity rather than another identifier. See
8257 -- Qualify_Universal_Operands for details.
8259 elsif Nkind (N) = N_Identifier
8260 and then Nkind (Parent (N)) = N_Qualified_Expression
8261 and then Subtype_Mark (Parent (N)) = N
8262 and then Is_Qualified_Universal_Literal (Parent (N))
8263 then
8264 Set_Entity (New_N, Assoc);
8266 -- Cope with the rewriting into expanded name that may have
8267 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8268 -- generic renaming declarations.
8270 elsif Nkind (Assoc) = N_Expanded_Name then
8271 Rewrite (N, New_Copy_Tree (Assoc));
8272 Set_Associated_Node (N, Assoc);
8273 return Copy_Generic_Node (N, Parent_Id, Instantiating);
8275 -- The name in the call may be a selected component if the
8276 -- call has not been analyzed yet, as may be the case for
8277 -- pre/post conditions in a generic unit.
8279 elsif Nkind (Assoc) = N_Function_Call
8280 and then Is_Entity_Name (Name (Assoc))
8281 then
8282 Set_Entity (New_N, Entity (Name (Assoc)));
8283 Check_Private_View (N);
8285 elsif Nkind (Assoc) in N_Entity
8286 and then (Expander_Active
8287 or else (GNATprove_Mode
8288 and then not In_Spec_Expression
8289 and then not Inside_A_Generic))
8290 then
8291 -- Inlining case: we are copying a tree that contains
8292 -- global entities, which are preserved in the copy to be
8293 -- used for subsequent inlining.
8295 null;
8297 else
8298 Set_Entity (New_N, Empty);
8299 end if;
8300 end if;
8301 end;
8302 end if;
8304 -- For expanded name, we must copy the Prefix and Selector_Name
8306 if Nkind (N) = N_Expanded_Name then
8307 Set_Prefix
8308 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8310 Set_Selector_Name (New_N,
8311 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8313 -- For operators, copy the operands
8315 elsif Nkind (N) in N_Op then
8316 if Nkind (N) in N_Binary_Op then
8317 Set_Left_Opnd (New_N,
8318 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8319 end if;
8321 Set_Right_Opnd (New_N,
8322 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8323 end if;
8325 -- Establish a link between an entity from the generic template and the
8326 -- corresponding entity in the generic copy to be analyzed.
8328 elsif Nkind (N) in N_Entity then
8329 if not Instantiating then
8330 Set_Associated_Entity (N, New_N);
8331 end if;
8333 -- Clear any existing link the copy may inherit from the replicated
8334 -- generic template entity.
8336 Set_Associated_Entity (New_N, Empty);
8338 -- Special casing for stubs
8340 elsif Nkind (N) in N_Body_Stub then
8342 -- In any case, we must copy the specification or defining
8343 -- identifier as appropriate.
8345 if Nkind (N) = N_Subprogram_Body_Stub then
8346 Set_Specification (New_N,
8347 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8349 else
8350 Set_Defining_Identifier (New_N,
8351 Copy_Generic_Node
8352 (Defining_Identifier (N), New_N, Instantiating));
8353 end if;
8355 -- If we are not instantiating, then this is where we load and
8356 -- analyze subunits, i.e. at the point where the stub occurs. A
8357 -- more permissive system might defer this analysis to the point
8358 -- of instantiation, but this seems too complicated for now.
8360 if not Instantiating then
8361 declare
8362 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8363 Subunit : Node_Id;
8364 Unum : Unit_Number_Type;
8365 New_Body : Node_Id;
8367 begin
8368 -- Make sure that, if it is a subunit of the main unit that is
8369 -- preprocessed and if -gnateG is specified, the preprocessed
8370 -- file will be written.
8372 Lib.Analysing_Subunit_Of_Main :=
8373 Lib.In_Extended_Main_Source_Unit (N);
8374 Unum :=
8375 Load_Unit
8376 (Load_Name => Subunit_Name,
8377 Required => False,
8378 Subunit => True,
8379 Error_Node => N);
8380 Lib.Analysing_Subunit_Of_Main := False;
8382 -- If the proper body is not found, a warning message will be
8383 -- emitted when analyzing the stub, or later at the point of
8384 -- instantiation. Here we just leave the stub as is.
8386 if Unum = No_Unit then
8387 Subunits_Missing := True;
8388 goto Subunit_Not_Found;
8389 end if;
8391 Subunit := Cunit (Unum);
8393 if Nkind (Unit (Subunit)) /= N_Subunit then
8394 Error_Msg_N
8395 ("found child unit instead of expected SEPARATE subunit",
8396 Subunit);
8397 Error_Msg_Sloc := Sloc (N);
8398 Error_Msg_N ("\to complete stub #", Subunit);
8399 goto Subunit_Not_Found;
8400 end if;
8402 -- We must create a generic copy of the subunit, in order to
8403 -- perform semantic analysis on it, and we must replace the
8404 -- stub in the original generic unit with the subunit, in order
8405 -- to preserve non-local references within.
8407 -- Only the proper body needs to be copied. Library_Unit and
8408 -- context clause are simply inherited by the generic copy.
8409 -- Note that the copy (which may be recursive if there are
8410 -- nested subunits) must be done first, before attaching it to
8411 -- the enclosing generic.
8413 New_Body :=
8414 Copy_Generic_Node
8415 (Proper_Body (Unit (Subunit)),
8416 Empty, Instantiating => False);
8418 -- Now place the original proper body in the original generic
8419 -- unit. This is a body, not a compilation unit.
8421 Rewrite (N, Proper_Body (Unit (Subunit)));
8422 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8423 Set_Was_Originally_Stub (N);
8425 -- Finally replace the body of the subunit with its copy, and
8426 -- make this new subunit into the library unit of the generic
8427 -- copy, which does not have stubs any longer.
8429 Set_Proper_Body (Unit (Subunit), New_Body);
8430 Set_Library_Unit (New_N, Subunit);
8431 Inherit_Context (Unit (Subunit), N);
8432 end;
8434 -- If we are instantiating, this must be an error case, since
8435 -- otherwise we would have replaced the stub node by the proper body
8436 -- that corresponds. So just ignore it in the copy (i.e. we have
8437 -- copied it, and that is good enough).
8439 else
8440 null;
8441 end if;
8443 <<Subunit_Not_Found>> null;
8445 -- If the node is a compilation unit, it is the subunit of a stub, which
8446 -- has been loaded already (see code below). In this case, the library
8447 -- unit field of N points to the parent unit (which is a compilation
8448 -- unit) and need not (and cannot) be copied.
8450 -- When the proper body of the stub is analyzed, the library_unit link
8451 -- is used to establish the proper context (see sem_ch10).
8453 -- The other fields of a compilation unit are copied as usual
8455 elsif Nkind (N) = N_Compilation_Unit then
8457 -- This code can only be executed when not instantiating, because in
8458 -- the copy made for an instantiation, the compilation unit node has
8459 -- disappeared at the point that a stub is replaced by its proper
8460 -- body.
8462 pragma Assert (not Instantiating);
8464 Set_Context_Items (New_N,
8465 Copy_Generic_List (Context_Items (N), New_N));
8467 Set_Unit (New_N,
8468 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8470 Set_First_Inlined_Subprogram (New_N,
8471 Copy_Generic_Node
8472 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8474 Set_Aux_Decls_Node
8475 (New_N,
8476 Copy_Generic_Node
8477 (Aux_Decls_Node (N), New_N, Instantiating => False));
8479 -- For an assignment node, the assignment is known to be semantically
8480 -- legal if we are instantiating the template. This avoids incorrect
8481 -- diagnostics in generated code.
8483 elsif Nkind (N) = N_Assignment_Statement then
8485 -- Copy name and expression fields in usual manner
8487 Set_Name (New_N,
8488 Copy_Generic_Node (Name (N), New_N, Instantiating));
8490 Set_Expression (New_N,
8491 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8493 if Instantiating then
8494 Set_Assignment_OK (Name (New_N), True);
8495 end if;
8497 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8498 if not Instantiating then
8499 Set_Associated_Node (N, New_N);
8501 else
8502 -- If, in the generic, the aggregate has a global composite type
8503 -- and, at the point of instantiation, the type has a private view
8504 -- then install the full view.
8506 declare
8507 Assoc : constant Node_Id := Get_Associated_Node (N);
8509 begin
8510 if Present (Assoc)
8511 and then Nkind (Assoc) = Nkind (N)
8512 and then Present (Etype (Assoc))
8513 and then Is_Private_Type (Etype (Assoc))
8514 then
8515 Switch_View (Etype (Assoc));
8516 end if;
8517 end;
8519 -- Moreover, for a full aggregate, if the type is a derived tagged
8520 -- type and has a global ancestor, then also restore the full view
8521 -- of this ancestor and do so up to the root type. Beware that the
8522 -- Ancestor_Type field is overloaded, so test that it's an entity.
8524 if Nkind (N) = N_Aggregate
8525 and then Present (Ancestor_Type (N))
8526 and then Nkind (Ancestor_Type (N)) in N_Entity
8527 then
8528 declare
8529 Root_Typ : constant Entity_Id :=
8530 Root_Type (Ancestor_Type (N));
8532 Typ : Entity_Id := Ancestor_Type (N);
8534 begin
8535 loop
8536 if Is_Private_Type (Typ) then
8537 Switch_View (Typ);
8538 end if;
8540 exit when Typ = Root_Typ;
8542 Typ := Etype (Typ);
8543 end loop;
8544 end;
8545 end if;
8546 end if;
8548 -- Do not copy the associated node, which points to the generic copy
8549 -- of the aggregate.
8551 if Nkind (N) = N_Aggregate then
8552 Set_Aggregate_Bounds
8553 (New_N,
8554 Node_Id (Copy_Generic_Descendant
8555 (Union_Id (Aggregate_Bounds (N)))));
8557 elsif Nkind (N) = N_Extension_Aggregate then
8558 Set_Ancestor_Part
8559 (New_N,
8560 Node_Id (Copy_Generic_Descendant
8561 (Union_Id (Ancestor_Part (N)))));
8563 else
8564 pragma Assert (False);
8565 end if;
8567 Set_Expressions
8568 (New_N,
8569 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8570 Set_Component_Associations
8571 (New_N,
8572 List_Id (Copy_Generic_Descendant
8573 (Union_Id (Component_Associations (N)))));
8574 Set_Etype
8575 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8577 -- Allocators do not have an identifier denoting the access type, so we
8578 -- must locate it through the expression to check whether the views are
8579 -- consistent.
8581 elsif Nkind (N) = N_Allocator
8582 and then Nkind (Expression (N)) = N_Qualified_Expression
8583 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8584 and then Instantiating
8585 then
8586 declare
8587 T : constant Node_Id :=
8588 Get_Associated_Node (Subtype_Mark (Expression (N)));
8589 Acc_T : Entity_Id;
8591 begin
8592 if Present (T) then
8594 -- Retrieve the allocator node in the generic copy
8596 Acc_T := Etype (Parent (Parent (T)));
8598 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8599 Switch_View (Acc_T);
8600 end if;
8601 end if;
8603 Copy_Descendants;
8604 end;
8606 -- Loop parameter specifications do not have an identifier denoting the
8607 -- index type, so we must locate it through the defining identifier to
8608 -- check whether the views are consistent.
8610 elsif Nkind (N) = N_Loop_Parameter_Specification
8611 and then Instantiating
8612 then
8613 declare
8614 Id : constant Entity_Id :=
8615 Get_Associated_Entity (Defining_Identifier (N));
8617 Index_T : Entity_Id;
8619 begin
8620 if Present (Id) and then Present (Etype (Id)) then
8621 Index_T := First_Subtype (Etype (Id));
8623 if Present (Index_T) and then Is_Private_Type (Index_T) then
8624 Switch_View (Index_T);
8625 end if;
8626 end if;
8628 Copy_Descendants;
8629 end;
8631 -- For a proper body, we must catch the case of a proper body that
8632 -- replaces a stub. This represents the point at which a separate
8633 -- compilation unit, and hence template file, may be referenced, so we
8634 -- must make a new source instantiation entry for the template of the
8635 -- subunit, and ensure that all nodes in the subunit are adjusted using
8636 -- this new source instantiation entry.
8638 elsif Nkind (N) in N_Proper_Body then
8639 declare
8640 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8641 begin
8642 if Instantiating and then Was_Originally_Stub (N) then
8643 Create_Instantiation_Source
8644 (Instantiation_Node,
8645 Defining_Entity (N),
8646 S_Adjustment);
8648 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8649 end if;
8651 -- Now copy the fields of the proper body, using the new
8652 -- adjustment factor if one was needed as per test above.
8654 Copy_Descendants;
8656 -- Restore the original adjustment factor
8658 S_Adjustment := Save_Adjustment;
8659 end;
8661 elsif Nkind (N) = N_Pragma and then Instantiating then
8663 -- Do not copy Comment or Ident pragmas their content is relevant to
8664 -- the generic unit, not to the instantiating unit.
8666 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8667 New_N := Make_Null_Statement (Sloc (N));
8669 -- Do not copy pragmas generated from aspects because the pragmas do
8670 -- not carry any semantic information, plus they will be regenerated
8671 -- in the instance.
8673 -- However, generating C we need to copy them since postconditions
8674 -- are inlined by the front end, and the front-end inlining machinery
8675 -- relies on this routine to perform inlining.
8677 elsif From_Aspect_Specification (N)
8678 and then not Modify_Tree_For_C
8679 then
8680 New_N := Make_Null_Statement (Sloc (N));
8682 else
8683 Copy_Descendants;
8684 end if;
8686 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8688 -- No descendant fields need traversing
8690 null;
8692 elsif Nkind (N) = N_String_Literal
8693 and then Present (Etype (N))
8694 and then Instantiating
8695 then
8696 -- If the string is declared in an outer scope, the string_literal
8697 -- subtype created for it may have the wrong scope. Force reanalysis
8698 -- of the constant to generate a new itype in the proper context.
8700 Set_Etype (New_N, Empty);
8701 Set_Analyzed (New_N, False);
8703 -- For the remaining nodes, copy their descendants recursively
8705 else
8706 Copy_Descendants;
8708 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8709 Set_Generic_Parent (Specification (New_N), N);
8711 -- Should preserve Corresponding_Spec??? (12.3(14))
8712 end if;
8713 end if;
8715 -- Propagate dimensions if present, so that they are reflected in the
8716 -- instance.
8718 if Nkind (N) in N_Has_Etype
8719 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8720 and then Present (Etype (N))
8721 and then Is_Floating_Point_Type (Etype (N))
8722 and then Has_Dimension_System (Etype (N))
8723 then
8724 Copy_Dimensions (N, New_N);
8725 end if;
8727 return New_N;
8728 end Copy_Generic_Node;
8730 ----------------------------
8731 -- Denotes_Formal_Package --
8732 ----------------------------
8734 function Denotes_Formal_Package
8735 (Pack : Entity_Id;
8736 On_Exit : Boolean := False;
8737 Instance : Entity_Id := Empty) return Boolean
8739 Par : Entity_Id;
8740 Scop : constant Entity_Id := Scope (Pack);
8741 E : Entity_Id;
8743 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8744 -- The package in question may be an actual for a previous formal
8745 -- package P of the current instance, so examine its actuals as well.
8746 -- This must be recursive over other formal packages.
8748 ----------------------------------
8749 -- Is_Actual_Of_Previous_Formal --
8750 ----------------------------------
8752 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8753 E1 : Entity_Id;
8755 begin
8756 E1 := First_Entity (P);
8757 while Present (E1) and then E1 /= Instance loop
8758 if Ekind (E1) = E_Package
8759 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8760 then
8761 if Renamed_Entity (E1) = Pack then
8762 return True;
8764 elsif E1 = P or else Renamed_Entity (E1) = P then
8765 return False;
8767 elsif Is_Actual_Of_Previous_Formal (E1) then
8768 return True;
8769 end if;
8770 end if;
8772 Next_Entity (E1);
8773 end loop;
8775 return False;
8776 end Is_Actual_Of_Previous_Formal;
8778 -- Start of processing for Denotes_Formal_Package
8780 begin
8781 if On_Exit then
8782 Par :=
8783 Instance_Envs.Table
8784 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8785 else
8786 Par := Current_Instantiated_Parent.Act_Id;
8787 end if;
8789 if Ekind (Scop) = E_Generic_Package
8790 or else Nkind (Unit_Declaration_Node (Scop)) =
8791 N_Generic_Subprogram_Declaration
8792 then
8793 return True;
8795 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8796 N_Formal_Package_Declaration
8797 then
8798 return True;
8800 elsif No (Par) then
8801 return False;
8803 else
8804 -- Check whether this package is associated with a formal package of
8805 -- the enclosing instantiation. Iterate over the list of renamings.
8807 E := First_Entity (Par);
8808 while Present (E) loop
8809 if Ekind (E) /= E_Package
8810 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8811 then
8812 null;
8814 elsif Renamed_Entity (E) = Par then
8815 return False;
8817 elsif Renamed_Entity (E) = Pack then
8818 return True;
8820 elsif Is_Actual_Of_Previous_Formal (E) then
8821 return True;
8823 end if;
8825 Next_Entity (E);
8826 end loop;
8828 return False;
8829 end if;
8830 end Denotes_Formal_Package;
8832 -----------------
8833 -- End_Generic --
8834 -----------------
8836 procedure End_Generic is
8837 begin
8838 -- ??? More things could be factored out in this routine. Should
8839 -- probably be done at a later stage.
8841 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8842 Generic_Flags.Decrement_Last;
8844 Expander_Mode_Restore;
8845 end End_Generic;
8847 -------------
8848 -- Earlier --
8849 -------------
8851 function Earlier (N1, N2 : Node_Id) return Boolean is
8852 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8853 -- Find distance from given node to enclosing compilation unit
8855 ----------------
8856 -- Find_Depth --
8857 ----------------
8859 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8860 begin
8861 while Present (P)
8862 and then Nkind (P) /= N_Compilation_Unit
8863 loop
8864 P := True_Parent (P);
8865 D := D + 1;
8866 end loop;
8867 end Find_Depth;
8869 -- Local declarations
8871 D1 : Integer := 0;
8872 D2 : Integer := 0;
8873 P1 : Node_Id := N1;
8874 P2 : Node_Id := N2;
8875 T1 : Source_Ptr;
8876 T2 : Source_Ptr;
8878 -- Start of processing for Earlier
8880 begin
8881 Find_Depth (P1, D1);
8882 Find_Depth (P2, D2);
8884 if P1 /= P2 then
8885 return False;
8886 else
8887 P1 := N1;
8888 P2 := N2;
8889 end if;
8891 while D1 > D2 loop
8892 P1 := True_Parent (P1);
8893 D1 := D1 - 1;
8894 end loop;
8896 while D2 > D1 loop
8897 P2 := True_Parent (P2);
8898 D2 := D2 - 1;
8899 end loop;
8901 -- At this point P1 and P2 are at the same distance from the root.
8902 -- We examine their parents until we find a common declarative list.
8903 -- If we reach the root, N1 and N2 do not descend from the same
8904 -- declarative list (e.g. one is nested in the declarative part and
8905 -- the other is in a block in the statement part) and the earlier
8906 -- one is already frozen.
8908 while not Is_List_Member (P1)
8909 or else not Is_List_Member (P2)
8910 or else not In_Same_List (P1, P2)
8911 loop
8912 P1 := True_Parent (P1);
8913 P2 := True_Parent (P2);
8915 if Nkind (Parent (P1)) = N_Subunit then
8916 P1 := Corresponding_Stub (Parent (P1));
8917 end if;
8919 if Nkind (Parent (P2)) = N_Subunit then
8920 P2 := Corresponding_Stub (Parent (P2));
8921 end if;
8923 if P1 = P2 then
8924 return False;
8925 end if;
8926 end loop;
8928 -- Expanded code usually shares the source location of the original
8929 -- construct it was generated for. This however may not necessarily
8930 -- reflect the true location of the code within the tree.
8932 -- Before comparing the slocs of the two nodes, make sure that we are
8933 -- working with correct source locations. Assume that P1 is to the left
8934 -- of P2. If either one does not come from source, traverse the common
8935 -- list heading towards the other node and locate the first source
8936 -- statement.
8938 -- P1 P2
8939 -- ----+===+===+--------------+===+===+----
8940 -- expanded code expanded code
8942 if not Comes_From_Source (P1) then
8943 while Present (P1) loop
8945 -- Neither P2 nor a source statement were located during the
8946 -- search. If we reach the end of the list, then P1 does not
8947 -- occur earlier than P2.
8949 -- ---->
8950 -- start --- P2 ----- P1 --- end
8952 if No (Next (P1)) then
8953 return False;
8955 -- We encounter P2 while going to the right of the list. This
8956 -- means that P1 does indeed appear earlier.
8958 -- ---->
8959 -- start --- P1 ===== P2 --- end
8960 -- expanded code in between
8962 elsif P1 = P2 then
8963 return True;
8965 -- No need to look any further since we have located a source
8966 -- statement.
8968 elsif Comes_From_Source (P1) then
8969 exit;
8970 end if;
8972 -- Keep going right
8974 Next (P1);
8975 end loop;
8976 end if;
8978 if not Comes_From_Source (P2) then
8979 while Present (P2) loop
8981 -- Neither P1 nor a source statement were located during the
8982 -- search. If we reach the start of the list, then P1 does not
8983 -- occur earlier than P2.
8985 -- <----
8986 -- start --- P2 --- P1 --- end
8988 if No (Prev (P2)) then
8989 return False;
8991 -- We encounter P1 while going to the left of the list. This
8992 -- means that P1 does indeed appear earlier.
8994 -- <----
8995 -- start --- P1 ===== P2 --- end
8996 -- expanded code in between
8998 elsif P2 = P1 then
8999 return True;
9001 -- No need to look any further since we have located a source
9002 -- statement.
9004 elsif Comes_From_Source (P2) then
9005 exit;
9006 end if;
9008 -- Keep going left
9010 Prev (P2);
9011 end loop;
9012 end if;
9014 -- At this point either both nodes came from source or we approximated
9015 -- their source locations through neighboring source statements.
9017 T1 := Top_Level_Location (Sloc (P1));
9018 T2 := Top_Level_Location (Sloc (P2));
9020 -- When two nodes come from the same instance, they have identical top
9021 -- level locations. To determine proper relation within the tree, check
9022 -- their locations within the template.
9024 if T1 = T2 then
9025 return Sloc (P1) < Sloc (P2);
9027 -- The two nodes either come from unrelated instances or do not come
9028 -- from instantiated code at all.
9030 else
9031 return T1 < T2;
9032 end if;
9033 end Earlier;
9035 ----------------------
9036 -- Find_Actual_Type --
9037 ----------------------
9039 function Find_Actual_Type
9040 (Typ : Entity_Id;
9041 Gen_Type : Entity_Id) return Entity_Id
9043 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9044 T : Entity_Id;
9046 begin
9047 -- Special processing only applies to child units
9049 if not Is_Child_Unit (Gen_Scope) then
9050 return Get_Instance_Of (Typ);
9052 -- If designated or component type is itself a formal of the child unit,
9053 -- its instance is available.
9055 elsif Scope (Typ) = Gen_Scope then
9056 return Get_Instance_Of (Typ);
9058 -- If the array or access type is not declared in the parent unit,
9059 -- no special processing needed.
9061 elsif not Is_Generic_Type (Typ)
9062 and then Scope (Gen_Scope) /= Scope (Typ)
9063 then
9064 return Get_Instance_Of (Typ);
9066 -- Otherwise, retrieve designated or component type by visibility
9068 else
9069 T := Current_Entity (Typ);
9070 while Present (T) loop
9071 if In_Open_Scopes (Scope (T)) then
9072 return T;
9073 elsif Is_Generic_Actual_Type (T) then
9074 return T;
9075 end if;
9077 T := Homonym (T);
9078 end loop;
9080 return Typ;
9081 end if;
9082 end Find_Actual_Type;
9084 -----------------------------
9085 -- Freeze_Package_Instance --
9086 -----------------------------
9088 procedure Freeze_Package_Instance
9089 (N : Node_Id;
9090 Gen_Body : Node_Id;
9091 Gen_Decl : Node_Id;
9092 Act_Id : Entity_Id)
9094 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9095 -- Check if the generic definition and the instantiation come from
9096 -- a common scope, in which case the instance must be frozen after
9097 -- the generic body.
9099 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9100 -- If the instance is nested inside a generic unit, the Sloc of the
9101 -- instance indicates the place of the original definition, not the
9102 -- point of the current enclosing instance. Pending a better usage of
9103 -- Slocs to indicate instantiation places, we determine the place of
9104 -- origin of a node by finding the maximum sloc of any ancestor node.
9106 -- Why is this not equivalent to Top_Level_Location ???
9108 -------------------
9109 -- In_Same_Scope --
9110 -------------------
9112 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9113 Act_Scop : Entity_Id := Scope (Act_Id);
9114 Gen_Scop : Entity_Id := Scope (Gen_Id);
9116 begin
9117 while Act_Scop /= Standard_Standard
9118 and then Gen_Scop /= Standard_Standard
9119 loop
9120 if Act_Scop = Gen_Scop then
9121 return True;
9122 end if;
9124 Act_Scop := Scope (Act_Scop);
9125 Gen_Scop := Scope (Gen_Scop);
9126 end loop;
9128 return False;
9129 end In_Same_Scope;
9131 ---------------
9132 -- True_Sloc --
9133 ---------------
9135 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9136 N1 : Node_Id;
9137 Res : Source_Ptr;
9139 begin
9140 Res := Sloc (N);
9141 N1 := N;
9142 while Present (N1) and then N1 /= Act_Unit loop
9143 if Sloc (N1) > Res then
9144 Res := Sloc (N1);
9145 end if;
9147 N1 := Parent (N1);
9148 end loop;
9150 return Res;
9151 end True_Sloc;
9153 -- Local variables
9155 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9156 Par_Id : constant Entity_Id := Scope (Gen_Id);
9157 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9158 Gen_Unit : constant Node_Id :=
9159 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9161 Body_Unit : Node_Id;
9162 F_Node : Node_Id;
9163 Must_Delay : Boolean;
9164 Orig_Body : Node_Id;
9166 -- Start of processing for Freeze_Package_Instance
9168 begin
9169 -- If the body is a subunit, the freeze point is the corresponding stub
9170 -- in the current compilation, not the subunit itself.
9172 if Nkind (Parent (Gen_Body)) = N_Subunit then
9173 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9174 else
9175 Orig_Body := Gen_Body;
9176 end if;
9178 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9180 -- If the instantiation and the generic definition appear in the same
9181 -- package declaration, this is an early instantiation. If they appear
9182 -- in the same declarative part, it is an early instantiation only if
9183 -- the generic body appears textually later, and the generic body is
9184 -- also in the main unit.
9186 -- If instance is nested within a subprogram, and the generic body
9187 -- is not, the instance is delayed because the enclosing body is. If
9188 -- instance and body are within the same scope, or the same subprogram
9189 -- body, indicate explicitly that the instance is delayed.
9191 Must_Delay :=
9192 (Gen_Unit = Act_Unit
9193 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9194 | N_Package_Declaration
9195 or else (Gen_Unit = Body_Unit
9196 and then
9197 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9198 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9199 and then In_Same_Scope (Gen_Id, Act_Id));
9201 -- If this is an early instantiation, the freeze node is placed after
9202 -- the generic body. Otherwise, if the generic appears in an instance,
9203 -- we cannot freeze the current instance until the outer one is frozen.
9204 -- This is only relevant if the current instance is nested within some
9205 -- inner scope not itself within the outer instance. If this scope is
9206 -- a package body in the same declarative part as the outer instance,
9207 -- then that body needs to be frozen after the outer instance. Finally,
9208 -- if no delay is needed, we place the freeze node at the end of the
9209 -- current declarative part.
9211 if No (Freeze_Node (Act_Id))
9212 or else not Is_List_Member (Freeze_Node (Act_Id))
9213 then
9214 Ensure_Freeze_Node (Act_Id);
9215 F_Node := Freeze_Node (Act_Id);
9217 if Must_Delay then
9218 Insert_After (Orig_Body, F_Node);
9220 elsif Is_Generic_Instance (Par_Id)
9221 and then Present (Freeze_Node (Par_Id))
9222 and then Scope (Act_Id) /= Par_Id
9223 then
9224 -- Freeze instance of inner generic after instance of enclosing
9225 -- generic.
9227 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9229 -- Handle the following case:
9231 -- package Parent_Inst is new ...
9232 -- freeze Parent_Inst []
9234 -- procedure P ... -- this body freezes Parent_Inst
9236 -- package Inst is new ...
9238 -- In this particular scenario, the freeze node for Inst must
9239 -- be inserted in the same manner as that of Parent_Inst,
9240 -- before the next source body or at the end of the declarative
9241 -- list (body not available). If body P did not exist and
9242 -- Parent_Inst was frozen after Inst, either by a body
9243 -- following Inst or at the end of the declarative region,
9244 -- the freeze node for Inst must be inserted after that of
9245 -- Parent_Inst. This relation is established by comparing
9246 -- the Slocs of Parent_Inst freeze node and Inst.
9247 -- We examine the parents of the enclosing lists to handle
9248 -- the case where the parent instance is in the visible part
9249 -- of a package declaration, and the inner instance is in
9250 -- the corresponding private part.
9252 if Parent (List_Containing (Freeze_Node (Par_Id)))
9253 = Parent (List_Containing (N))
9254 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9255 then
9256 Insert_Freeze_Node_For_Instance (N, F_Node);
9257 else
9258 Insert_After (Freeze_Node (Par_Id), F_Node);
9259 end if;
9261 -- Freeze package enclosing instance of inner generic after
9262 -- instance of enclosing generic.
9264 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9265 and then In_Same_Declarative_Part
9266 (Parent (Freeze_Node (Par_Id)), Parent (N))
9267 then
9268 declare
9269 Enclosing : Entity_Id;
9271 begin
9272 Enclosing := Corresponding_Spec (Parent (N));
9274 if No (Enclosing) then
9275 Enclosing := Defining_Entity (Parent (N));
9276 end if;
9278 Insert_Freeze_Node_For_Instance (N, F_Node);
9279 Ensure_Freeze_Node (Enclosing);
9281 if not Is_List_Member (Freeze_Node (Enclosing)) then
9283 -- The enclosing context is a subunit, insert the freeze
9284 -- node after the stub.
9286 if Nkind (Parent (Parent (N))) = N_Subunit then
9287 Insert_Freeze_Node_For_Instance
9288 (Corresponding_Stub (Parent (Parent (N))),
9289 Freeze_Node (Enclosing));
9291 -- The enclosing context is a package with a stub body
9292 -- which has already been replaced by the real body.
9293 -- Insert the freeze node after the actual body.
9295 elsif Ekind (Enclosing) = E_Package
9296 and then Present (Body_Entity (Enclosing))
9297 and then Was_Originally_Stub
9298 (Parent (Body_Entity (Enclosing)))
9299 then
9300 Insert_Freeze_Node_For_Instance
9301 (Parent (Body_Entity (Enclosing)),
9302 Freeze_Node (Enclosing));
9304 -- The parent instance has been frozen before the body of
9305 -- the enclosing package, insert the freeze node after
9306 -- the body.
9308 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9309 and then
9310 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9311 then
9312 Insert_Freeze_Node_For_Instance
9313 (Parent (N), Freeze_Node (Enclosing));
9315 else
9316 Insert_After
9317 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9318 end if;
9319 end if;
9320 end;
9322 else
9323 Insert_Freeze_Node_For_Instance (N, F_Node);
9324 end if;
9326 else
9327 Insert_Freeze_Node_For_Instance (N, F_Node);
9328 end if;
9329 end if;
9330 end Freeze_Package_Instance;
9332 --------------------------------
9333 -- Freeze_Subprogram_Instance --
9334 --------------------------------
9336 procedure Freeze_Subprogram_Instance
9337 (N : Node_Id;
9338 Gen_Body : Node_Id;
9339 Pack_Id : Entity_Id)
9341 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9342 -- Find innermost package body that encloses the given node, and which
9343 -- is not a compilation unit. Freeze nodes for the instance, or for its
9344 -- enclosing body, may be inserted after the enclosing_body of the
9345 -- generic unit. Used to determine proper placement of freeze node for
9346 -- both package and subprogram instances.
9348 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9349 -- Find entity for given package body, and locate or create a freeze
9350 -- node for it.
9352 ----------------------------
9353 -- Enclosing_Package_Body --
9354 ----------------------------
9356 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9357 P : Node_Id;
9359 begin
9360 P := Parent (N);
9361 while Present (P)
9362 and then Nkind (Parent (P)) /= N_Compilation_Unit
9363 loop
9364 if Nkind (P) = N_Package_Body then
9365 if Nkind (Parent (P)) = N_Subunit then
9366 return Corresponding_Stub (Parent (P));
9367 else
9368 return P;
9369 end if;
9370 end if;
9372 P := True_Parent (P);
9373 end loop;
9375 return Empty;
9376 end Enclosing_Package_Body;
9378 -------------------------
9379 -- Package_Freeze_Node --
9380 -------------------------
9382 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9383 Id : Entity_Id;
9385 begin
9386 if Nkind (B) = N_Package_Body then
9387 Id := Corresponding_Spec (B);
9388 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9389 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9390 end if;
9392 Ensure_Freeze_Node (Id);
9393 return Freeze_Node (Id);
9394 end Package_Freeze_Node;
9396 -- Local variables
9398 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9399 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9400 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9402 Enc_G_F : Node_Id;
9403 F_Node : Node_Id;
9405 -- Start of processing for Freeze_Subprogram_Instance
9407 begin
9408 -- If the instance and the generic body appear within the same unit, and
9409 -- the instance precedes the generic, the freeze node for the instance
9410 -- must appear after that of the generic. If the generic is nested
9411 -- within another instance I2, then current instance must be frozen
9412 -- after I2. In both cases, the freeze nodes are those of enclosing
9413 -- packages. Otherwise, the freeze node is placed at the end of the
9414 -- current declarative part.
9416 Ensure_Freeze_Node (Pack_Id);
9417 F_Node := Freeze_Node (Pack_Id);
9419 if Is_Generic_Instance (Par_Id)
9420 and then Present (Freeze_Node (Par_Id))
9421 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9422 then
9423 -- The parent was a premature instantiation. Insert freeze node at
9424 -- the end the current declarative part.
9426 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9427 Insert_Freeze_Node_For_Instance (N, F_Node);
9429 -- Handle the following case:
9431 -- package Parent_Inst is new ...
9432 -- freeze Parent_Inst []
9434 -- procedure P ... -- this body freezes Parent_Inst
9436 -- procedure Inst is new ...
9438 -- In this particular scenario, the freeze node for Inst must be
9439 -- inserted in the same manner as that of Parent_Inst - before the
9440 -- next source body or at the end of the declarative list (body not
9441 -- available). If body P did not exist and Parent_Inst was frozen
9442 -- after Inst, either by a body following Inst or at the end of the
9443 -- declarative region, the freeze node for Inst must be inserted
9444 -- after that of Parent_Inst. This relation is established by
9445 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9447 elsif In_Same_List (Freeze_Node (Par_Id), N)
9448 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9449 then
9450 Insert_Freeze_Node_For_Instance (N, F_Node);
9452 else
9453 Insert_After (Freeze_Node (Par_Id), F_Node);
9454 end if;
9456 -- The body enclosing the instance should be frozen after the body that
9457 -- includes the generic, because the body of the instance may make
9458 -- references to entities therein. If the two are not in the same
9459 -- declarative part, or if the one enclosing the instance is frozen
9460 -- already, freeze the instance at the end of the current declarative
9461 -- part.
9463 elsif Is_Generic_Instance (Par_Id)
9464 and then Present (Freeze_Node (Par_Id))
9465 and then Present (Enc_N)
9466 then
9467 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9468 then
9469 -- The enclosing package may contain several instances. Rather
9470 -- than computing the earliest point at which to insert its freeze
9471 -- node, we place it at the end of the declarative part of the
9472 -- parent of the generic.
9474 Insert_Freeze_Node_For_Instance
9475 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9476 end if;
9478 Insert_Freeze_Node_For_Instance (N, F_Node);
9480 elsif Present (Enc_G)
9481 and then Present (Enc_N)
9482 and then Enc_G /= Enc_N
9483 and then Earlier (N, Gen_Body)
9484 then
9485 -- Freeze package that encloses instance, and place node after the
9486 -- package that encloses generic. If enclosing package is already
9487 -- frozen we have to assume it is at the proper place. This may be a
9488 -- potential ABE that requires dynamic checking. Do not add a freeze
9489 -- node if the package that encloses the generic is inside the body
9490 -- that encloses the instance, because the freeze node would be in
9491 -- the wrong scope. Additional contortions needed if the bodies are
9492 -- within a subunit.
9494 declare
9495 Enclosing_Body : Node_Id;
9497 begin
9498 if Nkind (Enc_N) = N_Package_Body_Stub then
9499 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9500 else
9501 Enclosing_Body := Enc_N;
9502 end if;
9504 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9505 Insert_Freeze_Node_For_Instance
9506 (Enc_G, Package_Freeze_Node (Enc_N));
9507 end if;
9508 end;
9510 -- Freeze enclosing subunit before instance
9512 Enc_G_F := Package_Freeze_Node (Enc_G);
9514 if not Is_List_Member (Enc_G_F) then
9515 Insert_After (Enc_G, Enc_G_F);
9516 end if;
9518 Insert_Freeze_Node_For_Instance (N, F_Node);
9520 else
9521 -- If none of the above, insert freeze node at the end of the current
9522 -- declarative part.
9524 Insert_Freeze_Node_For_Instance (N, F_Node);
9525 end if;
9526 end Freeze_Subprogram_Instance;
9528 ----------------
9529 -- Get_Gen_Id --
9530 ----------------
9532 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9533 begin
9534 return Generic_Renamings.Table (E).Gen_Id;
9535 end Get_Gen_Id;
9537 ---------------------
9538 -- Get_Instance_Of --
9539 ---------------------
9541 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9542 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9544 begin
9545 if Res /= Assoc_Null then
9546 return Generic_Renamings.Table (Res).Act_Id;
9548 else
9549 -- On exit, entity is not instantiated: not a generic parameter, or
9550 -- else parameter of an inner generic unit.
9552 return A;
9553 end if;
9554 end Get_Instance_Of;
9556 ---------------------------------
9557 -- Get_Unit_Instantiation_Node --
9558 ---------------------------------
9560 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9561 Decl : Node_Id := Unit_Declaration_Node (A);
9562 Inst : Node_Id;
9564 begin
9565 -- If the Package_Instantiation attribute has been set on the package
9566 -- entity, then use it directly when it (or its Original_Node) refers
9567 -- to an N_Package_Instantiation node. In principle it should be
9568 -- possible to have this field set in all cases, which should be
9569 -- investigated, and would allow this function to be significantly
9570 -- simplified. ???
9572 Inst := Package_Instantiation (A);
9574 if Present (Inst) then
9575 if Nkind (Inst) = N_Package_Instantiation then
9576 return Inst;
9578 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9579 return Original_Node (Inst);
9580 end if;
9581 end if;
9583 -- If the instantiation is a compilation unit that does not need body
9584 -- then the instantiation node has been rewritten as a package
9585 -- declaration for the instance, and we return the original node.
9587 -- If it is a compilation unit and the instance node has not been
9588 -- rewritten, then it is still the unit of the compilation. Finally, if
9589 -- a body is present, this is a parent of the main unit whose body has
9590 -- been compiled for inlining purposes, and the instantiation node has
9591 -- been rewritten with the instance body.
9593 -- Otherwise the instantiation node appears after the declaration. If
9594 -- the entity is a formal package, the declaration may have been
9595 -- rewritten as a generic declaration (in the case of a formal with box)
9596 -- or left as a formal package declaration if it has actuals, and is
9597 -- found with a forward search.
9599 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9600 if Nkind (Decl) = N_Package_Declaration
9601 and then Present (Corresponding_Body (Decl))
9602 then
9603 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9604 end if;
9606 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9607 return Original_Node (Decl);
9608 else
9609 return Unit (Parent (Decl));
9610 end if;
9612 elsif Nkind (Decl) = N_Package_Declaration
9613 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9614 then
9615 return Original_Node (Decl);
9617 else
9618 Inst := Next (Decl);
9619 while Nkind (Inst) not in N_Formal_Package_Declaration
9620 | N_Function_Instantiation
9621 | N_Package_Instantiation
9622 | N_Procedure_Instantiation
9623 loop
9624 Next (Inst);
9625 end loop;
9627 return Inst;
9628 end if;
9629 end Get_Unit_Instantiation_Node;
9631 ------------------------
9632 -- Has_Been_Exchanged --
9633 ------------------------
9635 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9636 Next : Elmt_Id;
9638 begin
9639 Next := First_Elmt (Exchanged_Views);
9640 while Present (Next) loop
9641 if Full_View (Node (Next)) = E then
9642 return True;
9643 end if;
9645 Next_Elmt (Next);
9646 end loop;
9648 return False;
9649 end Has_Been_Exchanged;
9651 -------------------
9652 -- Has_Contracts --
9653 -------------------
9655 function Has_Contracts (Decl : Node_Id) return Boolean is
9656 A_List : constant List_Id := Aspect_Specifications (Decl);
9657 A_Spec : Node_Id;
9658 A_Id : Aspect_Id;
9659 begin
9660 if No (A_List) then
9661 return False;
9662 else
9663 A_Spec := First (A_List);
9664 while Present (A_Spec) loop
9665 A_Id := Get_Aspect_Id (A_Spec);
9666 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9667 return True;
9668 end if;
9670 Next (A_Spec);
9671 end loop;
9673 return False;
9674 end if;
9675 end Has_Contracts;
9677 ----------
9678 -- Hash --
9679 ----------
9681 function Hash (F : Entity_Id) return HTable_Range is
9682 begin
9683 return HTable_Range (F mod HTable_Size);
9684 end Hash;
9686 ------------------------
9687 -- Hide_Current_Scope --
9688 ------------------------
9690 procedure Hide_Current_Scope is
9691 C : constant Entity_Id := Current_Scope;
9692 E : Entity_Id;
9694 begin
9695 Set_Is_Hidden_Open_Scope (C);
9697 E := First_Entity (C);
9698 while Present (E) loop
9699 if Is_Immediately_Visible (E) then
9700 Set_Is_Immediately_Visible (E, False);
9701 Append_Elmt (E, Hidden_Entities);
9702 end if;
9704 Next_Entity (E);
9705 end loop;
9707 -- Make the scope name invisible as well. This is necessary, but might
9708 -- conflict with calls to Rtsfind later on, in case the scope is a
9709 -- predefined one. There is no clean solution to this problem, so for
9710 -- now we depend on the user not redefining Standard itself in one of
9711 -- the parent units.
9713 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9714 Set_Is_Immediately_Visible (C, False);
9715 Append_Elmt (C, Hidden_Entities);
9716 end if;
9718 end Hide_Current_Scope;
9720 --------------
9721 -- Init_Env --
9722 --------------
9724 procedure Init_Env is
9725 Saved : Instance_Env;
9727 begin
9728 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9729 Saved.Exchanged_Views := Exchanged_Views;
9730 Saved.Hidden_Entities := Hidden_Entities;
9731 Saved.Current_Sem_Unit := Current_Sem_Unit;
9732 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9733 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9735 -- Save configuration switches. These may be reset if the unit is a
9736 -- predefined unit, and the current mode is not Ada 2005.
9738 Saved.Switches := Save_Config_Switches;
9740 Instance_Envs.Append (Saved);
9742 Exchanged_Views := New_Elmt_List;
9743 Hidden_Entities := New_Elmt_List;
9745 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9746 -- this is set properly in Set_Instance_Env.
9748 Current_Instantiated_Parent :=
9749 (Current_Scope, Current_Scope, Assoc_Null);
9750 end Init_Env;
9752 ---------------------
9753 -- In_Main_Context --
9754 ---------------------
9756 function In_Main_Context (E : Entity_Id) return Boolean is
9757 Context : List_Id;
9758 Clause : Node_Id;
9759 Nam : Node_Id;
9761 begin
9762 if not Is_Compilation_Unit (E)
9763 or else Ekind (E) /= E_Package
9764 or else In_Private_Part (E)
9765 then
9766 return False;
9767 end if;
9769 Context := Context_Items (Cunit (Main_Unit));
9771 Clause := First (Context);
9772 while Present (Clause) loop
9773 if Nkind (Clause) = N_With_Clause then
9774 Nam := Name (Clause);
9776 -- If the current scope is part of the context of the main unit,
9777 -- analysis of the corresponding with_clause is not complete, and
9778 -- the entity is not set. We use the Chars field directly, which
9779 -- might produce false positives in rare cases, but guarantees
9780 -- that we produce all the instance bodies we will need.
9782 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9783 or else (Nkind (Nam) = N_Selected_Component
9784 and then Chars (Selector_Name (Nam)) = Chars (E))
9785 then
9786 return True;
9787 end if;
9788 end if;
9790 Next (Clause);
9791 end loop;
9793 return False;
9794 end In_Main_Context;
9796 ---------------------
9797 -- Inherit_Context --
9798 ---------------------
9800 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9801 Current_Context : List_Id;
9802 Current_Unit : Node_Id;
9803 Item : Node_Id;
9804 New_I : Node_Id;
9806 Clause : Node_Id;
9807 OK : Boolean;
9808 Lib_Unit : Node_Id;
9810 begin
9811 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9813 -- The inherited context is attached to the enclosing compilation
9814 -- unit. This is either the main unit, or the declaration for the
9815 -- main unit (in case the instantiation appears within the package
9816 -- declaration and the main unit is its body).
9818 Current_Unit := Parent (Inst);
9819 while Present (Current_Unit)
9820 and then Nkind (Current_Unit) /= N_Compilation_Unit
9821 loop
9822 Current_Unit := Parent (Current_Unit);
9823 end loop;
9825 Current_Context := Context_Items (Current_Unit);
9827 Item := First (Context_Items (Parent (Gen_Decl)));
9828 while Present (Item) loop
9829 if Nkind (Item) = N_With_Clause then
9830 Lib_Unit := Library_Unit (Item);
9832 -- Take care to prevent direct cyclic with's
9834 if Lib_Unit /= Current_Unit then
9836 -- Do not add a unit if it is already in the context
9838 Clause := First (Current_Context);
9839 OK := True;
9840 while Present (Clause) loop
9841 if Nkind (Clause) = N_With_Clause
9842 and then Library_Unit (Clause) = Lib_Unit
9843 then
9844 OK := False;
9845 exit;
9846 end if;
9848 Next (Clause);
9849 end loop;
9851 if OK then
9852 New_I := New_Copy (Item);
9853 Set_Implicit_With (New_I);
9855 Append (New_I, Current_Context);
9856 end if;
9857 end if;
9858 end if;
9860 Next (Item);
9861 end loop;
9862 end if;
9863 end Inherit_Context;
9865 ----------------
9866 -- Initialize --
9867 ----------------
9869 procedure Initialize is
9870 begin
9871 Generic_Renamings.Init;
9872 Instance_Envs.Init;
9873 Generic_Flags.Init;
9874 Generic_Renamings_HTable.Reset;
9875 Circularity_Detected := False;
9876 Exchanged_Views := No_Elist;
9877 Hidden_Entities := No_Elist;
9878 end Initialize;
9880 -------------------------------------
9881 -- Insert_Freeze_Node_For_Instance --
9882 -------------------------------------
9884 procedure Insert_Freeze_Node_For_Instance
9885 (N : Node_Id;
9886 F_Node : Node_Id)
9888 function Enclosing_Body (N : Node_Id) return Node_Id;
9889 -- Find enclosing package or subprogram body, if any. Freeze node may
9890 -- be placed at end of current declarative list if previous instance
9891 -- and current one have different enclosing bodies.
9893 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9894 -- Find the local instance, if any, that declares the generic that is
9895 -- being instantiated. If present, the freeze node for this instance
9896 -- must follow the freeze node for the previous instance.
9898 --------------------
9899 -- Enclosing_Body --
9900 --------------------
9902 function Enclosing_Body (N : Node_Id) return Node_Id is
9903 P : Node_Id;
9905 begin
9906 P := Parent (N);
9907 while Present (P)
9908 and then Nkind (Parent (P)) /= N_Compilation_Unit
9909 loop
9910 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9911 if Nkind (Parent (P)) = N_Subunit then
9912 return Corresponding_Stub (Parent (P));
9913 else
9914 return P;
9915 end if;
9916 end if;
9918 P := True_Parent (P);
9919 end loop;
9921 return Empty;
9922 end Enclosing_Body;
9924 -----------------------
9925 -- Previous_Instance --
9926 -----------------------
9928 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9929 S : Entity_Id;
9931 begin
9932 S := Scope (Gen);
9933 while Present (S) and then S /= Standard_Standard loop
9934 if Is_Generic_Instance (S)
9935 and then In_Same_Source_Unit (S, N)
9936 then
9937 return S;
9938 end if;
9940 S := Scope (S);
9941 end loop;
9943 return Empty;
9944 end Previous_Instance;
9946 -- Local variables
9948 Decl : Node_Id;
9949 Decls : List_Id;
9950 Inst : Entity_Id;
9951 Origin : Entity_Id;
9952 Par_Inst : Node_Id;
9953 Par_N : Node_Id;
9955 -- Start of processing for Insert_Freeze_Node_For_Instance
9957 begin
9958 -- Nothing to do if the freeze node has already been inserted
9960 if Is_List_Member (F_Node) then
9961 return;
9962 end if;
9964 Inst := Entity (F_Node);
9966 -- When processing a subprogram instantiation, utilize the actual
9967 -- subprogram instantiation rather than its package wrapper as it
9968 -- carries all the context information.
9970 if Is_Wrapper_Package (Inst) then
9971 Inst := Related_Instance (Inst);
9972 end if;
9974 Par_Inst := Parent (Inst);
9976 -- If this is a package instance, check whether the generic is declared
9977 -- in a previous instance and the current instance is not within the
9978 -- previous one.
9980 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9981 declare
9982 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9983 Par_I : constant Entity_Id :=
9984 Previous_Instance (Generic_Parent (Par_Inst));
9985 Scop : Entity_Id;
9987 begin
9988 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9989 Scop := Scope (Inst);
9991 -- If the current instance is within the one that contains
9992 -- the generic, the freeze node for the current one must
9993 -- appear in the current declarative part. Ditto, if the
9994 -- current instance is within another package instance or
9995 -- within a body that does not enclose the current instance.
9996 -- In these three cases the freeze node of the previous
9997 -- instance is not relevant.
9999 while Present (Scop) and then Scop /= Standard_Standard loop
10000 exit when Scop = Par_I
10001 or else
10002 (Is_Generic_Instance (Scop)
10003 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
10004 Scop := Scope (Scop);
10005 end loop;
10007 -- Previous instance encloses current instance
10009 if Scop = Par_I then
10010 null;
10012 -- If the next node is a source body we must freeze in the
10013 -- current scope as well.
10015 elsif Present (Next (N))
10016 and then Nkind (Next (N)) in N_Subprogram_Body
10017 | N_Package_Body
10018 and then Comes_From_Source (Next (N))
10019 then
10020 null;
10022 -- Current instance is within an unrelated instance
10024 elsif Is_Generic_Instance (Scop) then
10025 null;
10027 -- Current instance is within an unrelated body
10029 elsif Present (Enclosing_N)
10030 and then Enclosing_N /= Enclosing_Body (Par_I)
10031 then
10032 null;
10034 else
10035 Insert_After (Freeze_Node (Par_I), F_Node);
10036 return;
10037 end if;
10038 end if;
10039 end;
10040 end if;
10042 Decl := N;
10043 Decls := List_Containing (N);
10044 Par_N := Parent (Decls);
10045 Origin := Empty;
10047 -- Determine the proper freeze point of an instantiation
10049 if Is_Generic_Instance (Inst) then
10050 loop
10051 -- When the instantiation occurs in a package spec, append the
10052 -- freeze node to the private declarations (if any).
10054 if Nkind (Par_N) = N_Package_Specification
10055 and then Decls = Visible_Declarations (Par_N)
10056 and then not Is_Empty_List (Private_Declarations (Par_N))
10057 then
10058 Decls := Private_Declarations (Par_N);
10059 Decl := First (Decls);
10060 end if;
10062 -- We adhere to the general rule of a package or subprogram body
10063 -- causing freezing of anything before it in the same declarative
10064 -- region. In this respect, the proper freeze point of a package
10065 -- instantiation is before the first source body which follows, or
10066 -- before a stub. This ensures that entities from the instance are
10067 -- already frozen and therefore usable in source bodies.
10069 if Nkind (Par_N) /= N_Package_Declaration
10070 and then
10071 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10072 then
10073 while Present (Decl) loop
10074 if ((Nkind (Decl) in N_Unit_Body
10075 or else
10076 Nkind (Decl) in N_Body_Stub)
10077 and then Comes_From_Source (Decl))
10078 or else (Present (Origin)
10079 and then Nkind (Decl) in N_Generic_Instantiation
10080 and then Instance_Spec (Decl) /= Origin)
10081 then
10082 Set_Sloc (F_Node, Sloc (Decl));
10083 Insert_Before (Decl, F_Node);
10084 return;
10085 end if;
10087 Next (Decl);
10088 end loop;
10089 end if;
10091 -- When the instantiation occurs in a package spec and there is
10092 -- no source body which follows, and the package has a body but
10093 -- is delayed, then insert immediately before its freeze node.
10095 if Nkind (Par_N) = N_Package_Specification
10096 and then Present (Corresponding_Body (Parent (Par_N)))
10097 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10098 then
10099 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10100 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10101 return;
10103 -- When the instantiation occurs in a package spec and there is
10104 -- no source body which follows, not even of the package itself,
10105 -- then insert into the declaration list of the outer level, but
10106 -- do not jump over following instantiations in this list because
10107 -- they may have a body that has not materialized yet, see above.
10109 elsif Nkind (Par_N) = N_Package_Specification
10110 and then No (Corresponding_Body (Parent (Par_N)))
10111 and then Is_List_Member (Parent (Par_N))
10112 then
10113 Decl := Parent (Par_N);
10114 Decls := List_Containing (Decl);
10115 Par_N := Parent (Decls);
10116 Origin := Decl;
10118 -- In a package declaration, or if no source body which follows
10119 -- and at library level, then insert at end of list.
10121 else
10122 exit;
10123 end if;
10124 end loop;
10125 end if;
10127 -- Insert and adjust the Sloc of the freeze node
10129 Set_Sloc (F_Node, Sloc (Last (Decls)));
10130 Insert_After (Last (Decls), F_Node);
10131 end Insert_Freeze_Node_For_Instance;
10133 -----------------------------
10134 -- Install_Formal_Packages --
10135 -----------------------------
10137 procedure Install_Formal_Packages (Par : Entity_Id) is
10138 E : Entity_Id;
10139 Gen : Entity_Id;
10140 Gen_E : Entity_Id := Empty;
10142 begin
10143 E := First_Entity (Par);
10145 -- If we are installing an instance parent, locate the formal packages
10146 -- of its generic parent.
10148 if Is_Generic_Instance (Par) then
10149 Gen := Generic_Parent (Package_Specification (Par));
10150 Gen_E := First_Entity (Gen);
10151 end if;
10153 while Present (E) loop
10154 if Ekind (E) = E_Package
10155 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10156 then
10157 -- If this is the renaming for the parent instance, done
10159 if Renamed_Entity (E) = Par then
10160 exit;
10162 -- The visibility of a formal of an enclosing generic is already
10163 -- correct.
10165 elsif Denotes_Formal_Package (E) then
10166 null;
10168 elsif Present (Associated_Formal_Package (E)) then
10169 Check_Generic_Actuals (Renamed_Entity (E), True);
10170 Set_Is_Hidden (E, False);
10172 -- Find formal package in generic unit that corresponds to
10173 -- (instance of) formal package in instance.
10175 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10176 Next_Entity (Gen_E);
10177 end loop;
10179 if Present (Gen_E) then
10180 Map_Formal_Package_Entities (Gen_E, E);
10181 end if;
10182 end if;
10183 end if;
10185 Next_Entity (E);
10187 if Present (Gen_E) then
10188 Next_Entity (Gen_E);
10189 end if;
10190 end loop;
10191 end Install_Formal_Packages;
10193 --------------------
10194 -- Install_Parent --
10195 --------------------
10197 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10198 Ancestors : constant Elist_Id := New_Elmt_List;
10199 S : constant Entity_Id := Current_Scope;
10200 Inst_Par : Entity_Id;
10201 First_Par : Entity_Id;
10202 Inst_Node : Node_Id;
10203 Gen_Par : Entity_Id;
10204 First_Gen : Entity_Id;
10205 Elmt : Elmt_Id;
10207 procedure Install_Noninstance_Specs (Par : Entity_Id);
10208 -- Install the scopes of noninstance parent units ending with Par
10210 procedure Install_Spec (Par : Entity_Id);
10211 -- The child unit is within the declarative part of the parent, so the
10212 -- declarations within the parent are immediately visible.
10214 -------------------------------
10215 -- Install_Noninstance_Specs --
10216 -------------------------------
10218 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10219 begin
10220 if Present (Par)
10221 and then Par /= Standard_Standard
10222 and then not In_Open_Scopes (Par)
10223 then
10224 Install_Noninstance_Specs (Scope (Par));
10225 Install_Spec (Par);
10226 end if;
10227 end Install_Noninstance_Specs;
10229 ------------------
10230 -- Install_Spec --
10231 ------------------
10233 procedure Install_Spec (Par : Entity_Id) is
10234 Spec : constant Node_Id := Package_Specification (Par);
10236 begin
10237 -- If this parent of the child instance is a top-level unit,
10238 -- then record the unit and its visibility for later resetting in
10239 -- Remove_Parent. We exclude units that are generic instances, as we
10240 -- only want to record this information for the ultimate top-level
10241 -- noninstance parent (is that always correct???).
10243 if Scope (Par) = Standard_Standard
10244 and then not Is_Generic_Instance (Par)
10245 then
10246 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10247 Instance_Parent_Unit := Par;
10248 end if;
10250 -- Open the parent scope and make it and its declarations visible.
10251 -- If this point is not within a body, then only the visible
10252 -- declarations should be made visible, and installation of the
10253 -- private declarations is deferred until the appropriate point
10254 -- within analysis of the spec being instantiated (see the handling
10255 -- of parent visibility in Analyze_Package_Specification). This is
10256 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10257 -- private view problems that occur when compiling instantiations of
10258 -- a generic child of that package (Generic_Dispatching_Constructor).
10259 -- If the instance freezes a tagged type, inlinings of operations
10260 -- from Ada.Tags may need the full view of type Tag. If inlining took
10261 -- proper account of establishing visibility of inlined subprograms'
10262 -- parents then it should be possible to remove this
10263 -- special check. ???
10265 Push_Scope (Par);
10266 Set_Is_Immediately_Visible (Par);
10267 Install_Visible_Declarations (Par);
10268 Set_Use (Visible_Declarations (Spec));
10270 if In_Body or else Is_RTU (Par, Ada_Tags) then
10271 Install_Private_Declarations (Par);
10272 Set_Use (Private_Declarations (Spec));
10273 end if;
10274 end Install_Spec;
10276 -- Start of processing for Install_Parent
10278 begin
10279 -- We need to install the parent instance to compile the instantiation
10280 -- of the child, but the child instance must appear in the current
10281 -- scope. Given that we cannot place the parent above the current scope
10282 -- in the scope stack, we duplicate the current scope and unstack both
10283 -- after the instantiation is complete.
10285 -- If the parent is itself the instantiation of a child unit, we must
10286 -- also stack the instantiation of its parent, and so on. Each such
10287 -- ancestor is the prefix of the name in a prior instantiation.
10289 -- If this is a nested instance, the parent unit itself resolves to
10290 -- a renaming of the parent instance, whose declaration we need.
10292 -- Finally, the parent may be a generic (not an instance) when the
10293 -- child unit appears as a formal package.
10295 Inst_Par := P;
10297 if Present (Renamed_Entity (Inst_Par)) then
10298 Inst_Par := Renamed_Entity (Inst_Par);
10299 end if;
10301 First_Par := Inst_Par;
10303 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10305 First_Gen := Gen_Par;
10307 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10309 -- Load grandparent instance as well
10311 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10313 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10314 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10316 if Present (Renamed_Entity (Inst_Par)) then
10317 Inst_Par := Renamed_Entity (Inst_Par);
10318 end if;
10320 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10322 if Present (Gen_Par) then
10323 Prepend_Elmt (Inst_Par, Ancestors);
10325 else
10326 -- Parent is not the name of an instantiation
10328 Install_Noninstance_Specs (Inst_Par);
10329 exit;
10330 end if;
10332 else
10333 -- Previous error
10335 exit;
10336 end if;
10337 end loop;
10339 if Present (First_Gen) then
10340 Append_Elmt (First_Par, Ancestors);
10341 else
10342 Install_Noninstance_Specs (First_Par);
10343 end if;
10345 if not Is_Empty_Elmt_List (Ancestors) then
10346 Elmt := First_Elmt (Ancestors);
10347 while Present (Elmt) loop
10348 Install_Spec (Node (Elmt));
10349 Install_Formal_Packages (Node (Elmt));
10350 Next_Elmt (Elmt);
10351 end loop;
10352 end if;
10354 if not In_Body then
10355 Push_Scope (S);
10356 end if;
10357 end Install_Parent;
10359 -------------------------------
10360 -- Install_Hidden_Primitives --
10361 -------------------------------
10363 procedure Install_Hidden_Primitives
10364 (Prims_List : in out Elist_Id;
10365 Gen_T : Entity_Id;
10366 Act_T : Entity_Id)
10368 Elmt : Elmt_Id;
10369 List : Elist_Id := No_Elist;
10370 Prim_G_Elmt : Elmt_Id;
10371 Prim_A_Elmt : Elmt_Id;
10372 Prim_G : Node_Id;
10373 Prim_A : Node_Id;
10375 begin
10376 -- No action needed in case of serious errors because we cannot trust
10377 -- in the order of primitives
10379 if Serious_Errors_Detected > 0 then
10380 return;
10382 -- No action possible if we don't have available the list of primitive
10383 -- operations
10385 elsif No (Gen_T)
10386 or else not Is_Record_Type (Gen_T)
10387 or else not Is_Tagged_Type (Gen_T)
10388 or else not Is_Record_Type (Act_T)
10389 or else not Is_Tagged_Type (Act_T)
10390 then
10391 return;
10393 -- There is no need to handle interface types since their primitives
10394 -- cannot be hidden
10396 elsif Is_Interface (Gen_T) then
10397 return;
10398 end if;
10400 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10402 if not Is_Class_Wide_Type (Act_T) then
10403 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10404 else
10405 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10406 end if;
10408 loop
10409 -- Skip predefined primitives in the generic formal
10411 while Present (Prim_G_Elmt)
10412 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10413 loop
10414 Next_Elmt (Prim_G_Elmt);
10415 end loop;
10417 -- Skip predefined primitives in the generic actual
10419 while Present (Prim_A_Elmt)
10420 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10421 loop
10422 Next_Elmt (Prim_A_Elmt);
10423 end loop;
10425 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10427 Prim_G := Node (Prim_G_Elmt);
10428 Prim_A := Node (Prim_A_Elmt);
10430 -- There is no need to handle interface primitives because their
10431 -- primitives are not hidden
10433 exit when Present (Interface_Alias (Prim_G));
10435 -- Here we install one hidden primitive
10437 if Chars (Prim_G) /= Chars (Prim_A)
10438 and then Has_Suffix (Prim_A, 'P')
10439 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10440 then
10441 Set_Chars (Prim_A, Chars (Prim_G));
10442 Append_New_Elmt (Prim_A, To => List);
10443 end if;
10445 Next_Elmt (Prim_A_Elmt);
10446 Next_Elmt (Prim_G_Elmt);
10447 end loop;
10449 -- Append the elements to the list of temporarily visible primitives
10450 -- avoiding duplicates.
10452 if Present (List) then
10453 if No (Prims_List) then
10454 Prims_List := New_Elmt_List;
10455 end if;
10457 Elmt := First_Elmt (List);
10458 while Present (Elmt) loop
10459 Append_Unique_Elmt (Node (Elmt), Prims_List);
10460 Next_Elmt (Elmt);
10461 end loop;
10462 end if;
10463 end Install_Hidden_Primitives;
10465 -------------------------------
10466 -- Restore_Hidden_Primitives --
10467 -------------------------------
10469 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10470 Prim_Elmt : Elmt_Id;
10471 Prim : Node_Id;
10473 begin
10474 if Present (Prims_List) then
10475 Prim_Elmt := First_Elmt (Prims_List);
10476 while Present (Prim_Elmt) loop
10477 Prim := Node (Prim_Elmt);
10478 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10479 Next_Elmt (Prim_Elmt);
10480 end loop;
10482 Prims_List := No_Elist;
10483 end if;
10484 end Restore_Hidden_Primitives;
10486 --------------------------------
10487 -- Instantiate_Formal_Package --
10488 --------------------------------
10490 function Instantiate_Formal_Package
10491 (Formal : Node_Id;
10492 Actual : Node_Id;
10493 Analyzed_Formal : Node_Id) return List_Id
10495 Loc : constant Source_Ptr := Sloc (Actual);
10496 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10498 Actual_Pack : Entity_Id;
10499 Formal_Pack : Entity_Id;
10500 Gen_Parent : Entity_Id;
10501 Decls : List_Id;
10502 Nod : Node_Id;
10503 Parent_Spec : Node_Id;
10505 procedure Find_Matching_Actual
10506 (F : Node_Id;
10507 Act : in out Entity_Id);
10508 -- We need to associate each formal entity in the formal package with
10509 -- the corresponding entity in the actual package. The actual package
10510 -- has been analyzed and possibly expanded, and as a result there is
10511 -- no one-to-one correspondence between the two lists (for example,
10512 -- the actual may include subtypes, itypes, and inherited primitive
10513 -- operations, interspersed among the renaming declarations for the
10514 -- actuals). We retrieve the corresponding actual by name because each
10515 -- actual has the same name as the formal, and they do appear in the
10516 -- same order.
10518 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10519 -- Retrieve entity of defining entity of generic formal parameter.
10520 -- Only the declarations of formals need to be considered when
10521 -- linking them to actuals, but the declarative list may include
10522 -- internal entities generated during analysis, and those are ignored.
10524 procedure Match_Formal_Entity
10525 (Formal_Node : Node_Id;
10526 Formal_Ent : Entity_Id;
10527 Actual_Ent : Entity_Id);
10528 -- Associates the formal entity with the actual. In the case where
10529 -- Formal_Ent is a formal package, this procedure iterates through all
10530 -- of its formals and enters associations between the actuals occurring
10531 -- in the formal package's corresponding actual package (given by
10532 -- Actual_Ent) and the formal package's formal parameters. This
10533 -- procedure recurses if any of the parameters is itself a package.
10535 function Is_Instance_Of
10536 (Act_Spec : Entity_Id;
10537 Gen_Anc : Entity_Id) return Boolean;
10538 -- The actual can be an instantiation of a generic within another
10539 -- instance, in which case there is no direct link from it to the
10540 -- original generic ancestor. In that case, we recognize that the
10541 -- ultimate ancestor is the same by examining names and scopes.
10543 procedure Process_Nested_Formal (Formal : Entity_Id);
10544 -- If the current formal is declared with a box, its own formals are
10545 -- visible in the instance, as they were in the generic, and their
10546 -- Hidden flag must be reset. If some of these formals are themselves
10547 -- packages declared with a box, the processing must be recursive.
10549 --------------------------
10550 -- Find_Matching_Actual --
10551 --------------------------
10553 procedure Find_Matching_Actual
10554 (F : Node_Id;
10555 Act : in out Entity_Id)
10557 Formal_Ent : Entity_Id;
10559 begin
10560 case Nkind (Original_Node (F)) is
10561 when N_Formal_Object_Declaration
10562 | N_Formal_Type_Declaration
10564 Formal_Ent := Defining_Identifier (F);
10566 while Present (Act)
10567 and then Chars (Act) /= Chars (Formal_Ent)
10568 loop
10569 Next_Entity (Act);
10570 end loop;
10572 when N_Formal_Package_Declaration
10573 | N_Formal_Subprogram_Declaration
10574 | N_Generic_Package_Declaration
10575 | N_Package_Declaration
10577 Formal_Ent := Defining_Entity (F);
10579 while Present (Act)
10580 and then Chars (Act) /= Chars (Formal_Ent)
10581 loop
10582 Next_Entity (Act);
10583 end loop;
10585 when others =>
10586 raise Program_Error;
10587 end case;
10588 end Find_Matching_Actual;
10590 -------------------------
10591 -- Match_Formal_Entity --
10592 -------------------------
10594 procedure Match_Formal_Entity
10595 (Formal_Node : Node_Id;
10596 Formal_Ent : Entity_Id;
10597 Actual_Ent : Entity_Id)
10599 Act_Pkg : Entity_Id;
10601 begin
10602 Set_Instance_Of (Formal_Ent, Actual_Ent);
10604 if Ekind (Actual_Ent) = E_Package then
10606 -- Record associations for each parameter
10608 Act_Pkg := Actual_Ent;
10610 declare
10611 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10612 F_Ent : Entity_Id;
10613 F_Node : Node_Id;
10615 Gen_Decl : Node_Id;
10616 Formals : List_Id;
10617 Actual : Entity_Id;
10619 begin
10620 -- Retrieve the actual given in the formal package declaration
10622 Actual := Entity (Name (Original_Node (Formal_Node)));
10624 -- The actual in the formal package declaration may be a
10625 -- renamed generic package, in which case we want to retrieve
10626 -- the original generic in order to traverse its formal part.
10628 if Present (Renamed_Entity (Actual)) then
10629 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10630 else
10631 Gen_Decl := Unit_Declaration_Node (Actual);
10632 end if;
10634 Formals := Generic_Formal_Declarations (Gen_Decl);
10636 if Present (Formals) then
10637 F_Node := First_Non_Pragma (Formals);
10638 else
10639 F_Node := Empty;
10640 end if;
10642 while Present (A_Ent)
10643 and then Present (F_Node)
10644 and then A_Ent /= First_Private_Entity (Act_Pkg)
10645 loop
10646 F_Ent := Get_Formal_Entity (F_Node);
10648 if Present (F_Ent) then
10650 -- This is a formal of the original package. Record
10651 -- association and recurse.
10653 Find_Matching_Actual (F_Node, A_Ent);
10654 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10655 Next_Entity (A_Ent);
10656 end if;
10658 Next_Non_Pragma (F_Node);
10659 end loop;
10660 end;
10661 end if;
10662 end Match_Formal_Entity;
10664 -----------------------
10665 -- Get_Formal_Entity --
10666 -----------------------
10668 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10669 Kind : constant Node_Kind := Nkind (Original_Node (N));
10670 begin
10671 case Kind is
10672 when N_Formal_Object_Declaration =>
10673 return Defining_Identifier (N);
10675 when N_Formal_Type_Declaration =>
10676 return Defining_Identifier (N);
10678 when N_Formal_Subprogram_Declaration =>
10679 return Defining_Unit_Name (Specification (N));
10681 when N_Formal_Package_Declaration =>
10682 return Defining_Identifier (Original_Node (N));
10684 when N_Generic_Package_Declaration =>
10685 return Defining_Identifier (Original_Node (N));
10687 -- All other declarations are introduced by semantic analysis and
10688 -- have no match in the actual.
10690 when others =>
10691 return Empty;
10692 end case;
10693 end Get_Formal_Entity;
10695 --------------------
10696 -- Is_Instance_Of --
10697 --------------------
10699 function Is_Instance_Of
10700 (Act_Spec : Entity_Id;
10701 Gen_Anc : Entity_Id) return Boolean
10703 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10705 begin
10706 if No (Gen_Par) then
10707 return False;
10709 -- Simplest case: the generic parent of the actual is the formal
10711 elsif Gen_Par = Gen_Anc then
10712 return True;
10714 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10715 return False;
10717 -- The actual may be obtained through several instantiations. Its
10718 -- scope must itself be an instance of a generic declared in the
10719 -- same scope as the formal. Any other case is detected above.
10721 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10722 return False;
10724 else
10725 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10726 end if;
10727 end Is_Instance_Of;
10729 ---------------------------
10730 -- Process_Nested_Formal --
10731 ---------------------------
10733 procedure Process_Nested_Formal (Formal : Entity_Id) is
10734 Ent : Entity_Id;
10736 begin
10737 if Present (Associated_Formal_Package (Formal))
10738 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10739 then
10740 Ent := First_Entity (Formal);
10741 while Present (Ent) loop
10742 Set_Is_Hidden (Ent, False);
10743 Set_Is_Visible_Formal (Ent);
10744 Set_Is_Potentially_Use_Visible
10745 (Ent, Is_Potentially_Use_Visible (Formal));
10747 if Ekind (Ent) = E_Package then
10748 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10749 Process_Nested_Formal (Ent);
10750 end if;
10752 Next_Entity (Ent);
10753 end loop;
10754 end if;
10755 end Process_Nested_Formal;
10757 -- Start of processing for Instantiate_Formal_Package
10759 begin
10760 Analyze (Actual);
10762 -- The actual must be a package instance, or else a current instance
10763 -- such as a parent generic within the body of a generic child.
10765 if not Is_Entity_Name (Actual)
10766 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10767 then
10768 Error_Msg_N
10769 ("expect package instance to instantiate formal", Actual);
10770 Abandon_Instantiation (Actual);
10772 else
10773 Actual_Pack := Entity (Actual);
10774 Set_Is_Instantiated (Actual_Pack);
10776 -- The actual may be a renamed package, or an outer generic formal
10777 -- package whose instantiation is converted into a renaming.
10779 if Present (Renamed_Entity (Actual_Pack)) then
10780 Actual_Pack := Renamed_Entity (Actual_Pack);
10781 end if;
10783 -- The analyzed formal is expected to be the result of the rewriting
10784 -- of the formal package into a regular package by analysis.
10786 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10787 and then Nkind (Original_Node (Analyzed_Formal)) =
10788 N_Formal_Package_Declaration);
10790 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10791 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10793 -- The actual for a ghost generic formal package should be a ghost
10794 -- package (SPARK RM 6.9(14)).
10796 Check_Ghost_Formal_Procedure_Or_Package
10797 (N => Actual,
10798 Actual => Actual_Pack,
10799 Formal => Formal_Pack);
10801 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10802 Parent_Spec := Package_Specification (Actual_Pack);
10803 else
10804 Parent_Spec := Parent (Actual_Pack);
10805 end if;
10807 if Gen_Parent = Any_Id then
10808 Error_Msg_N
10809 ("previous error in declaration of formal package", Actual);
10810 Abandon_Instantiation (Actual);
10812 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10813 null;
10815 -- If this is the current instance of an enclosing generic, that unit
10816 -- is the generic package we need.
10818 elsif In_Open_Scopes (Actual_Pack)
10819 and then Ekind (Actual_Pack) = E_Generic_Package
10820 then
10821 null;
10823 else
10824 Error_Msg_NE
10825 ("actual parameter must be instance of&", Actual, Gen_Parent);
10826 Abandon_Instantiation (Actual);
10827 end if;
10829 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10830 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10832 Nod :=
10833 Make_Package_Renaming_Declaration (Loc,
10834 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10835 Name => New_Occurrence_Of (Actual_Pack, Loc));
10837 Set_Associated_Formal_Package
10838 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10839 Decls := New_List (Nod);
10841 -- If the formal F has a box, then the generic declarations are
10842 -- visible in the generic G. In an instance of G, the corresponding
10843 -- entities in the actual for F (which are the actuals for the
10844 -- instantiation of the generic that F denotes) must also be made
10845 -- visible for analysis of the current instance. On exit from the
10846 -- current instance, those entities are made private again. If the
10847 -- actual is currently in use, these entities are also use-visible.
10849 -- The loop through the actual entities also steps through the formal
10850 -- entities and enters associations from formals to actuals into the
10851 -- renaming map. This is necessary to properly handle checking of
10852 -- actual parameter associations for later formals that depend on
10853 -- actuals declared in the formal package.
10855 -- In Ada 2005, partial parameterization requires that we make
10856 -- visible the actuals corresponding to formals that were defaulted
10857 -- in the formal package. There formals are identified because they
10858 -- remain formal generics within the formal package, rather than
10859 -- being renamings of the actuals supplied.
10861 declare
10862 Gen_Decl : constant Node_Id :=
10863 Unit_Declaration_Node (Gen_Parent);
10864 Formals : constant List_Id :=
10865 Generic_Formal_Declarations (Gen_Decl);
10867 Actual_Ent : Entity_Id;
10868 Actual_Of_Formal : Node_Id;
10869 Formal_Node : Node_Id;
10870 Formal_Ent : Entity_Id;
10872 begin
10873 if Present (Formals) then
10874 Formal_Node := First_Non_Pragma (Formals);
10875 else
10876 Formal_Node := Empty;
10877 end if;
10879 Actual_Ent := First_Entity (Actual_Pack);
10880 Actual_Of_Formal :=
10881 First (Visible_Declarations (Specification (Analyzed_Formal)));
10882 while Present (Actual_Ent)
10883 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10884 loop
10885 if Present (Formal_Node) then
10886 Formal_Ent := Get_Formal_Entity (Formal_Node);
10888 if Present (Formal_Ent) then
10889 Find_Matching_Actual (Formal_Node, Actual_Ent);
10890 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10892 -- We iterate at the same time over the actuals of the
10893 -- local package created for the formal, to determine
10894 -- which one of the formals of the original generic were
10895 -- defaulted in the formal. The corresponding actual
10896 -- entities are visible in the enclosing instance.
10898 if Box_Present (Formal)
10899 or else
10900 (Present (Actual_Of_Formal)
10901 and then
10902 Is_Generic_Formal
10903 (Get_Formal_Entity (Actual_Of_Formal)))
10904 then
10905 Set_Is_Hidden (Actual_Ent, False);
10906 Set_Is_Visible_Formal (Actual_Ent);
10907 Set_Is_Potentially_Use_Visible
10908 (Actual_Ent, In_Use (Actual_Pack));
10910 if Ekind (Actual_Ent) = E_Package then
10911 Process_Nested_Formal (Actual_Ent);
10912 end if;
10914 else
10915 if not Is_Hidden (Actual_Ent) then
10916 Append_Elmt (Actual_Ent, Hidden_Formals);
10917 end if;
10919 Set_Is_Hidden (Actual_Ent);
10920 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10921 end if;
10922 end if;
10924 Next_Non_Pragma (Formal_Node);
10925 Next (Actual_Of_Formal);
10927 -- A formal subprogram may be overloaded, so advance in
10928 -- the list of actuals to make sure we do not match two
10929 -- successive formals to the same actual. This is only
10930 -- relevant for overloadable entities, others have
10931 -- distinct names.
10933 if Is_Overloadable (Actual_Ent) then
10934 Next_Entity (Actual_Ent);
10935 end if;
10937 else
10938 -- No further formals to match, but the generic part may
10939 -- contain inherited operation that are not hidden in the
10940 -- enclosing instance.
10942 Next_Entity (Actual_Ent);
10943 end if;
10944 end loop;
10946 -- Inherited subprograms generated by formal derived types are
10947 -- also visible if the types are.
10949 Actual_Ent := First_Entity (Actual_Pack);
10950 while Present (Actual_Ent)
10951 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10952 loop
10953 if Is_Overloadable (Actual_Ent)
10954 and then
10955 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10956 and then
10957 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10958 then
10959 Set_Is_Hidden (Actual_Ent, False);
10960 Set_Is_Potentially_Use_Visible
10961 (Actual_Ent, In_Use (Actual_Pack));
10962 end if;
10964 Next_Entity (Actual_Ent);
10965 end loop;
10966 end;
10968 -- If the formal requires conformance checking, reanalyze it as an
10969 -- abbreviated instantiation, to verify the matching rules of 12.7.
10970 -- The actual checks are performed after the generic associations
10971 -- have been analyzed, to guarantee the same visibility for this
10972 -- instantiation and for the actuals.
10974 -- In Ada 2005, the generic associations for the formal can include
10975 -- defaulted parameters. These are ignored during check. This
10976 -- internal instantiation is removed from the tree after conformance
10977 -- checking, because it contains formal declarations for those
10978 -- defaulted parameters, and those should not reach the back-end.
10980 if Requires_Conformance_Checking (Formal) then
10981 declare
10982 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10984 I_Nam : Node_Id;
10986 begin
10987 Set_Is_Internal (I_Pack);
10988 Mutate_Ekind (I_Pack, E_Package);
10990 -- Insert the package into the list of its hidden entities so
10991 -- that the list is not empty for Is_Abbreviated_Instance.
10993 Append_Elmt (I_Pack, Hidden_Formals);
10995 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10997 -- If the generic is a child unit, Check_Generic_Child_Unit
10998 -- needs its original name in case it is qualified.
11000 if Is_Child_Unit (Gen_Parent) then
11001 I_Nam :=
11002 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
11003 pragma Assert (Entity (I_Nam) = Gen_Parent);
11005 else
11006 I_Nam :=
11007 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11008 end if;
11010 Append_To (Decls,
11011 Make_Package_Instantiation (Loc,
11012 Defining_Unit_Name => I_Pack,
11013 Name => I_Nam,
11014 Generic_Associations => Generic_Associations (Formal)));
11015 end;
11016 end if;
11018 return Decls;
11019 end if;
11020 end Instantiate_Formal_Package;
11022 -----------------------------------
11023 -- Instantiate_Formal_Subprogram --
11024 -----------------------------------
11026 function Instantiate_Formal_Subprogram
11027 (Formal : Node_Id;
11028 Actual : Node_Id;
11029 Analyzed_Formal : Node_Id) return Node_Id
11031 Analyzed_S : constant Entity_Id :=
11032 Defining_Unit_Name (Specification (Analyzed_Formal));
11033 Formal_Sub : constant Entity_Id :=
11034 Defining_Unit_Name (Specification (Formal));
11036 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11037 -- If the generic is a child unit, the parent has been installed on the
11038 -- scope stack, but a default subprogram cannot resolve to something
11039 -- on the parent because that parent is not really part of the visible
11040 -- context (it is there to resolve explicit local entities). If the
11041 -- default has resolved in this way, we remove the entity from immediate
11042 -- visibility and analyze the node again to emit an error message or
11043 -- find another visible candidate.
11045 procedure Valid_Actual_Subprogram (Act : Node_Id);
11046 -- Perform legality check and raise exception on failure
11048 -----------------------
11049 -- From_Parent_Scope --
11050 -----------------------
11052 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11053 Gen_Scope : Node_Id;
11055 begin
11056 Gen_Scope := Scope (Analyzed_S);
11057 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11058 if Scope (Subp) = Scope (Gen_Scope) then
11059 return True;
11060 end if;
11062 Gen_Scope := Scope (Gen_Scope);
11063 end loop;
11065 return False;
11066 end From_Parent_Scope;
11068 -----------------------------
11069 -- Valid_Actual_Subprogram --
11070 -----------------------------
11072 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11073 Act_E : Entity_Id;
11075 begin
11076 if Is_Entity_Name (Act) then
11077 Act_E := Entity (Act);
11079 elsif Nkind (Act) = N_Selected_Component
11080 and then Is_Entity_Name (Selector_Name (Act))
11081 then
11082 Act_E := Entity (Selector_Name (Act));
11084 else
11085 Act_E := Empty;
11086 end if;
11088 -- The actual for a ghost generic formal procedure should be a ghost
11089 -- procedure (SPARK RM 6.9(14)).
11091 if Present (Act_E)
11092 and then Ekind (Act_E) = E_Procedure
11093 then
11094 Check_Ghost_Formal_Procedure_Or_Package
11095 (N => Act,
11096 Actual => Act_E,
11097 Formal => Analyzed_S);
11098 end if;
11100 if (Present (Act_E) and then Is_Overloadable (Act_E))
11101 or else Nkind (Act) in N_Attribute_Reference
11102 | N_Indexed_Component
11103 | N_Character_Literal
11104 | N_Explicit_Dereference
11105 then
11106 return;
11107 end if;
11109 Error_Msg_NE
11110 ("expect subprogram or entry name in instantiation of &",
11111 Instantiation_Node, Formal_Sub);
11112 Abandon_Instantiation (Instantiation_Node);
11113 end Valid_Actual_Subprogram;
11115 -- Local variables
11117 Decl_Node : Node_Id;
11118 Loc : Source_Ptr;
11119 Nam : Node_Id;
11120 New_Spec : Node_Id;
11121 New_Subp : Entity_Id;
11123 -- Start of processing for Instantiate_Formal_Subprogram
11125 begin
11126 New_Spec := New_Copy_Tree (Specification (Formal));
11128 -- The tree copy has created the proper instantiation sloc for the
11129 -- new specification. Use this location for all other constructed
11130 -- declarations.
11132 Loc := Sloc (Defining_Unit_Name (New_Spec));
11134 -- Create new entity for the actual (New_Copy_Tree does not), and
11135 -- indicate that it is an actual.
11137 -- If the actual is not an entity (i.e. an attribute reference)
11138 -- and the formal includes aspect specifications for contracts,
11139 -- we create an internal name for the renaming declaration. The
11140 -- constructed wrapper contains a call to the entity in the renaming.
11141 -- This is an expansion activity, as is the wrapper creation.
11143 if Ada_Version >= Ada_2022
11144 and then Has_Contracts (Analyzed_Formal)
11145 and then not Is_Entity_Name (Actual)
11146 and then Expander_Active
11147 then
11148 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11149 else
11150 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11151 end if;
11153 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11154 Set_Is_Generic_Actual_Subprogram (New_Subp);
11155 Set_Defining_Unit_Name (New_Spec, New_Subp);
11157 -- Create new entities for the each of the formals in the specification
11158 -- of the renaming declaration built for the actual.
11160 if Present (Parameter_Specifications (New_Spec)) then
11161 declare
11162 F : Node_Id;
11163 F_Id : Entity_Id;
11165 begin
11166 F := First (Parameter_Specifications (New_Spec));
11167 while Present (F) loop
11168 F_Id := Defining_Identifier (F);
11170 Set_Defining_Identifier (F,
11171 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11172 Next (F);
11173 end loop;
11174 end;
11175 end if;
11177 -- Find entity of actual. If the actual is an attribute reference, it
11178 -- cannot be resolved here (its formal is missing) but is handled
11179 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11180 -- fully resolved subsequently, when the renaming declaration for the
11181 -- formal is analyzed. If it is an explicit dereference, resolve the
11182 -- prefix but not the actual itself, to prevent interpretation as call.
11184 if Present (Actual) then
11185 Loc := Sloc (Actual);
11186 Set_Sloc (New_Spec, Loc);
11188 if Nkind (Actual) = N_Operator_Symbol then
11189 Find_Direct_Name (Actual);
11191 elsif Nkind (Actual) = N_Explicit_Dereference then
11192 Analyze (Prefix (Actual));
11194 elsif Nkind (Actual) /= N_Attribute_Reference then
11195 Analyze (Actual);
11196 end if;
11198 Valid_Actual_Subprogram (Actual);
11199 Nam := Actual;
11201 elsif Present (Default_Name (Formal)) then
11202 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11203 | N_Selected_Component
11204 | N_Indexed_Component
11205 | N_Character_Literal
11206 and then Present (Entity (Default_Name (Formal)))
11207 then
11208 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11209 else
11210 Nam := New_Copy (Default_Name (Formal));
11211 Set_Sloc (Nam, Loc);
11212 end if;
11214 elsif Box_Present (Formal) then
11216 -- Actual is resolved at the point of instantiation. Create an
11217 -- identifier or operator with the same name as the formal.
11219 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11220 Nam :=
11221 Make_Operator_Symbol (Loc,
11222 Chars => Chars (Formal_Sub),
11223 Strval => No_String);
11224 else
11225 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11226 end if;
11228 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11229 and then Null_Present (Specification (Formal))
11230 then
11231 -- Generate null body for procedure, for use in the instance
11233 Decl_Node :=
11234 Make_Subprogram_Body (Loc,
11235 Specification => New_Spec,
11236 Declarations => New_List,
11237 Handled_Statement_Sequence =>
11238 Make_Handled_Sequence_Of_Statements (Loc,
11239 Statements => New_List (Make_Null_Statement (Loc))));
11241 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11243 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11245 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11247 -- Eliminate the calls to it when optimization is enabled
11249 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11250 return Decl_Node;
11252 -- Handle case of a formal function with an expression default (allowed
11253 -- when extensions are enabled).
11255 elsif Nkind (Specification (Formal)) = N_Function_Specification
11256 and then Present (Expression (Formal))
11257 then
11258 -- Generate body for function, for use in the instance
11260 declare
11261 Expr : constant Node_Id := New_Copy (Expression (Formal));
11262 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11263 begin
11264 Set_Sloc (Expr, Loc);
11265 Set_Expression (Stmt, Expr);
11267 Decl_Node :=
11268 Make_Subprogram_Body (Loc,
11269 Specification => New_Spec,
11270 Declarations => New_List,
11271 Handled_Statement_Sequence =>
11272 Make_Handled_Sequence_Of_Statements (Loc,
11273 Statements => New_List (Stmt)));
11274 end;
11276 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11277 -- has convention Intrinsic.
11279 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11281 -- Inline calls to it when optimization is enabled
11283 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11284 return Decl_Node;
11286 else
11287 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11288 Error_Msg_NE
11289 ("missing actual&", Instantiation_Node, Formal_Sub);
11290 Error_Msg_NE
11291 ("\in instantiation of & declared#",
11292 Instantiation_Node, Scope (Analyzed_S));
11293 Abandon_Instantiation (Instantiation_Node);
11294 end if;
11296 Decl_Node :=
11297 Make_Subprogram_Renaming_Declaration (Loc,
11298 Specification => New_Spec,
11299 Name => Nam);
11301 -- If we do not have an actual and the formal specified <> then set to
11302 -- get proper default.
11304 if No (Actual) and then Box_Present (Formal) then
11305 Set_From_Default (Decl_Node);
11306 end if;
11308 -- Gather possible interpretations for the actual before analyzing the
11309 -- instance. If overloaded, it will be resolved when analyzing the
11310 -- renaming declaration.
11312 if Box_Present (Formal) and then No (Actual) then
11313 Analyze (Nam);
11315 if Is_Child_Unit (Scope (Analyzed_S))
11316 and then Present (Entity (Nam))
11317 then
11318 if not Is_Overloaded (Nam) then
11319 if From_Parent_Scope (Entity (Nam)) then
11320 Set_Is_Immediately_Visible (Entity (Nam), False);
11321 Set_Entity (Nam, Empty);
11322 Set_Etype (Nam, Empty);
11324 Analyze (Nam);
11325 Set_Is_Immediately_Visible (Entity (Nam));
11326 end if;
11328 else
11329 declare
11330 I : Interp_Index;
11331 It : Interp;
11333 begin
11334 Get_First_Interp (Nam, I, It);
11335 while Present (It.Nam) loop
11336 if From_Parent_Scope (It.Nam) then
11337 Remove_Interp (I);
11338 end if;
11340 Get_Next_Interp (I, It);
11341 end loop;
11342 end;
11343 end if;
11344 end if;
11345 end if;
11347 -- The generic instantiation freezes the actual. This can only be done
11348 -- once the actual is resolved, in the analysis of the renaming
11349 -- declaration. To make the formal subprogram entity available, we set
11350 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11351 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11352 -- of formal abstract subprograms.
11354 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11356 -- We cannot analyze the renaming declaration, and thus find the actual,
11357 -- until all the actuals are assembled in the instance. For subsequent
11358 -- checks of other actuals, indicate the node that will hold the
11359 -- instance of this formal.
11361 Set_Instance_Of (Analyzed_S, Nam);
11363 if Nkind (Actual) = N_Selected_Component
11364 and then Is_Task_Type (Etype (Prefix (Actual)))
11365 and then not Is_Frozen (Etype (Prefix (Actual)))
11366 then
11367 -- The renaming declaration will create a body, which must appear
11368 -- outside of the instantiation, We move the renaming declaration
11369 -- out of the instance, and create an additional renaming inside,
11370 -- to prevent freezing anomalies.
11372 declare
11373 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11375 begin
11376 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11377 Insert_Before (Instantiation_Node, Decl_Node);
11378 Analyze (Decl_Node);
11380 -- Now create renaming within the instance
11382 Decl_Node :=
11383 Make_Subprogram_Renaming_Declaration (Loc,
11384 Specification => New_Copy_Tree (New_Spec),
11385 Name => New_Occurrence_Of (Anon_Id, Loc));
11387 Set_Defining_Unit_Name (Specification (Decl_Node),
11388 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11389 end;
11390 end if;
11392 return Decl_Node;
11393 end Instantiate_Formal_Subprogram;
11395 ------------------------
11396 -- Instantiate_Object --
11397 ------------------------
11399 function Instantiate_Object
11400 (Formal : Node_Id;
11401 Actual : Node_Id;
11402 Analyzed_Formal : Node_Id) return List_Id
11404 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11405 A_Gen_Obj : constant Entity_Id :=
11406 Defining_Identifier (Analyzed_Formal);
11407 Acc_Def : Node_Id := Empty;
11408 Act_Assoc : constant Node_Id :=
11409 (if No (Actual) then Empty else Parent (Actual));
11410 Actual_Decl : Node_Id := Empty;
11411 Decl_Node : Node_Id;
11412 Def : Node_Id;
11413 Ftyp : Entity_Id;
11414 List : constant List_Id := New_List;
11415 Loc : constant Source_Ptr := Sloc (Actual);
11416 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11417 Subt_Decl : Node_Id := Empty;
11418 Subt_Mark : Node_Id := Empty;
11420 -- Start of processing for Instantiate_Object
11422 begin
11423 -- Formal may be an anonymous access
11425 if Present (Subtype_Mark (Formal)) then
11426 Subt_Mark := Subtype_Mark (Formal);
11427 else
11428 Check_Access_Definition (Formal);
11429 Acc_Def := Access_Definition (Formal);
11430 end if;
11432 -- Sloc for error message on missing actual
11434 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11436 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11437 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11438 end if;
11440 Set_Parent (List, Act_Assoc);
11442 -- OUT present
11444 if Out_Present (Formal) then
11446 -- An IN OUT generic actual must be a name. The instantiation is a
11447 -- renaming declaration. The actual is the name being renamed. We
11448 -- use the actual directly, rather than a copy, because it is not
11449 -- used further in the list of actuals, and because a copy or a use
11450 -- of relocate_node is incorrect if the instance is nested within a
11451 -- generic. In order to simplify e.g. ASIS queries, the
11452 -- Generic_Parent field links the declaration to the generic
11453 -- association.
11455 if No (Actual) then
11456 Error_Msg_NE
11457 ("missing actual &",
11458 Instantiation_Node, Gen_Obj);
11459 Error_Msg_NE
11460 ("\in instantiation of & declared#",
11461 Instantiation_Node, Scope (A_Gen_Obj));
11462 Abandon_Instantiation (Instantiation_Node);
11463 end if;
11465 if Present (Subt_Mark) then
11466 Decl_Node :=
11467 Make_Object_Renaming_Declaration (Loc,
11468 Defining_Identifier => New_Copy (Gen_Obj),
11469 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11470 Name => Actual);
11472 else pragma Assert (Present (Acc_Def));
11473 Decl_Node :=
11474 Make_Object_Renaming_Declaration (Loc,
11475 Defining_Identifier => New_Copy (Gen_Obj),
11476 Access_Definition => New_Copy_Tree (Acc_Def),
11477 Name => Actual);
11478 end if;
11480 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11482 -- The analysis of the actual may produce Insert_Action nodes, so
11483 -- the declaration must have a context in which to attach them.
11485 Append (Decl_Node, List);
11486 Analyze (Actual);
11488 -- Return if the analysis of the actual reported some error
11490 if Etype (Actual) = Any_Type then
11491 return List;
11492 end if;
11494 -- This check is performed here because Analyze_Object_Renaming will
11495 -- not check it when Comes_From_Source is False. Note though that the
11496 -- check for the actual being the name of an object will be performed
11497 -- in Analyze_Object_Renaming.
11499 if Is_Object_Reference (Actual)
11500 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11501 then
11502 Error_Msg_N
11503 ("illegal discriminant-dependent component for in out parameter",
11504 Actual);
11505 end if;
11507 -- The actual has to be resolved in order to check that it is a
11508 -- variable (due to cases such as F (1), where F returns access to
11509 -- an array, and for overloaded prefixes).
11511 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11513 -- If the type of the formal is not itself a formal, and the current
11514 -- unit is a child unit, the formal type must be declared in a
11515 -- parent, and must be retrieved by visibility.
11517 if Ftyp = Orig_Ftyp
11518 and then Is_Generic_Unit (Scope (Ftyp))
11519 and then Is_Child_Unit (Scope (A_Gen_Obj))
11520 then
11521 declare
11522 Temp : constant Node_Id :=
11523 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11524 begin
11525 Set_Entity (Temp, Empty);
11526 Find_Type (Temp);
11527 Ftyp := Entity (Temp);
11528 end;
11529 end if;
11531 if Is_Private_Type (Ftyp)
11532 and then not Is_Private_Type (Etype (Actual))
11533 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11534 or else Base_Type (Etype (Actual)) = Ftyp)
11535 then
11536 -- If the actual has the type of the full view of the formal, or
11537 -- else a non-private subtype of the formal, then the visibility
11538 -- of the formal type has changed. Add to the actuals a subtype
11539 -- declaration that will force the exchange of views in the body
11540 -- of the instance as well.
11542 Subt_Decl :=
11543 Make_Subtype_Declaration (Loc,
11544 Defining_Identifier => Make_Temporary (Loc, 'P'),
11545 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11547 Prepend (Subt_Decl, List);
11549 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11550 Exchange_Declarations (Ftyp);
11551 end if;
11553 Resolve (Actual, Ftyp);
11555 if not Denotes_Variable (Actual) then
11556 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11558 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11560 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11561 -- the type of the actual shall resolve to a specific anonymous
11562 -- access type.
11564 if Ada_Version < Ada_2005
11565 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11566 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11567 then
11568 Error_Msg_NE
11569 ("type of actual does not match type of&", Actual, Gen_Obj);
11570 end if;
11571 end if;
11573 Note_Possible_Modification (Actual, Sure => True);
11575 -- Check for instantiation with atomic/volatile/VFA object actual for
11576 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11578 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11579 Error_Msg_NE
11580 ("cannot instantiate nonatomic formal & of mode in out",
11581 Actual, Gen_Obj);
11582 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11584 elsif Is_Volatile_Object_Ref (Actual)
11585 and then not Is_Volatile (Orig_Ftyp)
11586 then
11587 Error_Msg_NE
11588 ("cannot instantiate nonvolatile formal & of mode in out",
11589 Actual, Gen_Obj);
11590 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11592 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11593 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11594 then
11595 Error_Msg_NE
11596 ("cannot instantiate nonfull access formal & of mode in out",
11597 Actual, Gen_Obj);
11598 Error_Msg_N
11599 ("\with full access object actual (RM C.6(12))", Actual);
11600 end if;
11602 -- Check for instantiation on nonatomic subcomponent of a full access
11603 -- object in Ada 2022 (RM C.6 (12)).
11605 if Ada_Version >= Ada_2022
11606 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11607 and then not Is_Atomic_Object (Actual)
11608 then
11609 Error_Msg_NE
11610 ("cannot instantiate formal & of mode in out with actual",
11611 Actual, Gen_Obj);
11612 Error_Msg_N
11613 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11614 Actual);
11615 end if;
11617 -- The actual for a ghost generic formal IN OUT parameter should be a
11618 -- ghost object (SPARK RM 6.9(14)).
11620 Check_Ghost_Formal_Variable
11621 (Actual => Actual,
11622 Formal => A_Gen_Obj);
11624 -- Formal in-parameter
11626 else
11627 -- The instantiation of a generic formal in-parameter is constant
11628 -- declaration. The actual is the expression for that declaration.
11629 -- Its type is a full copy of the type of the formal. This may be
11630 -- an access to subprogram, for which we need to generate entities
11631 -- for the formals in the new signature.
11633 if Present (Actual) then
11634 if Present (Subt_Mark) then
11635 Def := New_Copy_Tree (Subt_Mark);
11636 else
11637 pragma Assert (Present (Acc_Def));
11638 Def := New_Copy_Tree (Acc_Def);
11639 end if;
11641 Decl_Node :=
11642 Make_Object_Declaration (Loc,
11643 Defining_Identifier => New_Copy (Gen_Obj),
11644 Constant_Present => True,
11645 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11646 Object_Definition => Def,
11647 Expression => Actual);
11649 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11650 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11652 -- A generic formal object of a tagged type is defined to be
11653 -- aliased so the new constant must also be treated as aliased.
11655 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11656 Set_Aliased_Present (Decl_Node);
11657 end if;
11659 Append (Decl_Node, List);
11661 -- The actual for a ghost generic formal IN parameter of
11662 -- access-to-variable type should be a ghost object (SPARK
11663 -- RM 6.9(14)).
11665 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11666 Check_Ghost_Formal_Variable
11667 (Actual => Actual,
11668 Formal => A_Gen_Obj);
11669 end if;
11671 -- No need to repeat (pre-)analysis of some expression nodes
11672 -- already handled in Preanalyze_Actuals.
11674 if Nkind (Actual) /= N_Allocator then
11675 Analyze (Actual);
11677 -- Return if the analysis of the actual reported some error
11679 if Etype (Actual) = Any_Type then
11680 return List;
11681 end if;
11682 end if;
11684 declare
11685 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11686 Typ : Entity_Id;
11688 begin
11689 Typ := Get_Instance_Of (Formal_Type);
11691 -- If the actual appears in the current or an enclosing scope,
11692 -- use its type directly. This is relevant if it has an actual
11693 -- subtype that is distinct from its nominal one. This cannot
11694 -- be done in general because the type of the actual may
11695 -- depend on other actuals, and only be fully determined when
11696 -- the enclosing instance is analyzed.
11698 if Present (Etype (Actual))
11699 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11700 then
11701 Freeze_Before (Instantiation_Node, Etype (Actual));
11702 else
11703 Freeze_Before (Instantiation_Node, Typ);
11704 end if;
11706 -- If the actual is an aggregate, perform name resolution on
11707 -- its components (the analysis of an aggregate does not do it)
11708 -- to capture local names that may be hidden if the generic is
11709 -- a child unit.
11711 if Nkind (Actual) = N_Aggregate then
11712 Preanalyze_And_Resolve (Actual, Typ);
11713 end if;
11715 if Is_Limited_Type (Typ)
11716 and then not OK_For_Limited_Init (Typ, Actual)
11717 then
11718 Error_Msg_N
11719 ("initialization not allowed for limited types", Actual);
11720 Explain_Limited_Type (Typ, Actual);
11721 end if;
11722 end;
11724 elsif Present (Default_Expression (Formal)) then
11726 -- Use default to construct declaration
11728 if Present (Subt_Mark) then
11729 Def := New_Copy_Tree (Subt_Mark);
11730 else
11731 pragma Assert (Present (Acc_Def));
11732 Def := New_Copy_Tree (Acc_Def);
11733 end if;
11735 Decl_Node :=
11736 Make_Object_Declaration (Sloc (Formal),
11737 Defining_Identifier => New_Copy (Gen_Obj),
11738 Constant_Present => True,
11739 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11740 Object_Definition => Def,
11741 Expression => New_Copy_Tree
11742 (Default_Expression (Formal)));
11744 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11745 Set_Corresponding_Generic_Association
11746 (Decl_Node, Expression (Decl_Node));
11748 Append (Decl_Node, List);
11749 Set_Analyzed (Expression (Decl_Node), False);
11751 else
11752 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11753 Error_Msg_NE ("\in instantiation of & declared#",
11754 Instantiation_Node, Scope (A_Gen_Obj));
11756 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11758 -- Create dummy constant declaration so that instance can be
11759 -- analyzed, to minimize cascaded visibility errors.
11761 if Present (Subt_Mark) then
11762 Def := Subt_Mark;
11763 else pragma Assert (Present (Acc_Def));
11764 Def := Acc_Def;
11765 end if;
11767 Decl_Node :=
11768 Make_Object_Declaration (Loc,
11769 Defining_Identifier => New_Copy (Gen_Obj),
11770 Constant_Present => True,
11771 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11772 Object_Definition => New_Copy (Def),
11773 Expression =>
11774 Make_Attribute_Reference (Sloc (Gen_Obj),
11775 Attribute_Name => Name_First,
11776 Prefix => New_Copy (Def)));
11778 Append (Decl_Node, List);
11780 else
11781 Abandon_Instantiation (Instantiation_Node);
11782 end if;
11783 end if;
11784 end if;
11786 if Nkind (Actual) in N_Has_Entity
11787 and then Present (Entity (Actual))
11788 then
11789 Actual_Decl := Parent (Entity (Actual));
11790 end if;
11792 -- Ada 2005 (AI-423) refined by AI12-0287:
11793 -- For an object_renaming_declaration with a null_exclusion or an
11794 -- access_definition that has a null_exclusion, the subtype of the
11795 -- object_name shall exclude null. In addition, if the
11796 -- object_renaming_declaration occurs within the body of a generic unit
11797 -- G or within the body of a generic unit declared within the
11798 -- declarative region of generic unit G, then:
11799 -- * if the object_name statically denotes a generic formal object of
11800 -- mode in out of G, then the declaration of that object shall have a
11801 -- null_exclusion;
11802 -- * if the object_name statically denotes a call of a generic formal
11803 -- function of G, then the declaration of the result of that function
11804 -- shall have a null_exclusion.
11806 if Ada_Version >= Ada_2005
11807 and then Present (Actual_Decl)
11808 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11809 | N_Object_Declaration
11810 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11811 and then not Has_Null_Exclusion (Actual_Decl)
11812 and then Has_Null_Exclusion (Analyzed_Formal)
11813 and then Ekind (Defining_Identifier (Analyzed_Formal))
11814 = E_Generic_In_Out_Parameter
11815 and then ((In_Generic_Scope (Entity (Actual))
11816 and then In_Package_Body (Scope (Entity (Actual))))
11817 or else not Can_Never_Be_Null (Etype (Actual)))
11818 then
11819 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11820 Error_Msg_N
11821 ("actual must exclude null to match generic formal#", Actual);
11822 end if;
11824 return List;
11825 end Instantiate_Object;
11827 ------------------------------
11828 -- Instantiate_Package_Body --
11829 ------------------------------
11831 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11832 -- must be replaced by gotos which jump to the end of the routine in order
11833 -- to restore the Ghost and SPARK modes.
11835 procedure Instantiate_Package_Body
11836 (Body_Info : Pending_Body_Info;
11837 Inlined_Body : Boolean := False;
11838 Body_Optional : Boolean := False)
11840 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11841 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11842 Act_Spec : constant Node_Id := Specification (Act_Decl);
11843 Ctx_Parents : Elist_Id := No_Elist;
11844 Ctx_Top : Int := 0;
11845 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11846 Gen_Id : constant Node_Id := Name (Inst_Node);
11847 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11848 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11849 Loc : constant Source_Ptr := Sloc (Inst_Node);
11851 procedure Check_Initialized_Types;
11852 -- In a generic package body, an entity of a generic private type may
11853 -- appear uninitialized. This is suspicious, unless the actual is a
11854 -- fully initialized type.
11856 procedure Install_Parents_Of_Generic_Context
11857 (Inst_Scope : Entity_Id;
11858 Ctx_Parents : out Elist_Id);
11859 -- Inst_Scope is the scope where the instance appears within; when it
11860 -- appears within a generic child package G, this routine collects and
11861 -- installs the enclosing packages of G in the scopes stack; installed
11862 -- packages are returned in Ctx_Parents.
11864 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11865 -- Reverse effect after instantiation is complete
11867 -----------------------------
11868 -- Check_Initialized_Types --
11869 -----------------------------
11871 procedure Check_Initialized_Types is
11872 Decl : Node_Id;
11873 Formal : Entity_Id;
11874 Actual : Entity_Id;
11875 Uninit_Var : Entity_Id;
11877 begin
11878 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11879 while Present (Decl) loop
11880 Uninit_Var := Empty;
11882 if Nkind (Decl) = N_Private_Extension_Declaration then
11883 Uninit_Var := Uninitialized_Variable (Decl);
11885 elsif Nkind (Decl) = N_Formal_Type_Declaration
11886 and then Nkind (Formal_Type_Definition (Decl)) =
11887 N_Formal_Private_Type_Definition
11888 then
11889 Uninit_Var :=
11890 Uninitialized_Variable (Formal_Type_Definition (Decl));
11891 end if;
11893 if Present (Uninit_Var) then
11894 Formal := Defining_Identifier (Decl);
11895 Actual := First_Entity (Act_Decl_Id);
11897 -- For each formal there is a subtype declaration that renames
11898 -- the actual and has the same name as the formal. Locate the
11899 -- formal for warning message about uninitialized variables
11900 -- in the generic, for which the actual type should be a fully
11901 -- initialized type.
11903 while Present (Actual) loop
11904 exit when Ekind (Actual) = E_Package
11905 and then Present (Renamed_Entity (Actual));
11907 if Chars (Actual) = Chars (Formal)
11908 and then not Is_Scalar_Type (Actual)
11909 and then not Is_Fully_Initialized_Type (Actual)
11910 and then Warn_On_No_Value_Assigned
11911 then
11912 Error_Msg_Node_2 := Formal;
11913 Error_Msg_NE
11914 ("generic unit has uninitialized variable& of "
11915 & "formal private type &?v?", Actual, Uninit_Var);
11916 Error_Msg_NE
11917 ("actual type for& should be fully initialized type?v?",
11918 Actual, Formal);
11919 exit;
11920 end if;
11922 Next_Entity (Actual);
11923 end loop;
11924 end if;
11926 Next (Decl);
11927 end loop;
11928 end Check_Initialized_Types;
11930 ----------------------------------------
11931 -- Install_Parents_Of_Generic_Context --
11932 ----------------------------------------
11934 procedure Install_Parents_Of_Generic_Context
11935 (Inst_Scope : Entity_Id;
11936 Ctx_Parents : out Elist_Id)
11938 Elmt : Elmt_Id;
11939 S : Entity_Id;
11941 begin
11942 Ctx_Parents := New_Elmt_List;
11944 -- Collect context parents (ie. parents where the instantiation
11945 -- appears within).
11947 S := Inst_Scope;
11948 while S /= Standard_Standard loop
11949 Prepend_Elmt (S, Ctx_Parents);
11950 S := Scope (S);
11951 end loop;
11953 -- Install enclosing parents
11955 Elmt := First_Elmt (Ctx_Parents);
11956 while Present (Elmt) loop
11957 Push_Scope (Node (Elmt));
11958 Set_Is_Immediately_Visible (Node (Elmt));
11959 Next_Elmt (Elmt);
11960 end loop;
11961 end Install_Parents_Of_Generic_Context;
11963 ---------------------------------------
11964 -- Remove_Parents_Of_Generic_Context --
11965 ---------------------------------------
11967 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11968 Elmt : Elmt_Id;
11970 begin
11971 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11973 Elmt := Last_Elmt (Ctx_Parents);
11974 while Present (Elmt) loop
11975 pragma Assert (Current_Scope = Node (Elmt));
11976 Set_Is_Immediately_Visible (Current_Scope, False);
11977 Pop_Scope;
11979 Remove_Last_Elmt (Ctx_Parents);
11980 Elmt := Last_Elmt (Ctx_Parents);
11981 end loop;
11982 end Remove_Parents_Of_Generic_Context;
11984 -- Local variables
11986 -- The following constants capture the context prior to instantiating
11987 -- the package body.
11989 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11990 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11991 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11992 Saved_ISMP : constant Boolean :=
11993 Ignore_SPARK_Mode_Pragmas_In_Instance;
11994 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11995 Local_Suppress_Stack_Top;
11996 Saved_SC : constant Boolean := Style_Check;
11997 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11998 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11999 Saved_SS : constant Suppress_Record := Scope_Suppress;
12000 Saved_Warn : constant Warnings_State := Save_Warnings;
12002 Act_Body : Node_Id;
12003 Act_Body_Id : Entity_Id;
12004 Act_Body_Name : Node_Id;
12005 Gen_Body : Node_Id;
12006 Gen_Body_Id : Node_Id;
12007 Par_Ent : Entity_Id := Empty;
12008 Par_Installed : Boolean := False;
12009 Par_Vis : Boolean := False;
12011 Scope_Check_Id : Entity_Id;
12012 Scope_Check_Last : Nat;
12013 -- Value of Current_Scope before calls to Install_Parents; used to check
12014 -- that scopes are correctly removed after instantiation.
12016 Vis_Prims_List : Elist_Id := No_Elist;
12017 -- List of primitives made temporarily visible in the instantiation
12018 -- to match the visibility of the formal type.
12020 -- Start of processing for Instantiate_Package_Body
12022 begin
12023 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12025 -- The instance body may already have been processed, as the parent of
12026 -- another instance that is inlined (Load_Parent_Of_Generic).
12028 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12029 return;
12030 end if;
12032 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12034 -- Re-establish the state of information on which checks are suppressed.
12035 -- This information was set in Body_Info at the point of instantiation,
12036 -- and now we restore it so that the instance is compiled using the
12037 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12039 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12040 Scope_Suppress := Body_Info.Scope_Suppress;
12042 Restore_Config_Switches (Body_Info.Config_Switches);
12043 Restore_Warnings (Body_Info.Warnings);
12045 if No (Gen_Body_Id) then
12047 -- Do not look for parent of generic body if none is required.
12048 -- This may happen when the routine is called as part of the
12049 -- Pending_Instantiations processing, when nested instances
12050 -- may precede the one generated from the main unit.
12052 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12053 and then Body_Optional
12054 then
12055 goto Leave;
12056 else
12057 Load_Parent_Of_Generic
12058 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12060 -- Surprisingly enough, loading the body of the parent can cause
12061 -- the body to be instantiated and the double instantiation needs
12062 -- to be prevented in order to avoid giving bogus semantic errors.
12064 -- This case can occur because of the Collect_Previous_Instances
12065 -- machinery of Load_Parent_Of_Generic, which will instantiate
12066 -- bodies that are deemed to be ahead of the body of the parent
12067 -- in the compilation unit. But the relative position of these
12068 -- bodies is computed using the mere comparison of their Sloc.
12070 -- Now suppose that you have two generic packages G and H, with
12071 -- G containing a mere instantiation of H:
12073 -- generic
12074 -- package H is
12076 -- generic
12077 -- package Nested_G is
12078 -- ...
12079 -- end Nested_G;
12081 -- end H;
12083 -- with H;
12085 -- generic
12086 -- package G is
12088 -- package My_H is new H;
12090 -- end G;
12092 -- and a third package Q instantiating G and Nested_G:
12094 -- with G;
12096 -- package Q is
12098 -- package My_G is new G;
12100 -- package My_Nested_G is new My_G.My_H.Nested_G;
12102 -- end Q;
12104 -- The body to be instantiated is that of My_Nested_G and its
12105 -- parent is the instance My_G.My_H. This latter instantiation
12106 -- is done when My_G is analyzed, i.e. after the declarations
12107 -- of My_G and My_Nested_G have been parsed; as a result, the
12108 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12110 -- Therefore loading the body of My_G.My_H will cause the body
12111 -- of My_Nested_G to be instantiated because it is deemed to be
12112 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12113 -- will again be invoked on My_G.My_H, but this time with the
12114 -- Collect_Previous_Instances machinery disabled, so there is
12115 -- no endless mutual recursion and things are done in order.
12117 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12118 goto Leave;
12119 end if;
12121 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12122 end if;
12123 end if;
12125 -- Establish global variable for sloc adjustment and for error recovery
12126 -- In the case of an instance body for an instantiation with actuals
12127 -- from a limited view, the instance body is placed at the beginning
12128 -- of the enclosing package body: use the body entity as the source
12129 -- location for nodes of the instance body.
12131 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12132 declare
12133 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12134 Body_Id : constant Node_Id :=
12135 Corresponding_Body (Unit_Declaration_Node (Scop));
12137 begin
12138 Instantiation_Node := Body_Id;
12139 end;
12140 else
12141 Instantiation_Node := Inst_Node;
12142 end if;
12144 -- The package being instantiated may be subject to pragma Ghost. Set
12145 -- the mode now to ensure that any nodes generated during instantiation
12146 -- are properly marked as Ghost.
12148 Set_Ghost_Mode (Act_Decl_Id);
12150 if Present (Gen_Body_Id) then
12151 Save_Env (Gen_Unit, Act_Decl_Id);
12152 Style_Check := False;
12154 -- If the context of the instance is subject to SPARK_Mode "off", the
12155 -- annotation is missing, or the body is instantiated at a later pass
12156 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12157 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12158 -- instance.
12160 if SPARK_Mode /= On
12161 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12162 then
12163 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12164 end if;
12166 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12167 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12169 Create_Instantiation_Source
12170 (Inst_Node, Gen_Body_Id, S_Adjustment);
12172 Act_Body :=
12173 Copy_Generic_Node
12174 (Original_Node (Gen_Body), Empty, Instantiating => True);
12176 -- Create proper (possibly qualified) defining name for the body, to
12177 -- correspond to the one in the spec.
12179 Act_Body_Id :=
12180 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12181 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12183 -- Some attributes of spec entity are not inherited by body entity
12185 Set_Handler_Records (Act_Body_Id, No_List);
12187 if Nkind (Defining_Unit_Name (Act_Spec)) =
12188 N_Defining_Program_Unit_Name
12189 then
12190 Act_Body_Name :=
12191 Make_Defining_Program_Unit_Name (Loc,
12192 Name =>
12193 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12194 Defining_Identifier => Act_Body_Id);
12195 else
12196 Act_Body_Name := Act_Body_Id;
12197 end if;
12199 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12201 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12202 Check_Generic_Actuals (Act_Decl_Id, False);
12203 Check_Initialized_Types;
12205 -- Install primitives hidden at the point of the instantiation but
12206 -- visible when processing the generic formals
12208 declare
12209 E : Entity_Id;
12211 begin
12212 E := First_Entity (Act_Decl_Id);
12213 while Present (E) loop
12214 if Is_Type (E)
12215 and then not Is_Itype (E)
12216 and then Is_Generic_Actual_Type (E)
12217 and then Is_Tagged_Type (E)
12218 then
12219 Install_Hidden_Primitives
12220 (Prims_List => Vis_Prims_List,
12221 Gen_T => Generic_Parent_Type (Parent (E)),
12222 Act_T => E);
12223 end if;
12225 Next_Entity (E);
12226 end loop;
12227 end;
12229 Scope_Check_Id := Current_Scope;
12230 Scope_Check_Last := Scope_Stack.Last;
12232 -- If the instantiation appears within a generic child some actual
12233 -- parameter may be the current instance of the enclosing generic
12234 -- parent.
12236 declare
12237 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12239 begin
12240 if Is_Child_Unit (Inst_Scope)
12241 and then Ekind (Inst_Scope) = E_Generic_Package
12242 and then Present (Generic_Associations (Inst_Node))
12243 then
12244 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12246 -- Hide them from visibility; required to avoid conflicts
12247 -- installing the parent instance.
12249 if Present (Ctx_Parents) then
12250 Push_Scope (Standard_Standard);
12251 Ctx_Top := Scope_Stack.Last;
12252 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12253 end if;
12254 end if;
12255 end;
12257 -- If it is a child unit, make the parent instance (which is an
12258 -- instance of the parent of the generic) visible.
12260 -- 1) The child unit's parent is an explicit parent instance (the
12261 -- prefix of the name of the generic unit):
12263 -- package Child_Package is new Parent_Instance.Child_Unit;
12265 -- 2) The child unit's parent is an implicit parent instance (e.g.
12266 -- when instantiating a sibling package):
12268 -- generic
12269 -- package Parent.Second_Child is
12270 -- ...
12272 -- generic
12273 -- package Parent.First_Child is
12274 -- package Sibling_Package is new Second_Child;
12276 -- 3) The child unit's parent is not an instance, so the scope is
12277 -- simply the one of the unit.
12279 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12280 and then Nkind (Gen_Id) = N_Expanded_Name
12281 then
12282 Par_Ent := Entity (Prefix (Gen_Id));
12284 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12285 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12286 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12287 and then Nkind
12288 (Name (Get_Unit_Instantiation_Node
12289 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12290 then
12291 Par_Ent := Entity
12292 (Prefix (Name (Get_Unit_Instantiation_Node
12293 (Scope (Act_Decl_Id)))));
12295 elsif Is_Child_Unit (Gen_Unit) then
12296 Par_Ent := Scope (Gen_Unit);
12297 end if;
12299 if Present (Par_Ent) then
12300 Par_Vis := Is_Immediately_Visible (Par_Ent);
12301 Install_Parent (Par_Ent, In_Body => True);
12302 Par_Installed := True;
12303 end if;
12305 -- If the instantiation is a library unit, and this is the main unit,
12306 -- then build the resulting compilation unit nodes for the instance.
12307 -- If this is a compilation unit but it is not the main unit, then it
12308 -- is the body of a unit in the context, that is being compiled
12309 -- because it is encloses some inlined unit or another generic unit
12310 -- being instantiated. In that case, this body is not part of the
12311 -- current compilation, and is not attached to the tree, but its
12312 -- parent must be set for analysis.
12314 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12316 -- Replace instance node with body of instance, and create new
12317 -- node for corresponding instance declaration.
12319 Build_Instance_Compilation_Unit_Nodes
12320 (Inst_Node, Act_Body, Act_Decl);
12322 -- If the instantiation appears within a generic child package
12323 -- enable visibility of current instance of enclosing generic
12324 -- parents.
12326 if Present (Ctx_Parents) then
12327 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12328 Analyze (Inst_Node);
12329 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12330 else
12331 Analyze (Inst_Node);
12332 end if;
12334 if Parent (Inst_Node) = Cunit (Main_Unit) then
12336 -- If the instance is a child unit itself, then set the scope
12337 -- of the expanded body to be the parent of the instantiation
12338 -- (ensuring that the fully qualified name will be generated
12339 -- for the elaboration subprogram).
12341 if Nkind (Defining_Unit_Name (Act_Spec)) =
12342 N_Defining_Program_Unit_Name
12343 then
12344 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12345 end if;
12346 end if;
12348 -- Case where instantiation is not a library unit
12350 else
12351 -- Handle the case of an instance with incomplete actual types.
12352 -- The instance body cannot be placed just after the declaration
12353 -- because full views have not been seen yet. Any use of the non-
12354 -- limited views in the instance body requires the presence of a
12355 -- regular with_clause in the enclosing unit. Therefore we place
12356 -- the instance body at the beginning of the enclosing body, and
12357 -- the freeze node for the instance is then placed after the body.
12359 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12360 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12361 then
12362 declare
12363 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12364 Body_Id : constant Node_Id :=
12365 Corresponding_Body (Unit_Declaration_Node (Scop));
12367 F_Node : Node_Id;
12369 begin
12370 pragma Assert (Present (Body_Id));
12372 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12374 if Expander_Active then
12375 Ensure_Freeze_Node (Act_Decl_Id);
12376 F_Node := Freeze_Node (Act_Decl_Id);
12377 Set_Is_Frozen (Act_Decl_Id, False);
12378 if Is_List_Member (F_Node) then
12379 Remove (F_Node);
12380 end if;
12382 Insert_After (Act_Body, F_Node);
12383 end if;
12384 end;
12386 else
12387 Insert_Before (Inst_Node, Act_Body);
12388 Mark_Rewrite_Insertion (Act_Body);
12390 -- Insert the freeze node for the instance if need be
12392 if Expander_Active then
12393 Freeze_Package_Instance
12394 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12395 Set_Is_Frozen (Act_Decl_Id);
12396 end if;
12397 end if;
12399 -- If the instantiation appears within a generic child package
12400 -- enable visibility of current instance of enclosing generic
12401 -- parents.
12403 if Present (Ctx_Parents) then
12404 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12405 Analyze (Act_Body);
12406 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12407 else
12408 Analyze (Act_Body);
12409 end if;
12410 end if;
12412 Inherit_Context (Gen_Body, Inst_Node);
12414 if Par_Installed then
12415 Remove_Parent (In_Body => True);
12417 -- Restore the previous visibility of the parent
12419 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12420 end if;
12422 -- Remove the parent instances if they have been placed on the scope
12423 -- stack to compile the body.
12425 if Present (Ctx_Parents) then
12426 pragma Assert (Scope_Stack.Last = Ctx_Top
12427 and then Current_Scope = Standard_Standard);
12428 Pop_Scope;
12430 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12431 end if;
12433 pragma Assert (Current_Scope = Scope_Check_Id);
12434 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12436 Restore_Hidden_Primitives (Vis_Prims_List);
12438 -- Restore the private views that were made visible when the body of
12439 -- the instantiation was created. Note that, in the case where one of
12440 -- these private views is declared in the parent, there is a nesting
12441 -- issue with the calls to Install_Parent and Remove_Parent made in
12442 -- between above with In_Body set to True, because these calls also
12443 -- want to swap and restore this private view respectively. In this
12444 -- case, the call to Install_Parent does nothing, but the call to
12445 -- Remove_Parent does restore the private view, thus undercutting the
12446 -- call to Restore_Private_Views. That's OK under the condition that
12447 -- the two mechanisms swap exactly the same entities, in particular
12448 -- the private entities dependent on the primary private entities.
12450 Restore_Private_Views (Act_Decl_Id);
12452 -- Remove the current unit from visibility if this is an instance
12453 -- that is not elaborated on the fly for inlining purposes.
12455 if not Inlined_Body then
12456 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12457 end if;
12459 Restore_Env;
12461 -- If we have no body, and the unit requires a body, then complain. This
12462 -- complaint is suppressed if we have detected other errors (since a
12463 -- common reason for missing the body is that it had errors).
12464 -- In CodePeer mode, a warning has been emitted already, no need for
12465 -- further messages.
12467 elsif Unit_Requires_Body (Gen_Unit)
12468 and then not Body_Optional
12469 then
12470 if CodePeer_Mode then
12471 null;
12473 elsif Serious_Errors_Detected = 0 then
12474 Error_Msg_NE
12475 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12477 -- Don't attempt to perform any cleanup actions if some other error
12478 -- was already detected, since this can cause blowups.
12480 else
12481 goto Leave;
12482 end if;
12484 -- Case of package that does not need a body
12486 else
12487 -- If the instantiation of the declaration is a library unit, rewrite
12488 -- the original package instantiation as a package declaration in the
12489 -- compilation unit node.
12491 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12492 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12493 Rewrite (Inst_Node, Act_Decl);
12495 -- Generate elaboration entity, in case spec has elaboration code.
12496 -- This cannot be done when the instance is analyzed, because it
12497 -- is not known yet whether the body exists.
12499 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12500 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12502 -- If the instantiation is not a library unit, then append the
12503 -- declaration to the list of implicitly generated entities, unless
12504 -- it is already a list member which means that it was already
12505 -- processed
12507 elsif not Is_List_Member (Act_Decl) then
12508 Mark_Rewrite_Insertion (Act_Decl);
12509 Insert_Before (Inst_Node, Act_Decl);
12510 end if;
12511 end if;
12513 <<Leave>>
12515 -- Restore the context that was in effect prior to instantiating the
12516 -- package body.
12518 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12519 Local_Suppress_Stack_Top := Saved_LSST;
12520 Scope_Suppress := Saved_SS;
12521 Style_Check := Saved_SC;
12523 Expander_Mode_Restore;
12524 Restore_Config_Switches (Saved_CS);
12525 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12526 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12527 Restore_Warnings (Saved_Warn);
12528 end Instantiate_Package_Body;
12530 ---------------------------------
12531 -- Instantiate_Subprogram_Body --
12532 ---------------------------------
12534 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12535 -- must be replaced by gotos which jump to the end of the routine in order
12536 -- to restore the Ghost and SPARK modes.
12538 procedure Instantiate_Subprogram_Body
12539 (Body_Info : Pending_Body_Info;
12540 Body_Optional : Boolean := False)
12542 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12543 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12544 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12545 Gen_Id : constant Node_Id := Name (Inst_Node);
12546 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12547 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12548 Loc : constant Source_Ptr := Sloc (Inst_Node);
12549 Pack_Id : constant Entity_Id :=
12550 Defining_Unit_Name (Parent (Act_Decl));
12552 -- The following constants capture the context prior to instantiating
12553 -- the subprogram body.
12555 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12556 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12557 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12558 Saved_ISMP : constant Boolean :=
12559 Ignore_SPARK_Mode_Pragmas_In_Instance;
12560 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12561 Local_Suppress_Stack_Top;
12562 Saved_SC : constant Boolean := Style_Check;
12563 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12564 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12565 Saved_SS : constant Suppress_Record := Scope_Suppress;
12566 Saved_Warn : constant Warnings_State := Save_Warnings;
12568 Act_Body : Node_Id;
12569 Act_Body_Id : Entity_Id;
12570 Gen_Body : Node_Id;
12571 Gen_Body_Id : Node_Id;
12572 Pack_Body : Node_Id;
12573 Par_Ent : Entity_Id := Empty;
12574 Par_Installed : Boolean := False;
12575 Par_Vis : Boolean := False;
12576 Ret_Expr : Node_Id;
12578 begin
12579 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12581 -- Subprogram body may have been created already because of an inline
12582 -- pragma, or because of multiple elaborations of the enclosing package
12583 -- when several instances of the subprogram appear in the main unit.
12585 if Present (Corresponding_Body (Act_Decl)) then
12586 return;
12587 end if;
12589 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12591 -- Re-establish the state of information on which checks are suppressed.
12592 -- This information was set in Body_Info at the point of instantiation,
12593 -- and now we restore it so that the instance is compiled using the
12594 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12596 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12597 Scope_Suppress := Body_Info.Scope_Suppress;
12599 Restore_Config_Switches (Body_Info.Config_Switches);
12600 Restore_Warnings (Body_Info.Warnings);
12602 if No (Gen_Body_Id) then
12604 -- For imported generic subprogram, no body to compile, complete
12605 -- the spec entity appropriately.
12607 if Is_Imported (Gen_Unit) then
12608 Set_Is_Imported (Act_Decl_Id);
12609 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12610 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12611 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12612 Set_Has_Completion (Act_Decl_Id);
12613 goto Leave;
12615 -- For other cases, compile the body
12617 else
12618 Load_Parent_Of_Generic
12619 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12620 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12621 end if;
12622 end if;
12624 Instantiation_Node := Inst_Node;
12626 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12627 -- the mode now to ensure that any nodes generated during instantiation
12628 -- are properly marked as Ghost.
12630 Set_Ghost_Mode (Act_Decl_Id);
12632 if Present (Gen_Body_Id) then
12633 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12635 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12637 -- Either body is not present, or context is non-expanding, as
12638 -- when compiling a subunit. Mark the instance as completed, and
12639 -- diagnose a missing body when needed.
12641 if Expander_Active
12642 and then Operating_Mode = Generate_Code
12643 then
12644 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12645 end if;
12647 Set_Has_Completion (Act_Decl_Id);
12648 goto Leave;
12649 end if;
12651 Save_Env (Gen_Unit, Act_Decl_Id);
12652 Style_Check := False;
12654 -- If the context of the instance is subject to SPARK_Mode "off", the
12655 -- annotation is missing, or the body is instantiated at a later pass
12656 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12657 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12658 -- instance.
12660 if SPARK_Mode /= On
12661 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12662 then
12663 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12664 end if;
12666 -- If the context of an instance is not subject to SPARK_Mode "off",
12667 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12668 -- the latter should be the one applicable to the instance.
12670 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12671 and then SPARK_Mode /= Off
12672 and then Present (SPARK_Pragma (Gen_Body_Id))
12673 then
12674 Set_SPARK_Mode (Gen_Body_Id);
12675 end if;
12677 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12678 Create_Instantiation_Source
12679 (Inst_Node,
12680 Gen_Body_Id,
12681 S_Adjustment);
12683 Act_Body :=
12684 Copy_Generic_Node
12685 (Original_Node (Gen_Body), Empty, Instantiating => True);
12687 -- Create proper defining name for the body, to correspond to the one
12688 -- in the spec.
12690 Act_Body_Id :=
12691 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12693 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12694 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12696 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12697 Set_Has_Completion (Act_Decl_Id);
12698 Check_Generic_Actuals (Pack_Id, False);
12700 -- Generate a reference to link the visible subprogram instance to
12701 -- the generic body, which for navigation purposes is the only
12702 -- available source for the instance.
12704 Generate_Reference
12705 (Related_Instance (Pack_Id),
12706 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12708 -- If it is a child unit, make the parent instance (which is an
12709 -- instance of the parent of the generic) visible. The parent
12710 -- instance is the prefix of the name of the generic unit.
12712 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12713 and then Nkind (Gen_Id) = N_Expanded_Name
12714 then
12715 Par_Ent := Entity (Prefix (Gen_Id));
12716 elsif Is_Child_Unit (Gen_Unit) then
12717 Par_Ent := Scope (Gen_Unit);
12718 end if;
12720 if Present (Par_Ent) then
12721 Par_Vis := Is_Immediately_Visible (Par_Ent);
12722 Install_Parent (Par_Ent, In_Body => True);
12723 Par_Installed := True;
12724 end if;
12726 -- Subprogram body is placed in the body of wrapper package,
12727 -- whose spec contains the subprogram declaration as well as
12728 -- the renaming declarations for the generic parameters.
12730 Pack_Body :=
12731 Make_Package_Body (Loc,
12732 Defining_Unit_Name => New_Copy (Pack_Id),
12733 Declarations => New_List (Act_Body));
12735 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12737 -- If the instantiation is a library unit, then build resulting
12738 -- compilation unit nodes for the instance. The declaration of
12739 -- the enclosing package is the grandparent of the subprogram
12740 -- declaration. First replace the instantiation node as the unit
12741 -- of the corresponding compilation.
12743 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12744 if Parent (Inst_Node) = Cunit (Main_Unit) then
12745 Set_Unit (Parent (Inst_Node), Inst_Node);
12746 Build_Instance_Compilation_Unit_Nodes
12747 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12748 Analyze (Inst_Node);
12749 else
12750 Set_Parent (Pack_Body, Parent (Inst_Node));
12751 Analyze (Pack_Body);
12752 end if;
12754 else
12755 Insert_Before (Inst_Node, Pack_Body);
12756 Mark_Rewrite_Insertion (Pack_Body);
12758 -- Insert the freeze node for the instance if need be
12760 if Expander_Active then
12761 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12762 end if;
12764 Analyze (Pack_Body);
12765 end if;
12767 Inherit_Context (Gen_Body, Inst_Node);
12769 Restore_Private_Views (Pack_Id, False);
12771 if Par_Installed then
12772 Remove_Parent (In_Body => True);
12774 -- Restore the previous visibility of the parent
12776 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12777 end if;
12779 Restore_Env;
12781 -- Body not found. Error was emitted already. If there were no previous
12782 -- errors, this may be an instance whose scope is a premature instance.
12783 -- In that case we must insure that the (legal) program does raise
12784 -- program error if executed. We generate a subprogram body for this
12785 -- purpose.
12787 elsif Serious_Errors_Detected = 0
12788 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12789 then
12790 if Body_Optional then
12791 goto Leave;
12793 elsif Ekind (Act_Decl_Id) = E_Procedure then
12794 Act_Body :=
12795 Make_Subprogram_Body (Loc,
12796 Specification =>
12797 Make_Procedure_Specification (Loc,
12798 Defining_Unit_Name =>
12799 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12800 Parameter_Specifications =>
12801 New_Copy_List
12802 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12804 Declarations => Empty_List,
12805 Handled_Statement_Sequence =>
12806 Make_Handled_Sequence_Of_Statements (Loc,
12807 Statements => New_List (
12808 Make_Raise_Program_Error (Loc,
12809 Reason => PE_Access_Before_Elaboration))));
12811 else
12812 Ret_Expr :=
12813 Make_Raise_Program_Error (Loc,
12814 Reason => PE_Access_Before_Elaboration);
12816 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12817 Set_Analyzed (Ret_Expr);
12819 Act_Body :=
12820 Make_Subprogram_Body (Loc,
12821 Specification =>
12822 Make_Function_Specification (Loc,
12823 Defining_Unit_Name =>
12824 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12825 Parameter_Specifications =>
12826 New_Copy_List
12827 (Parameter_Specifications (Parent (Act_Decl_Id))),
12828 Result_Definition =>
12829 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12831 Declarations => Empty_List,
12832 Handled_Statement_Sequence =>
12833 Make_Handled_Sequence_Of_Statements (Loc,
12834 Statements => New_List (
12835 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12836 end if;
12838 Pack_Body :=
12839 Make_Package_Body (Loc,
12840 Defining_Unit_Name => New_Copy (Pack_Id),
12841 Declarations => New_List (Act_Body));
12843 Insert_After (Inst_Node, Pack_Body);
12844 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12845 Analyze (Pack_Body);
12846 end if;
12848 <<Leave>>
12850 -- Restore the context that was in effect prior to instantiating the
12851 -- subprogram body.
12853 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12854 Local_Suppress_Stack_Top := Saved_LSST;
12855 Scope_Suppress := Saved_SS;
12856 Style_Check := Saved_SC;
12858 Expander_Mode_Restore;
12859 Restore_Config_Switches (Saved_CS);
12860 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12861 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12862 Restore_Warnings (Saved_Warn);
12863 end Instantiate_Subprogram_Body;
12865 ----------------------
12866 -- Instantiate_Type --
12867 ----------------------
12869 function Instantiate_Type
12870 (Formal : Node_Id;
12871 Actual : Node_Id;
12872 Analyzed_Formal : Node_Id;
12873 Actual_Decls : List_Id) return List_Id
12875 A_Gen_T : constant Entity_Id :=
12876 Defining_Identifier (Analyzed_Formal);
12877 Def : constant Node_Id := Formal_Type_Definition (Formal);
12878 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12879 Act_T : Entity_Id;
12880 Ancestor : Entity_Id := Empty;
12881 Decl_Node : Node_Id;
12882 Decl_Nodes : List_Id;
12883 Loc : Source_Ptr;
12884 Subt : Entity_Id;
12886 procedure Check_Shared_Variable_Control_Aspects;
12887 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12888 -- that may be specified for a formal type are obeyed by the actual.
12890 procedure Diagnose_Predicated_Actual;
12891 -- There are a number of constructs in which a discrete type with
12892 -- predicates is illegal, e.g. as an index in an array type declaration.
12893 -- If a generic type is used is such a construct in a generic package
12894 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12895 -- of the generic contract that the actual cannot have predicates.
12897 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12898 -- Check that base types are the same and that the subtypes match
12899 -- statically. Used in several of the validation subprograms for
12900 -- actuals in instantiations.
12902 procedure Validate_Array_Type_Instance;
12903 procedure Validate_Access_Subprogram_Instance;
12904 procedure Validate_Access_Type_Instance;
12905 procedure Validate_Derived_Type_Instance;
12906 procedure Validate_Derived_Interface_Type_Instance;
12907 procedure Validate_Discriminated_Formal_Type;
12908 procedure Validate_Interface_Type_Instance;
12909 procedure Validate_Private_Type_Instance;
12910 procedure Validate_Incomplete_Type_Instance;
12911 -- These procedures perform validation tests for the named case.
12912 -- Validate_Discriminated_Formal_Type is shared by formal private
12913 -- types and Ada 2012 formal incomplete types.
12915 --------------------------------------------
12916 -- Check_Shared_Variable_Control_Aspects --
12917 --------------------------------------------
12919 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12920 -- that may be specified for the formal are obeyed by the actual.
12921 -- If the formal is a derived type the aspect specifications must match.
12922 -- NOTE: AI12-0282 implies that matching of aspects is required between
12923 -- formal and actual in all cases, but this is too restrictive.
12924 -- In particular it violates a language design rule: a limited private
12925 -- indefinite formal can be matched by any actual. The current code
12926 -- reflects an older and more permissive version of RM C.6 (12/5).
12928 procedure Check_Shared_Variable_Control_Aspects is
12929 begin
12930 if Ada_Version >= Ada_2022 then
12931 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12932 Error_Msg_NE
12933 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12935 elsif Is_Derived_Type (A_Gen_T)
12936 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12937 then
12938 Error_Msg_NE
12939 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12940 end if;
12942 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12943 Error_Msg_NE
12944 ("actual for& must have Volatile aspect",
12945 Actual, A_Gen_T);
12947 elsif Is_Derived_Type (A_Gen_T)
12948 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12949 then
12950 Error_Msg_NE
12951 ("actual for& has different Volatile aspect",
12952 Actual, A_Gen_T);
12953 end if;
12955 -- We assume that an array type whose atomic component type
12956 -- is Atomic is equivalent to an array type with the explicit
12957 -- aspect Has_Atomic_Components. This is a reasonable inference
12958 -- from the intent of AI12-0282, and makes it legal to use an
12959 -- actual that does not have the identical aspect as the formal.
12960 -- Ditto for volatile components.
12962 declare
12963 Actual_Atomic_Comp : constant Boolean :=
12964 Has_Atomic_Components (Act_T)
12965 or else (Is_Array_Type (Act_T)
12966 and then Is_Atomic (Component_Type (Act_T)));
12967 begin
12968 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12969 Error_Msg_NE
12970 ("formal and actual for& must agree on atomic components",
12971 Actual, A_Gen_T);
12972 end if;
12973 end;
12975 declare
12976 Actual_Volatile_Comp : constant Boolean :=
12977 Has_Volatile_Components (Act_T)
12978 or else (Is_Array_Type (Act_T)
12979 and then Is_Volatile (Component_Type (Act_T)));
12980 begin
12981 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12982 then
12983 Error_Msg_NE
12984 ("actual for& must have volatile components",
12985 Actual, A_Gen_T);
12986 end if;
12987 end;
12989 -- The following two aspects do not require exact matching,
12990 -- but only one-way agreement. See RM C.6.
12992 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12993 then
12994 Error_Msg_NE
12995 ("actual for& must have Independent aspect specified",
12996 Actual, A_Gen_T);
12997 end if;
12999 if Has_Independent_Components (A_Gen_T)
13000 and then not Has_Independent_Components (Act_T)
13001 then
13002 Error_Msg_NE
13003 ("actual for& must have Independent_Components specified",
13004 Actual, A_Gen_T);
13005 end if;
13006 end if;
13007 end Check_Shared_Variable_Control_Aspects;
13009 ---------------------------------
13010 -- Diagnose_Predicated_Actual --
13011 ---------------------------------
13013 procedure Diagnose_Predicated_Actual is
13014 begin
13015 if No_Predicate_On_Actual (A_Gen_T)
13016 and then Has_Predicates (Act_T)
13017 then
13018 Error_Msg_NE
13019 ("actual for& cannot be a type with predicate",
13020 Instantiation_Node, A_Gen_T);
13022 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13023 and then Has_Predicates (Act_T)
13024 and then not Has_Static_Predicate_Aspect (Act_T)
13025 then
13026 Error_Msg_NE
13027 ("actual for& cannot be a type with a dynamic predicate",
13028 Instantiation_Node, A_Gen_T);
13029 end if;
13030 end Diagnose_Predicated_Actual;
13032 --------------------
13033 -- Subtypes_Match --
13034 --------------------
13036 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13037 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13039 begin
13040 -- Check that the base types, root types (when dealing with class
13041 -- wide types), or designated types (when dealing with anonymous
13042 -- access types) of Gen_T and Act_T are statically matching subtypes.
13044 return ((Base_Type (T) = Act_T
13045 or else Base_Type (T) = Base_Type (Act_T))
13046 and then Subtypes_Statically_Match (T, Act_T))
13048 or else (Is_Class_Wide_Type (Gen_T)
13049 and then Is_Class_Wide_Type (Act_T)
13050 and then Subtypes_Match
13051 (Get_Instance_Of (Root_Type (Gen_T)),
13052 Root_Type (Act_T)))
13054 or else (Is_Anonymous_Access_Type (Gen_T)
13055 and then Ekind (Act_T) = Ekind (Gen_T)
13056 and then Subtypes_Statically_Match
13057 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13058 end Subtypes_Match;
13060 -----------------------------------------
13061 -- Validate_Access_Subprogram_Instance --
13062 -----------------------------------------
13064 procedure Validate_Access_Subprogram_Instance is
13065 begin
13066 if not Is_Access_Type (Act_T)
13067 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13068 then
13069 Error_Msg_NE
13070 ("expect access type in instantiation of &", Actual, Gen_T);
13071 Abandon_Instantiation (Actual);
13072 end if;
13074 -- According to AI05-288, actuals for access_to_subprograms must be
13075 -- subtype conformant with the generic formal. Previous to AI05-288
13076 -- only mode conformance was required.
13078 -- This is a binding interpretation that applies to previous versions
13079 -- of the language, no need to maintain previous weaker checks.
13081 Check_Subtype_Conformant
13082 (Designated_Type (Act_T),
13083 Designated_Type (A_Gen_T),
13084 Actual,
13085 Get_Inst => True);
13087 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13088 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13089 Error_Msg_NE
13090 ("protected access type not allowed for formal &",
13091 Actual, Gen_T);
13092 end if;
13094 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13095 Error_Msg_NE
13096 ("expect protected access type for formal &",
13097 Actual, Gen_T);
13098 end if;
13100 -- If the formal has a specified convention (which in most cases
13101 -- will be StdCall) verify that the actual has the same convention.
13103 if Has_Convention_Pragma (A_Gen_T)
13104 and then Convention (A_Gen_T) /= Convention (Act_T)
13105 then
13106 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13107 Error_Msg_NE
13108 ("actual for formal & must have convention %", Actual, Gen_T);
13109 end if;
13111 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13112 Error_Msg_NE
13113 ("non null exclusion of actual and formal & do not match",
13114 Actual, Gen_T);
13115 end if;
13116 end Validate_Access_Subprogram_Instance;
13118 -----------------------------------
13119 -- Validate_Access_Type_Instance --
13120 -----------------------------------
13122 procedure Validate_Access_Type_Instance is
13123 Desig_Type : constant Entity_Id :=
13124 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13125 Desig_Act : Entity_Id;
13127 begin
13128 if not Is_Access_Type (Act_T) then
13129 Error_Msg_NE
13130 ("expect access type in instantiation of &", Actual, Gen_T);
13131 Abandon_Instantiation (Actual);
13132 end if;
13134 if Is_Access_Constant (A_Gen_T) then
13135 if not Is_Access_Constant (Act_T) then
13136 Error_Msg_N
13137 ("actual type must be access-to-constant type", Actual);
13138 Abandon_Instantiation (Actual);
13139 end if;
13140 else
13141 if Is_Access_Constant (Act_T) then
13142 Error_Msg_N
13143 ("actual type must be access-to-variable type", Actual);
13144 Abandon_Instantiation (Actual);
13146 elsif Ekind (A_Gen_T) = E_General_Access_Type
13147 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13148 then
13149 Error_Msg_N
13150 ("actual must be general access type!", Actual);
13151 Error_Msg_NE -- CODEFIX
13152 ("\add ALL to }!", Actual, Act_T);
13153 Abandon_Instantiation (Actual);
13154 end if;
13155 end if;
13157 -- The designated subtypes, that is to say the subtypes introduced
13158 -- by an access type declaration (and not by a subtype declaration)
13159 -- must match.
13161 Desig_Act := Designated_Type (Base_Type (Act_T));
13163 -- The designated type may have been introduced through a limited_
13164 -- with clause, in which case retrieve the non-limited view. This
13165 -- applies to incomplete types as well as to class-wide types.
13167 if From_Limited_With (Desig_Act) then
13168 Desig_Act := Available_View (Desig_Act);
13169 end if;
13171 if not Subtypes_Match (Desig_Type, Desig_Act) then
13172 Error_Msg_NE
13173 ("designated type of actual does not match that of formal &",
13174 Actual, Gen_T);
13176 if not Predicates_Match (Desig_Type, Desig_Act) then
13177 Error_Msg_N ("\predicates do not match", Actual);
13178 end if;
13180 Abandon_Instantiation (Actual);
13181 end if;
13183 -- Ada 2005: null-exclusion indicators of the two types must agree
13185 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13186 Error_Msg_NE
13187 ("non null exclusion of actual and formal & do not match",
13188 Actual, Gen_T);
13189 end if;
13190 end Validate_Access_Type_Instance;
13192 ----------------------------------
13193 -- Validate_Array_Type_Instance --
13194 ----------------------------------
13196 procedure Validate_Array_Type_Instance is
13197 I1 : Node_Id;
13198 I2 : Node_Id;
13199 T2 : Entity_Id;
13201 function Formal_Dimensions return Nat;
13202 -- Count number of dimensions in array type formal
13204 -----------------------
13205 -- Formal_Dimensions --
13206 -----------------------
13208 function Formal_Dimensions return Nat is
13209 Num : Nat := 0;
13210 Index : Node_Id;
13212 begin
13213 if Nkind (Def) = N_Constrained_Array_Definition then
13214 Index := First (Discrete_Subtype_Definitions (Def));
13215 else
13216 Index := First (Subtype_Marks (Def));
13217 end if;
13219 while Present (Index) loop
13220 Num := Num + 1;
13221 Next (Index);
13222 end loop;
13224 return Num;
13225 end Formal_Dimensions;
13227 -- Start of processing for Validate_Array_Type_Instance
13229 begin
13230 if not Is_Array_Type (Act_T) then
13231 Error_Msg_NE
13232 ("expect array type in instantiation of &", Actual, Gen_T);
13233 Abandon_Instantiation (Actual);
13235 elsif Nkind (Def) = N_Constrained_Array_Definition then
13236 if not (Is_Constrained (Act_T)) then
13237 Error_Msg_NE
13238 ("expect constrained array in instantiation of &",
13239 Actual, Gen_T);
13240 Abandon_Instantiation (Actual);
13241 end if;
13243 else
13244 if Is_Constrained (Act_T) then
13245 Error_Msg_NE
13246 ("expect unconstrained array in instantiation of &",
13247 Actual, Gen_T);
13248 Abandon_Instantiation (Actual);
13249 end if;
13250 end if;
13252 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13253 Error_Msg_NE
13254 ("dimensions of actual do not match formal &", Actual, Gen_T);
13255 Abandon_Instantiation (Actual);
13256 end if;
13258 I1 := First_Index (A_Gen_T);
13259 I2 := First_Index (Act_T);
13260 for J in 1 .. Formal_Dimensions loop
13262 -- If the indexes of the actual were given by a subtype_mark,
13263 -- the index was transformed into a range attribute. Retrieve
13264 -- the original type mark for checking.
13266 if Is_Entity_Name (Original_Node (I2)) then
13267 T2 := Entity (Original_Node (I2));
13268 else
13269 T2 := Etype (I2);
13270 end if;
13272 if not Subtypes_Match
13273 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13274 then
13275 Error_Msg_NE
13276 ("index types of actual do not match those of formal &",
13277 Actual, Gen_T);
13278 Abandon_Instantiation (Actual);
13279 end if;
13281 Next_Index (I1);
13282 Next_Index (I2);
13283 end loop;
13285 -- Check matching subtypes. Note that there are complex visibility
13286 -- issues when the generic is a child unit and some aspect of the
13287 -- generic type is declared in a parent unit of the generic. We do
13288 -- the test to handle this special case only after a direct check
13289 -- for static matching has failed. The case where both the component
13290 -- type and the array type are separate formals, and the component
13291 -- type is a private view may also require special checking in
13292 -- Subtypes_Match. Finally, we assume that a child instance where
13293 -- the component type comes from a formal of a parent instance is
13294 -- correct because the generic was correct. A more precise check
13295 -- seems too complex to install???
13297 if Subtypes_Match
13298 (Component_Type (A_Gen_T), Component_Type (Act_T))
13299 or else
13300 Subtypes_Match
13301 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13302 Component_Type (Act_T))
13303 or else
13304 (not Inside_A_Generic
13305 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13306 then
13307 null;
13308 else
13309 Error_Msg_NE
13310 ("component subtype of actual does not match that of formal &",
13311 Actual, Gen_T);
13312 Abandon_Instantiation (Actual);
13313 end if;
13315 if Has_Aliased_Components (A_Gen_T)
13316 and then not Has_Aliased_Components (Act_T)
13317 then
13318 Error_Msg_NE
13319 ("actual must have aliased components to match formal type &",
13320 Actual, Gen_T);
13321 end if;
13322 end Validate_Array_Type_Instance;
13324 -----------------------------------------------
13325 -- Validate_Derived_Interface_Type_Instance --
13326 -----------------------------------------------
13328 procedure Validate_Derived_Interface_Type_Instance is
13329 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13330 Elmt : Elmt_Id;
13332 begin
13333 -- First apply interface instance checks
13335 Validate_Interface_Type_Instance;
13337 -- Verify that immediate parent interface is an ancestor of
13338 -- the actual.
13340 if Present (Par)
13341 and then not Interface_Present_In_Ancestor (Act_T, Par)
13342 then
13343 Error_Msg_NE
13344 ("interface actual must include progenitor&", Actual, Par);
13345 end if;
13347 -- Now verify that the actual includes all other ancestors of
13348 -- the formal.
13350 Elmt := First_Elmt (Interfaces (A_Gen_T));
13351 while Present (Elmt) loop
13352 if not Interface_Present_In_Ancestor
13353 (Act_T, Get_Instance_Of (Node (Elmt)))
13354 then
13355 Error_Msg_NE
13356 ("interface actual must include progenitor&",
13357 Actual, Node (Elmt));
13358 end if;
13360 Next_Elmt (Elmt);
13361 end loop;
13362 end Validate_Derived_Interface_Type_Instance;
13364 ------------------------------------
13365 -- Validate_Derived_Type_Instance --
13366 ------------------------------------
13368 procedure Validate_Derived_Type_Instance is
13369 Actual_Discr : Entity_Id;
13370 Ancestor_Discr : Entity_Id;
13372 begin
13373 -- Verify that the actual includes the progenitors of the formal,
13374 -- if any. The formal may depend on previous formals and their
13375 -- instance, so we must examine instance of interfaces if present.
13376 -- The actual may be an extension of an interface, in which case
13377 -- it does not appear in the interface list, so this must be
13378 -- checked separately.
13380 if Present (Interface_List (Def)) then
13381 if not Has_Interfaces (Act_T) then
13382 Error_Msg_NE
13383 ("actual must implement all interfaces of formal&",
13384 Actual, A_Gen_T);
13386 else
13387 declare
13388 Act_Iface_List : Elist_Id;
13389 Iface : Node_Id;
13390 Iface_Ent : Entity_Id;
13392 function Instance_Exists (I : Entity_Id) return Boolean;
13393 -- If the interface entity is declared in a generic unit,
13394 -- this can only be legal if we are within an instantiation
13395 -- of a child of that generic. There is currently no
13396 -- mechanism to relate an interface declared within a
13397 -- generic to the corresponding interface in an instance,
13398 -- so we traverse the list of interfaces of the actual,
13399 -- looking for a name match.
13401 ---------------------
13402 -- Instance_Exists --
13403 ---------------------
13405 function Instance_Exists (I : Entity_Id) return Boolean is
13406 Iface_Elmt : Elmt_Id;
13408 begin
13409 Iface_Elmt := First_Elmt (Act_Iface_List);
13410 while Present (Iface_Elmt) loop
13411 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13412 and then Chars (Node (Iface_Elmt)) = Chars (I)
13413 then
13414 return True;
13415 end if;
13417 Next_Elmt (Iface_Elmt);
13418 end loop;
13420 return False;
13421 end Instance_Exists;
13423 begin
13424 Iface := First (Abstract_Interface_List (A_Gen_T));
13425 Collect_Interfaces (Act_T, Act_Iface_List);
13427 while Present (Iface) loop
13428 Iface_Ent := Get_Instance_Of (Entity (Iface));
13430 if Is_Ancestor (Iface_Ent, Act_T)
13431 or else Is_Progenitor (Iface_Ent, Act_T)
13432 then
13433 null;
13435 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13436 and then Instance_Exists (Iface_Ent)
13437 then
13438 null;
13440 else
13441 Error_Msg_Name_1 := Chars (Act_T);
13442 Error_Msg_NE
13443 ("actual% must implement interface&",
13444 Actual, Etype (Iface));
13445 end if;
13447 Next (Iface);
13448 end loop;
13449 end;
13450 end if;
13451 end if;
13453 -- If the parent type in the generic declaration is itself a previous
13454 -- formal type, then it is local to the generic and absent from the
13455 -- analyzed generic definition. In that case the ancestor is the
13456 -- instance of the formal (which must have been instantiated
13457 -- previously), unless the ancestor is itself a formal derived type.
13458 -- In this latter case (which is the subject of Corrigendum 8652/0038
13459 -- (AI-202) the ancestor of the formals is the ancestor of its
13460 -- parent. Otherwise, the analyzed generic carries the parent type.
13461 -- If the parent type is defined in a previous formal package, then
13462 -- the scope of that formal package is that of the generic type
13463 -- itself, and it has already been mapped into the corresponding type
13464 -- in the actual package.
13466 -- Common case: parent type defined outside of the generic
13468 if Is_Entity_Name (Subtype_Mark (Def))
13469 and then Present (Entity (Subtype_Mark (Def)))
13470 then
13471 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13473 -- Check whether parent is defined in a previous formal package
13475 elsif
13476 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13477 then
13478 Ancestor :=
13479 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13481 -- The type may be a local derivation, or a type extension of a
13482 -- previous formal, or of a formal of a parent package.
13484 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13485 or else
13486 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13487 then
13488 -- Check whether the parent is another derived formal type in the
13489 -- same generic unit.
13491 if Etype (A_Gen_T) /= A_Gen_T
13492 and then Is_Generic_Type (Etype (A_Gen_T))
13493 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13494 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13495 then
13496 -- Locate ancestor of parent from the subtype declaration
13497 -- created for the actual.
13499 declare
13500 Decl : Node_Id;
13502 begin
13503 Decl := First (Actual_Decls);
13504 while Present (Decl) loop
13505 if Nkind (Decl) = N_Subtype_Declaration
13506 and then Chars (Defining_Identifier (Decl)) =
13507 Chars (Etype (A_Gen_T))
13508 then
13509 Ancestor := Generic_Parent_Type (Decl);
13510 exit;
13511 else
13512 Next (Decl);
13513 end if;
13514 end loop;
13515 end;
13517 pragma Assert (Present (Ancestor));
13519 -- The ancestor itself may be a previous formal that has been
13520 -- instantiated.
13522 Ancestor := Get_Instance_Of (Ancestor);
13524 else
13525 Ancestor :=
13526 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13527 end if;
13529 -- Check whether parent is a previous formal of the current generic
13531 elsif Is_Derived_Type (A_Gen_T)
13532 and then Is_Generic_Type (Etype (A_Gen_T))
13533 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13534 then
13535 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13537 -- An unusual case: the actual is a type declared in a parent unit,
13538 -- but is not a formal type so there is no instance_of for it.
13539 -- Retrieve it by analyzing the record extension.
13541 elsif Is_Child_Unit (Scope (A_Gen_T))
13542 and then In_Open_Scopes (Scope (Act_T))
13543 and then Is_Generic_Instance (Scope (Act_T))
13544 then
13545 Analyze (Subtype_Mark (Def));
13546 Ancestor := Entity (Subtype_Mark (Def));
13548 else
13549 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13550 end if;
13552 -- If the formal derived type has pragma Preelaborable_Initialization
13553 -- then the actual type must have preelaborable initialization.
13555 if Known_To_Have_Preelab_Init (A_Gen_T)
13556 and then not Has_Preelaborable_Initialization (Act_T)
13557 then
13558 Error_Msg_NE
13559 ("actual for & must have preelaborable initialization",
13560 Actual, Gen_T);
13561 end if;
13563 -- Ada 2005 (AI-251)
13565 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13566 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13567 Error_Msg_NE
13568 ("(Ada 2005) expected type implementing & in instantiation",
13569 Actual, Ancestor);
13570 end if;
13572 -- Finally verify that the (instance of) the ancestor is an ancestor
13573 -- of the actual.
13575 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13576 Error_Msg_NE
13577 ("expect type derived from & in instantiation",
13578 Actual, First_Subtype (Ancestor));
13579 Abandon_Instantiation (Actual);
13580 end if;
13582 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13583 -- that the formal type declaration has been rewritten as a private
13584 -- extension.
13586 if Ada_Version >= Ada_2005
13587 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13588 and then Synchronized_Present (Parent (A_Gen_T))
13589 then
13590 -- The actual must be a synchronized tagged type
13592 if not Is_Tagged_Type (Act_T) then
13593 Error_Msg_N
13594 ("actual of synchronized type must be tagged", Actual);
13595 Abandon_Instantiation (Actual);
13597 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13598 and then Nkind (Type_Definition (Parent (Act_T))) =
13599 N_Derived_Type_Definition
13600 and then not Synchronized_Present
13601 (Type_Definition (Parent (Act_T)))
13602 then
13603 Error_Msg_N
13604 ("actual of synchronized type must be synchronized", Actual);
13605 Abandon_Instantiation (Actual);
13606 end if;
13607 end if;
13609 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13610 -- removes the second instance of the phrase "or allow pass by copy".
13612 -- For Ada 2022, the aspect may be specified explicitly for the
13613 -- formal regardless of whether an ancestor obeys it.
13615 if Is_Atomic (Act_T)
13616 and then not Is_Atomic (Ancestor)
13617 and then not Is_Atomic (A_Gen_T)
13618 then
13619 Error_Msg_N
13620 ("cannot have atomic actual type for non-atomic formal type",
13621 Actual);
13623 elsif Is_Volatile (Act_T)
13624 and then not Is_Volatile (Ancestor)
13625 and then not Is_Volatile (A_Gen_T)
13626 then
13627 Error_Msg_N
13628 ("cannot have volatile actual type for non-volatile formal type",
13629 Actual);
13630 end if;
13632 -- It should not be necessary to check for unknown discriminants on
13633 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13634 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13635 -- needs fixing. ???
13637 if Is_Definite_Subtype (A_Gen_T)
13638 and then not Unknown_Discriminants_Present (Formal)
13639 and then not Is_Definite_Subtype (Act_T)
13640 then
13641 Error_Msg_N ("actual subtype must be constrained", Actual);
13642 Abandon_Instantiation (Actual);
13643 end if;
13645 if not Unknown_Discriminants_Present (Formal) then
13646 if Is_Constrained (Ancestor) then
13647 if not Is_Constrained (Act_T) then
13648 Error_Msg_N ("actual subtype must be constrained", Actual);
13649 Abandon_Instantiation (Actual);
13650 end if;
13652 -- Ancestor is unconstrained, Check if generic formal and actual
13653 -- agree on constrainedness. The check only applies to array types
13654 -- and discriminated types.
13656 elsif Is_Constrained (Act_T) then
13657 if Ekind (Ancestor) = E_Access_Type
13658 or else (not Is_Constrained (A_Gen_T)
13659 and then Is_Composite_Type (A_Gen_T))
13660 then
13661 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13662 Abandon_Instantiation (Actual);
13663 end if;
13665 -- A class-wide type is only allowed if the formal has unknown
13666 -- discriminants.
13668 elsif Is_Class_Wide_Type (Act_T)
13669 and then not Has_Unknown_Discriminants (Ancestor)
13670 then
13671 Error_Msg_NE
13672 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13673 Abandon_Instantiation (Actual);
13675 -- Otherwise, the formal and actual must have the same number
13676 -- of discriminants and each discriminant of the actual must
13677 -- correspond to a discriminant of the formal.
13679 elsif Has_Discriminants (Act_T)
13680 and then not Has_Unknown_Discriminants (Act_T)
13681 and then Has_Discriminants (Ancestor)
13682 then
13683 Actual_Discr := First_Discriminant (Act_T);
13684 Ancestor_Discr := First_Discriminant (Ancestor);
13685 while Present (Actual_Discr)
13686 and then Present (Ancestor_Discr)
13687 loop
13688 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13689 No (Corresponding_Discriminant (Actual_Discr))
13690 then
13691 Error_Msg_NE
13692 ("discriminant & does not correspond "
13693 & "to ancestor discriminant", Actual, Actual_Discr);
13694 Abandon_Instantiation (Actual);
13695 end if;
13697 Next_Discriminant (Actual_Discr);
13698 Next_Discriminant (Ancestor_Discr);
13699 end loop;
13701 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13702 Error_Msg_NE
13703 ("actual for & must have same number of discriminants",
13704 Actual, Gen_T);
13705 Abandon_Instantiation (Actual);
13706 end if;
13708 -- This case should be caught by the earlier check for
13709 -- constrainedness, but the check here is added for completeness.
13711 elsif Has_Discriminants (Act_T)
13712 and then not Has_Unknown_Discriminants (Act_T)
13713 then
13714 Error_Msg_NE
13715 ("actual for & must not have discriminants", Actual, Gen_T);
13716 Abandon_Instantiation (Actual);
13718 elsif Has_Discriminants (Ancestor) then
13719 Error_Msg_NE
13720 ("actual for & must have known discriminants", Actual, Gen_T);
13721 Abandon_Instantiation (Actual);
13722 end if;
13724 if not Subtypes_Statically_Compatible
13725 (Act_T, Ancestor, Formal_Derived_Matching => True)
13726 then
13727 Error_Msg_NE
13728 ("actual for & must be statically compatible with ancestor",
13729 Actual, Gen_T);
13731 if not Predicates_Compatible (Act_T, Ancestor) then
13732 Error_Msg_N
13733 ("\predicate on actual is not compatible with ancestor",
13734 Actual);
13735 end if;
13737 Abandon_Instantiation (Actual);
13738 end if;
13739 end if;
13741 -- If the formal and actual types are abstract, check that there
13742 -- are no abstract primitives of the actual type that correspond to
13743 -- nonabstract primitives of the formal type (second sentence of
13744 -- RM95 3.9.3(9)).
13746 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13747 Check_Abstract_Primitives : declare
13748 Gen_Prims : constant Elist_Id :=
13749 Primitive_Operations (A_Gen_T);
13750 Gen_Elmt : Elmt_Id;
13751 Gen_Subp : Entity_Id;
13752 Anc_Subp : Entity_Id;
13753 Anc_Formal : Entity_Id;
13754 Anc_F_Type : Entity_Id;
13756 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13757 Act_Elmt : Elmt_Id;
13758 Act_Subp : Entity_Id;
13759 Act_Formal : Entity_Id;
13760 Act_F_Type : Entity_Id;
13762 Subprograms_Correspond : Boolean;
13764 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13765 -- Returns true if T2 is derived directly or indirectly from
13766 -- T1, including derivations from interfaces. T1 and T2 are
13767 -- required to be specific tagged base types.
13769 ------------------------
13770 -- Is_Tagged_Ancestor --
13771 ------------------------
13773 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13775 Intfc_Elmt : Elmt_Id;
13777 begin
13778 -- The predicate is satisfied if the types are the same
13780 if T1 = T2 then
13781 return True;
13783 -- If we've reached the top of the derivation chain then
13784 -- we know that T1 is not an ancestor of T2.
13786 elsif Etype (T2) = T2 then
13787 return False;
13789 -- Proceed to check T2's immediate parent
13791 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13792 return True;
13794 -- Finally, check to see if T1 is an ancestor of any of T2's
13795 -- progenitors.
13797 else
13798 Intfc_Elmt := First_Elmt (Interfaces (T2));
13799 while Present (Intfc_Elmt) loop
13800 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13801 return True;
13802 end if;
13804 Next_Elmt (Intfc_Elmt);
13805 end loop;
13806 end if;
13808 return False;
13809 end Is_Tagged_Ancestor;
13811 -- Start of processing for Check_Abstract_Primitives
13813 begin
13814 -- Loop over all of the formal derived type's primitives
13816 Gen_Elmt := First_Elmt (Gen_Prims);
13817 while Present (Gen_Elmt) loop
13818 Gen_Subp := Node (Gen_Elmt);
13820 -- If the primitive of the formal is not abstract, then
13821 -- determine whether there is a corresponding primitive of
13822 -- the actual type that's abstract.
13824 if not Is_Abstract_Subprogram (Gen_Subp) then
13825 Act_Elmt := First_Elmt (Act_Prims);
13826 while Present (Act_Elmt) loop
13827 Act_Subp := Node (Act_Elmt);
13829 -- If we find an abstract primitive of the actual,
13830 -- then we need to test whether it corresponds to the
13831 -- subprogram from which the generic formal primitive
13832 -- is inherited.
13834 if Is_Abstract_Subprogram (Act_Subp) then
13835 Anc_Subp := Alias (Gen_Subp);
13837 -- Test whether we have a corresponding primitive
13838 -- by comparing names, kinds, formal types, and
13839 -- result types.
13841 if Chars (Anc_Subp) = Chars (Act_Subp)
13842 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13843 then
13844 Anc_Formal := First_Formal (Anc_Subp);
13845 Act_Formal := First_Formal (Act_Subp);
13846 while Present (Anc_Formal)
13847 and then Present (Act_Formal)
13848 loop
13849 Anc_F_Type := Etype (Anc_Formal);
13850 Act_F_Type := Etype (Act_Formal);
13852 if Ekind (Anc_F_Type) =
13853 E_Anonymous_Access_Type
13854 then
13855 Anc_F_Type := Designated_Type (Anc_F_Type);
13857 if Ekind (Act_F_Type) =
13858 E_Anonymous_Access_Type
13859 then
13860 Act_F_Type :=
13861 Designated_Type (Act_F_Type);
13862 else
13863 exit;
13864 end if;
13866 elsif
13867 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13868 then
13869 exit;
13870 end if;
13872 Anc_F_Type := Base_Type (Anc_F_Type);
13873 Act_F_Type := Base_Type (Act_F_Type);
13875 -- If the formal is controlling, then the
13876 -- the type of the actual primitive's formal
13877 -- must be derived directly or indirectly
13878 -- from the type of the ancestor primitive's
13879 -- formal.
13881 if Is_Controlling_Formal (Anc_Formal) then
13882 if not Is_Tagged_Ancestor
13883 (Anc_F_Type, Act_F_Type)
13884 then
13885 exit;
13886 end if;
13888 -- Otherwise the types of the formals must
13889 -- be the same.
13891 elsif Anc_F_Type /= Act_F_Type then
13892 exit;
13893 end if;
13895 Next_Formal (Anc_Formal);
13896 Next_Formal (Act_Formal);
13897 end loop;
13899 -- If we traversed through all of the formals
13900 -- then so far the subprograms correspond, so
13901 -- now check that any result types correspond.
13903 if No (Anc_Formal) and then No (Act_Formal) then
13904 Subprograms_Correspond := True;
13906 if Ekind (Act_Subp) = E_Function then
13907 Anc_F_Type := Etype (Anc_Subp);
13908 Act_F_Type := Etype (Act_Subp);
13910 if Ekind (Anc_F_Type) =
13911 E_Anonymous_Access_Type
13912 then
13913 Anc_F_Type :=
13914 Designated_Type (Anc_F_Type);
13916 if Ekind (Act_F_Type) =
13917 E_Anonymous_Access_Type
13918 then
13919 Act_F_Type :=
13920 Designated_Type (Act_F_Type);
13921 else
13922 Subprograms_Correspond := False;
13923 end if;
13925 elsif
13926 Ekind (Act_F_Type)
13927 = E_Anonymous_Access_Type
13928 then
13929 Subprograms_Correspond := False;
13930 end if;
13932 Anc_F_Type := Base_Type (Anc_F_Type);
13933 Act_F_Type := Base_Type (Act_F_Type);
13935 -- Now either the result types must be
13936 -- the same or, if the result type is
13937 -- controlling, the result type of the
13938 -- actual primitive must descend from the
13939 -- result type of the ancestor primitive.
13941 if Subprograms_Correspond
13942 and then Anc_F_Type /= Act_F_Type
13943 and then
13944 Has_Controlling_Result (Anc_Subp)
13945 and then not Is_Tagged_Ancestor
13946 (Anc_F_Type, Act_F_Type)
13947 then
13948 Subprograms_Correspond := False;
13949 end if;
13950 end if;
13952 -- Found a matching subprogram belonging to
13953 -- formal ancestor type, so actual subprogram
13954 -- corresponds and this violates 3.9.3(9).
13956 if Subprograms_Correspond then
13957 Error_Msg_NE
13958 ("abstract subprogram & overrides "
13959 & "nonabstract subprogram of ancestor",
13960 Actual, Act_Subp);
13961 end if;
13962 end if;
13963 end if;
13964 end if;
13966 Next_Elmt (Act_Elmt);
13967 end loop;
13968 end if;
13970 Next_Elmt (Gen_Elmt);
13971 end loop;
13972 end Check_Abstract_Primitives;
13973 end if;
13975 -- Verify that limitedness matches. If parent is a limited
13976 -- interface then the generic formal is not unless declared
13977 -- explicitly so. If not declared limited, the actual cannot be
13978 -- limited (see AI05-0087).
13980 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13981 if not In_Instance then
13982 Error_Msg_NE
13983 ("actual for non-limited & cannot be a limited type",
13984 Actual, Gen_T);
13985 Explain_Limited_Type (Act_T, Actual);
13986 Abandon_Instantiation (Actual);
13987 end if;
13988 end if;
13990 -- Check for AI12-0036
13992 declare
13993 Formal_Is_Private_Extension : constant Boolean :=
13994 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13996 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13998 begin
13999 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
14000 if not In_Instance then
14001 if Actual_Is_Tagged then
14002 Error_Msg_NE
14003 ("actual for & cannot be a tagged type", Actual, Gen_T);
14004 else
14005 Error_Msg_NE
14006 ("actual for & must be a tagged type", Actual, Gen_T);
14007 end if;
14009 Abandon_Instantiation (Actual);
14010 end if;
14011 end if;
14012 end;
14013 end Validate_Derived_Type_Instance;
14015 ----------------------------------------
14016 -- Validate_Discriminated_Formal_Type --
14017 ----------------------------------------
14019 procedure Validate_Discriminated_Formal_Type is
14020 Formal_Discr : Entity_Id;
14021 Actual_Discr : Entity_Id;
14022 Formal_Subt : Entity_Id;
14024 begin
14025 if Has_Discriminants (A_Gen_T) then
14026 if not Has_Discriminants (Act_T) then
14027 Error_Msg_NE
14028 ("actual for & must have discriminants", Actual, Gen_T);
14029 Abandon_Instantiation (Actual);
14031 elsif Is_Constrained (Act_T) then
14032 Error_Msg_NE
14033 ("actual for & must be unconstrained", Actual, Gen_T);
14034 Abandon_Instantiation (Actual);
14036 else
14037 Formal_Discr := First_Discriminant (A_Gen_T);
14038 Actual_Discr := First_Discriminant (Act_T);
14039 while Formal_Discr /= Empty loop
14040 if Actual_Discr = Empty then
14041 Error_Msg_N
14042 ("discriminants on actual do not match formal",
14043 Actual);
14044 Abandon_Instantiation (Actual);
14045 end if;
14047 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14049 -- Access discriminants match if designated types do
14051 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14052 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14053 E_Anonymous_Access_Type
14054 and then
14055 Subtypes_Match
14056 (Get_Instance_Of
14057 (Designated_Type (Base_Type (Formal_Subt))),
14058 Designated_Type (Base_Type (Etype (Actual_Discr))))
14059 then
14060 null;
14062 elsif Base_Type (Formal_Subt) /=
14063 Base_Type (Etype (Actual_Discr))
14064 then
14065 Error_Msg_N
14066 ("types of actual discriminants must match formal",
14067 Actual);
14068 Abandon_Instantiation (Actual);
14070 elsif not Subtypes_Statically_Match
14071 (Formal_Subt, Etype (Actual_Discr))
14072 and then Ada_Version >= Ada_95
14073 then
14074 Error_Msg_N
14075 ("subtypes of actual discriminants must match formal",
14076 Actual);
14077 Abandon_Instantiation (Actual);
14078 end if;
14080 Next_Discriminant (Formal_Discr);
14081 Next_Discriminant (Actual_Discr);
14082 end loop;
14084 if Actual_Discr /= Empty then
14085 Error_Msg_NE
14086 ("discriminants on actual do not match formal",
14087 Actual, Gen_T);
14088 Abandon_Instantiation (Actual);
14089 end if;
14090 end if;
14091 end if;
14092 end Validate_Discriminated_Formal_Type;
14094 ---------------------------------------
14095 -- Validate_Incomplete_Type_Instance --
14096 ---------------------------------------
14098 procedure Validate_Incomplete_Type_Instance is
14099 begin
14100 if not Is_Tagged_Type (Act_T)
14101 and then Is_Tagged_Type (A_Gen_T)
14102 then
14103 Error_Msg_NE
14104 ("actual for & must be a tagged type", Actual, Gen_T);
14105 end if;
14107 Validate_Discriminated_Formal_Type;
14108 end Validate_Incomplete_Type_Instance;
14110 --------------------------------------
14111 -- Validate_Interface_Type_Instance --
14112 --------------------------------------
14114 procedure Validate_Interface_Type_Instance is
14115 begin
14116 if not Is_Interface (Act_T) then
14117 Error_Msg_NE
14118 ("actual for formal interface type must be an interface",
14119 Actual, Gen_T);
14121 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14122 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14123 or else Is_Protected_Interface (A_Gen_T) /=
14124 Is_Protected_Interface (Act_T)
14125 or else Is_Synchronized_Interface (A_Gen_T) /=
14126 Is_Synchronized_Interface (Act_T)
14127 then
14128 Error_Msg_NE
14129 ("actual for interface& does not match (RM 12.5.5(4))",
14130 Actual, Gen_T);
14131 end if;
14132 end Validate_Interface_Type_Instance;
14134 ------------------------------------
14135 -- Validate_Private_Type_Instance --
14136 ------------------------------------
14138 procedure Validate_Private_Type_Instance is
14139 begin
14140 if Is_Limited_Type (Act_T)
14141 and then not Is_Limited_Type (A_Gen_T)
14142 then
14143 if In_Instance then
14144 null;
14145 else
14146 Error_Msg_NE
14147 ("actual for non-limited & cannot be a limited type", Actual,
14148 Gen_T);
14149 Explain_Limited_Type (Act_T, Actual);
14150 Abandon_Instantiation (Actual);
14151 end if;
14153 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14154 and then not Has_Preelaborable_Initialization (Act_T)
14155 then
14156 Error_Msg_NE
14157 ("actual for & must have preelaborable initialization", Actual,
14158 Gen_T);
14160 elsif not Is_Definite_Subtype (Act_T)
14161 and then Is_Definite_Subtype (A_Gen_T)
14162 and then Ada_Version >= Ada_95
14163 then
14164 Error_Msg_NE
14165 ("actual for & must be a definite subtype", Actual, Gen_T);
14167 elsif not Is_Tagged_Type (Act_T)
14168 and then Is_Tagged_Type (A_Gen_T)
14169 then
14170 Error_Msg_NE
14171 ("actual for & must be a tagged type", Actual, Gen_T);
14172 end if;
14174 Validate_Discriminated_Formal_Type;
14175 Ancestor := Gen_T;
14176 end Validate_Private_Type_Instance;
14178 -- Start of processing for Instantiate_Type
14180 begin
14181 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14182 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14183 return New_List (Error);
14185 elsif not Is_Entity_Name (Actual)
14186 or else not Is_Type (Entity (Actual))
14187 then
14188 Error_Msg_NE
14189 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14190 Abandon_Instantiation (Actual);
14192 else
14193 Act_T := Entity (Actual);
14195 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14196 -- as a generic actual parameter if the corresponding formal type
14197 -- does not have a known_discriminant_part, or is a formal derived
14198 -- type that is an Unchecked_Union type.
14200 if Is_Unchecked_Union (Base_Type (Act_T)) then
14201 if not Has_Discriminants (A_Gen_T)
14202 or else (Is_Derived_Type (A_Gen_T)
14203 and then Is_Unchecked_Union (A_Gen_T))
14204 then
14205 null;
14206 else
14207 Error_Msg_N ("unchecked union cannot be the actual for a "
14208 & "discriminated formal type", Act_T);
14210 end if;
14211 end if;
14213 -- Deal with fixed/floating restrictions
14215 if Is_Floating_Point_Type (Act_T) then
14216 Check_Restriction (No_Floating_Point, Actual);
14217 elsif Is_Fixed_Point_Type (Act_T) then
14218 Check_Restriction (No_Fixed_Point, Actual);
14219 end if;
14221 -- Deal with error of using incomplete type as generic actual.
14222 -- This includes limited views of a type, even if the non-limited
14223 -- view may be available.
14225 if Ekind (Act_T) = E_Incomplete_Type
14226 or else (Is_Class_Wide_Type (Act_T)
14227 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14228 then
14229 -- If the formal is an incomplete type, the actual can be
14230 -- incomplete as well, but if an actual incomplete type has
14231 -- a full view, then we'll retrieve that.
14233 if Ekind (A_Gen_T) = E_Incomplete_Type
14234 and then No (Full_View (Act_T))
14235 then
14236 null;
14238 elsif Is_Class_Wide_Type (Act_T)
14239 or else No (Full_View (Act_T))
14240 then
14241 Error_Msg_N ("premature use of incomplete type", Actual);
14242 Abandon_Instantiation (Actual);
14244 else
14245 Act_T := Full_View (Act_T);
14246 Set_Entity (Actual, Act_T);
14248 if Has_Private_Component (Act_T) then
14249 Error_Msg_N
14250 ("premature use of type with private component", Actual);
14251 end if;
14252 end if;
14254 -- Deal with error of premature use of private type as generic actual
14256 elsif Is_Private_Type (Act_T)
14257 and then Is_Private_Type (Base_Type (Act_T))
14258 and then not Is_Generic_Type (Act_T)
14259 and then not Is_Derived_Type (Act_T)
14260 and then No (Full_View (Root_Type (Act_T)))
14261 then
14262 -- If the formal is an incomplete type, the actual can be
14263 -- private or incomplete as well.
14265 if Ekind (A_Gen_T) = E_Incomplete_Type then
14266 null;
14267 else
14268 Error_Msg_N ("premature use of private type", Actual);
14269 end if;
14271 elsif Has_Private_Component (Act_T) then
14272 Error_Msg_N
14273 ("premature use of type with private component", Actual);
14274 end if;
14276 Set_Instance_Of (A_Gen_T, Act_T);
14278 -- If the type is generic, the class-wide type may also be used
14280 if Is_Tagged_Type (A_Gen_T)
14281 and then Is_Tagged_Type (Act_T)
14282 and then not Is_Class_Wide_Type (A_Gen_T)
14283 then
14284 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14285 Class_Wide_Type (Act_T));
14286 end if;
14288 if not Is_Abstract_Type (A_Gen_T)
14289 and then Is_Abstract_Type (Act_T)
14290 then
14291 Error_Msg_N
14292 ("actual of non-abstract formal cannot be abstract", Actual);
14293 end if;
14295 -- A generic scalar type is a first subtype for which we generate
14296 -- an anonymous base type. Indicate that the instance of this base
14297 -- is the base type of the actual.
14299 if Is_Scalar_Type (A_Gen_T) then
14300 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14301 end if;
14302 end if;
14304 Check_Shared_Variable_Control_Aspects;
14306 if Error_Posted (Act_T) then
14307 null;
14308 else
14309 case Nkind (Def) is
14310 when N_Formal_Private_Type_Definition =>
14311 Validate_Private_Type_Instance;
14313 when N_Formal_Incomplete_Type_Definition =>
14314 Validate_Incomplete_Type_Instance;
14316 when N_Formal_Derived_Type_Definition =>
14317 Validate_Derived_Type_Instance;
14319 when N_Formal_Discrete_Type_Definition =>
14320 if not Is_Discrete_Type (Act_T) then
14321 Error_Msg_NE
14322 ("expect discrete type in instantiation of&",
14323 Actual, Gen_T);
14324 Abandon_Instantiation (Actual);
14325 end if;
14327 Diagnose_Predicated_Actual;
14329 when N_Formal_Signed_Integer_Type_Definition =>
14330 if not Is_Signed_Integer_Type (Act_T) then
14331 Error_Msg_NE
14332 ("expect signed integer type in instantiation of&",
14333 Actual, Gen_T);
14334 Abandon_Instantiation (Actual);
14335 end if;
14337 Diagnose_Predicated_Actual;
14339 when N_Formal_Modular_Type_Definition =>
14340 if not Is_Modular_Integer_Type (Act_T) then
14341 Error_Msg_NE
14342 ("expect modular type in instantiation of &",
14343 Actual, Gen_T);
14344 Abandon_Instantiation (Actual);
14345 end if;
14347 Diagnose_Predicated_Actual;
14349 when N_Formal_Floating_Point_Definition =>
14350 if not Is_Floating_Point_Type (Act_T) then
14351 Error_Msg_NE
14352 ("expect float type in instantiation of &", Actual, Gen_T);
14353 Abandon_Instantiation (Actual);
14354 end if;
14356 when N_Formal_Ordinary_Fixed_Point_Definition =>
14357 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14358 Error_Msg_NE
14359 ("expect ordinary fixed point type in instantiation of &",
14360 Actual, Gen_T);
14361 Abandon_Instantiation (Actual);
14362 end if;
14364 when N_Formal_Decimal_Fixed_Point_Definition =>
14365 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14366 Error_Msg_NE
14367 ("expect decimal type in instantiation of &",
14368 Actual, Gen_T);
14369 Abandon_Instantiation (Actual);
14370 end if;
14372 when N_Array_Type_Definition =>
14373 Validate_Array_Type_Instance;
14375 when N_Access_To_Object_Definition =>
14376 Validate_Access_Type_Instance;
14378 when N_Access_Function_Definition
14379 | N_Access_Procedure_Definition
14381 Validate_Access_Subprogram_Instance;
14383 when N_Record_Definition =>
14384 Validate_Interface_Type_Instance;
14386 when N_Derived_Type_Definition =>
14387 Validate_Derived_Interface_Type_Instance;
14389 when others =>
14390 raise Program_Error;
14391 end case;
14392 end if;
14394 Subt := New_Copy (Gen_T);
14396 -- Use adjusted sloc of subtype name as the location for other nodes in
14397 -- the subtype declaration.
14399 Loc := Sloc (Subt);
14401 Decl_Node :=
14402 Make_Subtype_Declaration (Loc,
14403 Defining_Identifier => Subt,
14404 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14406 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14408 -- Record whether the actual is private at this point, so that
14409 -- Check_Generic_Actuals can restore its proper view before the
14410 -- semantic analysis of the instance.
14412 if Is_Private_Type (Act_T) then
14413 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14415 elsif (Is_Access_Type (Act_T)
14416 and then Is_Private_Type (Designated_Type (Act_T)))
14417 or else (Is_Array_Type (Act_T)
14418 and then
14419 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
14420 then
14421 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
14422 end if;
14424 -- In Ada 2012 the actual may be a limited view. Indicate that
14425 -- the local subtype must be treated as such.
14427 if From_Limited_With (Act_T) then
14428 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14429 Set_From_Limited_With (Subt);
14430 end if;
14432 Decl_Nodes := New_List (Decl_Node);
14434 -- Flag actual derived types so their elaboration produces the
14435 -- appropriate renamings for the primitive operations of the ancestor.
14436 -- Flag actual for formal private types as well, to determine whether
14437 -- operations in the private part may override inherited operations.
14438 -- If the formal has an interface list, the ancestor is not the
14439 -- parent, but the analyzed formal that includes the interface
14440 -- operations of all its progenitors.
14442 -- Same treatment for formal private types, so we can check whether the
14443 -- type is tagged limited when validating derivations in the private
14444 -- part. (See AI05-096).
14446 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14447 if Present (Interface_List (Def)) then
14448 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14449 else
14450 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14451 end if;
14453 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14454 | N_Formal_Incomplete_Type_Definition
14455 then
14456 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14457 end if;
14459 -- If the actual is a synchronized type that implements an interface,
14460 -- the primitive operations are attached to the corresponding record,
14461 -- and we have to treat it as an additional generic actual, so that its
14462 -- primitive operations become visible in the instance. The task or
14463 -- protected type itself does not carry primitive operations.
14465 if Is_Concurrent_Type (Act_T)
14466 and then Is_Tagged_Type (Act_T)
14467 and then Present (Corresponding_Record_Type (Act_T))
14468 and then Present (Ancestor)
14469 and then Is_Interface (Ancestor)
14470 then
14471 declare
14472 Corr_Rec : constant Entity_Id :=
14473 Corresponding_Record_Type (Act_T);
14474 New_Corr : Entity_Id;
14475 Corr_Decl : Node_Id;
14477 begin
14478 New_Corr := Make_Temporary (Loc, 'S');
14479 Corr_Decl :=
14480 Make_Subtype_Declaration (Loc,
14481 Defining_Identifier => New_Corr,
14482 Subtype_Indication =>
14483 New_Occurrence_Of (Corr_Rec, Loc));
14484 Append_To (Decl_Nodes, Corr_Decl);
14486 if Ekind (Act_T) = E_Task_Type then
14487 Mutate_Ekind (Subt, E_Task_Subtype);
14488 else
14489 Mutate_Ekind (Subt, E_Protected_Subtype);
14490 end if;
14492 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14493 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14494 Set_Generic_Parent_Type (Decl_Node, Empty);
14495 end;
14496 end if;
14498 -- For a floating-point type, capture dimension info if any, because
14499 -- the generated subtype declaration does not come from source and
14500 -- will not process dimensions.
14502 if Is_Floating_Point_Type (Act_T) then
14503 Copy_Dimensions (Act_T, Subt);
14504 end if;
14506 return Decl_Nodes;
14507 end Instantiate_Type;
14509 -----------------------------
14510 -- Is_Abbreviated_Instance --
14511 -----------------------------
14513 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14514 begin
14515 return Ekind (E) = E_Package
14516 and then Present (Hidden_In_Formal_Instance (E));
14517 end Is_Abbreviated_Instance;
14519 ---------------------
14520 -- Is_In_Main_Unit --
14521 ---------------------
14523 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14524 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14525 Current_Unit : Node_Id;
14527 begin
14528 if Unum = Main_Unit then
14529 return True;
14531 -- If the current unit is a subunit then it is either the main unit or
14532 -- is being compiled as part of the main unit.
14534 elsif Nkind (N) = N_Compilation_Unit then
14535 return Nkind (Unit (N)) = N_Subunit;
14536 end if;
14538 Current_Unit := Parent (N);
14539 while Present (Current_Unit)
14540 and then Nkind (Current_Unit) /= N_Compilation_Unit
14541 loop
14542 Current_Unit := Parent (Current_Unit);
14543 end loop;
14545 -- The instantiation node is in the main unit, or else the current node
14546 -- (perhaps as the result of nested instantiations) is in the main unit,
14547 -- or in the declaration of the main unit, which in this last case must
14548 -- be a body.
14550 return
14551 Current_Unit = Cunit (Main_Unit)
14552 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14553 or else (Present (Current_Unit)
14554 and then Present (Library_Unit (Current_Unit))
14555 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14556 end Is_In_Main_Unit;
14558 ----------------------------
14559 -- Load_Parent_Of_Generic --
14560 ----------------------------
14562 procedure Load_Parent_Of_Generic
14563 (N : Node_Id;
14564 Spec : Node_Id;
14565 Body_Optional : Boolean := False)
14567 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14568 Saved_Style_Check : constant Boolean := Style_Check;
14569 Saved_Warn : constant Warnings_State := Save_Warnings;
14570 True_Parent : Node_Id;
14571 Inst_Node : Node_Id;
14572 OK : Boolean;
14573 Previous_Instances : constant Elist_Id := New_Elmt_List;
14575 procedure Collect_Previous_Instances (Decls : List_Id);
14576 -- Collect all instantiations in the given list of declarations, that
14577 -- precede the generic that we need to load. If the bodies of these
14578 -- instantiations are available, we must analyze them, to ensure that
14579 -- the public symbols generated are the same when the unit is compiled
14580 -- to generate code, and when it is compiled in the context of a unit
14581 -- that needs a particular nested instance. This process is applied to
14582 -- both package and subprogram instances.
14584 --------------------------------
14585 -- Collect_Previous_Instances --
14586 --------------------------------
14588 procedure Collect_Previous_Instances (Decls : List_Id) is
14589 Decl : Node_Id;
14591 begin
14592 Decl := First (Decls);
14593 while Present (Decl) loop
14594 if Sloc (Decl) >= Sloc (Inst_Node) then
14595 return;
14597 -- If Decl is an instantiation, then record it as requiring
14598 -- instantiation of the corresponding body, except if it is an
14599 -- abbreviated instantiation generated internally for conformance
14600 -- checking purposes only for the case of a formal package
14601 -- declared without a box (see Instantiate_Formal_Package). Such
14602 -- an instantiation does not generate any code (the actual code
14603 -- comes from actual) and thus does not need to be analyzed here.
14604 -- If the instantiation appears with a generic package body it is
14605 -- not analyzed here either.
14607 elsif Nkind (Decl) = N_Package_Instantiation
14608 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14609 then
14610 Append_Elmt (Decl, Previous_Instances);
14612 -- For a subprogram instantiation, omit instantiations intrinsic
14613 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14615 elsif Nkind (Decl) in N_Function_Instantiation
14616 | N_Procedure_Instantiation
14617 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14618 then
14619 Append_Elmt (Decl, Previous_Instances);
14621 elsif Nkind (Decl) = N_Package_Declaration then
14622 Collect_Previous_Instances
14623 (Visible_Declarations (Specification (Decl)));
14624 Collect_Previous_Instances
14625 (Private_Declarations (Specification (Decl)));
14627 -- Previous non-generic bodies may contain instances as well
14629 elsif Nkind (Decl) = N_Package_Body
14630 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14631 then
14632 Collect_Previous_Instances (Declarations (Decl));
14634 elsif Nkind (Decl) = N_Subprogram_Body
14635 and then not Acts_As_Spec (Decl)
14636 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14637 then
14638 Collect_Previous_Instances (Declarations (Decl));
14639 end if;
14641 Next (Decl);
14642 end loop;
14643 end Collect_Previous_Instances;
14645 -- Start of processing for Load_Parent_Of_Generic
14647 begin
14648 if not In_Same_Source_Unit (N, Spec)
14649 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14650 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14651 and then not Is_In_Main_Unit (Spec))
14652 then
14653 -- Find body of parent of spec, and analyze it. A special case arises
14654 -- when the parent is an instantiation, that is to say when we are
14655 -- currently instantiating a nested generic. In that case, there is
14656 -- no separate file for the body of the enclosing instance. Instead,
14657 -- the enclosing body must be instantiated as if it were a pending
14658 -- instantiation, in order to produce the body for the nested generic
14659 -- we require now. Note that in that case the generic may be defined
14660 -- in a package body, the instance defined in the same package body,
14661 -- and the original enclosing body may not be in the main unit.
14663 Inst_Node := Empty;
14665 True_Parent := Parent (Spec);
14666 while Present (True_Parent)
14667 and then Nkind (True_Parent) /= N_Compilation_Unit
14668 loop
14669 if Nkind (True_Parent) = N_Package_Declaration
14670 and then
14671 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14672 then
14673 -- Parent is a compilation unit that is an instantiation, and
14674 -- instantiation node has been replaced with package decl.
14676 Inst_Node := Original_Node (True_Parent);
14677 exit;
14679 elsif Nkind (True_Parent) = N_Package_Declaration
14680 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14681 and then
14682 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14683 then
14684 -- Parent is a compilation unit that is an instantiation, but
14685 -- instantiation node has not been replaced with package decl.
14687 Inst_Node := Unit (Parent (True_Parent));
14688 exit;
14690 elsif Nkind (True_Parent) = N_Package_Declaration
14691 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14692 and then Present (Generic_Parent (Specification (True_Parent)))
14693 then
14694 -- Parent is an instantiation within another specification.
14695 -- Declaration for instance has been inserted before original
14696 -- instantiation node. A direct link would be preferable?
14698 Inst_Node := Next (True_Parent);
14699 while Present (Inst_Node)
14700 and then Nkind (Inst_Node) /= N_Package_Instantiation
14701 loop
14702 Next (Inst_Node);
14703 end loop;
14705 -- If the instance appears within a generic, and the generic
14706 -- unit is defined within a formal package of the enclosing
14707 -- generic, there is no generic body available, and none
14708 -- needed. A more precise test should be used ???
14710 if No (Inst_Node) then
14711 return;
14712 end if;
14714 exit;
14716 -- If an ancestor of the generic comes from a formal package
14717 -- there is no source for the ancestor body. This is detected
14718 -- by examining the scope of the ancestor and its declaration.
14719 -- The body, if any is needed, will be available when the
14720 -- current unit (containing a formal package) is instantiated.
14722 elsif Nkind (True_Parent) = N_Package_Specification
14723 and then Present (Generic_Parent (True_Parent))
14724 and then Nkind
14725 (Original_Node (Unit_Declaration_Node
14726 (Scope (Generic_Parent (True_Parent)))))
14727 = N_Formal_Package_Declaration
14728 then
14729 return;
14731 else
14732 True_Parent := Parent (True_Parent);
14733 end if;
14734 end loop;
14736 -- Case where we are currently instantiating a nested generic
14738 if Present (Inst_Node) then
14739 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14741 -- Instantiation node and declaration of instantiated package
14742 -- were exchanged when only the declaration was needed.
14743 -- Restore instantiation node before proceeding with body.
14745 Set_Unit (Parent (True_Parent), Inst_Node);
14746 end if;
14748 -- Now complete instantiation of enclosing body, if it appears in
14749 -- some other unit. If it appears in the current unit, the body
14750 -- will have been instantiated already.
14752 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14754 -- We need to determine the expander mode to instantiate the
14755 -- enclosing body. Because the generic body we need may use
14756 -- global entities declared in the enclosing package (including
14757 -- aggregates) it is in general necessary to compile this body
14758 -- with expansion enabled, except if we are within a generic
14759 -- package, in which case the usual generic rule applies.
14761 declare
14762 Exp_Status : Boolean := True;
14763 Scop : Entity_Id;
14765 begin
14766 -- Loop through scopes looking for generic package
14768 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14769 while Present (Scop)
14770 and then Scop /= Standard_Standard
14771 loop
14772 if Ekind (Scop) = E_Generic_Package then
14773 Exp_Status := False;
14774 exit;
14775 end if;
14777 Scop := Scope (Scop);
14778 end loop;
14780 -- Collect previous instantiations in the unit that contains
14781 -- the desired generic.
14783 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14784 and then not Body_Optional
14785 then
14786 declare
14787 Decl : Elmt_Id;
14788 Info : Pending_Body_Info;
14789 Par : Node_Id;
14791 begin
14792 Par := Parent (Inst_Node);
14793 while Present (Par) loop
14794 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14795 Par := Parent (Par);
14796 end loop;
14798 pragma Assert (Present (Par));
14800 if Nkind (Par) = N_Package_Body then
14801 Collect_Previous_Instances (Declarations (Par));
14803 elsif Nkind (Par) = N_Package_Declaration then
14804 Collect_Previous_Instances
14805 (Visible_Declarations (Specification (Par)));
14806 Collect_Previous_Instances
14807 (Private_Declarations (Specification (Par)));
14809 else
14810 -- Enclosing unit is a subprogram body. In this
14811 -- case all instance bodies are processed in order
14812 -- and there is no need to collect them separately.
14814 null;
14815 end if;
14817 Decl := First_Elmt (Previous_Instances);
14818 while Present (Decl) loop
14819 Info :=
14820 (Inst_Node => Node (Decl),
14821 Act_Decl =>
14822 Instance_Spec (Node (Decl)),
14823 Fin_Scop => Empty,
14824 Config_Switches => Save_Config_Switches,
14825 Current_Sem_Unit =>
14826 Get_Code_Unit (Sloc (Node (Decl))),
14827 Expander_Status => Exp_Status,
14828 Local_Suppress_Stack_Top =>
14829 Local_Suppress_Stack_Top,
14830 Scope_Suppress => Scope_Suppress,
14831 Warnings => Save_Warnings);
14833 -- Package instance
14835 if Nkind (Node (Decl)) = N_Package_Instantiation
14836 then
14837 Instantiate_Package_Body
14838 (Info, Body_Optional => True);
14840 -- Subprogram instance
14842 else
14843 -- The instance_spec is in the wrapper package,
14844 -- usually followed by its local renaming
14845 -- declaration. See Build_Subprogram_Renaming
14846 -- for details. If the instance carries aspects,
14847 -- these result in the corresponding pragmas,
14848 -- inserted after the subprogram declaration.
14849 -- They must be skipped as well when retrieving
14850 -- the desired spec. Some of them may have been
14851 -- rewritten as null statements.
14852 -- A direct link would be more robust ???
14854 declare
14855 Decl : Node_Id :=
14856 (Last (Visible_Declarations
14857 (Specification (Info.Act_Decl))));
14858 begin
14859 while Nkind (Decl) in
14860 N_Null_Statement |
14861 N_Pragma |
14862 N_Subprogram_Renaming_Declaration
14863 loop
14864 Decl := Prev (Decl);
14865 end loop;
14867 Info.Act_Decl := Decl;
14868 end;
14870 Instantiate_Subprogram_Body
14871 (Info, Body_Optional => True);
14872 end if;
14874 Next_Elmt (Decl);
14875 end loop;
14876 end;
14877 end if;
14879 Instantiate_Package_Body
14880 (Body_Info =>
14881 ((Inst_Node => Inst_Node,
14882 Act_Decl => True_Parent,
14883 Fin_Scop => Empty,
14884 Config_Switches => Save_Config_Switches,
14885 Current_Sem_Unit =>
14886 Get_Code_Unit (Sloc (Inst_Node)),
14887 Expander_Status => Exp_Status,
14888 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14889 Scope_Suppress => Scope_Suppress,
14890 Warnings => Save_Warnings)),
14891 Body_Optional => Body_Optional);
14892 end;
14893 end if;
14895 -- Case where we are not instantiating a nested generic
14897 else
14898 Opt.Style_Check := False;
14899 Expander_Mode_Save_And_Set (True);
14900 Load_Needed_Body (Comp_Unit, OK);
14901 Opt.Style_Check := Saved_Style_Check;
14902 Restore_Warnings (Saved_Warn);
14903 Expander_Mode_Restore;
14905 if not OK
14906 and then Unit_Requires_Body (Defining_Entity (Spec))
14907 and then not Body_Optional
14908 then
14909 declare
14910 Bname : constant Unit_Name_Type :=
14911 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14913 begin
14914 -- In CodePeer mode, the missing body may make the analysis
14915 -- incomplete, but we do not treat it as fatal.
14917 if CodePeer_Mode then
14918 return;
14920 else
14921 Error_Msg_Unit_1 := Bname;
14922 Error_Msg_N ("this instantiation requires$!", N);
14923 Error_Msg_File_1 :=
14924 Get_File_Name (Bname, Subunit => False);
14925 Error_Msg_N ("\but file{ was not found!", N);
14926 raise Unrecoverable_Error;
14927 end if;
14928 end;
14929 end if;
14930 end if;
14931 end if;
14933 -- If loading parent of the generic caused an instantiation circularity,
14934 -- we abandon compilation at this point, because otherwise in some cases
14935 -- we get into trouble with infinite recursions after this point.
14937 if Circularity_Detected then
14938 raise Unrecoverable_Error;
14939 end if;
14940 end Load_Parent_Of_Generic;
14942 ---------------------------------
14943 -- Map_Formal_Package_Entities --
14944 ---------------------------------
14946 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14947 E1 : Entity_Id;
14948 E2 : Entity_Id;
14950 begin
14951 Set_Instance_Of (Form, Act);
14953 -- Traverse formal and actual package to map the corresponding entities.
14954 -- We skip over internal entities that may be generated during semantic
14955 -- analysis, and find the matching entities by name, given that they
14956 -- must appear in the same order.
14958 E1 := First_Entity (Form);
14959 E2 := First_Entity (Act);
14960 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14961 -- Could this test be a single condition??? Seems like it could, and
14962 -- isn't FPE (Form) a constant anyway???
14964 if not Is_Internal (E1)
14965 and then Present (Parent (E1))
14966 and then not Is_Class_Wide_Type (E1)
14967 and then not Is_Internal_Name (Chars (E1))
14968 then
14969 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14970 Next_Entity (E2);
14971 end loop;
14973 if No (E2) then
14974 exit;
14975 else
14976 Set_Instance_Of (E1, E2);
14978 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14979 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14980 end if;
14982 if Is_Constrained (E1) then
14983 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14984 end if;
14986 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14987 Map_Formal_Package_Entities (E1, E2);
14988 end if;
14989 end if;
14990 end if;
14992 Next_Entity (E1);
14993 end loop;
14994 end Map_Formal_Package_Entities;
14996 -----------------------
14997 -- Move_Freeze_Nodes --
14998 -----------------------
15000 procedure Move_Freeze_Nodes
15001 (Out_Of : Entity_Id;
15002 After : Node_Id;
15003 L : List_Id)
15005 Decl : Node_Id;
15006 Next_Decl : Node_Id;
15007 Next_Node : Node_Id := After;
15008 Spec : Node_Id;
15010 function Is_Outer_Type (T : Entity_Id) return Boolean;
15011 -- Check whether entity is declared in a scope external to that of the
15012 -- generic unit.
15014 -------------------
15015 -- Is_Outer_Type --
15016 -------------------
15018 function Is_Outer_Type (T : Entity_Id) return Boolean is
15019 Scop : Entity_Id := Scope (T);
15021 begin
15022 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15023 return True;
15025 else
15026 while Scop /= Standard_Standard loop
15027 if Scop = Out_Of then
15028 return False;
15029 else
15030 Scop := Scope (Scop);
15031 end if;
15032 end loop;
15034 return True;
15035 end if;
15036 end Is_Outer_Type;
15038 -- Start of processing for Move_Freeze_Nodes
15040 begin
15041 if No (L) then
15042 return;
15043 end if;
15045 -- First remove the freeze nodes that may appear before all other
15046 -- declarations.
15048 Decl := First (L);
15049 while Present (Decl)
15050 and then Nkind (Decl) = N_Freeze_Entity
15051 and then Is_Outer_Type (Entity (Decl))
15052 loop
15053 Decl := Remove_Head (L);
15054 Insert_After (Next_Node, Decl);
15055 Set_Analyzed (Decl, False);
15056 Next_Node := Decl;
15057 Decl := First (L);
15058 end loop;
15060 -- Next scan the list of declarations and remove each freeze node that
15061 -- appears ahead of the current node.
15063 while Present (Decl) loop
15064 while Present (Next (Decl))
15065 and then Nkind (Next (Decl)) = N_Freeze_Entity
15066 and then Is_Outer_Type (Entity (Next (Decl)))
15067 loop
15068 Next_Decl := Remove_Next (Decl);
15069 Insert_After (Next_Node, Next_Decl);
15070 Set_Analyzed (Next_Decl, False);
15071 Next_Node := Next_Decl;
15072 end loop;
15074 -- If the declaration is a nested package or concurrent type, then
15075 -- recurse. Nested generic packages will have been processed from the
15076 -- inside out.
15078 case Nkind (Decl) is
15079 when N_Package_Declaration =>
15080 Spec := Specification (Decl);
15082 when N_Task_Type_Declaration =>
15083 Spec := Task_Definition (Decl);
15085 when N_Protected_Type_Declaration =>
15086 Spec := Protected_Definition (Decl);
15088 when others =>
15089 Spec := Empty;
15090 end case;
15092 if Present (Spec) then
15093 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15094 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15095 end if;
15097 Next (Decl);
15098 end loop;
15099 end Move_Freeze_Nodes;
15101 ----------------
15102 -- Next_Assoc --
15103 ----------------
15105 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15106 begin
15107 return Generic_Renamings.Table (E).Next_In_HTable;
15108 end Next_Assoc;
15110 ------------------------
15111 -- Preanalyze_Actuals --
15112 ------------------------
15114 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15115 procedure Perform_Appropriate_Analysis (N : Node_Id);
15116 -- Determine if the actuals we are analyzing come from a generic
15117 -- instantiation that is a library unit and dispatch accordingly.
15119 ----------------------------------
15120 -- Perform_Appropriate_Analysis --
15121 ----------------------------------
15123 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15124 begin
15125 -- When we have a library instantiation we cannot allow any expansion
15126 -- to occur, since there may be no place to put it. Instead, in that
15127 -- case we perform a preanalysis of the actual.
15129 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15130 Preanalyze (N);
15131 else
15132 Analyze (N);
15133 end if;
15134 end Perform_Appropriate_Analysis;
15136 -- Local variables
15138 Errs : constant Nat := Serious_Errors_Detected;
15140 Assoc : Node_Id;
15141 Act : Node_Id;
15143 Cur : Entity_Id := Empty;
15144 -- Current homograph of the instance name
15146 Vis : Boolean := False;
15147 -- Saved visibility status of the current homograph
15149 -- Start of processing for Preanalyze_Actuals
15151 begin
15152 Assoc := First (Generic_Associations (N));
15154 -- If the instance is a child unit, its name may hide an outer homonym,
15155 -- so make it invisible to perform name resolution on the actuals.
15157 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15158 and then Present
15159 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15160 then
15161 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15163 if Is_Compilation_Unit (Cur) then
15164 Vis := Is_Immediately_Visible (Cur);
15165 Set_Is_Immediately_Visible (Cur, False);
15166 else
15167 Cur := Empty;
15168 end if;
15169 end if;
15171 while Present (Assoc) loop
15172 if Nkind (Assoc) /= N_Others_Choice then
15173 Act := Explicit_Generic_Actual_Parameter (Assoc);
15175 -- Within a nested instantiation, a defaulted actual is an empty
15176 -- association, so nothing to analyze. If the subprogram actual
15177 -- is an attribute, analyze prefix only, because actual is not a
15178 -- complete attribute reference.
15180 -- If actual is an allocator, analyze expression only. The full
15181 -- analysis can generate code, and if instance is a compilation
15182 -- unit we have to wait until the package instance is installed
15183 -- to have a proper place to insert this code.
15185 -- String literals may be operators, but at this point we do not
15186 -- know whether the actual is a formal subprogram or a string.
15188 if No (Act) then
15189 null;
15191 elsif Nkind (Act) = N_Attribute_Reference then
15192 Perform_Appropriate_Analysis (Prefix (Act));
15194 elsif Nkind (Act) = N_Explicit_Dereference then
15195 Perform_Appropriate_Analysis (Prefix (Act));
15197 elsif Nkind (Act) = N_Allocator then
15198 declare
15199 Expr : constant Node_Id := Expression (Act);
15201 begin
15202 if Nkind (Expr) = N_Subtype_Indication then
15203 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15205 -- Analyze separately each discriminant constraint, when
15206 -- given with a named association.
15208 declare
15209 Constr : Node_Id;
15211 begin
15212 Constr := First (Constraints (Constraint (Expr)));
15213 while Present (Constr) loop
15214 if Nkind (Constr) = N_Discriminant_Association then
15215 Perform_Appropriate_Analysis
15216 (Expression (Constr));
15217 else
15218 Perform_Appropriate_Analysis (Constr);
15219 end if;
15221 Next (Constr);
15222 end loop;
15223 end;
15225 else
15226 Perform_Appropriate_Analysis (Expr);
15227 end if;
15228 end;
15230 elsif Nkind (Act) /= N_Operator_Symbol then
15231 Perform_Appropriate_Analysis (Act);
15233 -- Within a package instance, mark actuals that are limited
15234 -- views, so their use can be moved to the body of the
15235 -- enclosing unit.
15237 if Is_Entity_Name (Act)
15238 and then Is_Type (Entity (Act))
15239 and then From_Limited_With (Entity (Act))
15240 and then Present (Inst)
15241 then
15242 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15243 end if;
15244 end if;
15246 if Errs /= Serious_Errors_Detected then
15248 -- Do a minimal analysis of the generic, to prevent spurious
15249 -- warnings complaining about the generic being unreferenced,
15250 -- before abandoning the instantiation.
15252 Perform_Appropriate_Analysis (Name (N));
15254 if Is_Entity_Name (Name (N))
15255 and then Etype (Name (N)) /= Any_Type
15256 then
15257 Generate_Reference (Entity (Name (N)), Name (N));
15258 Set_Is_Instantiated (Entity (Name (N)));
15259 end if;
15261 if Present (Cur) then
15263 -- For the case of a child instance hiding an outer homonym,
15264 -- provide additional warning which might explain the error.
15266 Set_Is_Immediately_Visible (Cur, Vis);
15267 Error_Msg_NE
15268 ("& hides outer unit with the same name??",
15269 N, Defining_Unit_Name (N));
15270 end if;
15272 Abandon_Instantiation (Act);
15273 end if;
15274 end if;
15276 Next (Assoc);
15277 end loop;
15279 if Present (Cur) then
15280 Set_Is_Immediately_Visible (Cur, Vis);
15281 end if;
15282 end Preanalyze_Actuals;
15284 -------------------------------
15285 -- Provide_Completing_Bodies --
15286 -------------------------------
15288 procedure Provide_Completing_Bodies (N : Node_Id) is
15289 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15290 -- Generate the completing body for subprogram declaration Subp_Decl
15292 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15293 -- Generating completing bodies for all subprograms found in declarative
15294 -- list Decls.
15296 ---------------------------
15297 -- Build_Completing_Body --
15298 ---------------------------
15300 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15301 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15302 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15303 Spec : Node_Id;
15305 begin
15306 -- Nothing to do if the subprogram already has a completing body
15308 if Present (Corresponding_Body (Subp_Decl)) then
15309 return;
15311 -- Mark the function as having a valid return statement even though
15312 -- the body contains a single raise statement.
15314 elsif Ekind (Subp_Id) = E_Function then
15315 Set_Return_Present (Subp_Id);
15316 end if;
15318 -- Clone the specification to obtain new entities and reset the only
15319 -- semantic field.
15321 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15322 Set_Generic_Parent (Spec, Empty);
15324 -- Generate:
15325 -- function Func ... return ... is
15326 -- <or>
15327 -- procedure Proc ... is
15328 -- begin
15329 -- raise Program_Error with "access before elaboration";
15330 -- edn Proc;
15332 Insert_After_And_Analyze (Subp_Decl,
15333 Make_Subprogram_Body (Loc,
15334 Specification => Spec,
15335 Declarations => New_List,
15336 Handled_Statement_Sequence =>
15337 Make_Handled_Sequence_Of_Statements (Loc,
15338 Statements => New_List (
15339 Make_Raise_Program_Error (Loc,
15340 Reason => PE_Access_Before_Elaboration)))));
15341 end Build_Completing_Body;
15343 ----------------------------------
15344 -- Provide_Completing_Bodies_In --
15345 ----------------------------------
15347 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15348 Decl : Node_Id;
15350 begin
15351 if Present (Decls) then
15352 Decl := First (Decls);
15353 while Present (Decl) loop
15354 Provide_Completing_Bodies (Decl);
15355 Next (Decl);
15356 end loop;
15357 end if;
15358 end Provide_Completing_Bodies_In;
15360 -- Local variables
15362 Spec : Node_Id;
15364 -- Start of processing for Provide_Completing_Bodies
15366 begin
15367 if Nkind (N) = N_Package_Declaration then
15368 Spec := Specification (N);
15370 Push_Scope (Defining_Entity (N));
15371 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15372 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15373 Pop_Scope;
15375 elsif Nkind (N) = N_Subprogram_Declaration then
15376 Build_Completing_Body (N);
15377 end if;
15378 end Provide_Completing_Bodies;
15380 -------------------
15381 -- Remove_Parent --
15382 -------------------
15384 procedure Remove_Parent (In_Body : Boolean := False) is
15385 S : Entity_Id := Current_Scope;
15386 -- S is the scope containing the instantiation just completed. The scope
15387 -- stack contains the parent instances of the instantiation, followed by
15388 -- the original S.
15390 Cur_P : Entity_Id;
15391 E : Entity_Id;
15392 P : Entity_Id;
15393 Hidden : Elmt_Id;
15395 begin
15396 -- After child instantiation is complete, remove from scope stack the
15397 -- extra copy of the current scope, and then remove parent instances.
15399 if not In_Body then
15400 Pop_Scope;
15402 while Current_Scope /= S loop
15403 P := Current_Scope;
15404 End_Package_Scope (Current_Scope);
15406 if In_Open_Scopes (P) then
15407 E := First_Entity (P);
15408 while Present (E) loop
15409 Set_Is_Immediately_Visible (E, True);
15410 Next_Entity (E);
15411 end loop;
15413 -- If instantiation is declared in a block, it is the enclosing
15414 -- scope that might be a parent instance. Note that only one
15415 -- block can be involved, because the parent instances have
15416 -- been installed within it.
15418 if Ekind (P) = E_Block then
15419 Cur_P := Scope (P);
15420 else
15421 Cur_P := P;
15422 end if;
15424 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15425 -- We are within an instance of some sibling. Retain
15426 -- visibility of parent, for proper subsequent cleanup, and
15427 -- reinstall private declarations as well.
15429 Set_In_Private_Part (P);
15430 Install_Private_Declarations (P);
15431 end if;
15433 -- If the ultimate parent is a top-level unit recorded in
15434 -- Instance_Parent_Unit, then reset its visibility to what it was
15435 -- before instantiation. (It's not clear what the purpose is of
15436 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15437 -- present before the ultimate parent test was added.???)
15439 elsif not In_Open_Scopes (Scope (P))
15440 or else (P = Instance_Parent_Unit
15441 and then not Parent_Unit_Visible)
15442 then
15443 Set_Is_Immediately_Visible (P, False);
15445 -- If the current scope is itself an instantiation of a generic
15446 -- nested within P, and we are in the private part of body of this
15447 -- instantiation, restore the full views of P, that were removed
15448 -- in End_Package_Scope above. This obscure case can occur when a
15449 -- subunit of a generic contains an instance of a child unit of
15450 -- its generic parent unit.
15452 elsif S = Current_Scope and then Is_Generic_Instance (S)
15453 and then (In_Package_Body (S) or else In_Private_Part (S))
15454 then
15455 declare
15456 Par : constant Entity_Id :=
15457 Generic_Parent (Package_Specification (S));
15458 begin
15459 if Present (Par)
15460 and then P = Scope (Par)
15461 then
15462 Set_In_Private_Part (P);
15463 Install_Private_Declarations (P);
15464 end if;
15465 end;
15466 end if;
15467 end loop;
15469 -- Reset visibility of entities in the enclosing scope
15471 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15473 Hidden := First_Elmt (Hidden_Entities);
15474 while Present (Hidden) loop
15475 Set_Is_Immediately_Visible (Node (Hidden), True);
15476 Next_Elmt (Hidden);
15477 end loop;
15479 else
15480 -- Each body is analyzed separately, and there is no context that
15481 -- needs preserving from one body instance to the next, so remove all
15482 -- parent scopes that have been installed.
15484 while Present (S) loop
15485 End_Package_Scope (S);
15486 Set_Is_Immediately_Visible (S, False);
15487 S := Current_Scope;
15488 exit when S = Standard_Standard;
15489 end loop;
15490 end if;
15491 end Remove_Parent;
15493 -----------------------------------
15494 -- Requires_Conformance_Checking --
15495 -----------------------------------
15497 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15498 begin
15499 -- No conformance checking required if the generic actual part is empty,
15500 -- or is a box or an others_clause (necessarily with a box).
15502 return Present (Generic_Associations (N))
15503 and then not Box_Present (N)
15504 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15505 end Requires_Conformance_Checking;
15507 -----------------
15508 -- Restore_Env --
15509 -----------------
15511 procedure Restore_Env is
15512 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15514 begin
15515 if No (Current_Instantiated_Parent.Act_Id) then
15516 -- Restore environment after subprogram inlining
15518 Restore_Private_Views (Empty);
15519 end if;
15521 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15522 Exchanged_Views := Saved.Exchanged_Views;
15523 Hidden_Entities := Saved.Hidden_Entities;
15524 Current_Sem_Unit := Saved.Current_Sem_Unit;
15525 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15526 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15528 Restore_Config_Switches (Saved.Switches);
15530 Instance_Envs.Decrement_Last;
15531 end Restore_Env;
15533 ---------------------------
15534 -- Restore_Private_Views --
15535 ---------------------------
15537 procedure Restore_Private_Views
15538 (Pack_Id : Entity_Id;
15539 Is_Package : Boolean := True)
15541 M : Elmt_Id;
15542 E : Entity_Id;
15543 Typ : Entity_Id;
15544 Dep_Elmt : Elmt_Id;
15545 Dep_Typ : Node_Id;
15547 procedure Restore_Nested_Formal (Formal : Entity_Id);
15548 -- Hide the generic formals of formal packages declared with box which
15549 -- were reachable in the current instantiation.
15551 ---------------------------
15552 -- Restore_Nested_Formal --
15553 ---------------------------
15555 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15556 pragma Assert (Ekind (Formal) = E_Package);
15557 Ent : Entity_Id;
15558 begin
15559 if Present (Renamed_Entity (Formal))
15560 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15561 then
15562 return;
15564 elsif Present (Associated_Formal_Package (Formal)) then
15565 Ent := First_Entity (Formal);
15566 while Present (Ent) loop
15567 exit when Ekind (Ent) = E_Package
15568 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15570 Set_Is_Hidden (Ent);
15571 Set_Is_Potentially_Use_Visible (Ent, False);
15573 -- If package, then recurse
15575 if Ekind (Ent) = E_Package then
15576 Restore_Nested_Formal (Ent);
15577 end if;
15579 Next_Entity (Ent);
15580 end loop;
15581 end if;
15582 end Restore_Nested_Formal;
15584 -- Start of processing for Restore_Private_Views
15586 begin
15587 M := First_Elmt (Exchanged_Views);
15588 while Present (M) loop
15589 Typ := Node (M);
15591 -- Subtypes of types whose views have been exchanged, and that are
15592 -- defined within the instance, were not on the Private_Dependents
15593 -- list on entry to the instance, so they have to be exchanged
15594 -- explicitly now, in order to remain consistent with the view of the
15595 -- parent type.
15597 if Ekind (Typ) in E_Private_Type
15598 | E_Limited_Private_Type
15599 | E_Record_Type_With_Private
15600 then
15601 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15602 while Present (Dep_Elmt) loop
15603 Dep_Typ := Node (Dep_Elmt);
15605 if Scope (Dep_Typ) = Pack_Id
15606 and then Present (Full_View (Dep_Typ))
15607 then
15608 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15609 Exchange_Declarations (Dep_Typ);
15610 end if;
15612 Next_Elmt (Dep_Elmt);
15613 end loop;
15614 end if;
15616 Exchange_Declarations (Typ);
15617 Next_Elmt (M);
15618 end loop;
15620 if No (Pack_Id) then
15621 return;
15622 end if;
15624 -- Make the generic formal parameters private, and make the formal types
15625 -- into subtypes of the actuals again.
15627 E := First_Entity (Pack_Id);
15628 while Present (E) loop
15629 Set_Is_Hidden (E, True);
15631 if Is_Type (E)
15632 and then Nkind (Parent (E)) = N_Subtype_Declaration
15633 then
15634 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15635 -- as it is needed to identify the subtype with the type it
15636 -- renames, when there are conversions between access types
15637 -- to these.
15639 if GNATprove_Mode then
15640 null;
15642 -- If the actual for E is itself a generic actual type from
15643 -- an enclosing instance, E is still a generic actual type
15644 -- outside of the current instance. This matter when resolving
15645 -- an overloaded call that may be ambiguous in the enclosing
15646 -- instance, when two of its actuals coincide.
15648 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15649 and then Is_Generic_Actual_Type
15650 (Entity (Subtype_Indication (Parent (E))))
15651 then
15652 null;
15653 else
15654 Set_Is_Generic_Actual_Type (E, False);
15656 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15657 -- flag also on the Full_View if the type is private, since it
15658 -- was set also on this Full_View. However, this flag is relied
15659 -- upon by Covers to spot "types exported from instantiations"
15660 -- which are implicit Full_Views built for instantiations made
15661 -- on private types and we get type mismatches if we do it when
15662 -- the block exchanging the declarations below triggers ???
15664 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15665 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15666 -- end if;
15667 end if;
15669 -- An unusual case of aliasing: the actual may also be directly
15670 -- visible in the generic, and be private there, while it is fully
15671 -- visible in the context of the instance. The internal subtype
15672 -- is private in the instance but has full visibility like its
15673 -- parent in the enclosing scope. This enforces the invariant that
15674 -- the privacy status of all private dependents of a type coincide
15675 -- with that of the parent type. This can only happen when a
15676 -- generic child unit is instantiated within a sibling.
15678 if Is_Private_Type (E)
15679 and then not Is_Private_Type (Etype (E))
15680 then
15681 Exchange_Declarations (E);
15682 end if;
15684 elsif Ekind (E) = E_Package then
15686 -- The end of the renaming list is the renaming of the generic
15687 -- package itself. If the instance is a subprogram, all entities
15688 -- in the corresponding package are renamings. If this entity is
15689 -- a formal package, make its own formals private as well. The
15690 -- actual in this case is itself the renaming of an instantiation.
15691 -- If the entity is not a package renaming, it is the entity
15692 -- created to validate formal package actuals: ignore it.
15694 -- If the actual is itself a formal package for the enclosing
15695 -- generic, or the actual for such a formal package, it remains
15696 -- visible on exit from the instance, and therefore nothing needs
15697 -- to be done either, except to keep it accessible.
15699 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15700 exit;
15702 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15703 null;
15705 elsif
15706 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15707 then
15708 Set_Is_Hidden (E, False);
15710 else
15711 declare
15712 Act_P : constant Entity_Id := Renamed_Entity (E);
15713 Id : Entity_Id;
15715 begin
15716 Id := First_Entity (Act_P);
15717 while Present (Id)
15718 and then Id /= First_Private_Entity (Act_P)
15719 loop
15720 exit when Ekind (Id) = E_Package
15721 and then Renamed_Entity (Id) = Act_P;
15723 Set_Is_Hidden (Id, True);
15724 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15726 if Ekind (Id) = E_Package then
15727 Restore_Nested_Formal (Id);
15728 end if;
15730 Next_Entity (Id);
15731 end loop;
15732 end;
15733 end if;
15734 end if;
15736 Next_Entity (E);
15737 end loop;
15738 end Restore_Private_Views;
15740 --------------
15741 -- Save_Env --
15742 --------------
15744 procedure Save_Env
15745 (Gen_Unit : Entity_Id;
15746 Act_Unit : Entity_Id)
15748 begin
15749 Init_Env;
15750 Set_Instance_Env (Gen_Unit, Act_Unit);
15751 end Save_Env;
15753 ----------------------------
15754 -- Save_Global_References --
15755 ----------------------------
15757 procedure Save_Global_References (Templ : Node_Id) is
15759 -- ??? it is horrible to use global variables in highly recursive code
15761 E : Entity_Id;
15762 -- The entity of the current associated node
15764 Gen_Scope : Entity_Id;
15765 -- The scope of the generic for which references are being saved
15767 N2 : Node_Id;
15768 -- The current associated node
15770 function Is_Global (E : Entity_Id) return Boolean;
15771 -- Check whether entity is defined outside of generic unit. Examine the
15772 -- scope of an entity, and the scope of the scope, etc, until we find
15773 -- either Standard, in which case the entity is global, or the generic
15774 -- unit itself, which indicates that the entity is local. If the entity
15775 -- is the generic unit itself, as in the case of a recursive call, or
15776 -- the enclosing generic unit, if different from the current scope, then
15777 -- it is local as well, because it will be replaced at the point of
15778 -- instantiation. On the other hand, if it is a reference to a child
15779 -- unit of a common ancestor, which appears in an instantiation, it is
15780 -- global because it is used to denote a specific compilation unit at
15781 -- the time the instantiations will be analyzed.
15783 procedure Qualify_Universal_Operands
15784 (Op : Node_Id;
15785 Func_Call : Node_Id);
15786 -- Op denotes a binary or unary operator in generic template Templ. Node
15787 -- Func_Call is the function call alternative of the operator within the
15788 -- the analyzed copy of the template. Change each operand which yields a
15789 -- universal type by wrapping it into a qualified expression
15791 -- Actual_Typ'(Operand)
15793 -- where Actual_Typ is the type of corresponding actual parameter of
15794 -- Operand in Func_Call.
15796 procedure Reset_Entity (N : Node_Id);
15797 -- Save semantic information on global entity so that it is not resolved
15798 -- again at instantiation time.
15800 procedure Save_Entity_Descendants (N : Node_Id);
15801 -- Apply Save_Global_References to the two syntactic descendants of
15802 -- non-terminal nodes that carry an Associated_Node and are processed
15803 -- through Reset_Entity. Once the global entity (if any) has been
15804 -- captured together with its type, only two syntactic descendants need
15805 -- to be traversed to complete the processing of the tree rooted at N.
15806 -- This applies to Selected_Components, Expanded_Names, and to Operator
15807 -- nodes. N can also be a character literal, identifier, or operator
15808 -- symbol node, but the call has no effect in these cases.
15810 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15811 -- Default actuals in nested instances must be handled specially
15812 -- because there is no link to them from the original tree. When an
15813 -- actual subprogram is given by a default, we add an explicit generic
15814 -- association for it in the instantiation node. When we save the
15815 -- global references on the name of the instance, we recover the list
15816 -- of generic associations, and add an explicit one to the original
15817 -- generic tree, through which a global actual can be preserved.
15818 -- Similarly, if a child unit is instantiated within a sibling, in the
15819 -- context of the parent, we must preserve the identifier of the parent
15820 -- so that it can be properly resolved in a subsequent instantiation.
15822 procedure Save_Global_Descendant (D : Union_Id);
15823 -- Apply Save_References recursively to the descendants of node D
15825 procedure Save_References (N : Node_Id);
15826 -- This is the recursive procedure that does the work, once the
15827 -- enclosing generic scope has been established.
15829 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15830 -- If the type of N2 is global to the generic unit, save the type in
15831 -- the generic node. Just as we perform name capture for explicit
15832 -- references within the generic, we must capture the global types
15833 -- of local entities because they may participate in resolution in
15834 -- the instance.
15836 ---------------
15837 -- Is_Global --
15838 ---------------
15840 function Is_Global (E : Entity_Id) return Boolean is
15841 Se : Entity_Id;
15843 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15844 -- Determine whether the parent node of a reference to a child unit
15845 -- denotes an instantiation or a formal package, in which case the
15846 -- reference to the child unit is global, even if it appears within
15847 -- the current scope (e.g. when the instance appears within the body
15848 -- of an ancestor).
15850 ----------------------
15851 -- Is_Instance_Node --
15852 ----------------------
15854 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15855 begin
15856 return Nkind (Decl) in N_Generic_Instantiation
15857 or else
15858 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15859 end Is_Instance_Node;
15861 -- Start of processing for Is_Global
15863 begin
15864 if E = Gen_Scope then
15865 return False;
15867 elsif E = Standard_Standard then
15868 return True;
15870 -- E should be an entity, but it is not always
15872 elsif Nkind (E) not in N_Entity then
15873 return False;
15875 elsif Nkind (E) /= N_Expanded_Name
15876 and then Is_Child_Unit (E)
15877 and then (Is_Instance_Node (Parent (N2))
15878 or else (Nkind (Parent (N2)) = N_Expanded_Name
15879 and then N2 = Selector_Name (Parent (N2))
15880 and then
15881 Is_Instance_Node (Parent (Parent (N2)))))
15882 then
15883 return True;
15885 else
15886 -- E may be an expanded name - typically an operator - in which
15887 -- case we must find its enclosing scope since expanded names
15888 -- don't have corresponding scopes.
15890 if Nkind (E) = N_Expanded_Name then
15891 Se := Find_Enclosing_Scope (E);
15893 -- Otherwise, E is an entity and will have Scope set
15895 else
15896 Se := Scope (E);
15897 end if;
15899 while Se /= Gen_Scope loop
15900 if Se = Standard_Standard then
15901 return True;
15902 else
15903 Se := Scope (Se);
15904 end if;
15905 end loop;
15907 return False;
15908 end if;
15909 end Is_Global;
15911 --------------------------------
15912 -- Qualify_Universal_Operands --
15913 --------------------------------
15915 procedure Qualify_Universal_Operands
15916 (Op : Node_Id;
15917 Func_Call : Node_Id)
15919 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15920 -- Rewrite operand Opnd as a qualified expression of the form
15922 -- Actual_Typ'(Opnd)
15924 -- where Actual is the corresponding actual parameter of Opnd in
15925 -- function call Func_Call.
15927 function Qualify_Type
15928 (Loc : Source_Ptr;
15929 Typ : Entity_Id) return Node_Id;
15930 -- Qualify type Typ by creating a selected component of the form
15932 -- Scope_Of_Typ.Typ
15934 ---------------------
15935 -- Qualify_Operand --
15936 ---------------------
15938 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15939 Loc : constant Source_Ptr := Sloc (Opnd);
15940 Typ : constant Entity_Id := Etype (Actual);
15941 Mark : Node_Id;
15942 Qual : Node_Id;
15944 begin
15945 -- Qualify the operand when it is of a universal type. Note that
15946 -- the template is unanalyzed and it is not possible to directly
15947 -- query the type. This transformation is not done when the type
15948 -- of the actual is internally generated because the type will be
15949 -- regenerated in the instance.
15951 if Yields_Universal_Type (Opnd)
15952 and then Comes_From_Source (Typ)
15953 and then not Is_Hidden (Typ)
15954 then
15955 -- The type of the actual may be a global reference. Save this
15956 -- information by creating a reference to it.
15958 if Is_Global (Typ) then
15959 Mark := New_Occurrence_Of (Typ, Loc);
15961 -- Otherwise rely on resolution to find the proper type within
15962 -- the instance.
15964 else
15965 Mark := Qualify_Type (Loc, Typ);
15966 end if;
15968 Qual :=
15969 Make_Qualified_Expression (Loc,
15970 Subtype_Mark => Mark,
15971 Expression => Relocate_Node (Opnd));
15973 -- Mark the qualification to distinguish it from other source
15974 -- constructs and signal the instantiation mechanism that this
15975 -- node requires special processing. See Copy_Generic_Node for
15976 -- details.
15978 Set_Is_Qualified_Universal_Literal (Qual);
15980 Rewrite (Opnd, Qual);
15981 end if;
15982 end Qualify_Operand;
15984 ------------------
15985 -- Qualify_Type --
15986 ------------------
15988 function Qualify_Type
15989 (Loc : Source_Ptr;
15990 Typ : Entity_Id) return Node_Id
15992 Scop : constant Entity_Id := Scope (Typ);
15993 Result : Node_Id;
15995 begin
15996 Result := Make_Identifier (Loc, Chars (Typ));
15998 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15999 Result :=
16000 Make_Selected_Component (Loc,
16001 Prefix => Make_Identifier (Loc, Chars (Scop)),
16002 Selector_Name => Result);
16003 end if;
16005 return Result;
16006 end Qualify_Type;
16008 -- Local variables
16010 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16012 -- Start of processing for Qualify_Universal_Operands
16014 begin
16015 if Nkind (Op) in N_Binary_Op then
16016 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16017 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16019 elsif Nkind (Op) in N_Unary_Op then
16020 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16021 end if;
16022 end Qualify_Universal_Operands;
16024 ------------------
16025 -- Reset_Entity --
16026 ------------------
16028 procedure Reset_Entity (N : Node_Id) is
16029 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16030 -- Find the ultimate ancestor of the current unit. If it is not a
16031 -- generic unit, then the name of the current unit in the prefix of
16032 -- an expanded name must be replaced with its generic homonym to
16033 -- ensure that it will be properly resolved in an instance.
16035 ------------------
16036 -- Top_Ancestor --
16037 ------------------
16039 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16040 Par : Entity_Id;
16042 begin
16043 Par := E;
16044 while Is_Child_Unit (Par) loop
16045 Par := Scope (Par);
16046 end loop;
16048 return Par;
16049 end Top_Ancestor;
16051 -- Start of processing for Reset_Entity
16053 begin
16054 N2 := Get_Associated_Node (N);
16055 E := Entity (N2);
16057 if Present (E) then
16059 -- If the node is an entry call to an entry in an enclosing task,
16060 -- it is rewritten as a selected component. No global entity to
16061 -- preserve in this case, since the expansion will be redone in
16062 -- the instance.
16064 if Nkind (E) not in N_Entity then
16065 Set_Associated_Node (N, Empty);
16066 Set_Etype (N, Empty);
16067 return;
16068 end if;
16070 -- If the entity is an itype created as a subtype of an access
16071 -- type with a null exclusion restore source entity for proper
16072 -- visibility. The itype will be created anew in the instance.
16074 if Is_Itype (E)
16075 and then Ekind (E) = E_Access_Subtype
16076 and then Is_Entity_Name (N)
16077 and then Chars (Etype (E)) = Chars (N)
16078 then
16079 E := Etype (E);
16080 Set_Entity (N2, E);
16081 Set_Etype (N2, E);
16082 end if;
16084 if Is_Global (E) then
16085 Set_Global_Type (N, N2);
16087 elsif Nkind (N) = N_Op_Concat
16088 and then Is_Generic_Type (Etype (N2))
16089 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16090 or else
16091 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16092 and then Is_Intrinsic_Subprogram (E)
16093 then
16094 null;
16096 -- Entity is local. Mark generic node as unresolved. Note that now
16097 -- it does not have an entity.
16099 else
16100 Set_Associated_Node (N, Empty);
16101 Set_Etype (N, Empty);
16102 end if;
16104 if Nkind (Parent (N)) in N_Generic_Instantiation
16105 and then N = Name (Parent (N))
16106 then
16107 Save_Global_Defaults (Parent (N), Parent (N2));
16108 end if;
16110 elsif Nkind (Parent (N)) = N_Selected_Component
16111 and then Nkind (Parent (N2)) = N_Expanded_Name
16112 then
16113 -- In case of previous errors, the tree might be malformed
16115 if No (Entity (Parent (N2))) then
16116 null;
16118 elsif Is_Global (Entity (Parent (N2))) then
16119 Change_Selected_Component_To_Expanded_Name (Parent (N));
16120 Set_Associated_Node (Parent (N), Parent (N2));
16121 Set_Global_Type (Parent (N), Parent (N2));
16122 Save_Entity_Descendants (N);
16124 -- If this is a reference to the current generic entity, replace
16125 -- by the name of the generic homonym of the current package. This
16126 -- is because in an instantiation Par.P.Q will not resolve to the
16127 -- name of the instance, whose enclosing scope is not necessarily
16128 -- Par. We use the generic homonym rather that the name of the
16129 -- generic itself because it may be hidden by a local declaration.
16131 elsif In_Open_Scopes (Entity (Parent (N2)))
16132 and then not
16133 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16134 then
16135 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16136 Rewrite (Parent (N),
16137 Make_Identifier (Sloc (N),
16138 Chars =>
16139 Chars (Generic_Homonym (Entity (Parent (N2))))));
16140 else
16141 Rewrite (Parent (N),
16142 Make_Identifier (Sloc (N),
16143 Chars => Chars (Selector_Name (Parent (N2)))));
16144 end if;
16145 end if;
16147 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16148 and then Parent (N) = Name (Parent (Parent (N)))
16149 then
16150 Save_Global_Defaults
16151 (Parent (Parent (N)), Parent (Parent (N2)));
16152 end if;
16154 -- A selected component may denote a static constant that has been
16155 -- folded. If the static constant is global to the generic, capture
16156 -- its value. Otherwise the folding will happen in any instantiation.
16158 elsif Nkind (Parent (N)) = N_Selected_Component
16159 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16160 then
16161 if Present (Entity (Original_Node (Parent (N2))))
16162 and then Is_Global (Entity (Original_Node (Parent (N2))))
16163 then
16164 Rewrite (Parent (N), New_Copy (Parent (N2)));
16165 Set_Analyzed (Parent (N), False);
16166 end if;
16168 -- A selected component may be transformed into a parameterless
16169 -- function call. If the called entity is global, rewrite the node
16170 -- appropriately, i.e. as an extended name for the global entity.
16172 elsif Nkind (Parent (N)) = N_Selected_Component
16173 and then Nkind (Parent (N2)) = N_Function_Call
16174 and then N = Selector_Name (Parent (N))
16175 then
16176 if No (Parameter_Associations (Parent (N2))) then
16177 if Is_Global (Entity (Name (Parent (N2)))) then
16178 Change_Selected_Component_To_Expanded_Name (Parent (N));
16179 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16180 Set_Global_Type (Parent (N), Name (Parent (N2)));
16181 Save_Entity_Descendants (N);
16183 else
16184 Set_Is_Prefixed_Call (Parent (N));
16185 Set_Associated_Node (N, Empty);
16186 Set_Etype (N, Empty);
16187 end if;
16189 -- In Ada 2005, X.F may be a call to a primitive operation,
16190 -- rewritten as F (X). This rewriting will be done again in an
16191 -- instance, so keep the original node. Global entities will be
16192 -- captured as for other constructs. Indicate that this must
16193 -- resolve as a call, to prevent accidental overloading in the
16194 -- instance, if both a component and a primitive operation appear
16195 -- as candidates.
16197 else
16198 Set_Is_Prefixed_Call (Parent (N));
16199 end if;
16201 -- Entity is local. Reset in generic unit, so that node is resolved
16202 -- anew at the point of instantiation.
16204 else
16205 Set_Associated_Node (N, Empty);
16206 Set_Etype (N, Empty);
16207 end if;
16208 end Reset_Entity;
16210 -----------------------------
16211 -- Save_Entity_Descendants --
16212 -----------------------------
16214 procedure Save_Entity_Descendants (N : Node_Id) is
16215 begin
16216 case Nkind (N) is
16217 when N_Binary_Op =>
16218 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16219 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16221 when N_Unary_Op =>
16222 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16224 when N_Expanded_Name
16225 | N_Selected_Component
16227 Save_Global_Descendant (Union_Id (Prefix (N)));
16228 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16230 when N_Character_Literal
16231 | N_Identifier
16232 | N_Operator_Symbol
16234 null;
16236 when others =>
16237 raise Program_Error;
16238 end case;
16239 end Save_Entity_Descendants;
16241 --------------------------
16242 -- Save_Global_Defaults --
16243 --------------------------
16245 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16246 Loc : constant Source_Ptr := Sloc (N1);
16247 Assoc2 : constant List_Id := Generic_Associations (N2);
16248 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16249 Assoc1 : List_Id;
16250 Act1 : Node_Id;
16251 Act2 : Node_Id;
16252 Def : Node_Id;
16253 Ndec : Node_Id;
16254 Subp : Entity_Id;
16255 Actual : Entity_Id;
16257 begin
16258 Assoc1 := Generic_Associations (N1);
16260 if Present (Assoc1) then
16261 Act1 := First (Assoc1);
16262 else
16263 Act1 := Empty;
16264 Set_Generic_Associations (N1, New_List);
16265 Assoc1 := Generic_Associations (N1);
16266 end if;
16268 if Present (Assoc2) then
16269 Act2 := First (Assoc2);
16270 else
16271 return;
16272 end if;
16274 while Present (Act1) and then Present (Act2) loop
16275 Next (Act1);
16276 Next (Act2);
16277 end loop;
16279 -- Find the associations added for default subprograms
16281 if Present (Act2) then
16282 while Nkind (Act2) /= N_Generic_Association
16283 or else No (Entity (Selector_Name (Act2)))
16284 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16285 loop
16286 Next (Act2);
16287 end loop;
16289 -- Add a similar association if the default is global. The
16290 -- renaming declaration for the actual has been analyzed, and
16291 -- its alias is the program it renames. Link the actual in the
16292 -- original generic tree with the node in the analyzed tree.
16294 while Present (Act2) loop
16295 Subp := Entity (Selector_Name (Act2));
16296 Def := Explicit_Generic_Actual_Parameter (Act2);
16298 -- Following test is defence against rubbish errors
16300 if No (Alias (Subp)) then
16301 return;
16302 end if;
16304 -- Retrieve the resolved actual from the renaming declaration
16305 -- created for the instantiated formal.
16307 Actual := Entity (Name (Parent (Parent (Subp))));
16308 Set_Entity (Def, Actual);
16309 Set_Etype (Def, Etype (Actual));
16311 if Is_Global (Actual) then
16312 Ndec :=
16313 Make_Generic_Association (Loc,
16314 Selector_Name =>
16315 New_Occurrence_Of (Subp, Loc),
16316 Explicit_Generic_Actual_Parameter =>
16317 New_Occurrence_Of (Actual, Loc));
16319 Set_Associated_Node
16320 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16322 Append (Ndec, Assoc1);
16324 -- If there are other defaults, add a dummy association in case
16325 -- there are other defaulted formals with the same name.
16327 elsif Present (Next (Act2)) then
16328 Ndec :=
16329 Make_Generic_Association (Loc,
16330 Selector_Name =>
16331 New_Occurrence_Of (Subp, Loc),
16332 Explicit_Generic_Actual_Parameter => Empty);
16334 Append (Ndec, Assoc1);
16335 end if;
16337 Next (Act2);
16338 end loop;
16339 end if;
16341 if Nkind (Name (N1)) = N_Identifier
16342 and then Is_Child_Unit (Gen_Id)
16343 and then Is_Global (Gen_Id)
16344 and then Is_Generic_Unit (Scope (Gen_Id))
16345 and then In_Open_Scopes (Scope (Gen_Id))
16346 then
16347 -- This is an instantiation of a child unit within a sibling, so
16348 -- that the generic parent is in scope. An eventual instance must
16349 -- occur within the scope of an instance of the parent. Make name
16350 -- in instance into an expanded name, to preserve the identifier
16351 -- of the parent, so it can be resolved subsequently.
16353 Rewrite (Name (N2),
16354 Make_Expanded_Name (Loc,
16355 Chars => Chars (Gen_Id),
16356 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16357 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16358 Set_Entity (Name (N2), Gen_Id);
16360 Rewrite (Name (N1),
16361 Make_Expanded_Name (Loc,
16362 Chars => Chars (Gen_Id),
16363 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16364 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16366 Set_Associated_Node (Name (N1), Name (N2));
16367 Set_Associated_Node (Prefix (Name (N1)), Empty);
16368 Set_Associated_Node
16369 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16370 Set_Etype (Name (N1), Etype (Gen_Id));
16371 end if;
16372 end Save_Global_Defaults;
16374 ----------------------------
16375 -- Save_Global_Descendant --
16376 ----------------------------
16378 procedure Save_Global_Descendant (D : Union_Id) is
16379 N1 : Node_Id;
16381 begin
16382 if D in Node_Range then
16383 if D = Union_Id (Empty) then
16384 null;
16386 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16387 Save_References (Node_Id (D));
16388 end if;
16390 elsif D in List_Range then
16391 pragma Assert (D /= Union_Id (No_List));
16392 -- Because No_List = Empty, which is in Node_Range above
16394 N1 := First (List_Id (D));
16395 while Present (N1) loop
16396 Save_References (N1);
16397 Next (N1);
16398 end loop;
16400 -- Element list or other non-node field, nothing to do
16402 else
16403 null;
16404 end if;
16405 end Save_Global_Descendant;
16407 ---------------------
16408 -- Save_References --
16409 ---------------------
16411 -- This is the recursive procedure that does the work once the enclosing
16412 -- generic scope has been established. We have to treat specially a
16413 -- number of node rewritings that are required by semantic processing
16414 -- and which change the kind of nodes in the generic copy: typically
16415 -- constant-folding, replacing an operator node by a string literal, or
16416 -- a selected component by an expanded name. In each of those cases, the
16417 -- transformation is propagated to the generic unit.
16419 procedure Save_References (N : Node_Id) is
16420 Loc : constant Source_Ptr := Sloc (N);
16422 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16423 -- Determine whether arbitrary node Nod requires delayed capture of
16424 -- global references within its aspect specifications.
16426 procedure Save_References_In_Aggregate (N : Node_Id);
16427 -- Save all global references in [extension] aggregate node N
16429 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16430 -- Save all global references in a character literal or operator
16431 -- symbol denoted by N.
16433 procedure Save_References_In_Descendants (N : Node_Id);
16434 -- Save all global references in all descendants of node N
16436 procedure Save_References_In_Identifier (N : Node_Id);
16437 -- Save all global references in identifier node N
16439 procedure Save_References_In_Operator (N : Node_Id);
16440 -- Save all global references in operator node N
16442 procedure Save_References_In_Pragma (Prag : Node_Id);
16443 -- Save all global references found within the expression of pragma
16444 -- Prag.
16446 ---------------------------
16447 -- Requires_Delayed_Save --
16448 ---------------------------
16450 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16451 begin
16452 -- Generic packages and subprograms require delayed capture of
16453 -- global references within their aspects due to the timing of
16454 -- annotation analysis.
16456 if Nkind (Nod) in N_Generic_Package_Declaration
16457 | N_Generic_Subprogram_Declaration
16458 | N_Package_Body
16459 | N_Package_Body_Stub
16460 | N_Subprogram_Body
16461 | N_Subprogram_Body_Stub
16462 then
16463 -- Since the capture of global references is done on the
16464 -- unanalyzed generic template, there is no information around
16465 -- to infer the context. Use the Associated_Entity linkages to
16466 -- peek into the analyzed generic copy and determine what the
16467 -- template corresponds to.
16469 if Nod = Templ then
16470 return
16471 Is_Generic_Declaration_Or_Body
16472 (Unit_Declaration_Node
16473 (Get_Associated_Entity (Defining_Entity (Nod))));
16475 -- Otherwise the generic unit being processed is not the top
16476 -- level template. It is safe to capture of global references
16477 -- within the generic unit because at this point the top level
16478 -- copy is fully analyzed.
16480 else
16481 return False;
16482 end if;
16484 -- Otherwise capture the global references without interference
16486 else
16487 return False;
16488 end if;
16489 end Requires_Delayed_Save;
16491 ----------------------------------
16492 -- Save_References_In_Aggregate --
16493 ----------------------------------
16495 procedure Save_References_In_Aggregate (N : Node_Id) is
16496 Nam : Node_Id;
16497 Qual : Node_Id := Empty;
16498 Typ : Entity_Id := Empty;
16500 begin
16501 N2 := Get_Associated_Node (N);
16503 if Present (N2) then
16504 Typ := Etype (N2);
16506 -- In an instance within a generic, use the name of the actual
16507 -- and not the original generic parameter. If the actual is
16508 -- global in the current generic it must be preserved for its
16509 -- instantiation.
16511 if Parent_Kind (Typ) = N_Subtype_Declaration
16512 and then Present (Generic_Parent_Type (Parent (Typ)))
16513 then
16514 Typ := Base_Type (Typ);
16515 Set_Etype (N2, Typ);
16516 end if;
16517 end if;
16519 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16520 Set_Associated_Node (N, Empty);
16522 -- For a full aggregate, if the type is local but is a derived
16523 -- tagged type of a global ancestor, we will need to have the
16524 -- full view of this global ancestor available in the instance
16525 -- in order to analyze the full aggregate.
16527 if Present (N2)
16528 and then Nkind (N2) = N_Aggregate
16529 and then Present (Typ)
16530 and then Is_Tagged_Type (Typ)
16531 and then Is_Derived_Type (Typ)
16532 then
16533 declare
16534 Root_Typ : constant Entity_Id := Root_Type (Typ);
16536 Parent_Typ : Entity_Id := Typ;
16538 begin
16539 loop
16540 Parent_Typ := Etype (Parent_Typ);
16542 if Is_Global (Parent_Typ) then
16543 Set_Ancestor_Type (N, Parent_Typ);
16544 exit;
16545 end if;
16547 exit when Parent_Typ = Root_Typ;
16548 end loop;
16549 end;
16550 end if;
16552 -- If the aggregate is an actual in a call, it has been
16553 -- resolved in the current context, to some local type. The
16554 -- enclosing call may have been disambiguated by the aggregate,
16555 -- and this disambiguation might fail at instantiation time
16556 -- because the type to which the aggregate did resolve is not
16557 -- preserved. In order to preserve some of this information,
16558 -- wrap the aggregate in a qualified expression, using the id
16559 -- of its type. For further disambiguation we qualify the type
16560 -- name with its scope (if visible and not hidden by a local
16561 -- homograph) because both id's will have corresponding
16562 -- entities in an instance. This resolves most of the problems
16563 -- with missing type information on aggregates in instances.
16565 if Present (N2)
16566 and then Nkind (N2) = Nkind (N)
16567 and then Nkind (Parent (N2)) in N_Subprogram_Call
16568 and then Present (Typ)
16569 and then Comes_From_Source (Typ)
16570 then
16571 Nam := Make_Identifier (Loc, Chars (Typ));
16573 if Is_Immediately_Visible (Scope (Typ))
16574 and then
16575 (not In_Open_Scopes (Scope (Typ))
16576 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16577 then
16578 Nam :=
16579 Make_Selected_Component (Loc,
16580 Prefix =>
16581 Make_Identifier (Loc, Chars (Scope (Typ))),
16582 Selector_Name => Nam);
16583 end if;
16585 Qual :=
16586 Make_Qualified_Expression (Loc,
16587 Subtype_Mark => Nam,
16588 Expression => Relocate_Node (N));
16589 end if;
16591 -- For a full aggregate, if the type is global and a derived
16592 -- tagged type, we will also need to have the full view of its
16593 -- ancestor available in the instance in order to analyze the
16594 -- full aggregate.
16596 elsif Present (N2)
16597 and then Nkind (N2) = N_Aggregate
16598 and then Present (Typ)
16599 and then Is_Tagged_Type (Typ)
16600 and then Is_Derived_Type (Typ)
16601 then
16602 Set_Ancestor_Type (N, Etype (Typ));
16603 end if;
16605 if Nkind (N) = N_Aggregate then
16606 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16608 elsif Nkind (N) = N_Extension_Aggregate then
16609 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16611 else
16612 pragma Assert (False);
16613 end if;
16615 Save_Global_Descendant (Union_Id (Expressions (N)));
16616 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16617 Save_Global_Descendant (Union_Id (Etype (N)));
16619 if Present (Qual) then
16620 Rewrite (N, Qual);
16621 end if;
16622 end Save_References_In_Aggregate;
16624 ----------------------------------------------
16625 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16626 ----------------------------------------------
16628 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16629 begin
16630 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16631 Reset_Entity (N);
16633 elsif Nkind (N) = N_Operator_Symbol
16634 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16635 then
16636 Change_Operator_Symbol_To_String_Literal (N);
16637 end if;
16638 end Save_References_In_Char_Lit_Or_Op_Symbol;
16640 ------------------------------------
16641 -- Save_References_In_Descendants --
16642 ------------------------------------
16644 procedure Save_References_In_Descendants (N : Node_Id) is
16645 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16646 begin
16647 Walk (N);
16648 end Save_References_In_Descendants;
16650 -----------------------------------
16651 -- Save_References_In_Identifier --
16652 -----------------------------------
16654 procedure Save_References_In_Identifier (N : Node_Id) is
16655 begin
16656 -- The node did not undergo a transformation
16658 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16659 -- If this is a discriminant reference, always save it.
16660 -- It is used in the instance to find the corresponding
16661 -- discriminant positionally rather than by name.
16663 Set_Original_Discriminant
16664 (N, Original_Discriminant (Get_Associated_Node (N)));
16666 Reset_Entity (N);
16668 -- The analysis of the generic copy transformed the identifier
16669 -- into another construct. Propagate the changes to the template.
16671 else
16672 N2 := Get_Associated_Node (N);
16674 -- The identifier denotes a call to a parameterless function.
16675 -- Mark the node as resolved when the function is external.
16677 if Nkind (N2) = N_Function_Call then
16678 E := Entity (Name (N2));
16680 if Present (E) and then Is_Global (E) then
16681 Set_Global_Type (N, N2);
16682 else
16683 Set_Associated_Node (N, Empty);
16684 Set_Etype (N, Empty);
16685 end if;
16687 -- The identifier denotes a named number that was constant
16688 -- folded. Preserve the original name for ASIS and undo the
16689 -- constant folding which will be repeated in the instance.
16690 -- Is this still needed???
16692 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16693 and then Is_Entity_Name (Original_Node (N2))
16694 then
16695 Set_Associated_Node (N, Original_Node (N2));
16696 Reset_Entity (N);
16698 -- The identifier resolved to a string literal. Propagate this
16699 -- information to the generic template.
16701 elsif Nkind (N2) = N_String_Literal then
16702 Rewrite (N, New_Copy (N2));
16704 -- The identifier is rewritten as a dereference if it is the
16705 -- prefix of an implicit dereference. Preserve the original
16706 -- tree as the analysis of the instance will expand the node
16707 -- again, but preserve the resolved entity if it is global.
16709 elsif Nkind (N2) = N_Explicit_Dereference then
16710 if Is_Entity_Name (Prefix (N2))
16711 and then Present (Entity (Prefix (N2)))
16712 and then Is_Global (Entity (Prefix (N2)))
16713 then
16714 Set_Associated_Node (N, Prefix (N2));
16715 Set_Global_Type (N, Prefix (N2));
16717 elsif Nkind (Prefix (N2)) = N_Function_Call
16718 and then Is_Entity_Name (Name (Prefix (N2)))
16719 and then Present (Entity (Name (Prefix (N2))))
16720 and then Is_Global (Entity (Name (Prefix (N2))))
16721 then
16722 Rewrite (N,
16723 Make_Explicit_Dereference (Loc,
16724 Prefix =>
16725 Make_Function_Call (Loc,
16726 Name =>
16727 New_Occurrence_Of
16728 (Entity (Name (Prefix (N2))), Loc))));
16729 Set_Associated_Node
16730 (Name (Prefix (N)), Name (Prefix (N2)));
16731 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
16733 else
16734 Set_Associated_Node (N, Empty);
16735 Set_Etype (N, Empty);
16736 end if;
16738 -- The subtype mark of a nominally unconstrained object is
16739 -- rewritten as a subtype indication using the bounds of the
16740 -- expression. Recover the original subtype mark.
16742 elsif Nkind (N2) = N_Subtype_Indication
16743 and then Is_Entity_Name (Original_Node (N2))
16744 then
16745 Set_Associated_Node (N, Original_Node (N2));
16746 Reset_Entity (N);
16747 end if;
16748 end if;
16749 end Save_References_In_Identifier;
16751 ---------------------------------
16752 -- Save_References_In_Operator --
16753 ---------------------------------
16755 procedure Save_References_In_Operator (N : Node_Id) is
16756 begin
16757 N2 := Get_Associated_Node (N);
16759 -- The node did not undergo a transformation
16761 if Nkind (N) = Nkind (N2) then
16762 if Nkind (N) = N_Op_Concat then
16763 Set_Is_Component_Left_Opnd
16764 (N, Is_Component_Left_Opnd (N2));
16765 Set_Is_Component_Right_Opnd
16766 (N, Is_Component_Right_Opnd (N2));
16767 end if;
16769 Reset_Entity (N);
16771 -- The analysis of the generic copy transformed the operator into
16772 -- some other construct. Propagate the changes to the template if
16773 -- applicable.
16775 else
16776 -- The operator resoved to a function call
16778 if Nkind (N2) = N_Function_Call then
16780 -- Add explicit qualifications in the generic template for
16781 -- all operands of universal type. This aids resolution by
16782 -- preserving the actual type of a literal or an attribute
16783 -- that yields a universal result.
16785 Qualify_Universal_Operands (N, N2);
16787 E := Entity (Name (N2));
16789 if Present (E) and then Is_Global (E) then
16790 Set_Global_Type (N, N2);
16791 else
16792 Set_Associated_Node (N, Empty);
16793 Set_Etype (N, Empty);
16794 end if;
16796 -- The operator was folded into a literal
16798 elsif Nkind (N2) in N_Integer_Literal
16799 | N_Real_Literal
16800 | N_String_Literal
16801 then
16802 if Present (Original_Node (N2))
16803 and then Nkind (Original_Node (N2)) = Nkind (N)
16804 then
16805 -- Operation was constant-folded. Whenever possible,
16806 -- recover semantic information from unfolded node.
16807 -- This was initially done for ASIS but is apparently
16808 -- needed also for e.g. compiling a-nbnbin.adb.
16810 Set_Associated_Node (N, Original_Node (N2));
16812 if Nkind (N) = N_Op_Concat then
16813 Set_Is_Component_Left_Opnd (N,
16814 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16815 Set_Is_Component_Right_Opnd (N,
16816 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16817 end if;
16819 Reset_Entity (N);
16821 -- Propagate the constant folding back to the template
16823 else
16824 Rewrite (N, New_Copy (N2));
16825 Set_Analyzed (N, False);
16826 end if;
16828 -- The operator was folded into an enumeration literal. Retain
16829 -- the entity to avoid spurious ambiguities if it is overloaded
16830 -- at the point of instantiation or inlining.
16832 elsif Nkind (N2) = N_Identifier
16833 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16834 then
16835 Rewrite (N, New_Copy (N2));
16836 Set_Analyzed (N, False);
16837 end if;
16838 end if;
16840 -- Complete the operands check if node has not been constant
16841 -- folded.
16843 if Nkind (N) in N_Op then
16844 Save_Entity_Descendants (N);
16845 end if;
16846 end Save_References_In_Operator;
16848 -------------------------------
16849 -- Save_References_In_Pragma --
16850 -------------------------------
16852 procedure Save_References_In_Pragma (Prag : Node_Id) is
16853 Context : Node_Id;
16854 Do_Save : Boolean := True;
16856 begin
16857 -- Do not save global references in pragmas generated from aspects
16858 -- because the pragmas will be regenerated at instantiation time.
16860 if From_Aspect_Specification (Prag) then
16861 Do_Save := False;
16863 -- The capture of global references within contract-related source
16864 -- pragmas associated with generic packages, subprograms or their
16865 -- respective bodies must be delayed due to timing of annotation
16866 -- analysis. Global references are still captured in routine
16867 -- Save_Global_References_In_Contract.
16869 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16870 if Is_Package_Contract_Annotation (Prag) then
16871 Context := Find_Related_Package_Or_Body (Prag);
16872 else
16873 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16874 Context := Find_Related_Declaration_Or_Body (Prag);
16875 end if;
16877 -- The use of Original_Node accounts for the case when the
16878 -- related context is generic template.
16880 if Requires_Delayed_Save (Original_Node (Context)) then
16881 Do_Save := False;
16882 end if;
16883 end if;
16885 -- For all other cases, save all global references within the
16886 -- descendants, but skip the following semantic fields:
16887 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16889 if Do_Save then
16890 Save_Global_Descendant
16891 (Union_Id (Pragma_Argument_Associations (N)));
16892 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16893 end if;
16894 end Save_References_In_Pragma;
16896 -- Start of processing for Save_References
16898 begin
16899 if N = Empty then
16900 null;
16902 -- Aggregates
16904 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16905 Save_References_In_Aggregate (N);
16907 -- Character literals, operator symbols
16909 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16910 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16912 -- Defining identifiers
16914 elsif Nkind (N) in N_Entity then
16915 null;
16917 -- Identifiers
16919 elsif Nkind (N) = N_Identifier then
16920 Save_References_In_Identifier (N);
16922 -- Operators
16924 elsif Nkind (N) in N_Op then
16925 Save_References_In_Operator (N);
16927 -- Pragmas
16929 elsif Nkind (N) = N_Pragma then
16930 Save_References_In_Pragma (N);
16932 elsif Nkind (N) = N_Aspect_Specification then
16933 declare
16934 P : constant Node_Id := Parent (N);
16935 Expr : Node_Id;
16936 begin
16938 if Permits_Aspect_Specifications (P) then
16940 -- The capture of global references within aspects
16941 -- associated with generic packages, subprograms or
16942 -- their bodies must be delayed due to timing of
16943 -- annotation analysis. Global references are still
16944 -- captured in routine Save_Global_References_In_Contract.
16946 if Requires_Delayed_Save (Original_Node (P)) then
16947 null;
16949 -- Otherwise save all global references within the
16950 -- aspects
16952 else
16953 Expr := Expression (N);
16955 if Present (Expr) then
16956 Save_Global_References (Expr);
16957 end if;
16958 end if;
16959 end if;
16960 end;
16962 -- Do not walk the node pointed to by Label_Construct twice
16964 elsif Nkind (N) = N_Implicit_Label_Declaration then
16965 null;
16967 else
16968 Save_References_In_Descendants (N);
16969 end if;
16971 end Save_References;
16973 ---------------------
16974 -- Set_Global_Type --
16975 ---------------------
16977 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
16978 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
16979 Typ : constant Entity_Id :=
16980 (if Comparison then Compare_Type (N2) else Etype (N2));
16982 begin
16983 -- For a comparison (or equality) operator, the Etype is Boolean, so
16984 -- it is always global. But the type subject to the Has_Private_View
16985 -- processing is the Compare_Type, so we must specifically check it.
16987 if Comparison then
16988 Set_Etype (N, Etype (N2));
16990 if not Is_Global (Typ) then
16991 return;
16992 end if;
16994 Set_Compare_Type (N, Typ);
16996 else
16997 Set_Etype (N, Typ);
16998 end if;
17000 -- If the entity of N is not the associated node, this is a
17001 -- nested generic and it has an associated node as well, whose
17002 -- type is already the full view (see below). Indicate that the
17003 -- original node has a private view.
17005 if Entity (N) /= N2 then
17006 if Has_Private_View (Entity (N)) then
17007 Set_Has_Private_View (N);
17008 end if;
17010 if Has_Secondary_Private_View (Entity (N)) then
17011 Set_Has_Secondary_Private_View (N);
17012 end if;
17013 end if;
17015 -- If not a private type, deal with a secondary private view
17017 if not Is_Private_Type (Typ) then
17018 if (Is_Access_Type (Typ)
17019 and then Is_Private_Type (Designated_Type (Typ)))
17020 or else (Is_Array_Type (Typ)
17021 and then
17022 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17023 then
17024 Set_Has_Secondary_Private_View (N);
17025 end if;
17027 -- If it is a derivation of a private type in a context where no
17028 -- full view is needed, nothing to do either.
17030 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17031 null;
17033 -- Otherwise mark the type for flipping and set the full view on N2
17034 -- when available, which is necessary for Check_Private_View to swap
17035 -- back the views in case the full declaration of Typ is visible in
17036 -- the instantiation context. Note that this will be problematic if
17037 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17039 else
17040 Set_Has_Private_View (N);
17042 if Present (Full_View (Typ)) then
17043 if Comparison then
17044 Set_Compare_Type (N2, Full_View (Typ));
17045 else
17046 Set_Etype (N2, Full_View (Typ));
17047 end if;
17048 end if;
17049 end if;
17051 if Is_Floating_Point_Type (Typ)
17052 and then Has_Dimension_System (Typ)
17053 then
17054 Copy_Dimensions (N2, N);
17055 end if;
17056 end Set_Global_Type;
17058 -- Start of processing for Save_Global_References
17060 begin
17061 Gen_Scope := Current_Scope;
17063 -- If the generic unit is a child unit, references to entities in the
17064 -- parent are treated as local, because they will be resolved anew in
17065 -- the context of the instance of the parent.
17067 while Is_Child_Unit (Gen_Scope)
17068 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17069 loop
17070 Gen_Scope := Scope (Gen_Scope);
17071 end loop;
17073 Save_References (Templ);
17074 end Save_Global_References;
17076 ---------------------------------------
17077 -- Save_Global_References_In_Aspects --
17078 ---------------------------------------
17080 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17081 Asp : Node_Id;
17082 Expr : Node_Id;
17084 begin
17085 Asp := First (Aspect_Specifications (N));
17086 while Present (Asp) loop
17087 Expr := Expression (Asp);
17089 if Present (Expr) then
17090 Save_Global_References (Expr);
17091 end if;
17093 Next (Asp);
17094 end loop;
17095 end Save_Global_References_In_Aspects;
17097 ------------------------------------------
17098 -- Set_Copied_Sloc_For_Inherited_Pragma --
17099 ------------------------------------------
17101 procedure Set_Copied_Sloc_For_Inherited_Pragma
17102 (N : Node_Id;
17103 E : Entity_Id)
17105 begin
17106 Create_Instantiation_Source (N, E,
17107 Inlined_Body => False,
17108 Inherited_Pragma => True,
17109 Factor => S_Adjustment);
17110 end Set_Copied_Sloc_For_Inherited_Pragma;
17112 --------------------------------------
17113 -- Set_Copied_Sloc_For_Inlined_Body --
17114 --------------------------------------
17116 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
17117 begin
17118 Create_Instantiation_Source (N, E,
17119 Inlined_Body => True,
17120 Inherited_Pragma => False,
17121 Factor => S_Adjustment);
17122 end Set_Copied_Sloc_For_Inlined_Body;
17124 ---------------------
17125 -- Set_Instance_Of --
17126 ---------------------
17128 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
17129 begin
17130 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
17131 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
17132 Generic_Renamings.Increment_Last;
17133 end Set_Instance_Of;
17135 --------------------
17136 -- Set_Next_Assoc --
17137 --------------------
17139 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
17140 begin
17141 Generic_Renamings.Table (E).Next_In_HTable := Next;
17142 end Set_Next_Assoc;
17144 -------------------
17145 -- Start_Generic --
17146 -------------------
17148 procedure Start_Generic is
17149 begin
17150 -- ??? More things could be factored out in this routine.
17151 -- Should probably be done at a later stage.
17153 Generic_Flags.Append (Inside_A_Generic);
17154 Inside_A_Generic := True;
17156 Expander_Mode_Save_And_Set (False);
17157 end Start_Generic;
17159 ----------------------
17160 -- Set_Instance_Env --
17161 ----------------------
17163 -- WARNING: This routine manages SPARK regions
17165 procedure Set_Instance_Env
17166 (Gen_Unit : Entity_Id;
17167 Act_Unit : Entity_Id)
17169 Saved_AE : constant Boolean := Assertions_Enabled;
17170 Saved_CPL : constant Node_Id := Check_Policy_List;
17171 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
17172 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
17173 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
17175 begin
17176 -- Regardless of the current mode, predefined units are analyzed in the
17177 -- most current Ada mode, and earlier version Ada checks do not apply
17178 -- to predefined units. Nothing needs to be done for non-internal units.
17179 -- These are always analyzed in the current mode.
17181 if In_Internal_Unit (Gen_Unit) then
17183 -- The following call resets all configuration attributes to default
17184 -- or the xxx_Config versions of the attributes when the current sem
17185 -- unit is the main unit. At the same time, internal units must also
17186 -- inherit certain configuration attributes from their context. It
17187 -- is unclear what these two sets are.
17189 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17191 -- Reinstall relevant configuration attributes of the context
17193 Assertions_Enabled := Saved_AE;
17194 Check_Policy_List := Saved_CPL;
17195 Dynamic_Elaboration_Checks := Saved_DEC;
17197 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17198 end if;
17200 Current_Instantiated_Parent :=
17201 (Gen_Id => Gen_Unit,
17202 Act_Id => Act_Unit,
17203 Next_In_HTable => Assoc_Null);
17204 end Set_Instance_Env;
17206 -----------------
17207 -- Switch_View --
17208 -----------------
17210 procedure Switch_View (T : Entity_Id) is
17211 BT : constant Entity_Id := Base_Type (T);
17212 Priv_Elmt : Elmt_Id := No_Elmt;
17213 Priv_Sub : Entity_Id;
17215 begin
17216 -- T may be private but its base type may have been exchanged through
17217 -- some other occurrence, in which case there is nothing to switch
17218 -- besides T itself. Note that a private dependent subtype of a private
17219 -- type might not have been switched even if the base type has been,
17220 -- because of the last branch of Check_Private_View (see comment there).
17222 if not Is_Private_Type (BT) then
17223 Prepend_Elmt (Full_View (T), Exchanged_Views);
17224 Exchange_Declarations (T);
17225 return;
17226 end if;
17228 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17230 if Present (Full_View (BT)) then
17231 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17232 Exchange_Declarations (BT);
17233 end if;
17235 while Present (Priv_Elmt) loop
17236 Priv_Sub := Node (Priv_Elmt);
17238 if Present (Full_View (Priv_Sub)) then
17239 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17240 Exchange_Declarations (Priv_Sub);
17241 end if;
17243 Next_Elmt (Priv_Elmt);
17244 end loop;
17245 end Switch_View;
17247 -----------------
17248 -- True_Parent --
17249 -----------------
17251 function True_Parent (N : Node_Id) return Node_Id is
17252 begin
17253 if Nkind (Parent (N)) = N_Subunit then
17254 return Parent (Corresponding_Stub (Parent (N)));
17255 else
17256 return Parent (N);
17257 end if;
17258 end True_Parent;
17260 -----------------------------
17261 -- Valid_Default_Attribute --
17262 -----------------------------
17264 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17265 Attr_Id : constant Attribute_Id :=
17266 Get_Attribute_Id (Attribute_Name (Def));
17267 T : constant Entity_Id := Entity (Prefix (Def));
17268 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17269 F : Entity_Id;
17270 Num_F : Nat;
17271 OK : Boolean;
17273 begin
17274 if No (T) or else T = Any_Id then
17275 return;
17276 end if;
17278 Num_F := 0;
17279 F := First_Formal (Nam);
17280 while Present (F) loop
17281 Num_F := Num_F + 1;
17282 Next_Formal (F);
17283 end loop;
17285 case Attr_Id is
17286 when Attribute_Adjacent
17287 | Attribute_Ceiling
17288 | Attribute_Copy_Sign
17289 | Attribute_Floor
17290 | Attribute_Fraction
17291 | Attribute_Machine
17292 | Attribute_Model
17293 | Attribute_Remainder
17294 | Attribute_Rounding
17295 | Attribute_Unbiased_Rounding
17297 OK := Is_Fun
17298 and then Num_F = 1
17299 and then Is_Floating_Point_Type (T);
17301 when Attribute_Image
17302 | Attribute_Pred
17303 | Attribute_Succ
17304 | Attribute_Value
17305 | Attribute_Wide_Image
17306 | Attribute_Wide_Value
17308 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17310 when Attribute_Max
17311 | Attribute_Min
17313 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17315 when Attribute_Input =>
17316 OK := (Is_Fun and then Num_F = 1);
17318 when Attribute_Output
17319 | Attribute_Put_Image
17320 | Attribute_Read
17321 | Attribute_Write
17323 OK := not Is_Fun and then Num_F = 2;
17325 when others =>
17326 OK := False;
17327 end case;
17329 if not OK then
17330 Error_Msg_N
17331 ("attribute reference has wrong profile for subprogram", Def);
17332 end if;
17333 end Valid_Default_Attribute;
17335 ----------------------------------
17336 -- Validate_Formal_Type_Default --
17337 ----------------------------------
17339 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17340 Default : constant Node_Id :=
17341 Default_Subtype_Mark (Original_Node (Decl));
17342 Formal : constant Entity_Id := Defining_Identifier (Decl);
17344 Def_Sub : Entity_Id; -- Default subtype mark
17345 Type_Def : Node_Id;
17347 procedure Check_Discriminated_Formal;
17348 -- Check that discriminants of default for private or incomplete
17349 -- type match those of formal type.
17351 function Reference_Formal (N : Node_Id) return Traverse_Result;
17352 -- Check whether formal type definition mentions a previous formal
17353 -- type of the same generic.
17355 ----------------------
17356 -- Reference_Formal --
17357 ----------------------
17359 function Reference_Formal (N : Node_Id) return Traverse_Result is
17360 begin
17361 if Is_Entity_Name (N)
17362 and then Scope (Entity (N)) = Current_Scope
17363 then
17364 return Abandon;
17365 else
17366 return OK;
17367 end if;
17368 end Reference_Formal;
17370 function Depends_On_Other_Formals is
17371 new Traverse_Func (Reference_Formal);
17373 function Default_Subtype_Matches
17374 (Gen_T, Def_T : Entity_Id) return Boolean;
17376 procedure Validate_Array_Type_Default;
17377 -- Verify that dimension, indices, and component types of default
17378 -- are compatible with formal array type definition.
17380 procedure Validate_Derived_Type_Default;
17381 -- Verify that ancestor and progenitor types match.
17383 ---------------------------------
17384 -- Check_Discriminated_Formal --
17385 ---------------------------------
17387 procedure Check_Discriminated_Formal is
17388 Formal_Discr : Entity_Id;
17389 Actual_Discr : Entity_Id;
17390 Formal_Subt : Entity_Id;
17392 begin
17393 if Has_Discriminants (Formal) then
17394 if not Has_Discriminants (Def_Sub) then
17395 Error_Msg_NE
17396 ("default for & must have discriminants", Default, Formal);
17398 elsif Is_Constrained (Def_Sub) then
17399 Error_Msg_NE
17400 ("default for & must be unconstrained", Default, Formal);
17402 else
17403 Formal_Discr := First_Discriminant (Formal);
17404 Actual_Discr := First_Discriminant (Def_Sub);
17405 while Formal_Discr /= Empty loop
17406 if Actual_Discr = Empty then
17407 Error_Msg_N
17408 ("discriminants on Formal do not match formal",
17409 Default);
17410 end if;
17412 Formal_Subt := Etype (Formal_Discr);
17414 -- Access discriminants match if designated types do
17416 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17417 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17418 E_Anonymous_Access_Type
17419 and then
17420 Base_Type
17421 (Designated_Type (Base_Type (Formal_Subt))) =
17422 Base_Type
17423 (Designated_Type (Base_Type (Etype (Actual_Discr))))
17424 and then
17425 Subtypes_Statically_Match
17426 (Designated_Type (Base_Type (Formal_Subt)),
17427 Designated_Type (Base_Type (Etype (Actual_Discr))))
17428 then
17429 null;
17431 elsif Base_Type (Formal_Subt) /=
17432 Base_Type (Etype (Actual_Discr))
17433 then
17434 Error_Msg_N
17435 ("types of discriminants of default must match formal",
17436 Default);
17438 elsif not Subtypes_Statically_Match
17439 (Formal_Subt, Etype (Actual_Discr))
17440 and then Ada_Version >= Ada_95
17441 then
17442 Error_Msg_N
17443 ("subtypes of discriminants of default "
17444 & "must match formal",
17445 Default);
17446 end if;
17448 Next_Discriminant (Formal_Discr);
17449 Next_Discriminant (Actual_Discr);
17450 end loop;
17452 if Actual_Discr /= Empty then
17453 Error_Msg_NE
17454 ("discriminants on default do not match formal",
17455 Default, Formal);
17456 end if;
17457 end if;
17458 end if;
17459 end Check_Discriminated_Formal;
17461 ---------------------------
17462 -- Default_Subtype_Matches --
17463 ---------------------------
17465 function Default_Subtype_Matches
17466 (Gen_T, Def_T : Entity_Id) return Boolean
17468 begin
17469 -- Check that the base types, root types (when dealing with class
17470 -- wide types), or designated types (when dealing with anonymous
17471 -- access types) of Gen_T and Def_T are statically matching subtypes.
17473 return (Base_Type (Gen_T) = Base_Type (Def_T)
17474 and then Subtypes_Statically_Match (Gen_T, Def_T))
17476 or else (Is_Class_Wide_Type (Gen_T)
17477 and then Is_Class_Wide_Type (Def_T)
17478 and then Default_Subtype_Matches
17479 (Root_Type (Gen_T), Root_Type (Def_T)))
17481 or else (Is_Anonymous_Access_Type (Gen_T)
17482 and then Ekind (Def_T) = Ekind (Gen_T)
17483 and then Subtypes_Statically_Match
17484 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17486 end Default_Subtype_Matches;
17488 ----------------------------------
17489 -- Validate_Array_Type_Default --
17490 ----------------------------------
17492 procedure Validate_Array_Type_Default is
17493 I1, I2 : Node_Id;
17494 T2 : Entity_Id;
17495 begin
17496 if not Is_Array_Type (Def_Sub) then
17497 Error_Msg_NE ("default for& must be an array type ",
17498 Default, Formal);
17499 return;
17501 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17502 or else Is_Constrained (Def_Sub) /=
17503 Is_Constrained (Formal)
17504 then
17505 Error_Msg_NE ("default array type does not match&",
17506 Default, Formal);
17507 return;
17508 end if;
17510 I1 := First_Index (Formal);
17511 I2 := First_Index (Def_Sub);
17512 for J in 1 .. Number_Dimensions (Formal) loop
17514 -- If the indexes of the actual were given by a subtype_mark,
17515 -- the index was transformed into a range attribute. Retrieve
17516 -- the original type mark for checking.
17518 if Is_Entity_Name (Original_Node (I2)) then
17519 T2 := Entity (Original_Node (I2));
17520 else
17521 T2 := Etype (I2);
17522 end if;
17524 if not Subtypes_Statically_Match (Etype (I1), T2) then
17525 Error_Msg_NE
17526 ("index types of default do not match those of formal &",
17527 Default, Formal);
17528 end if;
17530 Next_Index (I1);
17531 Next_Index (I2);
17532 end loop;
17534 if not Default_Subtype_Matches
17535 (Component_Type (Formal), Component_Type (Def_Sub))
17536 then
17537 Error_Msg_NE
17538 ("component subtype of default does not match that of formal &",
17539 Default, Formal);
17540 end if;
17542 if Has_Aliased_Components (Formal)
17543 and then not Has_Aliased_Components (Default)
17544 then
17545 Error_Msg_NE
17546 ("default must have aliased components to match formal type &",
17547 Default, Formal);
17548 end if;
17549 end Validate_Array_Type_Default;
17551 -----------------------------------
17552 -- Validate_Derived_Type_Default --
17553 -----------------------------------
17555 procedure Validate_Derived_Type_Default is
17556 begin
17557 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17558 Error_Msg_NE ("default must be a descendent of&",
17559 Default, Etype (Formal));
17560 end if;
17562 if Has_Interfaces (Formal) then
17563 if not Has_Interfaces (Def_Sub) then
17564 Error_Msg_NE
17565 ("default must implement all interfaces of formal&",
17566 Default, Formal);
17568 else
17569 declare
17570 Iface : Node_Id;
17571 Iface_Ent : Entity_Id;
17573 begin
17574 Iface := First (Abstract_Interface_List (Formal));
17576 while Present (Iface) loop
17577 Iface_Ent := Entity (Iface);
17579 if Is_Ancestor (Iface_Ent, Def_Sub)
17580 or else Is_Progenitor (Iface_Ent, Def_Sub)
17581 then
17582 null;
17584 else
17585 Error_Msg_NE
17586 ("Default must implement interface&",
17587 Default, Etype (Iface));
17588 end if;
17590 Next (Iface);
17591 end loop;
17592 end;
17593 end if;
17594 end if;
17595 end Validate_Derived_Type_Default;
17597 -- Start of processing for Validate_Formal_Type_Default
17599 begin
17600 Analyze (Default);
17601 if not Is_Entity_Name (Default)
17602 or else not Is_Type (Entity (Default))
17603 then
17604 Error_Msg_N
17605 ("Expect type name for default of formal type", Default);
17606 return;
17607 else
17608 Def_Sub := Entity (Default);
17609 end if;
17611 -- Formal derived_type declarations are transformed into full
17612 -- type declarations or Private_Type_Extensions for ease of processing.
17614 if Nkind (Decl) = N_Full_Type_Declaration then
17615 Type_Def := Type_Definition (Decl);
17617 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17618 Type_Def := Subtype_Indication (Decl);
17620 else
17621 Type_Def := Formal_Type_Definition (Decl);
17622 end if;
17624 if Depends_On_Other_Formals (Type_Def) = Abandon
17625 and then Scope (Def_Sub) /= Current_Scope
17626 then
17627 Error_Msg_N ("default of formal type that depends on "
17628 & "other formals must be a previous formal type", Default);
17629 return;
17631 elsif Def_Sub = Formal then
17632 Error_Msg_N
17633 ("default for formal type cannot be formal itsef", Default);
17634 return;
17635 end if;
17637 case Nkind (Type_Def) is
17639 when N_Formal_Private_Type_Definition =>
17640 if (Is_Abstract_Type (Formal)
17641 and then not Is_Abstract_Type (Def_Sub))
17642 or else (Is_Limited_Type (Formal)
17643 and then not Is_Limited_Type (Def_Sub))
17644 then
17645 Error_Msg_NE
17646 ("default for private type$ does not match",
17647 Default, Formal);
17648 end if;
17650 Check_Discriminated_Formal;
17652 when N_Formal_Derived_Type_Definition =>
17653 Check_Discriminated_Formal;
17654 Validate_Derived_Type_Default;
17656 when N_Formal_Incomplete_Type_Definition =>
17657 if Is_Tagged_Type (Formal)
17658 and then not Is_Tagged_Type (Def_Sub)
17659 then
17660 Error_Msg_NE
17661 ("default for & must be a tagged type", Default, Formal);
17662 end if;
17664 Check_Discriminated_Formal;
17666 when N_Formal_Discrete_Type_Definition =>
17667 if not Is_Discrete_Type (Def_Sub) then
17668 Error_Msg_NE ("default for& must be a discrete type",
17669 Default, Formal);
17670 end if;
17672 when N_Formal_Signed_Integer_Type_Definition =>
17673 if not Is_Integer_Type (Def_Sub) then
17674 Error_Msg_NE ("default for& must be a discrete type",
17675 Default, Formal);
17676 end if;
17678 when N_Formal_Modular_Type_Definition =>
17679 if not Is_Modular_Integer_Type (Def_Sub) then
17680 Error_Msg_NE ("default for& must be a modular_integer Type",
17681 Default, Formal);
17682 end if;
17684 when N_Formal_Floating_Point_Definition =>
17685 if not Is_Floating_Point_Type (Def_Sub) then
17686 Error_Msg_NE ("default for& must be a floating_point type",
17687 Default, Formal);
17688 end if;
17690 when N_Formal_Ordinary_Fixed_Point_Definition =>
17691 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17692 Error_Msg_NE ("default for& must be "
17693 & "an ordinary_fixed_point type ",
17694 Default, Formal);
17695 end if;
17697 when N_Formal_Decimal_Fixed_Point_Definition =>
17698 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17699 Error_Msg_NE ("default for& must be "
17700 & "an Decimal_fixed_point type ",
17701 Default, Formal);
17702 end if;
17704 when N_Array_Type_Definition =>
17705 Validate_Array_Type_Default;
17707 when N_Access_Function_Definition |
17708 N_Access_Procedure_Definition =>
17709 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17710 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17711 Default, Formal);
17712 end if;
17713 Check_Subtype_Conformant
17714 (Designated_Type (Formal), Designated_Type (Def_Sub));
17716 when N_Access_To_Object_Definition =>
17717 if not Is_Access_Object_Type (Def_Sub) then
17718 Error_Msg_NE ("default for& must be an Access_To_Object",
17719 Default, Formal);
17721 elsif not Default_Subtype_Matches
17722 (Designated_Type (Formal), Designated_Type (Def_Sub))
17723 then
17724 Error_Msg_NE ("designated type of defaul does not match "
17725 & "designated type of formal type",
17726 Default, Formal);
17727 end if;
17729 when N_Record_Definition => -- Formal interface type
17730 if not Is_Interface (Def_Sub) then
17731 Error_Msg_NE
17732 ("default for formal interface type must be an interface",
17733 Default, Formal);
17735 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17736 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17737 or else Is_Protected_Interface (Formal) /=
17738 Is_Protected_Interface (Def_Sub)
17739 or else Is_Synchronized_Interface (Formal) /=
17740 Is_Synchronized_Interface (Def_Sub)
17741 then
17742 Error_Msg_NE
17743 ("default for interface& does not match", Def_Sub, Formal);
17744 end if;
17746 when N_Derived_Type_Definition =>
17747 Validate_Derived_Type_Default;
17749 when N_Identifier => -- case of a private extension
17750 Validate_Derived_Type_Default;
17752 when N_Error =>
17753 null;
17755 when others =>
17756 raise Program_Error;
17757 end case;
17758 end Validate_Formal_Type_Default;
17759 end Sem_Ch12;