Skip various cmp-mem-const tests on lp64 hppa*-*-*
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob1d17cfacec30a0907a96f2cbd3a298d24cb37ce4
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 Subtype_Mark (Def) <= Empty_Or_Error then
2545 pragma Assert (Serious_Errors_Detected > 0);
2546 -- avoid passing bad argument to Entity
2547 return;
2548 end if;
2550 -- If the parent type has a known size, so does the formal, which makes
2551 -- legal representation clauses that involve the formal.
2553 Set_Size_Known_At_Compile_Time
2554 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2555 end Analyze_Formal_Derived_Type;
2557 ----------------------------------
2558 -- Analyze_Formal_Discrete_Type --
2559 ----------------------------------
2561 -- The operations defined for a discrete types are those of an enumeration
2562 -- type. The size is set to an arbitrary value, for use in analyzing the
2563 -- generic unit.
2565 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2566 Loc : constant Source_Ptr := Sloc (Def);
2567 Lo : Node_Id;
2568 Hi : Node_Id;
2570 Base : constant Entity_Id :=
2571 New_Internal_Entity
2572 (E_Floating_Point_Type, Current_Scope,
2573 Sloc (Defining_Identifier (Parent (Def))), 'G');
2575 begin
2576 Enter_Name (T);
2577 Mutate_Ekind (T, E_Enumeration_Subtype);
2578 Set_Etype (T, Base);
2579 Init_Size (T, 8);
2580 Reinit_Alignment (T);
2581 Set_Is_Generic_Type (T);
2582 Set_Is_Constrained (T);
2584 -- For semantic analysis, the bounds of the type must be set to some
2585 -- non-static value. The simplest is to create attribute nodes for those
2586 -- bounds, that refer to the type itself. These bounds are never
2587 -- analyzed but serve as place-holders.
2589 Lo :=
2590 Make_Attribute_Reference (Loc,
2591 Attribute_Name => Name_First,
2592 Prefix => New_Occurrence_Of (T, Loc));
2593 Set_Etype (Lo, T);
2595 Hi :=
2596 Make_Attribute_Reference (Loc,
2597 Attribute_Name => Name_Last,
2598 Prefix => New_Occurrence_Of (T, Loc));
2599 Set_Etype (Hi, T);
2601 Set_Scalar_Range (T,
2602 Make_Range (Loc,
2603 Low_Bound => Lo,
2604 High_Bound => Hi));
2606 Mutate_Ekind (Base, E_Enumeration_Type);
2607 Set_Etype (Base, Base);
2608 Init_Size (Base, 8);
2609 Reinit_Alignment (Base);
2610 Set_Is_Generic_Type (Base);
2611 Set_Scalar_Range (Base, Scalar_Range (T));
2612 Set_Parent (Base, Parent (Def));
2613 end Analyze_Formal_Discrete_Type;
2615 ----------------------------------
2616 -- Analyze_Formal_Floating_Type --
2617 ---------------------------------
2619 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2620 Base : constant Entity_Id :=
2621 New_Internal_Entity
2622 (E_Floating_Point_Type, Current_Scope,
2623 Sloc (Defining_Identifier (Parent (Def))), 'G');
2625 begin
2626 -- The various semantic attributes are taken from the predefined type
2627 -- Float, just so that all of them are initialized. Their values are
2628 -- never used because no constant folding or expansion takes place in
2629 -- the generic itself.
2631 Enter_Name (T);
2632 Mutate_Ekind (T, E_Floating_Point_Subtype);
2633 Set_Etype (T, Base);
2634 Set_Size_Info (T, (Standard_Float));
2635 Set_RM_Size (T, RM_Size (Standard_Float));
2636 Set_Digits_Value (T, Digits_Value (Standard_Float));
2637 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2638 Set_Is_Constrained (T);
2640 Set_Is_Generic_Type (Base);
2641 Set_Etype (Base, Base);
2642 Set_Size_Info (Base, (Standard_Float));
2643 Set_RM_Size (Base, RM_Size (Standard_Float));
2644 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2645 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2646 Set_Parent (Base, Parent (Def));
2648 Check_Restriction (No_Floating_Point, Def);
2649 end Analyze_Formal_Floating_Type;
2651 -----------------------------------
2652 -- Analyze_Formal_Interface_Type;--
2653 -----------------------------------
2655 procedure Analyze_Formal_Interface_Type
2656 (N : Node_Id;
2657 T : Entity_Id;
2658 Def : Node_Id)
2660 Loc : constant Source_Ptr := Sloc (N);
2661 New_N : Node_Id;
2663 begin
2664 New_N :=
2665 Make_Full_Type_Declaration (Loc,
2666 Defining_Identifier => T,
2667 Type_Definition => Def);
2669 Rewrite (N, New_N);
2671 -- Keep the aspects from the original node
2673 Move_Aspects (Original_Node (N), N);
2675 Analyze (N);
2676 Set_Is_Generic_Type (T);
2677 end Analyze_Formal_Interface_Type;
2679 ---------------------------------
2680 -- Analyze_Formal_Modular_Type --
2681 ---------------------------------
2683 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2684 begin
2685 -- Apart from their entity kind, generic modular types are treated like
2686 -- signed integer types, and have the same attributes.
2688 Analyze_Formal_Signed_Integer_Type (T, Def);
2689 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2690 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2692 end Analyze_Formal_Modular_Type;
2694 ---------------------------------------
2695 -- Analyze_Formal_Object_Declaration --
2696 ---------------------------------------
2698 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2699 E : constant Node_Id := Default_Expression (N);
2700 Id : constant Node_Id := Defining_Identifier (N);
2702 K : Entity_Kind;
2703 Parent_Installed : Boolean := False;
2704 T : Node_Id;
2706 begin
2707 Enter_Name (Id);
2709 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2711 -- Determine the mode of the formal object
2713 if Out_Present (N) then
2714 K := E_Generic_In_Out_Parameter;
2716 if not In_Present (N) then
2717 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2718 end if;
2720 else
2721 K := E_Generic_In_Parameter;
2722 end if;
2724 if Present (Subtype_Mark (N)) then
2725 Find_Type (Subtype_Mark (N));
2726 T := Entity (Subtype_Mark (N));
2728 -- Verify that there is no redundant null exclusion
2730 if Null_Exclusion_Present (N) then
2731 if not Is_Access_Type (T) then
2732 Error_Msg_N
2733 ("null exclusion can only apply to an access type", N);
2735 elsif Can_Never_Be_Null (T) then
2736 Error_Msg_NE
2737 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2738 end if;
2739 end if;
2741 -- Ada 2005 (AI-423): Formal object with an access definition
2743 else
2744 Check_Access_Definition (N);
2745 T := Access_Definition
2746 (Related_Nod => N,
2747 N => Access_Definition (N));
2748 end if;
2750 if Ekind (T) = E_Incomplete_Type then
2751 declare
2752 Error_Node : Node_Id;
2754 begin
2755 if Present (Subtype_Mark (N)) then
2756 Error_Node := Subtype_Mark (N);
2757 else
2758 Check_Access_Definition (N);
2759 Error_Node := Access_Definition (N);
2760 end if;
2762 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2763 end;
2764 end if;
2766 if K = E_Generic_In_Parameter then
2768 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2770 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2771 Error_Msg_N
2772 ("generic formal of mode IN must not be of limited type", N);
2773 Explain_Limited_Type (T, N);
2774 end if;
2776 if Is_Abstract_Type (T) then
2777 Error_Msg_N
2778 ("generic formal of mode IN must not be of abstract type", N);
2779 end if;
2781 if Present (E) then
2782 Preanalyze_Spec_Expression (E, T);
2784 -- The default for a ghost generic formal IN parameter of
2785 -- access-to-variable type should be a ghost object (SPARK
2786 -- RM 6.9(13)).
2788 if Is_Access_Variable (T) then
2789 Check_Ghost_Formal_Variable
2790 (Actual => E,
2791 Formal => Id,
2792 Is_Default => True);
2793 end if;
2795 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2796 Error_Msg_N
2797 ("initialization not allowed for limited types", E);
2798 Explain_Limited_Type (T, E);
2799 end if;
2800 end if;
2802 Mutate_Ekind (Id, K);
2803 Set_Etype (Id, T);
2805 -- Case of generic IN OUT parameter
2807 else
2808 -- If the formal has an unconstrained type, construct its actual
2809 -- subtype, as is done for subprogram formals. In this fashion, all
2810 -- its uses can refer to specific bounds.
2812 Mutate_Ekind (Id, K);
2813 Set_Etype (Id, T);
2815 if (Is_Array_Type (T) and then not Is_Constrained (T))
2816 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2817 then
2818 declare
2819 Non_Freezing_Ref : constant Node_Id :=
2820 New_Occurrence_Of (Id, Sloc (Id));
2821 Decl : Node_Id;
2823 begin
2824 -- Make sure the actual subtype doesn't generate bogus freezing
2826 Set_Must_Not_Freeze (Non_Freezing_Ref);
2827 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2828 Insert_Before_And_Analyze (N, Decl);
2829 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2830 end;
2831 else
2832 Set_Actual_Subtype (Id, T);
2833 end if;
2835 if Present (E) then
2836 Error_Msg_N
2837 ("initialization not allowed for `IN OUT` formals", N);
2838 end if;
2839 end if;
2841 Analyze_Aspect_Specifications (N, Id);
2843 if Parent_Installed then
2844 Remove_Parent;
2845 end if;
2846 end Analyze_Formal_Object_Declaration;
2848 ----------------------------------------------
2849 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2850 ----------------------------------------------
2852 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2853 (T : Entity_Id;
2854 Def : Node_Id)
2856 Loc : constant Source_Ptr := Sloc (Def);
2857 Base : constant Entity_Id :=
2858 New_Internal_Entity
2859 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2860 Sloc (Defining_Identifier (Parent (Def))), 'G');
2862 begin
2863 -- The semantic attributes are set for completeness only, their values
2864 -- will never be used, since all properties of the type are non-static.
2866 Enter_Name (T);
2867 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2868 Set_Etype (T, Base);
2869 Set_Size_Info (T, Standard_Integer);
2870 Set_RM_Size (T, RM_Size (Standard_Integer));
2871 Set_Small_Value (T, Ureal_1);
2872 Set_Delta_Value (T, Ureal_1);
2873 Set_Scalar_Range (T,
2874 Make_Range (Loc,
2875 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2876 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2877 Set_Is_Constrained (T);
2879 Set_Is_Generic_Type (Base);
2880 Set_Etype (Base, Base);
2881 Set_Size_Info (Base, Standard_Integer);
2882 Set_RM_Size (Base, RM_Size (Standard_Integer));
2883 Set_Small_Value (Base, Ureal_1);
2884 Set_Delta_Value (Base, Ureal_1);
2885 Set_Scalar_Range (Base, Scalar_Range (T));
2886 Set_Parent (Base, Parent (Def));
2888 Check_Restriction (No_Fixed_Point, Def);
2889 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2891 ----------------------------------------
2892 -- Analyze_Formal_Package_Declaration --
2893 ----------------------------------------
2895 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2896 Gen_Id : constant Node_Id := Name (N);
2897 Loc : constant Source_Ptr := Sloc (N);
2898 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2899 Formal : Entity_Id;
2900 Gen_Decl : Node_Id;
2901 Gen_Unit : Entity_Id;
2902 Renaming : Node_Id;
2904 Vis_Prims_List : Elist_Id := No_Elist;
2905 -- List of primitives made temporarily visible in the instantiation
2906 -- to match the visibility of the formal type.
2908 function Build_Local_Package return Node_Id;
2909 -- The formal package is rewritten so that its parameters are replaced
2910 -- with corresponding declarations. For parameters with bona fide
2911 -- associations these declarations are created by Analyze_Associations
2912 -- as for a regular instantiation. For boxed parameters, we preserve
2913 -- the formal declarations and analyze them, in order to introduce
2914 -- entities of the right kind in the environment of the formal.
2916 -------------------------
2917 -- Build_Local_Package --
2918 -------------------------
2920 function Build_Local_Package return Node_Id is
2921 Decls : List_Id;
2922 Pack_Decl : Node_Id;
2924 begin
2925 -- Within the formal, the name of the generic package is a renaming
2926 -- of the formal (as for a regular instantiation).
2928 Pack_Decl :=
2929 Make_Package_Declaration (Loc,
2930 Specification =>
2931 Copy_Generic_Node
2932 (Specification (Original_Node (Gen_Decl)),
2933 Empty, Instantiating => True));
2935 Renaming :=
2936 Make_Package_Renaming_Declaration (Loc,
2937 Defining_Unit_Name =>
2938 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2939 Name => New_Occurrence_Of (Formal, Loc));
2941 if Nkind (Gen_Id) = N_Identifier
2942 and then Chars (Gen_Id) = Chars (Pack_Id)
2943 then
2944 Error_Msg_NE
2945 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2946 end if;
2948 -- If the formal is declared with a box, or with an others choice,
2949 -- create corresponding declarations for all entities in the formal
2950 -- part, so that names with the proper types are available in the
2951 -- specification of the formal package.
2953 -- On the other hand, if there are no associations, then all the
2954 -- formals must have defaults, and this will be checked by the
2955 -- call to Analyze_Associations.
2957 if Box_Present (N)
2958 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2959 then
2960 declare
2961 Formal_Decl : Node_Id;
2963 begin
2964 -- TBA : for a formal package, need to recurse ???
2966 Decls := New_List;
2967 Formal_Decl :=
2968 First
2969 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2970 while Present (Formal_Decl) loop
2971 Append_To
2972 (Decls,
2973 Copy_Generic_Node
2974 (Formal_Decl, Empty, Instantiating => True));
2975 Next (Formal_Decl);
2976 end loop;
2977 end;
2979 -- If generic associations are present, use Analyze_Associations to
2980 -- create the proper renaming declarations.
2982 else
2983 declare
2984 Act_Tree : constant Node_Id :=
2985 Copy_Generic_Node
2986 (Original_Node (Gen_Decl), Empty,
2987 Instantiating => True);
2989 begin
2990 Generic_Renamings.Set_Last (0);
2991 Generic_Renamings_HTable.Reset;
2992 Instantiation_Node := N;
2994 Decls :=
2995 Analyze_Associations
2996 (I_Node => Original_Node (N),
2997 Formals => Generic_Formal_Declarations (Act_Tree),
2998 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3000 Vis_Prims_List := Check_Hidden_Primitives (Decls);
3001 end;
3002 end if;
3004 Append (Renaming, To => Decls);
3006 -- Add generated declarations ahead of local declarations in
3007 -- the package.
3009 if No (Visible_Declarations (Specification (Pack_Decl))) then
3010 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3011 else
3012 Insert_List_Before
3013 (First (Visible_Declarations (Specification (Pack_Decl))),
3014 Decls);
3015 end if;
3017 return Pack_Decl;
3018 end Build_Local_Package;
3020 -- Local variables
3022 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3023 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3025 Associations : Boolean := True;
3026 New_N : Node_Id;
3027 Parent_Installed : Boolean := False;
3028 Parent_Instance : Entity_Id;
3029 Renaming_In_Par : Entity_Id;
3031 -- Start of processing for Analyze_Formal_Package_Declaration
3033 begin
3034 Check_Text_IO_Special_Unit (Gen_Id);
3036 Init_Env;
3037 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3038 Gen_Unit := Entity (Gen_Id);
3040 -- Check for a formal package that is a package renaming
3042 if Present (Renamed_Entity (Gen_Unit)) then
3044 -- Indicate that unit is used, before replacing it with renamed
3045 -- entity for use below.
3047 if In_Extended_Main_Source_Unit (N) then
3048 Set_Is_Instantiated (Gen_Unit);
3049 Generate_Reference (Gen_Unit, N);
3050 end if;
3052 Gen_Unit := Renamed_Entity (Gen_Unit);
3053 end if;
3055 if Ekind (Gen_Unit) /= E_Generic_Package then
3056 Error_Msg_N ("expect generic package name", Gen_Id);
3057 Restore_Env;
3058 goto Leave;
3060 elsif Gen_Unit = Current_Scope then
3061 Error_Msg_N
3062 ("generic package cannot be used as a formal package of itself",
3063 Gen_Id);
3064 Restore_Env;
3065 goto Leave;
3067 elsif In_Open_Scopes (Gen_Unit) then
3068 if Is_Compilation_Unit (Gen_Unit)
3069 and then Is_Child_Unit (Current_Scope)
3070 then
3071 -- Special-case the error when the formal is a parent, and
3072 -- continue analysis to minimize cascaded errors.
3074 Error_Msg_N
3075 ("generic parent cannot be used as formal package of a child "
3076 & "unit", Gen_Id);
3078 else
3079 Error_Msg_N
3080 ("generic package cannot be used as a formal package within "
3081 & "itself", Gen_Id);
3082 Restore_Env;
3083 goto Leave;
3084 end if;
3085 end if;
3087 -- Check that name of formal package does not hide name of generic,
3088 -- or its leading prefix. This check must be done separately because
3089 -- the name of the generic has already been analyzed.
3091 declare
3092 Gen_Name : Entity_Id;
3094 begin
3095 Gen_Name := Gen_Id;
3096 while Nkind (Gen_Name) = N_Expanded_Name loop
3097 Gen_Name := Prefix (Gen_Name);
3098 end loop;
3100 if Chars (Gen_Name) = Chars (Pack_Id) then
3101 Error_Msg_NE
3102 ("& is hidden within declaration of formal package",
3103 Gen_Id, Gen_Name);
3104 end if;
3105 end;
3107 if Box_Present (N)
3108 or else No (Generic_Associations (N))
3109 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3110 then
3111 Associations := False;
3112 end if;
3114 -- If there are no generic associations, the generic parameters appear
3115 -- as local entities and are instantiated like them. We copy the generic
3116 -- package declaration as if it were an instantiation, and analyze it
3117 -- like a regular package, except that we treat the formals as
3118 -- additional visible components.
3120 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3122 if In_Extended_Main_Source_Unit (N) then
3123 Set_Is_Instantiated (Gen_Unit);
3124 Generate_Reference (Gen_Unit, N);
3125 end if;
3127 Formal := New_Copy (Pack_Id);
3128 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3130 -- Make local generic without formals. The formals will be replaced with
3131 -- internal declarations.
3133 begin
3134 New_N := Build_Local_Package;
3136 -- If there are errors in the parameter list, Analyze_Associations
3137 -- raises Instantiation_Error. Patch the declaration to prevent further
3138 -- exception propagation.
3140 exception
3141 when Instantiation_Error =>
3142 Enter_Name (Formal);
3143 Mutate_Ekind (Formal, E_Variable);
3144 Set_Etype (Formal, Any_Type);
3145 Restore_Hidden_Primitives (Vis_Prims_List);
3147 if Parent_Installed then
3148 Remove_Parent;
3149 end if;
3151 goto Leave;
3152 end;
3154 Rewrite (N, New_N);
3155 Set_Defining_Unit_Name (Specification (New_N), Formal);
3156 Set_Generic_Parent (Specification (N), Gen_Unit);
3157 Set_Instance_Env (Gen_Unit, Formal);
3158 Set_Is_Generic_Instance (Formal);
3160 Enter_Name (Formal);
3161 Mutate_Ekind (Formal, E_Package);
3162 Set_Etype (Formal, Standard_Void_Type);
3163 Set_Inner_Instances (Formal, New_Elmt_List);
3165 -- It is unclear that any aspects can apply to a formal package
3166 -- declaration, given that they look like a hidden conformance
3167 -- requirement on the corresponding actual. However, Abstract_State
3168 -- must be treated specially because it generates declarations that
3169 -- must appear before other declarations in the specification and
3170 -- must be analyzed at once.
3172 if Present (Aspect_Specifications (Gen_Decl)) then
3173 if No (Aspect_Specifications (N)) then
3174 Set_Aspect_Specifications (N, New_List);
3175 end if;
3177 declare
3178 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3179 New_A : Node_Id;
3181 begin
3182 while Present (ASN) loop
3183 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3184 New_A :=
3185 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3186 Set_Entity (New_A, Formal);
3187 Set_Analyzed (New_A, False);
3188 Append (New_A, Aspect_Specifications (N));
3189 Analyze_Aspect_Specifications (N, Formal);
3190 exit;
3191 end if;
3193 Next (ASN);
3194 end loop;
3195 end;
3196 end if;
3198 Push_Scope (Formal);
3200 -- Manually set the SPARK_Mode from the context because the package
3201 -- declaration is never analyzed.
3203 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3204 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3205 Set_SPARK_Pragma_Inherited (Formal);
3206 Set_SPARK_Aux_Pragma_Inherited (Formal);
3208 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3210 -- Similarly, we have to make the name of the formal visible in the
3211 -- parent instance, to resolve properly fully qualified names that
3212 -- may appear in the generic unit. The parent instance has been
3213 -- placed on the scope stack ahead of the current scope.
3215 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3217 Renaming_In_Par :=
3218 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3219 Mutate_Ekind (Renaming_In_Par, E_Package);
3220 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3221 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3222 Set_Scope (Renaming_In_Par, Parent_Instance);
3223 Set_Parent (Renaming_In_Par, Parent (Formal));
3224 Set_Renamed_Entity (Renaming_In_Par, Formal);
3225 Append_Entity (Renaming_In_Par, Parent_Instance);
3226 end if;
3228 -- A formal package declaration behaves as a package instantiation with
3229 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3230 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3231 -- all SPARK_Mode pragmas within the generic_package_name.
3233 if SPARK_Mode /= On then
3234 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3236 -- Mark the formal spec in case the body is instantiated at a later
3237 -- pass. This preserves the original context in effect for the body.
3239 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3240 end if;
3242 Analyze (Specification (N));
3244 -- The formals for which associations are provided are not visible
3245 -- outside of the formal package. The others are still declared by a
3246 -- formal parameter declaration.
3248 -- If there are no associations, the only local entity to hide is the
3249 -- generated package renaming itself.
3251 declare
3252 E : Entity_Id;
3254 begin
3255 E := First_Entity (Formal);
3256 while Present (E) loop
3257 if Associations and then not Is_Generic_Formal (E) then
3258 Set_Is_Hidden (E);
3259 end if;
3261 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3262 Set_Is_Hidden (E);
3263 exit;
3264 end if;
3266 Next_Entity (E);
3267 end loop;
3268 end;
3270 End_Package_Scope (Formal);
3271 Restore_Hidden_Primitives (Vis_Prims_List);
3273 if Parent_Installed then
3274 Remove_Parent;
3275 end if;
3277 Restore_Env;
3279 -- Inside the generic unit, the formal package is a regular package, but
3280 -- no body is needed for it. Note that after instantiation, the defining
3281 -- unit name we need is in the new tree and not in the original (see
3282 -- Package_Instantiation). A generic formal package is an instance, and
3283 -- can be used as an actual for an inner instance.
3285 Set_Has_Completion (Formal, True);
3287 -- Add semantic information to the original defining identifier.
3289 Mutate_Ekind (Pack_Id, E_Package);
3290 Set_Etype (Pack_Id, Standard_Void_Type);
3291 Set_Scope (Pack_Id, Scope (Formal));
3292 Set_Has_Completion (Pack_Id, True);
3294 <<Leave>>
3295 -- Unclear that any other aspects may appear here, analyze them
3296 -- for completion, given that the grammar allows their appearance.
3298 Analyze_Aspect_Specifications (N, Pack_Id);
3300 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3301 end Analyze_Formal_Package_Declaration;
3303 ---------------------------------
3304 -- Analyze_Formal_Private_Type --
3305 ---------------------------------
3307 procedure Analyze_Formal_Private_Type
3308 (N : Node_Id;
3309 T : Entity_Id;
3310 Def : Node_Id)
3312 begin
3313 New_Private_Type (N, T, Def);
3315 -- Set the size to an arbitrary but legal value
3317 Set_Size_Info (T, Standard_Integer);
3318 Set_RM_Size (T, RM_Size (Standard_Integer));
3319 end Analyze_Formal_Private_Type;
3321 ------------------------------------
3322 -- Analyze_Formal_Incomplete_Type --
3323 ------------------------------------
3325 procedure Analyze_Formal_Incomplete_Type
3326 (T : Entity_Id;
3327 Def : Node_Id)
3329 begin
3330 Enter_Name (T);
3331 Mutate_Ekind (T, E_Incomplete_Type);
3332 Set_Etype (T, T);
3333 Set_Private_Dependents (T, New_Elmt_List);
3335 if Tagged_Present (Def) then
3336 Set_Is_Tagged_Type (T);
3337 Make_Class_Wide_Type (T);
3338 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3339 end if;
3340 end Analyze_Formal_Incomplete_Type;
3342 ----------------------------------------
3343 -- Analyze_Formal_Signed_Integer_Type --
3344 ----------------------------------------
3346 procedure Analyze_Formal_Signed_Integer_Type
3347 (T : Entity_Id;
3348 Def : Node_Id)
3350 Base : constant Entity_Id :=
3351 New_Internal_Entity
3352 (E_Signed_Integer_Type,
3353 Current_Scope,
3354 Sloc (Defining_Identifier (Parent (Def))), 'G');
3356 begin
3357 Enter_Name (T);
3359 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3360 Set_Etype (T, Base);
3361 Set_Size_Info (T, Standard_Integer);
3362 Set_RM_Size (T, RM_Size (Standard_Integer));
3363 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3364 Set_Is_Constrained (T);
3366 Set_Is_Generic_Type (Base);
3367 Set_Size_Info (Base, Standard_Integer);
3368 Set_RM_Size (Base, RM_Size (Standard_Integer));
3369 Set_Etype (Base, Base);
3370 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3371 Set_Parent (Base, Parent (Def));
3372 end Analyze_Formal_Signed_Integer_Type;
3374 -------------------------------------------
3375 -- Analyze_Formal_Subprogram_Declaration --
3376 -------------------------------------------
3378 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3379 Spec : constant Node_Id := Specification (N);
3380 Def : constant Node_Id := Default_Name (N);
3381 Expr : constant Node_Id := Expression (N);
3382 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3384 Parent_Installed : Boolean := False;
3385 Subp : Entity_Id;
3387 begin
3388 if Nam = Error then
3389 return;
3390 end if;
3392 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3393 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3394 goto Leave;
3395 end if;
3397 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3399 Analyze_Subprogram_Declaration (N);
3400 Set_Is_Formal_Subprogram (Nam);
3401 Set_Has_Completion (Nam);
3403 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3404 Set_Is_Abstract_Subprogram (Nam);
3406 Set_Is_Dispatching_Operation (Nam);
3408 -- A formal abstract procedure cannot have a null default
3409 -- (RM 12.6(4.1/2)).
3411 if Nkind (Spec) = N_Procedure_Specification
3412 and then Null_Present (Spec)
3413 then
3414 Error_Msg_N
3415 ("a formal abstract subprogram cannot default to null", Spec);
3416 end if;
3418 -- A formal abstract function cannot have an expression default
3419 -- (expression defaults are allowed for nonabstract formal functions
3420 -- when extensions are enabled).
3422 if Nkind (Spec) = N_Function_Specification
3423 and then Present (Expr)
3424 then
3425 Error_Msg_N
3426 ("a formal abstract subprogram cannot default to an expression",
3427 Spec);
3428 end if;
3430 declare
3431 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3432 begin
3433 if No (Ctrl_Type) then
3434 Error_Msg_N
3435 ("abstract formal subprogram must have a controlling type",
3438 elsif Ada_Version >= Ada_2012
3439 and then Is_Incomplete_Type (Ctrl_Type)
3440 then
3441 Error_Msg_NE
3442 ("controlling type of abstract formal subprogram cannot "
3443 & "be incomplete type", N, Ctrl_Type);
3445 else
3446 Check_Controlling_Formals (Ctrl_Type, Nam);
3447 end if;
3448 end;
3449 end if;
3451 -- Default name is resolved at the point of instantiation
3453 if Box_Present (N) then
3454 null;
3456 -- Default name is bound at the point of generic declaration
3458 elsif Present (Def) then
3459 if Nkind (Def) = N_Operator_Symbol then
3460 Find_Direct_Name (Def);
3462 elsif Nkind (Def) /= N_Attribute_Reference then
3463 Analyze (Def);
3465 else
3466 -- For an attribute reference, analyze the prefix and verify
3467 -- that it has the proper profile for the subprogram.
3469 Analyze (Prefix (Def));
3470 Valid_Default_Attribute (Nam, Def);
3471 goto Leave;
3472 end if;
3474 -- The default for a ghost generic formal procedure should be a ghost
3475 -- procedure (SPARK RM 6.9(13)).
3477 if Ekind (Nam) = E_Procedure then
3478 declare
3479 Def_E : Entity_Id := Empty;
3480 begin
3481 if Nkind (Def) in N_Has_Entity then
3482 Def_E := Entity (Def);
3483 end if;
3485 Check_Ghost_Formal_Procedure_Or_Package
3486 (N => Def,
3487 Actual => Def_E,
3488 Formal => Nam,
3489 Is_Default => True);
3490 end;
3491 end if;
3493 -- Default name may be overloaded, in which case the interpretation
3494 -- with the correct profile must be selected, as for a renaming.
3495 -- If the definition is an indexed component, it must denote a
3496 -- member of an entry family. If it is a selected component, it
3497 -- can be a protected operation.
3499 if Etype (Def) = Any_Type then
3500 goto Leave;
3502 elsif Nkind (Def) = N_Selected_Component then
3503 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3504 Error_Msg_N ("expect valid subprogram name as default", Def);
3505 end if;
3507 elsif Nkind (Def) = N_Indexed_Component then
3508 if Is_Entity_Name (Prefix (Def)) then
3509 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3510 Error_Msg_N ("expect valid subprogram name as default", Def);
3511 end if;
3513 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3514 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3515 E_Entry_Family
3516 then
3517 Error_Msg_N ("expect valid subprogram name as default", Def);
3518 end if;
3520 else
3521 Error_Msg_N ("expect valid subprogram name as default", Def);
3522 goto Leave;
3523 end if;
3525 elsif Nkind (Def) = N_Character_Literal then
3527 -- Needs some type checks: subprogram should be parameterless???
3529 Resolve (Def, (Etype (Nam)));
3531 elsif not Is_Entity_Name (Def)
3532 or else not Is_Overloadable (Entity (Def))
3533 then
3534 Error_Msg_N ("expect valid subprogram name as default", Def);
3535 goto Leave;
3537 elsif not Is_Overloaded (Def) then
3538 Subp := Entity (Def);
3540 if Subp = Nam then
3541 Error_Msg_N ("premature usage of formal subprogram", Def);
3543 elsif not Entity_Matches_Spec (Subp, Nam) then
3544 Error_Msg_N ("no visible entity matches specification", Def);
3545 end if;
3547 -- More than one interpretation, so disambiguate as for a renaming
3549 else
3550 declare
3551 I : Interp_Index;
3552 I1 : Interp_Index := 0;
3553 It : Interp;
3554 It1 : Interp;
3556 begin
3557 Subp := Any_Id;
3558 Get_First_Interp (Def, I, It);
3559 while Present (It.Nam) loop
3560 if Entity_Matches_Spec (It.Nam, Nam) then
3561 if Subp /= Any_Id then
3562 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3564 if It1 = No_Interp then
3565 Error_Msg_N ("ambiguous default subprogram", Def);
3566 else
3567 Subp := It1.Nam;
3568 end if;
3570 exit;
3572 else
3573 I1 := I;
3574 Subp := It.Nam;
3575 end if;
3576 end if;
3578 Get_Next_Interp (I, It);
3579 end loop;
3580 end;
3582 if Subp /= Any_Id then
3584 -- Subprogram found, generate reference to it
3586 Set_Entity (Def, Subp);
3587 Generate_Reference (Subp, Def);
3589 if Subp = Nam then
3590 Error_Msg_N ("premature usage of formal subprogram", Def);
3592 elsif Ekind (Subp) /= E_Operator then
3593 Check_Mode_Conformant (Subp, Nam);
3594 end if;
3596 else
3597 Error_Msg_N ("no visible subprogram matches specification", N);
3598 end if;
3599 end if;
3601 -- When extensions are enabled, an expression can be given as default
3602 -- for a formal function. The expression must be of the function result
3603 -- type and can reference formal parameters of the function.
3605 elsif Present (Expr) then
3606 Push_Scope (Nam);
3607 Install_Formals (Nam);
3608 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3609 End_Scope;
3610 end if;
3612 <<Leave>>
3613 Analyze_Aspect_Specifications (N, Nam);
3615 if Parent_Installed then
3616 Remove_Parent;
3617 end if;
3618 end Analyze_Formal_Subprogram_Declaration;
3620 -------------------------------------
3621 -- Analyze_Formal_Type_Declaration --
3622 -------------------------------------
3624 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3625 Def : constant Node_Id := Formal_Type_Definition (N);
3627 Parent_Installed : Boolean := False;
3628 T : Entity_Id;
3630 begin
3631 T := Defining_Identifier (N);
3633 if Present (Discriminant_Specifications (N))
3634 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3635 then
3636 Error_Msg_N
3637 ("discriminants not allowed for this formal type", T);
3638 end if;
3640 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3642 -- Enter the new name, and branch to specific routine
3644 case Nkind (Def) is
3645 when N_Formal_Private_Type_Definition =>
3646 Analyze_Formal_Private_Type (N, T, Def);
3648 when N_Formal_Derived_Type_Definition =>
3649 Analyze_Formal_Derived_Type (N, T, Def);
3651 when N_Formal_Incomplete_Type_Definition =>
3652 Analyze_Formal_Incomplete_Type (T, Def);
3654 when N_Formal_Discrete_Type_Definition =>
3655 Analyze_Formal_Discrete_Type (T, Def);
3657 when N_Formal_Signed_Integer_Type_Definition =>
3658 Analyze_Formal_Signed_Integer_Type (T, Def);
3660 when N_Formal_Modular_Type_Definition =>
3661 Analyze_Formal_Modular_Type (T, Def);
3663 when N_Formal_Floating_Point_Definition =>
3664 Analyze_Formal_Floating_Type (T, Def);
3666 when N_Formal_Ordinary_Fixed_Point_Definition =>
3667 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3669 when N_Formal_Decimal_Fixed_Point_Definition =>
3670 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3672 when N_Array_Type_Definition =>
3673 Analyze_Formal_Array_Type (T, Def);
3675 when N_Access_Function_Definition
3676 | N_Access_Procedure_Definition
3677 | N_Access_To_Object_Definition
3679 Analyze_Generic_Access_Type (T, Def);
3681 -- Ada 2005: a interface declaration is encoded as an abstract
3682 -- record declaration or a abstract type derivation.
3684 when N_Record_Definition =>
3685 Analyze_Formal_Interface_Type (N, T, Def);
3687 when N_Derived_Type_Definition =>
3688 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3690 when N_Error =>
3691 null;
3693 when others =>
3694 raise Program_Error;
3695 end case;
3697 -- A formal type declaration declares a type and its first
3698 -- subtype.
3700 Set_Is_Generic_Type (T);
3701 Set_Is_First_Subtype (T);
3703 if Present (Default_Subtype_Mark (Original_Node (N))) then
3704 Validate_Formal_Type_Default (N);
3705 end if;
3707 Analyze_Aspect_Specifications (N, T);
3709 if Parent_Installed then
3710 Remove_Parent;
3711 end if;
3712 end Analyze_Formal_Type_Declaration;
3714 ------------------------------------
3715 -- Analyze_Function_Instantiation --
3716 ------------------------------------
3718 procedure Analyze_Function_Instantiation (N : Node_Id) is
3719 begin
3720 Analyze_Subprogram_Instantiation (N, E_Function);
3721 end Analyze_Function_Instantiation;
3723 ---------------------------------
3724 -- Analyze_Generic_Access_Type --
3725 ---------------------------------
3727 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3728 begin
3729 Enter_Name (T);
3731 if Nkind (Def) = N_Access_To_Object_Definition then
3732 Access_Type_Declaration (T, Def);
3734 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3735 and then No (Full_View (Designated_Type (T)))
3736 and then not Is_Generic_Type (Designated_Type (T))
3737 then
3738 Error_Msg_N ("premature usage of incomplete type", Def);
3740 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3741 Error_Msg_N
3742 ("only a subtype mark is allowed in a formal", Def);
3743 end if;
3745 else
3746 Access_Subprogram_Declaration (T, Def);
3747 end if;
3748 end Analyze_Generic_Access_Type;
3750 ---------------------------------
3751 -- Analyze_Generic_Formal_Part --
3752 ---------------------------------
3754 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3755 Gen_Parm_Decl : Node_Id;
3757 begin
3758 -- The generic formals are processed in the scope of the generic unit,
3759 -- where they are immediately visible. The scope is installed by the
3760 -- caller.
3762 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3763 while Present (Gen_Parm_Decl) loop
3764 Analyze (Gen_Parm_Decl);
3765 Next (Gen_Parm_Decl);
3766 end loop;
3768 Generate_Reference_To_Generic_Formals (Current_Scope);
3770 -- For Ada 2022, some formal parameters can carry aspects, which must
3771 -- be name-resolved at the end of the list of formal parameters (which
3772 -- has the semantics of a declaration list).
3774 Analyze_Contracts (Generic_Formal_Declarations (N));
3775 end Analyze_Generic_Formal_Part;
3777 ------------------------------------------
3778 -- Analyze_Generic_Package_Declaration --
3779 ------------------------------------------
3781 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3782 Decls : constant List_Id := Visible_Declarations (Specification (N));
3783 Loc : constant Source_Ptr := Sloc (N);
3785 Decl : Node_Id;
3786 Id : Entity_Id;
3787 New_N : Node_Id;
3788 Renaming : Node_Id;
3789 Save_Parent : Node_Id;
3791 begin
3792 -- A generic may grant access to its private enclosing context depending
3793 -- on the placement of its corresponding body. From elaboration point of
3794 -- view, the flow of execution may enter this private context, and then
3795 -- reach an external unit, thus producing a dependency on that external
3796 -- unit. For such a path to be properly discovered and encoded in the
3797 -- ALI file of the main unit, let the ABE mechanism process the body of
3798 -- the main unit, and encode all relevant invocation constructs and the
3799 -- relations between them.
3801 Mark_Save_Invocation_Graph_Of_Body;
3803 -- We introduce a renaming of the enclosing package, to have a usable
3804 -- entity as the prefix of an expanded name for a local entity of the
3805 -- form Par.P.Q, where P is the generic package. This is because a local
3806 -- entity named P may hide it, so that the usual visibility rules in
3807 -- the instance will not resolve properly.
3809 Renaming :=
3810 Make_Package_Renaming_Declaration (Loc,
3811 Defining_Unit_Name =>
3812 Make_Defining_Identifier (Loc,
3813 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3814 Name =>
3815 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3817 -- The declaration is inserted before other declarations, but before
3818 -- pragmas that may be library-unit pragmas and must appear before other
3819 -- declarations. The pragma Compile_Time_Error is not in this class, and
3820 -- may contain an expression that includes such a qualified name, so the
3821 -- renaming declaration must appear before it.
3823 -- Are there other pragmas that require this special handling ???
3825 if Present (Decls) then
3826 Decl := First (Decls);
3827 while Present (Decl)
3828 and then Nkind (Decl) = N_Pragma
3829 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3830 loop
3831 Next (Decl);
3832 end loop;
3834 if Present (Decl) then
3835 Insert_Before (Decl, Renaming);
3836 else
3837 Append (Renaming, Visible_Declarations (Specification (N)));
3838 end if;
3840 else
3841 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3842 end if;
3844 -- Create copy of generic unit, and save for instantiation. If the unit
3845 -- is a child unit, do not copy the specifications for the parent, which
3846 -- are not part of the generic tree.
3848 Save_Parent := Parent_Spec (N);
3849 Set_Parent_Spec (N, Empty);
3851 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3852 Set_Parent_Spec (New_N, Save_Parent);
3853 Rewrite (N, New_N);
3855 -- Collect all contract-related source pragmas found within the template
3856 -- and attach them to the contract of the package spec. This contract is
3857 -- used in the capture of global references within annotations.
3859 Create_Generic_Contract (N);
3861 Id := Defining_Entity (N);
3862 Generate_Definition (Id);
3864 -- Expansion is not applied to generic units
3866 Start_Generic;
3868 Enter_Name (Id);
3869 Mutate_Ekind (Id, E_Generic_Package);
3870 Set_Is_Not_Self_Hidden (Id);
3871 Set_Etype (Id, Standard_Void_Type);
3873 -- Set SPARK_Mode from context
3875 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3876 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3877 Set_SPARK_Pragma_Inherited (Id);
3878 Set_SPARK_Aux_Pragma_Inherited (Id);
3880 -- Preserve relevant elaboration-related attributes of the context which
3881 -- are no longer available or very expensive to recompute once analysis,
3882 -- resolution, and expansion are over.
3884 Mark_Elaboration_Attributes
3885 (N_Id => Id,
3886 Checks => True,
3887 Warnings => True);
3889 -- Analyze aspects now, so that generated pragmas appear in the
3890 -- declarations before building and analyzing the generic copy.
3892 Analyze_Aspect_Specifications (N, Id);
3894 Push_Scope (Id);
3895 Enter_Generic_Scope (Id);
3896 Set_Inner_Instances (Id, New_Elmt_List);
3898 Set_Categorization_From_Pragmas (N);
3899 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3901 -- Link the declaration of the generic homonym in the generic copy to
3902 -- the package it renames, so that it is always resolved properly.
3904 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3905 Set_Entity (Associated_Node (Name (Renaming)), Id);
3907 -- For a library unit, we have reconstructed the entity for the unit,
3908 -- and must reset it in the library tables.
3910 if Nkind (Parent (N)) = N_Compilation_Unit then
3911 Set_Cunit_Entity (Current_Sem_Unit, Id);
3912 end if;
3914 Analyze_Generic_Formal_Part (N);
3916 -- After processing the generic formals, analysis proceeds as for a
3917 -- non-generic package.
3919 Analyze (Specification (N));
3921 Validate_Categorization_Dependency (N, Id);
3923 End_Generic;
3925 End_Package_Scope (Id);
3926 Exit_Generic_Scope (Id);
3928 -- If the generic appears within a package unit, the body of that unit
3929 -- has to be present for instantiation and inlining.
3931 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3932 Set_Body_Needed_For_Inlining
3933 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3934 end if;
3936 if Nkind (Parent (N)) /= N_Compilation_Unit then
3937 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3938 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3939 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3941 else
3942 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3943 Validate_RT_RAT_Component (N);
3945 -- If this is a spec without a body, check that generic parameters
3946 -- are referenced.
3948 if not Body_Required (Parent (N)) then
3949 Check_References (Id);
3950 end if;
3951 end if;
3953 -- If there is a specified storage pool in the context, create an
3954 -- aspect on the package declaration, so that it is used in any
3955 -- instance that does not override it.
3957 if Present (Default_Pool) then
3958 declare
3959 ASN : Node_Id;
3961 begin
3962 ASN :=
3963 Make_Aspect_Specification (Loc,
3964 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3965 Expression => New_Copy (Default_Pool));
3967 if No (Aspect_Specifications (Specification (N))) then
3968 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3969 else
3970 Append (ASN, Aspect_Specifications (Specification (N)));
3971 end if;
3972 end;
3973 end if;
3974 end Analyze_Generic_Package_Declaration;
3976 --------------------------------------------
3977 -- Analyze_Generic_Subprogram_Declaration --
3978 --------------------------------------------
3980 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3981 Formals : List_Id;
3982 Id : Entity_Id;
3983 New_N : Node_Id;
3984 Result_Type : Entity_Id;
3985 Save_Parent : Node_Id;
3986 Spec : Node_Id;
3987 Typ : Entity_Id;
3989 begin
3990 -- A generic may grant access to its private enclosing context depending
3991 -- on the placement of its corresponding body. From elaboration point of
3992 -- view, the flow of execution may enter this private context, and then
3993 -- reach an external unit, thus producing a dependency on that external
3994 -- unit. For such a path to be properly discovered and encoded in the
3995 -- ALI file of the main unit, let the ABE mechanism process the body of
3996 -- the main unit, and encode all relevant invocation constructs and the
3997 -- relations between them.
3999 Mark_Save_Invocation_Graph_Of_Body;
4001 -- Create copy of generic unit, and save for instantiation. If the unit
4002 -- is a child unit, do not copy the specifications for the parent, which
4003 -- are not part of the generic tree.
4005 Save_Parent := Parent_Spec (N);
4006 Set_Parent_Spec (N, Empty);
4008 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4009 Set_Parent_Spec (New_N, Save_Parent);
4010 Rewrite (N, New_N);
4012 -- Collect all contract-related source pragmas found within the template
4013 -- and attach them to the contract of the subprogram spec. This contract
4014 -- is used in the capture of global references within annotations.
4016 Create_Generic_Contract (N);
4018 Spec := Specification (N);
4019 Id := Defining_Entity (Spec);
4020 Generate_Definition (Id);
4022 if Nkind (Id) = N_Defining_Operator_Symbol then
4023 Error_Msg_N
4024 ("operator symbol not allowed for generic subprogram", Id);
4025 end if;
4027 Start_Generic;
4029 Enter_Name (Id);
4030 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4032 Push_Scope (Id);
4033 Enter_Generic_Scope (Id);
4034 Set_Inner_Instances (Id, New_Elmt_List);
4035 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4037 Analyze_Generic_Formal_Part (N);
4039 if Nkind (Spec) = N_Function_Specification then
4040 Mutate_Ekind (Id, E_Generic_Function);
4041 else
4042 Mutate_Ekind (Id, E_Generic_Procedure);
4043 end if;
4045 -- Set SPARK_Mode from context
4047 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4048 Set_SPARK_Pragma_Inherited (Id);
4050 -- Preserve relevant elaboration-related attributes of the context which
4051 -- are no longer available or very expensive to recompute once analysis,
4052 -- resolution, and expansion are over.
4054 Mark_Elaboration_Attributes
4055 (N_Id => Id,
4056 Checks => True,
4057 Warnings => True);
4059 Formals := Parameter_Specifications (Spec);
4061 if Present (Formals) then
4062 Process_Formals (Formals, Spec);
4063 end if;
4065 if Nkind (Spec) = N_Function_Specification then
4066 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4067 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4068 Set_Etype (Id, Result_Type);
4070 -- Check restriction imposed by AI05-073: a generic function
4071 -- cannot return an abstract type or an access to such.
4073 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4074 Error_Msg_N
4075 ("generic function cannot have an access result "
4076 & "that designates an abstract type", Spec);
4077 end if;
4079 else
4080 Find_Type (Result_Definition (Spec));
4081 Typ := Entity (Result_Definition (Spec));
4083 if Is_Abstract_Type (Typ)
4084 and then Ada_Version >= Ada_2012
4085 then
4086 Error_Msg_N
4087 ("generic function cannot have abstract result type", Spec);
4088 end if;
4090 -- If a null exclusion is imposed on the result type, then create
4091 -- a null-excluding itype (an access subtype) and use it as the
4092 -- function's Etype.
4094 if Is_Access_Type (Typ)
4095 and then Null_Exclusion_Present (Spec)
4096 then
4097 Set_Etype (Id,
4098 Create_Null_Excluding_Itype
4099 (T => Typ,
4100 Related_Nod => Spec,
4101 Scope_Id => Defining_Unit_Name (Spec)));
4102 else
4103 Set_Etype (Id, Typ);
4104 end if;
4105 end if;
4107 else
4108 Set_Etype (Id, Standard_Void_Type);
4109 end if;
4111 Set_Is_Not_Self_Hidden (Id);
4113 -- Analyze the aspects of the generic copy to ensure that all generated
4114 -- pragmas (if any) perform their semantic effects.
4116 Analyze_Aspect_Specifications (N, Id);
4118 -- For a library unit, we have reconstructed the entity for the unit,
4119 -- and must reset it in the library tables. We also make sure that
4120 -- Body_Required is set properly in the original compilation unit node.
4122 if Nkind (Parent (N)) = N_Compilation_Unit then
4123 Set_Cunit_Entity (Current_Sem_Unit, Id);
4124 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4125 end if;
4127 -- If the generic appears within a package unit, the body of that unit
4128 -- has to be present for instantiation and inlining.
4130 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4131 and then Unit_Requires_Body (Id)
4132 then
4133 Set_Body_Needed_For_Inlining
4134 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4135 end if;
4137 Set_Categorization_From_Pragmas (N);
4138 Validate_Categorization_Dependency (N, Id);
4140 -- Capture all global references that occur within the profile of the
4141 -- generic subprogram. Aspects are not part of this processing because
4142 -- they must be delayed. If processed now, Save_Global_References will
4143 -- destroy the Associated_Node links and prevent the capture of global
4144 -- references when the contract of the generic subprogram is analyzed.
4146 Save_Global_References (Original_Node (N));
4148 End_Generic;
4149 End_Scope;
4150 Exit_Generic_Scope (Id);
4151 Generate_Reference_To_Formals (Id);
4153 List_Inherited_Pre_Post_Aspects (Id);
4154 end Analyze_Generic_Subprogram_Declaration;
4156 -----------------------------------
4157 -- Analyze_Package_Instantiation --
4158 -----------------------------------
4160 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4161 -- must be replaced by gotos which jump to the end of the routine in order
4162 -- to restore the Ghost and SPARK modes.
4164 procedure Analyze_Package_Instantiation (N : Node_Id) is
4165 Has_Inline_Always : Boolean := False;
4166 -- Set if the generic unit contains any subprograms with Inline_Always.
4167 -- Only relevant when back-end inlining is not enabled.
4169 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4170 -- Return True if inlining is active and Gen_Unit contains inlined
4171 -- subprograms. In this case, we may either instantiate the body when
4172 -- front-end inlining is enabled, or add a pending instantiation when
4173 -- back-end inlining is enabled. In the former case, this may cause
4174 -- superfluous instantiations, but in either case we need to perform
4175 -- the instantiation of the body in the context of the instance and
4176 -- not in that of the point of inlining.
4178 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4179 -- Return True if Gen_Unit needs to have its body instantiated in the
4180 -- context of N. This in particular excludes generic contexts.
4182 -----------------------
4183 -- Might_Inline_Subp --
4184 -----------------------
4186 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4187 E : Entity_Id;
4189 begin
4190 if Inline_Processing_Required then
4191 -- No need to recompute the answer if we know it is positive
4192 -- and back-end inlining is enabled.
4194 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4195 return True;
4196 end if;
4198 E := First_Entity (Gen_Unit);
4199 while Present (E) loop
4200 if Is_Subprogram (E) and then Is_Inlined (E) then
4201 -- Remember if there are any subprograms with Inline_Always
4203 if Has_Pragma_Inline_Always (E) then
4204 Has_Inline_Always := True;
4205 end if;
4207 Set_Is_Inlined (Gen_Unit);
4208 return True;
4209 end if;
4211 Next_Entity (E);
4212 end loop;
4213 end if;
4215 return False;
4216 end Might_Inline_Subp;
4218 -------------------------------
4219 -- Needs_Body_Instantiated --
4220 -------------------------------
4222 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4223 begin
4224 -- No need to instantiate bodies in generic units
4226 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4227 return False;
4228 end if;
4230 -- If the instantiation is in the main unit, then the body is needed
4232 if Is_In_Main_Unit (N) then
4233 return True;
4234 end if;
4236 -- In GNATprove mode, never instantiate bodies outside of the main
4237 -- unit, as it does not use frontend/backend inlining in the way that
4238 -- GNAT does, so does not benefit from such instantiations. On the
4239 -- contrary, such instantiations may bring artificial constraints,
4240 -- as for example such bodies may require preprocessing.
4242 if GNATprove_Mode then
4243 return False;
4244 end if;
4246 -- If not, then again no need to instantiate bodies in generic units
4248 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4249 return False;
4250 end if;
4252 -- Here we have a special handling for back-end inlining: if inline
4253 -- processing is required, then we unconditionally want to have the
4254 -- body instantiated. The reason is that Might_Inline_Subp does not
4255 -- catch all the cases (as it does not recurse into nested packages)
4256 -- so this avoids the need to patch things up afterwards. Moreover,
4257 -- these instantiations are only performed on demand when back-end
4258 -- inlining is enabled, so this causes very little extra work.
4260 if Inline_Processing_Required and then Back_End_Inlining then
4261 return True;
4262 end if;
4264 -- We want to have the bodies instantiated in non-main units if
4265 -- they might contribute inlined subprograms.
4267 return Might_Inline_Subp (Gen_Unit);
4268 end Needs_Body_Instantiated;
4270 -- Local declarations
4272 Gen_Id : constant Node_Id := Name (N);
4273 Inst_Id : constant Entity_Id := Defining_Entity (N);
4274 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4275 Loc : constant Source_Ptr := Sloc (N);
4277 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4278 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4279 Saved_ISMP : constant Boolean :=
4280 Ignore_SPARK_Mode_Pragmas_In_Instance;
4281 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4282 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4283 -- Save the Ghost and SPARK mode-related data to restore on exit
4285 Saved_Style_Check : constant Boolean := Style_Check;
4286 -- Save style check mode for restore on exit
4288 Act_Decl : Node_Id;
4289 Act_Decl_Name : Node_Id;
4290 Act_Decl_Id : Entity_Id;
4291 Act_Spec : Node_Id;
4292 Act_Tree : Node_Id;
4293 Env_Installed : Boolean := False;
4294 Gen_Decl : Node_Id;
4295 Gen_Spec : Node_Id;
4296 Gen_Unit : Entity_Id;
4297 Inline_Now : Boolean := False;
4298 Needs_Body : Boolean;
4299 Parent_Installed : Boolean := False;
4300 Renaming_List : List_Id;
4301 Unit_Renaming : Node_Id;
4303 Vis_Prims_List : Elist_Id := No_Elist;
4304 -- List of primitives made temporarily visible in the instantiation
4305 -- to match the visibility of the formal type
4307 -- Start of processing for Analyze_Package_Instantiation
4309 begin
4310 -- Preserve relevant elaboration-related attributes of the context which
4311 -- are no longer available or very expensive to recompute once analysis,
4312 -- resolution, and expansion are over.
4314 Mark_Elaboration_Attributes
4315 (N_Id => N,
4316 Checks => True,
4317 Level => True,
4318 Modes => True,
4319 Warnings => True);
4321 -- Very first thing: check for Text_IO special unit in case we are
4322 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4324 Check_Text_IO_Special_Unit (Name (N));
4326 -- Make node global for error reporting
4328 Instantiation_Node := N;
4330 -- Case of instantiation of a generic package
4332 if Nkind (N) = N_Package_Instantiation then
4333 Act_Decl_Id := New_Copy (Defining_Entity (N));
4335 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4336 Act_Decl_Name :=
4337 Make_Defining_Program_Unit_Name (Loc,
4338 Name =>
4339 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4340 Defining_Identifier => Act_Decl_Id);
4341 else
4342 Act_Decl_Name := Act_Decl_Id;
4343 end if;
4345 -- Case of instantiation of a formal package
4347 else
4348 Act_Decl_Id := Defining_Identifier (N);
4349 Act_Decl_Name := Act_Decl_Id;
4350 end if;
4352 Generate_Definition (Act_Decl_Id);
4353 Mutate_Ekind (Act_Decl_Id, E_Package);
4354 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4356 -- Initialize list of incomplete actuals before analysis
4358 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4360 Preanalyze_Actuals (N, Act_Decl_Id);
4362 -- Turn off style checking in instances. If the check is enabled on the
4363 -- generic unit, a warning in an instance would just be noise. If not
4364 -- enabled on the generic, then a warning in an instance is just wrong.
4365 -- This must be done after analyzing the actuals, which do come from
4366 -- source and are subject to style checking.
4368 Style_Check := False;
4370 Init_Env;
4371 Env_Installed := True;
4373 -- Reset renaming map for formal types. The mapping is established
4374 -- when analyzing the generic associations, but some mappings are
4375 -- inherited from formal packages of parent units, and these are
4376 -- constructed when the parents are installed.
4378 Generic_Renamings.Set_Last (0);
4379 Generic_Renamings_HTable.Reset;
4381 -- Except for an abbreviated instance created to check a formal package,
4382 -- install the parent if this is a generic child unit.
4384 if not Is_Abbreviated_Instance (Inst_Id) then
4385 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4386 end if;
4388 Gen_Unit := Entity (Gen_Id);
4390 -- A package instantiation is Ghost when it is subject to pragma Ghost
4391 -- or the generic template is Ghost. Set the mode now to ensure that
4392 -- any nodes generated during analysis and expansion are marked as
4393 -- Ghost.
4395 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4397 -- Verify that it is the name of a generic package
4399 -- A visibility glitch: if the instance is a child unit and the generic
4400 -- is the generic unit of a parent instance (i.e. both the parent and
4401 -- the child units are instances of the same package) the name now
4402 -- denotes the renaming within the parent, not the intended generic
4403 -- unit. See if there is a homonym that is the desired generic. The
4404 -- renaming declaration must be visible inside the instance of the
4405 -- child, but not when analyzing the name in the instantiation itself.
4407 if Ekind (Gen_Unit) = E_Package
4408 and then Present (Renamed_Entity (Gen_Unit))
4409 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4410 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4411 and then Present (Homonym (Gen_Unit))
4412 then
4413 Gen_Unit := Homonym (Gen_Unit);
4414 end if;
4416 if Etype (Gen_Unit) = Any_Type then
4417 Restore_Env;
4418 goto Leave;
4420 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4422 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4424 if From_Limited_With (Gen_Unit) then
4425 Error_Msg_N
4426 ("cannot instantiate a limited withed package", Gen_Id);
4427 else
4428 Error_Msg_NE
4429 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4430 end if;
4432 Restore_Env;
4433 goto Leave;
4434 end if;
4436 if In_Extended_Main_Source_Unit (N) then
4437 Set_Is_Instantiated (Gen_Unit);
4438 Generate_Reference (Gen_Unit, N);
4440 if Present (Renamed_Entity (Gen_Unit)) then
4441 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4442 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4443 end if;
4444 end if;
4446 if Nkind (Gen_Id) = N_Identifier
4447 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4448 then
4449 Error_Msg_NE
4450 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4452 elsif Nkind (Gen_Id) = N_Expanded_Name
4453 and then Is_Child_Unit (Gen_Unit)
4454 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4455 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4456 then
4457 Error_Msg_N
4458 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4459 end if;
4461 Set_Entity (Gen_Id, Gen_Unit);
4463 -- If generic is a renaming, get original generic unit
4465 if Present (Renamed_Entity (Gen_Unit))
4466 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4467 then
4468 Gen_Unit := Renamed_Entity (Gen_Unit);
4469 end if;
4471 -- Verify that there are no circular instantiations
4473 if In_Open_Scopes (Gen_Unit) then
4474 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4475 Restore_Env;
4476 goto Leave;
4478 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4479 Error_Msg_Node_2 := Current_Scope;
4480 Error_Msg_NE
4481 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4482 Circularity_Detected := True;
4483 Restore_Env;
4484 goto Leave;
4486 else
4487 Mutate_Ekind (Inst_Id, E_Package);
4488 Set_Scope (Inst_Id, Current_Scope);
4490 -- If the context of the instance is subject to SPARK_Mode "off" or
4491 -- the annotation is altogether missing, set the global flag which
4492 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4493 -- the instance.
4495 if SPARK_Mode /= On then
4496 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4498 -- Mark the instance spec in case the body is instantiated at a
4499 -- later pass. This preserves the original context in effect for
4500 -- the body.
4502 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4503 end if;
4505 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4506 Gen_Spec := Specification (Gen_Decl);
4508 -- Initialize renamings map, for error checking, and the list that
4509 -- holds private entities whose views have changed between generic
4510 -- definition and instantiation. If this is the instance created to
4511 -- validate an actual package, the instantiation environment is that
4512 -- of the enclosing instance.
4514 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4516 -- Copy original generic tree, to produce text for instantiation
4518 Act_Tree :=
4519 Copy_Generic_Node
4520 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4522 Act_Spec := Specification (Act_Tree);
4524 -- If this is the instance created to validate an actual package,
4525 -- only the formals matter, do not examine the package spec itself.
4527 if Is_Actual_Pack then
4528 Set_Visible_Declarations (Act_Spec, New_List);
4529 Set_Private_Declarations (Act_Spec, New_List);
4530 end if;
4532 Renaming_List :=
4533 Analyze_Associations
4534 (I_Node => N,
4535 Formals => Generic_Formal_Declarations (Act_Tree),
4536 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4538 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4540 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4541 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4542 Set_Is_Generic_Instance (Act_Decl_Id);
4543 Set_Generic_Parent (Act_Spec, Gen_Unit);
4545 -- References to the generic in its own declaration or its body are
4546 -- references to the instance. Add a renaming declaration for the
4547 -- generic unit itself. This declaration, as well as the renaming
4548 -- declarations for the generic formals, must remain private to the
4549 -- unit: the formals, because this is the language semantics, and
4550 -- the unit because its use is an artifact of the implementation.
4552 Unit_Renaming :=
4553 Make_Package_Renaming_Declaration (Loc,
4554 Defining_Unit_Name =>
4555 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4556 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4558 Append (Unit_Renaming, Renaming_List);
4560 -- The renaming declarations are the first local declarations of the
4561 -- new unit.
4563 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4564 Insert_List_Before
4565 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4566 else
4567 Set_Visible_Declarations (Act_Spec, Renaming_List);
4568 end if;
4570 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4572 -- Propagate the aspect specifications from the package declaration
4573 -- template to the instantiated version of the package declaration.
4575 if Has_Aspects (Act_Tree) then
4576 Set_Aspect_Specifications (Act_Decl,
4577 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4578 end if;
4580 -- The generic may have a generated Default_Storage_Pool aspect,
4581 -- set at the point of generic declaration. If the instance has
4582 -- that aspect, it overrides the one inherited from the generic.
4584 if Has_Aspects (Gen_Spec) then
4585 if No (Aspect_Specifications (N)) then
4586 Set_Aspect_Specifications (N,
4587 (New_Copy_List_Tree
4588 (Aspect_Specifications (Gen_Spec))));
4590 else
4591 declare
4592 Inherited_Aspects : constant List_Id :=
4593 New_Copy_List_Tree
4594 (Aspect_Specifications (Gen_Spec));
4596 ASN1 : Node_Id;
4597 ASN2 : Node_Id;
4598 Pool_Present : Boolean := False;
4600 begin
4601 ASN1 := First (Aspect_Specifications (N));
4602 while Present (ASN1) loop
4603 if Chars (Identifier (ASN1)) =
4604 Name_Default_Storage_Pool
4605 then
4606 Pool_Present := True;
4607 exit;
4608 end if;
4610 Next (ASN1);
4611 end loop;
4613 if Pool_Present then
4615 -- If generic carries a default storage pool, remove it
4616 -- in favor of the instance one.
4618 ASN2 := First (Inherited_Aspects);
4619 while Present (ASN2) loop
4620 if Chars (Identifier (ASN2)) =
4621 Name_Default_Storage_Pool
4622 then
4623 Remove (ASN2);
4624 exit;
4625 end if;
4627 Next (ASN2);
4628 end loop;
4629 end if;
4631 Prepend_List_To
4632 (Aspect_Specifications (N), Inherited_Aspects);
4633 end;
4634 end if;
4635 end if;
4637 -- Save the instantiation node for a subsequent instantiation of the
4638 -- body if there is one and it needs to be instantiated here.
4640 -- We instantiate the body only if we are generating code, or if we
4641 -- are generating cross-reference information, or for GNATprove use.
4643 declare
4644 Enclosing_Body_Present : Boolean := False;
4645 -- If the generic unit is not a compilation unit, then a body may
4646 -- be present in its parent even if none is required. We create a
4647 -- tentative pending instantiation for the body, which will be
4648 -- discarded if none is actually present.
4650 Scop : Entity_Id;
4652 begin
4653 if Scope (Gen_Unit) /= Standard_Standard
4654 and then not Is_Child_Unit (Gen_Unit)
4655 then
4656 Scop := Scope (Gen_Unit);
4657 while Present (Scop) and then Scop /= Standard_Standard loop
4658 if Unit_Requires_Body (Scop) then
4659 Enclosing_Body_Present := True;
4660 exit;
4662 elsif In_Open_Scopes (Scop)
4663 and then In_Package_Body (Scop)
4664 then
4665 Enclosing_Body_Present := True;
4666 exit;
4667 end if;
4669 exit when Is_Compilation_Unit (Scop);
4670 Scop := Scope (Scop);
4671 end loop;
4672 end if;
4674 -- If front-end inlining is enabled or there are any subprograms
4675 -- marked with Inline_Always, and this is a unit for which code
4676 -- will be generated, we instantiate the body at once.
4678 -- This is done if the instance is not the main unit, and if the
4679 -- generic is not a child unit of another generic, to avoid scope
4680 -- problems and the reinstallation of parent instances.
4682 if Expander_Active
4683 and then (not Is_Child_Unit (Gen_Unit)
4684 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4685 and then Might_Inline_Subp (Gen_Unit)
4686 and then not Is_Actual_Pack
4687 then
4688 if not Back_End_Inlining
4689 and then (Front_End_Inlining or else Has_Inline_Always)
4690 and then (Is_In_Main_Unit (N)
4691 or else In_Main_Context (Current_Scope))
4692 and then Nkind (Parent (N)) /= N_Compilation_Unit
4693 then
4694 Inline_Now := True;
4696 -- In configurable_run_time mode we force the inlining of
4697 -- predefined subprograms marked Inline_Always, to minimize
4698 -- the use of the run-time library.
4700 elsif In_Predefined_Unit (Gen_Decl)
4701 and then Configurable_Run_Time_Mode
4702 and then Nkind (Parent (N)) /= N_Compilation_Unit
4703 then
4704 Inline_Now := True;
4705 end if;
4707 -- If the current scope is itself an instance within a child
4708 -- unit, there will be duplications in the scope stack, and the
4709 -- unstacking mechanism in Inline_Instance_Body will fail.
4710 -- This loses some rare cases of optimization.
4712 if Is_Generic_Instance (Current_Scope) then
4713 declare
4714 Curr_Unit : constant Entity_Id :=
4715 Cunit_Entity (Current_Sem_Unit);
4716 begin
4717 if Curr_Unit /= Current_Scope
4718 and then Is_Child_Unit (Curr_Unit)
4719 then
4720 Inline_Now := False;
4721 end if;
4722 end;
4723 end if;
4724 end if;
4726 Needs_Body :=
4727 (Unit_Requires_Body (Gen_Unit)
4728 or else Enclosing_Body_Present
4729 or else Present (Corresponding_Body (Gen_Decl)))
4730 and then Needs_Body_Instantiated (Gen_Unit)
4731 and then not Is_Actual_Pack
4732 and then not Inline_Now
4733 and then (Operating_Mode = Generate_Code
4734 or else (Operating_Mode = Check_Semantics
4735 and then GNATprove_Mode));
4737 -- If front-end inlining is enabled or there are any subprograms
4738 -- marked with Inline_Always, do not instantiate body when within
4739 -- a generic context.
4741 if not Back_End_Inlining
4742 and then (Front_End_Inlining or else Has_Inline_Always)
4743 and then not Expander_Active
4744 then
4745 Needs_Body := False;
4746 end if;
4748 -- If the current context is generic, and the package being
4749 -- instantiated is declared within a formal package, there is no
4750 -- body to instantiate until the enclosing generic is instantiated
4751 -- and there is an actual for the formal package. If the formal
4752 -- package has parameters, we build a regular package instance for
4753 -- it, that precedes the original formal package declaration.
4755 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4756 declare
4757 Decl : constant Node_Id :=
4758 Original_Node
4759 (Unit_Declaration_Node (Scope (Gen_Unit)));
4760 begin
4761 if Nkind (Decl) = N_Formal_Package_Declaration
4762 or else (Nkind (Decl) = N_Package_Declaration
4763 and then Is_List_Member (Decl)
4764 and then Present (Next (Decl))
4765 and then
4766 Nkind (Next (Decl)) =
4767 N_Formal_Package_Declaration)
4768 then
4769 Needs_Body := False;
4770 end if;
4771 end;
4772 end if;
4773 end;
4775 -- For RCI unit calling stubs, we omit the instance body if the
4776 -- instance is the RCI library unit itself.
4778 -- However there is a special case for nested instances: in this case
4779 -- we do generate the instance body, as it might be required, e.g.
4780 -- because it provides stream attributes for some type used in the
4781 -- profile of a remote subprogram. This is consistent with 12.3(12),
4782 -- which indicates that the instance body occurs at the place of the
4783 -- instantiation, and thus is part of the RCI declaration, which is
4784 -- present on all client partitions (this is E.2.3(18)).
4786 -- Note that AI12-0002 may make it illegal at some point to have
4787 -- stream attributes defined in an RCI unit, in which case this
4788 -- special case will become unnecessary. In the meantime, there
4789 -- is known application code in production that depends on this
4790 -- being possible, so we definitely cannot eliminate the body in
4791 -- the case of nested instances for the time being.
4793 -- When we generate a nested instance body, calling stubs for any
4794 -- relevant subprogram will be inserted immediately after the
4795 -- subprogram declarations, and will take precedence over the
4796 -- subsequent (original) body. (The stub and original body will be
4797 -- complete homographs, but this is permitted in an instance).
4798 -- (Could we do better and remove the original body???)
4800 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4801 and then Comes_From_Source (N)
4802 and then Nkind (Parent (N)) = N_Compilation_Unit
4803 then
4804 Needs_Body := False;
4805 end if;
4807 -- If the context requires a full instantiation, set things up for
4808 -- subsequent construction of the body.
4810 if Needs_Body then
4811 declare
4812 Fin_Scop, S : Entity_Id;
4814 begin
4815 Check_Forward_Instantiation (Gen_Decl);
4817 Fin_Scop := Empty;
4819 -- For a package instantiation that is not a compilation unit,
4820 -- indicate that cleanup actions of the innermost enclosing
4821 -- scope for which they are generated should be delayed until
4822 -- after the package body is instantiated.
4824 if Nkind (N) = N_Package_Instantiation
4825 and then not Is_Compilation_Unit (Act_Decl_Id)
4826 then
4827 S := Current_Scope;
4829 while S /= Standard_Standard loop
4830 -- Cleanup actions are not generated within generic units
4831 -- or in the formal part of generic units.
4833 if not Expander_Active then
4834 exit;
4836 -- For package scopes, cleanup actions are generated only
4837 -- for compilation units, for spec and body separately.
4839 elsif Ekind (S) = E_Package then
4840 if Is_Compilation_Unit (S) then
4841 if In_Package_Body (S) then
4842 Fin_Scop := Body_Entity (S);
4843 else
4844 Fin_Scop := S;
4845 end if;
4847 Set_Delay_Cleanups (Fin_Scop);
4848 exit;
4850 else
4851 S := Scope (S);
4852 end if;
4854 -- Cleanup actions are generated for all dynamic scopes
4856 else
4857 Fin_Scop := S;
4858 Set_Delay_Cleanups (Fin_Scop);
4859 exit;
4860 end if;
4861 end loop;
4862 end if;
4864 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
4865 end;
4866 end if;
4868 Set_Categorization_From_Pragmas (Act_Decl);
4870 if Parent_Installed then
4871 Hide_Current_Scope;
4872 end if;
4874 Set_Instance_Spec (N, Act_Decl);
4876 -- If not a compilation unit, insert the package declaration before
4877 -- the original instantiation node.
4879 if Nkind (Parent (N)) /= N_Compilation_Unit then
4880 Mark_Rewrite_Insertion (Act_Decl);
4881 Insert_Before (N, Act_Decl);
4883 if Has_Aspects (N) then
4884 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4886 -- The pragma created for a Default_Storage_Pool aspect must
4887 -- appear ahead of the declarations in the instance spec.
4888 -- Analysis has placed it after the instance node, so remove
4889 -- it and reinsert it properly now.
4891 declare
4892 ASN : constant Node_Id := First (Aspect_Specifications (N));
4893 A_Name : constant Name_Id := Chars (Identifier (ASN));
4894 Decl : Node_Id;
4896 begin
4897 if A_Name = Name_Default_Storage_Pool then
4898 if No (Visible_Declarations (Act_Spec)) then
4899 Set_Visible_Declarations (Act_Spec, New_List);
4900 end if;
4902 Decl := Next (N);
4903 while Present (Decl) loop
4904 if Nkind (Decl) = N_Pragma then
4905 Remove (Decl);
4906 Prepend (Decl, Visible_Declarations (Act_Spec));
4907 exit;
4908 end if;
4910 Next (Decl);
4911 end loop;
4912 end if;
4913 end;
4914 end if;
4916 Analyze (Act_Decl);
4918 -- For an instantiation that is a compilation unit, place
4919 -- declaration on current node so context is complete for analysis
4920 -- (including nested instantiations). If this is the main unit,
4921 -- the declaration eventually replaces the instantiation node.
4922 -- If the instance body is created later, it replaces the
4923 -- instance node, and the declaration is attached to it
4924 -- (see Build_Instance_Compilation_Unit_Nodes).
4926 else
4927 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4929 -- The entity for the current unit is the newly created one,
4930 -- and all semantic information is attached to it.
4932 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4934 -- If this is the main unit, replace the main entity as well
4936 if Current_Sem_Unit = Main_Unit then
4937 Main_Unit_Entity := Act_Decl_Id;
4938 end if;
4939 end if;
4941 Set_Unit (Parent (N), Act_Decl);
4942 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4943 Set_Package_Instantiation (Act_Decl_Id, N);
4945 -- Process aspect specifications of the instance node, if any, to
4946 -- take into account categorization pragmas before analyzing the
4947 -- instance.
4949 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4951 Analyze (Act_Decl);
4952 Set_Unit (Parent (N), N);
4953 Set_Body_Required (Parent (N), False);
4955 -- We never need elaboration checks on instantiations, since by
4956 -- definition, the body instantiation is elaborated at the same
4957 -- time as the spec instantiation.
4959 if Legacy_Elaboration_Checks then
4960 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4961 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4962 end if;
4963 end if;
4965 if Legacy_Elaboration_Checks then
4966 Check_Elab_Instantiation (N);
4967 end if;
4969 -- Save the scenario for later examination by the ABE Processing
4970 -- phase.
4972 Record_Elaboration_Scenario (N);
4974 -- The instantiation results in a guaranteed ABE
4976 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4977 -- Do not instantiate the corresponding body because gigi cannot
4978 -- handle certain types of premature instantiations.
4980 Remove_Dead_Instance (N);
4982 -- Create completing bodies for all subprogram declarations since
4983 -- their real bodies will not be instantiated.
4985 Provide_Completing_Bodies (Instance_Spec (N));
4986 end if;
4988 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4990 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4991 First_Private_Entity (Act_Decl_Id));
4993 -- If the instantiation needs a body, the unit will be turned into
4994 -- a package body and receive its own elaboration entity. Otherwise,
4995 -- the nature of the unit is now a package declaration.
4997 -- Note that the below rewriting means that Act_Decl, which has been
4998 -- analyzed and expanded, will be re-expanded as the rewritten N.
5000 if Nkind (Parent (N)) = N_Compilation_Unit
5001 and then not Needs_Body
5002 then
5003 Rewrite (N, Act_Decl);
5004 end if;
5006 if Present (Corresponding_Body (Gen_Decl))
5007 or else Unit_Requires_Body (Gen_Unit)
5008 then
5009 Set_Has_Completion (Act_Decl_Id);
5010 end if;
5012 Check_Formal_Packages (Act_Decl_Id);
5014 Restore_Hidden_Primitives (Vis_Prims_List);
5015 Restore_Private_Views (Act_Decl_Id);
5017 Inherit_Context (Gen_Decl, N);
5019 if Parent_Installed then
5020 Remove_Parent;
5021 end if;
5023 Restore_Env;
5024 Env_Installed := False;
5025 end if;
5027 Validate_Categorization_Dependency (N, Act_Decl_Id);
5029 -- There used to be a check here to prevent instantiations in local
5030 -- contexts if the No_Local_Allocators restriction was active. This
5031 -- check was removed by a binding interpretation in AI-95-00130/07,
5032 -- but we retain the code for documentation purposes.
5034 -- if Ekind (Act_Decl_Id) /= E_Void
5035 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5036 -- then
5037 -- Check_Restriction (No_Local_Allocators, N);
5038 -- end if;
5040 if Inline_Now then
5041 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5042 end if;
5044 -- Check that if N is an instantiation of System.Dim_Float_IO or
5045 -- System.Dim_Integer_IO, the formal type has a dimension system.
5047 if Nkind (N) = N_Package_Instantiation
5048 and then Is_Dim_IO_Package_Instantiation (N)
5049 then
5050 declare
5051 Assoc : constant Node_Id := First (Generic_Associations (N));
5052 begin
5053 if not Has_Dimension_System
5054 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5055 then
5056 Error_Msg_N ("type with a dimension system expected", Assoc);
5057 end if;
5058 end;
5059 end if;
5061 <<Leave>>
5062 if Nkind (Parent (N)) /= N_Compilation_Unit then
5063 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5064 end if;
5066 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5067 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5068 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5069 Style_Check := Saved_Style_Check;
5071 exception
5072 when Instantiation_Error =>
5073 if Parent_Installed then
5074 Remove_Parent;
5075 end if;
5077 if Env_Installed then
5078 Restore_Env;
5079 end if;
5081 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5082 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5083 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5084 Style_Check := Saved_Style_Check;
5085 end Analyze_Package_Instantiation;
5087 --------------------------
5088 -- Inline_Instance_Body --
5089 --------------------------
5091 -- WARNING: This routine manages SPARK regions. Return statements must be
5092 -- replaced by gotos which jump to the end of the routine and restore the
5093 -- SPARK mode.
5095 procedure Inline_Instance_Body
5096 (N : Node_Id;
5097 Gen_Unit : Entity_Id;
5098 Act_Decl : Node_Id)
5100 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5102 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5103 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5104 Gen_Comp : constant Entity_Id :=
5105 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5107 Scope_Stack_Depth : constant Pos :=
5108 Scope_Stack.Last - Scope_Stack.First + 1;
5110 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5111 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5112 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5114 Curr_Scope : Entity_Id := Empty;
5115 List : Elist_Id := No_Elist; -- init to avoid warning
5116 N_Instances : Nat := 0;
5117 Num_Inner : Nat := 0;
5118 Num_Scopes : Nat := 0;
5119 Removed : Boolean := False;
5120 S : Entity_Id;
5121 Vis : Boolean;
5123 begin
5124 -- Case of generic unit defined in another unit. We must remove the
5125 -- complete context of the current unit to install that of the generic.
5127 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5129 -- Loop through enclosing scopes until we reach a generic instance,
5130 -- package body, or subprogram.
5132 S := Current_Scope;
5133 while Present (S) and then S /= Standard_Standard loop
5135 -- Save use clauses from enclosing scopes into Use_Clauses
5137 loop
5138 Num_Scopes := Num_Scopes + 1;
5140 Use_Clauses (Num_Scopes) :=
5141 (Scope_Stack.Table
5142 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5143 End_Use_Clauses (Use_Clauses (Num_Scopes));
5145 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5146 or else Scope_Stack.Table
5147 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5148 end loop;
5150 exit when Is_Generic_Instance (S)
5151 and then (In_Package_Body (S)
5152 or else Ekind (S) = E_Procedure
5153 or else Ekind (S) = E_Function);
5154 S := Scope (S);
5155 end loop;
5157 Vis := Is_Immediately_Visible (Gen_Comp);
5159 -- Find and save all enclosing instances
5161 S := Current_Scope;
5163 while Present (S)
5164 and then S /= Standard_Standard
5165 loop
5166 if Is_Generic_Instance (S) then
5167 N_Instances := N_Instances + 1;
5168 Instances (N_Instances) := S;
5170 exit when In_Package_Body (S);
5171 end if;
5173 S := Scope (S);
5174 end loop;
5176 -- Remove context of current compilation unit, unless we are within a
5177 -- nested package instantiation, in which case the context has been
5178 -- removed previously.
5180 -- If current scope is the body of a child unit, remove context of
5181 -- spec as well. If an enclosing scope is an instance body, the
5182 -- context has already been removed, but the entities in the body
5183 -- must be made invisible as well.
5185 S := Current_Scope;
5186 while Present (S) and then S /= Standard_Standard loop
5187 if Is_Generic_Instance (S)
5188 and then (In_Package_Body (S)
5189 or else Ekind (S) in E_Procedure | E_Function)
5190 then
5191 -- We still have to remove the entities of the enclosing
5192 -- instance from direct visibility.
5194 declare
5195 E : Entity_Id;
5196 begin
5197 E := First_Entity (S);
5198 while Present (E) loop
5199 Set_Is_Immediately_Visible (E, False);
5200 Next_Entity (E);
5201 end loop;
5202 end;
5204 exit;
5205 end if;
5207 if S = Curr_Unit
5208 or else (Ekind (Curr_Unit) = E_Package_Body
5209 and then S = Spec_Entity (Curr_Unit))
5210 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5211 and then S = Corresponding_Spec
5212 (Unit_Declaration_Node (Curr_Unit)))
5213 then
5214 Removed := True;
5216 -- Remove entities in current scopes from visibility, so that
5217 -- instance body is compiled in a clean environment.
5219 List := Save_Scope_Stack (Handle_Use => False);
5221 if Is_Child_Unit (S) then
5223 -- Remove child unit from stack, as well as inner scopes.
5224 -- Removing the context of a child unit removes parent units
5225 -- as well.
5227 while Current_Scope /= S loop
5228 Num_Inner := Num_Inner + 1;
5229 Inner_Scopes (Num_Inner) := Current_Scope;
5230 Pop_Scope;
5231 end loop;
5233 Pop_Scope;
5234 Remove_Context (Curr_Comp);
5235 Curr_Scope := S;
5237 else
5238 Remove_Context (Curr_Comp);
5239 end if;
5241 if Ekind (Curr_Unit) = E_Package_Body then
5242 Remove_Context (Library_Unit (Curr_Comp));
5243 end if;
5244 end if;
5246 S := Scope (S);
5247 end loop;
5249 pragma Assert (Num_Inner < Num_Scopes);
5251 Push_Scope (Standard_Standard);
5252 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5254 -- The inlined package body is analyzed with the configuration state
5255 -- of the context prior to the scope manipulations performed above.
5257 -- ??? shouldn't this also use the warning state of the context prior
5258 -- to the scope manipulations?
5260 Instantiate_Package_Body
5261 (Body_Info =>
5262 ((Inst_Node => N,
5263 Act_Decl => Act_Decl,
5264 Fin_Scop => Empty,
5265 Config_Switches => Config_Attrs,
5266 Current_Sem_Unit => Current_Sem_Unit,
5267 Expander_Status => Expander_Active,
5268 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5269 Scope_Suppress => Scope_Suppress,
5270 Warnings => Save_Warnings)),
5271 Inlined_Body => True);
5273 Pop_Scope;
5275 -- Restore context
5277 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5279 -- Reset Generic_Instance flag so that use clauses can be installed
5280 -- in the proper order. (See Use_One_Package for effect of enclosing
5281 -- instances on processing of use clauses).
5283 for J in 1 .. N_Instances loop
5284 Set_Is_Generic_Instance (Instances (J), False);
5285 end loop;
5287 if Removed then
5288 Install_Context (Curr_Comp, Chain => False);
5290 if Present (Curr_Scope)
5291 and then Is_Child_Unit (Curr_Scope)
5292 then
5293 Push_Scope (Curr_Scope);
5294 Set_Is_Immediately_Visible (Curr_Scope);
5296 -- Finally, restore inner scopes as well
5298 for J in reverse 1 .. Num_Inner loop
5299 Push_Scope (Inner_Scopes (J));
5300 end loop;
5301 end if;
5303 Restore_Scope_Stack (List, Handle_Use => False);
5305 if Present (Curr_Scope)
5306 and then
5307 (In_Private_Part (Curr_Scope)
5308 or else In_Package_Body (Curr_Scope))
5309 then
5310 -- Install private declaration of ancestor units, which are
5311 -- currently available. Restore_Scope_Stack and Install_Context
5312 -- only install the visible part of parents.
5314 declare
5315 Par : Entity_Id;
5316 begin
5317 Par := Scope (Curr_Scope);
5318 while Present (Par) and then Par /= Standard_Standard loop
5319 Install_Private_Declarations (Par);
5320 Par := Scope (Par);
5321 end loop;
5322 end;
5323 end if;
5324 end if;
5326 -- Restore use clauses. For a child unit, use clauses in the parents
5327 -- are restored when installing the context, so only those in inner
5328 -- scopes (and those local to the child unit itself) need to be
5329 -- installed explicitly.
5331 if Is_Child_Unit (Curr_Unit) and then Removed then
5332 for J in reverse 1 .. Num_Inner + 1 loop
5333 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5334 Use_Clauses (J);
5335 Install_Use_Clauses (Use_Clauses (J));
5336 end loop;
5338 else
5339 for J in reverse 1 .. Num_Scopes loop
5340 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5341 Use_Clauses (J);
5342 Install_Use_Clauses (Use_Clauses (J));
5343 end loop;
5344 end if;
5346 -- Restore status of instances. If one of them is a body, make its
5347 -- local entities visible again.
5349 declare
5350 E : Entity_Id;
5351 Inst : Entity_Id;
5353 begin
5354 for J in 1 .. N_Instances loop
5355 Inst := Instances (J);
5356 Set_Is_Generic_Instance (Inst, True);
5358 if In_Package_Body (Inst)
5359 or else Ekind (S) in E_Procedure | E_Function
5360 then
5361 E := First_Entity (Instances (J));
5362 while Present (E) loop
5363 Set_Is_Immediately_Visible (E);
5364 Next_Entity (E);
5365 end loop;
5366 end if;
5367 end loop;
5368 end;
5370 -- If generic unit is in current unit, current context is correct. Note
5371 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5372 -- enclosing scopes were removed.
5374 else
5375 Instantiate_Package_Body
5376 (Body_Info =>
5377 ((Inst_Node => N,
5378 Act_Decl => Act_Decl,
5379 Fin_Scop => Empty,
5380 Config_Switches => Save_Config_Switches,
5381 Current_Sem_Unit => Current_Sem_Unit,
5382 Expander_Status => Expander_Active,
5383 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5384 Scope_Suppress => Scope_Suppress,
5385 Warnings => Save_Warnings)),
5386 Inlined_Body => True);
5387 end if;
5388 end Inline_Instance_Body;
5390 -------------------------------------
5391 -- Analyze_Procedure_Instantiation --
5392 -------------------------------------
5394 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5395 begin
5396 Analyze_Subprogram_Instantiation (N, E_Procedure);
5397 end Analyze_Procedure_Instantiation;
5399 -----------------------------------
5400 -- Need_Subprogram_Instance_Body --
5401 -----------------------------------
5403 function Need_Subprogram_Instance_Body
5404 (N : Node_Id;
5405 Subp : Entity_Id) return Boolean
5407 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5408 -- Return True if E is an inlined subprogram, an inlined renaming or a
5409 -- subprogram nested in an inlined subprogram. The inlining machinery
5410 -- totally disregards nested subprograms since it considers that they
5411 -- will always be compiled if the parent is (see Inline.Is_Nested).
5413 ------------------------------------
5414 -- Is_Inlined_Or_Child_Of_Inlined --
5415 ------------------------------------
5417 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5418 Scop : Entity_Id;
5420 begin
5421 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5422 return True;
5423 end if;
5425 Scop := Scope (E);
5426 while Scop /= Standard_Standard loop
5427 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5428 return True;
5429 end if;
5431 Scop := Scope (Scop);
5432 end loop;
5434 return False;
5435 end Is_Inlined_Or_Child_Of_Inlined;
5437 begin
5438 -- Must be in the main unit or inlined (or child of inlined)
5440 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5442 -- Must be generating code or analyzing code in GNATprove mode
5444 and then (Operating_Mode = Generate_Code
5445 or else (Operating_Mode = Check_Semantics
5446 and then GNATprove_Mode))
5448 -- The body is needed when generating code (full expansion) and in
5449 -- in GNATprove mode (special expansion) for formal verification of
5450 -- the body itself.
5452 and then (Expander_Active or GNATprove_Mode)
5454 -- No point in inlining if ABE is inevitable
5456 and then not Is_Known_Guaranteed_ABE (N)
5458 -- Or if subprogram is eliminated
5460 and then not Is_Eliminated (Subp)
5461 then
5462 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5463 return True;
5465 -- Here if not inlined, or we ignore the inlining
5467 else
5468 return False;
5469 end if;
5470 end Need_Subprogram_Instance_Body;
5472 --------------------------------------
5473 -- Analyze_Subprogram_Instantiation --
5474 --------------------------------------
5476 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5477 -- must be replaced by gotos which jump to the end of the routine in order
5478 -- to restore the Ghost and SPARK modes.
5480 procedure Analyze_Subprogram_Instantiation
5481 (N : Node_Id;
5482 K : Entity_Kind)
5484 Errs : constant Nat := Serious_Errors_Detected;
5485 Gen_Id : constant Node_Id := Name (N);
5486 Inst_Id : constant Entity_Id := Defining_Entity (N);
5487 Anon_Id : constant Entity_Id :=
5488 Make_Defining_Identifier (Sloc (Inst_Id),
5489 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5490 Loc : constant Source_Ptr := Sloc (N);
5492 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5493 Act_Decl : Node_Id;
5494 Act_Spec : Node_Id;
5495 Act_Tree : Node_Id;
5497 Env_Installed : Boolean := False;
5498 Gen_Unit : Entity_Id;
5499 Gen_Decl : Node_Id;
5500 Pack_Id : Entity_Id;
5501 Parent_Installed : Boolean := False;
5503 Renaming_List : List_Id;
5504 -- The list of declarations that link formals and actuals of the
5505 -- instance. These are subtype declarations for formal types, and
5506 -- renaming declarations for other formals. The subprogram declaration
5507 -- for the instance is then appended to the list, and the last item on
5508 -- the list is the renaming declaration for the instance.
5510 procedure Analyze_Instance_And_Renamings;
5511 -- The instance must be analyzed in a context that includes the mappings
5512 -- of generic parameters into actuals. We create a package declaration
5513 -- for this purpose, and a subprogram with an internal name within the
5514 -- package. The subprogram instance is simply an alias for the internal
5515 -- subprogram, declared in the current scope.
5517 procedure Build_Subprogram_Renaming;
5518 -- If the subprogram is recursive, there are occurrences of the name of
5519 -- the generic within the body, which must resolve to the current
5520 -- instance. We add a renaming declaration after the declaration, which
5521 -- is available in the instance body, as well as in the analysis of
5522 -- aspects that appear in the generic. This renaming declaration is
5523 -- inserted after the instance declaration which it renames.
5525 ------------------------------------
5526 -- Analyze_Instance_And_Renamings --
5527 ------------------------------------
5529 procedure Analyze_Instance_And_Renamings is
5530 Def_Ent : constant Entity_Id := Defining_Entity (N);
5531 Pack_Decl : Node_Id;
5533 begin
5534 if Nkind (Parent (N)) = N_Compilation_Unit then
5536 -- For the case of a compilation unit, the container package has
5537 -- the same name as the instantiation, to insure that the binder
5538 -- calls the elaboration procedure with the right name. Copy the
5539 -- entity of the instance, which may have compilation level flags
5540 -- (e.g. Is_Child_Unit) set.
5542 Pack_Id := New_Copy (Def_Ent);
5544 else
5545 -- Otherwise we use the name of the instantiation concatenated
5546 -- with its source position to ensure uniqueness if there are
5547 -- several instantiations with the same name.
5549 Pack_Id :=
5550 Make_Defining_Identifier (Loc,
5551 Chars => New_External_Name
5552 (Related_Id => Chars (Def_Ent),
5553 Suffix => "GP",
5554 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5555 end if;
5557 Pack_Decl :=
5558 Make_Package_Declaration (Loc,
5559 Specification => Make_Package_Specification (Loc,
5560 Defining_Unit_Name => Pack_Id,
5561 Visible_Declarations => Renaming_List,
5562 End_Label => Empty));
5564 Set_Instance_Spec (N, Pack_Decl);
5565 Set_Is_Generic_Instance (Pack_Id);
5566 Set_Debug_Info_Needed (Pack_Id);
5568 -- Case of not a compilation unit
5570 if Nkind (Parent (N)) /= N_Compilation_Unit then
5571 Mark_Rewrite_Insertion (Pack_Decl);
5572 Insert_Before (N, Pack_Decl);
5573 Set_Has_Completion (Pack_Id);
5575 -- Case of an instantiation that is a compilation unit
5577 -- Place declaration on current node so context is complete for
5578 -- analysis (including nested instantiations), and for use in a
5579 -- context_clause (see Analyze_With_Clause).
5581 else
5582 Set_Unit (Parent (N), Pack_Decl);
5583 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5584 end if;
5586 Analyze (Pack_Decl);
5587 Check_Formal_Packages (Pack_Id);
5589 -- Body of the enclosing package is supplied when instantiating the
5590 -- subprogram body, after semantic analysis is completed.
5592 if Nkind (Parent (N)) = N_Compilation_Unit then
5594 -- Remove package itself from visibility, so it does not
5595 -- conflict with subprogram.
5597 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5599 -- Set name and scope of internal subprogram so that the proper
5600 -- external name will be generated. The proper scope is the scope
5601 -- of the wrapper package. We need to generate debugging info for
5602 -- the internal subprogram, so set flag accordingly.
5604 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5605 Set_Scope (Anon_Id, Scope (Pack_Id));
5607 -- Mark wrapper package as referenced, to avoid spurious warnings
5608 -- if the instantiation appears in various with_ clauses of
5609 -- subunits of the main unit.
5611 Set_Referenced (Pack_Id);
5612 end if;
5614 Set_Is_Generic_Instance (Anon_Id);
5615 Set_Debug_Info_Needed (Anon_Id);
5616 Act_Decl_Id := New_Copy (Anon_Id);
5618 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5619 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5620 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5622 -- Subprogram instance comes from source only if generic does
5624 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5626 -- If the instance is a child unit, mark the Id accordingly. Mark
5627 -- the anonymous entity as well, which is the real subprogram and
5628 -- which is used when the instance appears in a context clause.
5629 -- Similarly, propagate the Is_Eliminated flag to handle properly
5630 -- nested eliminated subprograms.
5632 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5633 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5634 New_Overloaded_Entity (Act_Decl_Id);
5635 Check_Eliminated (Act_Decl_Id);
5636 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5638 if Nkind (Parent (N)) = N_Compilation_Unit then
5640 -- In compilation unit case, kill elaboration checks on the
5641 -- instantiation, since they are never needed - the body is
5642 -- instantiated at the same point as the spec.
5644 if Legacy_Elaboration_Checks then
5645 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5646 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5647 end if;
5649 Set_Is_Compilation_Unit (Anon_Id);
5650 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5651 end if;
5653 -- The instance is not a freezing point for the new subprogram.
5654 -- The anonymous subprogram may have a freeze node, created for
5655 -- some delayed aspects. This freeze node must not be inherited
5656 -- by the visible subprogram entity.
5658 Set_Is_Frozen (Act_Decl_Id, False);
5659 Set_Freeze_Node (Act_Decl_Id, Empty);
5661 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5662 Valid_Operator_Definition (Act_Decl_Id);
5663 end if;
5665 Set_Alias (Act_Decl_Id, Anon_Id);
5666 Set_Has_Completion (Act_Decl_Id);
5667 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5669 if Nkind (Parent (N)) = N_Compilation_Unit then
5670 Set_Body_Required (Parent (N), False);
5671 end if;
5672 end Analyze_Instance_And_Renamings;
5674 -------------------------------
5675 -- Build_Subprogram_Renaming --
5676 -------------------------------
5678 procedure Build_Subprogram_Renaming is
5679 Renaming_Decl : Node_Id;
5680 Unit_Renaming : Node_Id;
5682 begin
5683 Unit_Renaming :=
5684 Make_Subprogram_Renaming_Declaration (Loc,
5685 Specification =>
5686 Copy_Generic_Node
5687 (Specification (Original_Node (Gen_Decl)),
5688 Empty,
5689 Instantiating => True),
5690 Name => New_Occurrence_Of (Anon_Id, Loc));
5692 -- The generic may be a child unit. The renaming needs an identifier
5693 -- with the proper name.
5695 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5696 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5698 -- If there is a formal subprogram with the same name as the unit
5699 -- itself, do not add this renaming declaration, to prevent
5700 -- ambiguities when there is a call with that name in the body.
5702 Renaming_Decl := First (Renaming_List);
5703 while Present (Renaming_Decl) loop
5704 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5705 and then
5706 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5707 then
5708 exit;
5709 end if;
5711 Next (Renaming_Decl);
5712 end loop;
5714 if No (Renaming_Decl) then
5715 Append (Unit_Renaming, Renaming_List);
5716 end if;
5717 end Build_Subprogram_Renaming;
5719 -- Local variables
5721 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5722 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5723 Saved_ISMP : constant Boolean :=
5724 Ignore_SPARK_Mode_Pragmas_In_Instance;
5725 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5726 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5727 -- Save the Ghost and SPARK mode-related data to restore on exit
5729 Vis_Prims_List : Elist_Id := No_Elist;
5730 -- List of primitives made temporarily visible in the instantiation
5731 -- to match the visibility of the formal type
5733 -- Start of processing for Analyze_Subprogram_Instantiation
5735 begin
5736 -- Preserve relevant elaboration-related attributes of the context which
5737 -- are no longer available or very expensive to recompute once analysis,
5738 -- resolution, and expansion are over.
5740 Mark_Elaboration_Attributes
5741 (N_Id => N,
5742 Checks => True,
5743 Level => True,
5744 Modes => True,
5745 Warnings => True);
5747 -- Very first thing: check for special Text_IO unit in case we are
5748 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5749 -- such an instantiation is bogus (these are packages, not subprograms),
5750 -- but we get a better error message if we do this.
5752 Check_Text_IO_Special_Unit (Gen_Id);
5754 -- Make node global for error reporting
5756 Instantiation_Node := N;
5758 -- For package instantiations we turn off style checks, because they
5759 -- will have been emitted in the generic. For subprogram instantiations
5760 -- we want to apply at least the check on overriding indicators so we
5761 -- do not modify the style check status.
5763 -- The renaming declarations for the actuals do not come from source and
5764 -- will not generate spurious warnings.
5766 Preanalyze_Actuals (N);
5768 Init_Env;
5769 Env_Installed := True;
5770 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5771 Gen_Unit := Entity (Gen_Id);
5773 -- A subprogram instantiation is Ghost when it is subject to pragma
5774 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5775 -- that any nodes generated during analysis and expansion are marked as
5776 -- Ghost.
5778 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5780 Generate_Reference (Gen_Unit, Gen_Id);
5782 if Nkind (Gen_Id) = N_Identifier
5783 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5784 then
5785 Error_Msg_NE
5786 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5787 end if;
5789 if Etype (Gen_Unit) = Any_Type then
5790 Restore_Env;
5791 goto Leave;
5792 end if;
5794 -- Verify that it is a generic subprogram of the right kind, and that
5795 -- it does not lead to a circular instantiation.
5797 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5798 Error_Msg_NE
5799 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5801 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5802 Error_Msg_NE
5803 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5805 elsif In_Open_Scopes (Gen_Unit) then
5806 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5808 else
5809 Mutate_Ekind (Inst_Id, K);
5810 Set_Scope (Inst_Id, Current_Scope);
5812 Set_Entity (Gen_Id, Gen_Unit);
5814 if In_Extended_Main_Source_Unit (N) then
5815 Set_Is_Instantiated (Gen_Unit);
5816 Generate_Reference (Gen_Unit, N);
5817 end if;
5819 -- If renaming, get original unit
5821 if Present (Renamed_Entity (Gen_Unit))
5822 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5823 then
5824 Gen_Unit := Renamed_Entity (Gen_Unit);
5825 Set_Is_Instantiated (Gen_Unit);
5826 Generate_Reference (Gen_Unit, N);
5827 end if;
5829 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5830 Error_Msg_Node_2 := Current_Scope;
5831 Error_Msg_NE
5832 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5833 Circularity_Detected := True;
5834 Restore_Hidden_Primitives (Vis_Prims_List);
5835 goto Leave;
5836 end if;
5838 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5840 -- Initialize renamings map, for error checking
5842 Generic_Renamings.Set_Last (0);
5843 Generic_Renamings_HTable.Reset;
5845 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5847 -- Copy original generic tree, to produce text for instantiation
5849 Act_Tree :=
5850 Copy_Generic_Node
5851 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5853 -- Inherit overriding indicator from instance node
5855 Act_Spec := Specification (Act_Tree);
5856 Set_Must_Override (Act_Spec, Must_Override (N));
5857 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5859 Renaming_List :=
5860 Analyze_Associations
5861 (I_Node => N,
5862 Formals => Generic_Formal_Declarations (Act_Tree),
5863 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5865 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5867 -- The subprogram itself cannot contain a nested instance, so the
5868 -- current parent is left empty.
5870 Set_Instance_Env (Gen_Unit, Empty);
5872 -- Build the subprogram declaration, which does not appear in the
5873 -- generic template, and give it a sloc consistent with that of the
5874 -- template.
5876 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5877 Set_Generic_Parent (Act_Spec, Gen_Unit);
5878 Act_Decl :=
5879 Make_Subprogram_Declaration (Sloc (Act_Spec),
5880 Specification => Act_Spec);
5882 -- The aspects have been copied previously, but they have to be
5883 -- linked explicitly to the new subprogram declaration. Explicit
5884 -- pre/postconditions on the instance are analyzed below, in a
5885 -- separate step.
5887 Move_Aspects (Act_Tree, To => Act_Decl);
5888 Set_Categorization_From_Pragmas (Act_Decl);
5890 if Parent_Installed then
5891 Hide_Current_Scope;
5892 end if;
5894 Append (Act_Decl, Renaming_List);
5896 -- Contract-related source pragmas that follow a generic subprogram
5897 -- must be instantiated explicitly because they are not part of the
5898 -- subprogram template.
5900 Instantiate_Subprogram_Contract
5901 (Original_Node (Gen_Decl), Renaming_List);
5903 Build_Subprogram_Renaming;
5905 -- If the context of the instance is subject to SPARK_Mode "off" or
5906 -- the annotation is altogether missing, set the global flag which
5907 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5908 -- the instance. This should be done prior to analyzing the instance.
5910 if SPARK_Mode /= On then
5911 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5912 end if;
5914 -- If the context of an instance is not subject to SPARK_Mode "off",
5915 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5916 -- the latter should be the one applicable to the instance.
5918 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5919 and then Saved_SM /= Off
5920 and then Present (SPARK_Pragma (Gen_Unit))
5921 then
5922 Set_SPARK_Mode (Gen_Unit);
5923 end if;
5925 -- Need to mark Anon_Id intrinsic before calling
5926 -- Analyze_Instance_And_Renamings because this flag may be propagated
5927 -- to other nodes.
5929 if Is_Intrinsic_Subprogram (Gen_Unit) then
5930 Set_Is_Intrinsic_Subprogram (Anon_Id);
5931 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5932 end if;
5934 Analyze_Instance_And_Renamings;
5936 -- Restore SPARK_Mode from the context after analysis of the package
5937 -- declaration, so that the SPARK_Mode on the generic spec does not
5938 -- apply to the pending instance for the instance body.
5940 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5941 and then Saved_SM /= Off
5942 and then Present (SPARK_Pragma (Gen_Unit))
5943 then
5944 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5945 end if;
5947 -- If the generic is marked Import (Intrinsic), then so is the
5948 -- instance; this indicates that there is no body to instantiate.
5949 -- We also copy the interface name in case this is handled by the
5950 -- back-end and deal with an instance of unchecked conversion.
5952 if Is_Intrinsic_Subprogram (Gen_Unit) then
5953 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5954 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5956 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5957 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5958 end if;
5959 end if;
5961 -- Inherit convention from generic unit. Intrinsic convention, as for
5962 -- an instance of unchecked conversion, is not inherited because an
5963 -- explicit Ada instance has been created.
5965 if Has_Convention_Pragma (Gen_Unit)
5966 and then Convention (Gen_Unit) /= Convention_Intrinsic
5967 then
5968 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5969 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5970 end if;
5972 Generate_Definition (Act_Decl_Id);
5974 -- Inherit all inlining-related flags which apply to the generic in
5975 -- the subprogram and its declaration.
5977 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5978 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5980 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5981 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5983 Set_Has_Pragma_Inline_Always
5984 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5985 Set_Has_Pragma_Inline_Always
5986 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5988 Set_Has_Pragma_No_Inline
5989 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5990 Set_Has_Pragma_No_Inline
5991 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5993 -- Propagate No_Return if pragma applied to generic unit. This must
5994 -- be done explicitly because pragma does not appear in generic
5995 -- declaration (unlike the aspect case).
5997 if No_Return (Gen_Unit) then
5998 Set_No_Return (Act_Decl_Id);
5999 Set_No_Return (Anon_Id);
6000 end if;
6002 -- Mark both the instance spec and the anonymous package in case the
6003 -- body is instantiated at a later pass. This preserves the original
6004 -- context in effect for the body.
6006 if SPARK_Mode /= On then
6007 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6008 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6009 end if;
6011 if Legacy_Elaboration_Checks
6012 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6013 then
6014 Check_Elab_Instantiation (N);
6015 end if;
6017 -- Save the scenario for later examination by the ABE Processing
6018 -- phase.
6020 Record_Elaboration_Scenario (N);
6022 -- The instantiation results in a guaranteed ABE. Create a completing
6023 -- body for the subprogram declaration because the real body will not
6024 -- be instantiated.
6026 if Is_Known_Guaranteed_ABE (N) then
6027 Provide_Completing_Bodies (Instance_Spec (N));
6028 end if;
6030 if Is_Dispatching_Operation (Act_Decl_Id)
6031 and then Ada_Version >= Ada_2005
6032 then
6033 declare
6034 Formal : Entity_Id;
6036 begin
6037 Formal := First_Formal (Act_Decl_Id);
6038 while Present (Formal) loop
6039 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6040 and then Is_Controlling_Formal (Formal)
6041 and then not Can_Never_Be_Null (Formal)
6042 then
6043 Error_Msg_NE
6044 ("access parameter& is controlling,", N, Formal);
6045 Error_Msg_NE
6046 ("\corresponding parameter of & must be explicitly "
6047 & "null-excluding", N, Gen_Id);
6048 end if;
6050 Next_Formal (Formal);
6051 end loop;
6052 end;
6053 end if;
6055 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6057 Validate_Categorization_Dependency (N, Act_Decl_Id);
6059 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6060 Inherit_Context (Gen_Decl, N);
6062 Restore_Private_Views (Pack_Id, False);
6064 -- If the context requires a full instantiation, mark node for
6065 -- subsequent construction of the body.
6067 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6068 Check_Forward_Instantiation (Gen_Decl);
6070 -- The wrapper package is always delayed, because it does not
6071 -- constitute a freeze point, but to insure that the freeze node
6072 -- is placed properly, it is created directly when instantiating
6073 -- the body (otherwise the freeze node might appear to early for
6074 -- nested instantiations).
6076 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6077 Rewrite (N, Unit (Parent (N)));
6078 Set_Unit (Parent (N), N);
6079 end if;
6081 -- Replace instance node for library-level instantiations of
6082 -- intrinsic subprograms.
6084 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6085 Rewrite (N, Unit (Parent (N)));
6086 Set_Unit (Parent (N), N);
6087 end if;
6089 if Parent_Installed then
6090 Remove_Parent;
6091 end if;
6093 Restore_Hidden_Primitives (Vis_Prims_List);
6094 Restore_Env;
6095 Env_Installed := False;
6096 Generic_Renamings.Set_Last (0);
6097 Generic_Renamings_HTable.Reset;
6098 end if;
6100 <<Leave>>
6101 -- Analyze aspects in declaration if no errors appear in the instance.
6103 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6104 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6105 end if;
6107 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6108 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6109 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6111 exception
6112 when Instantiation_Error =>
6113 if Parent_Installed then
6114 Remove_Parent;
6115 end if;
6117 if Env_Installed then
6118 Restore_Env;
6119 end if;
6121 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6122 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6123 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6124 end Analyze_Subprogram_Instantiation;
6126 ---------------------------
6127 -- Get_Associated_Entity --
6128 ---------------------------
6130 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6131 Assoc : Entity_Id;
6133 begin
6134 Assoc := Associated_Entity (Id);
6136 if Present (Assoc) then
6137 while Present (Associated_Entity (Assoc)) loop
6138 Assoc := Associated_Entity (Assoc);
6139 end loop;
6140 end if;
6142 return Assoc;
6143 end Get_Associated_Entity;
6145 -------------------------
6146 -- Get_Associated_Node --
6147 -------------------------
6149 function Get_Associated_Node (N : Node_Id) return Node_Id is
6150 Assoc : Node_Id;
6152 begin
6153 Assoc := Associated_Node (N);
6155 if Nkind (Assoc) /= Nkind (N) then
6156 return Assoc;
6158 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6159 return Assoc;
6161 else
6162 -- If the node is part of an inner generic, it may itself have been
6163 -- remapped into a further generic copy. Associated_Node is otherwise
6164 -- used for the entity of the node, and will be of a different node
6165 -- kind, or else N has been rewritten as a literal or function call.
6167 while Present (Associated_Node (Assoc))
6168 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6169 loop
6170 Assoc := Associated_Node (Assoc);
6171 end loop;
6173 -- Follow an additional link in case the final node was rewritten.
6174 -- This can only happen with nested generic units.
6176 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6177 and then Present (Associated_Node (Assoc))
6178 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6179 | N_Explicit_Dereference
6180 | N_Integer_Literal
6181 | N_Real_Literal
6182 | N_String_Literal
6183 then
6184 Assoc := Associated_Node (Assoc);
6185 end if;
6187 -- An additional special case: an unconstrained type in an object
6188 -- declaration may have been rewritten as a local subtype constrained
6189 -- by the expression in the declaration. We need to recover the
6190 -- original entity, which may be global.
6192 if Present (Original_Node (Assoc))
6193 and then Nkind (Parent (N)) = N_Object_Declaration
6194 then
6195 Assoc := Original_Node (Assoc);
6196 end if;
6198 return Assoc;
6199 end if;
6200 end Get_Associated_Node;
6202 -----------------------------------
6203 -- Build_Subprogram_Decl_Wrapper --
6204 -----------------------------------
6206 function Build_Subprogram_Decl_Wrapper
6207 (Formal_Subp : Entity_Id) return Node_Id
6209 Loc : constant Source_Ptr := Sloc (Current_Scope);
6210 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6211 Decl : Node_Id;
6212 Subp : Entity_Id;
6213 Parm_Spec : Node_Id;
6214 Profile : List_Id := New_List;
6215 Spec : Node_Id;
6216 Form_F : Entity_Id;
6217 New_F : Entity_Id;
6219 begin
6221 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6222 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6223 Set_Is_Generic_Actual_Subprogram (Subp);
6225 Profile := Parameter_Specifications (
6226 New_Copy_Tree
6227 (Specification (Unit_Declaration_Node (Formal_Subp))));
6229 Form_F := First_Formal (Formal_Subp);
6230 Parm_Spec := First (Profile);
6232 -- Create new entities for the formals. Reset entities so that
6233 -- parameter types are properly resolved when wrapper declaration
6234 -- is analyzed.
6236 while Present (Parm_Spec) loop
6237 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6238 Set_Defining_Identifier (Parm_Spec, New_F);
6239 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6240 Next (Parm_Spec);
6241 Next_Formal (Form_F);
6242 end loop;
6244 if Ret_Type = Standard_Void_Type then
6245 Spec :=
6246 Make_Procedure_Specification (Loc,
6247 Defining_Unit_Name => Subp,
6248 Parameter_Specifications => Profile);
6249 else
6250 Spec :=
6251 Make_Function_Specification (Loc,
6252 Defining_Unit_Name => Subp,
6253 Parameter_Specifications => Profile,
6254 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6255 end if;
6257 Decl :=
6258 Make_Subprogram_Declaration (Loc, Specification => Spec);
6260 return Decl;
6261 end Build_Subprogram_Decl_Wrapper;
6263 -----------------------------------
6264 -- Build_Subprogram_Body_Wrapper --
6265 -----------------------------------
6267 function Build_Subprogram_Body_Wrapper
6268 (Formal_Subp : Entity_Id;
6269 Actual_Name : Node_Id) return Node_Id
6271 Loc : constant Source_Ptr := Sloc (Current_Scope);
6272 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6273 Spec_Node : constant Node_Id :=
6274 Specification
6275 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6276 Act : Node_Id;
6277 Actuals : List_Id;
6278 Body_Node : Node_Id;
6279 Stmt : Node_Id;
6280 begin
6281 Actuals := New_List;
6282 Act := First (Parameter_Specifications (Spec_Node));
6284 while Present (Act) loop
6285 Append_To (Actuals,
6286 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6287 Next (Act);
6288 end loop;
6290 if Ret_Type = Standard_Void_Type then
6291 Stmt := Make_Procedure_Call_Statement (Loc,
6292 Name => Actual_Name,
6293 Parameter_Associations => Actuals);
6295 else
6296 Stmt := Make_Simple_Return_Statement (Loc,
6297 Expression =>
6298 Make_Function_Call (Loc,
6299 Name => Actual_Name,
6300 Parameter_Associations => Actuals));
6301 end if;
6303 Body_Node := Make_Subprogram_Body (Loc,
6304 Specification => Spec_Node,
6305 Declarations => New_List,
6306 Handled_Statement_Sequence =>
6307 Make_Handled_Sequence_Of_Statements (Loc,
6308 Statements => New_List (Stmt)));
6310 return Body_Node;
6311 end Build_Subprogram_Body_Wrapper;
6313 -------------------------------------------
6314 -- Build_Instance_Compilation_Unit_Nodes --
6315 -------------------------------------------
6317 procedure Build_Instance_Compilation_Unit_Nodes
6318 (N : Node_Id;
6319 Act_Body : Node_Id;
6320 Act_Decl : Node_Id)
6322 Decl_Cunit : Node_Id;
6323 Body_Cunit : Node_Id;
6324 Citem : Node_Id;
6325 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6326 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6328 begin
6329 -- A new compilation unit node is built for the instance declaration.
6330 -- It relocates the auxiliary declaration node from the compilation unit
6331 -- where the instance appeared, so that declarations that originally
6332 -- followed the instance will be attached to the spec compilation unit.
6334 Decl_Cunit :=
6335 Make_Compilation_Unit (Sloc (N),
6336 Context_Items => Empty_List,
6337 Unit => Act_Decl,
6338 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6340 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6342 -- The new compilation unit is linked to its body, but both share the
6343 -- same file, so we do not set Body_Required on the new unit so as not
6344 -- to create a spurious dependency on a non-existent body in the ali.
6345 -- This simplifies CodePeer unit traversal.
6347 -- We use the original instantiation compilation unit as the resulting
6348 -- compilation unit of the instance, since this is the main unit.
6350 Rewrite (N, Act_Body);
6352 Body_Cunit := Parent (N);
6354 -- The two compilation unit nodes are linked by the Library_Unit field
6356 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6357 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6359 -- Preserve the private nature of the package if needed
6361 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6363 -- If the instance is not the main unit, its context, categorization
6364 -- and elaboration entity are not relevant to the compilation.
6366 if Body_Cunit /= Cunit (Main_Unit) then
6367 Make_Instance_Unit (Body_Cunit, In_Main => False);
6368 return;
6369 end if;
6371 -- The context clause items on the instantiation, which are now attached
6372 -- to the body compilation unit (since the body overwrote the original
6373 -- instantiation node), semantically belong on the spec, so copy them
6374 -- there. It's harmless to leave them on the body as well. In fact one
6375 -- could argue that they belong in both places.
6377 Citem := First (Context_Items (Body_Cunit));
6378 while Present (Citem) loop
6379 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6380 Next (Citem);
6381 end loop;
6383 -- Propagate categorization flags on packages, so that they appear in
6384 -- the ali file for the spec of the unit.
6386 if Ekind (New_Main) = E_Package then
6387 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6388 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6389 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6390 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6391 Set_Is_Remote_Call_Interface
6392 (Old_Main, Is_Remote_Call_Interface (New_Main));
6393 end if;
6395 -- Make entry in Units table, so that binder can generate call to
6396 -- elaboration procedure for body, if any.
6398 Make_Instance_Unit (Body_Cunit, In_Main => True);
6399 Main_Unit_Entity := New_Main;
6400 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6402 -- Build elaboration entity, since the instance may certainly generate
6403 -- elaboration code requiring a flag for protection.
6405 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6406 end Build_Instance_Compilation_Unit_Nodes;
6408 --------------------------------
6409 -- Check_Abbreviated_Instance --
6410 --------------------------------
6412 procedure Check_Abbreviated_Instance
6413 (N : Node_Id;
6414 Parent_Installed : in out Boolean)
6416 Inst_Node : Node_Id;
6418 begin
6419 if Nkind (N) = N_Package_Specification
6420 and then Is_Abbreviated_Instance (Defining_Entity (N))
6421 then
6422 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6423 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6424 end if;
6425 end Check_Abbreviated_Instance;
6427 -----------------------------
6428 -- Check_Access_Definition --
6429 -----------------------------
6431 procedure Check_Access_Definition (N : Node_Id) is
6432 begin
6433 pragma Assert
6434 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6435 null;
6436 end Check_Access_Definition;
6438 -----------------------------------
6439 -- Check_Formal_Package_Instance --
6440 -----------------------------------
6442 -- If the formal has specific parameters, they must match those of the
6443 -- actual. Both of them are instances, and the renaming declarations for
6444 -- their formal parameters appear in the same order in both. The analyzed
6445 -- formal has been analyzed in the context of the current instance.
6447 procedure Check_Formal_Package_Instance
6448 (Formal_Pack : Entity_Id;
6449 Actual_Pack : Entity_Id)
6451 E1 : Entity_Id := First_Entity (Actual_Pack);
6452 E2 : Entity_Id := First_Entity (Formal_Pack);
6453 Prev_E1 : Entity_Id;
6455 Expr1 : Node_Id;
6456 Expr2 : Node_Id;
6458 procedure Check_Mismatch (B : Boolean);
6459 -- Common error routine for mismatch between the parameters of the
6460 -- actual instance and those of the formal package.
6462 function Is_Defaulted (Param : Entity_Id) return Boolean;
6463 -- If the formal package has partly box-initialized formals, skip
6464 -- conformance check for these formals. Previously the code assumed
6465 -- that box initialization for a formal package applied to all its
6466 -- formal parameters.
6468 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6469 -- The formal may come from a nested formal package, and the actual may
6470 -- have been constant-folded. To determine whether the two denote the
6471 -- same entity we may have to traverse several definitions to recover
6472 -- the ultimate entity that they refer to.
6474 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6475 -- The formal and the actual must be identical, but if both are
6476 -- given by attributes they end up renaming different generated bodies,
6477 -- and we must verify that the attributes themselves match.
6479 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6480 -- Similarly, if the formal comes from a nested formal package, the
6481 -- actual may designate the formal through multiple renamings, which
6482 -- have to be followed to determine the original variable in question.
6484 --------------------
6485 -- Check_Mismatch --
6486 --------------------
6488 procedure Check_Mismatch (B : Boolean) is
6489 -- A Formal_Type_Declaration for a derived private type is rewritten
6490 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6491 -- which is why we examine the original node.
6493 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6495 begin
6496 if Kind = N_Formal_Type_Declaration then
6497 return;
6499 elsif Kind in N_Formal_Object_Declaration
6500 | N_Formal_Package_Declaration
6501 | N_Formal_Subprogram_Declaration
6502 then
6503 null;
6505 -- Ada 2012: If both formal and actual are incomplete types they
6506 -- are conformant.
6508 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6509 null;
6511 elsif B then
6512 Error_Msg_NE
6513 ("actual for & in actual instance does not match formal",
6514 Parent (Actual_Pack), E1);
6515 end if;
6516 end Check_Mismatch;
6518 ------------------
6519 -- Is_Defaulted --
6520 ------------------
6522 function Is_Defaulted (Param : Entity_Id) return Boolean is
6523 Assoc : Node_Id;
6525 begin
6526 Assoc :=
6527 First (Generic_Associations (Parent
6528 (Associated_Formal_Package (Actual_Pack))));
6530 while Present (Assoc) loop
6531 if Nkind (Assoc) = N_Others_Choice then
6532 return True;
6534 elsif Nkind (Assoc) = N_Generic_Association
6535 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6536 then
6537 return Box_Present (Assoc);
6538 end if;
6540 Next (Assoc);
6541 end loop;
6543 return False;
6544 end Is_Defaulted;
6546 --------------------------------
6547 -- Same_Instantiated_Constant --
6548 --------------------------------
6550 function Same_Instantiated_Constant
6551 (E1, E2 : Entity_Id) return Boolean
6553 Ent : Entity_Id;
6555 begin
6556 Ent := E2;
6557 while Present (Ent) loop
6558 if E1 = Ent then
6559 return True;
6561 elsif Ekind (Ent) /= E_Constant then
6562 return False;
6564 elsif Is_Entity_Name (Constant_Value (Ent)) then
6565 if Entity (Constant_Value (Ent)) = E1 then
6566 return True;
6567 else
6568 Ent := Entity (Constant_Value (Ent));
6569 end if;
6571 -- The actual may be a constant that has been folded. Recover
6572 -- original name.
6574 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6575 Ent := Entity (Original_Node (Constant_Value (Ent)));
6577 else
6578 return False;
6579 end if;
6580 end loop;
6582 return False;
6583 end Same_Instantiated_Constant;
6585 --------------------------------
6586 -- Same_Instantiated_Function --
6587 --------------------------------
6589 function Same_Instantiated_Function
6590 (E1, E2 : Entity_Id) return Boolean
6592 U1, U2 : Node_Id;
6593 begin
6594 if Alias (E1) = Alias (E2) then
6595 return True;
6597 elsif Present (Alias (E2)) then
6598 U1 := Original_Node (Unit_Declaration_Node (E1));
6599 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6601 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6602 and then Nkind (Name (U1)) = N_Attribute_Reference
6604 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6605 and then Nkind (Name (U2)) = N_Attribute_Reference
6607 and then
6608 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6609 else
6610 return False;
6611 end if;
6612 end Same_Instantiated_Function;
6614 --------------------------------
6615 -- Same_Instantiated_Variable --
6616 --------------------------------
6618 function Same_Instantiated_Variable
6619 (E1, E2 : Entity_Id) return Boolean
6621 function Original_Entity (E : Entity_Id) return Entity_Id;
6622 -- Follow chain of renamings to the ultimate ancestor
6624 ---------------------
6625 -- Original_Entity --
6626 ---------------------
6628 function Original_Entity (E : Entity_Id) return Entity_Id is
6629 Orig : Entity_Id;
6631 begin
6632 Orig := E;
6633 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6634 and then Present (Renamed_Object (Orig))
6635 and then Is_Entity_Name (Renamed_Object (Orig))
6636 loop
6637 Orig := Entity (Renamed_Object (Orig));
6638 end loop;
6640 return Orig;
6641 end Original_Entity;
6643 -- Start of processing for Same_Instantiated_Variable
6645 begin
6646 return Ekind (E1) = Ekind (E2)
6647 and then Original_Entity (E1) = Original_Entity (E2);
6648 end Same_Instantiated_Variable;
6650 -- Start of processing for Check_Formal_Package_Instance
6652 begin
6653 Prev_E1 := E1;
6654 while Present (E1) and then Present (E2) loop
6655 exit when Ekind (E1) = E_Package
6656 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6658 -- If the formal is the renaming of the formal package, this
6659 -- is the end of its formal part, which may occur before the
6660 -- end of the formal part in the actual in the presence of
6661 -- defaulted parameters in the formal package.
6663 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6664 and then Renamed_Entity (E2) = Scope (E2);
6666 -- The analysis of the actual may generate additional internal
6667 -- entities. If the formal is defaulted, there is no corresponding
6668 -- analysis and the internal entities must be skipped, until we
6669 -- find corresponding entities again.
6671 if Comes_From_Source (E2)
6672 and then not Comes_From_Source (E1)
6673 and then Chars (E1) /= Chars (E2)
6674 then
6675 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6676 Next_Entity (E1);
6677 end loop;
6678 end if;
6680 if No (E1) then
6681 return;
6683 -- Entities may be declared without full declaration, such as
6684 -- itypes and predefined operators (concatenation for arrays, eg).
6685 -- Skip it and keep the formal entity to find a later match for it.
6687 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6688 E1 := Prev_E1;
6689 goto Next_E;
6691 -- If the formal entity comes from a formal declaration, it was
6692 -- defaulted in the formal package, and no check is needed on it.
6694 elsif Nkind (Original_Node (Parent (E2))) in
6695 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6696 then
6697 -- If the formal is a tagged type the corresponding class-wide
6698 -- type has been generated as well, and it must be skipped.
6700 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6701 Next_Entity (E2);
6702 end if;
6704 goto Next_E;
6706 -- Ditto for defaulted formal subprograms.
6708 elsif Is_Overloadable (E1)
6709 and then Nkind (Unit_Declaration_Node (E2)) in
6710 N_Formal_Subprogram_Declaration
6711 then
6712 goto Next_E;
6714 elsif Is_Defaulted (E1) then
6715 goto Next_E;
6717 elsif Is_Type (E1) then
6719 -- Subtypes must statically match. E1, E2 are the local entities
6720 -- that are subtypes of the actuals. Itypes generated for other
6721 -- parameters need not be checked, the check will be performed
6722 -- on the parameters themselves.
6724 -- If E2 is a formal type declaration, it is a defaulted parameter
6725 -- and needs no checking.
6727 if not Is_Itype (E1) and then not Is_Itype (E2) then
6728 Check_Mismatch
6729 (not Is_Type (E2)
6730 or else Etype (E1) /= Etype (E2)
6731 or else not Subtypes_Statically_Match (E1, E2));
6732 end if;
6734 elsif Ekind (E1) = E_Constant then
6736 -- IN parameters must denote the same static value, or the same
6737 -- constant, or the literal null.
6739 Expr1 := Expression (Parent (E1));
6741 if Ekind (E2) /= E_Constant then
6742 Check_Mismatch (True);
6743 goto Next_E;
6744 else
6745 Expr2 := Expression (Parent (E2));
6746 end if;
6748 if Is_OK_Static_Expression (Expr1) then
6749 if not Is_OK_Static_Expression (Expr2) then
6750 Check_Mismatch (True);
6752 elsif Is_Discrete_Type (Etype (E1)) then
6753 declare
6754 V1 : constant Uint := Expr_Value (Expr1);
6755 V2 : constant Uint := Expr_Value (Expr2);
6756 begin
6757 Check_Mismatch (V1 /= V2);
6758 end;
6760 elsif Is_Real_Type (Etype (E1)) then
6761 declare
6762 V1 : constant Ureal := Expr_Value_R (Expr1);
6763 V2 : constant Ureal := Expr_Value_R (Expr2);
6764 begin
6765 Check_Mismatch (V1 /= V2);
6766 end;
6768 elsif Is_String_Type (Etype (E1))
6769 and then Nkind (Expr1) = N_String_Literal
6770 then
6771 if Nkind (Expr2) /= N_String_Literal then
6772 Check_Mismatch (True);
6773 else
6774 Check_Mismatch
6775 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6776 end if;
6777 end if;
6779 elsif Is_Entity_Name (Expr1) then
6780 if Is_Entity_Name (Expr2) then
6781 if Entity (Expr1) = Entity (Expr2) then
6782 null;
6783 else
6784 Check_Mismatch
6785 (not Same_Instantiated_Constant
6786 (Entity (Expr1), Entity (Expr2)));
6787 end if;
6789 else
6790 Check_Mismatch (True);
6791 end if;
6793 elsif Is_Entity_Name (Original_Node (Expr1))
6794 and then Is_Entity_Name (Expr2)
6795 and then Same_Instantiated_Constant
6796 (Entity (Original_Node (Expr1)), Entity (Expr2))
6797 then
6798 null;
6800 elsif Nkind (Expr1) = N_Null then
6801 Check_Mismatch (Nkind (Expr1) /= N_Null);
6803 else
6804 Check_Mismatch (True);
6805 end if;
6807 elsif Ekind (E1) = E_Variable then
6808 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6810 elsif Ekind (E1) = E_Package then
6811 Check_Mismatch
6812 (Ekind (E1) /= Ekind (E2)
6813 or else (Present (Renamed_Entity (E2))
6814 and then Renamed_Entity (E1) /=
6815 Renamed_Entity (E2)));
6817 elsif Is_Overloadable (E1) then
6818 -- Verify that the actual subprograms match. Note that actuals
6819 -- that are attributes are rewritten as subprograms. If the
6820 -- subprogram in the formal package is defaulted, no check is
6821 -- needed. Note that this can only happen in Ada 2005 when the
6822 -- formal package can be partially parameterized.
6824 if Nkind (Unit_Declaration_Node (E1)) =
6825 N_Subprogram_Renaming_Declaration
6826 and then From_Default (Unit_Declaration_Node (E1))
6827 then
6828 null;
6830 -- If the formal package has an "others" box association that
6831 -- covers this formal, there is no need for a check either.
6833 elsif Nkind (Unit_Declaration_Node (E2)) in
6834 N_Formal_Subprogram_Declaration
6835 and then Box_Present (Unit_Declaration_Node (E2))
6836 then
6837 null;
6839 -- No check needed if subprogram is a defaulted null procedure
6841 elsif No (Alias (E2))
6842 and then Ekind (E2) = E_Procedure
6843 and then
6844 Null_Present (Specification (Unit_Declaration_Node (E2)))
6845 then
6846 null;
6848 -- Otherwise the actual in the formal and the actual in the
6849 -- instantiation of the formal must match, up to renamings.
6851 else
6852 Check_Mismatch
6853 (Ekind (E2) /= Ekind (E1)
6854 or else not Same_Instantiated_Function (E1, E2));
6855 end if;
6857 else
6858 raise Program_Error;
6859 end if;
6861 <<Next_E>>
6862 Prev_E1 := E1;
6863 Next_Entity (E1);
6864 Next_Entity (E2);
6865 end loop;
6866 end Check_Formal_Package_Instance;
6868 ---------------------------
6869 -- Check_Formal_Packages --
6870 ---------------------------
6872 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6873 E : Entity_Id;
6874 Formal_P : Entity_Id;
6875 Formal_Decl : Node_Id;
6877 begin
6878 -- Iterate through the declarations in the instance, looking for package
6879 -- renaming declarations that denote instances of formal packages, until
6880 -- we find the renaming of the current package itself. The declaration
6881 -- of a formal package that requires conformance checking is followed by
6882 -- an internal entity that is the abbreviated instance.
6884 E := First_Entity (P_Id);
6885 while Present (E) loop
6886 if Ekind (E) = E_Package then
6887 exit when Renamed_Entity (E) = P_Id;
6889 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6890 Formal_Decl := Parent (Associated_Formal_Package (E));
6892 if Requires_Conformance_Checking (Formal_Decl) then
6893 Formal_P := Next_Entity (E);
6895 -- If the instance is within an enclosing instance body
6896 -- there is no need to verify the legality of current formal
6897 -- packages because they were legal in the generic body.
6898 -- This optimization may be applicable elsewhere, and it
6899 -- also removes spurious errors that may arise with
6900 -- on-the-fly inlining and confusion between private and
6901 -- full views.
6903 if not In_Instance_Body then
6904 Check_Formal_Package_Instance (Formal_P, E);
6905 end if;
6907 -- Restore the visibility of formals of the formal instance
6908 -- that are not defaulted, and are hidden within the current
6909 -- generic. These formals may be visible within an enclosing
6910 -- generic.
6912 declare
6913 Elmt : Elmt_Id;
6914 begin
6915 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6916 while Present (Elmt) loop
6917 Set_Is_Hidden (Node (Elmt), False);
6918 Next_Elmt (Elmt);
6919 end loop;
6920 end;
6922 -- After checking, remove the internal validating package.
6923 -- It is only needed for semantic checks, and as it may
6924 -- contain generic formal declarations it should not reach
6925 -- gigi.
6927 Remove (Unit_Declaration_Node (Formal_P));
6928 end if;
6929 end if;
6930 end if;
6932 Next_Entity (E);
6933 end loop;
6934 end Check_Formal_Packages;
6936 ---------------------------------
6937 -- Check_Forward_Instantiation --
6938 ---------------------------------
6940 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6941 S : Entity_Id;
6942 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6944 begin
6945 -- The instantiation appears before the generic body if we are in the
6946 -- scope of the unit containing the generic, either in its spec or in
6947 -- the package body, and before the generic body.
6949 if Ekind (Gen_Comp) = E_Package_Body then
6950 Gen_Comp := Spec_Entity (Gen_Comp);
6951 end if;
6953 if In_Open_Scopes (Gen_Comp)
6954 and then No (Corresponding_Body (Decl))
6955 then
6956 S := Current_Scope;
6958 while Present (S)
6959 and then not Is_Compilation_Unit (S)
6960 and then not Is_Child_Unit (S)
6961 loop
6962 if Ekind (S) = E_Package then
6963 Set_Has_Forward_Instantiation (S);
6964 end if;
6966 S := Scope (S);
6967 end loop;
6968 end if;
6969 end Check_Forward_Instantiation;
6971 ---------------------------
6972 -- Check_Generic_Actuals --
6973 ---------------------------
6975 -- The visibility of the actuals may be different between the point of
6976 -- generic instantiation and the instantiation of the body.
6978 procedure Check_Generic_Actuals
6979 (Instance : Entity_Id;
6980 Is_Formal_Box : Boolean)
6982 Gen_Id : constant Entity_Id
6983 := (if Is_Generic_Unit (Instance) then
6984 Instance
6985 elsif Is_Wrapper_Package (Instance) then
6986 Generic_Parent
6987 (Specification
6988 (Unit_Declaration_Node (Related_Instance (Instance))))
6989 else
6990 Generic_Parent (Package_Specification (Instance)));
6991 -- The generic unit
6993 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
6994 -- The enclosing scope of the generic unit
6996 procedure Check_Actual_Type (Typ : Entity_Id);
6997 -- If the type of the actual is a private type declared in the enclosing
6998 -- scope of the generic, either directly or through packages nested in
6999 -- bodies, but not a derived type of a private type declared elsewhere,
7000 -- then the body of the generic sees the full view of the type because
7001 -- it has to appear in the package body. If the type is private now then
7002 -- exchange views to restore the proper visibility in the instance.
7004 -----------------------
7005 -- Check_Actual_Type --
7006 -----------------------
7008 procedure Check_Actual_Type (Typ : Entity_Id) is
7009 Btyp : constant Entity_Id := Base_Type (Typ);
7011 function Scope_Within_Body_Or_Same
7012 (Inner : Entity_Id;
7013 Outer : Entity_Id) return Boolean;
7014 -- Determine whether scope Inner is within the body of scope Outer
7015 -- or is Outer itself.
7017 -------------------------------
7018 -- Scope_Within_Body_Or_Same --
7019 -------------------------------
7021 function Scope_Within_Body_Or_Same
7022 (Inner : Entity_Id;
7023 Outer : Entity_Id) return Boolean
7025 Curr : Entity_Id := Inner;
7027 begin
7028 while Curr /= Standard_Standard loop
7029 if Curr = Outer then
7030 return True;
7032 elsif Is_Package_Body_Entity (Curr) then
7033 Curr := Scope (Curr);
7035 else
7036 exit;
7037 end if;
7038 end loop;
7040 return False;
7041 end Scope_Within_Body_Or_Same;
7043 begin
7044 -- The exchange is only needed if the generic is defined
7045 -- within a package which is not a common ancestor of the
7046 -- scope of the instance, and is not already in scope.
7048 if Is_Private_Type (Btyp)
7049 and then not Has_Private_Ancestor (Btyp)
7050 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7051 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7052 and then Parent_Scope /= Scope (Instance)
7053 and then not Is_Child_Unit (Gen_Id)
7054 then
7055 Switch_View (Btyp);
7057 -- If the type of the entity is a subtype, it may also have
7058 -- to be made visible, together with the base type of its
7059 -- full view, after exchange.
7061 if Is_Private_Type (Typ) then
7062 Switch_View (Typ);
7063 Switch_View (Base_Type (Typ));
7064 end if;
7065 end if;
7066 end Check_Actual_Type;
7068 -- Local variables
7070 Astype : Entity_Id;
7071 E : Entity_Id;
7072 Formal : Node_Id;
7074 -- Start of processing for Check_Generic_Actuals
7076 begin
7077 E := First_Entity (Instance);
7078 while Present (E) loop
7079 if Is_Type (E)
7080 and then Nkind (Parent (E)) = N_Subtype_Declaration
7081 and then Scope (Etype (E)) /= Instance
7082 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7083 then
7084 declare
7085 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7087 begin
7088 -- Restore the proper view of the actual from the information
7089 -- saved earlier by Instantiate_Type.
7091 Check_Private_View (Indic);
7093 -- If this view is an array type, check its component type.
7094 -- This handles the case of an array type whose component
7095 -- type is private, used as the actual in an instantiation
7096 -- of a generic construct declared in the same package as
7097 -- the component type and taking an array type with this
7098 -- component type as formal type parameter.
7100 if Is_Array_Type (Etype (Indic)) then
7101 Check_Actual_Type
7102 (Component_Type_For_Private_View (Etype (Indic)));
7103 end if;
7104 end;
7106 -- If the actual is itself the formal of a parent instance,
7107 -- then also restore the proper view of its actual and so on.
7108 -- That's necessary for nested instantiations of the form
7110 -- generic
7111 -- type Component is private;
7112 -- type Array_Type is array (Positive range <>) of Component;
7113 -- procedure Proc;
7115 -- when the outermost actuals have inconsistent views, because
7116 -- the Component_Type of Array_Type of the inner instantiations
7117 -- is the actual of Component of the outermost one and not that
7118 -- of the corresponding inner instantiations.
7120 Astype := Ancestor_Subtype (E);
7121 while Present (Astype)
7122 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7123 and then Present (Generic_Parent_Type (Parent (Astype)))
7124 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7125 loop
7126 Check_Private_View (Subtype_Indication (Parent (Astype)));
7127 Astype := Ancestor_Subtype (Astype);
7128 end loop;
7130 Set_Is_Generic_Actual_Type (E);
7132 if Is_Private_Type (E) and then Present (Full_View (E)) then
7133 Set_Is_Generic_Actual_Type (Full_View (E));
7134 end if;
7136 Set_Is_Hidden (E, False);
7137 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7139 -- We constructed the generic actual type as a subtype of the
7140 -- supplied type. This means that it normally would not inherit
7141 -- subtype specific attributes of the actual, which is wrong for
7142 -- the generic case.
7144 Astype := Ancestor_Subtype (E);
7146 if No (Astype) then
7148 -- This can happen when E is an itype that is the full view of
7149 -- a private type completed, e.g. with a constrained array. In
7150 -- that case, use the first subtype, which will carry size
7151 -- information. The base type itself is unconstrained and will
7152 -- not carry it.
7154 Astype := First_Subtype (E);
7155 end if;
7157 Set_Size_Info (E, Astype);
7158 Copy_RM_Size (To => E, From => Astype);
7159 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7161 if Is_Discrete_Or_Fixed_Point_Type (E) then
7162 Set_RM_Size (E, RM_Size (Astype));
7163 end if;
7165 elsif Ekind (E) = E_Package then
7167 -- If this is the renaming for the current instance, we're done.
7168 -- Otherwise it is a formal package. If the corresponding formal
7169 -- was declared with a box, the (instantiations of the) generic
7170 -- formal part are also visible. Otherwise, ignore the entity
7171 -- created to validate the actuals.
7173 if Renamed_Entity (E) = Instance then
7174 exit;
7176 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7177 null;
7179 -- The visibility of a formal of an enclosing generic is already
7180 -- correct.
7182 elsif Denotes_Formal_Package (E) then
7183 null;
7185 elsif Present (Associated_Formal_Package (E))
7186 and then not Is_Generic_Formal (E)
7187 then
7188 Check_Generic_Actuals
7189 (Renamed_Entity (E),
7190 Is_Formal_Box =>
7191 Box_Present (Parent (Associated_Formal_Package (E))));
7193 Set_Is_Hidden (E, False);
7194 end if;
7196 -- If this is a subprogram instance (in a wrapper package) the
7197 -- actual is fully visible.
7199 elsif Is_Wrapper_Package (Instance) then
7200 Set_Is_Hidden (E, False);
7202 -- If the formal package is declared with a box, or if the formal
7203 -- parameter is defaulted, it is visible in the body.
7205 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7206 Set_Is_Hidden (E, False);
7207 end if;
7209 -- Check directly the type of the actual objects, including the
7210 -- component type for array types.
7212 if Ekind (E) in E_Constant | E_Variable then
7213 Check_Actual_Type (Etype (E));
7215 if Is_Array_Type (Etype (E)) then
7216 Check_Actual_Type (Component_Type (Etype (E)));
7217 end if;
7219 -- As well as the type of formal parameters of actual subprograms
7221 elsif Ekind (E) in E_Function | E_Procedure
7222 and then Is_Generic_Actual_Subprogram (E)
7223 and then Present (Alias (E))
7224 then
7225 Formal := First_Formal (Alias (E));
7226 while Present (Formal) loop
7227 Check_Actual_Type (Etype (Formal));
7228 Next_Formal (Formal);
7229 end loop;
7230 end if;
7232 Next_Entity (E);
7233 end loop;
7234 end Check_Generic_Actuals;
7236 ------------------------------
7237 -- Check_Generic_Child_Unit --
7238 ------------------------------
7240 procedure Check_Generic_Child_Unit
7241 (Gen_Id : Node_Id;
7242 Parent_Installed : in out Boolean)
7244 Loc : constant Source_Ptr := Sloc (Gen_Id);
7245 Gen_Par : Entity_Id := Empty;
7246 E : Entity_Id;
7247 Inst_Par : Entity_Id := Empty;
7248 S : Node_Id;
7250 function Find_Generic_Child
7251 (Scop : Entity_Id;
7252 Id : Node_Id) return Entity_Id;
7253 -- Search generic parent for possible child unit with the given name
7255 function In_Enclosing_Instance return Boolean;
7256 -- Within an instance of the parent, the child unit may be denoted by
7257 -- a simple name, or an abbreviated expanded name. Examine enclosing
7258 -- scopes to locate a possible parent instantiation.
7260 ------------------------
7261 -- Find_Generic_Child --
7262 ------------------------
7264 function Find_Generic_Child
7265 (Scop : Entity_Id;
7266 Id : Node_Id) return Entity_Id
7268 E : Entity_Id;
7270 begin
7271 -- If entity of name is already set, instance has already been
7272 -- resolved, e.g. in an enclosing instantiation.
7274 if Present (Entity (Id)) then
7275 if Scope (Entity (Id)) = Scop then
7276 return Entity (Id);
7277 else
7278 return Empty;
7279 end if;
7281 else
7282 E := First_Entity (Scop);
7283 while Present (E) loop
7284 if Chars (E) = Chars (Id)
7285 and then Is_Child_Unit (E)
7286 then
7287 if Is_Child_Unit (E)
7288 and then not Is_Visible_Lib_Unit (E)
7289 then
7290 Error_Msg_NE
7291 ("generic child unit& is not visible", Gen_Id, E);
7292 end if;
7294 Set_Entity (Id, E);
7295 return E;
7296 end if;
7298 Next_Entity (E);
7299 end loop;
7301 return Empty;
7302 end if;
7303 end Find_Generic_Child;
7305 ---------------------------
7306 -- In_Enclosing_Instance --
7307 ---------------------------
7309 function In_Enclosing_Instance return Boolean is
7310 Enclosing_Instance : Node_Id;
7311 Instance_Decl : Node_Id;
7313 begin
7314 -- We do not inline any call that contains instantiations, except
7315 -- for instantiations of Unchecked_Conversion, so if we are within
7316 -- an inlined body the current instance does not require parents.
7318 if In_Inlined_Body then
7319 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7320 return False;
7321 end if;
7323 -- Loop to check enclosing scopes
7325 Enclosing_Instance := Current_Scope;
7326 while Present (Enclosing_Instance) loop
7327 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7329 if Ekind (Enclosing_Instance) = E_Package
7330 and then Is_Generic_Instance (Enclosing_Instance)
7331 and then Present
7332 (Generic_Parent (Specification (Instance_Decl)))
7333 then
7334 -- Check whether the generic we are looking for is a child of
7335 -- this instance.
7337 E := Find_Generic_Child
7338 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7339 exit when Present (E);
7341 else
7342 E := Empty;
7343 end if;
7345 Enclosing_Instance := Scope (Enclosing_Instance);
7346 end loop;
7348 if No (E) then
7350 -- Not a child unit
7352 Analyze (Gen_Id);
7353 return False;
7355 else
7356 Rewrite (Gen_Id,
7357 Make_Expanded_Name (Loc,
7358 Chars => Chars (E),
7359 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7360 Selector_Name => New_Occurrence_Of (E, Loc)));
7362 Set_Entity (Gen_Id, E);
7363 Set_Etype (Gen_Id, Etype (E));
7364 Parent_Installed := False; -- Already in scope.
7365 return True;
7366 end if;
7367 end In_Enclosing_Instance;
7369 -- Start of processing for Check_Generic_Child_Unit
7371 begin
7372 -- If the name of the generic is given by a selected component, it may
7373 -- be the name of a generic child unit, and the prefix is the name of an
7374 -- instance of the parent, in which case the child unit must be visible.
7375 -- If this instance is not in scope, it must be placed there and removed
7376 -- after instantiation, because what is being instantiated is not the
7377 -- original child, but the corresponding child present in the instance
7378 -- of the parent.
7380 -- If the child is instantiated within the parent, it can be given by
7381 -- a simple name. In this case the instance is already in scope, but
7382 -- the child generic must be recovered from the generic parent as well.
7384 if Nkind (Gen_Id) = N_Selected_Component then
7385 S := Selector_Name (Gen_Id);
7386 Analyze (Prefix (Gen_Id));
7387 Inst_Par := Entity (Prefix (Gen_Id));
7389 if Ekind (Inst_Par) = E_Package
7390 and then Present (Renamed_Entity (Inst_Par))
7391 then
7392 Inst_Par := Renamed_Entity (Inst_Par);
7393 end if;
7395 if Ekind (Inst_Par) = E_Package then
7396 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7397 Gen_Par := Generic_Parent (Parent (Inst_Par));
7399 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7400 and then
7401 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7402 then
7403 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7404 end if;
7406 elsif Ekind (Inst_Par) = E_Generic_Package
7407 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7408 then
7409 -- A formal package may be a real child package, and not the
7410 -- implicit instance within a parent. In this case the child is
7411 -- not visible and has to be retrieved explicitly as well.
7413 Gen_Par := Inst_Par;
7414 end if;
7416 if Present (Gen_Par) then
7418 -- The prefix denotes an instantiation. The entity itself may be a
7419 -- nested generic, or a child unit.
7421 E := Find_Generic_Child (Gen_Par, S);
7423 if Present (E) then
7424 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7425 Set_Entity (Gen_Id, E);
7426 Set_Etype (Gen_Id, Etype (E));
7427 Set_Entity (S, E);
7428 Set_Etype (S, Etype (E));
7430 -- Indicate that this is a reference to the parent
7432 if In_Extended_Main_Source_Unit (Gen_Id) then
7433 Set_Is_Instantiated (Inst_Par);
7434 end if;
7436 -- A common mistake is to replicate the naming scheme of a
7437 -- hierarchy by instantiating a generic child directly, rather
7438 -- than the implicit child in a parent instance:
7440 -- generic .. package Gpar is ..
7441 -- generic .. package Gpar.Child is ..
7442 -- package Par is new Gpar ();
7444 -- with Gpar.Child;
7445 -- package Par.Child is new Gpar.Child ();
7446 -- rather than Par.Child
7448 -- In this case the instantiation is within Par, which is an
7449 -- instance, but Gpar does not denote Par because we are not IN
7450 -- the instance of Gpar, so this is illegal. The test below
7451 -- recognizes this particular case.
7453 declare
7454 -- We want to reject the final instantiation in
7455 -- generic package G1 is end G1;
7456 -- generic package G1.G2 is end G1.G2;
7457 -- with G1; package I1 is new G1;
7458 -- with G1.G2; package I1.I2 is new G1.G2;
7459 -- because the use of G1.G2 should instead be either
7460 -- I1.G2 or simply G2. However, the tree that is built
7461 -- in this case is wrong. In the expanded copy
7462 -- of G2, we need (and therefore generate) a renaming
7463 -- package G1 renames I1;
7464 -- but this renaming should not participate in resolving
7465 -- this occurrence of the name "G1.G2"; unfortunately,
7466 -- it does. Rather than correct this error, we compensate
7467 -- for it in this function.
7469 -- We also perform another adjustment here. If we are
7470 -- currently inside a generic package, then that
7471 -- generic package needs to be treated as a package.
7472 -- For example, if a generic Aaa declares a nested generic
7473 -- Bbb (perhaps as a child unit) then Aaa can also legally
7474 -- declare an instance of Aaa.Bbb.
7476 function Adjusted_Inst_Par_Ekind return Entity_Kind;
7478 -----------------------------
7479 -- Adjusted_Inst_Par_Ekind --
7480 -----------------------------
7482 function Adjusted_Inst_Par_Ekind return Entity_Kind is
7483 Prefix_Entity : Entity_Id;
7484 Inst_Par_GP : Node_Id;
7485 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
7486 begin
7487 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
7488 then
7489 Inst_Par_Parent := Parent (Inst_Par_Parent);
7490 end if;
7492 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
7494 if Nkind (Gen_Id) = N_Expanded_Name
7495 and then Present (Inst_Par_GP)
7496 and then Ekind (Inst_Par_GP) = E_Generic_Package
7497 then
7498 Prefix_Entity := Entity (Prefix (Gen_Id));
7500 if Present (Prefix_Entity)
7501 and then not Comes_From_Source (Prefix_Entity)
7502 and then Nkind (Parent (Prefix_Entity)) =
7503 N_Package_Renaming_Declaration
7504 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
7505 then
7506 return E_Generic_Package;
7507 end if;
7508 end if;
7510 if Ekind (Inst_Par) = E_Generic_Package
7511 and then In_Open_Scopes (Inst_Par)
7512 then
7513 -- If we are inside a generic package then
7514 -- treat it as a package.
7515 return E_Package;
7516 end if;
7518 -- The usual path
7519 return Ekind (Inst_Par);
7520 end Adjusted_Inst_Par_Ekind;
7522 begin
7523 if Is_Child_Unit (E)
7524 and then (No (Inst_Par)
7525 or else Adjusted_Inst_Par_Ekind =
7526 E_Generic_Package)
7527 and then (not In_Instance
7528 or else Nkind (Parent (Parent (Gen_Id))) =
7529 N_Compilation_Unit)
7530 then
7531 Error_Msg_N
7532 ("prefix of generic child unit must be " &
7533 "instance of parent",
7534 Gen_Id);
7535 end if;
7536 end;
7538 if not In_Open_Scopes (Inst_Par)
7539 and then Nkind (Parent (Gen_Id)) not in
7540 N_Generic_Renaming_Declaration
7541 then
7542 Install_Parent (Inst_Par);
7543 Parent_Installed := True;
7545 elsif In_Open_Scopes (Inst_Par) then
7547 -- If the parent is already installed, install the actuals
7548 -- for its formal packages. This is necessary when the child
7549 -- instance is a child of the parent instance: in this case,
7550 -- the parent is placed on the scope stack but the formal
7551 -- packages are not made visible.
7553 Install_Formal_Packages (Inst_Par);
7554 end if;
7556 else
7557 -- If the generic parent does not contain an entity that
7558 -- corresponds to the selector, the instance doesn't either.
7559 -- Analyzing the node will yield the appropriate error message.
7560 -- If the entity is not a child unit, then it is an inner
7561 -- generic in the parent.
7563 Analyze (Gen_Id);
7564 end if;
7566 else
7567 Analyze (Gen_Id);
7569 if Is_Child_Unit (Entity (Gen_Id))
7570 and then
7571 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7572 and then not In_Open_Scopes (Inst_Par)
7573 then
7574 Install_Parent (Inst_Par);
7575 Parent_Installed := True;
7577 -- The generic unit may be the renaming of the implicit child
7578 -- present in an instance. In that case the parent instance is
7579 -- obtained from the name of the renamed entity.
7581 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7582 and then Present (Renamed_Entity (Entity (Gen_Id)))
7583 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7584 then
7585 declare
7586 Renamed_Package : constant Node_Id :=
7587 Name (Parent (Entity (Gen_Id)));
7588 begin
7589 if Nkind (Renamed_Package) = N_Expanded_Name then
7590 Inst_Par := Entity (Prefix (Renamed_Package));
7591 Install_Parent (Inst_Par);
7592 Parent_Installed := True;
7593 end if;
7594 end;
7595 end if;
7596 end if;
7598 elsif Nkind (Gen_Id) = N_Expanded_Name then
7600 -- Entity already present, analyze prefix, whose meaning may be an
7601 -- instance in the current context. If it is an instance of a
7602 -- relative within another, the proper parent may still have to be
7603 -- installed, if they are not of the same generation.
7605 Analyze (Prefix (Gen_Id));
7607 -- Prevent cascaded errors
7609 if Etype (Prefix (Gen_Id)) = Any_Type then
7610 return;
7611 end if;
7613 -- In the unlikely case that a local declaration hides the name of
7614 -- the parent package, locate it on the homonym chain. If the context
7615 -- is an instance of the parent, the renaming entity is flagged as
7616 -- such.
7618 Inst_Par := Entity (Prefix (Gen_Id));
7619 while Present (Inst_Par)
7620 and then not Is_Package_Or_Generic_Package (Inst_Par)
7621 loop
7622 Inst_Par := Homonym (Inst_Par);
7623 end loop;
7625 pragma Assert (Present (Inst_Par));
7626 Set_Entity (Prefix (Gen_Id), Inst_Par);
7628 if In_Enclosing_Instance then
7629 null;
7631 elsif Present (Entity (Gen_Id))
7632 and then No (Renamed_Entity (Entity (Gen_Id)))
7633 and then Is_Child_Unit (Entity (Gen_Id))
7634 and then not In_Open_Scopes (Inst_Par)
7635 then
7636 Install_Parent (Inst_Par);
7637 Parent_Installed := True;
7639 -- Handle renaming of generic child unit
7641 elsif Present (Entity (Gen_Id))
7642 and then Present (Renamed_Entity (Entity (Gen_Id)))
7643 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7644 then
7645 declare
7646 E : Entity_Id;
7647 Ren_Decl : Node_Id;
7649 begin
7650 -- The entity of the renamed generic child unit does not
7651 -- have any reference to the instantiated parent. In order to
7652 -- locate it we traverse the scope containing the renaming
7653 -- declaration; the instance of the parent is available in
7654 -- the prefix of the renaming declaration. For example:
7656 -- package A is
7657 -- package Inst_Par is new ...
7658 -- generic package Ren_Child renames Ins_Par.Child;
7659 -- end;
7661 -- with A;
7662 -- package B is
7663 -- package Inst_Child is new A.Ren_Child;
7664 -- end;
7666 E := First_Entity (Entity (Prefix (Gen_Id)));
7667 while Present (E) loop
7668 if not Is_Object (E)
7669 and then Present (Renamed_Entity (E))
7670 and then
7671 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7672 then
7673 Ren_Decl := Parent (E);
7674 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7676 if not In_Open_Scopes (Inst_Par) then
7677 Install_Parent (Inst_Par);
7678 Parent_Installed := True;
7679 end if;
7681 exit;
7682 end if;
7684 E := Next_Entity (E);
7685 end loop;
7686 end;
7687 end if;
7689 elsif In_Enclosing_Instance then
7691 -- The child unit is found in some enclosing scope
7693 null;
7695 else
7696 Analyze (Gen_Id);
7698 -- If this is the renaming of the implicit child in a parent
7699 -- instance, recover the parent name and install it.
7701 if Is_Entity_Name (Gen_Id) then
7702 E := Entity (Gen_Id);
7704 if Is_Generic_Unit (E)
7705 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7706 and then Is_Child_Unit (Renamed_Entity (E))
7707 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7708 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7709 then
7710 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7711 Inst_Par := Entity (Prefix (Gen_Id));
7713 if not In_Open_Scopes (Inst_Par) then
7714 Install_Parent (Inst_Par);
7715 Parent_Installed := True;
7716 end if;
7718 -- If it is a child unit of a non-generic parent, it may be
7719 -- use-visible and given by a direct name. Install parent as
7720 -- for other cases.
7722 elsif Is_Generic_Unit (E)
7723 and then Is_Child_Unit (E)
7724 and then
7725 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7726 and then not Is_Generic_Unit (Scope (E))
7727 then
7728 if not In_Open_Scopes (Scope (E)) then
7729 Install_Parent (Scope (E));
7730 Parent_Installed := True;
7731 end if;
7732 end if;
7733 end if;
7734 end if;
7735 end Check_Generic_Child_Unit;
7737 -----------------------------
7738 -- Check_Hidden_Child_Unit --
7739 -----------------------------
7741 procedure Check_Hidden_Child_Unit
7742 (N : Node_Id;
7743 Gen_Unit : Entity_Id;
7744 Act_Decl_Id : Entity_Id)
7746 Gen_Id : constant Node_Id := Name (N);
7748 begin
7749 if Is_Child_Unit (Gen_Unit)
7750 and then Is_Child_Unit (Act_Decl_Id)
7751 and then Nkind (Gen_Id) = N_Expanded_Name
7752 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7753 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7754 then
7755 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7756 Error_Msg_NE
7757 ("generic unit & is implicitly declared in &",
7758 Defining_Unit_Name (N), Gen_Unit);
7759 Error_Msg_N ("\instance must have different name",
7760 Defining_Unit_Name (N));
7761 end if;
7762 end Check_Hidden_Child_Unit;
7764 ------------------------
7765 -- Check_Private_View --
7766 ------------------------
7768 procedure Check_Private_View (N : Node_Id) is
7769 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
7770 Typ : constant Entity_Id :=
7771 (if Comparison then Compare_Type (N) else Etype (N));
7773 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
7774 -- Check that the available view of T matches Private_View and, if not,
7775 -- switch the view of T or of its base type.
7777 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
7778 BT : constant Entity_Id := Base_Type (T);
7780 begin
7781 -- If the full declaration was not visible in the generic, stop here
7783 if Private_View then
7784 return;
7785 end if;
7787 -- Exchange views if the type was not private in the generic but is
7788 -- private at the point of instantiation. Do not exchange views if
7789 -- the scope of the type is in scope. This can happen if both generic
7790 -- and instance are sibling units, or if type is defined in a parent.
7791 -- In this case the visibility of the type will be correct for all
7792 -- semantic checks.
7794 if Is_Private_Type (T)
7795 and then Present (Full_View (T))
7796 and then not In_Open_Scopes (Scope (T))
7797 then
7798 Switch_View (T);
7800 -- Finally, a nonprivate subtype may have a private base type, which
7801 -- must be exchanged for consistency. This can happen when a package
7802 -- body is instantiated, when the scope stack is empty but in fact
7803 -- the subtype and the base type are declared in an enclosing scope.
7805 -- Note that in this case we introduce an inconsistency in the view
7806 -- set, because we switch the base type BT, but there could be some
7807 -- private dependent subtypes of BT which remain unswitched. Such
7808 -- subtypes might need to be switched at a later point (see specific
7809 -- provision for that case in Switch_View).
7811 elsif not Is_Private_Type (T)
7812 and then Is_Private_Type (BT)
7813 and then Present (Full_View (BT))
7814 and then not In_Open_Scopes (BT)
7815 then
7816 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7817 Exchange_Declarations (BT);
7818 end if;
7819 end Check_Private_Type;
7821 begin
7822 if Present (Typ) then
7823 -- If the type appears in a subtype declaration, the subtype in
7824 -- instance must have a view compatible with that of its parent,
7825 -- which must be exchanged (see corresponding code in Restore_
7826 -- Private_Views) so we make an exception to the open scope rule
7827 -- implemented by Check_Private_Type above.
7829 if Has_Private_View (N)
7830 and then not Is_Private_Type (Typ)
7831 and then not Has_Been_Exchanged (Typ)
7832 and then (not In_Open_Scopes (Scope (Typ))
7833 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7834 then
7835 declare
7836 Assoc : constant Node_Id := Get_Associated_Node (N);
7838 begin
7839 -- In the generic, only the private declaration was visible
7841 Prepend_Elmt (Typ, Exchanged_Views);
7842 Exchange_Declarations
7843 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
7844 end;
7846 -- Check that the available views of Typ match their respective flag.
7847 -- Note that the type of a visible discriminant is never private.
7849 else
7850 Check_Private_Type (Typ, Has_Private_View (N));
7852 if Is_Access_Type (Typ) then
7853 Check_Private_Type
7854 (Designated_Type (Typ), Has_Secondary_Private_View (N));
7856 elsif Is_Array_Type (Typ) then
7857 Check_Private_Type
7858 (Component_Type_For_Private_View (Typ),
7859 Has_Secondary_Private_View (N));
7861 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
7862 and then Has_Discriminants (Typ)
7863 then
7864 declare
7865 Disc : Entity_Id;
7867 begin
7868 Disc := First_Discriminant (Typ);
7869 while Present (Disc) loop
7870 Check_Private_Type (Etype (Disc), False);
7871 Next_Discriminant (Disc);
7872 end loop;
7873 end;
7874 end if;
7875 end if;
7876 end if;
7877 end Check_Private_View;
7879 -----------------------------
7880 -- Check_Hidden_Primitives --
7881 -----------------------------
7883 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7884 Actual : Node_Id;
7885 Gen_T : Entity_Id;
7886 Result : Elist_Id := No_Elist;
7888 begin
7889 if No (Assoc_List) then
7890 return No_Elist;
7891 end if;
7893 -- Traverse the list of associations between formals and actuals
7894 -- searching for renamings of tagged types
7896 Actual := First (Assoc_List);
7897 while Present (Actual) loop
7898 if Nkind (Actual) = N_Subtype_Declaration then
7899 Gen_T := Generic_Parent_Type (Actual);
7901 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7903 -- Traverse the list of primitives of the actual types
7904 -- searching for hidden primitives that are visible in the
7905 -- corresponding generic formal; leave them visible and
7906 -- append them to Result to restore their decoration later.
7908 Install_Hidden_Primitives
7909 (Prims_List => Result,
7910 Gen_T => Gen_T,
7911 Act_T => Entity (Subtype_Indication (Actual)));
7912 end if;
7913 end if;
7915 Next (Actual);
7916 end loop;
7918 return Result;
7919 end Check_Hidden_Primitives;
7921 -------------------------------------
7922 -- Component_Type_For_Private_View --
7923 -------------------------------------
7925 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
7926 Typ : constant Entity_Id := Component_Type (T);
7928 begin
7929 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
7930 return Component_Type_For_Private_View (Typ);
7931 else
7932 return Typ;
7933 end if;
7934 end Component_Type_For_Private_View;
7936 --------------------------
7937 -- Contains_Instance_Of --
7938 --------------------------
7940 function Contains_Instance_Of
7941 (Inner : Entity_Id;
7942 Outer : Entity_Id;
7943 N : Node_Id) return Boolean
7945 Elmt : Elmt_Id;
7946 Scop : Entity_Id;
7948 begin
7949 Scop := Outer;
7951 -- Verify that there are no circular instantiations. We check whether
7952 -- the unit contains an instance of the current scope or some enclosing
7953 -- scope (in case one of the instances appears in a subunit). Longer
7954 -- circularities involving subunits might seem too pathological to
7955 -- consider, but they were not too pathological for the authors of
7956 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7957 -- enclosing generic scopes as containing an instance.
7959 loop
7960 -- Within a generic subprogram body, the scope is not generic, to
7961 -- allow for recursive subprograms. Use the declaration to determine
7962 -- whether this is a generic unit.
7964 if Ekind (Scop) = E_Generic_Package
7965 or else (Is_Subprogram (Scop)
7966 and then Nkind (Unit_Declaration_Node (Scop)) =
7967 N_Generic_Subprogram_Declaration)
7968 then
7969 Elmt := First_Elmt (Inner_Instances (Inner));
7971 while Present (Elmt) loop
7972 if Node (Elmt) = Scop then
7973 Error_Msg_Node_2 := Inner;
7974 Error_Msg_NE
7975 ("circular instantiation: & instantiated within &!",
7976 N, Scop);
7977 return True;
7979 elsif Node (Elmt) = Inner then
7980 return True;
7982 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7983 Error_Msg_Node_2 := Inner;
7984 Error_Msg_NE
7985 ("circular instantiation: & instantiated within &!",
7986 N, Node (Elmt));
7987 return True;
7988 end if;
7990 Next_Elmt (Elmt);
7991 end loop;
7993 -- Indicate that Inner is being instantiated within Scop
7995 Append_Elmt (Inner, Inner_Instances (Scop));
7996 end if;
7998 if Scop = Standard_Standard then
7999 exit;
8000 else
8001 Scop := Scope (Scop);
8002 end if;
8003 end loop;
8005 return False;
8006 end Contains_Instance_Of;
8008 -----------------------
8009 -- Copy_Generic_Node --
8010 -----------------------
8012 function Copy_Generic_Node
8013 (N : Node_Id;
8014 Parent_Id : Node_Id;
8015 Instantiating : Boolean) return Node_Id
8017 Ent : Entity_Id;
8018 New_N : Node_Id;
8020 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8021 -- Check the given value of one of the Fields referenced by the current
8022 -- node to determine whether to copy it recursively. The field may hold
8023 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8024 -- Char) in which case it need not be copied.
8026 procedure Copy_Descendants;
8027 -- Common utility for various nodes
8029 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8030 -- Make copy of element list
8032 function Copy_Generic_List
8033 (L : List_Id;
8034 Parent_Id : Node_Id) return List_Id;
8035 -- Apply Copy_Generic_Node recursively to the members of a node list
8037 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8038 -- True if an identifier is part of the defining program unit name of
8039 -- a child unit.
8040 -- Consider removing this subprogram now that ASIS no longer uses it.
8042 ----------------------
8043 -- Copy_Descendants --
8044 ----------------------
8046 procedure Copy_Descendants is
8047 procedure Walk is new
8048 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8049 begin
8050 Walk (New_N, N);
8051 end Copy_Descendants;
8053 -----------------------------
8054 -- Copy_Generic_Descendant --
8055 -----------------------------
8057 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8058 begin
8059 if D = Union_Id (Empty) then
8060 return D;
8062 elsif D in Node_Range then
8063 return Union_Id
8064 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8066 elsif D in List_Range then
8067 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8069 elsif D in Elist_Range then
8070 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8072 -- Nothing else is copyable (e.g. Uint values), return as is
8074 else
8075 return D;
8076 end if;
8077 end Copy_Generic_Descendant;
8079 ------------------------
8080 -- Copy_Generic_Elist --
8081 ------------------------
8083 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8084 M : Elmt_Id;
8085 L : Elist_Id;
8087 begin
8088 if Present (E) then
8089 L := New_Elmt_List;
8090 M := First_Elmt (E);
8091 while Present (M) loop
8092 Append_Elmt
8093 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8094 Next_Elmt (M);
8095 end loop;
8097 return L;
8099 else
8100 return No_Elist;
8101 end if;
8102 end Copy_Generic_Elist;
8104 -----------------------
8105 -- Copy_Generic_List --
8106 -----------------------
8108 function Copy_Generic_List
8109 (L : List_Id;
8110 Parent_Id : Node_Id) return List_Id
8112 N : Node_Id;
8113 New_L : List_Id;
8115 begin
8116 if Present (L) then
8117 New_L := New_List;
8118 Set_Parent (New_L, Parent_Id);
8120 N := First (L);
8121 while Present (N) loop
8122 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8123 Next (N);
8124 end loop;
8126 return New_L;
8128 else
8129 return No_List;
8130 end if;
8131 end Copy_Generic_List;
8133 ---------------------------
8134 -- In_Defining_Unit_Name --
8135 ---------------------------
8137 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8138 begin
8139 return
8140 Present (Parent (Nam))
8141 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8142 or else
8143 (Nkind (Parent (Nam)) = N_Expanded_Name
8144 and then In_Defining_Unit_Name (Parent (Nam))));
8145 end In_Defining_Unit_Name;
8147 -- Start of processing for Copy_Generic_Node
8149 begin
8150 if N = Empty then
8151 return N;
8152 end if;
8154 New_N := New_Copy (N);
8156 -- If we are instantiating, we want to adjust the sloc based on the
8157 -- current S_Adjustment. However, if this is the root node of a subunit,
8158 -- we need to defer that adjustment to below (see "elsif Instantiating
8159 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8160 -- computed the adjustment.
8162 if Instantiating
8163 and then not (Nkind (N) in N_Proper_Body
8164 and then Was_Originally_Stub (N))
8165 then
8166 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8167 end if;
8169 if not Is_List_Member (N) then
8170 Set_Parent (New_N, Parent_Id);
8171 end if;
8173 -- Special casing for identifiers and other entity names and operators
8175 if Nkind (N) in N_Character_Literal
8176 | N_Expanded_Name
8177 | N_Identifier
8178 | N_Operator_Symbol
8179 | N_Op
8180 then
8181 if not Instantiating then
8183 -- Link both nodes in order to assign subsequently the entity of
8184 -- the copy to the original node, in case this is a global
8185 -- reference.
8187 Set_Associated_Node (N, New_N);
8189 -- If we are within an instantiation, this is a nested generic
8190 -- that has already been analyzed at the point of definition.
8191 -- We must preserve references that were global to the enclosing
8192 -- parent at that point. Other occurrences, whether global or
8193 -- local to the current generic, must be resolved anew, so we
8194 -- reset the entity in the generic copy. A global reference has a
8195 -- smaller depth than the parent, or else the same depth in case
8196 -- both are distinct compilation units.
8198 -- A child unit is implicitly declared within the enclosing parent
8199 -- but is in fact global to it, and must be preserved.
8201 -- It is also possible for Current_Instantiated_Parent to be
8202 -- defined, and for this not to be a nested generic, namely if
8203 -- the unit is loaded through Rtsfind. In that case, the entity of
8204 -- New_N is only a link to the associated node, and not a defining
8205 -- occurrence.
8207 -- The entities for parent units in the defining_program_unit of a
8208 -- generic child unit are established when the context of the unit
8209 -- is first analyzed, before the generic copy is made. They are
8210 -- preserved in the copy for use in e.g. ASIS queries.
8212 Ent := Entity (New_N);
8214 if No (Current_Instantiated_Parent.Gen_Id) then
8215 if No (Ent)
8216 or else Nkind (Ent) /= N_Defining_Identifier
8217 or else not In_Defining_Unit_Name (N)
8218 then
8219 Set_Associated_Node (New_N, Empty);
8220 end if;
8222 elsif No (Ent)
8223 or else Nkind (Ent) not in N_Entity
8224 or else No (Scope (Ent))
8225 or else
8226 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8227 and then not Is_Child_Unit (Ent))
8228 or else
8229 (Scope_Depth_Set (Scope (Ent))
8230 and then
8231 Scope_Depth (Scope (Ent)) >
8232 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8233 and then
8234 Get_Source_Unit (Ent) =
8235 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8236 then
8237 Set_Associated_Node (New_N, Empty);
8238 end if;
8240 -- Case of instantiating identifier or some other name or operator
8242 else
8243 -- If the associated node is still defined, the entity in it
8244 -- is global, and must be copied to the instance. If this copy
8245 -- is being made for a body to inline, it is applied to an
8246 -- instantiated tree, and the entity is already present and
8247 -- must be also preserved.
8249 declare
8250 Assoc : constant Node_Id := Get_Associated_Node (N);
8252 begin
8253 if Present (Assoc) then
8254 if Nkind (Assoc) = Nkind (N) then
8255 Set_Entity (New_N, Entity (Assoc));
8256 Check_Private_View (N);
8258 -- The node is a reference to a global type and acts as the
8259 -- subtype mark of a qualified expression created in order
8260 -- to aid resolution of accidental overloading in instances.
8261 -- Since N is a reference to a type, the Associated_Node of
8262 -- N denotes an entity rather than another identifier. See
8263 -- Qualify_Universal_Operands for details.
8265 elsif Nkind (N) = N_Identifier
8266 and then Nkind (Parent (N)) = N_Qualified_Expression
8267 and then Subtype_Mark (Parent (N)) = N
8268 and then Is_Qualified_Universal_Literal (Parent (N))
8269 then
8270 Set_Entity (New_N, Assoc);
8272 -- Cope with the rewriting into expanded name that may have
8273 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8274 -- generic renaming declarations.
8276 elsif Nkind (Assoc) = N_Expanded_Name then
8277 Rewrite (N, New_Copy_Tree (Assoc));
8278 Set_Associated_Node (N, Assoc);
8279 return Copy_Generic_Node (N, Parent_Id, Instantiating);
8281 -- The name in the call may be a selected component if the
8282 -- call has not been analyzed yet, as may be the case for
8283 -- pre/post conditions in a generic unit.
8285 elsif Nkind (Assoc) = N_Function_Call
8286 and then Is_Entity_Name (Name (Assoc))
8287 then
8288 Set_Entity (New_N, Entity (Name (Assoc)));
8289 Check_Private_View (N);
8291 elsif Nkind (Assoc) in N_Entity
8292 and then (Expander_Active
8293 or else (GNATprove_Mode
8294 and then not In_Spec_Expression
8295 and then not Inside_A_Generic))
8296 then
8297 -- Inlining case: we are copying a tree that contains
8298 -- global entities, which are preserved in the copy to be
8299 -- used for subsequent inlining.
8301 null;
8303 else
8304 Set_Entity (New_N, Empty);
8305 end if;
8306 end if;
8307 end;
8308 end if;
8310 -- For expanded name, we must copy the Prefix and Selector_Name
8312 if Nkind (N) = N_Expanded_Name then
8313 Set_Prefix
8314 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8316 Set_Selector_Name (New_N,
8317 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8319 -- For operators, copy the operands
8321 elsif Nkind (N) in N_Op then
8322 if Nkind (N) in N_Binary_Op then
8323 Set_Left_Opnd (New_N,
8324 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8325 end if;
8327 Set_Right_Opnd (New_N,
8328 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8329 end if;
8331 -- Establish a link between an entity from the generic template and the
8332 -- corresponding entity in the generic copy to be analyzed.
8334 elsif Nkind (N) in N_Entity then
8335 if not Instantiating then
8336 Set_Associated_Entity (N, New_N);
8337 end if;
8339 -- Clear any existing link the copy may inherit from the replicated
8340 -- generic template entity.
8342 Set_Associated_Entity (New_N, Empty);
8344 -- Special casing for stubs
8346 elsif Nkind (N) in N_Body_Stub then
8348 -- In any case, we must copy the specification or defining
8349 -- identifier as appropriate.
8351 if Nkind (N) = N_Subprogram_Body_Stub then
8352 Set_Specification (New_N,
8353 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8355 else
8356 Set_Defining_Identifier (New_N,
8357 Copy_Generic_Node
8358 (Defining_Identifier (N), New_N, Instantiating));
8359 end if;
8361 -- If we are not instantiating, then this is where we load and
8362 -- analyze subunits, i.e. at the point where the stub occurs. A
8363 -- more permissive system might defer this analysis to the point
8364 -- of instantiation, but this seems too complicated for now.
8366 if not Instantiating then
8367 declare
8368 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8369 Subunit : Node_Id;
8370 Unum : Unit_Number_Type;
8371 New_Body : Node_Id;
8373 begin
8374 -- Make sure that, if it is a subunit of the main unit that is
8375 -- preprocessed and if -gnateG is specified, the preprocessed
8376 -- file will be written.
8378 Lib.Analysing_Subunit_Of_Main :=
8379 Lib.In_Extended_Main_Source_Unit (N);
8380 Unum :=
8381 Load_Unit
8382 (Load_Name => Subunit_Name,
8383 Required => False,
8384 Subunit => True,
8385 Error_Node => N);
8386 Lib.Analysing_Subunit_Of_Main := False;
8388 -- If the proper body is not found, a warning message will be
8389 -- emitted when analyzing the stub, or later at the point of
8390 -- instantiation. Here we just leave the stub as is.
8392 if Unum = No_Unit then
8393 Subunits_Missing := True;
8394 goto Subunit_Not_Found;
8395 end if;
8397 Subunit := Cunit (Unum);
8399 if Nkind (Unit (Subunit)) /= N_Subunit then
8400 Error_Msg_N
8401 ("found child unit instead of expected SEPARATE subunit",
8402 Subunit);
8403 Error_Msg_Sloc := Sloc (N);
8404 Error_Msg_N ("\to complete stub #", Subunit);
8405 goto Subunit_Not_Found;
8406 end if;
8408 -- We must create a generic copy of the subunit, in order to
8409 -- perform semantic analysis on it, and we must replace the
8410 -- stub in the original generic unit with the subunit, in order
8411 -- to preserve non-local references within.
8413 -- Only the proper body needs to be copied. Library_Unit and
8414 -- context clause are simply inherited by the generic copy.
8415 -- Note that the copy (which may be recursive if there are
8416 -- nested subunits) must be done first, before attaching it to
8417 -- the enclosing generic.
8419 New_Body :=
8420 Copy_Generic_Node
8421 (Proper_Body (Unit (Subunit)),
8422 Empty, Instantiating => False);
8424 -- Now place the original proper body in the original generic
8425 -- unit. This is a body, not a compilation unit.
8427 Rewrite (N, Proper_Body (Unit (Subunit)));
8428 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8429 Set_Was_Originally_Stub (N);
8431 -- Finally replace the body of the subunit with its copy, and
8432 -- make this new subunit into the library unit of the generic
8433 -- copy, which does not have stubs any longer.
8435 Set_Proper_Body (Unit (Subunit), New_Body);
8436 Set_Library_Unit (New_N, Subunit);
8437 Inherit_Context (Unit (Subunit), N);
8438 end;
8440 -- If we are instantiating, this must be an error case, since
8441 -- otherwise we would have replaced the stub node by the proper body
8442 -- that corresponds. So just ignore it in the copy (i.e. we have
8443 -- copied it, and that is good enough).
8445 else
8446 null;
8447 end if;
8449 <<Subunit_Not_Found>> null;
8451 -- If the node is a compilation unit, it is the subunit of a stub, which
8452 -- has been loaded already (see code below). In this case, the library
8453 -- unit field of N points to the parent unit (which is a compilation
8454 -- unit) and need not (and cannot) be copied.
8456 -- When the proper body of the stub is analyzed, the library_unit link
8457 -- is used to establish the proper context (see sem_ch10).
8459 -- The other fields of a compilation unit are copied as usual
8461 elsif Nkind (N) = N_Compilation_Unit then
8463 -- This code can only be executed when not instantiating, because in
8464 -- the copy made for an instantiation, the compilation unit node has
8465 -- disappeared at the point that a stub is replaced by its proper
8466 -- body.
8468 pragma Assert (not Instantiating);
8470 Set_Context_Items (New_N,
8471 Copy_Generic_List (Context_Items (N), New_N));
8473 Set_Unit (New_N,
8474 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8476 Set_First_Inlined_Subprogram (New_N,
8477 Copy_Generic_Node
8478 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8480 Set_Aux_Decls_Node
8481 (New_N,
8482 Copy_Generic_Node
8483 (Aux_Decls_Node (N), New_N, Instantiating => False));
8485 -- For an assignment node, the assignment is known to be semantically
8486 -- legal if we are instantiating the template. This avoids incorrect
8487 -- diagnostics in generated code.
8489 elsif Nkind (N) = N_Assignment_Statement then
8491 -- Copy name and expression fields in usual manner
8493 Set_Name (New_N,
8494 Copy_Generic_Node (Name (N), New_N, Instantiating));
8496 Set_Expression (New_N,
8497 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8499 if Instantiating then
8500 Set_Assignment_OK (Name (New_N), True);
8501 end if;
8503 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8504 if not Instantiating then
8505 Set_Associated_Node (N, New_N);
8507 else
8508 -- If, in the generic, the aggregate has a global composite type
8509 -- and, at the point of instantiation, the type has a private view
8510 -- then install the full view.
8512 declare
8513 Assoc : constant Node_Id := Get_Associated_Node (N);
8515 begin
8516 if Present (Assoc)
8517 and then Nkind (Assoc) = Nkind (N)
8518 and then Present (Etype (Assoc))
8519 and then Is_Private_Type (Etype (Assoc))
8520 then
8521 Switch_View (Etype (Assoc));
8522 end if;
8523 end;
8525 -- Moreover, for a full aggregate, if the type is a derived tagged
8526 -- type and has a global ancestor, then also restore the full view
8527 -- of this ancestor and do so up to the root type. Beware that the
8528 -- Ancestor_Type field is overloaded, so test that it's an entity.
8530 if Nkind (N) = N_Aggregate
8531 and then Present (Ancestor_Type (N))
8532 and then Nkind (Ancestor_Type (N)) in N_Entity
8533 then
8534 declare
8535 Root_Typ : constant Entity_Id :=
8536 Root_Type (Ancestor_Type (N));
8538 Typ : Entity_Id := Ancestor_Type (N);
8540 begin
8541 loop
8542 if Is_Private_Type (Typ) then
8543 Switch_View (Typ);
8544 end if;
8546 exit when Typ = Root_Typ;
8548 Typ := Etype (Typ);
8549 end loop;
8550 end;
8551 end if;
8552 end if;
8554 -- Do not copy the associated node, which points to the generic copy
8555 -- of the aggregate.
8557 if Nkind (N) = N_Aggregate then
8558 Set_Aggregate_Bounds
8559 (New_N,
8560 Node_Id (Copy_Generic_Descendant
8561 (Union_Id (Aggregate_Bounds (N)))));
8563 elsif Nkind (N) = N_Extension_Aggregate then
8564 Set_Ancestor_Part
8565 (New_N,
8566 Node_Id (Copy_Generic_Descendant
8567 (Union_Id (Ancestor_Part (N)))));
8569 else
8570 pragma Assert (False);
8571 end if;
8573 Set_Expressions
8574 (New_N,
8575 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8576 Set_Component_Associations
8577 (New_N,
8578 List_Id (Copy_Generic_Descendant
8579 (Union_Id (Component_Associations (N)))));
8580 Set_Etype
8581 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8583 -- Allocators do not have an identifier denoting the access type, so we
8584 -- must locate it through the expression to check whether the views are
8585 -- consistent.
8587 elsif Nkind (N) = N_Allocator
8588 and then Nkind (Expression (N)) = N_Qualified_Expression
8589 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8590 and then Instantiating
8591 then
8592 declare
8593 T : constant Node_Id :=
8594 Get_Associated_Node (Subtype_Mark (Expression (N)));
8595 Acc_T : Entity_Id;
8597 begin
8598 if Present (T) then
8600 -- Retrieve the allocator node in the generic copy
8602 Acc_T := Etype (Parent (Parent (T)));
8604 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8605 Switch_View (Acc_T);
8606 end if;
8607 end if;
8609 Copy_Descendants;
8610 end;
8612 -- Loop parameter specifications do not have an identifier denoting the
8613 -- index type, so we must locate it through the defining identifier to
8614 -- check whether the views are consistent.
8616 elsif Nkind (N) = N_Loop_Parameter_Specification
8617 and then Instantiating
8618 then
8619 declare
8620 Id : constant Entity_Id :=
8621 Get_Associated_Entity (Defining_Identifier (N));
8623 Index_T : Entity_Id;
8625 begin
8626 if Present (Id) and then Present (Etype (Id)) then
8627 Index_T := First_Subtype (Etype (Id));
8629 if Present (Index_T) and then Is_Private_Type (Index_T) then
8630 Switch_View (Index_T);
8631 end if;
8632 end if;
8634 Copy_Descendants;
8635 end;
8637 -- For a proper body, we must catch the case of a proper body that
8638 -- replaces a stub. This represents the point at which a separate
8639 -- compilation unit, and hence template file, may be referenced, so we
8640 -- must make a new source instantiation entry for the template of the
8641 -- subunit, and ensure that all nodes in the subunit are adjusted using
8642 -- this new source instantiation entry.
8644 elsif Nkind (N) in N_Proper_Body then
8645 declare
8646 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8647 begin
8648 if Instantiating and then Was_Originally_Stub (N) then
8649 Create_Instantiation_Source
8650 (Instantiation_Node,
8651 Defining_Entity (N),
8652 S_Adjustment);
8654 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8655 end if;
8657 -- Now copy the fields of the proper body, using the new
8658 -- adjustment factor if one was needed as per test above.
8660 Copy_Descendants;
8662 -- Restore the original adjustment factor
8664 S_Adjustment := Save_Adjustment;
8665 end;
8667 elsif Nkind (N) = N_Pragma and then Instantiating then
8669 -- Do not copy Comment or Ident pragmas their content is relevant to
8670 -- the generic unit, not to the instantiating unit.
8672 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8673 New_N := Make_Null_Statement (Sloc (N));
8675 -- Do not copy pragmas generated from aspects because the pragmas do
8676 -- not carry any semantic information, plus they will be regenerated
8677 -- in the instance.
8679 -- However, generating C we need to copy them since postconditions
8680 -- are inlined by the front end, and the front-end inlining machinery
8681 -- relies on this routine to perform inlining.
8683 elsif From_Aspect_Specification (N)
8684 and then not Modify_Tree_For_C
8685 then
8686 New_N := Make_Null_Statement (Sloc (N));
8688 else
8689 Copy_Descendants;
8690 end if;
8692 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8694 -- No descendant fields need traversing
8696 null;
8698 elsif Nkind (N) = N_String_Literal
8699 and then Present (Etype (N))
8700 and then Instantiating
8701 then
8702 -- If the string is declared in an outer scope, the string_literal
8703 -- subtype created for it may have the wrong scope. Force reanalysis
8704 -- of the constant to generate a new itype in the proper context.
8706 Set_Etype (New_N, Empty);
8707 Set_Analyzed (New_N, False);
8709 -- For the remaining nodes, copy their descendants recursively
8711 else
8712 Copy_Descendants;
8714 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8715 Set_Generic_Parent (Specification (New_N), N);
8717 -- Should preserve Corresponding_Spec??? (12.3(14))
8718 end if;
8719 end if;
8721 -- Propagate dimensions if present, so that they are reflected in the
8722 -- instance.
8724 if Nkind (N) in N_Has_Etype
8725 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8726 and then Present (Etype (N))
8727 and then Is_Floating_Point_Type (Etype (N))
8728 and then Has_Dimension_System (Etype (N))
8729 then
8730 Copy_Dimensions (N, New_N);
8731 end if;
8733 return New_N;
8734 end Copy_Generic_Node;
8736 ----------------------------
8737 -- Denotes_Formal_Package --
8738 ----------------------------
8740 function Denotes_Formal_Package
8741 (Pack : Entity_Id;
8742 On_Exit : Boolean := False;
8743 Instance : Entity_Id := Empty) return Boolean
8745 Par : Entity_Id;
8746 Scop : constant Entity_Id := Scope (Pack);
8747 E : Entity_Id;
8749 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8750 -- The package in question may be an actual for a previous formal
8751 -- package P of the current instance, so examine its actuals as well.
8752 -- This must be recursive over other formal packages.
8754 ----------------------------------
8755 -- Is_Actual_Of_Previous_Formal --
8756 ----------------------------------
8758 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8759 E1 : Entity_Id;
8761 begin
8762 E1 := First_Entity (P);
8763 while Present (E1) and then E1 /= Instance loop
8764 if Ekind (E1) = E_Package
8765 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8766 then
8767 if Renamed_Entity (E1) = Pack then
8768 return True;
8770 elsif E1 = P or else Renamed_Entity (E1) = P then
8771 return False;
8773 elsif Is_Actual_Of_Previous_Formal (E1) then
8774 return True;
8775 end if;
8776 end if;
8778 Next_Entity (E1);
8779 end loop;
8781 return False;
8782 end Is_Actual_Of_Previous_Formal;
8784 -- Start of processing for Denotes_Formal_Package
8786 begin
8787 if On_Exit then
8788 Par :=
8789 Instance_Envs.Table
8790 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8791 else
8792 Par := Current_Instantiated_Parent.Act_Id;
8793 end if;
8795 if Ekind (Scop) = E_Generic_Package
8796 or else Nkind (Unit_Declaration_Node (Scop)) =
8797 N_Generic_Subprogram_Declaration
8798 then
8799 return True;
8801 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8802 N_Formal_Package_Declaration
8803 then
8804 return True;
8806 elsif No (Par) then
8807 return False;
8809 else
8810 -- Check whether this package is associated with a formal package of
8811 -- the enclosing instantiation. Iterate over the list of renamings.
8813 E := First_Entity (Par);
8814 while Present (E) loop
8815 if Ekind (E) /= E_Package
8816 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8817 then
8818 null;
8820 elsif Renamed_Entity (E) = Par then
8821 return False;
8823 elsif Renamed_Entity (E) = Pack then
8824 return True;
8826 elsif Is_Actual_Of_Previous_Formal (E) then
8827 return True;
8829 end if;
8831 Next_Entity (E);
8832 end loop;
8834 return False;
8835 end if;
8836 end Denotes_Formal_Package;
8838 -----------------
8839 -- End_Generic --
8840 -----------------
8842 procedure End_Generic is
8843 begin
8844 -- ??? More things could be factored out in this routine. Should
8845 -- probably be done at a later stage.
8847 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8848 Generic_Flags.Decrement_Last;
8850 Expander_Mode_Restore;
8851 end End_Generic;
8853 -------------
8854 -- Earlier --
8855 -------------
8857 function Earlier (N1, N2 : Node_Id) return Boolean is
8858 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8859 -- Find distance from given node to enclosing compilation unit
8861 ----------------
8862 -- Find_Depth --
8863 ----------------
8865 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8866 begin
8867 while Present (P)
8868 and then Nkind (P) /= N_Compilation_Unit
8869 loop
8870 P := True_Parent (P);
8871 D := D + 1;
8872 end loop;
8873 end Find_Depth;
8875 -- Local declarations
8877 D1 : Integer := 0;
8878 D2 : Integer := 0;
8879 P1 : Node_Id := N1;
8880 P2 : Node_Id := N2;
8881 T1 : Source_Ptr;
8882 T2 : Source_Ptr;
8884 -- Start of processing for Earlier
8886 begin
8887 Find_Depth (P1, D1);
8888 Find_Depth (P2, D2);
8890 if P1 /= P2 then
8891 return False;
8892 else
8893 P1 := N1;
8894 P2 := N2;
8895 end if;
8897 while D1 > D2 loop
8898 P1 := True_Parent (P1);
8899 D1 := D1 - 1;
8900 end loop;
8902 while D2 > D1 loop
8903 P2 := True_Parent (P2);
8904 D2 := D2 - 1;
8905 end loop;
8907 -- At this point P1 and P2 are at the same distance from the root.
8908 -- We examine their parents until we find a common declarative list.
8909 -- If we reach the root, N1 and N2 do not descend from the same
8910 -- declarative list (e.g. one is nested in the declarative part and
8911 -- the other is in a block in the statement part) and the earlier
8912 -- one is already frozen.
8914 while not Is_List_Member (P1)
8915 or else not Is_List_Member (P2)
8916 or else not In_Same_List (P1, P2)
8917 loop
8918 P1 := True_Parent (P1);
8919 P2 := True_Parent (P2);
8921 if Nkind (Parent (P1)) = N_Subunit then
8922 P1 := Corresponding_Stub (Parent (P1));
8923 end if;
8925 if Nkind (Parent (P2)) = N_Subunit then
8926 P2 := Corresponding_Stub (Parent (P2));
8927 end if;
8929 if P1 = P2 then
8930 return False;
8931 end if;
8932 end loop;
8934 -- Expanded code usually shares the source location of the original
8935 -- construct it was generated for. This however may not necessarily
8936 -- reflect the true location of the code within the tree.
8938 -- Before comparing the slocs of the two nodes, make sure that we are
8939 -- working with correct source locations. Assume that P1 is to the left
8940 -- of P2. If either one does not come from source, traverse the common
8941 -- list heading towards the other node and locate the first source
8942 -- statement.
8944 -- P1 P2
8945 -- ----+===+===+--------------+===+===+----
8946 -- expanded code expanded code
8948 if not Comes_From_Source (P1) then
8949 while Present (P1) loop
8951 -- Neither P2 nor a source statement were located during the
8952 -- search. If we reach the end of the list, then P1 does not
8953 -- occur earlier than P2.
8955 -- ---->
8956 -- start --- P2 ----- P1 --- end
8958 if No (Next (P1)) then
8959 return False;
8961 -- We encounter P2 while going to the right of the list. This
8962 -- means that P1 does indeed appear earlier.
8964 -- ---->
8965 -- start --- P1 ===== P2 --- end
8966 -- expanded code in between
8968 elsif P1 = P2 then
8969 return True;
8971 -- No need to look any further since we have located a source
8972 -- statement.
8974 elsif Comes_From_Source (P1) then
8975 exit;
8976 end if;
8978 -- Keep going right
8980 Next (P1);
8981 end loop;
8982 end if;
8984 if not Comes_From_Source (P2) then
8985 while Present (P2) loop
8987 -- Neither P1 nor a source statement were located during the
8988 -- search. If we reach the start of the list, then P1 does not
8989 -- occur earlier than P2.
8991 -- <----
8992 -- start --- P2 --- P1 --- end
8994 if No (Prev (P2)) then
8995 return False;
8997 -- We encounter P1 while going to the left of the list. This
8998 -- means that P1 does indeed appear earlier.
9000 -- <----
9001 -- start --- P1 ===== P2 --- end
9002 -- expanded code in between
9004 elsif P2 = P1 then
9005 return True;
9007 -- No need to look any further since we have located a source
9008 -- statement.
9010 elsif Comes_From_Source (P2) then
9011 exit;
9012 end if;
9014 -- Keep going left
9016 Prev (P2);
9017 end loop;
9018 end if;
9020 -- At this point either both nodes came from source or we approximated
9021 -- their source locations through neighboring source statements.
9023 T1 := Top_Level_Location (Sloc (P1));
9024 T2 := Top_Level_Location (Sloc (P2));
9026 -- When two nodes come from the same instance, they have identical top
9027 -- level locations. To determine proper relation within the tree, check
9028 -- their locations within the template.
9030 if T1 = T2 then
9031 return Sloc (P1) < Sloc (P2);
9033 -- The two nodes either come from unrelated instances or do not come
9034 -- from instantiated code at all.
9036 else
9037 return T1 < T2;
9038 end if;
9039 end Earlier;
9041 ----------------------
9042 -- Find_Actual_Type --
9043 ----------------------
9045 function Find_Actual_Type
9046 (Typ : Entity_Id;
9047 Gen_Type : Entity_Id) return Entity_Id
9049 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9050 T : Entity_Id;
9052 begin
9053 -- Special processing only applies to child units
9055 if not Is_Child_Unit (Gen_Scope) then
9056 return Get_Instance_Of (Typ);
9058 -- If designated or component type is itself a formal of the child unit,
9059 -- its instance is available.
9061 elsif Scope (Typ) = Gen_Scope then
9062 return Get_Instance_Of (Typ);
9064 -- If the array or access type is not declared in the parent unit,
9065 -- no special processing needed.
9067 elsif not Is_Generic_Type (Typ)
9068 and then Scope (Gen_Scope) /= Scope (Typ)
9069 then
9070 return Get_Instance_Of (Typ);
9072 -- Otherwise, retrieve designated or component type by visibility
9074 else
9075 T := Current_Entity (Typ);
9076 while Present (T) loop
9077 if In_Open_Scopes (Scope (T)) then
9078 return T;
9079 elsif Is_Generic_Actual_Type (T) then
9080 return T;
9081 end if;
9083 T := Homonym (T);
9084 end loop;
9086 return Typ;
9087 end if;
9088 end Find_Actual_Type;
9090 -----------------------------
9091 -- Freeze_Package_Instance --
9092 -----------------------------
9094 procedure Freeze_Package_Instance
9095 (N : Node_Id;
9096 Gen_Body : Node_Id;
9097 Gen_Decl : Node_Id;
9098 Act_Id : Entity_Id)
9100 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9101 -- Check if the generic definition and the instantiation come from
9102 -- a common scope, in which case the instance must be frozen after
9103 -- the generic body.
9105 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9106 -- If the instance is nested inside a generic unit, the Sloc of the
9107 -- instance indicates the place of the original definition, not the
9108 -- point of the current enclosing instance. Pending a better usage of
9109 -- Slocs to indicate instantiation places, we determine the place of
9110 -- origin of a node by finding the maximum sloc of any ancestor node.
9112 -- Why is this not equivalent to Top_Level_Location ???
9114 -------------------
9115 -- In_Same_Scope --
9116 -------------------
9118 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9119 Act_Scop : Entity_Id := Scope (Act_Id);
9120 Gen_Scop : Entity_Id := Scope (Gen_Id);
9122 begin
9123 while Act_Scop /= Standard_Standard
9124 and then Gen_Scop /= Standard_Standard
9125 loop
9126 if Act_Scop = Gen_Scop then
9127 return True;
9128 end if;
9130 Act_Scop := Scope (Act_Scop);
9131 Gen_Scop := Scope (Gen_Scop);
9132 end loop;
9134 return False;
9135 end In_Same_Scope;
9137 ---------------
9138 -- True_Sloc --
9139 ---------------
9141 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9142 N1 : Node_Id;
9143 Res : Source_Ptr;
9145 begin
9146 Res := Sloc (N);
9147 N1 := N;
9148 while Present (N1) and then N1 /= Act_Unit loop
9149 if Sloc (N1) > Res then
9150 Res := Sloc (N1);
9151 end if;
9153 N1 := Parent (N1);
9154 end loop;
9156 return Res;
9157 end True_Sloc;
9159 -- Local variables
9161 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9162 Par_Id : constant Entity_Id := Scope (Gen_Id);
9163 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9164 Gen_Unit : constant Node_Id :=
9165 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9167 Body_Unit : Node_Id;
9168 F_Node : Node_Id;
9169 Must_Delay : Boolean;
9170 Orig_Body : Node_Id;
9172 -- Start of processing for Freeze_Package_Instance
9174 begin
9175 -- If the body is a subunit, the freeze point is the corresponding stub
9176 -- in the current compilation, not the subunit itself.
9178 if Nkind (Parent (Gen_Body)) = N_Subunit then
9179 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9180 else
9181 Orig_Body := Gen_Body;
9182 end if;
9184 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9186 -- If the instantiation and the generic definition appear in the same
9187 -- package declaration, this is an early instantiation. If they appear
9188 -- in the same declarative part, it is an early instantiation only if
9189 -- the generic body appears textually later, and the generic body is
9190 -- also in the main unit.
9192 -- If instance is nested within a subprogram, and the generic body
9193 -- is not, the instance is delayed because the enclosing body is. If
9194 -- instance and body are within the same scope, or the same subprogram
9195 -- body, indicate explicitly that the instance is delayed.
9197 Must_Delay :=
9198 (Gen_Unit = Act_Unit
9199 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9200 | N_Package_Declaration
9201 or else (Gen_Unit = Body_Unit
9202 and then
9203 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9204 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9205 and then In_Same_Scope (Gen_Id, Act_Id));
9207 -- If this is an early instantiation, the freeze node is placed after
9208 -- the generic body. Otherwise, if the generic appears in an instance,
9209 -- we cannot freeze the current instance until the outer one is frozen.
9210 -- This is only relevant if the current instance is nested within some
9211 -- inner scope not itself within the outer instance. If this scope is
9212 -- a package body in the same declarative part as the outer instance,
9213 -- then that body needs to be frozen after the outer instance. Finally,
9214 -- if no delay is needed, we place the freeze node at the end of the
9215 -- current declarative part.
9217 if No (Freeze_Node (Act_Id))
9218 or else not Is_List_Member (Freeze_Node (Act_Id))
9219 then
9220 Ensure_Freeze_Node (Act_Id);
9221 F_Node := Freeze_Node (Act_Id);
9223 if Must_Delay then
9224 Insert_After (Orig_Body, F_Node);
9226 elsif Is_Generic_Instance (Par_Id)
9227 and then Present (Freeze_Node (Par_Id))
9228 and then Scope (Act_Id) /= Par_Id
9229 then
9230 -- Freeze instance of inner generic after instance of enclosing
9231 -- generic.
9233 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9235 -- Handle the following case:
9237 -- package Parent_Inst is new ...
9238 -- freeze Parent_Inst []
9240 -- procedure P ... -- this body freezes Parent_Inst
9242 -- package Inst is new ...
9244 -- In this particular scenario, the freeze node for Inst must
9245 -- be inserted in the same manner as that of Parent_Inst,
9246 -- before the next source body or at the end of the declarative
9247 -- list (body not available). If body P did not exist and
9248 -- Parent_Inst was frozen after Inst, either by a body
9249 -- following Inst or at the end of the declarative region,
9250 -- the freeze node for Inst must be inserted after that of
9251 -- Parent_Inst. This relation is established by comparing
9252 -- the Slocs of Parent_Inst freeze node and Inst.
9253 -- We examine the parents of the enclosing lists to handle
9254 -- the case where the parent instance is in the visible part
9255 -- of a package declaration, and the inner instance is in
9256 -- the corresponding private part.
9258 if Parent (List_Containing (Freeze_Node (Par_Id)))
9259 = Parent (List_Containing (N))
9260 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9261 then
9262 Insert_Freeze_Node_For_Instance (N, F_Node);
9263 else
9264 Insert_After (Freeze_Node (Par_Id), F_Node);
9265 end if;
9267 -- Freeze package enclosing instance of inner generic after
9268 -- instance of enclosing generic.
9270 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9271 and then In_Same_Declarative_Part
9272 (Parent (Freeze_Node (Par_Id)), Parent (N))
9273 then
9274 declare
9275 Enclosing : Entity_Id;
9277 begin
9278 Enclosing := Corresponding_Spec (Parent (N));
9280 if No (Enclosing) then
9281 Enclosing := Defining_Entity (Parent (N));
9282 end if;
9284 Insert_Freeze_Node_For_Instance (N, F_Node);
9285 Ensure_Freeze_Node (Enclosing);
9287 if not Is_List_Member (Freeze_Node (Enclosing)) then
9289 -- The enclosing context is a subunit, insert the freeze
9290 -- node after the stub.
9292 if Nkind (Parent (Parent (N))) = N_Subunit then
9293 Insert_Freeze_Node_For_Instance
9294 (Corresponding_Stub (Parent (Parent (N))),
9295 Freeze_Node (Enclosing));
9297 -- The enclosing context is a package with a stub body
9298 -- which has already been replaced by the real body.
9299 -- Insert the freeze node after the actual body.
9301 elsif Ekind (Enclosing) = E_Package
9302 and then Present (Body_Entity (Enclosing))
9303 and then Was_Originally_Stub
9304 (Parent (Body_Entity (Enclosing)))
9305 then
9306 Insert_Freeze_Node_For_Instance
9307 (Parent (Body_Entity (Enclosing)),
9308 Freeze_Node (Enclosing));
9310 -- The parent instance has been frozen before the body of
9311 -- the enclosing package, insert the freeze node after
9312 -- the body.
9314 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9315 and then
9316 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9317 then
9318 Insert_Freeze_Node_For_Instance
9319 (Parent (N), Freeze_Node (Enclosing));
9321 else
9322 Insert_After
9323 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9324 end if;
9325 end if;
9326 end;
9328 else
9329 Insert_Freeze_Node_For_Instance (N, F_Node);
9330 end if;
9332 else
9333 Insert_Freeze_Node_For_Instance (N, F_Node);
9334 end if;
9335 end if;
9336 end Freeze_Package_Instance;
9338 --------------------------------
9339 -- Freeze_Subprogram_Instance --
9340 --------------------------------
9342 procedure Freeze_Subprogram_Instance
9343 (N : Node_Id;
9344 Gen_Body : Node_Id;
9345 Pack_Id : Entity_Id)
9347 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9348 -- Find innermost package body that encloses the given node, and which
9349 -- is not a compilation unit. Freeze nodes for the instance, or for its
9350 -- enclosing body, may be inserted after the enclosing_body of the
9351 -- generic unit. Used to determine proper placement of freeze node for
9352 -- both package and subprogram instances.
9354 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9355 -- Find entity for given package body, and locate or create a freeze
9356 -- node for it.
9358 ----------------------------
9359 -- Enclosing_Package_Body --
9360 ----------------------------
9362 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9363 P : Node_Id;
9365 begin
9366 P := Parent (N);
9367 while Present (P)
9368 and then Nkind (Parent (P)) /= N_Compilation_Unit
9369 loop
9370 if Nkind (P) = N_Package_Body then
9371 if Nkind (Parent (P)) = N_Subunit then
9372 return Corresponding_Stub (Parent (P));
9373 else
9374 return P;
9375 end if;
9376 end if;
9378 P := True_Parent (P);
9379 end loop;
9381 return Empty;
9382 end Enclosing_Package_Body;
9384 -------------------------
9385 -- Package_Freeze_Node --
9386 -------------------------
9388 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9389 Id : Entity_Id;
9391 begin
9392 if Nkind (B) = N_Package_Body then
9393 Id := Corresponding_Spec (B);
9394 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9395 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9396 end if;
9398 Ensure_Freeze_Node (Id);
9399 return Freeze_Node (Id);
9400 end Package_Freeze_Node;
9402 -- Local variables
9404 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9405 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9406 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9408 Enc_G_F : Node_Id;
9409 F_Node : Node_Id;
9411 -- Start of processing for Freeze_Subprogram_Instance
9413 begin
9414 -- If the instance and the generic body appear within the same unit, and
9415 -- the instance precedes the generic, the freeze node for the instance
9416 -- must appear after that of the generic. If the generic is nested
9417 -- within another instance I2, then current instance must be frozen
9418 -- after I2. In both cases, the freeze nodes are those of enclosing
9419 -- packages. Otherwise, the freeze node is placed at the end of the
9420 -- current declarative part.
9422 Ensure_Freeze_Node (Pack_Id);
9423 F_Node := Freeze_Node (Pack_Id);
9425 if Is_Generic_Instance (Par_Id)
9426 and then Present (Freeze_Node (Par_Id))
9427 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9428 then
9429 -- The parent was a premature instantiation. Insert freeze node at
9430 -- the end the current declarative part.
9432 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9433 Insert_Freeze_Node_For_Instance (N, F_Node);
9435 -- Handle the following case:
9437 -- package Parent_Inst is new ...
9438 -- freeze Parent_Inst []
9440 -- procedure P ... -- this body freezes Parent_Inst
9442 -- procedure Inst is new ...
9444 -- In this particular scenario, the freeze node for Inst must be
9445 -- inserted in the same manner as that of Parent_Inst - before the
9446 -- next source body or at the end of the declarative list (body not
9447 -- available). If body P did not exist and Parent_Inst was frozen
9448 -- after Inst, either by a body following Inst or at the end of the
9449 -- declarative region, the freeze node for Inst must be inserted
9450 -- after that of Parent_Inst. This relation is established by
9451 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9453 elsif In_Same_List (Freeze_Node (Par_Id), N)
9454 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9455 then
9456 Insert_Freeze_Node_For_Instance (N, F_Node);
9458 else
9459 Insert_After (Freeze_Node (Par_Id), F_Node);
9460 end if;
9462 -- The body enclosing the instance should be frozen after the body that
9463 -- includes the generic, because the body of the instance may make
9464 -- references to entities therein. If the two are not in the same
9465 -- declarative part, or if the one enclosing the instance is frozen
9466 -- already, freeze the instance at the end of the current declarative
9467 -- part.
9469 elsif Is_Generic_Instance (Par_Id)
9470 and then Present (Freeze_Node (Par_Id))
9471 and then Present (Enc_N)
9472 then
9473 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9474 then
9475 -- The enclosing package may contain several instances. Rather
9476 -- than computing the earliest point at which to insert its freeze
9477 -- node, we place it at the end of the declarative part of the
9478 -- parent of the generic.
9480 Insert_Freeze_Node_For_Instance
9481 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9482 end if;
9484 Insert_Freeze_Node_For_Instance (N, F_Node);
9486 elsif Present (Enc_G)
9487 and then Present (Enc_N)
9488 and then Enc_G /= Enc_N
9489 and then Earlier (N, Gen_Body)
9490 then
9491 -- Freeze package that encloses instance, and place node after the
9492 -- package that encloses generic. If enclosing package is already
9493 -- frozen we have to assume it is at the proper place. This may be a
9494 -- potential ABE that requires dynamic checking. Do not add a freeze
9495 -- node if the package that encloses the generic is inside the body
9496 -- that encloses the instance, because the freeze node would be in
9497 -- the wrong scope. Additional contortions needed if the bodies are
9498 -- within a subunit.
9500 declare
9501 Enclosing_Body : Node_Id;
9503 begin
9504 if Nkind (Enc_N) = N_Package_Body_Stub then
9505 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9506 else
9507 Enclosing_Body := Enc_N;
9508 end if;
9510 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9511 Insert_Freeze_Node_For_Instance
9512 (Enc_G, Package_Freeze_Node (Enc_N));
9513 end if;
9514 end;
9516 -- Freeze enclosing subunit before instance
9518 Enc_G_F := Package_Freeze_Node (Enc_G);
9520 if not Is_List_Member (Enc_G_F) then
9521 Insert_After (Enc_G, Enc_G_F);
9522 end if;
9524 Insert_Freeze_Node_For_Instance (N, F_Node);
9526 else
9527 -- If none of the above, insert freeze node at the end of the current
9528 -- declarative part.
9530 Insert_Freeze_Node_For_Instance (N, F_Node);
9531 end if;
9532 end Freeze_Subprogram_Instance;
9534 ----------------
9535 -- Get_Gen_Id --
9536 ----------------
9538 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9539 begin
9540 return Generic_Renamings.Table (E).Gen_Id;
9541 end Get_Gen_Id;
9543 ---------------------
9544 -- Get_Instance_Of --
9545 ---------------------
9547 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9548 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9550 begin
9551 if Res /= Assoc_Null then
9552 return Generic_Renamings.Table (Res).Act_Id;
9554 else
9555 -- On exit, entity is not instantiated: not a generic parameter, or
9556 -- else parameter of an inner generic unit.
9558 return A;
9559 end if;
9560 end Get_Instance_Of;
9562 ---------------------------------
9563 -- Get_Unit_Instantiation_Node --
9564 ---------------------------------
9566 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9567 Decl : Node_Id := Unit_Declaration_Node (A);
9568 Inst : Node_Id;
9570 begin
9571 -- If the Package_Instantiation attribute has been set on the package
9572 -- entity, then use it directly when it (or its Original_Node) refers
9573 -- to an N_Package_Instantiation node. In principle it should be
9574 -- possible to have this field set in all cases, which should be
9575 -- investigated, and would allow this function to be significantly
9576 -- simplified. ???
9578 Inst := Package_Instantiation (A);
9580 if Present (Inst) then
9581 if Nkind (Inst) = N_Package_Instantiation then
9582 return Inst;
9584 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9585 return Original_Node (Inst);
9586 end if;
9587 end if;
9589 -- If the instantiation is a compilation unit that does not need body
9590 -- then the instantiation node has been rewritten as a package
9591 -- declaration for the instance, and we return the original node.
9593 -- If it is a compilation unit and the instance node has not been
9594 -- rewritten, then it is still the unit of the compilation. Finally, if
9595 -- a body is present, this is a parent of the main unit whose body has
9596 -- been compiled for inlining purposes, and the instantiation node has
9597 -- been rewritten with the instance body.
9599 -- Otherwise the instantiation node appears after the declaration. If
9600 -- the entity is a formal package, the declaration may have been
9601 -- rewritten as a generic declaration (in the case of a formal with box)
9602 -- or left as a formal package declaration if it has actuals, and is
9603 -- found with a forward search.
9605 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9606 if Nkind (Decl) = N_Package_Declaration
9607 and then Present (Corresponding_Body (Decl))
9608 then
9609 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9610 end if;
9612 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9613 return Original_Node (Decl);
9614 else
9615 return Unit (Parent (Decl));
9616 end if;
9618 elsif Nkind (Decl) = N_Package_Declaration
9619 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9620 then
9621 return Original_Node (Decl);
9623 else
9624 Inst := Next (Decl);
9625 while Nkind (Inst) not in N_Formal_Package_Declaration
9626 | N_Function_Instantiation
9627 | N_Package_Instantiation
9628 | N_Procedure_Instantiation
9629 loop
9630 Next (Inst);
9631 end loop;
9633 return Inst;
9634 end if;
9635 end Get_Unit_Instantiation_Node;
9637 ------------------------
9638 -- Has_Been_Exchanged --
9639 ------------------------
9641 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9642 Next : Elmt_Id;
9644 begin
9645 Next := First_Elmt (Exchanged_Views);
9646 while Present (Next) loop
9647 if Full_View (Node (Next)) = E then
9648 return True;
9649 end if;
9651 Next_Elmt (Next);
9652 end loop;
9654 return False;
9655 end Has_Been_Exchanged;
9657 -------------------
9658 -- Has_Contracts --
9659 -------------------
9661 function Has_Contracts (Decl : Node_Id) return Boolean is
9662 A_List : constant List_Id := Aspect_Specifications (Decl);
9663 A_Spec : Node_Id;
9664 A_Id : Aspect_Id;
9665 begin
9666 if No (A_List) then
9667 return False;
9668 else
9669 A_Spec := First (A_List);
9670 while Present (A_Spec) loop
9671 A_Id := Get_Aspect_Id (A_Spec);
9672 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9673 return True;
9674 end if;
9676 Next (A_Spec);
9677 end loop;
9679 return False;
9680 end if;
9681 end Has_Contracts;
9683 ----------
9684 -- Hash --
9685 ----------
9687 function Hash (F : Entity_Id) return HTable_Range is
9688 begin
9689 return HTable_Range (F mod HTable_Size);
9690 end Hash;
9692 ------------------------
9693 -- Hide_Current_Scope --
9694 ------------------------
9696 procedure Hide_Current_Scope is
9697 C : constant Entity_Id := Current_Scope;
9698 E : Entity_Id;
9700 begin
9701 Set_Is_Hidden_Open_Scope (C);
9703 E := First_Entity (C);
9704 while Present (E) loop
9705 if Is_Immediately_Visible (E) then
9706 Set_Is_Immediately_Visible (E, False);
9707 Append_Elmt (E, Hidden_Entities);
9708 end if;
9710 Next_Entity (E);
9711 end loop;
9713 -- Make the scope name invisible as well. This is necessary, but might
9714 -- conflict with calls to Rtsfind later on, in case the scope is a
9715 -- predefined one. There is no clean solution to this problem, so for
9716 -- now we depend on the user not redefining Standard itself in one of
9717 -- the parent units.
9719 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9720 Set_Is_Immediately_Visible (C, False);
9721 Append_Elmt (C, Hidden_Entities);
9722 end if;
9724 end Hide_Current_Scope;
9726 --------------
9727 -- Init_Env --
9728 --------------
9730 procedure Init_Env is
9731 Saved : Instance_Env;
9733 begin
9734 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9735 Saved.Exchanged_Views := Exchanged_Views;
9736 Saved.Hidden_Entities := Hidden_Entities;
9737 Saved.Current_Sem_Unit := Current_Sem_Unit;
9738 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9739 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9741 -- Save configuration switches. These may be reset if the unit is a
9742 -- predefined unit, and the current mode is not Ada 2005.
9744 Saved.Switches := Save_Config_Switches;
9746 Instance_Envs.Append (Saved);
9748 Exchanged_Views := New_Elmt_List;
9749 Hidden_Entities := New_Elmt_List;
9751 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9752 -- this is set properly in Set_Instance_Env.
9754 Current_Instantiated_Parent :=
9755 (Current_Scope, Current_Scope, Assoc_Null);
9756 end Init_Env;
9758 ---------------------
9759 -- In_Main_Context --
9760 ---------------------
9762 function In_Main_Context (E : Entity_Id) return Boolean is
9763 Context : List_Id;
9764 Clause : Node_Id;
9765 Nam : Node_Id;
9767 begin
9768 if not Is_Compilation_Unit (E)
9769 or else Ekind (E) /= E_Package
9770 or else In_Private_Part (E)
9771 then
9772 return False;
9773 end if;
9775 Context := Context_Items (Cunit (Main_Unit));
9777 Clause := First (Context);
9778 while Present (Clause) loop
9779 if Nkind (Clause) = N_With_Clause then
9780 Nam := Name (Clause);
9782 -- If the current scope is part of the context of the main unit,
9783 -- analysis of the corresponding with_clause is not complete, and
9784 -- the entity is not set. We use the Chars field directly, which
9785 -- might produce false positives in rare cases, but guarantees
9786 -- that we produce all the instance bodies we will need.
9788 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9789 or else (Nkind (Nam) = N_Selected_Component
9790 and then Chars (Selector_Name (Nam)) = Chars (E))
9791 then
9792 return True;
9793 end if;
9794 end if;
9796 Next (Clause);
9797 end loop;
9799 return False;
9800 end In_Main_Context;
9802 ---------------------
9803 -- Inherit_Context --
9804 ---------------------
9806 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9807 Current_Context : List_Id;
9808 Current_Unit : Node_Id;
9809 Item : Node_Id;
9810 New_I : Node_Id;
9812 Clause : Node_Id;
9813 OK : Boolean;
9814 Lib_Unit : Node_Id;
9816 begin
9817 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9819 -- The inherited context is attached to the enclosing compilation
9820 -- unit. This is either the main unit, or the declaration for the
9821 -- main unit (in case the instantiation appears within the package
9822 -- declaration and the main unit is its body).
9824 Current_Unit := Parent (Inst);
9825 while Present (Current_Unit)
9826 and then Nkind (Current_Unit) /= N_Compilation_Unit
9827 loop
9828 Current_Unit := Parent (Current_Unit);
9829 end loop;
9831 Current_Context := Context_Items (Current_Unit);
9833 Item := First (Context_Items (Parent (Gen_Decl)));
9834 while Present (Item) loop
9835 if Nkind (Item) = N_With_Clause then
9836 Lib_Unit := Library_Unit (Item);
9838 -- Take care to prevent direct cyclic with's
9840 if Lib_Unit /= Current_Unit then
9842 -- Do not add a unit if it is already in the context
9844 Clause := First (Current_Context);
9845 OK := True;
9846 while Present (Clause) loop
9847 if Nkind (Clause) = N_With_Clause
9848 and then Library_Unit (Clause) = Lib_Unit
9849 then
9850 OK := False;
9851 exit;
9852 end if;
9854 Next (Clause);
9855 end loop;
9857 if OK then
9858 New_I := New_Copy (Item);
9859 Set_Implicit_With (New_I);
9861 Append (New_I, Current_Context);
9862 end if;
9863 end if;
9864 end if;
9866 Next (Item);
9867 end loop;
9868 end if;
9869 end Inherit_Context;
9871 ----------------
9872 -- Initialize --
9873 ----------------
9875 procedure Initialize is
9876 begin
9877 Generic_Renamings.Init;
9878 Instance_Envs.Init;
9879 Generic_Flags.Init;
9880 Generic_Renamings_HTable.Reset;
9881 Circularity_Detected := False;
9882 Exchanged_Views := No_Elist;
9883 Hidden_Entities := No_Elist;
9884 end Initialize;
9886 -------------------------------------
9887 -- Insert_Freeze_Node_For_Instance --
9888 -------------------------------------
9890 procedure Insert_Freeze_Node_For_Instance
9891 (N : Node_Id;
9892 F_Node : Node_Id)
9894 function Enclosing_Body (N : Node_Id) return Node_Id;
9895 -- Find enclosing package or subprogram body, if any. Freeze node may
9896 -- be placed at end of current declarative list if previous instance
9897 -- and current one have different enclosing bodies.
9899 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9900 -- Find the local instance, if any, that declares the generic that is
9901 -- being instantiated. If present, the freeze node for this instance
9902 -- must follow the freeze node for the previous instance.
9904 --------------------
9905 -- Enclosing_Body --
9906 --------------------
9908 function Enclosing_Body (N : Node_Id) return Node_Id is
9909 P : Node_Id;
9911 begin
9912 P := Parent (N);
9913 while Present (P)
9914 and then Nkind (Parent (P)) /= N_Compilation_Unit
9915 loop
9916 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9917 if Nkind (Parent (P)) = N_Subunit then
9918 return Corresponding_Stub (Parent (P));
9919 else
9920 return P;
9921 end if;
9922 end if;
9924 P := True_Parent (P);
9925 end loop;
9927 return Empty;
9928 end Enclosing_Body;
9930 -----------------------
9931 -- Previous_Instance --
9932 -----------------------
9934 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9935 S : Entity_Id;
9937 begin
9938 S := Scope (Gen);
9939 while Present (S) and then S /= Standard_Standard loop
9940 if Is_Generic_Instance (S)
9941 and then In_Same_Source_Unit (S, N)
9942 then
9943 return S;
9944 end if;
9946 S := Scope (S);
9947 end loop;
9949 return Empty;
9950 end Previous_Instance;
9952 -- Local variables
9954 Decl : Node_Id;
9955 Decls : List_Id;
9956 Inst : Entity_Id;
9957 Origin : Entity_Id;
9958 Par_Inst : Node_Id;
9959 Par_N : Node_Id;
9961 -- Start of processing for Insert_Freeze_Node_For_Instance
9963 begin
9964 -- Nothing to do if the freeze node has already been inserted
9966 if Is_List_Member (F_Node) then
9967 return;
9968 end if;
9970 Inst := Entity (F_Node);
9972 -- When processing a subprogram instantiation, utilize the actual
9973 -- subprogram instantiation rather than its package wrapper as it
9974 -- carries all the context information.
9976 if Is_Wrapper_Package (Inst) then
9977 Inst := Related_Instance (Inst);
9978 end if;
9980 Par_Inst := Parent (Inst);
9982 -- If this is a package instance, check whether the generic is declared
9983 -- in a previous instance and the current instance is not within the
9984 -- previous one.
9986 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9987 declare
9988 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9989 Par_I : constant Entity_Id :=
9990 Previous_Instance (Generic_Parent (Par_Inst));
9991 Scop : Entity_Id;
9993 begin
9994 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9995 Scop := Scope (Inst);
9997 -- If the current instance is within the one that contains
9998 -- the generic, the freeze node for the current one must
9999 -- appear in the current declarative part. Ditto, if the
10000 -- current instance is within another package instance or
10001 -- within a body that does not enclose the current instance.
10002 -- In these three cases the freeze node of the previous
10003 -- instance is not relevant.
10005 while Present (Scop) and then Scop /= Standard_Standard loop
10006 exit when Scop = Par_I
10007 or else
10008 (Is_Generic_Instance (Scop)
10009 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
10010 Scop := Scope (Scop);
10011 end loop;
10013 -- Previous instance encloses current instance
10015 if Scop = Par_I then
10016 null;
10018 -- If the next node is a source body we must freeze in the
10019 -- current scope as well.
10021 elsif Present (Next (N))
10022 and then Nkind (Next (N)) in N_Subprogram_Body
10023 | N_Package_Body
10024 and then Comes_From_Source (Next (N))
10025 then
10026 null;
10028 -- Current instance is within an unrelated instance
10030 elsif Is_Generic_Instance (Scop) then
10031 null;
10033 -- Current instance is within an unrelated body
10035 elsif Present (Enclosing_N)
10036 and then Enclosing_N /= Enclosing_Body (Par_I)
10037 then
10038 null;
10040 else
10041 Insert_After (Freeze_Node (Par_I), F_Node);
10042 return;
10043 end if;
10044 end if;
10045 end;
10046 end if;
10048 Decl := N;
10049 Decls := List_Containing (N);
10050 Par_N := Parent (Decls);
10051 Origin := Empty;
10053 -- Determine the proper freeze point of an instantiation
10055 if Is_Generic_Instance (Inst) then
10056 loop
10057 -- When the instantiation occurs in a package spec, append the
10058 -- freeze node to the private declarations (if any).
10060 if Nkind (Par_N) = N_Package_Specification
10061 and then Decls = Visible_Declarations (Par_N)
10062 and then not Is_Empty_List (Private_Declarations (Par_N))
10063 then
10064 Decls := Private_Declarations (Par_N);
10065 Decl := First (Decls);
10066 end if;
10068 -- We adhere to the general rule of a package or subprogram body
10069 -- causing freezing of anything before it in the same declarative
10070 -- region. In this respect, the proper freeze point of a package
10071 -- instantiation is before the first source body which follows, or
10072 -- before a stub. This ensures that entities from the instance are
10073 -- already frozen and therefore usable in source bodies.
10075 if Nkind (Par_N) /= N_Package_Declaration
10076 and then
10077 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10078 then
10079 while Present (Decl) loop
10080 if ((Nkind (Decl) in N_Unit_Body
10081 or else
10082 Nkind (Decl) in N_Body_Stub)
10083 and then Comes_From_Source (Decl))
10084 or else (Present (Origin)
10085 and then Nkind (Decl) in N_Generic_Instantiation
10086 and then Instance_Spec (Decl) /= Origin)
10087 then
10088 Set_Sloc (F_Node, Sloc (Decl));
10089 Insert_Before (Decl, F_Node);
10090 return;
10091 end if;
10093 Next (Decl);
10094 end loop;
10095 end if;
10097 -- When the instantiation occurs in a package spec and there is
10098 -- no source body which follows, and the package has a body but
10099 -- is delayed, then insert immediately before its freeze node.
10101 if Nkind (Par_N) = N_Package_Specification
10102 and then Present (Corresponding_Body (Parent (Par_N)))
10103 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10104 then
10105 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10106 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10107 return;
10109 -- When the instantiation occurs in a package spec and there is
10110 -- no source body which follows, not even of the package itself,
10111 -- then insert into the declaration list of the outer level, but
10112 -- do not jump over following instantiations in this list because
10113 -- they may have a body that has not materialized yet, see above.
10115 elsif Nkind (Par_N) = N_Package_Specification
10116 and then No (Corresponding_Body (Parent (Par_N)))
10117 and then Is_List_Member (Parent (Par_N))
10118 then
10119 Decl := Parent (Par_N);
10120 Decls := List_Containing (Decl);
10121 Par_N := Parent (Decls);
10122 Origin := Decl;
10124 -- In a package declaration, or if no source body which follows
10125 -- and at library level, then insert at end of list.
10127 else
10128 exit;
10129 end if;
10130 end loop;
10131 end if;
10133 -- Insert and adjust the Sloc of the freeze node
10135 Set_Sloc (F_Node, Sloc (Last (Decls)));
10136 Insert_After (Last (Decls), F_Node);
10137 end Insert_Freeze_Node_For_Instance;
10139 -----------------------------
10140 -- Install_Formal_Packages --
10141 -----------------------------
10143 procedure Install_Formal_Packages (Par : Entity_Id) is
10144 E : Entity_Id;
10145 Gen : Entity_Id;
10146 Gen_E : Entity_Id := Empty;
10148 begin
10149 E := First_Entity (Par);
10151 -- If we are installing an instance parent, locate the formal packages
10152 -- of its generic parent.
10154 if Is_Generic_Instance (Par) then
10155 Gen := Generic_Parent (Package_Specification (Par));
10156 Gen_E := First_Entity (Gen);
10157 end if;
10159 while Present (E) loop
10160 if Ekind (E) = E_Package
10161 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10162 then
10163 -- If this is the renaming for the parent instance, done
10165 if Renamed_Entity (E) = Par then
10166 exit;
10168 -- The visibility of a formal of an enclosing generic is already
10169 -- correct.
10171 elsif Denotes_Formal_Package (E) then
10172 null;
10174 elsif Present (Associated_Formal_Package (E)) then
10175 Check_Generic_Actuals (Renamed_Entity (E), True);
10176 Set_Is_Hidden (E, False);
10178 -- Find formal package in generic unit that corresponds to
10179 -- (instance of) formal package in instance.
10181 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10182 Next_Entity (Gen_E);
10183 end loop;
10185 if Present (Gen_E) then
10186 Map_Formal_Package_Entities (Gen_E, E);
10187 end if;
10188 end if;
10189 end if;
10191 Next_Entity (E);
10193 if Present (Gen_E) then
10194 Next_Entity (Gen_E);
10195 end if;
10196 end loop;
10197 end Install_Formal_Packages;
10199 --------------------
10200 -- Install_Parent --
10201 --------------------
10203 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10204 Ancestors : constant Elist_Id := New_Elmt_List;
10205 S : constant Entity_Id := Current_Scope;
10206 Inst_Par : Entity_Id;
10207 First_Par : Entity_Id;
10208 Inst_Node : Node_Id;
10209 Gen_Par : Entity_Id;
10210 First_Gen : Entity_Id;
10211 Elmt : Elmt_Id;
10213 procedure Install_Noninstance_Specs (Par : Entity_Id);
10214 -- Install the scopes of noninstance parent units ending with Par
10216 procedure Install_Spec (Par : Entity_Id);
10217 -- The child unit is within the declarative part of the parent, so the
10218 -- declarations within the parent are immediately visible.
10220 -------------------------------
10221 -- Install_Noninstance_Specs --
10222 -------------------------------
10224 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10225 begin
10226 if Present (Par)
10227 and then Par /= Standard_Standard
10228 and then not In_Open_Scopes (Par)
10229 then
10230 Install_Noninstance_Specs (Scope (Par));
10231 Install_Spec (Par);
10232 end if;
10233 end Install_Noninstance_Specs;
10235 ------------------
10236 -- Install_Spec --
10237 ------------------
10239 procedure Install_Spec (Par : Entity_Id) is
10240 Spec : constant Node_Id := Package_Specification (Par);
10242 begin
10243 -- If this parent of the child instance is a top-level unit,
10244 -- then record the unit and its visibility for later resetting in
10245 -- Remove_Parent. We exclude units that are generic instances, as we
10246 -- only want to record this information for the ultimate top-level
10247 -- noninstance parent (is that always correct???).
10249 if Scope (Par) = Standard_Standard
10250 and then not Is_Generic_Instance (Par)
10251 then
10252 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10253 Instance_Parent_Unit := Par;
10254 end if;
10256 -- Open the parent scope and make it and its declarations visible.
10257 -- If this point is not within a body, then only the visible
10258 -- declarations should be made visible, and installation of the
10259 -- private declarations is deferred until the appropriate point
10260 -- within analysis of the spec being instantiated (see the handling
10261 -- of parent visibility in Analyze_Package_Specification). This is
10262 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10263 -- private view problems that occur when compiling instantiations of
10264 -- a generic child of that package (Generic_Dispatching_Constructor).
10265 -- If the instance freezes a tagged type, inlinings of operations
10266 -- from Ada.Tags may need the full view of type Tag. If inlining took
10267 -- proper account of establishing visibility of inlined subprograms'
10268 -- parents then it should be possible to remove this
10269 -- special check. ???
10271 Push_Scope (Par);
10272 Set_Is_Immediately_Visible (Par);
10273 Install_Visible_Declarations (Par);
10274 Set_Use (Visible_Declarations (Spec));
10276 if In_Body or else Is_RTU (Par, Ada_Tags) then
10277 Install_Private_Declarations (Par);
10278 Set_Use (Private_Declarations (Spec));
10279 end if;
10280 end Install_Spec;
10282 -- Start of processing for Install_Parent
10284 begin
10285 -- We need to install the parent instance to compile the instantiation
10286 -- of the child, but the child instance must appear in the current
10287 -- scope. Given that we cannot place the parent above the current scope
10288 -- in the scope stack, we duplicate the current scope and unstack both
10289 -- after the instantiation is complete.
10291 -- If the parent is itself the instantiation of a child unit, we must
10292 -- also stack the instantiation of its parent, and so on. Each such
10293 -- ancestor is the prefix of the name in a prior instantiation.
10295 -- If this is a nested instance, the parent unit itself resolves to
10296 -- a renaming of the parent instance, whose declaration we need.
10298 -- Finally, the parent may be a generic (not an instance) when the
10299 -- child unit appears as a formal package.
10301 Inst_Par := P;
10303 if Present (Renamed_Entity (Inst_Par)) then
10304 Inst_Par := Renamed_Entity (Inst_Par);
10305 end if;
10307 First_Par := Inst_Par;
10309 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10311 First_Gen := Gen_Par;
10313 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10315 -- Load grandparent instance as well
10317 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10319 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10320 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10322 if Present (Renamed_Entity (Inst_Par)) then
10323 Inst_Par := Renamed_Entity (Inst_Par);
10324 end if;
10326 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10328 if Present (Gen_Par) then
10329 Prepend_Elmt (Inst_Par, Ancestors);
10331 else
10332 -- Parent is not the name of an instantiation
10334 Install_Noninstance_Specs (Inst_Par);
10335 exit;
10336 end if;
10338 else
10339 -- Previous error
10341 exit;
10342 end if;
10343 end loop;
10345 if Present (First_Gen) then
10346 Append_Elmt (First_Par, Ancestors);
10347 else
10348 Install_Noninstance_Specs (First_Par);
10349 end if;
10351 if not Is_Empty_Elmt_List (Ancestors) then
10352 Elmt := First_Elmt (Ancestors);
10353 while Present (Elmt) loop
10354 Install_Spec (Node (Elmt));
10355 Install_Formal_Packages (Node (Elmt));
10356 Next_Elmt (Elmt);
10357 end loop;
10358 end if;
10360 if not In_Body then
10361 Push_Scope (S);
10362 end if;
10363 end Install_Parent;
10365 -------------------------------
10366 -- Install_Hidden_Primitives --
10367 -------------------------------
10369 procedure Install_Hidden_Primitives
10370 (Prims_List : in out Elist_Id;
10371 Gen_T : Entity_Id;
10372 Act_T : Entity_Id)
10374 Elmt : Elmt_Id;
10375 List : Elist_Id := No_Elist;
10376 Prim_G_Elmt : Elmt_Id;
10377 Prim_A_Elmt : Elmt_Id;
10378 Prim_G : Node_Id;
10379 Prim_A : Node_Id;
10381 begin
10382 -- No action needed in case of serious errors because we cannot trust
10383 -- in the order of primitives
10385 if Serious_Errors_Detected > 0 then
10386 return;
10388 -- No action possible if we don't have available the list of primitive
10389 -- operations
10391 elsif No (Gen_T)
10392 or else not Is_Record_Type (Gen_T)
10393 or else not Is_Tagged_Type (Gen_T)
10394 or else not Is_Record_Type (Act_T)
10395 or else not Is_Tagged_Type (Act_T)
10396 then
10397 return;
10399 -- There is no need to handle interface types since their primitives
10400 -- cannot be hidden
10402 elsif Is_Interface (Gen_T) then
10403 return;
10404 end if;
10406 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10408 if not Is_Class_Wide_Type (Act_T) then
10409 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10410 else
10411 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10412 end if;
10414 loop
10415 -- Skip predefined primitives in the generic formal
10417 while Present (Prim_G_Elmt)
10418 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10419 loop
10420 Next_Elmt (Prim_G_Elmt);
10421 end loop;
10423 -- Skip predefined primitives in the generic actual
10425 while Present (Prim_A_Elmt)
10426 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10427 loop
10428 Next_Elmt (Prim_A_Elmt);
10429 end loop;
10431 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10433 Prim_G := Node (Prim_G_Elmt);
10434 Prim_A := Node (Prim_A_Elmt);
10436 -- There is no need to handle interface primitives because their
10437 -- primitives are not hidden
10439 exit when Present (Interface_Alias (Prim_G));
10441 -- Here we install one hidden primitive
10443 if Chars (Prim_G) /= Chars (Prim_A)
10444 and then Has_Suffix (Prim_A, 'P')
10445 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10446 then
10447 Set_Chars (Prim_A, Chars (Prim_G));
10448 Append_New_Elmt (Prim_A, To => List);
10449 end if;
10451 Next_Elmt (Prim_A_Elmt);
10452 Next_Elmt (Prim_G_Elmt);
10453 end loop;
10455 -- Append the elements to the list of temporarily visible primitives
10456 -- avoiding duplicates.
10458 if Present (List) then
10459 if No (Prims_List) then
10460 Prims_List := New_Elmt_List;
10461 end if;
10463 Elmt := First_Elmt (List);
10464 while Present (Elmt) loop
10465 Append_Unique_Elmt (Node (Elmt), Prims_List);
10466 Next_Elmt (Elmt);
10467 end loop;
10468 end if;
10469 end Install_Hidden_Primitives;
10471 -------------------------------
10472 -- Restore_Hidden_Primitives --
10473 -------------------------------
10475 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10476 Prim_Elmt : Elmt_Id;
10477 Prim : Node_Id;
10479 begin
10480 if Present (Prims_List) then
10481 Prim_Elmt := First_Elmt (Prims_List);
10482 while Present (Prim_Elmt) loop
10483 Prim := Node (Prim_Elmt);
10484 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10485 Next_Elmt (Prim_Elmt);
10486 end loop;
10488 Prims_List := No_Elist;
10489 end if;
10490 end Restore_Hidden_Primitives;
10492 --------------------------------
10493 -- Instantiate_Formal_Package --
10494 --------------------------------
10496 function Instantiate_Formal_Package
10497 (Formal : Node_Id;
10498 Actual : Node_Id;
10499 Analyzed_Formal : Node_Id) return List_Id
10501 Loc : constant Source_Ptr := Sloc (Actual);
10502 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10504 Actual_Pack : Entity_Id;
10505 Formal_Pack : Entity_Id;
10506 Gen_Parent : Entity_Id;
10507 Decls : List_Id;
10508 Nod : Node_Id;
10509 Parent_Spec : Node_Id;
10511 procedure Find_Matching_Actual
10512 (F : Node_Id;
10513 Act : in out Entity_Id);
10514 -- We need to associate each formal entity in the formal package with
10515 -- the corresponding entity in the actual package. The actual package
10516 -- has been analyzed and possibly expanded, and as a result there is
10517 -- no one-to-one correspondence between the two lists (for example,
10518 -- the actual may include subtypes, itypes, and inherited primitive
10519 -- operations, interspersed among the renaming declarations for the
10520 -- actuals). We retrieve the corresponding actual by name because each
10521 -- actual has the same name as the formal, and they do appear in the
10522 -- same order.
10524 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10525 -- Retrieve entity of defining entity of generic formal parameter.
10526 -- Only the declarations of formals need to be considered when
10527 -- linking them to actuals, but the declarative list may include
10528 -- internal entities generated during analysis, and those are ignored.
10530 procedure Match_Formal_Entity
10531 (Formal_Node : Node_Id;
10532 Formal_Ent : Entity_Id;
10533 Actual_Ent : Entity_Id);
10534 -- Associates the formal entity with the actual. In the case where
10535 -- Formal_Ent is a formal package, this procedure iterates through all
10536 -- of its formals and enters associations between the actuals occurring
10537 -- in the formal package's corresponding actual package (given by
10538 -- Actual_Ent) and the formal package's formal parameters. This
10539 -- procedure recurses if any of the parameters is itself a package.
10541 function Is_Instance_Of
10542 (Act_Spec : Entity_Id;
10543 Gen_Anc : Entity_Id) return Boolean;
10544 -- The actual can be an instantiation of a generic within another
10545 -- instance, in which case there is no direct link from it to the
10546 -- original generic ancestor. In that case, we recognize that the
10547 -- ultimate ancestor is the same by examining names and scopes.
10549 procedure Process_Nested_Formal (Formal : Entity_Id);
10550 -- If the current formal is declared with a box, its own formals are
10551 -- visible in the instance, as they were in the generic, and their
10552 -- Hidden flag must be reset. If some of these formals are themselves
10553 -- packages declared with a box, the processing must be recursive.
10555 --------------------------
10556 -- Find_Matching_Actual --
10557 --------------------------
10559 procedure Find_Matching_Actual
10560 (F : Node_Id;
10561 Act : in out Entity_Id)
10563 Formal_Ent : Entity_Id;
10565 begin
10566 case Nkind (Original_Node (F)) is
10567 when N_Formal_Object_Declaration
10568 | N_Formal_Type_Declaration
10570 Formal_Ent := Defining_Identifier (F);
10572 while Present (Act)
10573 and then Chars (Act) /= Chars (Formal_Ent)
10574 loop
10575 Next_Entity (Act);
10576 end loop;
10578 when N_Formal_Package_Declaration
10579 | N_Formal_Subprogram_Declaration
10580 | N_Generic_Package_Declaration
10581 | N_Package_Declaration
10583 Formal_Ent := Defining_Entity (F);
10585 while Present (Act)
10586 and then Chars (Act) /= Chars (Formal_Ent)
10587 loop
10588 Next_Entity (Act);
10589 end loop;
10591 when others =>
10592 raise Program_Error;
10593 end case;
10594 end Find_Matching_Actual;
10596 -------------------------
10597 -- Match_Formal_Entity --
10598 -------------------------
10600 procedure Match_Formal_Entity
10601 (Formal_Node : Node_Id;
10602 Formal_Ent : Entity_Id;
10603 Actual_Ent : Entity_Id)
10605 Act_Pkg : Entity_Id;
10607 begin
10608 Set_Instance_Of (Formal_Ent, Actual_Ent);
10610 if Ekind (Actual_Ent) = E_Package then
10612 -- Record associations for each parameter
10614 Act_Pkg := Actual_Ent;
10616 declare
10617 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10618 F_Ent : Entity_Id;
10619 F_Node : Node_Id;
10621 Gen_Decl : Node_Id;
10622 Formals : List_Id;
10623 Actual : Entity_Id;
10625 begin
10626 -- Retrieve the actual given in the formal package declaration
10628 Actual := Entity (Name (Original_Node (Formal_Node)));
10630 -- The actual in the formal package declaration may be a
10631 -- renamed generic package, in which case we want to retrieve
10632 -- the original generic in order to traverse its formal part.
10634 if Present (Renamed_Entity (Actual)) then
10635 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10636 else
10637 Gen_Decl := Unit_Declaration_Node (Actual);
10638 end if;
10640 Formals := Generic_Formal_Declarations (Gen_Decl);
10642 if Present (Formals) then
10643 F_Node := First_Non_Pragma (Formals);
10644 else
10645 F_Node := Empty;
10646 end if;
10648 while Present (A_Ent)
10649 and then Present (F_Node)
10650 and then A_Ent /= First_Private_Entity (Act_Pkg)
10651 loop
10652 F_Ent := Get_Formal_Entity (F_Node);
10654 if Present (F_Ent) then
10656 -- This is a formal of the original package. Record
10657 -- association and recurse.
10659 Find_Matching_Actual (F_Node, A_Ent);
10660 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10661 Next_Entity (A_Ent);
10662 end if;
10664 Next_Non_Pragma (F_Node);
10665 end loop;
10666 end;
10667 end if;
10668 end Match_Formal_Entity;
10670 -----------------------
10671 -- Get_Formal_Entity --
10672 -----------------------
10674 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10675 Kind : constant Node_Kind := Nkind (Original_Node (N));
10676 begin
10677 case Kind is
10678 when N_Formal_Object_Declaration =>
10679 return Defining_Identifier (N);
10681 when N_Formal_Type_Declaration =>
10682 return Defining_Identifier (N);
10684 when N_Formal_Subprogram_Declaration =>
10685 return Defining_Unit_Name (Specification (N));
10687 when N_Formal_Package_Declaration =>
10688 return Defining_Identifier (Original_Node (N));
10690 when N_Generic_Package_Declaration =>
10691 return Defining_Identifier (Original_Node (N));
10693 -- All other declarations are introduced by semantic analysis and
10694 -- have no match in the actual.
10696 when others =>
10697 return Empty;
10698 end case;
10699 end Get_Formal_Entity;
10701 --------------------
10702 -- Is_Instance_Of --
10703 --------------------
10705 function Is_Instance_Of
10706 (Act_Spec : Entity_Id;
10707 Gen_Anc : Entity_Id) return Boolean
10709 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10711 begin
10712 if No (Gen_Par) then
10713 return False;
10715 -- Simplest case: the generic parent of the actual is the formal
10717 elsif Gen_Par = Gen_Anc then
10718 return True;
10720 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10721 return False;
10723 -- The actual may be obtained through several instantiations. Its
10724 -- scope must itself be an instance of a generic declared in the
10725 -- same scope as the formal. Any other case is detected above.
10727 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10728 return False;
10730 else
10731 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10732 end if;
10733 end Is_Instance_Of;
10735 ---------------------------
10736 -- Process_Nested_Formal --
10737 ---------------------------
10739 procedure Process_Nested_Formal (Formal : Entity_Id) is
10740 Ent : Entity_Id;
10742 begin
10743 if Present (Associated_Formal_Package (Formal))
10744 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10745 then
10746 Ent := First_Entity (Formal);
10747 while Present (Ent) loop
10748 Set_Is_Hidden (Ent, False);
10749 Set_Is_Visible_Formal (Ent);
10750 Set_Is_Potentially_Use_Visible
10751 (Ent, Is_Potentially_Use_Visible (Formal));
10753 if Ekind (Ent) = E_Package then
10754 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10755 Process_Nested_Formal (Ent);
10756 end if;
10758 Next_Entity (Ent);
10759 end loop;
10760 end if;
10761 end Process_Nested_Formal;
10763 -- Start of processing for Instantiate_Formal_Package
10765 begin
10766 Analyze (Actual);
10768 -- The actual must be a package instance, or else a current instance
10769 -- such as a parent generic within the body of a generic child.
10771 if not Is_Entity_Name (Actual)
10772 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10773 then
10774 Error_Msg_N
10775 ("expect package instance to instantiate formal", Actual);
10776 Abandon_Instantiation (Actual);
10778 else
10779 Actual_Pack := Entity (Actual);
10780 Set_Is_Instantiated (Actual_Pack);
10782 -- The actual may be a renamed package, or an outer generic formal
10783 -- package whose instantiation is converted into a renaming.
10785 if Present (Renamed_Entity (Actual_Pack)) then
10786 Actual_Pack := Renamed_Entity (Actual_Pack);
10787 end if;
10789 -- The analyzed formal is expected to be the result of the rewriting
10790 -- of the formal package into a regular package by analysis.
10792 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10793 and then Nkind (Original_Node (Analyzed_Formal)) =
10794 N_Formal_Package_Declaration);
10796 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10797 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10799 -- The actual for a ghost generic formal package should be a ghost
10800 -- package (SPARK RM 6.9(14)).
10802 Check_Ghost_Formal_Procedure_Or_Package
10803 (N => Actual,
10804 Actual => Actual_Pack,
10805 Formal => Formal_Pack);
10807 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10808 Parent_Spec := Package_Specification (Actual_Pack);
10809 else
10810 Parent_Spec := Parent (Actual_Pack);
10811 end if;
10813 if Gen_Parent = Any_Id then
10814 Error_Msg_N
10815 ("previous error in declaration of formal package", Actual);
10816 Abandon_Instantiation (Actual);
10818 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10819 null;
10821 -- If this is the current instance of an enclosing generic, that unit
10822 -- is the generic package we need.
10824 elsif In_Open_Scopes (Actual_Pack)
10825 and then Ekind (Actual_Pack) = E_Generic_Package
10826 then
10827 null;
10829 else
10830 Error_Msg_NE
10831 ("actual parameter must be instance of&", Actual, Gen_Parent);
10832 Abandon_Instantiation (Actual);
10833 end if;
10835 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10836 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10838 Nod :=
10839 Make_Package_Renaming_Declaration (Loc,
10840 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10841 Name => New_Occurrence_Of (Actual_Pack, Loc));
10843 Set_Associated_Formal_Package
10844 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10845 Decls := New_List (Nod);
10847 -- If the formal F has a box, then the generic declarations are
10848 -- visible in the generic G. In an instance of G, the corresponding
10849 -- entities in the actual for F (which are the actuals for the
10850 -- instantiation of the generic that F denotes) must also be made
10851 -- visible for analysis of the current instance. On exit from the
10852 -- current instance, those entities are made private again. If the
10853 -- actual is currently in use, these entities are also use-visible.
10855 -- The loop through the actual entities also steps through the formal
10856 -- entities and enters associations from formals to actuals into the
10857 -- renaming map. This is necessary to properly handle checking of
10858 -- actual parameter associations for later formals that depend on
10859 -- actuals declared in the formal package.
10861 -- In Ada 2005, partial parameterization requires that we make
10862 -- visible the actuals corresponding to formals that were defaulted
10863 -- in the formal package. There formals are identified because they
10864 -- remain formal generics within the formal package, rather than
10865 -- being renamings of the actuals supplied.
10867 declare
10868 Gen_Decl : constant Node_Id :=
10869 Unit_Declaration_Node (Gen_Parent);
10870 Formals : constant List_Id :=
10871 Generic_Formal_Declarations (Gen_Decl);
10873 Actual_Ent : Entity_Id;
10874 Actual_Of_Formal : Node_Id;
10875 Formal_Node : Node_Id;
10876 Formal_Ent : Entity_Id;
10878 begin
10879 if Present (Formals) then
10880 Formal_Node := First_Non_Pragma (Formals);
10881 else
10882 Formal_Node := Empty;
10883 end if;
10885 Actual_Ent := First_Entity (Actual_Pack);
10886 Actual_Of_Formal :=
10887 First (Visible_Declarations (Specification (Analyzed_Formal)));
10888 while Present (Actual_Ent)
10889 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10890 loop
10891 if Present (Formal_Node) then
10892 Formal_Ent := Get_Formal_Entity (Formal_Node);
10894 if Present (Formal_Ent) then
10895 Find_Matching_Actual (Formal_Node, Actual_Ent);
10896 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10898 -- We iterate at the same time over the actuals of the
10899 -- local package created for the formal, to determine
10900 -- which one of the formals of the original generic were
10901 -- defaulted in the formal. The corresponding actual
10902 -- entities are visible in the enclosing instance.
10904 if Box_Present (Formal)
10905 or else
10906 (Present (Actual_Of_Formal)
10907 and then
10908 Is_Generic_Formal
10909 (Get_Formal_Entity (Actual_Of_Formal)))
10910 then
10911 Set_Is_Hidden (Actual_Ent, False);
10912 Set_Is_Visible_Formal (Actual_Ent);
10913 Set_Is_Potentially_Use_Visible
10914 (Actual_Ent, In_Use (Actual_Pack));
10916 if Ekind (Actual_Ent) = E_Package then
10917 Process_Nested_Formal (Actual_Ent);
10918 end if;
10920 else
10921 if not Is_Hidden (Actual_Ent) then
10922 Append_Elmt (Actual_Ent, Hidden_Formals);
10923 end if;
10925 Set_Is_Hidden (Actual_Ent);
10926 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10927 end if;
10928 end if;
10930 Next_Non_Pragma (Formal_Node);
10931 Next (Actual_Of_Formal);
10933 -- A formal subprogram may be overloaded, so advance in
10934 -- the list of actuals to make sure we do not match two
10935 -- successive formals to the same actual. This is only
10936 -- relevant for overloadable entities, others have
10937 -- distinct names.
10939 if Is_Overloadable (Actual_Ent) then
10940 Next_Entity (Actual_Ent);
10941 end if;
10943 else
10944 -- No further formals to match, but the generic part may
10945 -- contain inherited operation that are not hidden in the
10946 -- enclosing instance.
10948 Next_Entity (Actual_Ent);
10949 end if;
10950 end loop;
10952 -- Inherited subprograms generated by formal derived types are
10953 -- also visible if the types are.
10955 Actual_Ent := First_Entity (Actual_Pack);
10956 while Present (Actual_Ent)
10957 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10958 loop
10959 if Is_Overloadable (Actual_Ent)
10960 and then
10961 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10962 and then
10963 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10964 then
10965 Set_Is_Hidden (Actual_Ent, False);
10966 Set_Is_Potentially_Use_Visible
10967 (Actual_Ent, In_Use (Actual_Pack));
10968 end if;
10970 Next_Entity (Actual_Ent);
10971 end loop;
10972 end;
10974 -- If the formal requires conformance checking, reanalyze it as an
10975 -- abbreviated instantiation, to verify the matching rules of 12.7.
10976 -- The actual checks are performed after the generic associations
10977 -- have been analyzed, to guarantee the same visibility for this
10978 -- instantiation and for the actuals.
10980 -- In Ada 2005, the generic associations for the formal can include
10981 -- defaulted parameters. These are ignored during check. This
10982 -- internal instantiation is removed from the tree after conformance
10983 -- checking, because it contains formal declarations for those
10984 -- defaulted parameters, and those should not reach the back-end.
10986 if Requires_Conformance_Checking (Formal) then
10987 declare
10988 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10990 I_Nam : Node_Id;
10992 begin
10993 Set_Is_Internal (I_Pack);
10994 Mutate_Ekind (I_Pack, E_Package);
10996 -- Insert the package into the list of its hidden entities so
10997 -- that the list is not empty for Is_Abbreviated_Instance.
10999 Append_Elmt (I_Pack, Hidden_Formals);
11001 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
11003 -- If the generic is a child unit, Check_Generic_Child_Unit
11004 -- needs its original name in case it is qualified.
11006 if Is_Child_Unit (Gen_Parent) then
11007 I_Nam :=
11008 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
11009 pragma Assert (Entity (I_Nam) = Gen_Parent);
11011 else
11012 I_Nam :=
11013 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11014 end if;
11016 Append_To (Decls,
11017 Make_Package_Instantiation (Loc,
11018 Defining_Unit_Name => I_Pack,
11019 Name => I_Nam,
11020 Generic_Associations => Generic_Associations (Formal)));
11021 end;
11022 end if;
11024 return Decls;
11025 end if;
11026 end Instantiate_Formal_Package;
11028 -----------------------------------
11029 -- Instantiate_Formal_Subprogram --
11030 -----------------------------------
11032 function Instantiate_Formal_Subprogram
11033 (Formal : Node_Id;
11034 Actual : Node_Id;
11035 Analyzed_Formal : Node_Id) return Node_Id
11037 Analyzed_S : constant Entity_Id :=
11038 Defining_Unit_Name (Specification (Analyzed_Formal));
11039 Formal_Sub : constant Entity_Id :=
11040 Defining_Unit_Name (Specification (Formal));
11042 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11043 -- If the generic is a child unit, the parent has been installed on the
11044 -- scope stack, but a default subprogram cannot resolve to something
11045 -- on the parent because that parent is not really part of the visible
11046 -- context (it is there to resolve explicit local entities). If the
11047 -- default has resolved in this way, we remove the entity from immediate
11048 -- visibility and analyze the node again to emit an error message or
11049 -- find another visible candidate.
11051 procedure Valid_Actual_Subprogram (Act : Node_Id);
11052 -- Perform legality check and raise exception on failure
11054 -----------------------
11055 -- From_Parent_Scope --
11056 -----------------------
11058 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11059 Gen_Scope : Node_Id;
11061 begin
11062 Gen_Scope := Scope (Analyzed_S);
11063 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11064 if Scope (Subp) = Scope (Gen_Scope) then
11065 return True;
11066 end if;
11068 Gen_Scope := Scope (Gen_Scope);
11069 end loop;
11071 return False;
11072 end From_Parent_Scope;
11074 -----------------------------
11075 -- Valid_Actual_Subprogram --
11076 -----------------------------
11078 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11079 Act_E : Entity_Id;
11081 begin
11082 if Is_Entity_Name (Act) then
11083 Act_E := Entity (Act);
11085 elsif Nkind (Act) = N_Selected_Component
11086 and then Is_Entity_Name (Selector_Name (Act))
11087 then
11088 Act_E := Entity (Selector_Name (Act));
11090 else
11091 Act_E := Empty;
11092 end if;
11094 -- The actual for a ghost generic formal procedure should be a ghost
11095 -- procedure (SPARK RM 6.9(14)).
11097 if Present (Act_E)
11098 and then Ekind (Act_E) = E_Procedure
11099 then
11100 Check_Ghost_Formal_Procedure_Or_Package
11101 (N => Act,
11102 Actual => Act_E,
11103 Formal => Analyzed_S);
11104 end if;
11106 if (Present (Act_E) and then Is_Overloadable (Act_E))
11107 or else Nkind (Act) in N_Attribute_Reference
11108 | N_Indexed_Component
11109 | N_Character_Literal
11110 | N_Explicit_Dereference
11111 then
11112 return;
11113 end if;
11115 Error_Msg_NE
11116 ("expect subprogram or entry name in instantiation of &",
11117 Instantiation_Node, Formal_Sub);
11118 Abandon_Instantiation (Instantiation_Node);
11119 end Valid_Actual_Subprogram;
11121 -- Local variables
11123 Decl_Node : Node_Id;
11124 Loc : Source_Ptr;
11125 Nam : Node_Id;
11126 New_Spec : Node_Id;
11127 New_Subp : Entity_Id;
11129 -- Start of processing for Instantiate_Formal_Subprogram
11131 begin
11132 New_Spec := New_Copy_Tree (Specification (Formal));
11134 -- The tree copy has created the proper instantiation sloc for the
11135 -- new specification. Use this location for all other constructed
11136 -- declarations.
11138 Loc := Sloc (Defining_Unit_Name (New_Spec));
11140 -- Create new entity for the actual (New_Copy_Tree does not), and
11141 -- indicate that it is an actual.
11143 -- If the actual is not an entity (i.e. an attribute reference)
11144 -- and the formal includes aspect specifications for contracts,
11145 -- we create an internal name for the renaming declaration. The
11146 -- constructed wrapper contains a call to the entity in the renaming.
11147 -- This is an expansion activity, as is the wrapper creation.
11149 if Ada_Version >= Ada_2022
11150 and then Has_Contracts (Analyzed_Formal)
11151 and then not Is_Entity_Name (Actual)
11152 and then Expander_Active
11153 then
11154 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11155 else
11156 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11157 end if;
11159 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11160 Set_Is_Generic_Actual_Subprogram (New_Subp);
11161 Set_Defining_Unit_Name (New_Spec, New_Subp);
11163 -- Create new entities for the each of the formals in the specification
11164 -- of the renaming declaration built for the actual.
11166 if Present (Parameter_Specifications (New_Spec)) then
11167 declare
11168 F : Node_Id;
11169 F_Id : Entity_Id;
11171 begin
11172 F := First (Parameter_Specifications (New_Spec));
11173 while Present (F) loop
11174 F_Id := Defining_Identifier (F);
11176 Set_Defining_Identifier (F,
11177 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11178 Next (F);
11179 end loop;
11180 end;
11181 end if;
11183 -- Find entity of actual. If the actual is an attribute reference, it
11184 -- cannot be resolved here (its formal is missing) but is handled
11185 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11186 -- fully resolved subsequently, when the renaming declaration for the
11187 -- formal is analyzed. If it is an explicit dereference, resolve the
11188 -- prefix but not the actual itself, to prevent interpretation as call.
11190 if Present (Actual) then
11191 Loc := Sloc (Actual);
11192 Set_Sloc (New_Spec, Loc);
11194 if Nkind (Actual) = N_Operator_Symbol then
11195 Find_Direct_Name (Actual);
11197 elsif Nkind (Actual) = N_Explicit_Dereference then
11198 Analyze (Prefix (Actual));
11200 elsif Nkind (Actual) /= N_Attribute_Reference then
11201 Analyze (Actual);
11202 end if;
11204 Valid_Actual_Subprogram (Actual);
11205 Nam := Actual;
11207 elsif Present (Default_Name (Formal)) then
11208 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11209 | N_Selected_Component
11210 | N_Indexed_Component
11211 | N_Character_Literal
11212 and then Present (Entity (Default_Name (Formal)))
11213 then
11214 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11215 else
11216 Nam := New_Copy (Default_Name (Formal));
11217 Set_Sloc (Nam, Loc);
11218 end if;
11220 elsif Box_Present (Formal) then
11222 -- Actual is resolved at the point of instantiation. Create an
11223 -- identifier or operator with the same name as the formal.
11225 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11226 Nam :=
11227 Make_Operator_Symbol (Loc,
11228 Chars => Chars (Formal_Sub),
11229 Strval => No_String);
11230 else
11231 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11232 end if;
11234 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11235 and then Null_Present (Specification (Formal))
11236 then
11237 -- Generate null body for procedure, for use in the instance
11239 Decl_Node :=
11240 Make_Subprogram_Body (Loc,
11241 Specification => New_Spec,
11242 Declarations => New_List,
11243 Handled_Statement_Sequence =>
11244 Make_Handled_Sequence_Of_Statements (Loc,
11245 Statements => New_List (Make_Null_Statement (Loc))));
11247 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11249 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11251 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11253 -- Eliminate the calls to it when optimization is enabled
11255 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11256 return Decl_Node;
11258 -- Handle case of a formal function with an expression default (allowed
11259 -- when extensions are enabled).
11261 elsif Nkind (Specification (Formal)) = N_Function_Specification
11262 and then Present (Expression (Formal))
11263 then
11264 -- Generate body for function, for use in the instance
11266 declare
11267 Expr : constant Node_Id := New_Copy (Expression (Formal));
11268 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11269 begin
11270 Set_Sloc (Expr, Loc);
11271 Set_Expression (Stmt, Expr);
11273 Decl_Node :=
11274 Make_Subprogram_Body (Loc,
11275 Specification => New_Spec,
11276 Declarations => New_List,
11277 Handled_Statement_Sequence =>
11278 Make_Handled_Sequence_Of_Statements (Loc,
11279 Statements => New_List (Stmt)));
11280 end;
11282 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11283 -- has convention Intrinsic.
11285 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11287 -- Inline calls to it when optimization is enabled
11289 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11290 return Decl_Node;
11292 else
11293 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11294 Error_Msg_NE
11295 ("missing actual&", Instantiation_Node, Formal_Sub);
11296 Error_Msg_NE
11297 ("\in instantiation of & declared#",
11298 Instantiation_Node, Scope (Analyzed_S));
11299 Abandon_Instantiation (Instantiation_Node);
11300 end if;
11302 Decl_Node :=
11303 Make_Subprogram_Renaming_Declaration (Loc,
11304 Specification => New_Spec,
11305 Name => Nam);
11307 -- If we do not have an actual and the formal specified <> then set to
11308 -- get proper default.
11310 if No (Actual) and then Box_Present (Formal) then
11311 Set_From_Default (Decl_Node);
11312 end if;
11314 -- Gather possible interpretations for the actual before analyzing the
11315 -- instance. If overloaded, it will be resolved when analyzing the
11316 -- renaming declaration.
11318 if Box_Present (Formal) and then No (Actual) then
11319 Analyze (Nam);
11321 if Is_Child_Unit (Scope (Analyzed_S))
11322 and then Present (Entity (Nam))
11323 then
11324 if not Is_Overloaded (Nam) then
11325 if From_Parent_Scope (Entity (Nam)) then
11326 Set_Is_Immediately_Visible (Entity (Nam), False);
11327 Set_Entity (Nam, Empty);
11328 Set_Etype (Nam, Empty);
11330 Analyze (Nam);
11331 Set_Is_Immediately_Visible (Entity (Nam));
11332 end if;
11334 else
11335 declare
11336 I : Interp_Index;
11337 It : Interp;
11339 begin
11340 Get_First_Interp (Nam, I, It);
11341 while Present (It.Nam) loop
11342 if From_Parent_Scope (It.Nam) then
11343 Remove_Interp (I);
11344 end if;
11346 Get_Next_Interp (I, It);
11347 end loop;
11348 end;
11349 end if;
11350 end if;
11351 end if;
11353 -- The generic instantiation freezes the actual. This can only be done
11354 -- once the actual is resolved, in the analysis of the renaming
11355 -- declaration. To make the formal subprogram entity available, we set
11356 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11357 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11358 -- of formal abstract subprograms.
11360 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11362 -- We cannot analyze the renaming declaration, and thus find the actual,
11363 -- until all the actuals are assembled in the instance. For subsequent
11364 -- checks of other actuals, indicate the node that will hold the
11365 -- instance of this formal.
11367 Set_Instance_Of (Analyzed_S, Nam);
11369 if Nkind (Actual) = N_Selected_Component
11370 and then Is_Task_Type (Etype (Prefix (Actual)))
11371 and then not Is_Frozen (Etype (Prefix (Actual)))
11372 then
11373 -- The renaming declaration will create a body, which must appear
11374 -- outside of the instantiation, We move the renaming declaration
11375 -- out of the instance, and create an additional renaming inside,
11376 -- to prevent freezing anomalies.
11378 declare
11379 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11381 begin
11382 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11383 Insert_Before (Instantiation_Node, Decl_Node);
11384 Analyze (Decl_Node);
11386 -- Now create renaming within the instance
11388 Decl_Node :=
11389 Make_Subprogram_Renaming_Declaration (Loc,
11390 Specification => New_Copy_Tree (New_Spec),
11391 Name => New_Occurrence_Of (Anon_Id, Loc));
11393 Set_Defining_Unit_Name (Specification (Decl_Node),
11394 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11395 end;
11396 end if;
11398 return Decl_Node;
11399 end Instantiate_Formal_Subprogram;
11401 ------------------------
11402 -- Instantiate_Object --
11403 ------------------------
11405 function Instantiate_Object
11406 (Formal : Node_Id;
11407 Actual : Node_Id;
11408 Analyzed_Formal : Node_Id) return List_Id
11410 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11411 A_Gen_Obj : constant Entity_Id :=
11412 Defining_Identifier (Analyzed_Formal);
11413 Acc_Def : Node_Id := Empty;
11414 Act_Assoc : constant Node_Id :=
11415 (if No (Actual) then Empty else Parent (Actual));
11416 Actual_Decl : Node_Id := Empty;
11417 Decl_Node : Node_Id;
11418 Def : Node_Id;
11419 Ftyp : Entity_Id;
11420 List : constant List_Id := New_List;
11421 Loc : constant Source_Ptr := Sloc (Actual);
11422 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11423 Subt_Decl : Node_Id := Empty;
11424 Subt_Mark : Node_Id := Empty;
11426 -- Start of processing for Instantiate_Object
11428 begin
11429 -- Formal may be an anonymous access
11431 if Present (Subtype_Mark (Formal)) then
11432 Subt_Mark := Subtype_Mark (Formal);
11433 else
11434 Check_Access_Definition (Formal);
11435 Acc_Def := Access_Definition (Formal);
11436 end if;
11438 -- Sloc for error message on missing actual
11440 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11442 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11443 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11444 end if;
11446 Set_Parent (List, Act_Assoc);
11448 -- OUT present
11450 if Out_Present (Formal) then
11452 -- An IN OUT generic actual must be a name. The instantiation is a
11453 -- renaming declaration. The actual is the name being renamed. We
11454 -- use the actual directly, rather than a copy, because it is not
11455 -- used further in the list of actuals, and because a copy or a use
11456 -- of relocate_node is incorrect if the instance is nested within a
11457 -- generic. In order to simplify e.g. ASIS queries, the
11458 -- Generic_Parent field links the declaration to the generic
11459 -- association.
11461 if No (Actual) then
11462 Error_Msg_NE
11463 ("missing actual &",
11464 Instantiation_Node, Gen_Obj);
11465 Error_Msg_NE
11466 ("\in instantiation of & declared#",
11467 Instantiation_Node, Scope (A_Gen_Obj));
11468 Abandon_Instantiation (Instantiation_Node);
11469 end if;
11471 if Present (Subt_Mark) then
11472 Decl_Node :=
11473 Make_Object_Renaming_Declaration (Loc,
11474 Defining_Identifier => New_Copy (Gen_Obj),
11475 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11476 Name => Actual);
11478 else pragma Assert (Present (Acc_Def));
11479 Decl_Node :=
11480 Make_Object_Renaming_Declaration (Loc,
11481 Defining_Identifier => New_Copy (Gen_Obj),
11482 Access_Definition => New_Copy_Tree (Acc_Def),
11483 Name => Actual);
11484 end if;
11486 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11488 -- The analysis of the actual may produce Insert_Action nodes, so
11489 -- the declaration must have a context in which to attach them.
11491 Append (Decl_Node, List);
11492 Analyze (Actual);
11494 -- Return if the analysis of the actual reported some error
11496 if Etype (Actual) = Any_Type then
11497 return List;
11498 end if;
11500 -- This check is performed here because Analyze_Object_Renaming will
11501 -- not check it when Comes_From_Source is False. Note though that the
11502 -- check for the actual being the name of an object will be performed
11503 -- in Analyze_Object_Renaming.
11505 if Is_Object_Reference (Actual)
11506 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11507 then
11508 Error_Msg_N
11509 ("illegal discriminant-dependent component for in out parameter",
11510 Actual);
11511 end if;
11513 -- The actual has to be resolved in order to check that it is a
11514 -- variable (due to cases such as F (1), where F returns access to
11515 -- an array, and for overloaded prefixes).
11517 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11519 -- If the type of the formal is not itself a formal, and the current
11520 -- unit is a child unit, the formal type must be declared in a
11521 -- parent, and must be retrieved by visibility.
11523 if Ftyp = Orig_Ftyp
11524 and then Is_Generic_Unit (Scope (Ftyp))
11525 and then Is_Child_Unit (Scope (A_Gen_Obj))
11526 then
11527 declare
11528 Temp : constant Node_Id :=
11529 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11530 begin
11531 Set_Entity (Temp, Empty);
11532 Find_Type (Temp);
11533 Ftyp := Entity (Temp);
11534 end;
11535 end if;
11537 if Is_Private_Type (Ftyp)
11538 and then not Is_Private_Type (Etype (Actual))
11539 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11540 or else Base_Type (Etype (Actual)) = Ftyp)
11541 then
11542 -- If the actual has the type of the full view of the formal, or
11543 -- else a non-private subtype of the formal, then the visibility
11544 -- of the formal type has changed. Add to the actuals a subtype
11545 -- declaration that will force the exchange of views in the body
11546 -- of the instance as well.
11548 Subt_Decl :=
11549 Make_Subtype_Declaration (Loc,
11550 Defining_Identifier => Make_Temporary (Loc, 'P'),
11551 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11553 Prepend (Subt_Decl, List);
11555 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11556 Exchange_Declarations (Ftyp);
11557 end if;
11559 Resolve (Actual, Ftyp);
11561 if not Denotes_Variable (Actual) then
11562 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11564 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11566 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11567 -- the type of the actual shall resolve to a specific anonymous
11568 -- access type.
11570 if Ada_Version < Ada_2005
11571 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11572 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11573 then
11574 Error_Msg_NE
11575 ("type of actual does not match type of&", Actual, Gen_Obj);
11576 end if;
11577 end if;
11579 Note_Possible_Modification (Actual, Sure => True);
11581 -- Check for instantiation with atomic/volatile/VFA object actual for
11582 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11584 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11585 Error_Msg_NE
11586 ("cannot instantiate nonatomic formal & of mode in out",
11587 Actual, Gen_Obj);
11588 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11590 elsif Is_Volatile_Object_Ref (Actual)
11591 and then not Is_Volatile (Orig_Ftyp)
11592 then
11593 Error_Msg_NE
11594 ("cannot instantiate nonvolatile formal & of mode in out",
11595 Actual, Gen_Obj);
11596 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11598 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11599 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11600 then
11601 Error_Msg_NE
11602 ("cannot instantiate nonfull access formal & of mode in out",
11603 Actual, Gen_Obj);
11604 Error_Msg_N
11605 ("\with full access object actual (RM C.6(12))", Actual);
11606 end if;
11608 -- Check for instantiation on nonatomic subcomponent of a full access
11609 -- object in Ada 2022 (RM C.6 (12)).
11611 if Ada_Version >= Ada_2022
11612 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11613 and then not Is_Atomic_Object (Actual)
11614 then
11615 Error_Msg_NE
11616 ("cannot instantiate formal & of mode in out with actual",
11617 Actual, Gen_Obj);
11618 Error_Msg_N
11619 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11620 Actual);
11621 end if;
11623 -- The actual for a ghost generic formal IN OUT parameter should be a
11624 -- ghost object (SPARK RM 6.9(14)).
11626 Check_Ghost_Formal_Variable
11627 (Actual => Actual,
11628 Formal => A_Gen_Obj);
11630 -- Formal in-parameter
11632 else
11633 -- The instantiation of a generic formal in-parameter is constant
11634 -- declaration. The actual is the expression for that declaration.
11635 -- Its type is a full copy of the type of the formal. This may be
11636 -- an access to subprogram, for which we need to generate entities
11637 -- for the formals in the new signature.
11639 if Present (Actual) then
11640 if Present (Subt_Mark) then
11641 Def := New_Copy_Tree (Subt_Mark);
11642 else
11643 pragma Assert (Present (Acc_Def));
11644 Def := New_Copy_Tree (Acc_Def);
11645 end if;
11647 Decl_Node :=
11648 Make_Object_Declaration (Loc,
11649 Defining_Identifier => New_Copy (Gen_Obj),
11650 Constant_Present => True,
11651 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11652 Object_Definition => Def,
11653 Expression => Actual);
11655 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11656 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11658 -- A generic formal object of a tagged type is defined to be
11659 -- aliased so the new constant must also be treated as aliased.
11661 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11662 Set_Aliased_Present (Decl_Node);
11663 end if;
11665 Append (Decl_Node, List);
11667 -- The actual for a ghost generic formal IN parameter of
11668 -- access-to-variable type should be a ghost object (SPARK
11669 -- RM 6.9(14)).
11671 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11672 Check_Ghost_Formal_Variable
11673 (Actual => Actual,
11674 Formal => A_Gen_Obj);
11675 end if;
11677 -- No need to repeat (pre-)analysis of some expression nodes
11678 -- already handled in Preanalyze_Actuals.
11680 if Nkind (Actual) /= N_Allocator then
11681 Analyze (Actual);
11683 -- Return if the analysis of the actual reported some error
11685 if Etype (Actual) = Any_Type then
11686 return List;
11687 end if;
11688 end if;
11690 declare
11691 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11692 Typ : Entity_Id;
11694 begin
11695 Typ := Get_Instance_Of (Formal_Type);
11697 -- If the actual appears in the current or an enclosing scope,
11698 -- use its type directly. This is relevant if it has an actual
11699 -- subtype that is distinct from its nominal one. This cannot
11700 -- be done in general because the type of the actual may
11701 -- depend on other actuals, and only be fully determined when
11702 -- the enclosing instance is analyzed.
11704 if Present (Etype (Actual))
11705 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11706 then
11707 Freeze_Before (Instantiation_Node, Etype (Actual));
11708 else
11709 Freeze_Before (Instantiation_Node, Typ);
11710 end if;
11712 -- If the actual is an aggregate, perform name resolution on
11713 -- its components (the analysis of an aggregate does not do it)
11714 -- to capture local names that may be hidden if the generic is
11715 -- a child unit.
11717 if Nkind (Actual) = N_Aggregate then
11718 Preanalyze_And_Resolve (Actual, Typ);
11719 end if;
11721 if Is_Limited_Type (Typ)
11722 and then not OK_For_Limited_Init (Typ, Actual)
11723 then
11724 Error_Msg_N
11725 ("initialization not allowed for limited types", Actual);
11726 Explain_Limited_Type (Typ, Actual);
11727 end if;
11728 end;
11730 elsif Present (Default_Expression (Formal)) then
11732 -- Use default to construct declaration
11734 if Present (Subt_Mark) then
11735 Def := New_Copy_Tree (Subt_Mark);
11736 else
11737 pragma Assert (Present (Acc_Def));
11738 Def := New_Copy_Tree (Acc_Def);
11739 end if;
11741 Decl_Node :=
11742 Make_Object_Declaration (Sloc (Formal),
11743 Defining_Identifier => New_Copy (Gen_Obj),
11744 Constant_Present => True,
11745 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11746 Object_Definition => Def,
11747 Expression => New_Copy_Tree
11748 (Default_Expression (Formal)));
11750 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11751 Set_Corresponding_Generic_Association
11752 (Decl_Node, Expression (Decl_Node));
11754 Append (Decl_Node, List);
11755 Set_Analyzed (Expression (Decl_Node), False);
11757 else
11758 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11759 Error_Msg_NE ("\in instantiation of & declared#",
11760 Instantiation_Node, Scope (A_Gen_Obj));
11762 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11764 -- Create dummy constant declaration so that instance can be
11765 -- analyzed, to minimize cascaded visibility errors.
11767 if Present (Subt_Mark) then
11768 Def := Subt_Mark;
11769 else pragma Assert (Present (Acc_Def));
11770 Def := Acc_Def;
11771 end if;
11773 Decl_Node :=
11774 Make_Object_Declaration (Loc,
11775 Defining_Identifier => New_Copy (Gen_Obj),
11776 Constant_Present => True,
11777 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11778 Object_Definition => New_Copy (Def),
11779 Expression =>
11780 Make_Attribute_Reference (Sloc (Gen_Obj),
11781 Attribute_Name => Name_First,
11782 Prefix => New_Copy (Def)));
11784 Append (Decl_Node, List);
11786 else
11787 Abandon_Instantiation (Instantiation_Node);
11788 end if;
11789 end if;
11790 end if;
11792 if Nkind (Actual) in N_Has_Entity
11793 and then Present (Entity (Actual))
11794 then
11795 Actual_Decl := Parent (Entity (Actual));
11796 end if;
11798 -- Ada 2005 (AI-423) refined by AI12-0287:
11799 -- For an object_renaming_declaration with a null_exclusion or an
11800 -- access_definition that has a null_exclusion, the subtype of the
11801 -- object_name shall exclude null. In addition, if the
11802 -- object_renaming_declaration occurs within the body of a generic unit
11803 -- G or within the body of a generic unit declared within the
11804 -- declarative region of generic unit G, then:
11805 -- * if the object_name statically denotes a generic formal object of
11806 -- mode in out of G, then the declaration of that object shall have a
11807 -- null_exclusion;
11808 -- * if the object_name statically denotes a call of a generic formal
11809 -- function of G, then the declaration of the result of that function
11810 -- shall have a null_exclusion.
11812 if Ada_Version >= Ada_2005
11813 and then Present (Actual_Decl)
11814 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11815 | N_Object_Declaration
11816 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11817 and then not Has_Null_Exclusion (Actual_Decl)
11818 and then Has_Null_Exclusion (Analyzed_Formal)
11819 and then Ekind (Defining_Identifier (Analyzed_Formal))
11820 = E_Generic_In_Out_Parameter
11821 and then ((In_Generic_Scope (Entity (Actual))
11822 and then In_Package_Body (Scope (Entity (Actual))))
11823 or else not Can_Never_Be_Null (Etype (Actual)))
11824 then
11825 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11826 Error_Msg_N
11827 ("actual must exclude null to match generic formal#", Actual);
11828 end if;
11830 return List;
11831 end Instantiate_Object;
11833 ------------------------------
11834 -- Instantiate_Package_Body --
11835 ------------------------------
11837 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11838 -- must be replaced by gotos which jump to the end of the routine in order
11839 -- to restore the Ghost and SPARK modes.
11841 procedure Instantiate_Package_Body
11842 (Body_Info : Pending_Body_Info;
11843 Inlined_Body : Boolean := False;
11844 Body_Optional : Boolean := False)
11846 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11847 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11848 Act_Spec : constant Node_Id := Specification (Act_Decl);
11849 Ctx_Parents : Elist_Id := No_Elist;
11850 Ctx_Top : Int := 0;
11851 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11852 Gen_Id : constant Node_Id := Name (Inst_Node);
11853 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11854 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11855 Loc : constant Source_Ptr := Sloc (Inst_Node);
11857 procedure Check_Initialized_Types;
11858 -- In a generic package body, an entity of a generic private type may
11859 -- appear uninitialized. This is suspicious, unless the actual is a
11860 -- fully initialized type.
11862 procedure Install_Parents_Of_Generic_Context
11863 (Inst_Scope : Entity_Id;
11864 Ctx_Parents : out Elist_Id);
11865 -- Inst_Scope is the scope where the instance appears within; when it
11866 -- appears within a generic child package G, this routine collects and
11867 -- installs the enclosing packages of G in the scopes stack; installed
11868 -- packages are returned in Ctx_Parents.
11870 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11871 -- Reverse effect after instantiation is complete
11873 -----------------------------
11874 -- Check_Initialized_Types --
11875 -----------------------------
11877 procedure Check_Initialized_Types is
11878 Decl : Node_Id;
11879 Formal : Entity_Id;
11880 Actual : Entity_Id;
11881 Uninit_Var : Entity_Id;
11883 begin
11884 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11885 while Present (Decl) loop
11886 Uninit_Var := Empty;
11888 if Nkind (Decl) = N_Private_Extension_Declaration then
11889 Uninit_Var := Uninitialized_Variable (Decl);
11891 elsif Nkind (Decl) = N_Formal_Type_Declaration
11892 and then Nkind (Formal_Type_Definition (Decl)) =
11893 N_Formal_Private_Type_Definition
11894 then
11895 Uninit_Var :=
11896 Uninitialized_Variable (Formal_Type_Definition (Decl));
11897 end if;
11899 if Present (Uninit_Var) then
11900 Formal := Defining_Identifier (Decl);
11901 Actual := First_Entity (Act_Decl_Id);
11903 -- For each formal there is a subtype declaration that renames
11904 -- the actual and has the same name as the formal. Locate the
11905 -- formal for warning message about uninitialized variables
11906 -- in the generic, for which the actual type should be a fully
11907 -- initialized type.
11909 while Present (Actual) loop
11910 exit when Ekind (Actual) = E_Package
11911 and then Present (Renamed_Entity (Actual));
11913 if Chars (Actual) = Chars (Formal)
11914 and then not Is_Scalar_Type (Actual)
11915 and then not Is_Fully_Initialized_Type (Actual)
11916 and then Warn_On_No_Value_Assigned
11917 then
11918 Error_Msg_Node_2 := Formal;
11919 Error_Msg_NE
11920 ("generic unit has uninitialized variable& of "
11921 & "formal private type &?v?", Actual, Uninit_Var);
11922 Error_Msg_NE
11923 ("actual type for& should be fully initialized type?v?",
11924 Actual, Formal);
11925 exit;
11926 end if;
11928 Next_Entity (Actual);
11929 end loop;
11930 end if;
11932 Next (Decl);
11933 end loop;
11934 end Check_Initialized_Types;
11936 ----------------------------------------
11937 -- Install_Parents_Of_Generic_Context --
11938 ----------------------------------------
11940 procedure Install_Parents_Of_Generic_Context
11941 (Inst_Scope : Entity_Id;
11942 Ctx_Parents : out Elist_Id)
11944 Elmt : Elmt_Id;
11945 S : Entity_Id;
11947 begin
11948 Ctx_Parents := New_Elmt_List;
11950 -- Collect context parents (ie. parents where the instantiation
11951 -- appears within).
11953 S := Inst_Scope;
11954 while S /= Standard_Standard loop
11955 Prepend_Elmt (S, Ctx_Parents);
11956 S := Scope (S);
11957 end loop;
11959 -- Install enclosing parents
11961 Elmt := First_Elmt (Ctx_Parents);
11962 while Present (Elmt) loop
11963 Push_Scope (Node (Elmt));
11964 Set_Is_Immediately_Visible (Node (Elmt));
11965 Next_Elmt (Elmt);
11966 end loop;
11967 end Install_Parents_Of_Generic_Context;
11969 ---------------------------------------
11970 -- Remove_Parents_Of_Generic_Context --
11971 ---------------------------------------
11973 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11974 Elmt : Elmt_Id;
11976 begin
11977 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11979 Elmt := Last_Elmt (Ctx_Parents);
11980 while Present (Elmt) loop
11981 pragma Assert (Current_Scope = Node (Elmt));
11982 Set_Is_Immediately_Visible (Current_Scope, False);
11983 Pop_Scope;
11985 Remove_Last_Elmt (Ctx_Parents);
11986 Elmt := Last_Elmt (Ctx_Parents);
11987 end loop;
11988 end Remove_Parents_Of_Generic_Context;
11990 -- Local variables
11992 -- The following constants capture the context prior to instantiating
11993 -- the package body.
11995 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11996 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11997 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11998 Saved_ISMP : constant Boolean :=
11999 Ignore_SPARK_Mode_Pragmas_In_Instance;
12000 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12001 Local_Suppress_Stack_Top;
12002 Saved_SC : constant Boolean := Style_Check;
12003 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12004 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12005 Saved_SS : constant Suppress_Record := Scope_Suppress;
12006 Saved_Warn : constant Warnings_State := Save_Warnings;
12008 Act_Body : Node_Id;
12009 Act_Body_Id : Entity_Id;
12010 Act_Body_Name : Node_Id;
12011 Gen_Body : Node_Id;
12012 Gen_Body_Id : Node_Id;
12013 Par_Ent : Entity_Id := Empty;
12014 Par_Installed : Boolean := False;
12015 Par_Vis : Boolean := False;
12017 Scope_Check_Id : Entity_Id;
12018 Scope_Check_Last : Nat;
12019 -- Value of Current_Scope before calls to Install_Parents; used to check
12020 -- that scopes are correctly removed after instantiation.
12022 Vis_Prims_List : Elist_Id := No_Elist;
12023 -- List of primitives made temporarily visible in the instantiation
12024 -- to match the visibility of the formal type.
12026 -- Start of processing for Instantiate_Package_Body
12028 begin
12029 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12031 -- The instance body may already have been processed, as the parent of
12032 -- another instance that is inlined (Load_Parent_Of_Generic).
12034 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12035 return;
12036 end if;
12038 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12040 -- Re-establish the state of information on which checks are suppressed.
12041 -- This information was set in Body_Info at the point of instantiation,
12042 -- and now we restore it so that the instance is compiled using the
12043 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12045 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12046 Scope_Suppress := Body_Info.Scope_Suppress;
12048 Restore_Config_Switches (Body_Info.Config_Switches);
12049 Restore_Warnings (Body_Info.Warnings);
12051 if No (Gen_Body_Id) then
12053 -- Do not look for parent of generic body if none is required.
12054 -- This may happen when the routine is called as part of the
12055 -- Pending_Instantiations processing, when nested instances
12056 -- may precede the one generated from the main unit.
12058 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12059 and then Body_Optional
12060 then
12061 goto Leave;
12062 else
12063 Load_Parent_Of_Generic
12064 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12066 -- Surprisingly enough, loading the body of the parent can cause
12067 -- the body to be instantiated and the double instantiation needs
12068 -- to be prevented in order to avoid giving bogus semantic errors.
12070 -- This case can occur because of the Collect_Previous_Instances
12071 -- machinery of Load_Parent_Of_Generic, which will instantiate
12072 -- bodies that are deemed to be ahead of the body of the parent
12073 -- in the compilation unit. But the relative position of these
12074 -- bodies is computed using the mere comparison of their Sloc.
12076 -- Now suppose that you have two generic packages G and H, with
12077 -- G containing a mere instantiation of H:
12079 -- generic
12080 -- package H is
12082 -- generic
12083 -- package Nested_G is
12084 -- ...
12085 -- end Nested_G;
12087 -- end H;
12089 -- with H;
12091 -- generic
12092 -- package G is
12094 -- package My_H is new H;
12096 -- end G;
12098 -- and a third package Q instantiating G and Nested_G:
12100 -- with G;
12102 -- package Q is
12104 -- package My_G is new G;
12106 -- package My_Nested_G is new My_G.My_H.Nested_G;
12108 -- end Q;
12110 -- The body to be instantiated is that of My_Nested_G and its
12111 -- parent is the instance My_G.My_H. This latter instantiation
12112 -- is done when My_G is analyzed, i.e. after the declarations
12113 -- of My_G and My_Nested_G have been parsed; as a result, the
12114 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12116 -- Therefore loading the body of My_G.My_H will cause the body
12117 -- of My_Nested_G to be instantiated because it is deemed to be
12118 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12119 -- will again be invoked on My_G.My_H, but this time with the
12120 -- Collect_Previous_Instances machinery disabled, so there is
12121 -- no endless mutual recursion and things are done in order.
12123 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12124 goto Leave;
12125 end if;
12127 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12128 end if;
12129 end if;
12131 -- Establish global variable for sloc adjustment and for error recovery
12132 -- In the case of an instance body for an instantiation with actuals
12133 -- from a limited view, the instance body is placed at the beginning
12134 -- of the enclosing package body: use the body entity as the source
12135 -- location for nodes of the instance body.
12137 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12138 declare
12139 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12140 Body_Id : constant Node_Id :=
12141 Corresponding_Body (Unit_Declaration_Node (Scop));
12143 begin
12144 Instantiation_Node := Body_Id;
12145 end;
12146 else
12147 Instantiation_Node := Inst_Node;
12148 end if;
12150 -- The package being instantiated may be subject to pragma Ghost. Set
12151 -- the mode now to ensure that any nodes generated during instantiation
12152 -- are properly marked as Ghost.
12154 Set_Ghost_Mode (Act_Decl_Id);
12156 if Present (Gen_Body_Id) then
12157 Save_Env (Gen_Unit, Act_Decl_Id);
12158 Style_Check := False;
12160 -- If the context of the instance is subject to SPARK_Mode "off", the
12161 -- annotation is missing, or the body is instantiated at a later pass
12162 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12163 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12164 -- instance.
12166 if SPARK_Mode /= On
12167 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12168 then
12169 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12170 end if;
12172 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12173 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12175 Create_Instantiation_Source
12176 (Inst_Node, Gen_Body_Id, S_Adjustment);
12178 Act_Body :=
12179 Copy_Generic_Node
12180 (Original_Node (Gen_Body), Empty, Instantiating => True);
12182 -- Create proper (possibly qualified) defining name for the body, to
12183 -- correspond to the one in the spec.
12185 Act_Body_Id :=
12186 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12187 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12189 -- Some attributes of spec entity are not inherited by body entity
12191 Set_Handler_Records (Act_Body_Id, No_List);
12193 if Nkind (Defining_Unit_Name (Act_Spec)) =
12194 N_Defining_Program_Unit_Name
12195 then
12196 Act_Body_Name :=
12197 Make_Defining_Program_Unit_Name (Loc,
12198 Name =>
12199 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12200 Defining_Identifier => Act_Body_Id);
12201 else
12202 Act_Body_Name := Act_Body_Id;
12203 end if;
12205 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12207 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12208 Check_Generic_Actuals (Act_Decl_Id, False);
12209 Check_Initialized_Types;
12211 -- Install primitives hidden at the point of the instantiation but
12212 -- visible when processing the generic formals
12214 declare
12215 E : Entity_Id;
12217 begin
12218 E := First_Entity (Act_Decl_Id);
12219 while Present (E) loop
12220 if Is_Type (E)
12221 and then not Is_Itype (E)
12222 and then Is_Generic_Actual_Type (E)
12223 and then Is_Tagged_Type (E)
12224 then
12225 Install_Hidden_Primitives
12226 (Prims_List => Vis_Prims_List,
12227 Gen_T => Generic_Parent_Type (Parent (E)),
12228 Act_T => E);
12229 end if;
12231 Next_Entity (E);
12232 end loop;
12233 end;
12235 Scope_Check_Id := Current_Scope;
12236 Scope_Check_Last := Scope_Stack.Last;
12238 -- If the instantiation appears within a generic child some actual
12239 -- parameter may be the current instance of the enclosing generic
12240 -- parent.
12242 declare
12243 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12245 begin
12246 if Is_Child_Unit (Inst_Scope)
12247 and then Ekind (Inst_Scope) = E_Generic_Package
12248 and then Present (Generic_Associations (Inst_Node))
12249 then
12250 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12252 -- Hide them from visibility; required to avoid conflicts
12253 -- installing the parent instance.
12255 if Present (Ctx_Parents) then
12256 Push_Scope (Standard_Standard);
12257 Ctx_Top := Scope_Stack.Last;
12258 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12259 end if;
12260 end if;
12261 end;
12263 -- If it is a child unit, make the parent instance (which is an
12264 -- instance of the parent of the generic) visible.
12266 -- 1) The child unit's parent is an explicit parent instance (the
12267 -- prefix of the name of the generic unit):
12269 -- package Child_Package is new Parent_Instance.Child_Unit;
12271 -- 2) The child unit's parent is an implicit parent instance (e.g.
12272 -- when instantiating a sibling package):
12274 -- generic
12275 -- package Parent.Second_Child is
12276 -- ...
12278 -- generic
12279 -- package Parent.First_Child is
12280 -- package Sibling_Package is new Second_Child;
12282 -- 3) The child unit's parent is not an instance, so the scope is
12283 -- simply the one of the unit.
12285 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12286 and then Nkind (Gen_Id) = N_Expanded_Name
12287 then
12288 Par_Ent := Entity (Prefix (Gen_Id));
12290 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12291 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12292 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12293 and then Nkind
12294 (Name (Get_Unit_Instantiation_Node
12295 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12296 then
12297 Par_Ent := Entity
12298 (Prefix (Name (Get_Unit_Instantiation_Node
12299 (Scope (Act_Decl_Id)))));
12301 elsif Is_Child_Unit (Gen_Unit) then
12302 Par_Ent := Scope (Gen_Unit);
12303 end if;
12305 if Present (Par_Ent) then
12306 Par_Vis := Is_Immediately_Visible (Par_Ent);
12307 Install_Parent (Par_Ent, In_Body => True);
12308 Par_Installed := True;
12309 end if;
12311 -- If the instantiation is a library unit, and this is the main unit,
12312 -- then build the resulting compilation unit nodes for the instance.
12313 -- If this is a compilation unit but it is not the main unit, then it
12314 -- is the body of a unit in the context, that is being compiled
12315 -- because it is encloses some inlined unit or another generic unit
12316 -- being instantiated. In that case, this body is not part of the
12317 -- current compilation, and is not attached to the tree, but its
12318 -- parent must be set for analysis.
12320 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12322 -- Replace instance node with body of instance, and create new
12323 -- node for corresponding instance declaration.
12325 Build_Instance_Compilation_Unit_Nodes
12326 (Inst_Node, Act_Body, Act_Decl);
12328 -- If the instantiation appears within a generic child package
12329 -- enable visibility of current instance of enclosing generic
12330 -- parents.
12332 if Present (Ctx_Parents) then
12333 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12334 Analyze (Inst_Node);
12335 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12336 else
12337 Analyze (Inst_Node);
12338 end if;
12340 if Parent (Inst_Node) = Cunit (Main_Unit) then
12342 -- If the instance is a child unit itself, then set the scope
12343 -- of the expanded body to be the parent of the instantiation
12344 -- (ensuring that the fully qualified name will be generated
12345 -- for the elaboration subprogram).
12347 if Nkind (Defining_Unit_Name (Act_Spec)) =
12348 N_Defining_Program_Unit_Name
12349 then
12350 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12351 end if;
12352 end if;
12354 -- Case where instantiation is not a library unit
12356 else
12357 -- Handle the case of an instance with incomplete actual types.
12358 -- The instance body cannot be placed just after the declaration
12359 -- because full views have not been seen yet. Any use of the non-
12360 -- limited views in the instance body requires the presence of a
12361 -- regular with_clause in the enclosing unit. Therefore we place
12362 -- the instance body at the beginning of the enclosing body, and
12363 -- the freeze node for the instance is then placed after the body.
12365 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12366 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12367 then
12368 declare
12369 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12370 Body_Id : constant Node_Id :=
12371 Corresponding_Body (Unit_Declaration_Node (Scop));
12373 F_Node : Node_Id;
12375 begin
12376 pragma Assert (Present (Body_Id));
12378 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12380 if Expander_Active then
12381 Ensure_Freeze_Node (Act_Decl_Id);
12382 F_Node := Freeze_Node (Act_Decl_Id);
12383 Set_Is_Frozen (Act_Decl_Id, False);
12384 if Is_List_Member (F_Node) then
12385 Remove (F_Node);
12386 end if;
12388 Insert_After (Act_Body, F_Node);
12389 end if;
12390 end;
12392 else
12393 Insert_Before (Inst_Node, Act_Body);
12394 Mark_Rewrite_Insertion (Act_Body);
12396 -- Insert the freeze node for the instance if need be
12398 if Expander_Active then
12399 Freeze_Package_Instance
12400 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12401 Set_Is_Frozen (Act_Decl_Id);
12402 end if;
12403 end if;
12405 -- If the instantiation appears within a generic child package
12406 -- enable visibility of current instance of enclosing generic
12407 -- parents.
12409 if Present (Ctx_Parents) then
12410 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12411 Analyze (Act_Body);
12412 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12413 else
12414 Analyze (Act_Body);
12415 end if;
12416 end if;
12418 Inherit_Context (Gen_Body, Inst_Node);
12420 if Par_Installed then
12421 Remove_Parent (In_Body => True);
12423 -- Restore the previous visibility of the parent
12425 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12426 end if;
12428 -- Remove the parent instances if they have been placed on the scope
12429 -- stack to compile the body.
12431 if Present (Ctx_Parents) then
12432 pragma Assert (Scope_Stack.Last = Ctx_Top
12433 and then Current_Scope = Standard_Standard);
12434 Pop_Scope;
12436 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12437 end if;
12439 pragma Assert (Current_Scope = Scope_Check_Id);
12440 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12442 Restore_Hidden_Primitives (Vis_Prims_List);
12444 -- Restore the private views that were made visible when the body of
12445 -- the instantiation was created. Note that, in the case where one of
12446 -- these private views is declared in the parent, there is a nesting
12447 -- issue with the calls to Install_Parent and Remove_Parent made in
12448 -- between above with In_Body set to True, because these calls also
12449 -- want to swap and restore this private view respectively. In this
12450 -- case, the call to Install_Parent does nothing, but the call to
12451 -- Remove_Parent does restore the private view, thus undercutting the
12452 -- call to Restore_Private_Views. That's OK under the condition that
12453 -- the two mechanisms swap exactly the same entities, in particular
12454 -- the private entities dependent on the primary private entities.
12456 Restore_Private_Views (Act_Decl_Id);
12458 -- Remove the current unit from visibility if this is an instance
12459 -- that is not elaborated on the fly for inlining purposes.
12461 if not Inlined_Body then
12462 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12463 end if;
12465 Restore_Env;
12467 -- If we have no body, and the unit requires a body, then complain. This
12468 -- complaint is suppressed if we have detected other errors (since a
12469 -- common reason for missing the body is that it had errors).
12470 -- In CodePeer mode, a warning has been emitted already, no need for
12471 -- further messages.
12473 elsif Unit_Requires_Body (Gen_Unit)
12474 and then not Body_Optional
12475 then
12476 if CodePeer_Mode then
12477 null;
12479 elsif Serious_Errors_Detected = 0 then
12480 Error_Msg_NE
12481 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12483 -- Don't attempt to perform any cleanup actions if some other error
12484 -- was already detected, since this can cause blowups.
12486 else
12487 goto Leave;
12488 end if;
12490 -- Case of package that does not need a body
12492 else
12493 -- If the instantiation of the declaration is a library unit, rewrite
12494 -- the original package instantiation as a package declaration in the
12495 -- compilation unit node.
12497 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12498 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12499 Rewrite (Inst_Node, Act_Decl);
12501 -- Generate elaboration entity, in case spec has elaboration code.
12502 -- This cannot be done when the instance is analyzed, because it
12503 -- is not known yet whether the body exists.
12505 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12506 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12508 -- If the instantiation is not a library unit, then append the
12509 -- declaration to the list of implicitly generated entities, unless
12510 -- it is already a list member which means that it was already
12511 -- processed
12513 elsif not Is_List_Member (Act_Decl) then
12514 Mark_Rewrite_Insertion (Act_Decl);
12515 Insert_Before (Inst_Node, Act_Decl);
12516 end if;
12517 end if;
12519 <<Leave>>
12521 -- Restore the context that was in effect prior to instantiating the
12522 -- package body.
12524 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12525 Local_Suppress_Stack_Top := Saved_LSST;
12526 Scope_Suppress := Saved_SS;
12527 Style_Check := Saved_SC;
12529 Expander_Mode_Restore;
12530 Restore_Config_Switches (Saved_CS);
12531 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12532 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12533 Restore_Warnings (Saved_Warn);
12534 end Instantiate_Package_Body;
12536 ---------------------------------
12537 -- Instantiate_Subprogram_Body --
12538 ---------------------------------
12540 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12541 -- must be replaced by gotos which jump to the end of the routine in order
12542 -- to restore the Ghost and SPARK modes.
12544 procedure Instantiate_Subprogram_Body
12545 (Body_Info : Pending_Body_Info;
12546 Body_Optional : Boolean := False)
12548 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12549 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12550 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12551 Gen_Id : constant Node_Id := Name (Inst_Node);
12552 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12553 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12554 Loc : constant Source_Ptr := Sloc (Inst_Node);
12555 Pack_Id : constant Entity_Id :=
12556 Defining_Unit_Name (Parent (Act_Decl));
12558 -- The following constants capture the context prior to instantiating
12559 -- the subprogram body.
12561 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12562 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12563 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12564 Saved_ISMP : constant Boolean :=
12565 Ignore_SPARK_Mode_Pragmas_In_Instance;
12566 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12567 Local_Suppress_Stack_Top;
12568 Saved_SC : constant Boolean := Style_Check;
12569 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12570 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12571 Saved_SS : constant Suppress_Record := Scope_Suppress;
12572 Saved_Warn : constant Warnings_State := Save_Warnings;
12574 Act_Body : Node_Id;
12575 Act_Body_Id : Entity_Id;
12576 Gen_Body : Node_Id;
12577 Gen_Body_Id : Node_Id;
12578 Pack_Body : Node_Id;
12579 Par_Ent : Entity_Id := Empty;
12580 Par_Installed : Boolean := False;
12581 Par_Vis : Boolean := False;
12582 Ret_Expr : Node_Id;
12584 begin
12585 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12587 -- Subprogram body may have been created already because of an inline
12588 -- pragma, or because of multiple elaborations of the enclosing package
12589 -- when several instances of the subprogram appear in the main unit.
12591 if Present (Corresponding_Body (Act_Decl)) then
12592 return;
12593 end if;
12595 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12597 -- Re-establish the state of information on which checks are suppressed.
12598 -- This information was set in Body_Info at the point of instantiation,
12599 -- and now we restore it so that the instance is compiled using the
12600 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12602 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12603 Scope_Suppress := Body_Info.Scope_Suppress;
12605 Restore_Config_Switches (Body_Info.Config_Switches);
12606 Restore_Warnings (Body_Info.Warnings);
12608 if No (Gen_Body_Id) then
12610 -- For imported generic subprogram, no body to compile, complete
12611 -- the spec entity appropriately.
12613 if Is_Imported (Gen_Unit) then
12614 Set_Is_Imported (Act_Decl_Id);
12615 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12616 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12617 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12618 Set_Has_Completion (Act_Decl_Id);
12619 goto Leave;
12621 -- For other cases, compile the body
12623 else
12624 Load_Parent_Of_Generic
12625 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12626 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12627 end if;
12628 end if;
12630 Instantiation_Node := Inst_Node;
12632 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12633 -- the mode now to ensure that any nodes generated during instantiation
12634 -- are properly marked as Ghost.
12636 Set_Ghost_Mode (Act_Decl_Id);
12638 if Present (Gen_Body_Id) then
12639 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12641 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12643 -- Either body is not present, or context is non-expanding, as
12644 -- when compiling a subunit. Mark the instance as completed, and
12645 -- diagnose a missing body when needed.
12647 if Expander_Active
12648 and then Operating_Mode = Generate_Code
12649 then
12650 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12651 end if;
12653 Set_Has_Completion (Act_Decl_Id);
12654 goto Leave;
12655 end if;
12657 Save_Env (Gen_Unit, Act_Decl_Id);
12658 Style_Check := False;
12660 -- If the context of the instance is subject to SPARK_Mode "off", the
12661 -- annotation is missing, or the body is instantiated at a later pass
12662 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12663 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12664 -- instance.
12666 if SPARK_Mode /= On
12667 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12668 then
12669 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12670 end if;
12672 -- If the context of an instance is not subject to SPARK_Mode "off",
12673 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12674 -- the latter should be the one applicable to the instance.
12676 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12677 and then SPARK_Mode /= Off
12678 and then Present (SPARK_Pragma (Gen_Body_Id))
12679 then
12680 Set_SPARK_Mode (Gen_Body_Id);
12681 end if;
12683 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12684 Create_Instantiation_Source
12685 (Inst_Node,
12686 Gen_Body_Id,
12687 S_Adjustment);
12689 Act_Body :=
12690 Copy_Generic_Node
12691 (Original_Node (Gen_Body), Empty, Instantiating => True);
12693 -- Create proper defining name for the body, to correspond to the one
12694 -- in the spec.
12696 Act_Body_Id :=
12697 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12699 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12700 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12702 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12703 Set_Has_Completion (Act_Decl_Id);
12704 Check_Generic_Actuals (Pack_Id, False);
12706 -- Generate a reference to link the visible subprogram instance to
12707 -- the generic body, which for navigation purposes is the only
12708 -- available source for the instance.
12710 Generate_Reference
12711 (Related_Instance (Pack_Id),
12712 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12714 -- If it is a child unit, make the parent instance (which is an
12715 -- instance of the parent of the generic) visible. The parent
12716 -- instance is the prefix of the name of the generic unit.
12718 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12719 and then Nkind (Gen_Id) = N_Expanded_Name
12720 then
12721 Par_Ent := Entity (Prefix (Gen_Id));
12722 elsif Is_Child_Unit (Gen_Unit) then
12723 Par_Ent := Scope (Gen_Unit);
12724 end if;
12726 if Present (Par_Ent) then
12727 Par_Vis := Is_Immediately_Visible (Par_Ent);
12728 Install_Parent (Par_Ent, In_Body => True);
12729 Par_Installed := True;
12730 end if;
12732 -- Subprogram body is placed in the body of wrapper package,
12733 -- whose spec contains the subprogram declaration as well as
12734 -- the renaming declarations for the generic parameters.
12736 Pack_Body :=
12737 Make_Package_Body (Loc,
12738 Defining_Unit_Name => New_Copy (Pack_Id),
12739 Declarations => New_List (Act_Body));
12741 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12743 -- If the instantiation is a library unit, then build resulting
12744 -- compilation unit nodes for the instance. The declaration of
12745 -- the enclosing package is the grandparent of the subprogram
12746 -- declaration. First replace the instantiation node as the unit
12747 -- of the corresponding compilation.
12749 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12750 if Parent (Inst_Node) = Cunit (Main_Unit) then
12751 Set_Unit (Parent (Inst_Node), Inst_Node);
12752 Build_Instance_Compilation_Unit_Nodes
12753 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12754 Analyze (Inst_Node);
12755 else
12756 Set_Parent (Pack_Body, Parent (Inst_Node));
12757 Analyze (Pack_Body);
12758 end if;
12760 else
12761 Insert_Before (Inst_Node, Pack_Body);
12762 Mark_Rewrite_Insertion (Pack_Body);
12764 -- Insert the freeze node for the instance if need be
12766 if Expander_Active then
12767 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12768 end if;
12770 Analyze (Pack_Body);
12771 end if;
12773 Inherit_Context (Gen_Body, Inst_Node);
12775 Restore_Private_Views (Pack_Id, False);
12777 if Par_Installed then
12778 Remove_Parent (In_Body => True);
12780 -- Restore the previous visibility of the parent
12782 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12783 end if;
12785 Restore_Env;
12787 -- Body not found. Error was emitted already. If there were no previous
12788 -- errors, this may be an instance whose scope is a premature instance.
12789 -- In that case we must insure that the (legal) program does raise
12790 -- program error if executed. We generate a subprogram body for this
12791 -- purpose.
12793 elsif Serious_Errors_Detected = 0
12794 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12795 then
12796 if Body_Optional then
12797 goto Leave;
12799 elsif Ekind (Act_Decl_Id) = E_Procedure then
12800 Act_Body :=
12801 Make_Subprogram_Body (Loc,
12802 Specification =>
12803 Make_Procedure_Specification (Loc,
12804 Defining_Unit_Name =>
12805 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12806 Parameter_Specifications =>
12807 New_Copy_List
12808 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12810 Declarations => Empty_List,
12811 Handled_Statement_Sequence =>
12812 Make_Handled_Sequence_Of_Statements (Loc,
12813 Statements => New_List (
12814 Make_Raise_Program_Error (Loc,
12815 Reason => PE_Access_Before_Elaboration))));
12817 else
12818 Ret_Expr :=
12819 Make_Raise_Program_Error (Loc,
12820 Reason => PE_Access_Before_Elaboration);
12822 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12823 Set_Analyzed (Ret_Expr);
12825 Act_Body :=
12826 Make_Subprogram_Body (Loc,
12827 Specification =>
12828 Make_Function_Specification (Loc,
12829 Defining_Unit_Name =>
12830 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12831 Parameter_Specifications =>
12832 New_Copy_List
12833 (Parameter_Specifications (Parent (Act_Decl_Id))),
12834 Result_Definition =>
12835 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12837 Declarations => Empty_List,
12838 Handled_Statement_Sequence =>
12839 Make_Handled_Sequence_Of_Statements (Loc,
12840 Statements => New_List (
12841 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12842 end if;
12844 Pack_Body :=
12845 Make_Package_Body (Loc,
12846 Defining_Unit_Name => New_Copy (Pack_Id),
12847 Declarations => New_List (Act_Body));
12849 Insert_After (Inst_Node, Pack_Body);
12850 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12851 Analyze (Pack_Body);
12852 end if;
12854 <<Leave>>
12856 -- Restore the context that was in effect prior to instantiating the
12857 -- subprogram body.
12859 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12860 Local_Suppress_Stack_Top := Saved_LSST;
12861 Scope_Suppress := Saved_SS;
12862 Style_Check := Saved_SC;
12864 Expander_Mode_Restore;
12865 Restore_Config_Switches (Saved_CS);
12866 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12867 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12868 Restore_Warnings (Saved_Warn);
12869 end Instantiate_Subprogram_Body;
12871 ----------------------
12872 -- Instantiate_Type --
12873 ----------------------
12875 function Instantiate_Type
12876 (Formal : Node_Id;
12877 Actual : Node_Id;
12878 Analyzed_Formal : Node_Id;
12879 Actual_Decls : List_Id) return List_Id
12881 A_Gen_T : constant Entity_Id :=
12882 Defining_Identifier (Analyzed_Formal);
12883 Def : constant Node_Id := Formal_Type_Definition (Formal);
12884 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12885 Act_T : Entity_Id;
12886 Ancestor : Entity_Id := Empty;
12887 Decl_Node : Node_Id;
12888 Decl_Nodes : List_Id;
12889 Loc : Source_Ptr;
12890 Subt : Entity_Id;
12892 procedure Check_Shared_Variable_Control_Aspects;
12893 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12894 -- that may be specified for a formal type are obeyed by the actual.
12896 procedure Diagnose_Predicated_Actual;
12897 -- There are a number of constructs in which a discrete type with
12898 -- predicates is illegal, e.g. as an index in an array type declaration.
12899 -- If a generic type is used is such a construct in a generic package
12900 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12901 -- of the generic contract that the actual cannot have predicates.
12903 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12904 -- Check that base types are the same and that the subtypes match
12905 -- statically. Used in several of the validation subprograms for
12906 -- actuals in instantiations.
12908 procedure Validate_Array_Type_Instance;
12909 procedure Validate_Access_Subprogram_Instance;
12910 procedure Validate_Access_Type_Instance;
12911 procedure Validate_Derived_Type_Instance;
12912 procedure Validate_Derived_Interface_Type_Instance;
12913 procedure Validate_Discriminated_Formal_Type;
12914 procedure Validate_Interface_Type_Instance;
12915 procedure Validate_Private_Type_Instance;
12916 procedure Validate_Incomplete_Type_Instance;
12917 -- These procedures perform validation tests for the named case.
12918 -- Validate_Discriminated_Formal_Type is shared by formal private
12919 -- types and Ada 2012 formal incomplete types.
12921 --------------------------------------------
12922 -- Check_Shared_Variable_Control_Aspects --
12923 --------------------------------------------
12925 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12926 -- that may be specified for the formal are obeyed by the actual.
12927 -- If the formal is a derived type the aspect specifications must match.
12928 -- NOTE: AI12-0282 implies that matching of aspects is required between
12929 -- formal and actual in all cases, but this is too restrictive.
12930 -- In particular it violates a language design rule: a limited private
12931 -- indefinite formal can be matched by any actual. The current code
12932 -- reflects an older and more permissive version of RM C.6 (12/5).
12934 procedure Check_Shared_Variable_Control_Aspects is
12935 begin
12936 if Ada_Version >= Ada_2022 then
12937 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12938 Error_Msg_NE
12939 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12941 elsif Is_Derived_Type (A_Gen_T)
12942 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12943 then
12944 Error_Msg_NE
12945 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12946 end if;
12948 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12949 Error_Msg_NE
12950 ("actual for& must have Volatile aspect",
12951 Actual, A_Gen_T);
12953 elsif Is_Derived_Type (A_Gen_T)
12954 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12955 then
12956 Error_Msg_NE
12957 ("actual for& has different Volatile aspect",
12958 Actual, A_Gen_T);
12959 end if;
12961 -- We assume that an array type whose atomic component type
12962 -- is Atomic is equivalent to an array type with the explicit
12963 -- aspect Has_Atomic_Components. This is a reasonable inference
12964 -- from the intent of AI12-0282, and makes it legal to use an
12965 -- actual that does not have the identical aspect as the formal.
12966 -- Ditto for volatile components.
12968 declare
12969 Actual_Atomic_Comp : constant Boolean :=
12970 Has_Atomic_Components (Act_T)
12971 or else (Is_Array_Type (Act_T)
12972 and then Is_Atomic (Component_Type (Act_T)));
12973 begin
12974 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12975 Error_Msg_NE
12976 ("formal and actual for& must agree on atomic components",
12977 Actual, A_Gen_T);
12978 end if;
12979 end;
12981 declare
12982 Actual_Volatile_Comp : constant Boolean :=
12983 Has_Volatile_Components (Act_T)
12984 or else (Is_Array_Type (Act_T)
12985 and then Is_Volatile (Component_Type (Act_T)));
12986 begin
12987 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12988 then
12989 Error_Msg_NE
12990 ("actual for& must have volatile components",
12991 Actual, A_Gen_T);
12992 end if;
12993 end;
12995 -- The following two aspects do not require exact matching,
12996 -- but only one-way agreement. See RM C.6.
12998 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12999 then
13000 Error_Msg_NE
13001 ("actual for& must have Independent aspect specified",
13002 Actual, A_Gen_T);
13003 end if;
13005 if Has_Independent_Components (A_Gen_T)
13006 and then not Has_Independent_Components (Act_T)
13007 then
13008 Error_Msg_NE
13009 ("actual for& must have Independent_Components specified",
13010 Actual, A_Gen_T);
13011 end if;
13012 end if;
13013 end Check_Shared_Variable_Control_Aspects;
13015 ---------------------------------
13016 -- Diagnose_Predicated_Actual --
13017 ---------------------------------
13019 procedure Diagnose_Predicated_Actual is
13020 begin
13021 if No_Predicate_On_Actual (A_Gen_T)
13022 and then Has_Predicates (Act_T)
13023 then
13024 Error_Msg_NE
13025 ("actual for& cannot be a type with predicate",
13026 Instantiation_Node, A_Gen_T);
13028 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13029 and then Has_Predicates (Act_T)
13030 and then not Has_Static_Predicate_Aspect (Act_T)
13031 then
13032 Error_Msg_NE
13033 ("actual for& cannot be a type with a dynamic predicate",
13034 Instantiation_Node, A_Gen_T);
13035 end if;
13036 end Diagnose_Predicated_Actual;
13038 --------------------
13039 -- Subtypes_Match --
13040 --------------------
13042 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13043 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13045 begin
13046 -- Check that the base types, root types (when dealing with class
13047 -- wide types), or designated types (when dealing with anonymous
13048 -- access types) of Gen_T and Act_T are statically matching subtypes.
13050 return ((Base_Type (T) = Act_T
13051 or else Base_Type (T) = Base_Type (Act_T))
13052 and then Subtypes_Statically_Match (T, Act_T))
13054 or else (Is_Class_Wide_Type (Gen_T)
13055 and then Is_Class_Wide_Type (Act_T)
13056 and then Subtypes_Match
13057 (Get_Instance_Of (Root_Type (Gen_T)),
13058 Root_Type (Act_T)))
13060 or else (Is_Anonymous_Access_Type (Gen_T)
13061 and then Ekind (Act_T) = Ekind (Gen_T)
13062 and then Subtypes_Statically_Match
13063 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13064 end Subtypes_Match;
13066 -----------------------------------------
13067 -- Validate_Access_Subprogram_Instance --
13068 -----------------------------------------
13070 procedure Validate_Access_Subprogram_Instance is
13071 begin
13072 if not Is_Access_Type (Act_T)
13073 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13074 then
13075 Error_Msg_NE
13076 ("expect access type in instantiation of &", Actual, Gen_T);
13077 Abandon_Instantiation (Actual);
13078 end if;
13080 -- According to AI05-288, actuals for access_to_subprograms must be
13081 -- subtype conformant with the generic formal. Previous to AI05-288
13082 -- only mode conformance was required.
13084 -- This is a binding interpretation that applies to previous versions
13085 -- of the language, no need to maintain previous weaker checks.
13087 Check_Subtype_Conformant
13088 (Designated_Type (Act_T),
13089 Designated_Type (A_Gen_T),
13090 Actual,
13091 Get_Inst => True);
13093 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13094 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13095 Error_Msg_NE
13096 ("protected access type not allowed for formal &",
13097 Actual, Gen_T);
13098 end if;
13100 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13101 Error_Msg_NE
13102 ("expect protected access type for formal &",
13103 Actual, Gen_T);
13104 end if;
13106 -- If the formal has a specified convention (which in most cases
13107 -- will be StdCall) verify that the actual has the same convention.
13109 if Has_Convention_Pragma (A_Gen_T)
13110 and then Convention (A_Gen_T) /= Convention (Act_T)
13111 then
13112 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13113 Error_Msg_NE
13114 ("actual for formal & must have convention %", Actual, Gen_T);
13115 end if;
13117 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13118 Error_Msg_NE
13119 ("non null exclusion of actual and formal & do not match",
13120 Actual, Gen_T);
13121 end if;
13122 end Validate_Access_Subprogram_Instance;
13124 -----------------------------------
13125 -- Validate_Access_Type_Instance --
13126 -----------------------------------
13128 procedure Validate_Access_Type_Instance is
13129 Desig_Type : constant Entity_Id :=
13130 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13131 Desig_Act : Entity_Id;
13133 begin
13134 if not Is_Access_Type (Act_T) then
13135 Error_Msg_NE
13136 ("expect access type in instantiation of &", Actual, Gen_T);
13137 Abandon_Instantiation (Actual);
13138 end if;
13140 if Is_Access_Constant (A_Gen_T) then
13141 if not Is_Access_Constant (Act_T) then
13142 Error_Msg_N
13143 ("actual type must be access-to-constant type", Actual);
13144 Abandon_Instantiation (Actual);
13145 end if;
13146 else
13147 if Is_Access_Constant (Act_T) then
13148 Error_Msg_N
13149 ("actual type must be access-to-variable type", Actual);
13150 Abandon_Instantiation (Actual);
13152 elsif Ekind (A_Gen_T) = E_General_Access_Type
13153 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13154 then
13155 Error_Msg_N
13156 ("actual must be general access type!", Actual);
13157 Error_Msg_NE -- CODEFIX
13158 ("\add ALL to }!", Actual, Act_T);
13159 Abandon_Instantiation (Actual);
13160 end if;
13161 end if;
13163 -- The designated subtypes, that is to say the subtypes introduced
13164 -- by an access type declaration (and not by a subtype declaration)
13165 -- must match.
13167 Desig_Act := Designated_Type (Base_Type (Act_T));
13169 -- The designated type may have been introduced through a limited_
13170 -- with clause, in which case retrieve the non-limited view. This
13171 -- applies to incomplete types as well as to class-wide types.
13173 if From_Limited_With (Desig_Act) then
13174 Desig_Act := Available_View (Desig_Act);
13175 end if;
13177 if not Subtypes_Match (Desig_Type, Desig_Act) then
13178 Error_Msg_NE
13179 ("designated type of actual does not match that of formal &",
13180 Actual, Gen_T);
13182 if not Predicates_Match (Desig_Type, Desig_Act) then
13183 Error_Msg_N ("\predicates do not match", Actual);
13184 end if;
13186 Abandon_Instantiation (Actual);
13187 end if;
13189 -- Ada 2005: null-exclusion indicators of the two types must agree
13191 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13192 Error_Msg_NE
13193 ("non null exclusion of actual and formal & do not match",
13194 Actual, Gen_T);
13195 end if;
13196 end Validate_Access_Type_Instance;
13198 ----------------------------------
13199 -- Validate_Array_Type_Instance --
13200 ----------------------------------
13202 procedure Validate_Array_Type_Instance is
13203 I1 : Node_Id;
13204 I2 : Node_Id;
13205 T2 : Entity_Id;
13207 function Formal_Dimensions return Nat;
13208 -- Count number of dimensions in array type formal
13210 -----------------------
13211 -- Formal_Dimensions --
13212 -----------------------
13214 function Formal_Dimensions return Nat is
13215 Num : Nat := 0;
13216 Index : Node_Id;
13218 begin
13219 if Nkind (Def) = N_Constrained_Array_Definition then
13220 Index := First (Discrete_Subtype_Definitions (Def));
13221 else
13222 Index := First (Subtype_Marks (Def));
13223 end if;
13225 while Present (Index) loop
13226 Num := Num + 1;
13227 Next (Index);
13228 end loop;
13230 return Num;
13231 end Formal_Dimensions;
13233 -- Start of processing for Validate_Array_Type_Instance
13235 begin
13236 if not Is_Array_Type (Act_T) then
13237 Error_Msg_NE
13238 ("expect array type in instantiation of &", Actual, Gen_T);
13239 Abandon_Instantiation (Actual);
13241 elsif Nkind (Def) = N_Constrained_Array_Definition then
13242 if not (Is_Constrained (Act_T)) then
13243 Error_Msg_NE
13244 ("expect constrained array in instantiation of &",
13245 Actual, Gen_T);
13246 Abandon_Instantiation (Actual);
13247 end if;
13249 else
13250 if Is_Constrained (Act_T) then
13251 Error_Msg_NE
13252 ("expect unconstrained array in instantiation of &",
13253 Actual, Gen_T);
13254 Abandon_Instantiation (Actual);
13255 end if;
13256 end if;
13258 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13259 Error_Msg_NE
13260 ("dimensions of actual do not match formal &", Actual, Gen_T);
13261 Abandon_Instantiation (Actual);
13262 end if;
13264 I1 := First_Index (A_Gen_T);
13265 I2 := First_Index (Act_T);
13266 for J in 1 .. Formal_Dimensions loop
13268 -- If the indexes of the actual were given by a subtype_mark,
13269 -- the index was transformed into a range attribute. Retrieve
13270 -- the original type mark for checking.
13272 if Is_Entity_Name (Original_Node (I2)) then
13273 T2 := Entity (Original_Node (I2));
13274 else
13275 T2 := Etype (I2);
13276 end if;
13278 if not Subtypes_Match
13279 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13280 then
13281 Error_Msg_NE
13282 ("index types of actual do not match those of formal &",
13283 Actual, Gen_T);
13284 Abandon_Instantiation (Actual);
13285 end if;
13287 Next_Index (I1);
13288 Next_Index (I2);
13289 end loop;
13291 -- Check matching subtypes. Note that there are complex visibility
13292 -- issues when the generic is a child unit and some aspect of the
13293 -- generic type is declared in a parent unit of the generic. We do
13294 -- the test to handle this special case only after a direct check
13295 -- for static matching has failed. The case where both the component
13296 -- type and the array type are separate formals, and the component
13297 -- type is a private view may also require special checking in
13298 -- Subtypes_Match. Finally, we assume that a child instance where
13299 -- the component type comes from a formal of a parent instance is
13300 -- correct because the generic was correct. A more precise check
13301 -- seems too complex to install???
13303 if Subtypes_Match
13304 (Component_Type (A_Gen_T), Component_Type (Act_T))
13305 or else
13306 Subtypes_Match
13307 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13308 Component_Type (Act_T))
13309 or else
13310 (not Inside_A_Generic
13311 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13312 then
13313 null;
13314 else
13315 Error_Msg_NE
13316 ("component subtype of actual does not match that of formal &",
13317 Actual, Gen_T);
13318 Abandon_Instantiation (Actual);
13319 end if;
13321 if Has_Aliased_Components (A_Gen_T)
13322 and then not Has_Aliased_Components (Act_T)
13323 then
13324 Error_Msg_NE
13325 ("actual must have aliased components to match formal type &",
13326 Actual, Gen_T);
13327 end if;
13328 end Validate_Array_Type_Instance;
13330 -----------------------------------------------
13331 -- Validate_Derived_Interface_Type_Instance --
13332 -----------------------------------------------
13334 procedure Validate_Derived_Interface_Type_Instance is
13335 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13336 Elmt : Elmt_Id;
13338 begin
13339 -- First apply interface instance checks
13341 Validate_Interface_Type_Instance;
13343 -- Verify that immediate parent interface is an ancestor of
13344 -- the actual.
13346 if Present (Par)
13347 and then not Interface_Present_In_Ancestor (Act_T, Par)
13348 then
13349 Error_Msg_NE
13350 ("interface actual must include progenitor&", Actual, Par);
13351 end if;
13353 -- Now verify that the actual includes all other ancestors of
13354 -- the formal.
13356 Elmt := First_Elmt (Interfaces (A_Gen_T));
13357 while Present (Elmt) loop
13358 if not Interface_Present_In_Ancestor
13359 (Act_T, Get_Instance_Of (Node (Elmt)))
13360 then
13361 Error_Msg_NE
13362 ("interface actual must include progenitor&",
13363 Actual, Node (Elmt));
13364 end if;
13366 Next_Elmt (Elmt);
13367 end loop;
13368 end Validate_Derived_Interface_Type_Instance;
13370 ------------------------------------
13371 -- Validate_Derived_Type_Instance --
13372 ------------------------------------
13374 procedure Validate_Derived_Type_Instance is
13375 Actual_Discr : Entity_Id;
13376 Ancestor_Discr : Entity_Id;
13378 begin
13379 -- Verify that the actual includes the progenitors of the formal,
13380 -- if any. The formal may depend on previous formals and their
13381 -- instance, so we must examine instance of interfaces if present.
13382 -- The actual may be an extension of an interface, in which case
13383 -- it does not appear in the interface list, so this must be
13384 -- checked separately.
13386 if Present (Interface_List (Def)) then
13387 if not Has_Interfaces (Act_T) then
13388 Error_Msg_NE
13389 ("actual must implement all interfaces of formal&",
13390 Actual, A_Gen_T);
13392 else
13393 declare
13394 Act_Iface_List : Elist_Id;
13395 Iface : Node_Id;
13396 Iface_Ent : Entity_Id;
13398 function Instance_Exists (I : Entity_Id) return Boolean;
13399 -- If the interface entity is declared in a generic unit,
13400 -- this can only be legal if we are within an instantiation
13401 -- of a child of that generic. There is currently no
13402 -- mechanism to relate an interface declared within a
13403 -- generic to the corresponding interface in an instance,
13404 -- so we traverse the list of interfaces of the actual,
13405 -- looking for a name match.
13407 ---------------------
13408 -- Instance_Exists --
13409 ---------------------
13411 function Instance_Exists (I : Entity_Id) return Boolean is
13412 Iface_Elmt : Elmt_Id;
13414 begin
13415 Iface_Elmt := First_Elmt (Act_Iface_List);
13416 while Present (Iface_Elmt) loop
13417 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13418 and then Chars (Node (Iface_Elmt)) = Chars (I)
13419 then
13420 return True;
13421 end if;
13423 Next_Elmt (Iface_Elmt);
13424 end loop;
13426 return False;
13427 end Instance_Exists;
13429 begin
13430 Iface := First (Abstract_Interface_List (A_Gen_T));
13431 Collect_Interfaces (Act_T, Act_Iface_List);
13433 while Present (Iface) loop
13434 Iface_Ent := Get_Instance_Of (Entity (Iface));
13436 if Is_Ancestor (Iface_Ent, Act_T)
13437 or else Is_Progenitor (Iface_Ent, Act_T)
13438 then
13439 null;
13441 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13442 and then Instance_Exists (Iface_Ent)
13443 then
13444 null;
13446 else
13447 Error_Msg_Name_1 := Chars (Act_T);
13448 Error_Msg_NE
13449 ("actual% must implement interface&",
13450 Actual, Etype (Iface));
13451 end if;
13453 Next (Iface);
13454 end loop;
13455 end;
13456 end if;
13457 end if;
13459 -- If the parent type in the generic declaration is itself a previous
13460 -- formal type, then it is local to the generic and absent from the
13461 -- analyzed generic definition. In that case the ancestor is the
13462 -- instance of the formal (which must have been instantiated
13463 -- previously), unless the ancestor is itself a formal derived type.
13464 -- In this latter case (which is the subject of Corrigendum 8652/0038
13465 -- (AI-202) the ancestor of the formals is the ancestor of its
13466 -- parent. Otherwise, the analyzed generic carries the parent type.
13467 -- If the parent type is defined in a previous formal package, then
13468 -- the scope of that formal package is that of the generic type
13469 -- itself, and it has already been mapped into the corresponding type
13470 -- in the actual package.
13472 -- Common case: parent type defined outside of the generic
13474 if Is_Entity_Name (Subtype_Mark (Def))
13475 and then Present (Entity (Subtype_Mark (Def)))
13476 then
13477 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13479 -- Check whether parent is defined in a previous formal package
13481 elsif
13482 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13483 then
13484 Ancestor :=
13485 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13487 -- The type may be a local derivation, or a type extension of a
13488 -- previous formal, or of a formal of a parent package.
13490 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13491 or else
13492 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13493 then
13494 -- Check whether the parent is another derived formal type in the
13495 -- same generic unit.
13497 if Etype (A_Gen_T) /= A_Gen_T
13498 and then Is_Generic_Type (Etype (A_Gen_T))
13499 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13500 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13501 then
13502 -- Locate ancestor of parent from the subtype declaration
13503 -- created for the actual.
13505 declare
13506 Decl : Node_Id;
13508 begin
13509 Decl := First (Actual_Decls);
13510 while Present (Decl) loop
13511 if Nkind (Decl) = N_Subtype_Declaration
13512 and then Chars (Defining_Identifier (Decl)) =
13513 Chars (Etype (A_Gen_T))
13514 then
13515 Ancestor := Generic_Parent_Type (Decl);
13516 exit;
13517 else
13518 Next (Decl);
13519 end if;
13520 end loop;
13521 end;
13523 pragma Assert (Present (Ancestor));
13525 -- The ancestor itself may be a previous formal that has been
13526 -- instantiated.
13528 Ancestor := Get_Instance_Of (Ancestor);
13530 else
13531 Ancestor := Get_Instance_Of (Etype (Get_Instance_Of (A_Gen_T)));
13532 end if;
13534 -- Check whether parent is a previous formal of the current generic
13536 elsif Is_Derived_Type (A_Gen_T)
13537 and then Is_Generic_Type (Etype (A_Gen_T))
13538 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13539 then
13540 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13542 -- An unusual case: the actual is a type declared in a parent unit,
13543 -- but is not a formal type so there is no instance_of for it.
13544 -- Retrieve it by analyzing the record extension.
13546 elsif Is_Child_Unit (Scope (A_Gen_T))
13547 and then In_Open_Scopes (Scope (Act_T))
13548 and then Is_Generic_Instance (Scope (Act_T))
13549 then
13550 Analyze (Subtype_Mark (Def));
13551 Ancestor := Entity (Subtype_Mark (Def));
13553 else
13554 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13555 end if;
13557 -- If the formal derived type has pragma Preelaborable_Initialization
13558 -- then the actual type must have preelaborable initialization.
13560 if Known_To_Have_Preelab_Init (A_Gen_T)
13561 and then not Has_Preelaborable_Initialization (Act_T)
13562 then
13563 Error_Msg_NE
13564 ("actual for & must have preelaborable initialization",
13565 Actual, Gen_T);
13566 end if;
13568 -- Ada 2005 (AI-251)
13570 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13571 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13572 Error_Msg_NE
13573 ("(Ada 2005) expected type implementing & in instantiation",
13574 Actual, Ancestor);
13575 end if;
13577 -- Finally verify that the (instance of) the ancestor is an ancestor
13578 -- of the actual.
13580 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13581 Error_Msg_NE
13582 ("expect type derived from & in instantiation",
13583 Actual, First_Subtype (Ancestor));
13584 Abandon_Instantiation (Actual);
13585 end if;
13587 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13588 -- that the formal type declaration has been rewritten as a private
13589 -- extension.
13591 if Ada_Version >= Ada_2005
13592 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13593 and then Synchronized_Present (Parent (A_Gen_T))
13594 then
13595 -- The actual must be a synchronized tagged type
13597 if not Is_Tagged_Type (Act_T) then
13598 Error_Msg_N
13599 ("actual of synchronized type must be tagged", Actual);
13600 Abandon_Instantiation (Actual);
13602 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13603 and then Nkind (Type_Definition (Parent (Act_T))) =
13604 N_Derived_Type_Definition
13605 and then not Synchronized_Present
13606 (Type_Definition (Parent (Act_T)))
13607 then
13608 Error_Msg_N
13609 ("actual of synchronized type must be synchronized", Actual);
13610 Abandon_Instantiation (Actual);
13611 end if;
13612 end if;
13614 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13615 -- removes the second instance of the phrase "or allow pass by copy".
13617 -- For Ada 2022, the aspect may be specified explicitly for the
13618 -- formal regardless of whether an ancestor obeys it.
13620 if Is_Atomic (Act_T)
13621 and then not Is_Atomic (Ancestor)
13622 and then not Is_Atomic (A_Gen_T)
13623 then
13624 Error_Msg_N
13625 ("cannot have atomic actual type for non-atomic formal type",
13626 Actual);
13628 elsif Is_Volatile (Act_T)
13629 and then not Is_Volatile (Ancestor)
13630 and then not Is_Volatile (A_Gen_T)
13631 then
13632 Error_Msg_N
13633 ("cannot have volatile actual type for non-volatile formal type",
13634 Actual);
13635 end if;
13637 -- It should not be necessary to check for unknown discriminants on
13638 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13639 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13640 -- needs fixing. ???
13642 if Is_Definite_Subtype (A_Gen_T)
13643 and then not Unknown_Discriminants_Present (Formal)
13644 and then not Is_Definite_Subtype (Act_T)
13645 then
13646 Error_Msg_N ("actual subtype must be constrained", Actual);
13647 Abandon_Instantiation (Actual);
13648 end if;
13650 if not Unknown_Discriminants_Present (Formal) then
13651 if Is_Constrained (Ancestor) then
13652 if not Is_Constrained (Act_T) then
13653 Error_Msg_N ("actual subtype must be constrained", Actual);
13654 Abandon_Instantiation (Actual);
13655 end if;
13657 -- Ancestor is unconstrained, Check if generic formal and actual
13658 -- agree on constrainedness. The check only applies to array types
13659 -- and discriminated types.
13661 elsif Is_Constrained (Act_T) then
13662 if Ekind (Ancestor) = E_Access_Type
13663 or else (not Is_Constrained (A_Gen_T)
13664 and then Is_Composite_Type (A_Gen_T))
13665 then
13666 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13667 Abandon_Instantiation (Actual);
13668 end if;
13670 -- A class-wide type is only allowed if the formal has unknown
13671 -- discriminants.
13673 elsif Is_Class_Wide_Type (Act_T)
13674 and then not Has_Unknown_Discriminants (Ancestor)
13675 then
13676 Error_Msg_NE
13677 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13678 Abandon_Instantiation (Actual);
13680 -- Otherwise, the formal and actual must have the same number
13681 -- of discriminants and each discriminant of the actual must
13682 -- correspond to a discriminant of the formal.
13684 elsif Has_Discriminants (Act_T)
13685 and then not Has_Unknown_Discriminants (Act_T)
13686 and then Has_Discriminants (Ancestor)
13687 then
13688 Actual_Discr := First_Discriminant (Act_T);
13689 Ancestor_Discr := First_Discriminant (Ancestor);
13690 while Present (Actual_Discr)
13691 and then Present (Ancestor_Discr)
13692 loop
13693 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13694 No (Corresponding_Discriminant (Actual_Discr))
13695 then
13696 Error_Msg_NE
13697 ("discriminant & does not correspond "
13698 & "to ancestor discriminant", Actual, Actual_Discr);
13699 Abandon_Instantiation (Actual);
13700 end if;
13702 Next_Discriminant (Actual_Discr);
13703 Next_Discriminant (Ancestor_Discr);
13704 end loop;
13706 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13707 Error_Msg_NE
13708 ("actual for & must have same number of discriminants",
13709 Actual, Gen_T);
13710 Abandon_Instantiation (Actual);
13711 end if;
13713 -- This case should be caught by the earlier check for
13714 -- constrainedness, but the check here is added for completeness.
13716 elsif Has_Discriminants (Act_T)
13717 and then not Has_Unknown_Discriminants (Act_T)
13718 then
13719 Error_Msg_NE
13720 ("actual for & must not have discriminants", Actual, Gen_T);
13721 Abandon_Instantiation (Actual);
13723 elsif Has_Discriminants (Ancestor) then
13724 Error_Msg_NE
13725 ("actual for & must have known discriminants", Actual, Gen_T);
13726 Abandon_Instantiation (Actual);
13727 end if;
13729 if not Subtypes_Statically_Compatible
13730 (Act_T, Ancestor, Formal_Derived_Matching => True)
13731 then
13732 Error_Msg_NE
13733 ("actual for & must be statically compatible with ancestor",
13734 Actual, Gen_T);
13736 if not Predicates_Compatible (Act_T, Ancestor) then
13737 Error_Msg_N
13738 ("\predicate on actual is not compatible with ancestor",
13739 Actual);
13740 end if;
13742 Abandon_Instantiation (Actual);
13743 end if;
13744 end if;
13746 -- If the formal and actual types are abstract, check that there
13747 -- are no abstract primitives of the actual type that correspond to
13748 -- nonabstract primitives of the formal type (second sentence of
13749 -- RM95 3.9.3(9)).
13751 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13752 Check_Abstract_Primitives : declare
13753 Gen_Prims : constant Elist_Id :=
13754 Primitive_Operations (A_Gen_T);
13755 Gen_Elmt : Elmt_Id;
13756 Gen_Subp : Entity_Id;
13757 Anc_Subp : Entity_Id;
13758 Anc_Formal : Entity_Id;
13759 Anc_F_Type : Entity_Id;
13761 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13762 Act_Elmt : Elmt_Id;
13763 Act_Subp : Entity_Id;
13764 Act_Formal : Entity_Id;
13765 Act_F_Type : Entity_Id;
13767 Subprograms_Correspond : Boolean;
13769 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13770 -- Returns true if T2 is derived directly or indirectly from
13771 -- T1, including derivations from interfaces. T1 and T2 are
13772 -- required to be specific tagged base types.
13774 ------------------------
13775 -- Is_Tagged_Ancestor --
13776 ------------------------
13778 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13780 Intfc_Elmt : Elmt_Id;
13782 begin
13783 -- The predicate is satisfied if the types are the same
13785 if T1 = T2 then
13786 return True;
13788 -- If we've reached the top of the derivation chain then
13789 -- we know that T1 is not an ancestor of T2.
13791 elsif Etype (T2) = T2 then
13792 return False;
13794 -- Proceed to check T2's immediate parent
13796 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13797 return True;
13799 -- Finally, check to see if T1 is an ancestor of any of T2's
13800 -- progenitors.
13802 else
13803 Intfc_Elmt := First_Elmt (Interfaces (T2));
13804 while Present (Intfc_Elmt) loop
13805 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13806 return True;
13807 end if;
13809 Next_Elmt (Intfc_Elmt);
13810 end loop;
13811 end if;
13813 return False;
13814 end Is_Tagged_Ancestor;
13816 -- Start of processing for Check_Abstract_Primitives
13818 begin
13819 -- Loop over all of the formal derived type's primitives
13821 Gen_Elmt := First_Elmt (Gen_Prims);
13822 while Present (Gen_Elmt) loop
13823 Gen_Subp := Node (Gen_Elmt);
13825 -- If the primitive of the formal is not abstract, then
13826 -- determine whether there is a corresponding primitive of
13827 -- the actual type that's abstract.
13829 if not Is_Abstract_Subprogram (Gen_Subp) then
13830 Act_Elmt := First_Elmt (Act_Prims);
13831 while Present (Act_Elmt) loop
13832 Act_Subp := Node (Act_Elmt);
13834 -- If we find an abstract primitive of the actual,
13835 -- then we need to test whether it corresponds to the
13836 -- subprogram from which the generic formal primitive
13837 -- is inherited.
13839 if Is_Abstract_Subprogram (Act_Subp) then
13840 Anc_Subp := Alias (Gen_Subp);
13842 -- Test whether we have a corresponding primitive
13843 -- by comparing names, kinds, formal types, and
13844 -- result types.
13846 if Chars (Anc_Subp) = Chars (Act_Subp)
13847 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13848 then
13849 Anc_Formal := First_Formal (Anc_Subp);
13850 Act_Formal := First_Formal (Act_Subp);
13851 while Present (Anc_Formal)
13852 and then Present (Act_Formal)
13853 loop
13854 Anc_F_Type := Etype (Anc_Formal);
13855 Act_F_Type := Etype (Act_Formal);
13857 if Ekind (Anc_F_Type) =
13858 E_Anonymous_Access_Type
13859 then
13860 Anc_F_Type := Designated_Type (Anc_F_Type);
13862 if Ekind (Act_F_Type) =
13863 E_Anonymous_Access_Type
13864 then
13865 Act_F_Type :=
13866 Designated_Type (Act_F_Type);
13867 else
13868 exit;
13869 end if;
13871 elsif
13872 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13873 then
13874 exit;
13875 end if;
13877 Anc_F_Type := Base_Type (Anc_F_Type);
13878 Act_F_Type := Base_Type (Act_F_Type);
13880 -- If the formal is controlling, then the
13881 -- the type of the actual primitive's formal
13882 -- must be derived directly or indirectly
13883 -- from the type of the ancestor primitive's
13884 -- formal.
13886 if Is_Controlling_Formal (Anc_Formal) then
13887 if not Is_Tagged_Ancestor
13888 (Anc_F_Type, Act_F_Type)
13889 then
13890 exit;
13891 end if;
13893 -- Otherwise the types of the formals must
13894 -- be the same.
13896 elsif Anc_F_Type /= Act_F_Type then
13897 exit;
13898 end if;
13900 Next_Formal (Anc_Formal);
13901 Next_Formal (Act_Formal);
13902 end loop;
13904 -- If we traversed through all of the formals
13905 -- then so far the subprograms correspond, so
13906 -- now check that any result types correspond.
13908 if No (Anc_Formal) and then No (Act_Formal) then
13909 Subprograms_Correspond := True;
13911 if Ekind (Act_Subp) = E_Function then
13912 Anc_F_Type := Etype (Anc_Subp);
13913 Act_F_Type := Etype (Act_Subp);
13915 if Ekind (Anc_F_Type) =
13916 E_Anonymous_Access_Type
13917 then
13918 Anc_F_Type :=
13919 Designated_Type (Anc_F_Type);
13921 if Ekind (Act_F_Type) =
13922 E_Anonymous_Access_Type
13923 then
13924 Act_F_Type :=
13925 Designated_Type (Act_F_Type);
13926 else
13927 Subprograms_Correspond := False;
13928 end if;
13930 elsif
13931 Ekind (Act_F_Type)
13932 = E_Anonymous_Access_Type
13933 then
13934 Subprograms_Correspond := False;
13935 end if;
13937 Anc_F_Type := Base_Type (Anc_F_Type);
13938 Act_F_Type := Base_Type (Act_F_Type);
13940 -- Now either the result types must be
13941 -- the same or, if the result type is
13942 -- controlling, the result type of the
13943 -- actual primitive must descend from the
13944 -- result type of the ancestor primitive.
13946 if Subprograms_Correspond
13947 and then Anc_F_Type /= Act_F_Type
13948 and then
13949 Has_Controlling_Result (Anc_Subp)
13950 and then not Is_Tagged_Ancestor
13951 (Anc_F_Type, Act_F_Type)
13952 then
13953 Subprograms_Correspond := False;
13954 end if;
13955 end if;
13957 -- Found a matching subprogram belonging to
13958 -- formal ancestor type, so actual subprogram
13959 -- corresponds and this violates 3.9.3(9).
13961 if Subprograms_Correspond then
13962 Error_Msg_NE
13963 ("abstract subprogram & overrides "
13964 & "nonabstract subprogram of ancestor",
13965 Actual, Act_Subp);
13966 end if;
13967 end if;
13968 end if;
13969 end if;
13971 Next_Elmt (Act_Elmt);
13972 end loop;
13973 end if;
13975 Next_Elmt (Gen_Elmt);
13976 end loop;
13977 end Check_Abstract_Primitives;
13978 end if;
13980 -- Verify that limitedness matches. If parent is a limited
13981 -- interface then the generic formal is not unless declared
13982 -- explicitly so. If not declared limited, the actual cannot be
13983 -- limited (see AI05-0087).
13985 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13986 if not In_Instance then
13987 Error_Msg_NE
13988 ("actual for non-limited & cannot be a limited type",
13989 Actual, Gen_T);
13990 Explain_Limited_Type (Act_T, Actual);
13991 Abandon_Instantiation (Actual);
13992 end if;
13993 end if;
13995 -- Check for AI12-0036
13997 declare
13998 Formal_Is_Private_Extension : constant Boolean :=
13999 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
14001 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
14003 begin
14004 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
14005 if not In_Instance then
14006 if Actual_Is_Tagged then
14007 Error_Msg_NE
14008 ("actual for & cannot be a tagged type", Actual, Gen_T);
14009 else
14010 Error_Msg_NE
14011 ("actual for & must be a tagged type", Actual, Gen_T);
14012 end if;
14014 Abandon_Instantiation (Actual);
14015 end if;
14016 end if;
14017 end;
14018 end Validate_Derived_Type_Instance;
14020 ----------------------------------------
14021 -- Validate_Discriminated_Formal_Type --
14022 ----------------------------------------
14024 procedure Validate_Discriminated_Formal_Type is
14025 Formal_Discr : Entity_Id;
14026 Actual_Discr : Entity_Id;
14027 Formal_Subt : Entity_Id;
14029 begin
14030 if Has_Discriminants (A_Gen_T) then
14031 if not Has_Discriminants (Act_T) then
14032 Error_Msg_NE
14033 ("actual for & must have discriminants", Actual, Gen_T);
14034 Abandon_Instantiation (Actual);
14036 elsif Is_Constrained (Act_T) then
14037 Error_Msg_NE
14038 ("actual for & must be unconstrained", Actual, Gen_T);
14039 Abandon_Instantiation (Actual);
14041 else
14042 Formal_Discr := First_Discriminant (A_Gen_T);
14043 Actual_Discr := First_Discriminant (Act_T);
14044 while Formal_Discr /= Empty loop
14045 if Actual_Discr = Empty then
14046 Error_Msg_N
14047 ("discriminants on actual do not match formal",
14048 Actual);
14049 Abandon_Instantiation (Actual);
14050 end if;
14052 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14054 -- Access discriminants match if designated types do
14056 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14057 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14058 E_Anonymous_Access_Type
14059 and then
14060 Subtypes_Match
14061 (Get_Instance_Of
14062 (Designated_Type (Base_Type (Formal_Subt))),
14063 Designated_Type (Base_Type (Etype (Actual_Discr))))
14064 then
14065 null;
14067 elsif Base_Type (Formal_Subt) /=
14068 Base_Type (Etype (Actual_Discr))
14069 then
14070 Error_Msg_N
14071 ("types of actual discriminants must match formal",
14072 Actual);
14073 Abandon_Instantiation (Actual);
14075 elsif not Subtypes_Statically_Match
14076 (Formal_Subt, Etype (Actual_Discr))
14077 and then Ada_Version >= Ada_95
14078 then
14079 Error_Msg_N
14080 ("subtypes of actual discriminants must match formal",
14081 Actual);
14082 Abandon_Instantiation (Actual);
14083 end if;
14085 Next_Discriminant (Formal_Discr);
14086 Next_Discriminant (Actual_Discr);
14087 end loop;
14089 if Actual_Discr /= Empty then
14090 Error_Msg_NE
14091 ("discriminants on actual do not match formal",
14092 Actual, Gen_T);
14093 Abandon_Instantiation (Actual);
14094 end if;
14095 end if;
14096 end if;
14097 end Validate_Discriminated_Formal_Type;
14099 ---------------------------------------
14100 -- Validate_Incomplete_Type_Instance --
14101 ---------------------------------------
14103 procedure Validate_Incomplete_Type_Instance is
14104 begin
14105 if not Is_Tagged_Type (Act_T)
14106 and then Is_Tagged_Type (A_Gen_T)
14107 then
14108 Error_Msg_NE
14109 ("actual for & must be a tagged type", Actual, Gen_T);
14110 end if;
14112 Validate_Discriminated_Formal_Type;
14113 end Validate_Incomplete_Type_Instance;
14115 --------------------------------------
14116 -- Validate_Interface_Type_Instance --
14117 --------------------------------------
14119 procedure Validate_Interface_Type_Instance is
14120 begin
14121 if not Is_Interface (Act_T) then
14122 Error_Msg_NE
14123 ("actual for formal interface type must be an interface",
14124 Actual, Gen_T);
14126 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14127 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14128 or else Is_Protected_Interface (A_Gen_T) /=
14129 Is_Protected_Interface (Act_T)
14130 or else Is_Synchronized_Interface (A_Gen_T) /=
14131 Is_Synchronized_Interface (Act_T)
14132 then
14133 Error_Msg_NE
14134 ("actual for interface& does not match (RM 12.5.5(4))",
14135 Actual, Gen_T);
14136 end if;
14137 end Validate_Interface_Type_Instance;
14139 ------------------------------------
14140 -- Validate_Private_Type_Instance --
14141 ------------------------------------
14143 procedure Validate_Private_Type_Instance is
14144 begin
14145 if Is_Limited_Type (Act_T)
14146 and then not Is_Limited_Type (A_Gen_T)
14147 then
14148 if In_Instance then
14149 null;
14150 else
14151 Error_Msg_NE
14152 ("actual for non-limited & cannot be a limited type", Actual,
14153 Gen_T);
14154 Explain_Limited_Type (Act_T, Actual);
14155 Abandon_Instantiation (Actual);
14156 end if;
14158 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14159 and then not Has_Preelaborable_Initialization (Act_T)
14160 then
14161 Error_Msg_NE
14162 ("actual for & must have preelaborable initialization", Actual,
14163 Gen_T);
14165 elsif not Is_Definite_Subtype (Act_T)
14166 and then Is_Definite_Subtype (A_Gen_T)
14167 and then Ada_Version >= Ada_95
14168 then
14169 Error_Msg_NE
14170 ("actual for & must be a definite subtype", Actual, Gen_T);
14172 elsif not Is_Tagged_Type (Act_T)
14173 and then Is_Tagged_Type (A_Gen_T)
14174 then
14175 Error_Msg_NE
14176 ("actual for & must be a tagged type", Actual, Gen_T);
14177 end if;
14179 Validate_Discriminated_Formal_Type;
14180 Ancestor := Gen_T;
14181 end Validate_Private_Type_Instance;
14183 -- Start of processing for Instantiate_Type
14185 begin
14186 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14187 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14188 return New_List (Error);
14189 end if;
14191 if not Is_Entity_Name (Actual)
14192 or else not Is_Type (Entity (Actual))
14193 then
14194 Error_Msg_NE
14195 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14196 Abandon_Instantiation (Actual);
14197 end if;
14199 Act_T := Entity (Actual);
14201 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14202 -- as a generic actual parameter if the corresponding formal type
14203 -- does not have a known_discriminant_part, or is a formal derived
14204 -- type that is an Unchecked_Union type.
14206 if Is_Unchecked_Union (Base_Type (Act_T)) then
14207 if not Has_Discriminants (A_Gen_T)
14208 or else (Is_Derived_Type (A_Gen_T)
14209 and then Is_Unchecked_Union (A_Gen_T))
14210 then
14211 null;
14212 else
14213 Error_Msg_N ("unchecked union cannot be the actual for a "
14214 & "discriminated formal type", Act_T);
14216 end if;
14217 end if;
14219 -- Deal with fixed/floating restrictions
14221 if Is_Floating_Point_Type (Act_T) then
14222 Check_Restriction (No_Floating_Point, Actual);
14223 elsif Is_Fixed_Point_Type (Act_T) then
14224 Check_Restriction (No_Fixed_Point, Actual);
14225 end if;
14227 -- Deal with error of using incomplete type as generic actual.
14228 -- This includes limited views of a type, even if the non-limited
14229 -- view may be available.
14231 if Ekind (Act_T) = E_Incomplete_Type
14232 or else (Is_Class_Wide_Type (Act_T)
14233 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14234 then
14235 -- If the formal is an incomplete type, the actual can be
14236 -- incomplete as well, but if an actual incomplete type has
14237 -- a full view, then we'll retrieve that.
14239 if Ekind (A_Gen_T) = E_Incomplete_Type
14240 and then No (Full_View (Act_T))
14241 then
14242 null;
14244 elsif Is_Class_Wide_Type (Act_T)
14245 or else No (Full_View (Act_T))
14246 then
14247 Error_Msg_N ("premature use of incomplete type", Actual);
14248 Abandon_Instantiation (Actual);
14250 else
14251 Act_T := Full_View (Act_T);
14252 Set_Entity (Actual, Act_T);
14254 if Has_Private_Component (Act_T) then
14255 Error_Msg_N
14256 ("premature use of type with private component", Actual);
14257 end if;
14258 end if;
14260 -- Deal with error of premature use of private type as generic actual,
14261 -- which is allowed for incomplete formals.
14263 elsif Ekind (A_Gen_T) /= E_Incomplete_Type then
14264 if Is_Private_Type (Act_T)
14265 and then Is_Private_Type (Base_Type (Act_T))
14266 and then not Is_Generic_Type (Act_T)
14267 and then not Is_Derived_Type (Act_T)
14268 and then No (Full_View (Root_Type (Act_T)))
14269 then
14270 Error_Msg_N ("premature use of private type", Actual);
14272 elsif Has_Private_Component (Act_T) then
14273 Error_Msg_N
14274 ("premature use of type with private component", Actual);
14275 end if;
14276 end if;
14278 Set_Instance_Of (A_Gen_T, Act_T);
14280 -- If the type is generic, the class-wide type may also be used
14282 if Is_Tagged_Type (A_Gen_T)
14283 and then Is_Tagged_Type (Act_T)
14284 and then not Is_Class_Wide_Type (A_Gen_T)
14285 then
14286 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14287 Class_Wide_Type (Act_T));
14288 end if;
14290 if not Is_Abstract_Type (A_Gen_T)
14291 and then Is_Abstract_Type (Act_T)
14292 then
14293 Error_Msg_N
14294 ("actual of non-abstract formal cannot be abstract", Actual);
14295 end if;
14297 -- A generic scalar type is a first subtype for which we generate
14298 -- an anonymous base type. Indicate that the instance of this base
14299 -- is the base type of the actual.
14301 if Is_Scalar_Type (A_Gen_T) then
14302 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14303 end if;
14305 Check_Shared_Variable_Control_Aspects;
14307 if Error_Posted (Act_T) then
14308 null;
14309 else
14310 case Nkind (Def) is
14311 when N_Formal_Private_Type_Definition =>
14312 Validate_Private_Type_Instance;
14314 when N_Formal_Incomplete_Type_Definition =>
14315 Validate_Incomplete_Type_Instance;
14317 when N_Formal_Derived_Type_Definition =>
14318 Validate_Derived_Type_Instance;
14320 when N_Formal_Discrete_Type_Definition =>
14321 if not Is_Discrete_Type (Act_T) then
14322 Error_Msg_NE
14323 ("expect discrete type in instantiation of&",
14324 Actual, Gen_T);
14325 Abandon_Instantiation (Actual);
14326 end if;
14328 Diagnose_Predicated_Actual;
14330 when N_Formal_Signed_Integer_Type_Definition =>
14331 if not Is_Signed_Integer_Type (Act_T) then
14332 Error_Msg_NE
14333 ("expect signed integer type in instantiation of&",
14334 Actual, Gen_T);
14335 Abandon_Instantiation (Actual);
14336 end if;
14338 Diagnose_Predicated_Actual;
14340 when N_Formal_Modular_Type_Definition =>
14341 if not Is_Modular_Integer_Type (Act_T) then
14342 Error_Msg_NE
14343 ("expect modular type in instantiation of &",
14344 Actual, Gen_T);
14345 Abandon_Instantiation (Actual);
14346 end if;
14348 Diagnose_Predicated_Actual;
14350 when N_Formal_Floating_Point_Definition =>
14351 if not Is_Floating_Point_Type (Act_T) then
14352 Error_Msg_NE
14353 ("expect float type in instantiation of &", Actual, Gen_T);
14354 Abandon_Instantiation (Actual);
14355 end if;
14357 when N_Formal_Ordinary_Fixed_Point_Definition =>
14358 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14359 Error_Msg_NE
14360 ("expect ordinary fixed point type in instantiation of &",
14361 Actual, Gen_T);
14362 Abandon_Instantiation (Actual);
14363 end if;
14365 when N_Formal_Decimal_Fixed_Point_Definition =>
14366 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14367 Error_Msg_NE
14368 ("expect decimal type in instantiation of &",
14369 Actual, Gen_T);
14370 Abandon_Instantiation (Actual);
14371 end if;
14373 when N_Array_Type_Definition =>
14374 Validate_Array_Type_Instance;
14376 when N_Access_To_Object_Definition =>
14377 Validate_Access_Type_Instance;
14379 when N_Access_Function_Definition
14380 | N_Access_Procedure_Definition
14382 Validate_Access_Subprogram_Instance;
14384 when N_Record_Definition =>
14385 Validate_Interface_Type_Instance;
14387 when N_Derived_Type_Definition =>
14388 Validate_Derived_Interface_Type_Instance;
14390 when others =>
14391 raise Program_Error;
14392 end case;
14393 end if;
14395 Subt := New_Copy (Gen_T);
14397 -- Use adjusted sloc of subtype name as the location for other nodes in
14398 -- the subtype declaration.
14400 Loc := Sloc (Subt);
14402 Decl_Node :=
14403 Make_Subtype_Declaration (Loc,
14404 Defining_Identifier => Subt,
14405 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14407 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14409 -- Record whether the actual is private at this point, so that
14410 -- Check_Generic_Actuals can restore its proper view before the
14411 -- semantic analysis of the instance.
14413 if Is_Private_Type (Act_T) then
14414 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14416 elsif (Is_Access_Type (Act_T)
14417 and then Is_Private_Type (Designated_Type (Act_T)))
14418 or else (Is_Array_Type (Act_T)
14419 and then
14420 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
14421 then
14422 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
14423 end if;
14425 -- In Ada 2012 the actual may be a limited view. Indicate that
14426 -- the local subtype must be treated as such.
14428 if From_Limited_With (Act_T) then
14429 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14430 Set_From_Limited_With (Subt);
14431 end if;
14433 Decl_Nodes := New_List (Decl_Node);
14435 -- Flag actual derived types so their elaboration produces the
14436 -- appropriate renamings for the primitive operations of the ancestor.
14437 -- Flag actual for formal private types as well, to determine whether
14438 -- operations in the private part may override inherited operations.
14439 -- If the formal has an interface list, the ancestor is not the
14440 -- parent, but the analyzed formal that includes the interface
14441 -- operations of all its progenitors.
14443 -- Same treatment for formal private types, so we can check whether the
14444 -- type is tagged limited when validating derivations in the private
14445 -- part. (See AI05-096).
14447 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14448 if Present (Interface_List (Def)) then
14449 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14450 else
14451 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14452 end if;
14454 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14455 | N_Formal_Incomplete_Type_Definition
14456 then
14457 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14458 end if;
14460 -- If the actual is a synchronized type that implements an interface,
14461 -- the primitive operations are attached to the corresponding record,
14462 -- and we have to treat it as an additional generic actual, so that its
14463 -- primitive operations become visible in the instance. The task or
14464 -- protected type itself does not carry primitive operations.
14466 if Is_Concurrent_Type (Act_T)
14467 and then Is_Tagged_Type (Act_T)
14468 and then Present (Corresponding_Record_Type (Act_T))
14469 and then Present (Ancestor)
14470 and then Is_Interface (Ancestor)
14471 then
14472 declare
14473 Corr_Rec : constant Entity_Id :=
14474 Corresponding_Record_Type (Act_T);
14475 New_Corr : Entity_Id;
14476 Corr_Decl : Node_Id;
14478 begin
14479 New_Corr := Make_Temporary (Loc, 'S');
14480 Corr_Decl :=
14481 Make_Subtype_Declaration (Loc,
14482 Defining_Identifier => New_Corr,
14483 Subtype_Indication =>
14484 New_Occurrence_Of (Corr_Rec, Loc));
14485 Append_To (Decl_Nodes, Corr_Decl);
14487 if Ekind (Act_T) = E_Task_Type then
14488 Mutate_Ekind (Subt, E_Task_Subtype);
14489 else
14490 Mutate_Ekind (Subt, E_Protected_Subtype);
14491 end if;
14493 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14494 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14495 Set_Generic_Parent_Type (Decl_Node, Empty);
14496 end;
14497 end if;
14499 -- For a floating-point type, capture dimension info if any, because
14500 -- the generated subtype declaration does not come from source and
14501 -- will not process dimensions.
14503 if Is_Floating_Point_Type (Act_T) then
14504 Copy_Dimensions (Act_T, Subt);
14505 end if;
14507 return Decl_Nodes;
14508 end Instantiate_Type;
14510 -----------------------------
14511 -- Is_Abbreviated_Instance --
14512 -----------------------------
14514 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14515 begin
14516 return Ekind (E) = E_Package
14517 and then Present (Hidden_In_Formal_Instance (E));
14518 end Is_Abbreviated_Instance;
14520 ---------------------
14521 -- Is_In_Main_Unit --
14522 ---------------------
14524 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14525 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14526 Current_Unit : Node_Id;
14528 begin
14529 if Unum = Main_Unit then
14530 return True;
14532 -- If the current unit is a subunit then it is either the main unit or
14533 -- is being compiled as part of the main unit.
14535 elsif Nkind (N) = N_Compilation_Unit then
14536 return Nkind (Unit (N)) = N_Subunit;
14537 end if;
14539 Current_Unit := Parent (N);
14540 while Present (Current_Unit)
14541 and then Nkind (Current_Unit) /= N_Compilation_Unit
14542 loop
14543 Current_Unit := Parent (Current_Unit);
14544 end loop;
14546 -- The instantiation node is in the main unit, or else the current node
14547 -- (perhaps as the result of nested instantiations) is in the main unit,
14548 -- or in the declaration of the main unit, which in this last case must
14549 -- be a body.
14551 return
14552 Current_Unit = Cunit (Main_Unit)
14553 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14554 or else (Present (Current_Unit)
14555 and then Present (Library_Unit (Current_Unit))
14556 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14557 end Is_In_Main_Unit;
14559 ----------------------------
14560 -- Load_Parent_Of_Generic --
14561 ----------------------------
14563 procedure Load_Parent_Of_Generic
14564 (N : Node_Id;
14565 Spec : Node_Id;
14566 Body_Optional : Boolean := False)
14568 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14569 Saved_Style_Check : constant Boolean := Style_Check;
14570 Saved_Warn : constant Warnings_State := Save_Warnings;
14571 True_Parent : Node_Id;
14572 Inst_Node : Node_Id;
14573 OK : Boolean;
14574 Previous_Instances : constant Elist_Id := New_Elmt_List;
14576 procedure Collect_Previous_Instances (Decls : List_Id);
14577 -- Collect all instantiations in the given list of declarations, that
14578 -- precede the generic that we need to load. If the bodies of these
14579 -- instantiations are available, we must analyze them, to ensure that
14580 -- the public symbols generated are the same when the unit is compiled
14581 -- to generate code, and when it is compiled in the context of a unit
14582 -- that needs a particular nested instance. This process is applied to
14583 -- both package and subprogram instances.
14585 --------------------------------
14586 -- Collect_Previous_Instances --
14587 --------------------------------
14589 procedure Collect_Previous_Instances (Decls : List_Id) is
14590 Decl : Node_Id;
14592 begin
14593 Decl := First (Decls);
14594 while Present (Decl) loop
14595 if Sloc (Decl) >= Sloc (Inst_Node) then
14596 return;
14598 -- If Decl is an instantiation, then record it as requiring
14599 -- instantiation of the corresponding body, except if it is an
14600 -- abbreviated instantiation generated internally for conformance
14601 -- checking purposes only for the case of a formal package
14602 -- declared without a box (see Instantiate_Formal_Package). Such
14603 -- an instantiation does not generate any code (the actual code
14604 -- comes from actual) and thus does not need to be analyzed here.
14605 -- If the instantiation appears with a generic package body it is
14606 -- not analyzed here either.
14608 elsif Nkind (Decl) = N_Package_Instantiation
14609 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14610 then
14611 Append_Elmt (Decl, Previous_Instances);
14613 -- For a subprogram instantiation, omit instantiations intrinsic
14614 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14616 elsif Nkind (Decl) in N_Function_Instantiation
14617 | N_Procedure_Instantiation
14618 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14619 then
14620 Append_Elmt (Decl, Previous_Instances);
14622 elsif Nkind (Decl) = N_Package_Declaration then
14623 Collect_Previous_Instances
14624 (Visible_Declarations (Specification (Decl)));
14625 Collect_Previous_Instances
14626 (Private_Declarations (Specification (Decl)));
14628 -- Previous non-generic bodies may contain instances as well
14630 elsif Nkind (Decl) = N_Package_Body
14631 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14632 then
14633 Collect_Previous_Instances (Declarations (Decl));
14635 elsif Nkind (Decl) = N_Subprogram_Body
14636 and then not Acts_As_Spec (Decl)
14637 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14638 then
14639 Collect_Previous_Instances (Declarations (Decl));
14640 end if;
14642 Next (Decl);
14643 end loop;
14644 end Collect_Previous_Instances;
14646 -- Start of processing for Load_Parent_Of_Generic
14648 begin
14649 if not In_Same_Source_Unit (N, Spec)
14650 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14651 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14652 and then not Is_In_Main_Unit (Spec))
14653 then
14654 -- Find body of parent of spec, and analyze it. A special case arises
14655 -- when the parent is an instantiation, that is to say when we are
14656 -- currently instantiating a nested generic. In that case, there is
14657 -- no separate file for the body of the enclosing instance. Instead,
14658 -- the enclosing body must be instantiated as if it were a pending
14659 -- instantiation, in order to produce the body for the nested generic
14660 -- we require now. Note that in that case the generic may be defined
14661 -- in a package body, the instance defined in the same package body,
14662 -- and the original enclosing body may not be in the main unit.
14664 Inst_Node := Empty;
14666 True_Parent := Parent (Spec);
14667 while Present (True_Parent)
14668 and then Nkind (True_Parent) /= N_Compilation_Unit
14669 loop
14670 if Nkind (True_Parent) = N_Package_Declaration
14671 and then
14672 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14673 then
14674 -- Parent is a compilation unit that is an instantiation, and
14675 -- instantiation node has been replaced with package decl.
14677 Inst_Node := Original_Node (True_Parent);
14678 exit;
14680 elsif Nkind (True_Parent) = N_Package_Declaration
14681 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14682 and then
14683 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14684 then
14685 -- Parent is a compilation unit that is an instantiation, but
14686 -- instantiation node has not been replaced with package decl.
14688 Inst_Node := Unit (Parent (True_Parent));
14689 exit;
14691 elsif Nkind (True_Parent) = N_Package_Declaration
14692 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14693 and then Present (Generic_Parent (Specification (True_Parent)))
14694 then
14695 -- Parent is an instantiation within another specification.
14696 -- Declaration for instance has been inserted before original
14697 -- instantiation node. A direct link would be preferable?
14699 Inst_Node := Next (True_Parent);
14700 while Present (Inst_Node)
14701 and then Nkind (Inst_Node) /= N_Package_Instantiation
14702 loop
14703 Next (Inst_Node);
14704 end loop;
14706 -- If the instance appears within a generic, and the generic
14707 -- unit is defined within a formal package of the enclosing
14708 -- generic, there is no generic body available, and none
14709 -- needed. A more precise test should be used ???
14711 if No (Inst_Node) then
14712 return;
14713 end if;
14715 exit;
14717 -- If an ancestor of the generic comes from a formal package
14718 -- there is no source for the ancestor body. This is detected
14719 -- by examining the scope of the ancestor and its declaration.
14720 -- The body, if any is needed, will be available when the
14721 -- current unit (containing a formal package) is instantiated.
14723 elsif Nkind (True_Parent) = N_Package_Specification
14724 and then Present (Generic_Parent (True_Parent))
14725 and then Nkind
14726 (Original_Node (Unit_Declaration_Node
14727 (Scope (Generic_Parent (True_Parent)))))
14728 = N_Formal_Package_Declaration
14729 then
14730 return;
14732 else
14733 True_Parent := Parent (True_Parent);
14734 end if;
14735 end loop;
14737 -- Case where we are currently instantiating a nested generic
14739 if Present (Inst_Node) then
14740 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14742 -- Instantiation node and declaration of instantiated package
14743 -- were exchanged when only the declaration was needed.
14744 -- Restore instantiation node before proceeding with body.
14746 Set_Unit (Parent (True_Parent), Inst_Node);
14747 end if;
14749 -- Now complete instantiation of enclosing body, if it appears in
14750 -- some other unit. If it appears in the current unit, the body
14751 -- will have been instantiated already.
14753 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14755 -- We need to determine the expander mode to instantiate the
14756 -- enclosing body. Because the generic body we need may use
14757 -- global entities declared in the enclosing package (including
14758 -- aggregates) it is in general necessary to compile this body
14759 -- with expansion enabled, except if we are within a generic
14760 -- package, in which case the usual generic rule applies.
14762 declare
14763 Exp_Status : Boolean := True;
14764 Scop : Entity_Id;
14766 begin
14767 -- Loop through scopes looking for generic package
14769 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14770 while Present (Scop)
14771 and then Scop /= Standard_Standard
14772 loop
14773 if Ekind (Scop) = E_Generic_Package then
14774 Exp_Status := False;
14775 exit;
14776 end if;
14778 Scop := Scope (Scop);
14779 end loop;
14781 -- Collect previous instantiations in the unit that contains
14782 -- the desired generic.
14784 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14785 and then not Body_Optional
14786 then
14787 declare
14788 Decl : Elmt_Id;
14789 Info : Pending_Body_Info;
14790 Par : Node_Id;
14792 begin
14793 Par := Parent (Inst_Node);
14794 while Present (Par) loop
14795 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14796 Par := Parent (Par);
14797 end loop;
14799 pragma Assert (Present (Par));
14801 if Nkind (Par) = N_Package_Body then
14802 Collect_Previous_Instances (Declarations (Par));
14804 elsif Nkind (Par) = N_Package_Declaration then
14805 Collect_Previous_Instances
14806 (Visible_Declarations (Specification (Par)));
14807 Collect_Previous_Instances
14808 (Private_Declarations (Specification (Par)));
14810 else
14811 -- Enclosing unit is a subprogram body. In this
14812 -- case all instance bodies are processed in order
14813 -- and there is no need to collect them separately.
14815 null;
14816 end if;
14818 Decl := First_Elmt (Previous_Instances);
14819 while Present (Decl) loop
14820 Info :=
14821 (Inst_Node => Node (Decl),
14822 Act_Decl =>
14823 Instance_Spec (Node (Decl)),
14824 Fin_Scop => Empty,
14825 Config_Switches => Save_Config_Switches,
14826 Current_Sem_Unit =>
14827 Get_Code_Unit (Sloc (Node (Decl))),
14828 Expander_Status => Exp_Status,
14829 Local_Suppress_Stack_Top =>
14830 Local_Suppress_Stack_Top,
14831 Scope_Suppress => Scope_Suppress,
14832 Warnings => Save_Warnings);
14834 -- Package instance
14836 if Nkind (Node (Decl)) = N_Package_Instantiation
14837 then
14838 Instantiate_Package_Body
14839 (Info, Body_Optional => True);
14841 -- Subprogram instance
14843 else
14844 -- The instance_spec is in the wrapper package,
14845 -- usually followed by its local renaming
14846 -- declaration. See Build_Subprogram_Renaming
14847 -- for details. If the instance carries aspects,
14848 -- these result in the corresponding pragmas,
14849 -- inserted after the subprogram declaration.
14850 -- They must be skipped as well when retrieving
14851 -- the desired spec. Some of them may have been
14852 -- rewritten as null statements.
14853 -- A direct link would be more robust ???
14855 declare
14856 Decl : Node_Id :=
14857 (Last (Visible_Declarations
14858 (Specification (Info.Act_Decl))));
14859 begin
14860 while Nkind (Decl) in
14861 N_Null_Statement |
14862 N_Pragma |
14863 N_Subprogram_Renaming_Declaration
14864 loop
14865 Decl := Prev (Decl);
14866 end loop;
14868 Info.Act_Decl := Decl;
14869 end;
14871 Instantiate_Subprogram_Body
14872 (Info, Body_Optional => True);
14873 end if;
14875 Next_Elmt (Decl);
14876 end loop;
14877 end;
14878 end if;
14880 Instantiate_Package_Body
14881 (Body_Info =>
14882 ((Inst_Node => Inst_Node,
14883 Act_Decl => True_Parent,
14884 Fin_Scop => Empty,
14885 Config_Switches => Save_Config_Switches,
14886 Current_Sem_Unit =>
14887 Get_Code_Unit (Sloc (Inst_Node)),
14888 Expander_Status => Exp_Status,
14889 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14890 Scope_Suppress => Scope_Suppress,
14891 Warnings => Save_Warnings)),
14892 Body_Optional => Body_Optional);
14893 end;
14894 end if;
14896 -- Case where we are not instantiating a nested generic
14898 else
14899 Opt.Style_Check := False;
14900 Expander_Mode_Save_And_Set (True);
14901 Load_Needed_Body (Comp_Unit, OK);
14902 Opt.Style_Check := Saved_Style_Check;
14903 Restore_Warnings (Saved_Warn);
14904 Expander_Mode_Restore;
14906 if not OK
14907 and then Unit_Requires_Body (Defining_Entity (Spec))
14908 and then not Body_Optional
14909 then
14910 declare
14911 Bname : constant Unit_Name_Type :=
14912 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14914 begin
14915 -- In CodePeer mode, the missing body may make the analysis
14916 -- incomplete, but we do not treat it as fatal.
14918 if CodePeer_Mode then
14919 return;
14921 else
14922 Error_Msg_Unit_1 := Bname;
14923 Error_Msg_N ("this instantiation requires$!", N);
14924 Error_Msg_File_1 :=
14925 Get_File_Name (Bname, Subunit => False);
14926 Error_Msg_N ("\but file{ was not found!", N);
14927 raise Unrecoverable_Error;
14928 end if;
14929 end;
14930 end if;
14931 end if;
14932 end if;
14934 -- If loading parent of the generic caused an instantiation circularity,
14935 -- we abandon compilation at this point, because otherwise in some cases
14936 -- we get into trouble with infinite recursions after this point.
14938 if Circularity_Detected then
14939 raise Unrecoverable_Error;
14940 end if;
14941 end Load_Parent_Of_Generic;
14943 ---------------------------------
14944 -- Map_Formal_Package_Entities --
14945 ---------------------------------
14947 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14948 E1 : Entity_Id;
14949 E2 : Entity_Id;
14951 begin
14952 Set_Instance_Of (Form, Act);
14954 -- Traverse formal and actual package to map the corresponding entities.
14955 -- We skip over internal entities that may be generated during semantic
14956 -- analysis, and find the matching entities by name, given that they
14957 -- must appear in the same order.
14959 E1 := First_Entity (Form);
14960 E2 := First_Entity (Act);
14961 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14962 -- Could this test be a single condition??? Seems like it could, and
14963 -- isn't FPE (Form) a constant anyway???
14965 if not Is_Internal (E1)
14966 and then Present (Parent (E1))
14967 and then not Is_Class_Wide_Type (E1)
14968 and then not Is_Internal_Name (Chars (E1))
14969 then
14970 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14971 Next_Entity (E2);
14972 end loop;
14974 if No (E2) then
14975 exit;
14976 else
14977 Set_Instance_Of (E1, E2);
14979 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14980 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14981 end if;
14983 if Is_Constrained (E1) then
14984 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14985 end if;
14987 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14988 Map_Formal_Package_Entities (E1, E2);
14989 end if;
14990 end if;
14991 end if;
14993 Next_Entity (E1);
14994 end loop;
14995 end Map_Formal_Package_Entities;
14997 -----------------------
14998 -- Move_Freeze_Nodes --
14999 -----------------------
15001 procedure Move_Freeze_Nodes
15002 (Out_Of : Entity_Id;
15003 After : Node_Id;
15004 L : List_Id)
15006 Decl : Node_Id;
15007 Next_Decl : Node_Id;
15008 Next_Node : Node_Id := After;
15009 Spec : Node_Id;
15011 function Is_Outer_Type (T : Entity_Id) return Boolean;
15012 -- Check whether entity is declared in a scope external to that of the
15013 -- generic unit.
15015 -------------------
15016 -- Is_Outer_Type --
15017 -------------------
15019 function Is_Outer_Type (T : Entity_Id) return Boolean is
15020 Scop : Entity_Id := Scope (T);
15022 begin
15023 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15024 return True;
15026 else
15027 while Scop /= Standard_Standard loop
15028 if Scop = Out_Of then
15029 return False;
15030 else
15031 Scop := Scope (Scop);
15032 end if;
15033 end loop;
15035 return True;
15036 end if;
15037 end Is_Outer_Type;
15039 -- Start of processing for Move_Freeze_Nodes
15041 begin
15042 if No (L) then
15043 return;
15044 end if;
15046 -- First remove the freeze nodes that may appear before all other
15047 -- declarations.
15049 Decl := First (L);
15050 while Present (Decl)
15051 and then Nkind (Decl) = N_Freeze_Entity
15052 and then Is_Outer_Type (Entity (Decl))
15053 loop
15054 Decl := Remove_Head (L);
15055 Insert_After (Next_Node, Decl);
15056 Set_Analyzed (Decl, False);
15057 Next_Node := Decl;
15058 Decl := First (L);
15059 end loop;
15061 -- Next scan the list of declarations and remove each freeze node that
15062 -- appears ahead of the current node.
15064 while Present (Decl) loop
15065 while Present (Next (Decl))
15066 and then Nkind (Next (Decl)) = N_Freeze_Entity
15067 and then Is_Outer_Type (Entity (Next (Decl)))
15068 loop
15069 Next_Decl := Remove_Next (Decl);
15070 Insert_After (Next_Node, Next_Decl);
15071 Set_Analyzed (Next_Decl, False);
15072 Next_Node := Next_Decl;
15073 end loop;
15075 -- If the declaration is a nested package or concurrent type, then
15076 -- recurse. Nested generic packages will have been processed from the
15077 -- inside out.
15079 case Nkind (Decl) is
15080 when N_Package_Declaration =>
15081 Spec := Specification (Decl);
15083 when N_Task_Type_Declaration =>
15084 Spec := Task_Definition (Decl);
15086 when N_Protected_Type_Declaration =>
15087 Spec := Protected_Definition (Decl);
15089 when others =>
15090 Spec := Empty;
15091 end case;
15093 if Present (Spec) then
15094 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15095 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15096 end if;
15098 Next (Decl);
15099 end loop;
15100 end Move_Freeze_Nodes;
15102 ----------------
15103 -- Next_Assoc --
15104 ----------------
15106 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15107 begin
15108 return Generic_Renamings.Table (E).Next_In_HTable;
15109 end Next_Assoc;
15111 ------------------------
15112 -- Preanalyze_Actuals --
15113 ------------------------
15115 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15116 procedure Perform_Appropriate_Analysis (N : Node_Id);
15117 -- Determine if the actuals we are analyzing come from a generic
15118 -- instantiation that is a library unit and dispatch accordingly.
15120 ----------------------------------
15121 -- Perform_Appropriate_Analysis --
15122 ----------------------------------
15124 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15125 begin
15126 -- When we have a library instantiation we cannot allow any expansion
15127 -- to occur, since there may be no place to put it. Instead, in that
15128 -- case we perform a preanalysis of the actual.
15130 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15131 Preanalyze (N);
15132 else
15133 Analyze (N);
15134 end if;
15135 end Perform_Appropriate_Analysis;
15137 -- Local variables
15139 Errs : constant Nat := Serious_Errors_Detected;
15141 Assoc : Node_Id;
15142 Act : Node_Id;
15144 Cur : Entity_Id := Empty;
15145 -- Current homograph of the instance name
15147 Vis : Boolean := False;
15148 -- Saved visibility status of the current homograph
15150 -- Start of processing for Preanalyze_Actuals
15152 begin
15153 Assoc := First (Generic_Associations (N));
15155 -- If the instance is a child unit, its name may hide an outer homonym,
15156 -- so make it invisible to perform name resolution on the actuals.
15158 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15159 and then Present
15160 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15161 then
15162 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15164 if Is_Compilation_Unit (Cur) then
15165 Vis := Is_Immediately_Visible (Cur);
15166 Set_Is_Immediately_Visible (Cur, False);
15167 else
15168 Cur := Empty;
15169 end if;
15170 end if;
15172 while Present (Assoc) loop
15173 if Nkind (Assoc) /= N_Others_Choice then
15174 Act := Explicit_Generic_Actual_Parameter (Assoc);
15176 -- Within a nested instantiation, a defaulted actual is an empty
15177 -- association, so nothing to analyze. If the subprogram actual
15178 -- is an attribute, analyze prefix only, because actual is not a
15179 -- complete attribute reference.
15181 -- If actual is an allocator, analyze expression only. The full
15182 -- analysis can generate code, and if instance is a compilation
15183 -- unit we have to wait until the package instance is installed
15184 -- to have a proper place to insert this code.
15186 -- String literals may be operators, but at this point we do not
15187 -- know whether the actual is a formal subprogram or a string.
15189 if No (Act) then
15190 null;
15192 elsif Nkind (Act) = N_Attribute_Reference then
15193 Perform_Appropriate_Analysis (Prefix (Act));
15195 elsif Nkind (Act) = N_Explicit_Dereference then
15196 Perform_Appropriate_Analysis (Prefix (Act));
15198 elsif Nkind (Act) = N_Allocator then
15199 declare
15200 Expr : constant Node_Id := Expression (Act);
15202 begin
15203 if Nkind (Expr) = N_Subtype_Indication then
15204 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15206 -- Analyze separately each discriminant constraint, when
15207 -- given with a named association.
15209 declare
15210 Constr : Node_Id;
15212 begin
15213 Constr := First (Constraints (Constraint (Expr)));
15214 while Present (Constr) loop
15215 if Nkind (Constr) = N_Discriminant_Association then
15216 Perform_Appropriate_Analysis
15217 (Expression (Constr));
15218 else
15219 Perform_Appropriate_Analysis (Constr);
15220 end if;
15222 Next (Constr);
15223 end loop;
15224 end;
15226 else
15227 Perform_Appropriate_Analysis (Expr);
15228 end if;
15229 end;
15231 elsif Nkind (Act) /= N_Operator_Symbol then
15232 Perform_Appropriate_Analysis (Act);
15234 -- Within a package instance, mark actuals that are limited
15235 -- views, so their use can be moved to the body of the
15236 -- enclosing unit.
15238 if Is_Entity_Name (Act)
15239 and then Is_Type (Entity (Act))
15240 and then From_Limited_With (Entity (Act))
15241 and then Present (Inst)
15242 then
15243 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15244 end if;
15245 end if;
15247 if Errs /= Serious_Errors_Detected then
15249 -- Do a minimal analysis of the generic, to prevent spurious
15250 -- warnings complaining about the generic being unreferenced,
15251 -- before abandoning the instantiation.
15253 Perform_Appropriate_Analysis (Name (N));
15255 if Is_Entity_Name (Name (N))
15256 and then Etype (Name (N)) /= Any_Type
15257 then
15258 Generate_Reference (Entity (Name (N)), Name (N));
15259 Set_Is_Instantiated (Entity (Name (N)));
15260 end if;
15262 if Present (Cur) then
15264 -- For the case of a child instance hiding an outer homonym,
15265 -- provide additional warning which might explain the error.
15267 Set_Is_Immediately_Visible (Cur, Vis);
15268 Error_Msg_NE
15269 ("& hides outer unit with the same name??",
15270 N, Defining_Unit_Name (N));
15271 end if;
15273 Abandon_Instantiation (Act);
15274 end if;
15275 end if;
15277 Next (Assoc);
15278 end loop;
15280 if Present (Cur) then
15281 Set_Is_Immediately_Visible (Cur, Vis);
15282 end if;
15283 end Preanalyze_Actuals;
15285 -------------------------------
15286 -- Provide_Completing_Bodies --
15287 -------------------------------
15289 procedure Provide_Completing_Bodies (N : Node_Id) is
15290 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15291 -- Generate the completing body for subprogram declaration Subp_Decl
15293 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15294 -- Generating completing bodies for all subprograms found in declarative
15295 -- list Decls.
15297 ---------------------------
15298 -- Build_Completing_Body --
15299 ---------------------------
15301 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15302 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15303 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15304 Spec : Node_Id;
15306 begin
15307 -- Nothing to do if the subprogram already has a completing body
15309 if Present (Corresponding_Body (Subp_Decl)) then
15310 return;
15312 -- Mark the function as having a valid return statement even though
15313 -- the body contains a single raise statement.
15315 elsif Ekind (Subp_Id) = E_Function then
15316 Set_Return_Present (Subp_Id);
15317 end if;
15319 -- Clone the specification to obtain new entities and reset the only
15320 -- semantic field.
15322 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15323 Set_Generic_Parent (Spec, Empty);
15325 -- Generate:
15326 -- function Func ... return ... is
15327 -- <or>
15328 -- procedure Proc ... is
15329 -- begin
15330 -- raise Program_Error with "access before elaboration";
15331 -- edn Proc;
15333 Insert_After_And_Analyze (Subp_Decl,
15334 Make_Subprogram_Body (Loc,
15335 Specification => Spec,
15336 Declarations => New_List,
15337 Handled_Statement_Sequence =>
15338 Make_Handled_Sequence_Of_Statements (Loc,
15339 Statements => New_List (
15340 Make_Raise_Program_Error (Loc,
15341 Reason => PE_Access_Before_Elaboration)))));
15342 end Build_Completing_Body;
15344 ----------------------------------
15345 -- Provide_Completing_Bodies_In --
15346 ----------------------------------
15348 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15349 Decl : Node_Id;
15351 begin
15352 if Present (Decls) then
15353 Decl := First (Decls);
15354 while Present (Decl) loop
15355 Provide_Completing_Bodies (Decl);
15356 Next (Decl);
15357 end loop;
15358 end if;
15359 end Provide_Completing_Bodies_In;
15361 -- Local variables
15363 Spec : Node_Id;
15365 -- Start of processing for Provide_Completing_Bodies
15367 begin
15368 if Nkind (N) = N_Package_Declaration then
15369 Spec := Specification (N);
15371 Push_Scope (Defining_Entity (N));
15372 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15373 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15374 Pop_Scope;
15376 elsif Nkind (N) = N_Subprogram_Declaration then
15377 Build_Completing_Body (N);
15378 end if;
15379 end Provide_Completing_Bodies;
15381 -------------------
15382 -- Remove_Parent --
15383 -------------------
15385 procedure Remove_Parent (In_Body : Boolean := False) is
15386 S : Entity_Id := Current_Scope;
15387 -- S is the scope containing the instantiation just completed. The scope
15388 -- stack contains the parent instances of the instantiation, followed by
15389 -- the original S.
15391 Cur_P : Entity_Id;
15392 E : Entity_Id;
15393 P : Entity_Id;
15394 Hidden : Elmt_Id;
15396 begin
15397 -- After child instantiation is complete, remove from scope stack the
15398 -- extra copy of the current scope, and then remove parent instances.
15400 if not In_Body then
15401 Pop_Scope;
15403 while Current_Scope /= S loop
15404 P := Current_Scope;
15405 End_Package_Scope (Current_Scope);
15407 if In_Open_Scopes (P) then
15408 E := First_Entity (P);
15409 while Present (E) loop
15410 Set_Is_Immediately_Visible (E, True);
15411 Next_Entity (E);
15412 end loop;
15414 -- If instantiation is declared in a block, it is the enclosing
15415 -- scope that might be a parent instance. Note that only one
15416 -- block can be involved, because the parent instances have
15417 -- been installed within it.
15419 if Ekind (P) = E_Block then
15420 Cur_P := Scope (P);
15421 else
15422 Cur_P := P;
15423 end if;
15425 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15426 -- We are within an instance of some sibling. Retain
15427 -- visibility of parent, for proper subsequent cleanup, and
15428 -- reinstall private declarations as well.
15430 Set_In_Private_Part (P);
15431 Install_Private_Declarations (P);
15432 end if;
15434 -- If the ultimate parent is a top-level unit recorded in
15435 -- Instance_Parent_Unit, then reset its visibility to what it was
15436 -- before instantiation. (It's not clear what the purpose is of
15437 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15438 -- present before the ultimate parent test was added.???)
15440 elsif not In_Open_Scopes (Scope (P))
15441 or else (P = Instance_Parent_Unit
15442 and then not Parent_Unit_Visible)
15443 then
15444 Set_Is_Immediately_Visible (P, False);
15446 -- If the current scope is itself an instantiation of a generic
15447 -- nested within P, and we are in the private part of body of this
15448 -- instantiation, restore the full views of P, that were removed
15449 -- in End_Package_Scope above. This obscure case can occur when a
15450 -- subunit of a generic contains an instance of a child unit of
15451 -- its generic parent unit.
15453 elsif S = Current_Scope and then Is_Generic_Instance (S)
15454 and then (In_Package_Body (S) or else In_Private_Part (S))
15455 then
15456 declare
15457 Par : constant Entity_Id :=
15458 Generic_Parent (Package_Specification (S));
15459 begin
15460 if Present (Par)
15461 and then P = Scope (Par)
15462 then
15463 Set_In_Private_Part (P);
15464 Install_Private_Declarations (P);
15465 end if;
15466 end;
15467 end if;
15468 end loop;
15470 -- Reset visibility of entities in the enclosing scope
15472 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15474 Hidden := First_Elmt (Hidden_Entities);
15475 while Present (Hidden) loop
15476 Set_Is_Immediately_Visible (Node (Hidden), True);
15477 Next_Elmt (Hidden);
15478 end loop;
15480 else
15481 -- Each body is analyzed separately, and there is no context that
15482 -- needs preserving from one body instance to the next, so remove all
15483 -- parent scopes that have been installed.
15485 while Present (S) loop
15486 End_Package_Scope (S);
15487 Set_Is_Immediately_Visible (S, False);
15488 S := Current_Scope;
15489 exit when S = Standard_Standard;
15490 end loop;
15491 end if;
15492 end Remove_Parent;
15494 -----------------------------------
15495 -- Requires_Conformance_Checking --
15496 -----------------------------------
15498 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15499 begin
15500 -- No conformance checking required if the generic actual part is empty,
15501 -- or is a box or an others_clause (necessarily with a box).
15503 return Present (Generic_Associations (N))
15504 and then not Box_Present (N)
15505 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15506 end Requires_Conformance_Checking;
15508 -----------------
15509 -- Restore_Env --
15510 -----------------
15512 procedure Restore_Env is
15513 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15515 begin
15516 if No (Current_Instantiated_Parent.Act_Id) then
15517 -- Restore environment after subprogram inlining
15519 Restore_Private_Views (Empty);
15520 end if;
15522 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15523 Exchanged_Views := Saved.Exchanged_Views;
15524 Hidden_Entities := Saved.Hidden_Entities;
15525 Current_Sem_Unit := Saved.Current_Sem_Unit;
15526 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15527 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15529 Restore_Config_Switches (Saved.Switches);
15531 Instance_Envs.Decrement_Last;
15532 end Restore_Env;
15534 ---------------------------
15535 -- Restore_Private_Views --
15536 ---------------------------
15538 procedure Restore_Private_Views
15539 (Pack_Id : Entity_Id;
15540 Is_Package : Boolean := True)
15542 M : Elmt_Id;
15543 E : Entity_Id;
15544 Typ : Entity_Id;
15545 Dep_Elmt : Elmt_Id;
15546 Dep_Typ : Node_Id;
15548 procedure Restore_Nested_Formal (Formal : Entity_Id);
15549 -- Hide the generic formals of formal packages declared with box which
15550 -- were reachable in the current instantiation.
15552 ---------------------------
15553 -- Restore_Nested_Formal --
15554 ---------------------------
15556 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15557 pragma Assert (Ekind (Formal) = E_Package);
15558 Ent : Entity_Id;
15559 begin
15560 if Present (Renamed_Entity (Formal))
15561 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15562 then
15563 return;
15565 elsif Present (Associated_Formal_Package (Formal)) then
15566 Ent := First_Entity (Formal);
15567 while Present (Ent) loop
15568 exit when Ekind (Ent) = E_Package
15569 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15571 Set_Is_Hidden (Ent);
15572 Set_Is_Potentially_Use_Visible (Ent, False);
15574 -- If package, then recurse
15576 if Ekind (Ent) = E_Package then
15577 Restore_Nested_Formal (Ent);
15578 end if;
15580 Next_Entity (Ent);
15581 end loop;
15582 end if;
15583 end Restore_Nested_Formal;
15585 -- Start of processing for Restore_Private_Views
15587 begin
15588 M := First_Elmt (Exchanged_Views);
15589 while Present (M) loop
15590 Typ := Node (M);
15592 -- Subtypes of types whose views have been exchanged, and that are
15593 -- defined within the instance, were not on the Private_Dependents
15594 -- list on entry to the instance, so they have to be exchanged
15595 -- explicitly now, in order to remain consistent with the view of the
15596 -- parent type.
15598 if Ekind (Typ) in E_Private_Type
15599 | E_Limited_Private_Type
15600 | E_Record_Type_With_Private
15601 then
15602 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15603 while Present (Dep_Elmt) loop
15604 Dep_Typ := Node (Dep_Elmt);
15606 if Scope (Dep_Typ) = Pack_Id
15607 and then Present (Full_View (Dep_Typ))
15608 then
15609 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15610 Exchange_Declarations (Dep_Typ);
15611 end if;
15613 Next_Elmt (Dep_Elmt);
15614 end loop;
15615 end if;
15617 Exchange_Declarations (Typ);
15618 Next_Elmt (M);
15619 end loop;
15621 if No (Pack_Id) then
15622 return;
15623 end if;
15625 -- Make the generic formal parameters private, and make the formal types
15626 -- into subtypes of the actuals again.
15628 E := First_Entity (Pack_Id);
15629 while Present (E) loop
15630 Set_Is_Hidden (E, True);
15632 if Is_Type (E)
15633 and then Nkind (Parent (E)) = N_Subtype_Declaration
15634 then
15635 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15636 -- as it is needed to identify the subtype with the type it
15637 -- renames, when there are conversions between access types
15638 -- to these.
15640 if GNATprove_Mode then
15641 null;
15643 -- If the actual for E is itself a generic actual type from
15644 -- an enclosing instance, E is still a generic actual type
15645 -- outside of the current instance. This matter when resolving
15646 -- an overloaded call that may be ambiguous in the enclosing
15647 -- instance, when two of its actuals coincide.
15649 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15650 and then Is_Generic_Actual_Type
15651 (Entity (Subtype_Indication (Parent (E))))
15652 then
15653 null;
15654 else
15655 Set_Is_Generic_Actual_Type (E, False);
15657 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15658 -- flag also on the Full_View if the type is private, since it
15659 -- was set also on this Full_View. However, this flag is relied
15660 -- upon by Covers to spot "types exported from instantiations"
15661 -- which are implicit Full_Views built for instantiations made
15662 -- on private types and we get type mismatches if we do it when
15663 -- the block exchanging the declarations below triggers ???
15665 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15666 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15667 -- end if;
15668 end if;
15670 -- An unusual case of aliasing: the actual may also be directly
15671 -- visible in the generic, and be private there, while it is fully
15672 -- visible in the context of the instance. The internal subtype
15673 -- is private in the instance but has full visibility like its
15674 -- parent in the enclosing scope. This enforces the invariant that
15675 -- the privacy status of all private dependents of a type coincide
15676 -- with that of the parent type. This can only happen when a
15677 -- generic child unit is instantiated within a sibling.
15679 if Is_Private_Type (E)
15680 and then not Is_Private_Type (Etype (E))
15681 then
15682 Exchange_Declarations (E);
15683 end if;
15685 elsif Ekind (E) = E_Package then
15687 -- The end of the renaming list is the renaming of the generic
15688 -- package itself. If the instance is a subprogram, all entities
15689 -- in the corresponding package are renamings. If this entity is
15690 -- a formal package, make its own formals private as well. The
15691 -- actual in this case is itself the renaming of an instantiation.
15692 -- If the entity is not a package renaming, it is the entity
15693 -- created to validate formal package actuals: ignore it.
15695 -- If the actual is itself a formal package for the enclosing
15696 -- generic, or the actual for such a formal package, it remains
15697 -- visible on exit from the instance, and therefore nothing needs
15698 -- to be done either, except to keep it accessible.
15700 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15701 exit;
15703 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15704 null;
15706 elsif
15707 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15708 then
15709 Set_Is_Hidden (E, False);
15711 else
15712 declare
15713 Act_P : constant Entity_Id := Renamed_Entity (E);
15714 Id : Entity_Id;
15716 begin
15717 Id := First_Entity (Act_P);
15718 while Present (Id)
15719 and then Id /= First_Private_Entity (Act_P)
15720 loop
15721 exit when Ekind (Id) = E_Package
15722 and then Renamed_Entity (Id) = Act_P;
15724 Set_Is_Hidden (Id, True);
15725 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15727 if Ekind (Id) = E_Package then
15728 Restore_Nested_Formal (Id);
15729 end if;
15731 Next_Entity (Id);
15732 end loop;
15733 end;
15734 end if;
15735 end if;
15737 Next_Entity (E);
15738 end loop;
15739 end Restore_Private_Views;
15741 --------------
15742 -- Save_Env --
15743 --------------
15745 procedure Save_Env
15746 (Gen_Unit : Entity_Id;
15747 Act_Unit : Entity_Id)
15749 begin
15750 Init_Env;
15751 Set_Instance_Env (Gen_Unit, Act_Unit);
15752 end Save_Env;
15754 ----------------------------
15755 -- Save_Global_References --
15756 ----------------------------
15758 procedure Save_Global_References (Templ : Node_Id) is
15760 -- ??? it is horrible to use global variables in highly recursive code
15762 E : Entity_Id;
15763 -- The entity of the current associated node
15765 Gen_Scope : Entity_Id;
15766 -- The scope of the generic for which references are being saved
15768 N2 : Node_Id;
15769 -- The current associated node
15771 function Is_Global (E : Entity_Id) return Boolean;
15772 -- Check whether entity is defined outside of generic unit. Examine the
15773 -- scope of an entity, and the scope of the scope, etc, until we find
15774 -- either Standard, in which case the entity is global, or the generic
15775 -- unit itself, which indicates that the entity is local. If the entity
15776 -- is the generic unit itself, as in the case of a recursive call, or
15777 -- the enclosing generic unit, if different from the current scope, then
15778 -- it is local as well, because it will be replaced at the point of
15779 -- instantiation. On the other hand, if it is a reference to a child
15780 -- unit of a common ancestor, which appears in an instantiation, it is
15781 -- global because it is used to denote a specific compilation unit at
15782 -- the time the instantiations will be analyzed.
15784 procedure Qualify_Universal_Operands
15785 (Op : Node_Id;
15786 Func_Call : Node_Id);
15787 -- Op denotes a binary or unary operator in generic template Templ. Node
15788 -- Func_Call is the function call alternative of the operator within the
15789 -- the analyzed copy of the template. Change each operand which yields a
15790 -- universal type by wrapping it into a qualified expression
15792 -- Actual_Typ'(Operand)
15794 -- where Actual_Typ is the type of corresponding actual parameter of
15795 -- Operand in Func_Call.
15797 procedure Reset_Entity (N : Node_Id);
15798 -- Save semantic information on global entity so that it is not resolved
15799 -- again at instantiation time.
15801 procedure Save_Entity_Descendants (N : Node_Id);
15802 -- Apply Save_Global_References to the two syntactic descendants of
15803 -- non-terminal nodes that carry an Associated_Node and are processed
15804 -- through Reset_Entity. Once the global entity (if any) has been
15805 -- captured together with its type, only two syntactic descendants need
15806 -- to be traversed to complete the processing of the tree rooted at N.
15807 -- This applies to Selected_Components, Expanded_Names, and to Operator
15808 -- nodes. N can also be a character literal, identifier, or operator
15809 -- symbol node, but the call has no effect in these cases.
15811 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15812 -- Default actuals in nested instances must be handled specially
15813 -- because there is no link to them from the original tree. When an
15814 -- actual subprogram is given by a default, we add an explicit generic
15815 -- association for it in the instantiation node. When we save the
15816 -- global references on the name of the instance, we recover the list
15817 -- of generic associations, and add an explicit one to the original
15818 -- generic tree, through which a global actual can be preserved.
15819 -- Similarly, if a child unit is instantiated within a sibling, in the
15820 -- context of the parent, we must preserve the identifier of the parent
15821 -- so that it can be properly resolved in a subsequent instantiation.
15823 procedure Save_Global_Descendant (D : Union_Id);
15824 -- Apply Save_References recursively to the descendants of node D
15826 procedure Save_References (N : Node_Id);
15827 -- This is the recursive procedure that does the work, once the
15828 -- enclosing generic scope has been established.
15830 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15831 -- If the type of N2 is global to the generic unit, save the type in
15832 -- the generic node. Just as we perform name capture for explicit
15833 -- references within the generic, we must capture the global types
15834 -- of local entities because they may participate in resolution in
15835 -- the instance.
15837 ---------------
15838 -- Is_Global --
15839 ---------------
15841 function Is_Global (E : Entity_Id) return Boolean is
15842 Se : Entity_Id;
15844 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15845 -- Determine whether the parent node of a reference to a child unit
15846 -- denotes an instantiation or a formal package, in which case the
15847 -- reference to the child unit is global, even if it appears within
15848 -- the current scope (e.g. when the instance appears within the body
15849 -- of an ancestor).
15851 ----------------------
15852 -- Is_Instance_Node --
15853 ----------------------
15855 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15856 begin
15857 return Nkind (Decl) in N_Generic_Instantiation
15858 or else
15859 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15860 end Is_Instance_Node;
15862 -- Start of processing for Is_Global
15864 begin
15865 if E = Gen_Scope then
15866 return False;
15868 elsif E = Standard_Standard then
15869 return True;
15871 -- E should be an entity, but it is not always
15873 elsif Nkind (E) not in N_Entity then
15874 return False;
15876 elsif Nkind (E) /= N_Expanded_Name
15877 and then Is_Child_Unit (E)
15878 and then (Is_Instance_Node (Parent (N2))
15879 or else (Nkind (Parent (N2)) = N_Expanded_Name
15880 and then N2 = Selector_Name (Parent (N2))
15881 and then
15882 Is_Instance_Node (Parent (Parent (N2)))))
15883 then
15884 return True;
15886 else
15887 -- E may be an expanded name - typically an operator - in which
15888 -- case we must find its enclosing scope since expanded names
15889 -- don't have corresponding scopes.
15891 if Nkind (E) = N_Expanded_Name then
15892 Se := Find_Enclosing_Scope (E);
15894 -- Otherwise, E is an entity and will have Scope set
15896 else
15897 Se := Scope (E);
15898 end if;
15900 while Se /= Gen_Scope loop
15901 if Se = Standard_Standard then
15902 return True;
15903 else
15904 Se := Scope (Se);
15905 end if;
15906 end loop;
15908 return False;
15909 end if;
15910 end Is_Global;
15912 --------------------------------
15913 -- Qualify_Universal_Operands --
15914 --------------------------------
15916 procedure Qualify_Universal_Operands
15917 (Op : Node_Id;
15918 Func_Call : Node_Id)
15920 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15921 -- Rewrite operand Opnd as a qualified expression of the form
15923 -- Actual_Typ'(Opnd)
15925 -- where Actual is the corresponding actual parameter of Opnd in
15926 -- function call Func_Call.
15928 function Qualify_Type
15929 (Loc : Source_Ptr;
15930 Typ : Entity_Id) return Node_Id;
15931 -- Qualify type Typ by creating a selected component of the form
15933 -- Scope_Of_Typ.Typ
15935 ---------------------
15936 -- Qualify_Operand --
15937 ---------------------
15939 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15940 Loc : constant Source_Ptr := Sloc (Opnd);
15941 Typ : constant Entity_Id := Etype (Actual);
15942 Mark : Node_Id;
15943 Qual : Node_Id;
15945 begin
15946 -- Qualify the operand when it is of a universal type. Note that
15947 -- the template is unanalyzed and it is not possible to directly
15948 -- query the type. This transformation is not done when the type
15949 -- of the actual is internally generated because the type will be
15950 -- regenerated in the instance.
15952 if Yields_Universal_Type (Opnd)
15953 and then Comes_From_Source (Typ)
15954 and then not Is_Hidden (Typ)
15955 then
15956 -- The type of the actual may be a global reference. Save this
15957 -- information by creating a reference to it.
15959 if Is_Global (Typ) then
15960 Mark := New_Occurrence_Of (Typ, Loc);
15962 -- Otherwise rely on resolution to find the proper type within
15963 -- the instance.
15965 else
15966 Mark := Qualify_Type (Loc, Typ);
15967 end if;
15969 Qual :=
15970 Make_Qualified_Expression (Loc,
15971 Subtype_Mark => Mark,
15972 Expression => Relocate_Node (Opnd));
15974 -- Mark the qualification to distinguish it from other source
15975 -- constructs and signal the instantiation mechanism that this
15976 -- node requires special processing. See Copy_Generic_Node for
15977 -- details.
15979 Set_Is_Qualified_Universal_Literal (Qual);
15981 Rewrite (Opnd, Qual);
15982 end if;
15983 end Qualify_Operand;
15985 ------------------
15986 -- Qualify_Type --
15987 ------------------
15989 function Qualify_Type
15990 (Loc : Source_Ptr;
15991 Typ : Entity_Id) return Node_Id
15993 Scop : constant Entity_Id := Scope (Typ);
15994 Result : Node_Id;
15996 begin
15997 Result := Make_Identifier (Loc, Chars (Typ));
15999 if Present (Scop) and then not Is_Generic_Unit (Scop) then
16000 Result :=
16001 Make_Selected_Component (Loc,
16002 Prefix => Make_Identifier (Loc, Chars (Scop)),
16003 Selector_Name => Result);
16004 end if;
16006 return Result;
16007 end Qualify_Type;
16009 -- Local variables
16011 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16013 -- Start of processing for Qualify_Universal_Operands
16015 begin
16016 if Nkind (Op) in N_Binary_Op then
16017 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16018 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16020 elsif Nkind (Op) in N_Unary_Op then
16021 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16022 end if;
16023 end Qualify_Universal_Operands;
16025 ------------------
16026 -- Reset_Entity --
16027 ------------------
16029 procedure Reset_Entity (N : Node_Id) is
16030 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16031 -- Find the ultimate ancestor of the current unit. If it is not a
16032 -- generic unit, then the name of the current unit in the prefix of
16033 -- an expanded name must be replaced with its generic homonym to
16034 -- ensure that it will be properly resolved in an instance.
16036 ------------------
16037 -- Top_Ancestor --
16038 ------------------
16040 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16041 Par : Entity_Id;
16043 begin
16044 Par := E;
16045 while Is_Child_Unit (Par) loop
16046 Par := Scope (Par);
16047 end loop;
16049 return Par;
16050 end Top_Ancestor;
16052 -- Start of processing for Reset_Entity
16054 begin
16055 N2 := Get_Associated_Node (N);
16056 E := Entity (N2);
16058 if Present (E) then
16060 -- If the node is an entry call to an entry in an enclosing task,
16061 -- it is rewritten as a selected component. No global entity to
16062 -- preserve in this case, since the expansion will be redone in
16063 -- the instance.
16065 if Nkind (E) not in N_Entity then
16066 Set_Associated_Node (N, Empty);
16067 Set_Etype (N, Empty);
16068 return;
16069 end if;
16071 -- If the entity is an itype created as a subtype of an access
16072 -- type with a null exclusion restore source entity for proper
16073 -- visibility. The itype will be created anew in the instance.
16075 if Is_Itype (E)
16076 and then Ekind (E) = E_Access_Subtype
16077 and then Is_Entity_Name (N)
16078 and then Chars (Etype (E)) = Chars (N)
16079 then
16080 E := Etype (E);
16081 Set_Entity (N2, E);
16082 Set_Etype (N2, E);
16083 end if;
16085 if Is_Global (E) then
16086 Set_Global_Type (N, N2);
16088 elsif Nkind (N) = N_Op_Concat
16089 and then Is_Generic_Type (Etype (N2))
16090 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16091 or else
16092 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16093 and then Is_Intrinsic_Subprogram (E)
16094 then
16095 null;
16097 -- Entity is local. Mark generic node as unresolved. Note that now
16098 -- it does not have an entity.
16100 else
16101 Set_Associated_Node (N, Empty);
16102 Set_Etype (N, Empty);
16103 end if;
16105 if Nkind (Parent (N)) in N_Generic_Instantiation
16106 and then N = Name (Parent (N))
16107 then
16108 Save_Global_Defaults (Parent (N), Parent (N2));
16109 end if;
16111 elsif Nkind (Parent (N)) = N_Selected_Component
16112 and then Nkind (Parent (N2)) = N_Expanded_Name
16113 then
16114 -- In case of previous errors, the tree might be malformed
16116 if No (Entity (Parent (N2))) then
16117 null;
16119 elsif Is_Global (Entity (Parent (N2))) then
16120 Change_Selected_Component_To_Expanded_Name (Parent (N));
16121 Set_Associated_Node (Parent (N), Parent (N2));
16122 Set_Global_Type (Parent (N), Parent (N2));
16123 Save_Entity_Descendants (N);
16125 -- If this is a reference to the current generic entity, replace
16126 -- by the name of the generic homonym of the current package. This
16127 -- is because in an instantiation Par.P.Q will not resolve to the
16128 -- name of the instance, whose enclosing scope is not necessarily
16129 -- Par. We use the generic homonym rather that the name of the
16130 -- generic itself because it may be hidden by a local declaration.
16132 elsif In_Open_Scopes (Entity (Parent (N2)))
16133 and then not
16134 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16135 then
16136 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16137 Rewrite (Parent (N),
16138 Make_Identifier (Sloc (N),
16139 Chars =>
16140 Chars (Generic_Homonym (Entity (Parent (N2))))));
16141 else
16142 Rewrite (Parent (N),
16143 Make_Identifier (Sloc (N),
16144 Chars => Chars (Selector_Name (Parent (N2)))));
16145 end if;
16146 end if;
16148 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16149 and then Parent (N) = Name (Parent (Parent (N)))
16150 then
16151 Save_Global_Defaults
16152 (Parent (Parent (N)), Parent (Parent (N2)));
16153 end if;
16155 -- A selected component may denote a static constant that has been
16156 -- folded. If the static constant is global to the generic, capture
16157 -- its value. Otherwise the folding will happen in any instantiation.
16159 elsif Nkind (Parent (N)) = N_Selected_Component
16160 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16161 then
16162 if Present (Entity (Original_Node (Parent (N2))))
16163 and then Is_Global (Entity (Original_Node (Parent (N2))))
16164 then
16165 Rewrite (Parent (N), New_Copy (Parent (N2)));
16166 Set_Analyzed (Parent (N), False);
16167 end if;
16169 -- A selected component may be transformed into a parameterless
16170 -- function call. If the called entity is global, rewrite the node
16171 -- appropriately, i.e. as an extended name for the global entity.
16173 elsif Nkind (Parent (N)) = N_Selected_Component
16174 and then Nkind (Parent (N2)) = N_Function_Call
16175 and then N = Selector_Name (Parent (N))
16176 then
16177 if No (Parameter_Associations (Parent (N2))) then
16178 if Is_Global (Entity (Name (Parent (N2)))) then
16179 Change_Selected_Component_To_Expanded_Name (Parent (N));
16180 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16181 Set_Global_Type (Parent (N), Name (Parent (N2)));
16182 Save_Entity_Descendants (N);
16184 else
16185 Set_Is_Prefixed_Call (Parent (N));
16186 Set_Associated_Node (N, Empty);
16187 Set_Etype (N, Empty);
16188 end if;
16190 -- In Ada 2005, X.F may be a call to a primitive operation,
16191 -- rewritten as F (X). This rewriting will be done again in an
16192 -- instance, so keep the original node. Global entities will be
16193 -- captured as for other constructs. Indicate that this must
16194 -- resolve as a call, to prevent accidental overloading in the
16195 -- instance, if both a component and a primitive operation appear
16196 -- as candidates.
16198 else
16199 Set_Is_Prefixed_Call (Parent (N));
16200 end if;
16202 -- Entity is local. Reset in generic unit, so that node is resolved
16203 -- anew at the point of instantiation.
16205 else
16206 Set_Associated_Node (N, Empty);
16207 Set_Etype (N, Empty);
16208 end if;
16209 end Reset_Entity;
16211 -----------------------------
16212 -- Save_Entity_Descendants --
16213 -----------------------------
16215 procedure Save_Entity_Descendants (N : Node_Id) is
16216 begin
16217 case Nkind (N) is
16218 when N_Binary_Op =>
16219 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16220 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16222 when N_Unary_Op =>
16223 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16225 when N_Expanded_Name
16226 | N_Selected_Component
16228 Save_Global_Descendant (Union_Id (Prefix (N)));
16229 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16231 when N_Character_Literal
16232 | N_Identifier
16233 | N_Operator_Symbol
16235 null;
16237 when others =>
16238 raise Program_Error;
16239 end case;
16240 end Save_Entity_Descendants;
16242 --------------------------
16243 -- Save_Global_Defaults --
16244 --------------------------
16246 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16247 Loc : constant Source_Ptr := Sloc (N1);
16248 Assoc2 : constant List_Id := Generic_Associations (N2);
16249 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16250 Assoc1 : List_Id;
16251 Act1 : Node_Id;
16252 Act2 : Node_Id;
16253 Def : Node_Id;
16254 Ndec : Node_Id;
16255 Subp : Entity_Id;
16256 Actual : Entity_Id;
16258 begin
16259 Assoc1 := Generic_Associations (N1);
16261 if Present (Assoc1) then
16262 Act1 := First (Assoc1);
16263 else
16264 Act1 := Empty;
16265 Set_Generic_Associations (N1, New_List);
16266 Assoc1 := Generic_Associations (N1);
16267 end if;
16269 if Present (Assoc2) then
16270 Act2 := First (Assoc2);
16271 else
16272 return;
16273 end if;
16275 while Present (Act1) and then Present (Act2) loop
16276 Next (Act1);
16277 Next (Act2);
16278 end loop;
16280 -- Find the associations added for default subprograms
16282 if Present (Act2) then
16283 while Nkind (Act2) /= N_Generic_Association
16284 or else No (Entity (Selector_Name (Act2)))
16285 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16286 loop
16287 Next (Act2);
16288 end loop;
16290 -- Add a similar association if the default is global. The
16291 -- renaming declaration for the actual has been analyzed, and
16292 -- its alias is the program it renames. Link the actual in the
16293 -- original generic tree with the node in the analyzed tree.
16295 while Present (Act2) loop
16296 Subp := Entity (Selector_Name (Act2));
16297 Def := Explicit_Generic_Actual_Parameter (Act2);
16299 -- Following test is defence against rubbish errors
16301 if No (Alias (Subp)) then
16302 return;
16303 end if;
16305 -- Retrieve the resolved actual from the renaming declaration
16306 -- created for the instantiated formal.
16308 Actual := Entity (Name (Parent (Parent (Subp))));
16309 Set_Entity (Def, Actual);
16310 Set_Etype (Def, Etype (Actual));
16312 if Is_Global (Actual) then
16313 Ndec :=
16314 Make_Generic_Association (Loc,
16315 Selector_Name =>
16316 New_Occurrence_Of (Subp, Loc),
16317 Explicit_Generic_Actual_Parameter =>
16318 New_Occurrence_Of (Actual, Loc));
16320 Set_Associated_Node
16321 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16323 Append (Ndec, Assoc1);
16325 -- If there are other defaults, add a dummy association in case
16326 -- there are other defaulted formals with the same name.
16328 elsif Present (Next (Act2)) then
16329 Ndec :=
16330 Make_Generic_Association (Loc,
16331 Selector_Name =>
16332 New_Occurrence_Of (Subp, Loc),
16333 Explicit_Generic_Actual_Parameter => Empty);
16335 Append (Ndec, Assoc1);
16336 end if;
16338 Next (Act2);
16339 end loop;
16340 end if;
16342 if Nkind (Name (N1)) = N_Identifier
16343 and then Is_Child_Unit (Gen_Id)
16344 and then Is_Global (Gen_Id)
16345 and then Is_Generic_Unit (Scope (Gen_Id))
16346 and then In_Open_Scopes (Scope (Gen_Id))
16347 then
16348 -- This is an instantiation of a child unit within a sibling, so
16349 -- that the generic parent is in scope. An eventual instance must
16350 -- occur within the scope of an instance of the parent. Make name
16351 -- in instance into an expanded name, to preserve the identifier
16352 -- of the parent, so it can be resolved subsequently.
16354 Rewrite (Name (N2),
16355 Make_Expanded_Name (Loc,
16356 Chars => Chars (Gen_Id),
16357 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16358 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16359 Set_Entity (Name (N2), Gen_Id);
16361 Rewrite (Name (N1),
16362 Make_Expanded_Name (Loc,
16363 Chars => Chars (Gen_Id),
16364 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16365 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16367 Set_Associated_Node (Name (N1), Name (N2));
16368 Set_Associated_Node (Prefix (Name (N1)), Empty);
16369 Set_Associated_Node
16370 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16371 Set_Etype (Name (N1), Etype (Gen_Id));
16372 end if;
16373 end Save_Global_Defaults;
16375 ----------------------------
16376 -- Save_Global_Descendant --
16377 ----------------------------
16379 procedure Save_Global_Descendant (D : Union_Id) is
16380 N1 : Node_Id;
16382 begin
16383 if D in Node_Range then
16384 if D = Union_Id (Empty) then
16385 null;
16387 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16388 Save_References (Node_Id (D));
16389 end if;
16391 elsif D in List_Range then
16392 pragma Assert (D /= Union_Id (No_List));
16393 -- Because No_List = Empty, which is in Node_Range above
16395 N1 := First (List_Id (D));
16396 while Present (N1) loop
16397 Save_References (N1);
16398 Next (N1);
16399 end loop;
16401 -- Element list or other non-node field, nothing to do
16403 else
16404 null;
16405 end if;
16406 end Save_Global_Descendant;
16408 ---------------------
16409 -- Save_References --
16410 ---------------------
16412 -- This is the recursive procedure that does the work once the enclosing
16413 -- generic scope has been established. We have to treat specially a
16414 -- number of node rewritings that are required by semantic processing
16415 -- and which change the kind of nodes in the generic copy: typically
16416 -- constant-folding, replacing an operator node by a string literal, or
16417 -- a selected component by an expanded name. In each of those cases, the
16418 -- transformation is propagated to the generic unit.
16420 procedure Save_References (N : Node_Id) is
16421 Loc : constant Source_Ptr := Sloc (N);
16423 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16424 -- Determine whether arbitrary node Nod requires delayed capture of
16425 -- global references within its aspect specifications.
16427 procedure Save_References_In_Aggregate (N : Node_Id);
16428 -- Save all global references in [extension] aggregate node N
16430 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16431 -- Save all global references in a character literal or operator
16432 -- symbol denoted by N.
16434 procedure Save_References_In_Descendants (N : Node_Id);
16435 -- Save all global references in all descendants of node N
16437 procedure Save_References_In_Identifier (N : Node_Id);
16438 -- Save all global references in identifier node N
16440 procedure Save_References_In_Operator (N : Node_Id);
16441 -- Save all global references in operator node N
16443 procedure Save_References_In_Pragma (Prag : Node_Id);
16444 -- Save all global references found within the expression of pragma
16445 -- Prag.
16447 ---------------------------
16448 -- Requires_Delayed_Save --
16449 ---------------------------
16451 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16452 begin
16453 -- Generic packages and subprograms require delayed capture of
16454 -- global references within their aspects due to the timing of
16455 -- annotation analysis.
16457 if Nkind (Nod) in N_Generic_Package_Declaration
16458 | N_Generic_Subprogram_Declaration
16459 | N_Package_Body
16460 | N_Package_Body_Stub
16461 | N_Subprogram_Body
16462 | N_Subprogram_Body_Stub
16463 then
16464 -- Since the capture of global references is done on the
16465 -- unanalyzed generic template, there is no information around
16466 -- to infer the context. Use the Associated_Entity linkages to
16467 -- peek into the analyzed generic copy and determine what the
16468 -- template corresponds to.
16470 if Nod = Templ then
16471 return
16472 Is_Generic_Declaration_Or_Body
16473 (Unit_Declaration_Node
16474 (Get_Associated_Entity (Defining_Entity (Nod))));
16476 -- Otherwise the generic unit being processed is not the top
16477 -- level template. It is safe to capture of global references
16478 -- within the generic unit because at this point the top level
16479 -- copy is fully analyzed.
16481 else
16482 return False;
16483 end if;
16485 -- Otherwise capture the global references without interference
16487 else
16488 return False;
16489 end if;
16490 end Requires_Delayed_Save;
16492 ----------------------------------
16493 -- Save_References_In_Aggregate --
16494 ----------------------------------
16496 procedure Save_References_In_Aggregate (N : Node_Id) is
16497 Nam : Node_Id;
16498 Qual : Node_Id := Empty;
16499 Typ : Entity_Id := Empty;
16501 begin
16502 N2 := Get_Associated_Node (N);
16504 if Present (N2) then
16505 Typ := Etype (N2);
16507 -- In an instance within a generic, use the name of the actual
16508 -- and not the original generic parameter. If the actual is
16509 -- global in the current generic it must be preserved for its
16510 -- instantiation.
16512 if Parent_Kind (Typ) = N_Subtype_Declaration
16513 and then Present (Generic_Parent_Type (Parent (Typ)))
16514 then
16515 Typ := Base_Type (Typ);
16516 Set_Etype (N2, Typ);
16517 end if;
16518 end if;
16520 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16521 Set_Associated_Node (N, Empty);
16523 -- For a full aggregate, if the type is local but is a derived
16524 -- tagged type of a global ancestor, we will need to have the
16525 -- full view of this global ancestor available in the instance
16526 -- in order to analyze the full aggregate.
16528 if Present (N2)
16529 and then Nkind (N2) = N_Aggregate
16530 and then Present (Typ)
16531 and then Is_Tagged_Type (Typ)
16532 and then Is_Derived_Type (Typ)
16533 then
16534 declare
16535 Root_Typ : constant Entity_Id := Root_Type (Typ);
16537 Parent_Typ : Entity_Id := Typ;
16539 begin
16540 loop
16541 Parent_Typ := Etype (Parent_Typ);
16543 if Is_Global (Parent_Typ) then
16544 Set_Ancestor_Type (N, Parent_Typ);
16545 exit;
16546 end if;
16548 exit when Parent_Typ = Root_Typ;
16549 end loop;
16550 end;
16551 end if;
16553 -- If the aggregate is an actual in a call, it has been
16554 -- resolved in the current context, to some local type. The
16555 -- enclosing call may have been disambiguated by the aggregate,
16556 -- and this disambiguation might fail at instantiation time
16557 -- because the type to which the aggregate did resolve is not
16558 -- preserved. In order to preserve some of this information,
16559 -- wrap the aggregate in a qualified expression, using the id
16560 -- of its type. For further disambiguation we qualify the type
16561 -- name with its scope (if visible and not hidden by a local
16562 -- homograph) because both id's will have corresponding
16563 -- entities in an instance. This resolves most of the problems
16564 -- with missing type information on aggregates in instances.
16566 if Present (N2)
16567 and then Nkind (N2) = Nkind (N)
16568 and then Nkind (Parent (N2)) in N_Subprogram_Call
16569 and then Present (Typ)
16570 and then Comes_From_Source (Typ)
16571 then
16572 Nam := Make_Identifier (Loc, Chars (Typ));
16574 if Is_Immediately_Visible (Scope (Typ))
16575 and then
16576 (not In_Open_Scopes (Scope (Typ))
16577 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16578 then
16579 Nam :=
16580 Make_Selected_Component (Loc,
16581 Prefix =>
16582 Make_Identifier (Loc, Chars (Scope (Typ))),
16583 Selector_Name => Nam);
16584 end if;
16586 Qual :=
16587 Make_Qualified_Expression (Loc,
16588 Subtype_Mark => Nam,
16589 Expression => Relocate_Node (N));
16590 end if;
16592 -- For a full aggregate, if the type is global and a derived
16593 -- tagged type, we will also need to have the full view of its
16594 -- ancestor available in the instance in order to analyze the
16595 -- full aggregate.
16597 elsif Present (N2)
16598 and then Nkind (N2) = N_Aggregate
16599 and then Present (Typ)
16600 and then Is_Tagged_Type (Typ)
16601 and then Is_Derived_Type (Typ)
16602 then
16603 Set_Ancestor_Type (N, Etype (Typ));
16604 end if;
16606 if Nkind (N) = N_Aggregate then
16607 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16609 elsif Nkind (N) = N_Extension_Aggregate then
16610 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16612 else
16613 pragma Assert (False);
16614 end if;
16616 Save_Global_Descendant (Union_Id (Expressions (N)));
16617 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16618 Save_Global_Descendant (Union_Id (Etype (N)));
16620 if Present (Qual) then
16621 Rewrite (N, Qual);
16622 end if;
16623 end Save_References_In_Aggregate;
16625 ----------------------------------------------
16626 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16627 ----------------------------------------------
16629 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16630 begin
16631 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16632 Reset_Entity (N);
16634 elsif Nkind (N) = N_Operator_Symbol
16635 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16636 then
16637 Change_Operator_Symbol_To_String_Literal (N);
16638 end if;
16639 end Save_References_In_Char_Lit_Or_Op_Symbol;
16641 ------------------------------------
16642 -- Save_References_In_Descendants --
16643 ------------------------------------
16645 procedure Save_References_In_Descendants (N : Node_Id) is
16646 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16647 begin
16648 Walk (N);
16649 end Save_References_In_Descendants;
16651 -----------------------------------
16652 -- Save_References_In_Identifier --
16653 -----------------------------------
16655 procedure Save_References_In_Identifier (N : Node_Id) is
16656 begin
16657 -- The node did not undergo a transformation
16659 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16660 -- If this is a discriminant reference, always save it.
16661 -- It is used in the instance to find the corresponding
16662 -- discriminant positionally rather than by name.
16664 Set_Original_Discriminant
16665 (N, Original_Discriminant (Get_Associated_Node (N)));
16667 Reset_Entity (N);
16669 -- The analysis of the generic copy transformed the identifier
16670 -- into another construct. Propagate the changes to the template.
16672 else
16673 N2 := Get_Associated_Node (N);
16675 -- The identifier denotes a call to a parameterless function.
16676 -- Mark the node as resolved when the function is external.
16678 if Nkind (N2) = N_Function_Call then
16679 E := Entity (Name (N2));
16681 if Present (E) and then Is_Global (E) then
16682 Set_Global_Type (N, N2);
16683 else
16684 Set_Associated_Node (N, Empty);
16685 Set_Etype (N, Empty);
16686 end if;
16688 -- The identifier denotes a named number that was constant
16689 -- folded. Preserve the original name for ASIS and undo the
16690 -- constant folding which will be repeated in the instance.
16691 -- Is this still needed???
16693 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16694 and then Is_Entity_Name (Original_Node (N2))
16695 then
16696 Set_Associated_Node (N, Original_Node (N2));
16697 Reset_Entity (N);
16699 -- The identifier resolved to a string literal. Propagate this
16700 -- information to the generic template.
16702 elsif Nkind (N2) = N_String_Literal then
16703 Rewrite (N, New_Copy (N2));
16705 -- The identifier is rewritten as a dereference if it is the
16706 -- prefix of an implicit dereference. Preserve the original
16707 -- tree as the analysis of the instance will expand the node
16708 -- again, but preserve the resolved entity if it is global.
16710 elsif Nkind (N2) = N_Explicit_Dereference then
16711 if Is_Entity_Name (Prefix (N2))
16712 and then Present (Entity (Prefix (N2)))
16713 and then Is_Global (Entity (Prefix (N2)))
16714 then
16715 Set_Associated_Node (N, Prefix (N2));
16716 Set_Global_Type (N, Prefix (N2));
16718 elsif Nkind (Prefix (N2)) = N_Function_Call
16719 and then Is_Entity_Name (Name (Prefix (N2)))
16720 and then Present (Entity (Name (Prefix (N2))))
16721 and then Is_Global (Entity (Name (Prefix (N2))))
16722 then
16723 Rewrite (N,
16724 Make_Explicit_Dereference (Loc,
16725 Prefix =>
16726 Make_Function_Call (Loc,
16727 Name =>
16728 New_Occurrence_Of
16729 (Entity (Name (Prefix (N2))), Loc))));
16730 Set_Associated_Node
16731 (Name (Prefix (N)), Name (Prefix (N2)));
16732 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
16734 else
16735 Set_Associated_Node (N, Empty);
16736 Set_Etype (N, Empty);
16737 end if;
16739 -- The subtype mark of a nominally unconstrained object is
16740 -- rewritten as a subtype indication using the bounds of the
16741 -- expression. Recover the original subtype mark.
16743 elsif Nkind (N2) = N_Subtype_Indication
16744 and then Is_Entity_Name (Original_Node (N2))
16745 then
16746 Set_Associated_Node (N, Original_Node (N2));
16747 Reset_Entity (N);
16748 end if;
16749 end if;
16750 end Save_References_In_Identifier;
16752 ---------------------------------
16753 -- Save_References_In_Operator --
16754 ---------------------------------
16756 procedure Save_References_In_Operator (N : Node_Id) is
16757 begin
16758 N2 := Get_Associated_Node (N);
16760 -- The node did not undergo a transformation
16762 if Nkind (N) = Nkind (N2) then
16763 if Nkind (N) = N_Op_Concat then
16764 Set_Is_Component_Left_Opnd
16765 (N, Is_Component_Left_Opnd (N2));
16766 Set_Is_Component_Right_Opnd
16767 (N, Is_Component_Right_Opnd (N2));
16768 end if;
16770 Reset_Entity (N);
16772 -- The analysis of the generic copy transformed the operator into
16773 -- some other construct. Propagate the changes to the template if
16774 -- applicable.
16776 else
16777 -- The operator resoved to a function call
16779 if Nkind (N2) = N_Function_Call then
16781 -- Add explicit qualifications in the generic template for
16782 -- all operands of universal type. This aids resolution by
16783 -- preserving the actual type of a literal or an attribute
16784 -- that yields a universal result.
16786 Qualify_Universal_Operands (N, N2);
16788 E := Entity (Name (N2));
16790 if Present (E) and then Is_Global (E) then
16791 Set_Global_Type (N, N2);
16792 else
16793 Set_Associated_Node (N, Empty);
16794 Set_Etype (N, Empty);
16795 end if;
16797 -- The operator was folded into a literal
16799 elsif Nkind (N2) in N_Integer_Literal
16800 | N_Real_Literal
16801 | N_String_Literal
16802 then
16803 if Present (Original_Node (N2))
16804 and then Nkind (Original_Node (N2)) = Nkind (N)
16805 then
16806 -- Operation was constant-folded. Whenever possible,
16807 -- recover semantic information from unfolded node.
16808 -- This was initially done for ASIS but is apparently
16809 -- needed also for e.g. compiling a-nbnbin.adb.
16811 Set_Associated_Node (N, Original_Node (N2));
16813 if Nkind (N) = N_Op_Concat then
16814 Set_Is_Component_Left_Opnd (N,
16815 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16816 Set_Is_Component_Right_Opnd (N,
16817 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16818 end if;
16820 Reset_Entity (N);
16822 -- Propagate the constant folding back to the template
16824 else
16825 Rewrite (N, New_Copy (N2));
16826 Set_Analyzed (N, False);
16827 end if;
16829 -- The operator was folded into an enumeration literal. Retain
16830 -- the entity to avoid spurious ambiguities if it is overloaded
16831 -- at the point of instantiation or inlining.
16833 elsif Nkind (N2) = N_Identifier
16834 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16835 then
16836 Rewrite (N, New_Copy (N2));
16837 Set_Analyzed (N, False);
16838 end if;
16839 end if;
16841 -- Complete the operands check if node has not been constant
16842 -- folded.
16844 if Nkind (N) in N_Op then
16845 Save_Entity_Descendants (N);
16846 end if;
16847 end Save_References_In_Operator;
16849 -------------------------------
16850 -- Save_References_In_Pragma --
16851 -------------------------------
16853 procedure Save_References_In_Pragma (Prag : Node_Id) is
16854 Context : Node_Id;
16855 Do_Save : Boolean := True;
16857 begin
16858 -- Do not save global references in pragmas generated from aspects
16859 -- because the pragmas will be regenerated at instantiation time.
16861 if From_Aspect_Specification (Prag) then
16862 Do_Save := False;
16864 -- The capture of global references within contract-related source
16865 -- pragmas associated with generic packages, subprograms or their
16866 -- respective bodies must be delayed due to timing of annotation
16867 -- analysis. Global references are still captured in routine
16868 -- Save_Global_References_In_Contract.
16870 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16871 if Is_Package_Contract_Annotation (Prag) then
16872 Context := Find_Related_Package_Or_Body (Prag);
16873 else
16874 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16875 Context := Find_Related_Declaration_Or_Body (Prag);
16876 end if;
16878 -- The use of Original_Node accounts for the case when the
16879 -- related context is generic template.
16881 if Requires_Delayed_Save (Original_Node (Context)) then
16882 Do_Save := False;
16883 end if;
16884 end if;
16886 -- For all other cases, save all global references within the
16887 -- descendants, but skip the following semantic fields:
16888 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16890 if Do_Save then
16891 Save_Global_Descendant
16892 (Union_Id (Pragma_Argument_Associations (N)));
16893 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16894 end if;
16895 end Save_References_In_Pragma;
16897 -- Start of processing for Save_References
16899 begin
16900 if N = Empty then
16901 null;
16903 -- Aggregates
16905 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16906 Save_References_In_Aggregate (N);
16908 -- Character literals, operator symbols
16910 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16911 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16913 -- Defining identifiers
16915 elsif Nkind (N) in N_Entity then
16916 null;
16918 -- Identifiers
16920 elsif Nkind (N) = N_Identifier then
16921 Save_References_In_Identifier (N);
16923 -- Operators
16925 elsif Nkind (N) in N_Op then
16926 Save_References_In_Operator (N);
16928 -- Pragmas
16930 elsif Nkind (N) = N_Pragma then
16931 Save_References_In_Pragma (N);
16933 elsif Nkind (N) = N_Aspect_Specification then
16934 declare
16935 P : constant Node_Id := Parent (N);
16936 Expr : Node_Id;
16937 begin
16939 if Permits_Aspect_Specifications (P) then
16941 -- The capture of global references within aspects
16942 -- associated with generic packages, subprograms or
16943 -- their bodies must be delayed due to timing of
16944 -- annotation analysis. Global references are still
16945 -- captured in routine Save_Global_References_In_Contract.
16947 if Requires_Delayed_Save (Original_Node (P)) then
16948 null;
16950 -- Otherwise save all global references within the
16951 -- aspects
16953 else
16954 Expr := Expression (N);
16956 if Present (Expr) then
16957 Save_Global_References (Expr);
16958 end if;
16959 end if;
16960 end if;
16961 end;
16963 -- Do not walk the node pointed to by Label_Construct twice
16965 elsif Nkind (N) = N_Implicit_Label_Declaration then
16966 null;
16968 else
16969 Save_References_In_Descendants (N);
16970 end if;
16972 end Save_References;
16974 ---------------------
16975 -- Set_Global_Type --
16976 ---------------------
16978 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
16979 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
16980 Typ : constant Entity_Id :=
16981 (if Comparison then Compare_Type (N2) else Etype (N2));
16983 begin
16984 -- For a comparison (or equality) operator, the Etype is Boolean, so
16985 -- it is always global. But the type subject to the Has_Private_View
16986 -- processing is the Compare_Type, so we must specifically check it.
16988 if Comparison then
16989 Set_Etype (N, Etype (N2));
16991 if not Is_Global (Typ) then
16992 return;
16993 end if;
16995 Set_Compare_Type (N, Typ);
16997 else
16998 Set_Etype (N, Typ);
16999 end if;
17001 -- If the entity of N is not the associated node, this is a
17002 -- nested generic and it has an associated node as well, whose
17003 -- type is already the full view (see below). Indicate that the
17004 -- original node has a private view.
17006 if Entity (N) /= N2 then
17007 if Has_Private_View (Entity (N)) then
17008 Set_Has_Private_View (N);
17009 end if;
17011 if Has_Secondary_Private_View (Entity (N)) then
17012 Set_Has_Secondary_Private_View (N);
17013 end if;
17014 end if;
17016 -- If not a private type, deal with a secondary private view
17018 if not Is_Private_Type (Typ) then
17019 if (Is_Access_Type (Typ)
17020 and then Is_Private_Type (Designated_Type (Typ)))
17021 or else (Is_Array_Type (Typ)
17022 and then
17023 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17024 then
17025 Set_Has_Secondary_Private_View (N);
17026 end if;
17028 -- If it is a derivation of a private type in a context where no
17029 -- full view is needed, nothing to do either.
17031 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17032 null;
17034 -- Otherwise mark the type for flipping and set the full view on N2
17035 -- when available, which is necessary for Check_Private_View to swap
17036 -- back the views in case the full declaration of Typ is visible in
17037 -- the instantiation context. Note that this will be problematic if
17038 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17040 else
17041 Set_Has_Private_View (N);
17043 if Present (Full_View (Typ)) then
17044 if Comparison then
17045 Set_Compare_Type (N2, Full_View (Typ));
17046 else
17047 Set_Etype (N2, Full_View (Typ));
17048 end if;
17049 end if;
17050 end if;
17052 if Is_Floating_Point_Type (Typ)
17053 and then Has_Dimension_System (Typ)
17054 then
17055 Copy_Dimensions (N2, N);
17056 end if;
17057 end Set_Global_Type;
17059 -- Start of processing for Save_Global_References
17061 begin
17062 Gen_Scope := Current_Scope;
17064 -- If the generic unit is a child unit, references to entities in the
17065 -- parent are treated as local, because they will be resolved anew in
17066 -- the context of the instance of the parent.
17068 while Is_Child_Unit (Gen_Scope)
17069 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17070 loop
17071 Gen_Scope := Scope (Gen_Scope);
17072 end loop;
17074 Save_References (Templ);
17075 end Save_Global_References;
17077 ---------------------------------------
17078 -- Save_Global_References_In_Aspects --
17079 ---------------------------------------
17081 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17082 Asp : Node_Id;
17083 Expr : Node_Id;
17085 begin
17086 Asp := First (Aspect_Specifications (N));
17087 while Present (Asp) loop
17088 Expr := Expression (Asp);
17090 if Present (Expr) then
17091 Save_Global_References (Expr);
17092 end if;
17094 Next (Asp);
17095 end loop;
17096 end Save_Global_References_In_Aspects;
17098 ------------------------------------------
17099 -- Set_Copied_Sloc_For_Inherited_Pragma --
17100 ------------------------------------------
17102 procedure Set_Copied_Sloc_For_Inherited_Pragma
17103 (N : Node_Id;
17104 E : Entity_Id)
17106 begin
17107 Create_Instantiation_Source (N, E,
17108 Inlined_Body => False,
17109 Inherited_Pragma => True,
17110 Factor => S_Adjustment);
17111 end Set_Copied_Sloc_For_Inherited_Pragma;
17113 --------------------------------------
17114 -- Set_Copied_Sloc_For_Inlined_Body --
17115 --------------------------------------
17117 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
17118 begin
17119 Create_Instantiation_Source (N, E,
17120 Inlined_Body => True,
17121 Inherited_Pragma => False,
17122 Factor => S_Adjustment);
17123 end Set_Copied_Sloc_For_Inlined_Body;
17125 ---------------------
17126 -- Set_Instance_Of --
17127 ---------------------
17129 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
17130 begin
17131 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
17132 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
17133 Generic_Renamings.Increment_Last;
17134 end Set_Instance_Of;
17136 --------------------
17137 -- Set_Next_Assoc --
17138 --------------------
17140 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
17141 begin
17142 Generic_Renamings.Table (E).Next_In_HTable := Next;
17143 end Set_Next_Assoc;
17145 -------------------
17146 -- Start_Generic --
17147 -------------------
17149 procedure Start_Generic is
17150 begin
17151 -- ??? More things could be factored out in this routine.
17152 -- Should probably be done at a later stage.
17154 Generic_Flags.Append (Inside_A_Generic);
17155 Inside_A_Generic := True;
17157 Expander_Mode_Save_And_Set (False);
17158 end Start_Generic;
17160 ----------------------
17161 -- Set_Instance_Env --
17162 ----------------------
17164 -- WARNING: This routine manages SPARK regions
17166 procedure Set_Instance_Env
17167 (Gen_Unit : Entity_Id;
17168 Act_Unit : Entity_Id)
17170 Saved_AE : constant Boolean := Assertions_Enabled;
17171 Saved_CPL : constant Node_Id := Check_Policy_List;
17172 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
17173 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
17174 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
17176 begin
17177 -- Regardless of the current mode, predefined units are analyzed in the
17178 -- most current Ada mode, and earlier version Ada checks do not apply
17179 -- to predefined units. Nothing needs to be done for non-internal units.
17180 -- These are always analyzed in the current mode.
17182 if In_Internal_Unit (Gen_Unit) then
17184 -- The following call resets all configuration attributes to default
17185 -- or the xxx_Config versions of the attributes when the current sem
17186 -- unit is the main unit. At the same time, internal units must also
17187 -- inherit certain configuration attributes from their context. It
17188 -- is unclear what these two sets are.
17190 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17192 -- Reinstall relevant configuration attributes of the context
17194 Assertions_Enabled := Saved_AE;
17195 Check_Policy_List := Saved_CPL;
17196 Dynamic_Elaboration_Checks := Saved_DEC;
17198 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17199 end if;
17201 Current_Instantiated_Parent :=
17202 (Gen_Id => Gen_Unit,
17203 Act_Id => Act_Unit,
17204 Next_In_HTable => Assoc_Null);
17205 end Set_Instance_Env;
17207 -----------------
17208 -- Switch_View --
17209 -----------------
17211 procedure Switch_View (T : Entity_Id) is
17212 BT : constant Entity_Id := Base_Type (T);
17213 Priv_Elmt : Elmt_Id := No_Elmt;
17214 Priv_Sub : Entity_Id;
17216 begin
17217 -- T may be private but its base type may have been exchanged through
17218 -- some other occurrence, in which case there is nothing to switch
17219 -- besides T itself. Note that a private dependent subtype of a private
17220 -- type might not have been switched even if the base type has been,
17221 -- because of the last branch of Check_Private_View (see comment there).
17223 if not Is_Private_Type (BT) then
17224 Prepend_Elmt (Full_View (T), Exchanged_Views);
17225 Exchange_Declarations (T);
17226 return;
17227 end if;
17229 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17231 if Present (Full_View (BT)) then
17232 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17233 Exchange_Declarations (BT);
17234 end if;
17236 while Present (Priv_Elmt) loop
17237 Priv_Sub := Node (Priv_Elmt);
17239 if Present (Full_View (Priv_Sub)) then
17240 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17241 Exchange_Declarations (Priv_Sub);
17242 end if;
17244 Next_Elmt (Priv_Elmt);
17245 end loop;
17246 end Switch_View;
17248 -----------------
17249 -- True_Parent --
17250 -----------------
17252 function True_Parent (N : Node_Id) return Node_Id is
17253 begin
17254 if Nkind (Parent (N)) = N_Subunit then
17255 return Parent (Corresponding_Stub (Parent (N)));
17256 else
17257 return Parent (N);
17258 end if;
17259 end True_Parent;
17261 -----------------------------
17262 -- Valid_Default_Attribute --
17263 -----------------------------
17265 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17266 Attr_Id : constant Attribute_Id :=
17267 Get_Attribute_Id (Attribute_Name (Def));
17268 T : constant Entity_Id := Entity (Prefix (Def));
17269 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17270 F : Entity_Id;
17271 Num_F : Nat;
17272 OK : Boolean;
17274 begin
17275 if No (T) or else T = Any_Id then
17276 return;
17277 end if;
17279 Num_F := 0;
17280 F := First_Formal (Nam);
17281 while Present (F) loop
17282 Num_F := Num_F + 1;
17283 Next_Formal (F);
17284 end loop;
17286 case Attr_Id is
17287 when Attribute_Adjacent
17288 | Attribute_Ceiling
17289 | Attribute_Copy_Sign
17290 | Attribute_Floor
17291 | Attribute_Fraction
17292 | Attribute_Machine
17293 | Attribute_Model
17294 | Attribute_Remainder
17295 | Attribute_Rounding
17296 | Attribute_Unbiased_Rounding
17298 OK := Is_Fun
17299 and then Num_F = 1
17300 and then Is_Floating_Point_Type (T);
17302 when Attribute_Image
17303 | Attribute_Pred
17304 | Attribute_Succ
17305 | Attribute_Value
17306 | Attribute_Wide_Image
17307 | Attribute_Wide_Value
17309 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17311 when Attribute_Max
17312 | Attribute_Min
17314 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17316 when Attribute_Input =>
17317 OK := (Is_Fun and then Num_F = 1);
17319 when Attribute_Output
17320 | Attribute_Put_Image
17321 | Attribute_Read
17322 | Attribute_Write
17324 OK := not Is_Fun and then Num_F = 2;
17326 when others =>
17327 OK := False;
17328 end case;
17330 if not OK then
17331 Error_Msg_N
17332 ("attribute reference has wrong profile for subprogram", Def);
17333 end if;
17334 end Valid_Default_Attribute;
17336 ----------------------------------
17337 -- Validate_Formal_Type_Default --
17338 ----------------------------------
17340 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17341 Default : constant Node_Id :=
17342 Default_Subtype_Mark (Original_Node (Decl));
17343 Formal : constant Entity_Id := Defining_Identifier (Decl);
17345 Def_Sub : Entity_Id; -- Default subtype mark
17346 Type_Def : Node_Id;
17348 procedure Check_Discriminated_Formal;
17349 -- Check that discriminants of default for private or incomplete
17350 -- type match those of formal type.
17352 function Reference_Formal (N : Node_Id) return Traverse_Result;
17353 -- Check whether formal type definition mentions a previous formal
17354 -- type of the same generic.
17356 ----------------------
17357 -- Reference_Formal --
17358 ----------------------
17360 function Reference_Formal (N : Node_Id) return Traverse_Result is
17361 begin
17362 if Is_Entity_Name (N)
17363 and then Scope (Entity (N)) = Current_Scope
17364 then
17365 return Abandon;
17366 else
17367 return OK;
17368 end if;
17369 end Reference_Formal;
17371 function Depends_On_Other_Formals is
17372 new Traverse_Func (Reference_Formal);
17374 function Default_Subtype_Matches
17375 (Gen_T, Def_T : Entity_Id) return Boolean;
17377 procedure Validate_Array_Type_Default;
17378 -- Verify that dimension, indices, and component types of default
17379 -- are compatible with formal array type definition.
17381 procedure Validate_Derived_Type_Default;
17382 -- Verify that ancestor and progenitor types match.
17384 ---------------------------------
17385 -- Check_Discriminated_Formal --
17386 ---------------------------------
17388 procedure Check_Discriminated_Formal is
17389 Formal_Discr : Entity_Id;
17390 Actual_Discr : Entity_Id;
17391 Formal_Subt : Entity_Id;
17393 begin
17394 if Has_Discriminants (Formal) then
17395 if not Has_Discriminants (Def_Sub) then
17396 Error_Msg_NE
17397 ("default for & must have discriminants", Default, Formal);
17399 elsif Is_Constrained (Def_Sub) then
17400 Error_Msg_NE
17401 ("default for & must be unconstrained", Default, Formal);
17403 else
17404 Formal_Discr := First_Discriminant (Formal);
17405 Actual_Discr := First_Discriminant (Def_Sub);
17406 while Formal_Discr /= Empty loop
17407 if Actual_Discr = Empty then
17408 Error_Msg_N
17409 ("discriminants on Formal do not match formal",
17410 Default);
17411 end if;
17413 Formal_Subt := Etype (Formal_Discr);
17415 -- Access discriminants match if designated types do
17417 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17418 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17419 E_Anonymous_Access_Type
17420 and then
17421 Base_Type
17422 (Designated_Type (Base_Type (Formal_Subt))) =
17423 Base_Type
17424 (Designated_Type (Base_Type (Etype (Actual_Discr))))
17425 and then
17426 Subtypes_Statically_Match
17427 (Designated_Type (Base_Type (Formal_Subt)),
17428 Designated_Type (Base_Type (Etype (Actual_Discr))))
17429 then
17430 null;
17432 elsif Base_Type (Formal_Subt) /=
17433 Base_Type (Etype (Actual_Discr))
17434 then
17435 Error_Msg_N
17436 ("types of discriminants of default must match formal",
17437 Default);
17439 elsif not Subtypes_Statically_Match
17440 (Formal_Subt, Etype (Actual_Discr))
17441 and then Ada_Version >= Ada_95
17442 then
17443 Error_Msg_N
17444 ("subtypes of discriminants of default "
17445 & "must match formal",
17446 Default);
17447 end if;
17449 Next_Discriminant (Formal_Discr);
17450 Next_Discriminant (Actual_Discr);
17451 end loop;
17453 if Actual_Discr /= Empty then
17454 Error_Msg_NE
17455 ("discriminants on default do not match formal",
17456 Default, Formal);
17457 end if;
17458 end if;
17459 end if;
17460 end Check_Discriminated_Formal;
17462 ---------------------------
17463 -- Default_Subtype_Matches --
17464 ---------------------------
17466 function Default_Subtype_Matches
17467 (Gen_T, Def_T : Entity_Id) return Boolean
17469 begin
17470 -- Check that the base types, root types (when dealing with class
17471 -- wide types), or designated types (when dealing with anonymous
17472 -- access types) of Gen_T and Def_T are statically matching subtypes.
17474 return (Base_Type (Gen_T) = Base_Type (Def_T)
17475 and then Subtypes_Statically_Match (Gen_T, Def_T))
17477 or else (Is_Class_Wide_Type (Gen_T)
17478 and then Is_Class_Wide_Type (Def_T)
17479 and then Default_Subtype_Matches
17480 (Root_Type (Gen_T), Root_Type (Def_T)))
17482 or else (Is_Anonymous_Access_Type (Gen_T)
17483 and then Ekind (Def_T) = Ekind (Gen_T)
17484 and then Subtypes_Statically_Match
17485 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17487 end Default_Subtype_Matches;
17489 ----------------------------------
17490 -- Validate_Array_Type_Default --
17491 ----------------------------------
17493 procedure Validate_Array_Type_Default is
17494 I1, I2 : Node_Id;
17495 T2 : Entity_Id;
17496 begin
17497 if not Is_Array_Type (Def_Sub) then
17498 Error_Msg_NE ("default for& must be an array type ",
17499 Default, Formal);
17500 return;
17502 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17503 or else Is_Constrained (Def_Sub) /=
17504 Is_Constrained (Formal)
17505 then
17506 Error_Msg_NE ("default array type does not match&",
17507 Default, Formal);
17508 return;
17509 end if;
17511 I1 := First_Index (Formal);
17512 I2 := First_Index (Def_Sub);
17513 for J in 1 .. Number_Dimensions (Formal) loop
17515 -- If the indexes of the actual were given by a subtype_mark,
17516 -- the index was transformed into a range attribute. Retrieve
17517 -- the original type mark for checking.
17519 if Is_Entity_Name (Original_Node (I2)) then
17520 T2 := Entity (Original_Node (I2));
17521 else
17522 T2 := Etype (I2);
17523 end if;
17525 if not Subtypes_Statically_Match (Etype (I1), T2) then
17526 Error_Msg_NE
17527 ("index types of default do not match those of formal &",
17528 Default, Formal);
17529 end if;
17531 Next_Index (I1);
17532 Next_Index (I2);
17533 end loop;
17535 if not Default_Subtype_Matches
17536 (Component_Type (Formal), Component_Type (Def_Sub))
17537 then
17538 Error_Msg_NE
17539 ("component subtype of default does not match that of formal &",
17540 Default, Formal);
17541 end if;
17543 if Has_Aliased_Components (Formal)
17544 and then not Has_Aliased_Components (Default)
17545 then
17546 Error_Msg_NE
17547 ("default must have aliased components to match formal type &",
17548 Default, Formal);
17549 end if;
17550 end Validate_Array_Type_Default;
17552 -----------------------------------
17553 -- Validate_Derived_Type_Default --
17554 -----------------------------------
17556 procedure Validate_Derived_Type_Default is
17557 begin
17558 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17559 Error_Msg_NE ("default must be a descendent of&",
17560 Default, Etype (Formal));
17561 end if;
17563 if Has_Interfaces (Formal) then
17564 if not Has_Interfaces (Def_Sub) then
17565 Error_Msg_NE
17566 ("default must implement all interfaces of formal&",
17567 Default, Formal);
17569 else
17570 declare
17571 Iface : Node_Id;
17572 Iface_Ent : Entity_Id;
17574 begin
17575 Iface := First (Abstract_Interface_List (Formal));
17577 while Present (Iface) loop
17578 Iface_Ent := Entity (Iface);
17580 if Is_Ancestor (Iface_Ent, Def_Sub)
17581 or else Is_Progenitor (Iface_Ent, Def_Sub)
17582 then
17583 null;
17585 else
17586 Error_Msg_NE
17587 ("Default must implement interface&",
17588 Default, Etype (Iface));
17589 end if;
17591 Next (Iface);
17592 end loop;
17593 end;
17594 end if;
17595 end if;
17596 end Validate_Derived_Type_Default;
17598 -- Start of processing for Validate_Formal_Type_Default
17600 begin
17601 Analyze (Default);
17602 if not Is_Entity_Name (Default)
17603 or else not Is_Type (Entity (Default))
17604 then
17605 Error_Msg_N
17606 ("Expect type name for default of formal type", Default);
17607 return;
17608 else
17609 Def_Sub := Entity (Default);
17610 end if;
17612 -- Formal derived_type declarations are transformed into full
17613 -- type declarations or Private_Type_Extensions for ease of processing.
17615 if Nkind (Decl) = N_Full_Type_Declaration then
17616 Type_Def := Type_Definition (Decl);
17618 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17619 Type_Def := Subtype_Indication (Decl);
17621 else
17622 Type_Def := Formal_Type_Definition (Decl);
17623 end if;
17625 if Depends_On_Other_Formals (Type_Def) = Abandon
17626 and then Scope (Def_Sub) /= Current_Scope
17627 then
17628 Error_Msg_N ("default of formal type that depends on "
17629 & "other formals must be a previous formal type", Default);
17630 return;
17632 elsif Def_Sub = Formal then
17633 Error_Msg_N
17634 ("default for formal type cannot be formal itsef", Default);
17635 return;
17636 end if;
17638 case Nkind (Type_Def) is
17640 when N_Formal_Private_Type_Definition =>
17641 if (Is_Abstract_Type (Formal)
17642 and then not Is_Abstract_Type (Def_Sub))
17643 or else (Is_Limited_Type (Formal)
17644 and then not Is_Limited_Type (Def_Sub))
17645 then
17646 Error_Msg_NE
17647 ("default for private type$ does not match",
17648 Default, Formal);
17649 end if;
17651 Check_Discriminated_Formal;
17653 when N_Formal_Derived_Type_Definition =>
17654 Check_Discriminated_Formal;
17655 Validate_Derived_Type_Default;
17657 when N_Formal_Incomplete_Type_Definition =>
17658 if Is_Tagged_Type (Formal)
17659 and then not Is_Tagged_Type (Def_Sub)
17660 then
17661 Error_Msg_NE
17662 ("default for & must be a tagged type", Default, Formal);
17663 end if;
17665 Check_Discriminated_Formal;
17667 when N_Formal_Discrete_Type_Definition =>
17668 if not Is_Discrete_Type (Def_Sub) then
17669 Error_Msg_NE ("default for& must be a discrete type",
17670 Default, Formal);
17671 end if;
17673 when N_Formal_Signed_Integer_Type_Definition =>
17674 if not Is_Integer_Type (Def_Sub) then
17675 Error_Msg_NE ("default for& must be a discrete type",
17676 Default, Formal);
17677 end if;
17679 when N_Formal_Modular_Type_Definition =>
17680 if not Is_Modular_Integer_Type (Def_Sub) then
17681 Error_Msg_NE ("default for& must be a modular_integer Type",
17682 Default, Formal);
17683 end if;
17685 when N_Formal_Floating_Point_Definition =>
17686 if not Is_Floating_Point_Type (Def_Sub) then
17687 Error_Msg_NE ("default for& must be a floating_point type",
17688 Default, Formal);
17689 end if;
17691 when N_Formal_Ordinary_Fixed_Point_Definition =>
17692 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17693 Error_Msg_NE ("default for& must be "
17694 & "an ordinary_fixed_point type ",
17695 Default, Formal);
17696 end if;
17698 when N_Formal_Decimal_Fixed_Point_Definition =>
17699 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17700 Error_Msg_NE ("default for& must be "
17701 & "an Decimal_fixed_point type ",
17702 Default, Formal);
17703 end if;
17705 when N_Array_Type_Definition =>
17706 Validate_Array_Type_Default;
17708 when N_Access_Function_Definition |
17709 N_Access_Procedure_Definition =>
17710 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17711 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17712 Default, Formal);
17713 end if;
17714 Check_Subtype_Conformant
17715 (Designated_Type (Formal), Designated_Type (Def_Sub));
17717 when N_Access_To_Object_Definition =>
17718 if not Is_Access_Object_Type (Def_Sub) then
17719 Error_Msg_NE ("default for& must be an Access_To_Object",
17720 Default, Formal);
17722 elsif not Default_Subtype_Matches
17723 (Designated_Type (Formal), Designated_Type (Def_Sub))
17724 then
17725 Error_Msg_NE ("designated type of defaul does not match "
17726 & "designated type of formal type",
17727 Default, Formal);
17728 end if;
17730 when N_Record_Definition => -- Formal interface type
17731 if not Is_Interface (Def_Sub) then
17732 Error_Msg_NE
17733 ("default for formal interface type must be an interface",
17734 Default, Formal);
17736 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17737 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17738 or else Is_Protected_Interface (Formal) /=
17739 Is_Protected_Interface (Def_Sub)
17740 or else Is_Synchronized_Interface (Formal) /=
17741 Is_Synchronized_Interface (Def_Sub)
17742 then
17743 Error_Msg_NE
17744 ("default for interface& does not match", Def_Sub, Formal);
17745 end if;
17747 when N_Derived_Type_Definition =>
17748 Validate_Derived_Type_Default;
17750 when N_Identifier => -- case of a private extension
17751 Validate_Derived_Type_Default;
17753 when N_Error =>
17754 null;
17756 when others =>
17757 raise Program_Error;
17758 end case;
17759 end Validate_Formal_Type_Default;
17760 end Sem_Ch12;